2024-04-08 00:24:14 +02:00
|
|
|
function commaSeparated(elem) {
|
|
|
|
return seq(elem, repeat(seq(",", elem)), optional(","))
|
|
|
|
}
|
|
|
|
|
2024-04-08 02:12:35 +02:00
|
|
|
function trailingCommaSeparated(elem) {
|
|
|
|
return repeat(seq(elem, ","))
|
|
|
|
}
|
|
|
|
|
2024-04-07 20:59:34 +02:00
|
|
|
module.exports = grammar({
|
2024-04-10 17:30:57 +02:00
|
|
|
name: "bp",
|
2024-04-07 20:59:34 +02:00
|
|
|
|
2024-04-07 22:37:52 +02:00
|
|
|
extras: ($) => [
|
|
|
|
/\s+/,
|
2024-04-11 19:33:52 +02:00
|
|
|
$.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
|
|
|
),
|
|
|
|
|
2024-04-11 19:33:52 +02:00
|
|
|
comment: (_) => choice(
|
2024-04-11 19:38:14 +02:00
|
|
|
seq("//", /(\\+(.|\r?\n)|[^\\\n])*/),
|
|
|
|
seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, "/"),
|
2024-04-11 19:33:52 +02:00
|
|
|
),
|
2024-04-08 09:48:10 +02:00
|
|
|
|
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),
|
|
|
|
"{",
|
2024-04-08 02:49:24 +02:00
|
|
|
optional(commaSeparated(
|
|
|
|
alias(field("property", $._colon_property), $.property)
|
|
|
|
)),
|
2024-04-08 02:36:27 +02:00
|
|
|
"}",
|
|
|
|
),
|
|
|
|
|
|
|
|
_new_module: ($) => seq(
|
2024-04-08 22:25:44 +02:00
|
|
|
field("type", $.identifier),
|
2024-04-08 02:36:27 +02:00
|
|
|
"(",
|
2024-04-08 02:49:24 +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,
|
2024-04-08 00:16:31 +02:00
|
|
|
$._string_literal,
|
2024-04-08 02:12:35 +02:00
|
|
|
// 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
|
|
|
|
2024-04-08 00:16:31 +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}/,
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
|
2024-04-08 02:12:35 +02:00
|
|
|
select_expression: ($) => seq(
|
|
|
|
"select",
|
|
|
|
"(",
|
2024-04-23 16:47:14 +02:00
|
|
|
$.select_value,
|
2024-04-08 02:12:35 +02:00
|
|
|
",",
|
|
|
|
$.select_cases,
|
|
|
|
")",
|
|
|
|
),
|
|
|
|
|
|
|
|
select_value: ($) => seq(
|
2024-04-23 16:56:10 +02:00
|
|
|
field("name", $.identifier),
|
2024-04-08 02:25:09 +02:00
|
|
|
"(",
|
2024-04-23 16:52:36 +02:00
|
|
|
field("arguments", optional(commaSeparated($._string_literal))),
|
2024-04-08 02:25:09 +02:00
|
|
|
")",
|
|
|
|
),
|
|
|
|
|
2024-04-08 02:12:35 +02:00
|
|
|
select_cases: ($) => seq(
|
|
|
|
"{",
|
|
|
|
optional(trailingCommaSeparated($.select_case)),
|
|
|
|
"}",
|
|
|
|
),
|
|
|
|
|
|
|
|
select_case: ($) => seq(
|
2024-04-23 16:38:32 +02:00
|
|
|
field("pattern", choice(
|
|
|
|
$._string_literal,
|
2024-04-23 17:00:13 +02:00
|
|
|
$.boolean_literal,
|
2024-04-23 16:38:32 +02:00
|
|
|
alias("default", $.default),
|
|
|
|
)),
|
2024-04-08 02:12:35 +02:00
|
|
|
":",
|
|
|
|
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(
|
|
|
|
"{",
|
2024-04-08 02:49:24 +02:00
|
|
|
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
|