chore: remove trailing spaces from identifier

This commit is contained in:
Dmitriy Pleshevskiy 2022-12-06 10:32:04 +03:00
parent 811520dfd4
commit 7a76b25c11
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
4 changed files with 223 additions and 209 deletions

View file

@ -1,6 +1,5 @@
foo -- bar
biz -> baz
foo biz <-> baz
biz <- baz
bar : Foo Bar; baz

View file

@ -1,11 +1,14 @@
const spaces = repeat(" ");
module.exports = grammar({ module.exports = grammar({
name: "d2", name: "d2",
// TODO: handle empty lines
extras: ($) => [], extras: ($) => [],
word: ($) => $._word, word: ($) => $._word,
conflicts: ($) => [[$.identifier], [$.arrow]],
rules: { rules: {
source_file: ($) => repeat($._definition), source_file: ($) => repeat($._definition),
@ -14,8 +17,8 @@ module.exports = grammar({
connection: ($) => connection: ($) =>
seq( seq(
$.identifier, $._identifier,
repeat1(seq($.arrow, $.identifier)), repeat1(seq($._arrow, $._identifier)),
optional( optional(
choice(seq($.dot, $._connection_attribute), seq($._colon, $.label)) choice(seq($.dot, $._connection_attribute), seq($._colon, $.label))
), ),
@ -24,16 +27,14 @@ module.exports = grammar({
shape: ($) => shape: ($) =>
seq( seq(
$.identifier, $._identifier,
repeat(seq($.dot, $.identifier)), repeat(seq($.dot, $._identifier)),
optional( optional(
choice(seq($.dot, $._shape_attribute), seq($._colon, $.label)) choice(seq($.dot, $._shape_attribute), seq($._colon, $.label))
), ),
$._end $._end
), ),
identifier: ($) => $._identifier,
label: ($) => choice($.string, $._unquoted_string), label: ($) => choice($.string, $._unquoted_string),
attr_value: ($) => choice($.string, $._unquoted_string), attr_value: ($) => choice($.string, $._unquoted_string),
@ -93,36 +94,34 @@ module.exports = grammar({
_connection_attr_key: ($) => choice("source-arrowhead", "target-arrowhead"), _connection_attr_key: ($) => choice("source-arrowhead", "target-arrowhead"),
_identifier: ($) => _identifier: ($) => seq(spaces, $.identifier),
prec.right(
seq( identifier: ($) =>
repeat(" "), seq(
optional($._dash), optional($._dash),
choice( choice(
$._word, $._word,
repeat1(seq($._word, choice(repeat(" "), $._dash), $._word)) repeat1(seq($._word, choice(spaces, $._dash), $._word))
), ),
optional($._dash), optional($._dash)
repeat(" ")
)
), ),
_colon: ($) => seq(repeat(" "), ":", repeat(" ")), _colon: ($) => seq(spaces, ":", spaces),
_word: ($) => /[\w\d]+/, _arrow: ($) => seq(spaces, $.arrow),
arrow: ($) => arrow: ($) =>
prec.left( choice(
choice( seq("--", repeat($._dash)),
seq("--", repeat($._dash)), seq("<-", repeat($._dash)),
seq("<-", repeat($._dash)), seq("<-", repeat($._dash), ">"),
seq("<-", repeat($._dash), ">"), seq(repeat($._dash), "->")
seq(repeat($._dash), "->")
)
), ),
_dash: ($) => token.immediate("-"), _dash: ($) => token.immediate("-"),
dot: ($) => token.immediate("."),
_unquoted_string: ($) => token.immediate(/[^'"`\n;{]+/), _unquoted_string: ($) => token.immediate(/[^'"`\n;{]+/),
string: ($) => string: ($) =>
@ -132,9 +131,9 @@ module.exports = grammar({
seq("`", repeat(token.immediate(/[^`\n]+/)), "`") seq("`", repeat(token.immediate(/[^`\n]+/)), "`")
), ),
_eof: ($) => choice("\n", "\0"), _word: ($) => /[\w\d]+/,
_end: ($) => seq(repeat(" "), choice(";", $._eof)),
dot: ($) => ".", _eof: ($) => choice("\n", "\0"),
_end: ($) => seq(spaces, choice(";", $._eof)),
}, },
}); });

View file

@ -35,7 +35,7 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "_identifier"
}, },
{ {
"type": "REPEAT1", "type": "REPEAT1",
@ -44,11 +44,11 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "arrow" "name": "_arrow"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "_identifier"
} }
] ]
} }
@ -103,7 +103,7 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "_identifier"
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@ -116,7 +116,7 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "_identifier"
} }
] ]
} }
@ -166,10 +166,6 @@
} }
] ]
}, },
"identifier": {
"type": "SYMBOL",
"name": "_identifier"
},
"label": { "label": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -414,92 +410,90 @@
] ]
}, },
"_identifier": { "_identifier": {
"type": "PREC_RIGHT", "type": "SEQ",
"value": 0, "members": [
"content": { {
"type": "SEQ", "type": "REPEAT",
"members": [ "content": {
{ "type": "STRING",
"type": "REPEAT", "value": " "
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_dash"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_word"
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_word"
},
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "SYMBOL",
"name": "_dash"
}
]
},
{
"type": "SYMBOL",
"name": "_word"
}
]
}
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_dash"
},
{
"type": "BLANK"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
} }
] },
} {
"type": "SYMBOL",
"name": "identifier"
}
]
},
"identifier": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_dash"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_word"
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_word"
},
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "SYMBOL",
"name": "_dash"
}
]
},
{
"type": "SYMBOL",
"name": "_word"
}
]
}
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_dash"
},
{
"type": "BLANK"
}
]
}
]
}, },
"_colon": { "_colon": {
"type": "SEQ", "type": "SEQ",
@ -524,86 +518,94 @@
} }
] ]
}, },
"_word": { "_arrow": {
"type": "PATTERN", "type": "SEQ",
"value": "[\\w\\d]+" "members": [
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "SYMBOL",
"name": "arrow"
}
]
}, },
"arrow": { "arrow": {
"type": "PREC_LEFT", "type": "CHOICE",
"value": 0, "members": [
"content": { {
"type": "CHOICE", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SEQ", "type": "STRING",
"members": [ "value": "--"
{ },
"type": "STRING", {
"value": "--" "type": "REPEAT",
}, "content": {
{ "type": "SYMBOL",
"type": "REPEAT", "name": "_dash"
"content": {
"type": "SYMBOL",
"name": "_dash"
}
} }
] }
}, ]
{ },
"type": "SEQ", {
"members": [ "type": "SEQ",
{ "members": [
"type": "STRING", {
"value": "<-" "type": "STRING",
}, "value": "<-"
{ },
"type": "REPEAT", {
"content": { "type": "REPEAT",
"type": "SYMBOL", "content": {
"name": "_dash" "type": "SYMBOL",
} "name": "_dash"
} }
] }
}, ]
{ },
"type": "SEQ", {
"members": [ "type": "SEQ",
{ "members": [
"type": "STRING", {
"value": "<-" "type": "STRING",
}, "value": "<-"
{ },
"type": "REPEAT", {
"content": { "type": "REPEAT",
"type": "SYMBOL", "content": {
"name": "_dash" "type": "SYMBOL",
} "name": "_dash"
},
{
"type": "STRING",
"value": ">"
} }
] },
}, {
{ "type": "STRING",
"type": "SEQ", "value": ">"
"members": [ }
{ ]
"type": "REPEAT", },
"content": { {
"type": "SYMBOL", "type": "SEQ",
"name": "_dash" "members": [
} {
}, "type": "REPEAT",
{ "content": {
"type": "STRING", "type": "SYMBOL",
"value": "->" "name": "_dash"
} }
] },
} {
] "type": "STRING",
} "value": "->"
}
]
}
]
}, },
"_dash": { "_dash": {
"type": "IMMEDIATE_TOKEN", "type": "IMMEDIATE_TOKEN",
@ -612,6 +614,13 @@
"value": "-" "value": "-"
} }
}, },
"dot": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "."
}
},
"_unquoted_string": { "_unquoted_string": {
"type": "IMMEDIATE_TOKEN", "type": "IMMEDIATE_TOKEN",
"content": { "content": {
@ -693,6 +702,10 @@
} }
] ]
}, },
"_word": {
"type": "PATTERN",
"value": "[\\w\\d]+"
},
"_eof": { "_eof": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -730,14 +743,17 @@
] ]
} }
] ]
},
"dot": {
"type": "STRING",
"value": "."
} }
}, },
"extras": [], "extras": [],
"conflicts": [], "conflicts": [
[
"identifier"
],
[
"arrow"
]
],
"precedences": [], "precedences": [],
"externals": [], "externals": [],
"inline": [], "inline": [],

Binary file not shown.