tree-sitter-d2/grammar.js

187 lines
4.2 KiB
JavaScript
Raw Normal View History

const spaces = repeat(" ");
2022-12-04 00:07:26 +03:00
module.exports = grammar({
name: "d2",
2022-12-07 12:09:27 +03:00
extras: ($) => [$.line_comment],
2022-12-05 11:36:14 +03:00
2022-12-04 18:35:50 +03:00
word: ($) => $._word,
2022-12-06 11:57:59 +03:00
conflicts: ($) => [
[$.identifier],
[$.arrow],
2022-12-06 12:40:18 +03:00
[$._identifier],
2022-12-06 12:32:50 +03:00
[$._shape_block],
[$._shape_block_definition],
2022-12-06 11:57:59 +03:00
[$._style_attr_block],
[$._inner_style_attribute],
2022-12-06 12:40:18 +03:00
[$._emptyline],
2022-12-06 11:57:59 +03:00
],
2022-12-04 00:07:26 +03:00
rules: {
source_file: ($) => repeat($._definition),
2022-12-06 00:53:06 +03:00
_definition: ($) =>
2022-12-06 12:40:18 +03:00
choice($._emptyline, $._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(
2022-12-06 12:32:50 +03:00
choice(
seq($.dot, $._shape_attribute),
seq(
$._colon,
optional($.label),
optional(alias($._shape_block, $.block))
)
)
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-06 11:57:59 +03:00
attr_value: ($) => seq(spaces, choice($.string, $._unquoted_string)),
2022-12-04 23:45:53 +03:00
_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",
2022-12-06 12:32:50 +03:00
_shape_block: ($) =>
seq(
spaces,
"{",
spaces,
2022-12-06 12:40:18 +03:00
repeat(choice($._emptyline, seq($._shape_block_definition, $._end))),
2022-12-06 12:32:50 +03:00
optional(seq($._shape_block_definition, optional($._end))),
spaces,
"}"
),
_shape_block_definition: ($) =>
choice($.connection, $.shape, $._shape_attribute),
2022-12-06 11:57:59 +03:00
_shape_attribute: ($) =>
choice(
seq(alias($._shape_attr_key, $.attr_key), $._colon, $.attr_value),
$._style_attribute
),
2022-12-04 23:45:53 +03:00
_style_attribute: ($) =>
seq(
alias("style", $.attr_key),
2022-12-06 11:57:59 +03:00
choice(
seq($.dot, $._inner_style_attribute),
seq($._colon, alias($._style_attr_block, $.block))
)
2022-12-04 23:45:53 +03:00
),
2022-12-06 11:57:59 +03:00
_style_attr_block: ($) =>
seq(
spaces,
"{",
spaces,
2022-12-06 12:40:18 +03:00
repeat(choice($._emptyline, seq($._inner_style_attribute, $._end))),
2022-12-06 11:57:59 +03:00
optional(seq($._inner_style_attribute, optional($._end))),
spaces,
"}"
2022-12-04 23:45:53 +03:00
),
2022-12-06 11:57:59 +03:00
_inner_style_attribute: ($) =>
seq(spaces, alias($._style_attr_key, $.attr_key), $._colon, $.attr_value),
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
),
2022-12-06 11:57:59 +03:00
_colon: ($) => seq(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-06 11:57:59 +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
2022-12-07 12:09:27 +03:00
line_comment: ($) => token(seq("#", /.*/)),
_word: ($) => /[\w\d]+/,
2022-12-04 23:45:53 +03:00
2022-12-06 12:40:18 +03:00
_emptyline: ($) => seq(spaces, $._eof),
_eof: ($) => choice("\n", "\0"),
_end: ($) => seq(spaces, choice(";", $._eof)),
2022-12-04 00:07:26 +03:00
},
});