diff --git a/common/define-grammar.js b/common/define-grammar.js index e2079ca4..d665bc45 100644 --- a/common/define-grammar.js +++ b/common/define-grammar.js @@ -997,12 +997,14 @@ module.exports = function defineGrammar(dialect) { $._variable_name, ), - update_expression: $ => prec.left(PREC.INC, choice( - seq($._variable, '++'), - seq($._variable, '--'), - seq('++', $._variable), - seq('--', $._variable), - )), + update_expression: $ => { + const argument = field('argument', $._variable); + const operator = field('operator', choice('--', '++')); + return prec.left(PREC.INC, choice( + seq(operator, argument), + seq(argument, operator), + )); + }, cast_expression: $ => prec(PREC.CAST, seq( '(', field('type', $.cast_type), ')', diff --git a/common/test/corpus/statements.txt b/common/test/corpus/statements.txt index 7e2e7024..38f085d4 100644 --- a/common/test/corpus/statements.txt +++ b/common/test/corpus/statements.txt @@ -103,7 +103,7 @@ while ($a < 10) { right: (integer))) body: (compound_statement (echo_statement (variable_name (name))) - (expression_statement (update_expression (variable_name (name))))))) + (expression_statement (update_expression argument: (variable_name (name))))))) ============================== Alternative while statements @@ -125,13 +125,13 @@ endwhile; condition: (parenthesized_expression (binary_expression left: (variable_name (name)) right: (integer))) - body: (echo_statement (update_expression (variable_name (name))))) + body: (echo_statement (update_expression argument: (variable_name (name))))) (while_statement condition: (parenthesized_expression (binary_expression left: (variable_name (name)) right: (integer))) body: (colon_block - (echo_statement (update_expression (variable_name (name)))) + (echo_statement (update_expression argument: (variable_name (name)))) (echo_statement (variable_name (name)))))) ============================== @@ -150,14 +150,22 @@ endfor; (program (php_tag) (for_statement - (assignment_expression (variable_name (name)) (integer)) - (binary_expression (variable_name (name)) (integer)) - (update_expression (variable_name (name))) + (assignment_expression + left: (variable_name (name)) + right: (integer)) + (binary_expression + left: (variable_name (name)) + right: (integer)) + (update_expression argument: (variable_name (name))) (echo_statement (variable_name (name)))) (for_statement - (assignment_expression (variable_name (name)) (integer)) - (binary_expression (variable_name (name)) (integer)) - (update_expression (variable_name (name))) + (assignment_expression + left: (variable_name (name)) + right: (integer)) + (binary_expression + left: (variable_name (name)) + right: (integer)) + (update_expression argument: (variable_name (name))) (echo_statement (variable_name (name))))) ============================== @@ -260,7 +268,7 @@ do { (do_statement body: (compound_statement (echo_statement (variable_name (name))) - (expression_statement (update_expression (variable_name (name))))) + (expression_statement (update_expression argument: (variable_name (name))))) condition: (parenthesized_expression (binary_expression left: (variable_name (name)) right: (integer)))))