tree-sitter-d2/grammar.js

137 lines
3.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-05 00:26:24 +03:00
_definition: ($) => choice($._root_attribute, $.connection, $.shape),
2022-12-04 00:07:26 +03:00
connection: ($) =>
2022-12-04 03:25:38 +03:00
seq(
$.identifier,
2022-12-05 10:33:49 +03:00
repeat1(seq($.arrow, $.identifier)),
2022-12-05 10:37:04 +03:00
optional(
choice(seq($.dot, $._connection_attribute), seq($._colon, $.label))
),
2022-12-04 03:25:38 +03:00
$._end
),
2022-12-04 00:07:26 +03:00
2022-12-04 03:36:46 +03:00
shape: ($) =>
seq(
$.identifier,
repeat(seq($.dot, $.identifier)),
2022-12-05 10:37:04 +03:00
optional(
choice(seq($.dot, $._shape_attribute), seq($._colon, $.label))
2022-12-05 00:26:24 +03:00
),
2022-12-04 03:36:46 +03:00
$._end
),
2022-12-04 00:07:26 +03:00
identifier: ($) => $._identifier,
2022-12-05 10:09:14 +03:00
label: ($) => choice($.string, $._unquoted_string),
2022-12-04 23:45:53 +03:00
attr_value: ($) => choice($.string, $._unquoted_string),
_root_attribute: ($) =>
2022-12-05 00:26:24 +03:00
seq(alias($._root_attr_key, $.attr_key), $._colon, $.attr_value, $._end),
2022-12-04 23:45:53 +03:00
_root_attr_key: ($) => "direction",
_style_attribute: ($) =>
seq(
alias("style", $.attr_key),
$.dot,
alias($._style_attr_key, $.attr_key),
2022-12-05 00:26:24 +03:00
$._colon,
$.attr_value
2022-12-04 23:45:53 +03:00
),
_shape_attribute: ($) =>
2022-12-05 00:26:24 +03:00
choice(
seq(alias($._shape_attr_key, $.attr_key), $._colon, $.attr_value),
$._style_attribute
2022-12-04 23:45:53 +03:00
),
2022-12-05 10:37:04 +03:00
_connection_attribute: ($) =>
seq(alias($._connection_attr_key, $.attr_key), $._colon, $.attr_value),
2022-12-04 23:45:53 +03:00
_shape_attr_key: ($) =>
choice(
"shape",
"label",
// sql
"constraint",
// image
"icon",
"width",
"height"
),
_style_attr_key: ($) =>
choice(
"opacity",
"fill",
"stroke",
"stroke-width",
"stroke-dash",
"border-radius",
"font-color",
"shadow",
"multiple",
"animated",
"3d",
"link"
),
_text_attr_key: ($) => "near",
_connection_attr_key: ($) => choice("source-arrowhead", "target-arrowhead"),
2022-12-04 18:35:50 +03:00
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(" ")
)
),
2022-12-05 00:26:24 +03:00
_colon: ($) => seq(repeat(" "), ":", repeat(" ")),
2022-12-04 18:35:37 +03:00
_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-05 00:26:24 +03:00
_unquoted_string: ($) => token.immediate(/[^'"`\n;{]+/),
2022-12-04 03:36:46 +03:00
2022-12-04 00:07:26 +03:00
string: ($) =>
choice(
2022-12-05 00:26:24 +03:00
seq("'", repeat(token.immediate(/[^'\n]+/)), "'"),
seq('"', repeat(token.immediate(/[^"\n]+/)), '"'),
seq("`", repeat(token.immediate(/[^`\n]+/)), "`")
2022-12-04 00:07:26 +03:00
),
2022-12-04 23:45:53 +03:00
_end: ($) => choice(";", "\n", "\0"),
dot: ($) => ".",
2022-12-04 00:07:26 +03:00
},
});