tree-sitter-bp/grammar.js

220 lines
4.5 KiB
JavaScript
Raw Normal View History

2024-04-08 00:24:14 +02:00
function commaSeparated(elem) {
return seq(elem, repeat(seq(",", elem)), optional(","))
}
function trailingCommaSeparated(elem) {
return repeat(seq(elem, ","))
}
2024-04-07 20:59:34 +02:00
module.exports = grammar({
name: "blueprint",
2024-04-07 22:37:52 +02:00
extras: ($) => [
/\s+/,
$.line_comment,
2024-04-08 09:48:10 +02:00
$.block_comment,
2024-04-07 22:37:52 +02:00
],
2024-04-07 20:59:34 +02:00
rules: {
2024-04-07 22:37:52 +02:00
source_file: ($) => repeat($._definition),
_definition: ($) => choice(
$.assignment,
2024-04-08 02:36:27 +02:00
$.module,
2024-04-07 22:37:52 +02:00
),
line_comment: (_) => seq("//", /.*/),
2024-04-07 23:20:57 +02:00
2024-04-08 09:48:10 +02:00
block_comment: (_) => seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, '/'),
2024-04-07 22:37:52 +02:00
// Definitions {{{
assignment: ($) => seq(
field("left", $.identifier),
2024-04-08 03:13:43 +02:00
field("operator", alias(choice("=", "+="), $.operator)),
2024-04-07 22:37:52 +02:00
field("right", $._expr),
),
2024-04-08 02:36:27 +02:00
module: ($) => choice(
$._old_module,
$._new_module,
),
// This syntax is deprecated, but still accepted
_old_module: ($) => seq(
field("type", $.identifier),
"{",
optional(commaSeparated(
alias(field("property", $._colon_property), $.property)
)),
2024-04-08 02:36:27 +02:00
"}",
),
_new_module: ($) => seq(
field("type", $.identifier),
2024-04-08 02:36:27 +02:00
"(",
optional(commaSeparated(
alias(field("property", $._equal_property), $.property)
)),
2024-04-08 02:36:27 +02:00
")",
),
2024-04-07 22:37:52 +02:00
// }}}
// Expressions {{{
_expr: ($) => choice(
// Literals
$.identifier,
2024-04-08 00:39:47 +02:00
$.boolean_literal,
2024-04-07 22:37:52 +02:00
$.integer_literal,
$._string_literal,
// Conditionals
$.select_expression,
2024-04-08 00:24:14 +02:00
// Composites
$.list_expression,
2024-04-08 00:36:21 +02:00
$.map_expression,
2024-04-08 03:12:22 +02:00
// Operators
$.binary_expression,
2024-04-07 22:37:52 +02:00
),
// The Blueprint scanner makes use of Go's lexer, so copy their rule
identifier: (_) => /[_\p{XID_Start}][_\p{XID_Continue}]*/,
2024-04-08 00:39:47 +02:00
boolean_literal: (_) => choice("true", "false"),
2024-04-07 23:49:15 +02:00
integer_literal: (_) => seq(optional("-"), /[0-9]+/),
2024-04-07 22:37:52 +02:00
// The Blueprint scanner makes use of Go's lexer, so copy their rule
_string_literal: ($) => choice(
$.raw_string_literal,
$.interpreted_string_literal,
),
raw_string_literal: (_) => token(seq(
"`",
/[^`]+/,
"`",
)),
interpreted_string_literal: $ => seq(
'"',
repeat(choice(
// Allow all characters without special meaning, disallow newlines
/[^"\n\\]+/,
$.escape_sequence,
)),
token.immediate('"'),
),
escape_sequence: (_) => token.immediate(seq(
'\\',
choice(
/[^xuU]/,
/\d{2,3}/,
/x[0-9a-fA-F]{2,}/,
/u[0-9a-fA-F]{4}/,
/U[0-9a-fA-F]{8}/,
),
)),
select_expression: ($) => seq(
"select",
"(",
2024-04-08 02:25:09 +02:00
choice($.select_value, $.soong_config_variable),
",",
$.select_cases,
")",
),
select_value: ($) => seq(
field("type", alias(
choice("product_variable", "release_variable", "variant"),
$.selection_type,
)),
"(",
field("condition", $._string_literal),
")",
),
2024-04-08 02:25:09 +02:00
soong_config_variable: ($) => seq(
field("type", alias("soong_config_variable", $.selection_type)),
"(",
field(
"condition",
seq(
field("namespace", $._string_literal),
",",
field("variable", $._string_literal),
),
),
")",
),
select_cases: ($) => seq(
"{",
optional(trailingCommaSeparated($.select_case)),
// default *must* be the last one, enforced at parse-time...
optional(seq($.default_case, ",")),
"}",
),
select_case: ($) => seq(
field("pattern", $._string_literal),
":",
field("value", $._case_value)
),
default_case: ($) => seq(
field("pattern", "default"),
":",
field("value", $._case_value),
),
_case_value: ($) => choice(
alias("unset", $.unset),
$._expr,
),
2024-04-08 00:24:14 +02:00
list_expression: ($) => seq(
"[",
2024-04-08 02:46:43 +02:00
optional(commaSeparated(field("element", $._expr))),
2024-04-08 00:24:14 +02:00
"]",
),
2024-04-08 00:36:21 +02:00
map_expression: ($) => seq(
"{",
optional(commaSeparated(
alias(field("property", $._colon_property), $.property)
)),
2024-04-08 00:36:21 +02:00
"}",
),
2024-04-08 03:12:22 +02:00
binary_expression: ($) => prec.left(seq(
field("left", $._expr),
2024-04-08 03:13:43 +02:00
field("operator", alias("+", $.operator)),
2024-04-08 03:12:22 +02:00
field("right", $._expr),
)),
2024-04-08 00:36:21 +02:00
// }}}
// Properties {{{
_colon_property: ($) => seq(
field("field", $.identifier),
":",
field("value", $._expr),
),
2024-04-08 02:36:27 +02:00
_equal_property: ($) => seq(
field("field", $.identifier),
"=",
field("value", $._expr),
),
2024-04-07 22:37:52 +02:00
// }}}
2024-04-07 20:59:34 +02:00
}
});
// vim: foldmethod=marker sw=2