diff --git a/grammar.js b/grammar.js index eb42878..7a4b946 100644 --- a/grammar.js +++ b/grammar.js @@ -1,9 +1,12 @@ const PREC = { COMMENT: -2, + EOL: -1, UNQUOTED_STRING: 0, CONTAINER: 2, CONNECTION: 2, SHAPE: 3, + CONTAINER_BLOCK: 4, + SHAPE_BLOCK: 4, IDENTIFIER: 0, ARROW: 0, ATTRIBUTE: 0, @@ -20,7 +23,10 @@ module.exports = grammar({ word: ($) => $._identifier, - conflicts: ($) => [[$._connection_path, $.container]], + conflicts: ($) => [ + [$._connection_path, $.container], + [$._container_block_definition, $._shape_block_definition], + ], rules: { source_file: ($) => repeat($._root_definition), @@ -83,7 +89,7 @@ module.exports = grammar({ _container_block_definition: ($) => prec( - PREC.CONTAINER, + PREC.CONTAINER_BLOCK, choice($._eol, seq(choice($.shape, $.container, $.connection), $._end)) ), @@ -114,7 +120,7 @@ module.exports = grammar({ _shape_block: ($) => prec(PREC.SHAPE, seq("{", repeat($._shape_block_definition), "}")), - _shape_block_definition: ($) => prec(PREC.SHAPE, choice($._eol)), + _shape_block_definition: ($) => prec(PREC.SHAPE_BLOCK, choice($._eol)), // attributes @@ -233,7 +239,7 @@ module.exports = grammar({ line_comment: ($) => token(prec(PREC.COMMENT, seq("#", /.*/))), - _eol: ($) => choice("\n", "\0"), + _eol: ($) => token(prec(PREC.EOL, choice("\n", "\0"))), _end: ($) => seq(choice(";", $._eol)), }, }); diff --git a/queries/highlights.scm b/queries/highlights.scm index 71fdff8..54a6672 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -3,6 +3,7 @@ (container_key) @constant (shape_key) @variable (attr_key) @property +(reserved) @error ; Literals ;------------------------------------------------------------------------------- diff --git a/src/grammar.json b/src/grammar.json index d2cd3a7..6e0a6fd 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -265,7 +265,7 @@ }, "_container_block_definition": { "type": "PREC", - "value": 2, + "value": 4, "content": { "type": "CHOICE", "members": [ @@ -469,7 +469,7 @@ }, "_shape_block_definition": { "type": "PREC", - "value": 3, + "value": 4, "content": { "type": "CHOICE", "members": [ @@ -977,17 +977,24 @@ } }, "_eol": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": "\n" - }, - { - "type": "STRING", - "value": "\u0000" + "type": "TOKEN", + "content": { + "type": "PREC", + "value": -1, + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "\n" + }, + { + "type": "STRING", + "value": "\u0000" + } + ] } - ] + } }, "_end": { "type": "SEQ", @@ -1022,6 +1029,10 @@ [ "_connection_path", "container" + ], + [ + "_container_block_definition", + "_shape_block_definition" ] ], "precedences": [], diff --git a/src/node-types.json b/src/node-types.json index fbd7a09..3a738c0 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -266,14 +266,6 @@ "named": true, "fields": {} }, - { - "type": "\u0000", - "named": false - }, - { - "type": "\n", - "named": false - }, { "type": "\"", "named": false diff --git a/src/parser.c b/src/parser.c index 3d1bdfa..c72ab26 100644 Binary files a/src/parser.c and b/src/parser.c differ diff --git a/test/corpus/container.txt b/test/corpus/container.txt index e95d05c..704d2b5 100644 --- a/test/corpus/container.txt +++ b/test/corpus/container.txt @@ -196,4 +196,31 @@ Foo biz bar: Biz biz Bar { ) ) +================================================================================ +Declare shapes sparsely in a container +================================================================================ +Foo: Baz { + + biz + + baz + + bar + + +} + +-------------------------------------------------------------------------------- + +(source_file + (container + (container_key) + (label) + (block + (shape (shape_key)) + (shape (shape_key)) + (shape (shape_key)) + ) + ) +) diff --git a/test/highlight/shape.d2 b/test/highlight/shape.d2 index ca98449..ef251c4 100644 --- a/test/highlight/shape.d2 +++ b/test/highlight/shape.d2 @@ -15,7 +15,7 @@ foo: Foo Bar # ^ string foo: Foo Bar { - # <- variable + # <- constant # ^ string # ^ punctuation.bracket diff --git a/tree-sitter-d2.wasm b/tree-sitter-d2.wasm index da01d16..07e5474 100755 Binary files a/tree-sitter-d2.wasm and b/tree-sitter-d2.wasm differ