Thread overview
need examples to understand postfix expression grammar
Apr 16, 2023
FabArd
Apr 16, 2023
Timon Gehr
Apr 16, 2023
Bastiaan Veelo
Apr 19, 2023
FabArd
Apr 19, 2023
Ali Çehreli
Apr 20, 2023
FabArd
April 16, 2023

Hi,

I have trouble understanding these two rules of grammar :

PostfixExpression:

  • PostfixExpression ( ArgumentListopt )
  • TypeCtorsopt BasicType ( ArgumentListopt )

It seems to me that the first rule corresponds to a function call expression.
Is my analysis correct?

As for the second one, I don't understand how to use it.
Can you give me examples of how to use it?

April 16, 2023
On 4/16/23 18:59, FabArd wrote:
> Hi,
> 
> I have trouble understanding these two rules of grammar :
> 
> PostfixExpression:
> - PostfixExpression ( ArgumentListopt )
> - TypeCtorsopt BasicType ( ArgumentListopt )
> 
> It seems to me that the first rule corresponds to a function call expression.
> Is my analysis correct?
> ...

Yes.

> As for the second one, I don't understand how to use it.
> Can you give me examples of how to use it?

It's for conversion to the specified basic type (including qualified ones).

E.g., this works

---
void main(){
    auto x = immutable double(2);
    static assert(is(typeof(x)==immutable double));
    assert(x==2);
}
---

TBH I think it would be better if basic types were just expressions in their own right, but the parser currently is what it is, so atm this requires a specialized grammar rule.
April 16, 2023

On Sunday, 16 April 2023 at 16:59:21 UTC, FabArd wrote:

>

Hi,

I have trouble understanding these two rules of grammar :

PostfixExpression:

  • PostfixExpression ( ArgumentListopt )
  • TypeCtorsopt BasicType ( ArgumentListopt )

It seems to me that the first rule corresponds to a function call expression.
Is my analysis correct?

I think it is.

>

As for the second one, I don't understand how to use it.
Can you give me examples of how to use it?

I think double(2) would be an example, as in

assert(1 / double(2) == 0.5);

-- Bastiaan.

April 19, 2023

Thank you for your examples.

I did some tests based on your examples and I found that you can also assign a type to an auto variable:

void main(){
   auto x = double();
   x = 2;
   static assert(is(typeof(x) == double));
   assert(x == 2);
}

Another example with a qualified identifier:

class Test {
    enum nb : int {
        one = 1
    }
}
void main(){
   auto d = Test.nb();
   static assert(is(typeof(d) == Test.nb));
}

It seems that the grammar rule could be written more precisely:

PostfixExpression:

  • PostfixExpression ( ArgumentListopt )
  • TypeCtors BasicType ( AssignExpressionopt )

Indeed the type conversion takes only one or no argument but never a list.

April 19, 2023
On 4/19/23 08:40, FabArd wrote:

> assign a type to an auto variable:
[...]
>     auto x = double();

A couple of perhaps-too-pedantic corrections:

1) "auto variable" is nothing special. D has type inference and 'auto' is just a place holder in your example. Otherwise, type inference is in effect anyway:

   auto a = double();
   const b = double();
   immutable c = double();
   shared d = double();
   static e = double();
   @nogc f = double();
   nothrow g = double();
   pure h = double();

2) The right hand side is not a type but an expression: double() is the construction of a double value of double.init.

Ali

April 20, 2023
I am trying to put the D grammar rules into tree-sitter in order to understand how it works and to deepen my knowledge of the D language.

So how to determine the following expressions:

1   Window window = Platform.instance.createWindow("Bezier curves", 300, 500);
2   window.show();
3   return Platform.instance.enterMessageLoop();
4   Point sz = font.textSize(text);

My problem is that all these expressions are interpreted with "type conversion" expression rule and not function call expression rule.

How to distinguish a function call expression from a "type conversion" expression?
Knowing that the two rules can be applied to all these expressions.

To solve part of my problem a solution would be to be able to restrict a rule to its strict minimum. This is why I made the previous proposal.

Is it correct to restrict this rule as I propose?
If possible, line 1 of the above examples becomes a function call expression.
If not can you give me examples of type conversion expression with a list of arguments.

Would there be a safe way to determine a function call expression from a type conversion expression?

Thanks