From 21edafd556b63595610f97ad39e1912ff09a2335 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 16 Jun 2023 00:58:51 +0300 Subject: [PATCH] grammar: add util to make blocks and lists --- grammar.js | 72 ++++++++++++++---------------------------------- src/grammar.json | 29 +++++++++++++++++++ 2 files changed, 50 insertions(+), 51 deletions(-) diff --git a/grammar.js b/grammar.js index 49315ef..ca8d276 100644 --- a/grammar.js +++ b/grammar.js @@ -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 = { COMMENT: -2, EOL: -1, @@ -79,13 +91,7 @@ module.exports = grammar({ $.shape_key ), - _connection_block: ($) => - seq( - "{", - repeat(choice($._eol, seq($._connection_attribute, $._end))), - optional(seq($._connection_attribute, optional($._end))), - "}" - ), + _connection_block: mkBlock(($) => $._connection_attribute), // classes @@ -104,13 +110,7 @@ module.exports = grammar({ ) ), - _classes_block: ($) => - seq( - "{", - repeat(choice($._eol, seq($._classes_item, $._end))), - optional(seq($._classes_item, optional($._end))), - "}" - ), + _classes_block: mkBlock(($) => $._classes_item), _classes_item: ($) => seq( @@ -124,13 +124,7 @@ module.exports = grammar({ ) ), - _classes_item_block: ($) => - seq( - "{", - repeat(choice($._eol, seq($._classes_item_attribute, $._end))), - optional(seq($._classes_item_attribute, optional($._end))), - "}" - ), + _classes_item_block: mkBlock(($) => $._classes_item_attribute), _classes_item_attribute: ($) => choice( @@ -155,13 +149,7 @@ module.exports = grammar({ ) ), - _container_block: ($) => - seq( - "{", - repeat(choice($._eol, seq($._container_block_definition, $._end))), - optional(seq($._container_block_definition, optional($._end))), - "}" - ), + _container_block: mkBlock(($) => $._container_block_definition), _container_block_definition: ($) => choice($.shape, $.container, $.connection, $._shape_attribute), @@ -241,13 +229,7 @@ module.exports = grammar({ _class_attribute: ($) => seq($.keyword_class, $._colon, choice($.class_list, $._class_name)), - class_list: ($) => - seq( - "[", - repeat(choice($._eol, seq($._class_name, $._end))), - optional(seq($._class_name, optional($._end))), - "]" - ), + class_list: mkList(($) => $._class_name), _class_name: ($) => alias($.shape_key, $.class_name), @@ -285,20 +267,9 @@ module.exports = grammar({ ) ), - _style_attribute_block: ($) => - seq( - "{", - repeat( - choice( - $._eol, - seq(alias($._inner_style_attribute, $.attribute), $._end) - ) - ), - optional( - seq(alias($._inner_style_attribute, $.attribute), optional($._end)) - ), - "}" - ), + _style_attribute_block: mkBlock(($) => + alias($._inner_style_attribute, $.attribute) + ), _inner_style_attribute: ($) => prec( @@ -359,8 +330,7 @@ module.exports = grammar({ ) ), - _connection_arrowhead_block: ($) => - seq("{", repeat(choice($._eol, seq($._shape_attribute, $._end))), "}"), + _connection_arrowhead_block: mkBlock(($) => $._shape_attribute), _connection_arrowhead_attr_key: ($) => choice("source-arrowhead", "target-arrowhead"), diff --git a/src/grammar.json b/src/grammar.json index 6a69750..c2299cf 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -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", "value": "}"