2022-12-04 00:07:26 +03:00
|
|
|
module.exports = grammar({
|
|
|
|
name: "d2",
|
|
|
|
|
|
|
|
externals: ($) => [$._identifier],
|
|
|
|
|
|
|
|
rules: {
|
|
|
|
// TODO: add the actual grammar rules
|
|
|
|
source_file: ($) => repeat($._definition),
|
|
|
|
|
|
|
|
_definition: ($) => choice($.connection, $.shape),
|
|
|
|
|
|
|
|
_end: ($) => choice(";", "\n"),
|
|
|
|
|
|
|
|
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:13:40 +03:00
|
|
|
shape: ($) => seq($.identifier, optional(seq(":", $.label)), $._end),
|
2022-12-04 00:07:26 +03:00
|
|
|
|
2022-12-04 03:13:40 +03:00
|
|
|
label: ($) => choice($.string, /[^\n;{]+/),
|
2022-12-04 00:07:26 +03:00
|
|
|
|
|
|
|
identifier: ($) => $._identifier,
|
|
|
|
|
|
|
|
arrow: ($) =>
|
|
|
|
choice(
|
|
|
|
seq("--", repeat("-")),
|
|
|
|
seq("<-", repeat("-")),
|
|
|
|
seq("<-", repeat("-"), ">"),
|
|
|
|
seq(repeat("-"), "->")
|
|
|
|
),
|
|
|
|
|
|
|
|
string: ($) =>
|
|
|
|
choice(
|
|
|
|
seq("'", repeat(token(/[^'\n]+/)), "'"),
|
|
|
|
seq('"', repeat(token(/[^'\n]+/)), '"'),
|
|
|
|
seq("`", repeat(token(/[^'\n]+/)), "`")
|
|
|
|
),
|
|
|
|
},
|
|
|
|
});
|