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

Fix multi-statements in for initializer and update parts #27

Merged
merged 2 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ int main() {

for (start(); check(); step())
3;

for (i = 0, j = 0, k = 0, l = 0; i < 1; i++, j++, k++, l++)
1;
}

---
Expand All @@ -56,14 +59,24 @@ int main() {
(for_statement
(declaration (primitive_type) (init_declarator (identifier) (number_literal)))
(binary_expression (identifier) (number_literal))
(comma_expression
(call_expression (identifier) (argument_list))
(update_expression (identifier)))
(call_expression (identifier) (argument_list))
(update_expression (identifier))
(compound_statement (expression_statement (number_literal))))
(for_statement
(call_expression (identifier) (argument_list))
(call_expression (identifier) (argument_list))
(call_expression (identifier) (argument_list))
(expression_statement (number_literal)))
(for_statement
(assignment_expression (identifier) (number_literal))
(assignment_expression (identifier) (number_literal))
(assignment_expression (identifier) (number_literal))
(assignment_expression (identifier) (number_literal))
(binary_expression (identifier) (number_literal))
(update_expression (identifier))
(update_expression (identifier))
(update_expression (identifier))
(update_expression (identifier))
(expression_statement (number_literal))))))

============================================
Expand Down
4 changes: 2 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,10 @@ module.exports = grammar({
'(',
choice(
field('initializer', $.declaration),
seq(field('initializer', optional($._expression)), ';')
seq(field('initializer', commaSep($._expression)), ';')
),
field('condition', optional($._expression)), ';',
field('update', optional(choice($._expression, $.comma_expression))),
field('update', commaSep($._expression)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did comma_expression not work here? I would have thought it would work. I would also think that it would work for the initializer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right: I misread the recursive comma_expression.
And I thought it was better to have a "list" of initiliazers but when dumping the ast from clang, ',' is really a bin operator.

echo 'void f() {
    int i, j, k, l;
    for (i = 0, j = 0, k = 0, l = 0; i < 1; i++, j++, k++, l++) { }
}' | clang-9 -Xclang -ast-dump -x c -c -

So I'll update my patch.

')',
$._statement
),
Expand Down
43 changes: 38 additions & 5 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2890,8 +2890,29 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
}
]
},
{
"type": "BLANK"
Expand Down Expand Up @@ -2934,15 +2955,27 @@
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "SYMBOL",
"name": "comma_expression"
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
}
]
},
Expand Down
10 changes: 7 additions & 3 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": ",",
"named": false
},
{
"type": "_expression",
"named": true
Expand All @@ -1232,11 +1236,11 @@
"required": false,
"types": [
{
"type": "_expression",
"named": true
"type": ",",
"named": false
},
{
"type": "comma_expression",
"type": "_expression",
"named": true
}
]
Expand Down
Loading