tree-sitter-d2/grammar.js

56 lines
1.1 KiB
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:25:38 +03:00
seq(
$.identifier,
choice(
seq(seq($.arrow, $.identifier, ":", $.label)),
seq(repeat1(seq($.arrow, $.identifier)))
),
$._end
),
2022-12-04 00:07:26 +03:00
2022-12-04 03:36:46 +03:00
shape: ($) =>
seq(
$.identifier,
repeat(seq($.dot, $.identifier)),
optional(seq(":", $.label)),
$._end
),
dot: ($) => ".",
2022-12-04 00:07:26 +03:00
2022-12-04 03:36:46 +03:00
label: ($) => choice($.string, $._unquoted_string),
2022-12-04 00:07:26 +03:00
identifier: ($) => $._identifier,
arrow: ($) =>
choice(
seq("--", repeat("-")),
seq("<-", repeat("-")),
seq("<-", repeat("-"), ">"),
seq(repeat("-"), "->")
),
2022-12-04 03:36:46 +03:00
_unquoted_string: ($) => /[^\n;{]+/,
2022-12-04 00:07:26 +03:00
string: ($) =>
choice(
seq("'", repeat(token(/[^'\n]+/)), "'"),
seq('"', repeat(token(/[^'\n]+/)), '"'),
seq("`", repeat(token(/[^'\n]+/)), "`")
),
},
});