grammar: add util to make blocks and lists

This commit is contained in:
Dmitriy Pleshevskiy 2023-06-16 00:58:51 +03:00
parent 3b38ac4fb8
commit 21edafd556
Signed by: pleshevskiy
GPG key ID: 79C4487B44403985
2 changed files with 50 additions and 51 deletions

View file

@ -1,3 +1,15 @@
// mkWrapCont :: string -> string -> ($ -> Rule) -> $ -> Rule
const mkWrapCont = (start, end) => (getRule) => ($) =>
seq(
start,
repeat(choice($._eol, seq(getRule($), $._end))),
optional(seq(getRule($), optional($._end))),
end
);
const mkBlock = mkWrapCont("{", "}");
const mkList = mkWrapCont("[", "]");
const PREC = { const PREC = {
COMMENT: -2, COMMENT: -2,
EOL: -1, EOL: -1,
@ -79,13 +91,7 @@ module.exports = grammar({
$.shape_key $.shape_key
), ),
_connection_block: ($) => _connection_block: mkBlock(($) => $._connection_attribute),
seq(
"{",
repeat(choice($._eol, seq($._connection_attribute, $._end))),
optional(seq($._connection_attribute, optional($._end))),
"}"
),
// classes // classes
@ -104,13 +110,7 @@ module.exports = grammar({
) )
), ),
_classes_block: ($) => _classes_block: mkBlock(($) => $._classes_item),
seq(
"{",
repeat(choice($._eol, seq($._classes_item, $._end))),
optional(seq($._classes_item, optional($._end))),
"}"
),
_classes_item: ($) => _classes_item: ($) =>
seq( seq(
@ -124,13 +124,7 @@ module.exports = grammar({
) )
), ),
_classes_item_block: ($) => _classes_item_block: mkBlock(($) => $._classes_item_attribute),
seq(
"{",
repeat(choice($._eol, seq($._classes_item_attribute, $._end))),
optional(seq($._classes_item_attribute, optional($._end))),
"}"
),
_classes_item_attribute: ($) => _classes_item_attribute: ($) =>
choice( choice(
@ -155,13 +149,7 @@ module.exports = grammar({
) )
), ),
_container_block: ($) => _container_block: mkBlock(($) => $._container_block_definition),
seq(
"{",
repeat(choice($._eol, seq($._container_block_definition, $._end))),
optional(seq($._container_block_definition, optional($._end))),
"}"
),
_container_block_definition: ($) => _container_block_definition: ($) =>
choice($.shape, $.container, $.connection, $._shape_attribute), choice($.shape, $.container, $.connection, $._shape_attribute),
@ -241,13 +229,7 @@ module.exports = grammar({
_class_attribute: ($) => _class_attribute: ($) =>
seq($.keyword_class, $._colon, choice($.class_list, $._class_name)), seq($.keyword_class, $._colon, choice($.class_list, $._class_name)),
class_list: ($) => class_list: mkList(($) => $._class_name),
seq(
"[",
repeat(choice($._eol, seq($._class_name, $._end))),
optional(seq($._class_name, optional($._end))),
"]"
),
_class_name: ($) => alias($.shape_key, $.class_name), _class_name: ($) => alias($.shape_key, $.class_name),
@ -285,19 +267,8 @@ module.exports = grammar({
) )
), ),
_style_attribute_block: ($) => _style_attribute_block: mkBlock(($) =>
seq( alias($._inner_style_attribute, $.attribute)
"{",
repeat(
choice(
$._eol,
seq(alias($._inner_style_attribute, $.attribute), $._end)
)
),
optional(
seq(alias($._inner_style_attribute, $.attribute), optional($._end))
),
"}"
), ),
_inner_style_attribute: ($) => _inner_style_attribute: ($) =>
@ -359,8 +330,7 @@ module.exports = grammar({
) )
), ),
_connection_arrowhead_block: ($) => _connection_arrowhead_block: mkBlock(($) => $._shape_attribute),
seq("{", repeat(choice($._eol, seq($._shape_attribute, $._end))), "}"),
_connection_arrowhead_attr_key: ($) => _connection_arrowhead_attr_key: ($) =>
choice("source-arrowhead", "target-arrowhead"), choice("source-arrowhead", "target-arrowhead"),

View file

@ -1655,6 +1655,35 @@
] ]
} }
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_shape_attribute"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_end"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{ {
"type": "STRING", "type": "STRING",
"value": "}" "value": "}"