diff --git a/grammar.js b/grammar.js index 2c28145..eb42878 100644 --- a/grammar.js +++ b/grammar.js @@ -10,8 +10,6 @@ const PREC = { ATTRIBUTE_KEY: 0, }; -spaces = /[ \t]/; - module.exports = grammar({ name: "d2", @@ -22,28 +20,17 @@ module.exports = grammar({ word: ($) => $._identifier, - conflicts: ($) => [ - [$._connection_path, $.container], - //[$.shape_key], - /* - [$._shape_path], - [$._shape_block], - [$._shape_block_definition], - [$._style_attr_block], - [$._inner_style_attribute], - [$._emptyline], -*/ - ], + conflicts: ($) => [[$._connection_path, $.container]], rules: { - source_file: ($) => repeat($._new_root_definition), + source_file: ($) => repeat($._root_definition), - _new_root_definition: ($) => + _root_definition: ($) => choice( $._eol, seq( choice( - alias($._new_root_attribute, $.attribute), + alias($._root_attribute, $.attribute), $.shape, $.container, $.connection @@ -57,17 +44,14 @@ module.exports = grammar({ connection: ($) => seq( $._connection_path, - repeat1(seq($._arrow, $._connection_path)), + repeat1(seq($.arrow, $._connection_path)), optional(seq($._colon, $.label)) ), _connection_path: ($) => seq( repeat( - prec( - PREC.CONNECTION, - seq(alias($.shape_key, $.container_key), $._dot) - ) + prec(PREC.CONNECTION, seq(alias($.shape_key, $.container_key), $.dot)) ), $.shape_key ), @@ -80,24 +64,24 @@ module.exports = grammar({ seq( alias($.shape_key, $.container_key), choice( - seq($._dot, choice($.shape, $.container)), + seq($.dot, choice($.shape, $.container)), seq( seq( optional(seq($._colon, optional($.label))), - optional(seq(alias($._new_container_block, $.block))) + optional(seq(alias($._container_block, $.block))) ) ) ) ) ), - _new_container_block: ($) => + _container_block: ($) => prec( PREC.CONTAINER, - seq("{", repeat($._new_container_block_definition), "}") + seq("{", repeat($._container_block_definition), "}") ), - _new_container_block_definition: ($) => + _container_block_definition: ($) => prec( PREC.CONTAINER, choice($._eol, seq(choice($.shape, $.container, $.connection), $._end)) @@ -112,10 +96,10 @@ module.exports = grammar({ $.shape_key, optional( choice( - seq($._dot, alias($._style_attribute, $.attribute)), + seq($.dot, alias($._style_attribute, $.attribute)), seq( optional(seq($._colon, optional($.label))), - optional(seq(alias($._new_shape_block, $.block))) + optional(seq(alias($._shape_block, $.block))) ) ) ) @@ -127,14 +111,14 @@ module.exports = grammar({ _identifier: ($) => token(prec(PREC.IDENTIFIER, /\-?([\w\d]+|([\w\d]+( +|\-)[\w\d]+)+)/)), - _new_shape_block: ($) => - prec(PREC.SHAPE, seq("{", repeat($._new_shape_block_definition), "}")), + _shape_block: ($) => + prec(PREC.SHAPE, seq("{", repeat($._shape_block_definition), "}")), - _new_shape_block_definition: ($) => prec(PREC.SHAPE, choice($._eol)), + _shape_block_definition: ($) => prec(PREC.SHAPE, choice($._eol)), // attributes - _new_root_attribute: ($) => + _root_attribute: ($) => seq(alias($._root_attr_key, $.attr_key), $._colon, $.attr_value), _root_attr_key: ($) => @@ -176,7 +160,7 @@ module.exports = grammar({ seq( alias("style", $.attr_key), choice( - seq($._dot, alias($._inner_style_attribute, $.attribute)), + seq($.dot, alias($._inner_style_attribute, $.attribute)), seq($._colon, alias($._style_attribute_block, $.block)) ) ) @@ -229,95 +213,12 @@ module.exports = grammar({ // -------------------------------------------- - // source_file: ($) => repeat($._root_definition), - /* - - _root_definition: ($) => - choice( - $._emptyline, - $._root_attribute, - $.connection, - $._shape_definition - ), - - _shape_definition: ($) => - seq( - $._shape_path, - optional( - choice( - seq($.dot, $._shape_attribute), - seq( - optional(seq($._colon, optional(seq(spaces, $.label)))), - optional(alias($._shape_block, $.block)) - ) - ) - ), - $._end - ), - - _shape_path: ($) => - seq( - spaces, - repeat(seq(alias($.shape_key, $.container_key), spaces, $.dot, spaces)), - $.shape_key - ), - - - _shape_block: ($) => - seq( - spaces, - "{", - repeat(choice($._emptyline, seq(spaces, $._shape_block_definition))), - optional(seq($._shape_block_definition, optional($._end))), - spaces, - "}" - ), - - _shape_block_definition: ($) => - choice($.connection, $._shape_definition, $._shape_attribute), - - _shape_attribute: ($) => - choice( - seq(alias($._shape_attr_key, $.attr_key), $._colon, $.attr_value), - $._style_attribute - ), - - _style_attribute: ($) => - seq( - alias("style", $.attr_key), - choice( - seq($.dot, $._inner_style_attribute), - seq($._colon, alias($._style_attr_block, $.block)) - ) - ), - - _style_attr_block: ($) => - seq( - spaces, - "{", - spaces, - repeat(choice($._emptyline, seq($._inner_style_attribute, $._end))), - optional(seq($._inner_style_attribute, optional($._end))), - spaces, - "}" - ), - - _inner_style_attribute: ($) => - seq(spaces, alias($._style_attr_key, $.attr_key), $._colon, $.attr_value), - - _connection_attribute: ($) => - seq(alias($._connection_attr_key, $.attr_key), $._colon, $.attr_value), - */ - _dash: ($) => token.immediate("-"), _colon: ($) => seq(":"), - _arrow: ($) => seq($.arrow), - arrow: ($) => token(prec(PREC.ARROW, choice(/-+>/, /--+/, /<-+/, /<-+>/))), - _dot: ($) => seq($.dot), dot: ($) => token("."), _unquoted_string: ($) => @@ -332,9 +233,6 @@ module.exports = grammar({ line_comment: ($) => token(prec(PREC.COMMENT, seq("#", /.*/))), - _word: ($) => /[\w\d]+/, - - _emptyline: ($) => prec(-1, seq(spaces, $._eol)), _eol: ($) => choice("\n", "\0"), _end: ($) => seq(choice(";", $._eol)), }, diff --git a/queries/highlights.scm b/queries/highlights.scm index 2e9f129..71fdff8 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -1,8 +1,3 @@ -; Special (treesitter don't overwrite) -;------------------------------------------------------------------------------- - -(ERROR) @error -; (invalid (_) @error) ;------------------------------------------------------------------------------- (container_key) @constant @@ -15,7 +10,7 @@ (container_key (string) @string.special) (shape_key (string) @string) (label) @string -; (attr_value) @string +(attr_value) @string ; Comments ;------------------------------------------------------------------------------- @@ -42,4 +37,3 @@ ;------------------------------------------------------------------------------- (ERROR) @error -; (invalid (_) @error) diff --git a/src/grammar.json b/src/grammar.json index 48a9f29..d2cd3a7 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -6,10 +6,10 @@ "type": "REPEAT", "content": { "type": "SYMBOL", - "name": "_new_root_definition" + "name": "_root_definition" } }, - "_new_root_definition": { + "_root_definition": { "type": "CHOICE", "members": [ { @@ -26,7 +26,7 @@ "type": "ALIAS", "content": { "type": "SYMBOL", - "name": "_new_root_attribute" + "name": "_root_attribute" }, "named": true, "value": "attribute" @@ -67,7 +67,7 @@ "members": [ { "type": "SYMBOL", - "name": "_arrow" + "name": "arrow" }, { "type": "SYMBOL", @@ -121,7 +121,7 @@ }, { "type": "SYMBOL", - "name": "_dot" + "name": "dot" } ] } @@ -156,7 +156,7 @@ "members": [ { "type": "SYMBOL", - "name": "_dot" + "name": "dot" }, { "type": "CHOICE", @@ -218,7 +218,7 @@ "type": "ALIAS", "content": { "type": "SYMBOL", - "name": "_new_container_block" + "name": "_container_block" }, "named": true, "value": "block" @@ -239,7 +239,7 @@ ] } }, - "_new_container_block": { + "_container_block": { "type": "PREC", "value": 2, "content": { @@ -253,7 +253,7 @@ "type": "REPEAT", "content": { "type": "SYMBOL", - "name": "_new_container_block_definition" + "name": "_container_block_definition" } }, { @@ -263,7 +263,7 @@ ] } }, - "_new_container_block_definition": { + "_container_block_definition": { "type": "PREC", "value": 2, "content": { @@ -323,7 +323,7 @@ "members": [ { "type": "SYMBOL", - "name": "_dot" + "name": "dot" }, { "type": "ALIAS", @@ -378,7 +378,7 @@ "type": "ALIAS", "content": { "type": "SYMBOL", - "name": "_new_shape_block" + "name": "_shape_block" }, "named": true, "value": "block" @@ -443,7 +443,7 @@ } } }, - "_new_shape_block": { + "_shape_block": { "type": "PREC", "value": 3, "content": { @@ -457,7 +457,7 @@ "type": "REPEAT", "content": { "type": "SYMBOL", - "name": "_new_shape_block_definition" + "name": "_shape_block_definition" } }, { @@ -467,7 +467,7 @@ ] } }, - "_new_shape_block_definition": { + "_shape_block_definition": { "type": "PREC", "value": 3, "content": { @@ -480,7 +480,7 @@ ] } }, - "_new_root_attribute": { + "_root_attribute": { "type": "SEQ", "members": [ { @@ -605,7 +605,7 @@ "members": [ { "type": "SYMBOL", - "name": "_dot" + "name": "dot" }, { "type": "ALIAS", @@ -836,15 +836,6 @@ } ] }, - "_arrow": { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "arrow" - } - ] - }, "arrow": { "type": "TOKEN", "content": { @@ -873,15 +864,6 @@ } } }, - "_dot": { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "dot" - } - ] - }, "dot": { "type": "TOKEN", "content": { @@ -994,27 +976,6 @@ } } }, - "_word": { - "type": "PATTERN", - "value": "[\\w\\d]+" - }, - "_emptyline": { - "type": "PREC", - "value": -1, - "content": { - "type": "SEQ", - "members": [ - { - "type": "PATTERN", - "value": "[ \\t]" - }, - { - "type": "SYMBOL", - "name": "_eol" - } - ] - } - }, "_eol": { "type": "CHOICE", "members": [ diff --git a/src/parser.c b/src/parser.c index aa657a9..3d1bdfa 100644 Binary files a/src/parser.c and b/src/parser.c differ diff --git a/test/old_corpus/attributes.txt b/test/old_corpus/attributes.txt deleted file mode 100644 index f48a55c..0000000 --- a/test/old_corpus/attributes.txt +++ /dev/null @@ -1,110 +0,0 @@ -================================================================================ -Root attribute -================================================================================ -direction: value - --------------------------------------------------------------------------------- - -(source_file - (attr_key) (attr_value) -) - -================================================================================ -Block shape attribute -================================================================================ -foo: { - shape: oval - - foo.bar.baz: { - shape: oval - } -} - --------------------------------------------------------------------------------- - -(source_file - (shape_key) - (block - (attr_key) (attr_value) - (container_key) (dot) (container_key) (dot) (shape_key) - (block - (attr_key) (attr_value) - ) - ) -) - - -================================================================================ -Inline shape attribute -================================================================================ -foo.shape: oval -foo.bar.baz.shape: oval - --------------------------------------------------------------------------------- - -(source_file - (shape_key) (dot) (attr_key) (attr_value) - (container_key) (dot) (container_key) (dot) (shape_key) (dot) (attr_key) (attr_value) -) - -================================================================================ -Inline style attribute -================================================================================ -foo.style.opacity: 5 - --------------------------------------------------------------------------------- - -(source_file - (shape_key) (dot) (attr_key) (dot) (attr_key) (attr_value) -) - -================================================================================ -Block style attributes -================================================================================ -foo.style: { - opacity: 5 - - fill: red; -} - --------------------------------------------------------------------------------- - -(source_file - (shape_key) (dot) (attr_key) - (block - (attr_key) (attr_value) - (attr_key) (attr_value) - ) -) - -================================================================================ -Inline block style attributes -================================================================================ -foo.style: { opacity: 5; fill: red; } -foo.style: { opacity: 5 } - --------------------------------------------------------------------------------- - -(source_file - (shape_key) (dot) (attr_key) - (block - (attr_key) (attr_value) - (attr_key) (attr_value) - ) - (shape_key) (dot) (attr_key) - (block - (attr_key) (attr_value) - ) -) - -================================================================================ -Color in string -================================================================================ -foo.style.fill: "#ffffff"; - --------------------------------------------------------------------------------- - -(source_file - (shape_key) (dot) (attr_key) (dot) (attr_key) (attr_value (string)) -) - diff --git a/test/old_corpus/connection.txt b/test/old_corpus/connection.txt deleted file mode 100644 index f1d8b64..0000000 --- a/test/old_corpus/connection.txt +++ /dev/null @@ -1,198 +0,0 @@ -================================================================================ -Simple connection -================================================================================ -foo--bar -biz->baz -biz<->baz -biz<-baz - --------------------------------------------------------------------------------- - -(source_file - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) -) - -================================================================================ -Formatted connection -================================================================================ -foo -- bar -biz -> baz -biz <-> baz -biz <- baz - --------------------------------------------------------------------------------- - -(source_file - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) -) - -================================================================================ -Connection with looooong arrow -================================================================================ -foo ----------- bar -biz ----------> baz -biz <---------> baz -biz <---------- baz - --------------------------------------------------------------------------------- - -(source_file - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) -) - - -================================================================================ -Complex identifier connection -================================================================================ -Foo Bar -- Biz Baz --Bar-Foo- <- -Baz-Biz- - --------------------------------------------------------------------------------- - -(source_file - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) -) - -================================================================================ -Inline connection -================================================================================ -foo--bar->biz->baz - --------------------------------------------------------------------------------- - -(source_file - (connection - (shape_key) - (arrow) - (shape_key) - (arrow) - (shape_key) - (arrow) - (shape_key) - ) -) - -================================================================================ -Many connections inline -================================================================================ -foo--bar;biz->baz - --------------------------------------------------------------------------------- - -(source_file - (connection - (shape_key) - (arrow) - (shape_key) - ) - (connection - (shape_key) - (arrow) - (shape_key) - ) -) - -================================================================================ -Labeled connections -================================================================================ -foo--bar: Question? -bar -> baz: Yes - --------------------------------------------------------------------------------- - -(source_file - (connection - (shape_key) - (arrow) - (shape_key) - (label) - ) - (connection - (shape_key) - (arrow) - (shape_key) - (label) - ) -) - -================================================================================ -Connection of shapes inside a containers -================================================================================ -foo.biz.baz -> bar.baz.biz: Label - --------------------------------------------------------------------------------- - -(source_file - (connection - (container_key) (dot) (container_key) (dot) (shape_key) - (arrow) - (container_key) (dot) (container_key) (dot) (shape_key) - (label) - ) -) diff --git a/test/old_corpus/container.txt b/test/old_corpus/container.txt deleted file mode 100644 index d0b5691..0000000 --- a/test/old_corpus/container.txt +++ /dev/null @@ -1,97 +0,0 @@ -================================================================================ -Declare a shape inside a container -================================================================================ -foo.baz - --------------------------------------------------------------------------------- - -(source_file - (container_key) (dot) (shape_key) -) - -================================================================================ -Use quoted string as a shape key -================================================================================ -'foo'.'baz' - --------------------------------------------------------------------------------- - -(source_file - (container_key (string)) (dot) (shape_key (string)) -) - -================================================================================ -Declare container inside a container using block -================================================================================ - -foo: { - bar: { - baz: { - biz - } - } -} - --------------------------------------------------------------------------------- - -(source_file - (shape_key) - (block - (shape_key) - (block - (shape_key) - (block - (shape_key) - ) - ) - ) -) - -================================================================================ -Declare labaled container inside a labeled container using block -================================================================================ - -foo: Foo { - bar: Bar { - baz: Baz { - biz: Biz - } - } -} - --------------------------------------------------------------------------------- - -(source_file - (shape_key) (label) - (block - (shape_key) (label) - (block - (shape_key) (label) - (block - (shape_key) (label) - ) - ) - ) -) - -================================================================================ -Declare many shapes inside a container -================================================================================ - -foo: { - bar - biz - baz -} - --------------------------------------------------------------------------------- - -(source_file - (shape_key) - (block - (shape_key) - (shape_key) - (shape_key) - ) -) - diff --git a/test/old_corpus/shape.txt b/test/old_corpus/shape.txt deleted file mode 100644 index c73d2f7..0000000 --- a/test/old_corpus/shape.txt +++ /dev/null @@ -1,134 +0,0 @@ -================================================================================ -Simple shape key -================================================================================ -foo -bar - --------------------------------------------------------------------------------- - -(source_file - (shape_key) - (shape_key) -) - -================================================================================ -Complex shape key -================================================================================ - Foo bar --Biz-baz- - --------------------------------------------------------------------------------- - -(source_file - (shape_key) - (shape_key) -) - -================================================================================ -Use quoted string as a shape key -================================================================================ -'foo' - --------------------------------------------------------------------------------- - -(source_file - (shape_key (string)) -) - -================================================================================ -Define multiple shapes using semicolons -================================================================================ -a;b;c - --------------------------------------------------------------------------------- - -(source_file - (shape_key) - (shape_key) - (shape_key) -) - -================================================================================ -Labeled shapes -================================================================================ -a: Foo Bar -a: Foo Bar; b: Biz Baz - --------------------------------------------------------------------------------- - -(source_file - (shape_key) (label) - (shape_key) (label) - (shape_key) (label) -) - -================================================================================ -It should skip white spaces -================================================================================ - -foo - -bar : Foo Bar; baz - --------------------------------------------------------------------------------- - -(source_file - (shape_key) - (shape_key) (label) - (shape_key) -) - -================================================================================ -Shape block -================================================================================ - -foo: { - bar: { - baz: { - biz - } - } -} - --------------------------------------------------------------------------------- - -(source_file - (shape_key) - (block - (shape_key) - (block - (shape_key) - (block - (shape_key) - ) - ) - ) -) - -================================================================================ -Aliased shape block -================================================================================ - -foo: Foo { - bar: Bar { - baz: Baz { - biz: Biz - } - } -} - --------------------------------------------------------------------------------- - -(source_file - (shape_key) (label) - (block - (shape_key) (label) - (block - (shape_key) (label) - (block - (shape_key) (label) - ) - ) - ) -) -