tree-sitter-d2/grammar.js

140 lines
3.0 KiB
JavaScript
Raw Normal View History

const spaces = repeat(" ");
2022-12-04 00:07:26 +03:00
module.exports = grammar({
name: "d2",
2022-12-05 11:36:14 +03:00
extras: ($) => [],
2022-12-04 18:35:50 +03:00
word: ($) => $._word,
conflicts: ($) => [[$.identifier], [$.arrow]],
2022-12-04 00:07:26 +03:00
rules: {
source_file: ($) => repeat($._definition),
2022-12-06 00:53:06 +03:00
_definition: ($) =>
choice($._end, $._root_attribute, $.connection, $.shape),
2022-12-04 00:07:26 +03:00
connection: ($) =>
2022-12-04 03:25:38 +03:00
seq(
$._identifier,
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-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
_identifier: ($) => seq(spaces, $.identifier),
identifier: ($) =>
seq(
optional($._dash),
choice(
$._word,
repeat1(seq($._word, choice(spaces, $._dash), $._word))
),
optional($._dash)
2022-12-04 18:35:37 +03:00
),
_colon: ($) => seq(spaces, ":", spaces),
2022-12-05 00:26:24 +03:00
_arrow: ($) => seq(spaces, $.arrow),
2022-12-04 18:35:37 +03:00
2022-12-04 00:07:26 +03:00
arrow: ($) =>
choice(
seq("--", repeat($._dash)),
seq("<-", repeat($._dash)),
seq("<-", repeat($._dash), ">"),
seq(repeat($._dash), "->")
2022-12-04 00:07:26 +03:00
),
2022-12-06 00:53:06 +03:00
_dash: ($) => token.immediate("-"),
dot: ($) => token.immediate("."),
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
_word: ($) => /[\w\d]+/,
2022-12-04 23:45:53 +03:00
_eof: ($) => choice("\n", "\0"),
_end: ($) => seq(spaces, choice(";", $._eof)),
2022-12-04 00:07:26 +03:00
},
});