Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign children name #101

Open
stevefan1999-personal opened this issue Oct 17, 2023 · 4 comments
Open

Assign children name #101

stevefan1999-personal opened this issue Oct 17, 2023 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@stevefan1999-personal
Copy link
Contributor

Wouldn't it be nice if we could do:

		for_statement -> 'for'! '('! initial=(expression_statement | declaration) condition=expression_statement post_operation=expression? ')'! body=statement;

Instead of:

		for_statement_initial -> expression_statement | declaration;
		for_statement_condition -> expression_statement;
		for_statement_post_operation -> expression;
		for_statement_body -> statement;

		for_statement -> 'for'! '('! for_statement_initial for_statement_condition for_statement_post_operation? ')'! for_statement_body;
@woutersl woutersl self-assigned this Oct 18, 2023
@woutersl woutersl added the question Further information is requested label Oct 18, 2023
@woutersl
Copy link
Member

It looks like you want sub rules, you can write this:

for_statement -> 'for'! '('!
    {expression_statement | declaration}
    {expression_statement} 
    {expression}?
')'! {statement};

This produces the same AST as the fully expanded form, except that the nodes for the sub rules are anonymous, i.e.:

for_statement
+-> <epsilon>
|       +-> expression_statement
+-> <epsilon>
|       +-> expression_statement
+-> <epsilon>
+-> <epsilon>
        +-> statement

If you want names on the sub rules, then in the current state you indeed have to fully expand the grammar.

IMO the expanded version is clearer than a more compact version, but YMMV.

@woutersl
Copy link
Member

woutersl commented Oct 18, 2023

I forgot that you can also use virtual symbols and promote them if that's what you want :

for_statement -> 'for'! '('!
    {"initial"^ (expression_statement | declaration)}
    {"condition"^ expression_statement} 
    {"post_operation"^ expression?}
')'! {"body"^ statement};

will give you this:

for_statement
+-> "initial"
|       +-> expression_statement
+-> "condition"
|       +-> expression_statement
+-> "post_operation"
+-> "body"
        +-> statement

@woutersl
Copy link
Member

If something like this is desirable:

for_statement -> 'for'! '('!
    initial=(expression_statement | declaration)
    condition=expression_statement
    post_operation=expression?
')'! body=statement;

It would be best to leverage the existing syntax for sub rules, expanded to look like this:

for_statement -> 'for'! '('!
    initial={expression_statement | declaration}
    condition={expression_statement}
    post_operation={expression?}
')'! body={statement};

The output AST would be the same:

for_statement
+-> "initial"
|       +-> expression_statement
+-> "condition"
|       +-> expression_statement
+-> "post_operation"
+-> "body"
        +-> statement

@stevefan1999-personal
Copy link
Contributor Author

If something like this is desirable:

for_statement -> 'for'! '('!
    initial=(expression_statement | declaration)
    condition=expression_statement
    post_operation=expression?
')'! body=statement;

It would be best to leverage the existing syntax for sub rules, expanded to look like this:

for_statement -> 'for'! '('!
    initial={expression_statement | declaration}
    condition={expression_statement}
    post_operation={expression?}
')'! body={statement};

The output AST would be the same:

for_statement
+-> "initial"
|       +-> expression_statement
+-> "condition"
|       +-> expression_statement
+-> "post_operation"
+-> "body"
        +-> statement

This would be awesome as I could leverage these information for AST building

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants