tree-sitter-d2/grammar.js

209 lines
4.6 KiB
JavaScript
Raw Normal View History

2022-12-07 18:52:19 +03:00
const spaces = repeat(choice(" ", "\t"));
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: ($) => [
2022-12-07 23:14:25 +03:00
[$.shape_key],
2022-12-06 11:57:59 +03:00
[$.arrow],
2022-12-07 23:14:25 +03:00
[$._shape_path],
[$._shape_key],
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: {
2022-12-07 23:14:25 +03:00
source_file: ($) => repeat($._root_definition),
2022-12-04 00:07:26 +03:00
2022-12-07 23:14:25 +03:00
_root_definition: ($) =>
choice(
$._emptyline,
$._root_attribute,
$.connection,
$._shape_definition
),
2022-12-04 00:07:26 +03:00
connection: ($) =>
2022-12-04 03:25:38 +03:00
seq(
2022-12-07 23:14:25 +03:00
$._shape_path,
repeat1(seq($._arrow, $._shape_path)),
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-07 23:14:25 +03:00
_shape_definition: ($) =>
2022-12-04 03:36:46 +03:00
seq(
2022-12-07 23:14:25 +03:00
$._shape_path,
2022-12-05 10:37:04 +03:00
optional(
2022-12-06 12:32:50 +03:00
choice(
seq($.dot, $._shape_attribute),
seq(
2022-12-07 18:52:19 +03:00
optional(seq($._colon, optional($.label))),
2022-12-06 12:32:50 +03:00
optional(alias($._shape_block, $.block))
)
)
2022-12-05 00:26:24 +03:00
),
2022-12-04 03:36:46 +03:00
$._end
),
2022-12-07 23:14:25 +03:00
_shape_path: ($) =>
choice(
$._shape_key,
seq($._shape_key, repeat1(seq($.dot, $._shape_key)))
),
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-07 18:52:19 +03:00
choice(
seq(
alias($._root_attr_key, $.attr_key),
$._colon,
$.attr_value,
$._end
),
alias(seq($._shape_attribute, $._end), $.invalid)
),
2022-12-04 23:45:53 +03:00
_root_attr_key: ($) => "direction",
2022-12-06 12:32:50 +03:00
_shape_block: ($) =>
seq(
spaces,
"{",
2022-12-07 18:52:19 +03:00
repeat(
choice($._emptyline, seq(spaces, $._shape_block_definition, $._end))
),
2022-12-06 12:32:50 +03:00
optional(seq($._shape_block_definition, optional($._end))),
spaces,
"}"
),
_shape_block_definition: ($) =>
2022-12-07 23:14:25 +03:00
choice($.connection, $._shape_definition, $._shape_attribute),
2022-12-06 12:32:50 +03:00
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
2022-12-07 23:14:25 +03:00
_shape_key: ($) => seq(spaces, $.shape_key),
2022-12-07 23:14:25 +03:00
shape_key: ($) =>
choice(
$.string,
seq(
optional($._dash),
choice(
$._word,
repeat1(seq($._word, choice(repeat1(" "), $._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
},
});