Skip to content

Commit

Permalink
chore: add fields to update expression
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jan 31, 2024
1 parent ab2d6e2 commit b0819d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
14 changes: 8 additions & 6 deletions common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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), ')',
Expand Down
28 changes: 18 additions & 10 deletions common/test/corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))))))

==============================
Expand All @@ -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)))))

==============================
Expand Down Expand Up @@ -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)))))
Expand Down

0 comments on commit b0819d3

Please sign in to comment.