fix: give for statement bodies a higher `dynamic` precedence to avoid parse errors

pull/708/head
Amaan Qureshi 2024-02-18 07:44:17 +07:00
parent 3d8d510a79
commit 1ef1791b3c
2 changed files with 19 additions and 2 deletions

@ -84,6 +84,8 @@ module.exports = grammar(C, {
[$._function_declarator_seq],
[$._type_specifier, $.sized_type_specifier],
[$.initializer_pair, $.comma_expression],
[$.expression_statement, $._for_statement_body],
[$.init_statement, $._for_statement_body],
],
inline: ($, original) => original.concat([
@ -821,7 +823,10 @@ module.exports = grammar(C, {
optional(field('alternative', $.else_clause)),
)),
_for_statement_body: ($, original) => prec(1, original),
// Using prec(1) instead of prec.dynamic(1) causes issues with the
// range loop's declaration specifiers if `int` is passed in, it'll
// always prefer the standard for loop and give us a parse error.
_for_statement_body: ($, original) => prec.dynamic(1, original),
for_range_loop: $ => seq(
'for',
'(',

@ -44,6 +44,8 @@ T main() {
for (using elem_t = T::value_type; elem_t i : v) {
cout << --n + i << ' ';
}
for (int x; int v : {1}) {}
}
--------------------------------------------------------------------------------
@ -140,7 +142,17 @@ T main() {
argument: (identifier))
right: (identifier)))
right: (char_literal
(character)))))))))
(character))))))
(for_range_loop
initializer: (init_statement
(declaration
type: (primitive_type)
declarator: (identifier)))
type: (primitive_type)
declarator: (identifier)
right: (initializer_list
(number_literal))
body: (compound_statement)))))
================================================================================
Constexpr if statements