tree-sitter-d2/grammar.js

39 lines
902 B
JavaScript
Raw Normal View History

2022-12-04 00:07:26 +03:00
module.exports = grammar({
name: "d2",
externals: ($) => [$._identifier],
rules: {
// TODO: add the actual grammar rules
source_file: ($) => repeat($._definition),
_definition: ($) => choice($.connection, $.shape),
_end: ($) => choice(";", "\n"),
connection: ($) =>
2022-12-04 03:21:42 +03:00
seq($.identifier, repeat1(seq($.arrow, $.identifier)), $._end),
2022-12-04 00:07:26 +03:00
2022-12-04 03:13:40 +03:00
shape: ($) => seq($.identifier, optional(seq(":", $.label)), $._end),
2022-12-04 00:07:26 +03:00
2022-12-04 03:13:40 +03:00
label: ($) => choice($.string, /[^\n;{]+/),
2022-12-04 00:07:26 +03:00
identifier: ($) => $._identifier,
arrow: ($) =>
choice(
seq("--", repeat("-")),
seq("<-", repeat("-")),
seq("<-", repeat("-"), ">"),
seq(repeat("-"), "->")
),
string: ($) =>
choice(
seq("'", repeat(token(/[^'\n]+/)), "'"),
seq('"', repeat(token(/[^'\n]+/)), '"'),
seq("`", repeat(token(/[^'\n]+/)), "`")
),
},
});