added field names to grammar
This commit is contained in:
142
grammar.js
142
grammar.js
@@ -26,7 +26,12 @@ module.exports = grammar({
|
|||||||
translation_entity: ($) => choice($.declaration, $.function_definition),
|
translation_entity: ($) => choice($.declaration, $.function_definition),
|
||||||
|
|
||||||
function_definition: ($) =>
|
function_definition: ($) =>
|
||||||
seq("function", $.identifier, $.function_signature, $.statement_block),
|
seq(
|
||||||
|
"function",
|
||||||
|
field("name", $.identifier),
|
||||||
|
field("signature", $.function_signature),
|
||||||
|
field("body", $.statement_block),
|
||||||
|
),
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
// Comments
|
// Comments
|
||||||
@@ -129,17 +134,21 @@ module.exports = grammar({
|
|||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
base_expression: ($) =>
|
base_expression: ($) =>
|
||||||
choice($.identifier, $.constant, seq("(", $.expression, ")")),
|
choice(
|
||||||
|
$.identifier,
|
||||||
|
$.constant,
|
||||||
|
seq("(", field("expression", $.expression), ")"),
|
||||||
|
),
|
||||||
|
|
||||||
// postfix_expression (Bison: left-recursive) -> base_expression plus a
|
// postfix_expression (Bison: left-recursive) -> base_expression plus a
|
||||||
// sequence of postfix operations.
|
// sequence of postfix operations.
|
||||||
postfix_expression: ($) =>
|
postfix_expression: ($) =>
|
||||||
seq(
|
seq(
|
||||||
$.base_expression,
|
field("base", $.base_expression),
|
||||||
repeat(
|
repeat(
|
||||||
choice(
|
choice(
|
||||||
seq("[", $.expression, "]"), // subscript
|
seq("[", field("index", $.expression), "]"), // subscript
|
||||||
seq("(", optional($.argument_expression_list), ")"), // call
|
seq("(", optional(field("arguments", $.argument_expression_list)), ")"), // call
|
||||||
"++", // post-inc
|
"++", // post-inc
|
||||||
"--", // post-dec
|
"--", // post-dec
|
||||||
),
|
),
|
||||||
@@ -153,38 +162,46 @@ module.exports = grammar({
|
|||||||
unary_expression: ($) =>
|
unary_expression: ($) =>
|
||||||
choice(
|
choice(
|
||||||
$.postfix_expression,
|
$.postfix_expression,
|
||||||
seq("++", $.unary_expression),
|
seq("++", field("operand", $.unary_expression)),
|
||||||
seq("--", $.unary_expression),
|
seq("--", field("operand", $.unary_expression)),
|
||||||
seq("+", $.unary_expression),
|
seq("+", field("operand", $.unary_expression)),
|
||||||
seq("-", $.unary_expression),
|
seq("-", field("operand", $.unary_expression)),
|
||||||
seq("!", $.unary_expression),
|
seq("!", field("operand", $.unary_expression)),
|
||||||
),
|
),
|
||||||
|
|
||||||
cast_expression: ($) =>
|
cast_expression: ($) =>
|
||||||
choice(
|
choice(
|
||||||
$.unary_expression,
|
$.unary_expression,
|
||||||
seq("(", $.type_specifier_qualifier, ")", $.cast_expression),
|
seq(
|
||||||
|
"(",
|
||||||
|
field("type", $.type_specifier_qualifier),
|
||||||
|
")",
|
||||||
|
field("value", $.cast_expression),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
// multiplicative_expression: cast_expression (( * | / ) cast_expression)*
|
// multiplicative_expression: cast_expression (( * | / ) cast_expression)*
|
||||||
multiplicative_expression: ($) =>
|
multiplicative_expression: ($) =>
|
||||||
seq($.cast_expression, repeat(seq(choice("*", "/"), $.cast_expression))),
|
seq(
|
||||||
|
field("left", $.cast_expression),
|
||||||
|
repeat(seq(choice("*", "/"), field("right", $.cast_expression))),
|
||||||
|
),
|
||||||
|
|
||||||
// additive_expression: multiplicative_expression (( + | - ) multiplicative_expression)*
|
// additive_expression: multiplicative_expression (( + | - ) multiplicative_expression)*
|
||||||
additive_expression: ($) =>
|
additive_expression: ($) =>
|
||||||
seq(
|
seq(
|
||||||
$.multiplicative_expression,
|
field("left", $.multiplicative_expression),
|
||||||
repeat(seq(choice("+", "-"), $.multiplicative_expression)),
|
repeat(seq(choice("+", "-"), field("right", $.multiplicative_expression))),
|
||||||
),
|
),
|
||||||
|
|
||||||
// relational_expression: additive_expression ( (< | > | <= | =< | >= | => ) additive_expression )*
|
// relational_expression: additive_expression ( (< | > | <= | =< | >= | => ) additive_expression )*
|
||||||
relational_expression: ($) =>
|
relational_expression: ($) =>
|
||||||
seq(
|
seq(
|
||||||
$.additive_expression,
|
field("left", $.additive_expression),
|
||||||
repeat(
|
repeat(
|
||||||
seq(
|
seq(
|
||||||
choice("<", ">", $.leq_operator, $.geq_operator),
|
choice("<", ">", $.leq_operator, $.geq_operator),
|
||||||
$.additive_expression,
|
field("right", $.additive_expression),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -192,15 +209,15 @@ module.exports = grammar({
|
|||||||
// equality_expression: relational_expression ( (== | !=) relational_expression )*
|
// equality_expression: relational_expression ( (== | !=) relational_expression )*
|
||||||
equality_expression: ($) =>
|
equality_expression: ($) =>
|
||||||
seq(
|
seq(
|
||||||
$.relational_expression,
|
field("left", $.relational_expression),
|
||||||
repeat(seq(choice("==", "!="), $.relational_expression)),
|
repeat(seq(choice("==", "!="), field("right", $.relational_expression))),
|
||||||
),
|
),
|
||||||
|
|
||||||
// logical_expression: equality_expression ( (&& ||) equality_expression )*
|
// logical_expression: equality_expression ( (&& ||) equality_expression )*
|
||||||
logical_expression: ($) =>
|
logical_expression: ($) =>
|
||||||
seq(
|
seq(
|
||||||
$.equality_expression,
|
field("left", $.equality_expression),
|
||||||
repeat(seq(choice("&&", "||"), $.equality_expression)),
|
repeat(seq(choice("&&", "||"), field("right", $.equality_expression))),
|
||||||
),
|
),
|
||||||
|
|
||||||
// assignment_expression:
|
// assignment_expression:
|
||||||
@@ -210,9 +227,9 @@ module.exports = grammar({
|
|||||||
choice(
|
choice(
|
||||||
$.logical_expression,
|
$.logical_expression,
|
||||||
seq(
|
seq(
|
||||||
$.unary_expression,
|
field("left", $.unary_expression),
|
||||||
choice("=", "+=", "-=", "*=", "/="),
|
field("operator", choice("=", "+=", "-=", "*=", "/=")),
|
||||||
$.assignment_expression,
|
field("right", $.assignment_expression),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
@@ -224,7 +241,12 @@ module.exports = grammar({
|
|||||||
// Types / declarations (port of Bison decl/type rules)
|
// Types / declarations (port of Bison decl/type rules)
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
declaration: ($) => seq($.declaration_specifier, $.init_declarator, ";"),
|
declaration: ($) =>
|
||||||
|
seq(
|
||||||
|
field("type", $.declaration_specifier),
|
||||||
|
field("declarator", $.init_declarator),
|
||||||
|
";",
|
||||||
|
),
|
||||||
|
|
||||||
declaration_specifier: ($) => $.type_specifier_qualifier,
|
declaration_specifier: ($) => $.type_specifier_qualifier,
|
||||||
|
|
||||||
@@ -271,14 +293,22 @@ module.exports = grammar({
|
|||||||
),
|
),
|
||||||
|
|
||||||
array_specifier: ($) =>
|
array_specifier: ($) =>
|
||||||
seq($.type_specifier, "[", $.constant_expression, "]"),
|
seq(
|
||||||
|
field("element", $.type_specifier),
|
||||||
|
"[",
|
||||||
|
field("size", $.constant_expression),
|
||||||
|
"]",
|
||||||
|
),
|
||||||
|
|
||||||
type_qualifier_list: ($) => seq($.type_qualifier, repeat($.type_qualifier)),
|
type_qualifier_list: ($) => seq($.type_qualifier, repeat($.type_qualifier)),
|
||||||
|
|
||||||
type_qualifier: ($) => choice("const", "mut"),
|
type_qualifier: ($) => choice("const", "mut"),
|
||||||
|
|
||||||
init_declarator: ($) =>
|
init_declarator: ($) =>
|
||||||
seq($.identifier, optional(seq("=", $.initializer))),
|
seq(
|
||||||
|
field("name", $.identifier),
|
||||||
|
optional(seq("=", field("value", $.initializer))),
|
||||||
|
),
|
||||||
|
|
||||||
braced_initializer: ($) =>
|
braced_initializer: ($) =>
|
||||||
choice(seq("{", "}"), seq("{", $.initializer_list, "}")),
|
choice(seq("{", "}"), seq("{", $.initializer_list, "}")),
|
||||||
@@ -291,11 +321,17 @@ module.exports = grammar({
|
|||||||
function_signature: ($) =>
|
function_signature: ($) =>
|
||||||
choice(
|
choice(
|
||||||
// (params) -> returns
|
// (params) -> returns
|
||||||
seq("(", $.parameter_list, ")", "->", $.return_list),
|
seq(
|
||||||
|
"(",
|
||||||
|
field("parameters", $.parameter_list),
|
||||||
|
")",
|
||||||
|
"->",
|
||||||
|
field("return_type", $.return_list),
|
||||||
|
),
|
||||||
// () -> returns
|
// () -> returns
|
||||||
seq("(", ")", "->", $.return_list),
|
seq("(", ")", "->", field("return_type", $.return_list)),
|
||||||
// (params) (implicit void)
|
// (params) (implicit void)
|
||||||
seq("(", $.parameter_list, ")"),
|
seq("(", field("parameters", $.parameter_list), ")"),
|
||||||
// () (implicit void)
|
// () (implicit void)
|
||||||
seq("(", ")"),
|
seq("(", ")"),
|
||||||
),
|
),
|
||||||
@@ -303,7 +339,8 @@ module.exports = grammar({
|
|||||||
parameter_list: ($) =>
|
parameter_list: ($) =>
|
||||||
seq($.parameter_declaration, repeat(seq(",", $.parameter_declaration))),
|
seq($.parameter_declaration, repeat(seq(",", $.parameter_declaration))),
|
||||||
|
|
||||||
parameter_declaration: ($) => seq($.declaration_specifier, $.identifier),
|
parameter_declaration: ($) =>
|
||||||
|
seq(field("type", $.declaration_specifier), field("name", $.identifier)),
|
||||||
|
|
||||||
return_list: ($) => $.declaration_specifier,
|
return_list: ($) => $.declaration_specifier,
|
||||||
|
|
||||||
@@ -319,7 +356,8 @@ module.exports = grammar({
|
|||||||
$.print_statement,
|
$.print_statement,
|
||||||
),
|
),
|
||||||
|
|
||||||
expression_statement: ($) => seq(optional($.expression), ";"),
|
expression_statement: ($) =>
|
||||||
|
seq(optional(field("expression", $.expression)), ";"),
|
||||||
|
|
||||||
primary_block: ($) =>
|
primary_block: ($) =>
|
||||||
choice($.statement_block, $.selection_statement, $.iteration_statement),
|
choice($.statement_block, $.selection_statement, $.iteration_statement),
|
||||||
@@ -340,15 +378,18 @@ module.exports = grammar({
|
|||||||
seq(
|
seq(
|
||||||
"if",
|
"if",
|
||||||
"(",
|
"(",
|
||||||
$.expression,
|
field("condition", $.expression),
|
||||||
")",
|
")",
|
||||||
$.statement_block,
|
field("consequence", $.statement_block),
|
||||||
optional(
|
optional(
|
||||||
seq(
|
seq(
|
||||||
"else",
|
"else",
|
||||||
choice(
|
field(
|
||||||
$.statement_block,
|
"alternative",
|
||||||
$.selection_statement, // else-if chain
|
choice(
|
||||||
|
$.statement_block,
|
||||||
|
$.selection_statement, // else-if chain
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -358,31 +399,37 @@ module.exports = grammar({
|
|||||||
iteration_statement: ($) =>
|
iteration_statement: ($) =>
|
||||||
choice(
|
choice(
|
||||||
// while (expr) statement
|
// while (expr) statement
|
||||||
seq("while", "(", $.expression, ")", $.secondary_block),
|
seq(
|
||||||
|
"while",
|
||||||
|
"(",
|
||||||
|
field("condition", $.expression),
|
||||||
|
")",
|
||||||
|
field("body", $.secondary_block),
|
||||||
|
),
|
||||||
|
|
||||||
// for (opt_expr ; opt_expr ; opt_expr) statement
|
// for (opt_expr ; opt_expr ; opt_expr) statement
|
||||||
seq(
|
seq(
|
||||||
"for",
|
"for",
|
||||||
"(",
|
"(",
|
||||||
optional($.expression),
|
optional(field("initializer", $.expression)),
|
||||||
";",
|
";",
|
||||||
optional($.expression),
|
optional(field("condition", $.expression)),
|
||||||
";",
|
";",
|
||||||
optional($.expression),
|
optional(field("update", $.expression)),
|
||||||
")",
|
")",
|
||||||
$.secondary_block,
|
field("body", $.secondary_block),
|
||||||
),
|
),
|
||||||
|
|
||||||
// for (declaration opt_expr ; opt_expr) statement
|
// for (declaration opt_expr ; opt_expr) statement
|
||||||
seq(
|
seq(
|
||||||
"for",
|
"for",
|
||||||
"(",
|
"(",
|
||||||
$.declaration,
|
field("initializer", $.declaration),
|
||||||
optional($.expression),
|
optional(field("condition", $.expression)),
|
||||||
";",
|
";",
|
||||||
optional($.expression),
|
optional(field("update", $.expression)),
|
||||||
")",
|
")",
|
||||||
$.secondary_block,
|
field("body", $.secondary_block),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
@@ -391,9 +438,10 @@ module.exports = grammar({
|
|||||||
seq("continue", ";"),
|
seq("continue", ";"),
|
||||||
seq("break", ";"),
|
seq("break", ";"),
|
||||||
seq("return", ";"),
|
seq("return", ";"),
|
||||||
seq("return", $.expression, ";"),
|
seq("return", field("value", $.expression), ";"),
|
||||||
),
|
),
|
||||||
|
|
||||||
print_statement: ($) => seq("print", "(", $.expression, ")", ";"),
|
print_statement: ($) =>
|
||||||
|
seq("print", "(", field("value", $.expression), ")", ";"),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user