diff --git a/grammar.js b/grammar.js index 202bb67..de062bf 100644 --- a/grammar.js +++ b/grammar.js @@ -28,7 +28,12 @@ module.exports = grammar({ $.block_comment, ], - conflicts: ($) => [[$._connection_path, $.container]], + conflicts: ($) => [ + [$._connection_path, $.container], + [$._container_block], + [$._connection_block], + [$._style_attribute_block], + ], rules: { source_file: ($) => repeat($._root_definition), @@ -70,10 +75,12 @@ module.exports = grammar({ ), _connection_block: ($) => - seq("{", repeat($._connection_block_definition), "}"), - - _connection_block_definition: ($) => - choice($._eol, seq($._connection_attribute, $._end)), + seq( + "{", + repeat(choice($._eol, seq($._connection_attribute, $._end))), + optional(seq($._connection_attribute, optional($._end))), + "}" + ), // containers @@ -93,16 +100,15 @@ module.exports = grammar({ ), _container_block: ($) => - seq("{", repeat($._container_block_definition), "}"), + seq( + "{", + repeat(choice($._eol, seq($._container_block_definition, $._end))), + optional(seq($._container_block_definition, optional($._end))), + "}" + ), _container_block_definition: ($) => - choice( - $._eol, - seq( - choice($.shape, $.container, $.connection, $._shape_attribute), - $._end - ) - ), + choice($.shape, $.container, $.connection, $._shape_attribute), // shapes @@ -212,6 +218,9 @@ module.exports = grammar({ seq(alias($._inner_style_attribute, $.attribute), $._end) ) ), + optional( + seq(alias($._inner_style_attribute, $.attribute), optional($._end)) + ), "}" ), diff --git a/src/grammar.json b/src/grammar.json index 597d872..78eaf22 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -194,38 +194,63 @@ { "type": "REPEAT", "content": { - "type": "SYMBOL", - "name": "_connection_block_definition" + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_eol" + }, + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_connection_attribute" + }, + { + "type": "SYMBOL", + "name": "_end" + } + ] + } + ] } }, + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_connection_attribute" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_end" + }, + { + "type": "BLANK" + } + ] + } + ] + }, + { + "type": "BLANK" + } + ] + }, { "type": "STRING", "value": "}" } ] }, - "_connection_block_definition": { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "_eol" - }, - { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_connection_attribute" - }, - { - "type": "SYMBOL", - "name": "_end" - } - ] - } - ] - }, "container": { "type": "PREC", "value": 2, @@ -337,10 +362,57 @@ { "type": "REPEAT", "content": { - "type": "SYMBOL", - "name": "_container_block_definition" + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_eol" + }, + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_container_block_definition" + }, + { + "type": "SYMBOL", + "name": "_end" + } + ] + } + ] } }, + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_container_block_definition" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_end" + }, + { + "type": "BLANK" + } + ] + } + ] + }, + { + "type": "BLANK" + } + ] + }, { "type": "STRING", "value": "}" @@ -352,37 +424,19 @@ "members": [ { "type": "SYMBOL", - "name": "_eol" + "name": "shape" }, { - "type": "SEQ", - "members": [ - { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "shape" - }, - { - "type": "SYMBOL", - "name": "container" - }, - { - "type": "SYMBOL", - "name": "connection" - }, - { - "type": "SYMBOL", - "name": "_shape_attribute" - } - ] - }, - { - "type": "SYMBOL", - "name": "_end" - } - ] + "type": "SYMBOL", + "name": "container" + }, + { + "type": "SYMBOL", + "name": "connection" + }, + { + "type": "SYMBOL", + "name": "_shape_attribute" } ] }, @@ -832,6 +886,40 @@ ] } }, + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "_inner_style_attribute" + }, + "named": true, + "value": "attribute" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_end" + }, + { + "type": "BLANK" + } + ] + } + ] + }, + { + "type": "BLANK" + } + ] + }, { "type": "STRING", "value": "}" @@ -1403,6 +1491,15 @@ [ "_connection_path", "container" + ], + [ + "_container_block" + ], + [ + "_connection_block" + ], + [ + "_style_attribute_block" ] ], "precedences": [], diff --git a/src/parser.c b/src/parser.c index 552efcd..0552d03 100644 Binary files a/src/parser.c and b/src/parser.c differ diff --git a/test/corpus/attributes.txt b/test/corpus/attributes.txt index a8e62e7..b38f8bd 100644 --- a/test/corpus/attributes.txt +++ b/test/corpus/attributes.txt @@ -301,6 +301,31 @@ foo -> bar: { ) ) +================================================================================ +Define attributes for connection in single-line +================================================================================ +foo -> bar: {source-arrowhead: 0; target-arrowhead: 1} + +-------------------------------------------------------------------------------- + +(source_file + (connection + (shape_key) + (arrow) + (shape_key) + (block + (attribute + (attr_key) + (label) + ) + (attribute + (attr_key) + (label) + ) + ) + ) +) + ================================================================================ Block style attributes inside a connection arrowhead ================================================================================ @@ -392,4 +417,23 @@ foo -> bar: { ) ) +================================================================================ +Declire style attributes in single-line +================================================================================ +foo.style: {opacity: 0.5; fill: red} + +-------------------------------------------------------------------------------- + +(source_file + (shape + (shape_key) (dot) + (attribute + (attr_key) + (block + (attribute (attr_key) (attr_value (float))) + (attribute (attr_key) (attr_value)) + ) + ) + ) +) diff --git a/test/corpus/container.txt b/test/corpus/container.txt index f0696e4..81bdf96 100644 --- a/test/corpus/container.txt +++ b/test/corpus/container.txt @@ -224,3 +224,46 @@ Foo: Baz { ) ) ) + +================================================================================ +Declare shapes in a container in single-line (Issue #11) +================================================================================ +primty: Primitive types { + Never: {"!"} + + Boolean: {bool} + + Textual: {char; str} +} + +-------------------------------------------------------------------------------- + +(source_file + (container + (container_key) + (label) + (block + (container + (container_key) + (block + (shape + (shape_key (string (string_fragment))) + ) + ) + ) + (container + (container_key) + (block + (shape (shape_key)) + ) + ) + (container + (container_key) + (block + (shape (shape_key)) + (shape (shape_key)) + ) + ) + ) + ) +) diff --git a/tree-sitter-d2.wasm b/tree-sitter-d2.wasm index df6db89..6811bb7 100755 Binary files a/tree-sitter-d2.wasm and b/tree-sitter-d2.wasm differ