tree-sitter-bp/grammar.js

109 lines
2 KiB
JavaScript
Raw Normal View History

2024-04-08 00:24:14 +02:00
function commaSeparated(elem) {
return seq(elem, repeat(seq(",", elem)), optional(","))
}
2024-04-07 20:59:34 +02:00
module.exports = grammar({
name: "blueprint",
2024-04-07 22:37:52 +02:00
extras: ($) => [
/\s+/,
2024-04-07 23:20:57 +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-07 23:20:57 +02:00
comment: (_) => seq("#", /.*/),
2024-04-07 22:37:52 +02:00
// Definitions {{{
assignment: ($) => seq(
field("left", $.identifier),
2024-04-07 22:40:17 +02:00
field("operator", choice("=", "+=")),
2024-04-07 22:37:52 +02:00
field("right", $._expr),
),
// }}}
// Expressions {{{
_expr: ($) => choice(
// Literals
$.identifier,
$.integer_literal,
$._string_literal,
2024-04-08 00:24:14 +02:00
// Composites
$.list_expression,
2024-04-08 00:36:21 +02:00
$.map_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-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}/,
),
)),
2024-04-08 00:24:14 +02:00
list_expression: ($) => seq(
"[",
optional(commaSeparated($._expr)),
"]",
),
2024-04-08 00:36:21 +02:00
map_expression: ($) => seq(
"{",
optional(commaSeparated($._colon_property)),
"}",
),
// }}}
// Properties {{{
_colon_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