tree-sitter-d2/grammar.js

96 lines
2.0 KiB
JavaScript
Raw Normal View History

2022-12-04 00:07:26 +03:00
module.exports = grammar({
name: "d2",
2022-12-04 18:35:50 +03:00
word: ($) => $._word,
2022-12-04 00:07:26 +03:00
rules: {
// TODO: add the actual grammar rules
source_file: ($) => repeat($._definition),
2022-12-04 18:35:50 +03:00
_definition: ($) => choice($.attribute, $.connection, $.shape),
2022-12-04 00:07:26 +03:00
2022-12-04 18:35:50 +03:00
_end: ($) => choice(";", "\n", "\0"),
attribute: ($) =>
choice(
seq(
$.identifier,
repeat1(seq($.dot, $.keyword)),
":",
alias($.label, $.attribute_value),
$._end
),
seq(
$.keyword,
repeat(seq($.dot, $.keyword)),
":",
alias($.label, $.attribute_value),
$._end
)
),
2022-12-04 00:07:26 +03:00
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,
2022-12-04 18:35:50 +03:00
keyword: ($) => choice("direction"),
2022-12-04 18:35:37 +03:00
_identifier: ($) =>
prec.right(
seq(
repeat(" "),
optional($._dash),
choice(
$._word,
repeat1(seq($._word, choice(repeat(" "), $._dash), $._word))
),
optional($._dash),
repeat(" ")
)
),
_dash: ($) => token.immediate("-"),
_word: ($) => /[\w\d]+/,
2022-12-04 00:07:26 +03:00
arrow: ($) =>
2022-12-04 18:35:37 +03:00
prec.left(
choice(
seq("--", repeat($._dash)),
seq("<-", repeat($._dash)),
seq("<-", repeat($._dash), ">"),
seq(repeat($._dash), "->")
)
2022-12-04 00:07:26 +03:00
),
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]+/)), "`")
),
},
});