diff --git a/examples/all.d2 b/examples/all.d2 index 191aaf6..f7936c0 100644 --- a/examples/all.d2 +++ b/examples/all.d2 @@ -1,2 +1,2 @@ -a.shape: oval +'a'.'B'.shape: oval b diff --git a/grammar.js b/grammar.js index b9bf2e1..7bf3b5e 100644 --- a/grammar.js +++ b/grammar.js @@ -8,9 +8,10 @@ module.exports = grammar({ word: ($) => $._word, conflicts: ($) => [ - [$.identifier], + [$.shape_key], [$.arrow], - [$._identifier], + [$._shape_path], + [$._shape_key], [$._shape_block], [$._shape_block_definition], [$._style_attr_block], @@ -19,25 +20,29 @@ module.exports = grammar({ ], rules: { - source_file: ($) => repeat($._definition), + source_file: ($) => repeat($._root_definition), - _definition: ($) => - choice($._emptyline, $._root_attribute, $.connection, $.shape), + _root_definition: ($) => + choice( + $._emptyline, + $._root_attribute, + $.connection, + $._shape_definition + ), connection: ($) => seq( - $._identifier, - repeat1(seq($._arrow, $._identifier)), + $._shape_path, + repeat1(seq($._arrow, $._shape_path)), optional( choice(seq($.dot, $._connection_attribute), seq($._colon, $.label)) ), $._end ), - shape: ($) => + _shape_definition: ($) => seq( - $._identifier, - repeat(seq($.dot, $._identifier)), + $._shape_path, optional( choice( seq($.dot, $._shape_attribute), @@ -50,6 +55,12 @@ module.exports = grammar({ $._end ), + _shape_path: ($) => + choice( + $._shape_key, + seq($._shape_key, repeat1(seq($.dot, $._shape_key))) + ), + label: ($) => choice($.string, $._unquoted_string), attr_value: ($) => seq(spaces, choice($.string, $._unquoted_string)), @@ -80,7 +91,7 @@ module.exports = grammar({ ), _shape_block_definition: ($) => - choice($.connection, $.shape, $._shape_attribute), + choice($.connection, $._shape_definition, $._shape_attribute), _shape_attribute: ($) => choice( @@ -146,16 +157,19 @@ module.exports = grammar({ _connection_attr_key: ($) => choice("source-arrowhead", "target-arrowhead"), - _identifier: ($) => seq(spaces, $.identifier), + _shape_key: ($) => seq(spaces, $.shape_key), - identifier: ($) => - seq( - optional($._dash), - choice( - $._word, - repeat1(seq($._word, choice(spaces, $._dash), $._word)) - ), - optional($._dash) + shape_key: ($) => + choice( + $.string, + seq( + optional($._dash), + choice( + $._word, + repeat1(seq($._word, choice(repeat1(" "), $._dash), $._word)) + ), + optional($._dash) + ) ), _colon: ($) => seq(spaces, ":"), diff --git a/queries/highlights.scm b/queries/highlights.scm index fd3236d..6072566 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -5,7 +5,7 @@ (invalid (_) @error) ;------------------------------------------------------------------------------- -(identifier) @variable +(shape_key) @variable (attr_key) @keyword ; Literals @@ -26,7 +26,6 @@ (arrow) @operator [ - (dot) ":" ";" ] @punctuation.delimiter diff --git a/src/grammar.json b/src/grammar.json index ad55347..b312705 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -6,10 +6,10 @@ "type": "REPEAT", "content": { "type": "SYMBOL", - "name": "_definition" + "name": "_root_definition" } }, - "_definition": { + "_root_definition": { "type": "CHOICE", "members": [ { @@ -26,7 +26,7 @@ }, { "type": "SYMBOL", - "name": "shape" + "name": "_shape_definition" } ] }, @@ -35,7 +35,7 @@ "members": [ { "type": "SYMBOL", - "name": "_identifier" + "name": "_shape_path" }, { "type": "REPEAT1", @@ -48,7 +48,7 @@ }, { "type": "SYMBOL", - "name": "_identifier" + "name": "_shape_path" } ] } @@ -98,28 +98,12 @@ } ] }, - "shape": { + "_shape_definition": { "type": "SEQ", "members": [ { "type": "SYMBOL", - "name": "_identifier" - }, - { - "type": "REPEAT", - "content": { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "dot" - }, - { - "type": "SYMBOL", - "name": "_identifier" - } - ] - } + "name": "_shape_path" }, { "type": "CHOICE", @@ -204,6 +188,40 @@ } ] }, + "_shape_path": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_shape_key" + }, + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_shape_key" + }, + { + "type": "REPEAT1", + "content": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "dot" + }, + { + "type": "SYMBOL", + "name": "_shape_key" + } + ] + } + } + ] + } + ] + }, "label": { "type": "CHOICE", "members": [ @@ -428,7 +446,7 @@ }, { "type": "SYMBOL", - "name": "shape" + "name": "_shape_definition" }, { "type": "SYMBOL", @@ -791,7 +809,7 @@ } ] }, - "_identifier": { + "_shape_key": { "type": "SEQ", "members": [ { @@ -812,84 +830,84 @@ }, { "type": "SYMBOL", - "name": "identifier" + "name": "shape_key" } ] }, - "identifier": { - "type": "SEQ", + "shape_key": { + "type": "CHOICE", "members": [ { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "_dash" - }, - { - "type": "BLANK" - } - ] + "type": "SYMBOL", + "name": "string" }, { - "type": "CHOICE", + "type": "SEQ", "members": [ { - "type": "SYMBOL", - "name": "_word" + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_dash" + }, + { + "type": "BLANK" + } + ] }, { - "type": "REPEAT1", - "content": { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_word" - }, - { - "type": "CHOICE", + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_word" + }, + { + "type": "REPEAT1", + "content": { + "type": "SEQ", "members": [ { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { + "type": "SYMBOL", + "name": "_word" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "REPEAT1", + "content": { "type": "STRING", "value": " " - }, - { - "type": "STRING", - "value": "\t" } - ] - } + }, + { + "type": "SYMBOL", + "name": "_dash" + } + ] }, { "type": "SYMBOL", - "name": "_dash" + "name": "_word" } ] - }, - { - "type": "SYMBOL", - "name": "_word" } - ] - } - } - ] - }, - { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "_dash" + } + ] }, { - "type": "BLANK" + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_dash" + }, + { + "type": "BLANK" + } + ] } ] } @@ -1214,13 +1232,16 @@ ], "conflicts": [ [ - "identifier" + "shape_key" ], [ "arrow" ], [ - "_identifier" + "_shape_path" + ], + [ + "_shape_key" ], [ "_shape_block" diff --git a/src/node-types.json b/src/node-types.json index 76ba27c..5576e31 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -53,7 +53,11 @@ "named": true }, { - "type": "shape", + "type": "label", + "named": true + }, + { + "type": "shape_key", "named": true } ] @@ -84,21 +88,16 @@ "named": true }, { - "type": "identifier", + "type": "label", "named": true }, { - "type": "label", + "type": "shape_key", "named": true } ] } }, - { - "type": "identifier", - "named": true, - "fields": {} - }, { "type": "invalid", "named": true, @@ -142,35 +141,15 @@ } }, { - "type": "shape", + "type": "shape_key", "named": true, "fields": {}, "children": { - "multiple": true, - "required": true, + "multiple": false, + "required": false, "types": [ { - "type": "attr_key", - "named": true - }, - { - "type": "attr_value", - "named": true - }, - { - "type": "block", - "named": true - }, - { - "type": "dot", - "named": true - }, - { - "type": "identifier", - "named": true - }, - { - "type": "label", + "type": "string", "named": true } ] @@ -192,16 +171,28 @@ "type": "attr_value", "named": true }, + { + "type": "block", + "named": true + }, { "type": "connection", "named": true }, + { + "type": "dot", + "named": true + }, { "type": "invalid", "named": true }, { - "type": "shape", + "type": "label", + "named": true + }, + { + "type": "shape_key", "named": true } ] diff --git a/src/parser.c b/src/parser.c index f7a11ca..b256edf 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 e0b2f09..19f2777 100644 --- a/test/corpus/attributes.txt +++ b/test/corpus/attributes.txt @@ -23,12 +23,14 @@ foo: { -------------------------------------------------------------------------------- (source_file - (shape (identifier) (block + (shape_key) + (block (attr_key) (attr_value) - (shape (identifier) (dot) (identifier) (dot) (identifier) (block + (shape_key) (dot) (shape_key) (dot) (shape_key) + (block (attr_key) (attr_value) - )) - )) + ) + ) ) @@ -41,8 +43,8 @@ foo.bar.baz.shape: oval -------------------------------------------------------------------------------- (source_file - (shape (identifier) (dot) (attr_key) (attr_value)) - (shape (identifier) (dot) (identifier) (dot) (identifier) (dot) (attr_key) (attr_value)) + (shape_key) (dot) (attr_key) (attr_value) + (shape_key) (dot) (shape_key) (dot) (shape_key) (dot) (attr_key) (attr_value) ) ================================================================================ @@ -53,7 +55,7 @@ foo.style.opacity: 5 -------------------------------------------------------------------------------- (source_file - (shape (identifier) (dot) (attr_key) (dot) (attr_key) (attr_value)) + (shape_key) (dot) (attr_key) (dot) (attr_key) (attr_value) ) ================================================================================ @@ -68,12 +70,10 @@ foo.style: { -------------------------------------------------------------------------------- (source_file - (shape - (identifier) (dot) (attr_key) - (block - (attr_key) (attr_value) - (attr_key) (attr_value) - ) + (shape_key) (dot) (attr_key) + (block + (attr_key) (attr_value) + (attr_key) (attr_value) ) ) @@ -86,18 +86,14 @@ foo.style: { opacity: 5 } -------------------------------------------------------------------------------- (source_file - (shape - (identifier) (dot) (attr_key) - (block - (attr_key) (attr_value) - (attr_key) (attr_value) - ) + (shape_key) (dot) (attr_key) + (block + (attr_key) (attr_value) + (attr_key) (attr_value) ) - (shape - (identifier) (dot) (attr_key) - (block - (attr_key) (attr_value) - ) + (shape_key) (dot) (attr_key) + (block + (attr_key) (attr_value) ) ) diff --git a/test/corpus/connection.txt b/test/corpus/connection.txt index 9368c63..e79a12d 100644 --- a/test/corpus/connection.txt +++ b/test/corpus/connection.txt @@ -10,24 +10,24 @@ biz<-baz (source_file (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) ) @@ -43,24 +43,24 @@ biz <- baz (source_file (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) ) @@ -75,14 +75,14 @@ Foo Bar -- Biz Baz (source_file (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) ) @@ -95,13 +95,13 @@ foo--bar->biz->baz (source_file (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) ) @@ -114,14 +114,14 @@ foo--bar;biz->baz (source_file (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) ) ) @@ -135,15 +135,31 @@ bar -> baz: Yes (source_file (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) (label) ) (connection - (identifier) + (shape_key) (arrow) - (identifier) + (shape_key) + (label) + ) +) + +================================================================================ +Connection using shape path +================================================================================ +foo.biz.baz -> bar.baz.biz: Label + +-------------------------------------------------------------------------------- + +(source_file + (connection + (shape_key) (dot) (shape_key) (dot) (shape_key) + (arrow) + (shape_key) (dot) (shape_key) (dot) (shape_key) (label) ) ) diff --git a/test/corpus/shape.txt b/test/corpus/shape.txt index a10a67f..c814671 100644 --- a/test/corpus/shape.txt +++ b/test/corpus/shape.txt @@ -7,8 +7,8 @@ bar -------------------------------------------------------------------------------- (source_file - (shape (identifier)) - (shape (identifier)) + (shape_key) + (shape_key) ) ================================================================================ @@ -20,37 +20,43 @@ Complex identifier -------------------------------------------------------------------------------- (source_file - (shape (identifier)) - (shape (identifier)) + (shape_key) + (shape_key) ) ================================================================================ -Inline shapes +Declare a shape inside a container ================================================================================ foo.baz -------------------------------------------------------------------------------- (source_file - (shape - (identifier) - (dot) - (identifier) - ) + (shape_key) (dot) (shape_key) ) +================================================================================ +Use quoted string as a shape key +================================================================================ +'foo'.'baz' + +-------------------------------------------------------------------------------- + +(source_file + (shape_key (string)) (dot) (shape_key (string)) +) ================================================================================ -Many shapes inline +Define multiple shapes using semicolons ================================================================================ a;b;c -------------------------------------------------------------------------------- (source_file - (shape (identifier)) - (shape (identifier)) - (shape (identifier)) + (shape_key) + (shape_key) + (shape_key) ) ================================================================================ @@ -62,9 +68,9 @@ a: Foo Bar; b: Biz Baz -------------------------------------------------------------------------------- (source_file - (shape (identifier) (label)) - (shape (identifier) (label)) - (shape (identifier) (label)) + (shape_key) (label) + (shape_key) (label) + (shape_key) (label) ) ================================================================================ @@ -78,9 +84,9 @@ bar : Foo Bar; baz -------------------------------------------------------------------------------- (source_file - (shape (identifier)) - (shape (identifier) (label)) - (shape (identifier)) + (shape_key) + (shape_key) (label) + (shape_key) ) ================================================================================ @@ -98,13 +104,16 @@ foo: { -------------------------------------------------------------------------------- (source_file - (shape (identifier) (block - (shape (identifier) (block - (shape (identifier) (block - (shape (identifier)) - )) - )) - )) + (shape_key) + (block + (shape_key) + (block + (shape_key) + (block + (shape_key) + ) + ) + ) ) ================================================================================ @@ -122,12 +131,15 @@ foo: Foo { -------------------------------------------------------------------------------- (source_file - (shape (identifier) (label) (block - (shape (identifier) (label) (block - (shape (identifier) (label) (block - (shape (identifier) (label)) - )) - )) - )) + (shape_key) (label) + (block + (shape_key) (label) + (block + (shape_key) (label) + (block + (shape_key) (label) + ) + ) + ) ) diff --git a/test/highlight/keyword.d2 b/test/highlight/keyword.d2 index 98273d0..98c3151 100644 --- a/test/highlight/keyword.d2 +++ b/test/highlight/keyword.d2 @@ -4,7 +4,7 @@ direction: right; # ^ string # ^ punctuation.delimiter -shape: oval; +shape: oval # <- error # ^ error diff --git a/test/highlight/shape.d2 b/test/highlight/shape.d2 index 2043f77..daba794 100644 --- a/test/highlight/shape.d2 +++ b/test/highlight/shape.d2 @@ -1,8 +1,6 @@ foo.baz.biz # <- variable -# ^ punctuation.delimiter # ^ variable -# ^ punctuation.delimiter # ^ variable foo: Foo Bar diff --git a/tree-sitter-d2.wasm b/tree-sitter-d2.wasm index de14719..b5be90e 100755 Binary files a/tree-sitter-d2.wasm and b/tree-sitter-d2.wasm differ