Allow structured binding declarators in pmore places

Refs atom/language-c#325
edge_only_predecessors
Max Brunsfeld 2019-05-08 15:51:47 +07:00
parent 966a467029
commit df7bc44e33
4 changed files with 175587 additions and 176208 deletions

@ -656,27 +656,44 @@ auto [a] = B{};
int main() { int main() {
auto &&[b, c] = std::make_tuple(c); auto &&[b, c] = std::make_tuple(c);
const auto [x, y] {1, 2}; const auto [x, y] {1, 2};
for (const auto &[a, b] : c) {}
} }
--- ---
(translation_unit (translation_unit
(structured_binding_declaration (auto) (structured_binding_declarator (identifier)) (compound_literal_expression (type_identifier) (initializer_list))) (declaration
(auto)
(init_declarator
(structured_binding_declarator (identifier))
(compound_literal_expression (type_identifier) (initializer_list))))
(function_definition (function_definition
(primitive_type) (primitive_type)
(function_declarator (identifier) (parameter_list)) (function_declarator (identifier) (parameter_list))
(compound_statement (compound_statement
(structured_binding_declaration (declaration
(auto) (auto)
(reference_declarator (structured_binding_declarator (identifier) (identifier))) (init_declarator
(call_expression (reference_declarator (structured_binding_declarator (identifier) (identifier)))
(scoped_identifier (namespace_identifier) (identifier)) (call_expression
(argument_list (identifier)))) (scoped_identifier (namespace_identifier) (identifier))
(structured_binding_declaration (argument_list (identifier)))))
(declaration
(type_qualifier) (type_qualifier)
(auto) (auto)
(structured_binding_declarator (identifier) (identifier)) (init_declarator
(initializer_list (number_literal) (number_literal)))))) (structured_binding_declarator (identifier) (identifier))
(initializer_list (number_literal) (number_literal))))
(for_range_loop
(for_range_declaration
(type_qualifier)
(auto)
(reference_declarator (structured_binding_declarator
(identifier)
(identifier))))
(identifier)
(compound_statement)))))
========================== ==========================
Constexpr declarations Constexpr declarations

@ -22,8 +22,9 @@ module.exports = grammar(C, {
[$.scoped_type_identifier, $.scoped_field_identifier], [$.scoped_type_identifier, $.scoped_field_identifier],
[$.comma_expression, $.initializer_list], [$.comma_expression, $.initializer_list],
[$._type_specifier, $.optional_type_parameter_declaration], [$._type_specifier, $.optional_type_parameter_declaration],
[$._type_specifier, $._declarator, $._expression], [$._expression, $._declarator],
[$._declarator, $._expression], [$._expression, $.structured_binding_declarator],
[$._expression, $._declarator, $._type_specifier],
[$.parameter_list, $.argument_list], [$.parameter_list, $.argument_list],
]), ]),
@ -40,7 +41,7 @@ module.exports = grammar(C, {
$.alias_declaration, $.alias_declaration,
$.template_declaration, $.template_declaration,
$.template_instantiation, $.template_instantiation,
$.structured_binding_declaration, // $.structured_binding_declaration,
alias($.constructor_or_destructor_definition, $.function_definition) alias($.constructor_or_destructor_definition, $.function_definition)
), ),
@ -149,19 +150,19 @@ module.exports = grammar(C, {
original original
), ),
structured_binding_declaration: $ => seq( // structured_binding_declaration: $ => seq(
$._declaration_specifiers, // $._declaration_specifiers,
choice( // choice(
alias($.structured_binding_reference_declarator, $.reference_declarator), // alias($.structured_binding_reference_declarator, $.reference_declarator),
$.structured_binding_declarator, // $.structured_binding_declarator,
), // ),
choice( // choice(
seq('=', choice($.initializer_list, $._expression)), // seq('=', choice($.initializer_list, $._expression)),
$.initializer_list, // $.initializer_list,
$.argument_list // $.argument_list
), // ),
';' // ';'
), // ),
template_declaration: $ => seq( template_declaration: $ => seq(
'template', 'template',
@ -361,9 +362,16 @@ module.exports = grammar(C, {
$.scoped_identifier, $.scoped_identifier,
$.template_function, $.template_function,
$.operator_name, $.operator_name,
$.destructor_name $.destructor_name,
$.structured_binding_declarator
), ),
// structured_declarator: $ => seq(
// '[',
// commaSep1($.identifier),
// ']'
// ),
_field_declarator: ($, original) => choice( _field_declarator: ($, original) => choice(
original, original,
alias($.reference_field_declarator, $.reference_declarator), alias($.reference_field_declarator, $.reference_declarator),
@ -380,7 +388,7 @@ module.exports = grammar(C, {
reference_field_declarator: $ => prec.dynamic(1, prec.right(seq(choice('&', '&&'), $._field_declarator))), reference_field_declarator: $ => prec.dynamic(1, prec.right(seq(choice('&', '&&'), $._field_declarator))),
abstract_reference_declarator: $ => prec.right(seq(choice('&', '&&'), optional($._abstract_declarator))), abstract_reference_declarator: $ => prec.right(seq(choice('&', '&&'), optional($._abstract_declarator))),
structured_binding_reference_declarator: $ => seq(choice('&', '&&'), $.structured_binding_declarator), // structured_binding_reference_declarator: $ => seq(choice('&', '&&'), $.structured_binding_declarator),
structured_binding_declarator: $ => seq('[', commaSep1($.identifier), ']'), structured_binding_declarator: $ => seq('[', commaSep1($.identifier), ']'),
function_declarator: ($, original) => seq( function_declarator: ($, original) => seq(

108
src/grammar.json vendored

@ -85,10 +85,6 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "template_instantiation" "name": "template_instantiation"
}, },
{
"type": "SYMBOL",
"name": "structured_binding_declaration"
},
{ {
"type": "ALIAS", "type": "ALIAS",
"content": { "content": {
@ -1022,6 +1018,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "destructor_name" "name": "destructor_name"
},
{
"type": "SYMBOL",
"name": "structured_binding_declarator"
} }
] ]
}, },
@ -5316,72 +5316,6 @@
] ]
} }
}, },
"structured_binding_declaration": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_declaration_specifiers"
},
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "structured_binding_reference_declarator"
},
"named": true,
"value": "reference_declarator"
},
{
"type": "SYMBOL",
"name": "structured_binding_declarator"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "initializer_list"
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
]
},
{
"type": "SYMBOL",
"name": "initializer_list"
},
{
"type": "SYMBOL",
"name": "argument_list"
}
]
},
{
"type": "STRING",
"value": ";"
}
]
},
"template_declaration": { "template_declaration": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -6088,28 +6022,6 @@
] ]
} }
}, },
"structured_binding_reference_declarator": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "&"
},
{
"type": "STRING",
"value": "&&"
}
]
},
{
"type": "SYMBOL",
"name": "structured_binding_declarator"
}
]
},
"structured_binding_declarator": { "structured_binding_declarator": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -7239,13 +7151,17 @@
"optional_type_parameter_declaration" "optional_type_parameter_declaration"
], ],
[ [
"_type_specifier", "_expression",
"_declarator", "_declarator"
"_expression" ],
[
"_expression",
"structured_binding_declarator"
], ],
[ [
"_expression",
"_declarator", "_declarator",
"_expression" "_type_specifier"
], ],
[ [
"parameter_list", "parameter_list",

351608
src/parser.c vendored

File diff suppressed because it is too large Load Diff