diff --git a/.gitignore b/.gitignore index 42d67a4..74045d4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,7 @@ .envrc .direnv +# test data +test.d2 # produced by d2 *.svg diff --git a/grammar.js b/grammar.js index 7e6bf9d..bd702d7 100644 --- a/grammar.js +++ b/grammar.js @@ -1,155 +1,194 @@ -const spaces = repeat(choice(" ", "\t")); +const PREC = { + COMMENT: -2, + EOL: -1, + UNQUOTED_STRING: 0, + CONTAINER: 2, + CONNECTION: 2, + SHAPE: 3, + IDENTIFIER: 0, + ARROW: 0, + ATTRIBUTE: 0, + ATTRIBUTE_KEY: 0, +}; module.exports = grammar({ name: "d2", - extras: ($) => [$.line_comment], - - word: ($) => $._word, - - conflicts: ($) => [ - [$.shape_key], - [$._shape_path], - [$._shape_block], - [$._shape_block_definition], - [$._style_attr_block], - [$._inner_style_attribute], - [$._emptyline], + extras: ($) => [ + /[ \f\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/, + $.line_comment, ], + word: ($) => $._identifier, + + conflicts: ($) => [[$._connection_path, $.container]], + rules: { source_file: ($) => repeat($._root_definition), _root_definition: ($) => choice( - $._emptyline, - $._root_attribute, - $.connection, - $._shape_definition + $._eol, + seq( + choice( + alias($._root_attribute, $.attribute), + $.shape, + $.container, + $.connection + ), + $._end + ) ), + // connections + connection: ($) => seq( - $._shape_path, - repeat1(seq($._arrow, $._shape_path)), - optional( - choice(seq($.dot, $._connection_attribute), seq($._colon, $.label)) - ), - $._end + $._connection_path, + repeat1(seq($.arrow, $._connection_path)), + optional(seq($._colon, $.label)) ), - _shape_definition: ($) => + _connection_path: ($) => seq( - $._shape_path, - optional( - choice( - seq($.dot, $._shape_attribute), - seq( - optional(seq($._colon, optional(seq(spaces, $.label)))), - optional(alias($._shape_block, $.block)) - ) - ) + repeat( + prec(PREC.CONNECTION, seq(alias($.shape_key, $.container_key), $.dot)) ), - $._end - ), - - _shape_path: ($) => - seq( - spaces, - repeat(seq(alias($.shape_key, $.container_key), spaces, $.dot, spaces)), $.shape_key ), - shape_key: ($) => - choice( - $.string, + // containers + + container: ($) => + prec( + PREC.CONTAINER, seq( - optional($._dash), + alias($.shape_key, $.container_key), choice( - $._word, - repeat1(seq($._word, choice(repeat1(" "), $._dash), $._word)) - ), - optional($._dash) + seq($.dot, choice($.shape, $.container)), + seq( + seq( + optional(seq($._colon, optional($.label))), + optional(seq(alias($._container_block, $.block))) + ) + ) + ) ) ), - _dash: ($) => token.immediate("-"), + _container_block: ($) => + seq("{", repeat($._container_block_definition), "}"), - label: ($) => choice($.string, $._unquoted_string), + _container_block_definition: ($) => + choice( + $._eol, + seq( + choice($.shape, $.container, $.connection, $._shape_attribute), + $._end + ) + ), - attr_value: ($) => seq(spaces, choice($.string, $._unquoted_string)), + // shapes + + shape: ($) => + prec( + PREC.SHAPE, + seq( + $.shape_key, + optional( + choice( + seq($.dot, alias($._style_attribute, $.attribute)), + seq(optional(seq($._colon, optional($.label)))) + ) + ) + ) + ), + + shape_key: ($) => choice($.string, seq($._identifier, optional($._dash))), + + _identifier: ($) => + token(prec(PREC.IDENTIFIER, /\-?([\w\d]+|([\w\d]+( +|\-)[\w\d]+)+)/)), + + // attributes _root_attribute: ($) => + seq(alias($._root_attr_key, $.attr_key), $._colon, $.attr_value), + + _root_attr_key: ($) => choice( - seq( - alias($._root_attr_key, $.attr_key), - $._colon, - $.attr_value, - $._end - ), - alias(seq($._shape_attribute, $._end), $.invalid) + "direction", + // reserved but doesn't affected for root + alias( + choice( + "shape", + "label", + "constraint", + "icon", + "style", + $._common_style_attr_key, + $._text_attr_key + ), + $.reserved + ) ), - _root_attr_key: ($) => "direction", - - _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 + alias($._base_shape_attribute, $.attribute), + alias($._style_attribute, $.attribute) ), - _style_attribute: ($) => - seq( - alias("style", $.attr_key), + _base_shape_attribute: ($) => + seq(alias($._shape_attr_key, $.attr_key), $._colon, $.attr_value), + + _shape_attr_key: ($) => + prec( + PREC.ATTRIBUTE_KEY, choice( - seq($.dot, $._inner_style_attribute), - seq($._colon, alias($._style_attr_block, $.block)) + "shape", + "label", + // sql + "constraint", + // image + "icon", + "width", + "height" ) ), - _style_attr_block: ($) => + _style_attribute: ($) => + prec( + PREC.ATTRIBUTE, + seq( + alias("style", $.attr_key), + choice( + seq($.dot, alias($._inner_style_attribute, $.attribute)), + seq($._colon, alias($._style_attribute_block, $.block)) + ) + ) + ), + + _style_attribute_block: ($) => seq( - spaces, "{", - spaces, - repeat(choice($._emptyline, seq($._inner_style_attribute, $._end))), - optional(seq($._inner_style_attribute, optional($._end))), - spaces, + repeat( + choice( + $._eol, + seq(alias($._inner_style_attribute, $.attribute), $._end) + ) + ), "}" ), _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), - - _shape_attr_key: ($) => - choice( - "shape", - "label", - // sql - "constraint", - // image - "icon", - "width", - "height" + prec( + PREC.ATTRIBUTE, + seq(alias($._style_attr_key, $.attr_key), $._colon, $.attr_value) ), - _style_attr_key: ($) => + _style_attr_key: ($) => choice($._common_style_attr_key, "3d"), + + _common_style_attr_key: ($) => choice( "opacity", "fill", @@ -161,7 +200,6 @@ module.exports = grammar({ "shadow", "multiple", "animated", - "3d", "link" ), @@ -169,15 +207,24 @@ module.exports = grammar({ _connection_attr_key: ($) => choice("source-arrowhead", "target-arrowhead"), - _colon: ($) => seq(spaces, ":"), + // - _arrow: ($) => seq(spaces, $.arrow), + label: ($) => choice($.string, $._unquoted_string), - arrow: ($) => choice(/-+>/, /--+/, /<-+/, /<-+>/), + attr_value: ($) => seq(choice($.string, $._unquoted_string)), - dot: ($) => token.immediate("."), + // -------------------------------------------- - _unquoted_string: ($) => token.immediate(/[^'"`\n;{}]+/), + _dash: ($) => token.immediate("-"), + + _colon: ($) => seq(":"), + + arrow: ($) => token(prec(PREC.ARROW, choice(/-+>/, /--+/, /<-+/, /<-+>/))), + + dot: ($) => token("."), + + _unquoted_string: ($) => + token(prec(PREC.UNQUOTED_STRING, /[\w\-?!]([^'"`\n;{}]*[\w\-?!])?/)), string: ($) => choice( @@ -186,12 +233,9 @@ module.exports = grammar({ seq("`", repeat(token.immediate(/[^`\n]+/)), "`") ), - line_comment: ($) => token(prec(-2, seq("#", /.*/))), + line_comment: ($) => token(prec(PREC.COMMENT, seq("#", /.*/))), - _word: ($) => /[\w\d]+/, - - _emptyline: ($) => seq(spaces, $._eof), - _eof: ($) => choice("\n", "\0"), - _end: ($) => seq(spaces, choice(";", $._eof)), + _eol: ($) => token(prec(PREC.EOL, "\n")), + _end: ($) => seq(choice(";", $._eol)), }, }); diff --git a/queries/highlights.scm b/queries/highlights.scm index f9e3551..54a6672 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -1,13 +1,9 @@ -; Special (treesitter don't overwrite) -;------------------------------------------------------------------------------- - -(ERROR) @error -(invalid (_) @error) ;------------------------------------------------------------------------------- (container_key) @constant (shape_key) @variable (attr_key) @property +(reserved) @error ; Literals ;------------------------------------------------------------------------------- @@ -42,4 +38,3 @@ ;------------------------------------------------------------------------------- (ERROR) @error -(invalid (_) @error) diff --git a/src/grammar.json b/src/grammar.json index 537f9ee..45a4ba0 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -1,6 +1,6 @@ { "name": "d2", - "word": "_word", + "word": "_identifier", "rules": { "source_file": { "type": "REPEAT", @@ -14,19 +14,42 @@ "members": [ { "type": "SYMBOL", - "name": "_emptyline" + "name": "_eol" }, { - "type": "SYMBOL", - "name": "_root_attribute" - }, - { - "type": "SYMBOL", - "name": "connection" - }, - { - "type": "SYMBOL", - "name": "_shape_definition" + "type": "SEQ", + "members": [ + { + "type": "CHOICE", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "_root_attribute" + }, + "named": true, + "value": "attribute" + }, + { + "type": "SYMBOL", + "name": "shape" + }, + { + "type": "SYMBOL", + "name": "container" + }, + { + "type": "SYMBOL", + "name": "connection" + } + ] + }, + { + "type": "SYMBOL", + "name": "_end" + } + ] } ] }, @@ -35,7 +58,7 @@ "members": [ { "type": "SYMBOL", - "name": "_shape_path" + "name": "_connection_path" }, { "type": "REPEAT1", @@ -44,11 +67,11 @@ "members": [ { "type": "SYMBOL", - "name": "_arrow" + "name": "arrow" }, { "type": "SYMBOL", - "name": "_shape_path" + "name": "_connection_path" } ] } @@ -57,228 +80,51 @@ "type": "CHOICE", "members": [ { - "type": "CHOICE", + "type": "SEQ", "members": [ { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "dot" - }, - { - "type": "SYMBOL", - "name": "_connection_attribute" - } - ] - }, - { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_colon" - }, - { - "type": "SYMBOL", - "name": "label" - } - ] - } - ] - }, - { - "type": "BLANK" - } - ] - }, - { - "type": "SYMBOL", - "name": "_end" - } - ] - }, - "_shape_definition": { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_shape_path" - }, - { - "type": "CHOICE", - "members": [ - { - "type": "CHOICE", - "members": [ - { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "dot" - }, - { - "type": "SYMBOL", - "name": "_shape_attribute" - } - ] - }, - { - "type": "SEQ", - "members": [ - { - "type": "CHOICE", - "members": [ - { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_colon" - }, - { - "type": "CHOICE", - "members": [ - { - "type": "SEQ", - "members": [ - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "SYMBOL", - "name": "label" - } - ] - }, - { - "type": "BLANK" - } - ] - } - ] - }, - { - "type": "BLANK" - } - ] - }, - { - "type": "CHOICE", - "members": [ - { - "type": "ALIAS", - "content": { - "type": "SYMBOL", - "name": "_shape_block" - }, - "named": true, - "value": "block" - }, - { - "type": "BLANK" - } - ] - } - ] - } - ] - }, - { - "type": "BLANK" - } - ] - }, - { - "type": "SYMBOL", - "name": "_end" - } - ] - }, - "_shape_path": { - "type": "SEQ", - "members": [ - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "REPEAT", - "content": { - "type": "SEQ", - "members": [ - { - "type": "ALIAS", - "content": { "type": "SYMBOL", - "name": "shape_key" + "name": "_colon" }, - "named": true, - "value": "container_key" - }, - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] + { + "type": "SYMBOL", + "name": "label" } - }, - { - "type": "SYMBOL", - "name": "dot" - }, - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] + ] + }, + { + "type": "BLANK" + } + ] + } + ] + }, + "_connection_path": { + "type": "SEQ", + "members": [ + { + "type": "REPEAT", + "content": { + "type": "PREC", + "value": 2, + "content": { + "type": "SEQ", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "shape_key" + }, + "named": true, + "value": "container_key" + }, + { + "type": "SYMBOL", + "name": "dot" } - } - ] + ] + } } }, { @@ -287,6 +133,249 @@ } ] }, + "container": { + "type": "PREC", + "value": 2, + "content": { + "type": "SEQ", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "shape_key" + }, + "named": true, + "value": "container_key" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "dot" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "shape" + }, + { + "type": "SYMBOL", + "name": "container" + } + ] + } + ] + }, + { + "type": "SEQ", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_colon" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "label" + }, + { + "type": "BLANK" + } + ] + } + ] + }, + { + "type": "BLANK" + } + ] + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "_container_block" + }, + "named": true, + "value": "block" + } + ] + }, + { + "type": "BLANK" + } + ] + } + ] + } + ] + } + ] + } + ] + } + }, + "_container_block": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "{" + }, + { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "_container_block_definition" + } + }, + { + "type": "STRING", + "value": "}" + } + ] + }, + "_container_block_definition": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_eol" + }, + { + "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" + } + ] + } + ] + }, + "shape": { + "type": "PREC", + "value": 3, + "content": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "shape_key" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "dot" + }, + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "_style_attribute" + }, + "named": true, + "value": "attribute" + } + ] + }, + { + "type": "SEQ", + "members": [ + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_colon" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "label" + }, + { + "type": "BLANK" + } + ] + } + ] + }, + { + "type": "BLANK" + } + ] + } + ] + } + ] + }, + { + "type": "BLANK" + } + ] + } + ] + } + }, "shape_key": { "type": "CHOICE", "members": [ @@ -298,57 +387,8 @@ "type": "SEQ", "members": [ { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "_dash" - }, - { - "type": "BLANK" - } - ] - }, - { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "_word" - }, - { - "type": "REPEAT1", - "content": { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_word" - }, - { - "type": "CHOICE", - "members": [ - { - "type": "REPEAT1", - "content": { - "type": "STRING", - "value": " " - } - }, - { - "type": "SYMBOL", - "name": "_dash" - } - ] - }, - { - "type": "SYMBOL", - "name": "_word" - } - ] - } - } - ] + "type": "SYMBOL", + "name": "_identifier" }, { "type": "CHOICE", @@ -366,461 +406,25 @@ } ] }, - "_dash": { - "type": "IMMEDIATE_TOKEN", + "_identifier": { + "type": "TOKEN", "content": { - "type": "STRING", - "value": "-" + "type": "PREC", + "value": 0, + "content": { + "type": "PATTERN", + "value": "\\-?([\\w\\d]+|([\\w\\d]+( +|\\-)[\\w\\d]+)+)" + } } }, - "label": { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "string" - }, - { - "type": "SYMBOL", - "name": "_unquoted_string" - } - ] - }, - "attr_value": { - "type": "SEQ", - "members": [ - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "string" - }, - { - "type": "SYMBOL", - "name": "_unquoted_string" - } - ] - } - ] - }, "_root_attribute": { - "type": "CHOICE", - "members": [ - { - "type": "SEQ", - "members": [ - { - "type": "ALIAS", - "content": { - "type": "SYMBOL", - "name": "_root_attr_key" - }, - "named": true, - "value": "attr_key" - }, - { - "type": "SYMBOL", - "name": "_colon" - }, - { - "type": "SYMBOL", - "name": "attr_value" - }, - { - "type": "SYMBOL", - "name": "_end" - } - ] - }, - { - "type": "ALIAS", - "content": { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_shape_attribute" - }, - { - "type": "SYMBOL", - "name": "_end" - } - ] - }, - "named": true, - "value": "invalid" - } - ] - }, - "_root_attr_key": { - "type": "STRING", - "value": "direction" - }, - "_shape_block": { "type": "SEQ", "members": [ - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "STRING", - "value": "{" - }, - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "_emptyline" - }, - { - "type": "SEQ", - "members": [ - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "SYMBOL", - "name": "_shape_block_definition" - } - ] - } - ] - } - }, - { - "type": "CHOICE", - "members": [ - { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_shape_block_definition" - }, - { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "_end" - }, - { - "type": "BLANK" - } - ] - } - ] - }, - { - "type": "BLANK" - } - ] - }, - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "STRING", - "value": "}" - } - ] - }, - "_shape_block_definition": { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "connection" - }, - { - "type": "SYMBOL", - "name": "_shape_definition" - }, - { - "type": "SYMBOL", - "name": "_shape_attribute" - } - ] - }, - "_shape_attribute": { - "type": "CHOICE", - "members": [ - { - "type": "SEQ", - "members": [ - { - "type": "ALIAS", - "content": { - "type": "SYMBOL", - "name": "_shape_attr_key" - }, - "named": true, - "value": "attr_key" - }, - { - "type": "SYMBOL", - "name": "_colon" - }, - { - "type": "SYMBOL", - "name": "attr_value" - } - ] - }, - { - "type": "SYMBOL", - "name": "_style_attribute" - } - ] - }, - "_style_attribute": { - "type": "SEQ", - "members": [ - { - "type": "ALIAS", - "content": { - "type": "STRING", - "value": "style" - }, - "named": true, - "value": "attr_key" - }, - { - "type": "CHOICE", - "members": [ - { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "dot" - }, - { - "type": "SYMBOL", - "name": "_inner_style_attribute" - } - ] - }, - { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_colon" - }, - { - "type": "ALIAS", - "content": { - "type": "SYMBOL", - "name": "_style_attr_block" - }, - "named": true, - "value": "block" - } - ] - } - ] - } - ] - }, - "_style_attr_block": { - "type": "SEQ", - "members": [ - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "STRING", - "value": "{" - }, - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "_emptyline" - }, - { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_inner_style_attribute" - }, - { - "type": "SYMBOL", - "name": "_end" - } - ] - } - ] - } - }, - { - "type": "CHOICE", - "members": [ - { - "type": "SEQ", - "members": [ - { - "type": "SYMBOL", - "name": "_inner_style_attribute" - }, - { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "_end" - }, - { - "type": "BLANK" - } - ] - } - ] - }, - { - "type": "BLANK" - } - ] - }, - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "STRING", - "value": "}" - } - ] - }, - "_inner_style_attribute": { - "type": "SEQ", - "members": [ - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, { "type": "ALIAS", "content": { "type": "SYMBOL", - "name": "_style_attr_key" + "name": "_root_attr_key" }, "named": true, "value": "attr_key" @@ -835,14 +439,84 @@ } ] }, - "_connection_attribute": { + "_root_attr_key": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "direction" + }, + { + "type": "ALIAS", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "shape" + }, + { + "type": "STRING", + "value": "label" + }, + { + "type": "STRING", + "value": "constraint" + }, + { + "type": "STRING", + "value": "icon" + }, + { + "type": "STRING", + "value": "style" + }, + { + "type": "SYMBOL", + "name": "_common_style_attr_key" + }, + { + "type": "SYMBOL", + "name": "_text_attr_key" + } + ] + }, + "named": true, + "value": "reserved" + } + ] + }, + "_shape_attribute": { + "type": "CHOICE", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "_base_shape_attribute" + }, + "named": true, + "value": "attribute" + }, + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "_style_attribute" + }, + "named": true, + "value": "attribute" + } + ] + }, + "_base_shape_attribute": { "type": "SEQ", "members": [ { "type": "ALIAS", "content": { "type": "SYMBOL", - "name": "_connection_attr_key" + "name": "_shape_attr_key" }, "named": true, "value": "attr_key" @@ -858,35 +532,180 @@ ] }, "_shape_attr_key": { - "type": "CHOICE", + "type": "PREC", + "value": 0, + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "shape" + }, + { + "type": "STRING", + "value": "label" + }, + { + "type": "STRING", + "value": "constraint" + }, + { + "type": "STRING", + "value": "icon" + }, + { + "type": "STRING", + "value": "width" + }, + { + "type": "STRING", + "value": "height" + } + ] + } + }, + "_style_attribute": { + "type": "PREC", + "value": 0, + "content": { + "type": "SEQ", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "style" + }, + "named": true, + "value": "attr_key" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "dot" + }, + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "_inner_style_attribute" + }, + "named": true, + "value": "attribute" + } + ] + }, + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_colon" + }, + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "_style_attribute_block" + }, + "named": true, + "value": "block" + } + ] + } + ] + } + ] + } + }, + "_style_attribute_block": { + "type": "SEQ", "members": [ { "type": "STRING", - "value": "shape" + "value": "{" + }, + { + "type": "REPEAT", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_eol" + }, + { + "type": "SEQ", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "_inner_style_attribute" + }, + "named": true, + "value": "attribute" + }, + { + "type": "SYMBOL", + "name": "_end" + } + ] + } + ] + } }, { "type": "STRING", - "value": "label" - }, - { - "type": "STRING", - "value": "constraint" - }, - { - "type": "STRING", - "value": "icon" - }, - { - "type": "STRING", - "value": "width" - }, - { - "type": "STRING", - "value": "height" + "value": "}" } ] }, + "_inner_style_attribute": { + "type": "PREC", + "value": 0, + "content": { + "type": "SEQ", + "members": [ + { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "_style_attr_key" + }, + "named": true, + "value": "attr_key" + }, + { + "type": "SYMBOL", + "name": "_colon" + }, + { + "type": "SYMBOL", + "name": "attr_value" + } + ] + } + }, "_style_attr_key": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_common_style_attr_key" + }, + { + "type": "STRING", + "value": "3d" + } + ] + }, + "_common_style_attr_key": { "type": "CHOICE", "members": [ { @@ -929,10 +748,6 @@ "type": "STRING", "value": "animated" }, - { - "type": "STRING", - "value": "3d" - }, { "type": "STRING", "value": "link" @@ -956,89 +771,97 @@ } ] }, - "_colon": { + "label": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "string" + }, + { + "type": "SYMBOL", + "name": "_unquoted_string" + } + ] + }, + "attr_value": { "type": "SEQ", "members": [ { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "string" + }, + { + "type": "SYMBOL", + "name": "_unquoted_string" + } + ] + } + ] + }, + "_dash": { + "type": "IMMEDIATE_TOKEN", + "content": { + "type": "STRING", + "value": "-" + } + }, + "_colon": { + "type": "SEQ", + "members": [ { "type": "STRING", "value": ":" } ] }, - "_arrow": { - "type": "SEQ", - "members": [ - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "SYMBOL", - "name": "arrow" - } - ] - }, "arrow": { - "type": "CHOICE", - "members": [ - { - "type": "PATTERN", - "value": "-+>" - }, - { - "type": "PATTERN", - "value": "--+" - }, - { - "type": "PATTERN", - "value": "<-+" - }, - { - "type": "PATTERN", - "value": "<-+>" + "type": "TOKEN", + "content": { + "type": "PREC", + "value": 0, + "content": { + "type": "CHOICE", + "members": [ + { + "type": "PATTERN", + "value": "-+>" + }, + { + "type": "PATTERN", + "value": "--+" + }, + { + "type": "PATTERN", + "value": "<-+" + }, + { + "type": "PATTERN", + "value": "<-+>" + } + ] } - ] + } }, "dot": { - "type": "IMMEDIATE_TOKEN", + "type": "TOKEN", "content": { "type": "STRING", "value": "." } }, "_unquoted_string": { - "type": "IMMEDIATE_TOKEN", + "type": "TOKEN", "content": { - "type": "PATTERN", - "value": "[^'\"`\\n;{}]+" + "type": "PREC", + "value": 0, + "content": { + "type": "PATTERN", + "value": "[\\w\\-?!]([^'\"`\\n;{}]*[\\w\\-?!])?" + } } }, "string": { @@ -1135,67 +958,20 @@ } } }, - "_word": { - "type": "PATTERN", - "value": "[\\w\\d]+" - }, - "_emptyline": { - "type": "SEQ", - "members": [ - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, - { - "type": "SYMBOL", - "name": "_eof" - } - ] - }, - "_eof": { - "type": "CHOICE", - "members": [ - { + "_eol": { + "type": "TOKEN", + "content": { + "type": "PREC", + "value": -1, + "content": { "type": "STRING", "value": "\n" - }, - { - "type": "STRING", - "value": "\u0000" } - ] + } }, "_end": { "type": "SEQ", "members": [ - { - "type": "REPEAT", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": " " - }, - { - "type": "STRING", - "value": "\t" - } - ] - } - }, { "type": "CHOICE", "members": [ @@ -1205,7 +981,7 @@ }, { "type": "SYMBOL", - "name": "_eof" + "name": "_eol" } ] } @@ -1213,6 +989,10 @@ } }, "extras": [ + { + "type": "PATTERN", + "value": "[ \\f\\t\\v\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff]" + }, { "type": "SYMBOL", "name": "line_comment" @@ -1220,25 +1000,8 @@ ], "conflicts": [ [ - "shape_key" - ], - [ - "_shape_path" - ], - [ - "_shape_block" - ], - [ - "_shape_block_definition" - ], - [ - "_style_attr_block" - ], - [ - "_inner_style_attribute" - ], - [ - "_emptyline" + "_connection_path", + "container" ] ], "precedences": [], diff --git a/src/node-types.json b/src/node-types.json index 2efdd90..810cf29 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -1,13 +1,18 @@ [ - { - "type": "arrow", - "named": true, - "fields": {} - }, { "type": "attr_key", "named": true, - "fields": {} + "fields": {}, + "children": { + "multiple": false, + "required": false, + "types": [ + { + "type": "reserved", + "named": true + } + ] + } }, { "type": "attr_value", @@ -25,12 +30,12 @@ } }, { - "type": "block", + "type": "attribute", "named": true, "fields": {}, "children": { "multiple": true, - "required": false, + "required": true, "types": [ { "type": "attr_key", @@ -40,28 +45,43 @@ "type": "attr_value", "named": true }, + { + "type": "attribute", + "named": true + }, { "type": "block", "named": true }, + { + "type": "dot", + "named": true + } + ] + } + }, + { + "type": "block", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "attribute", + "named": true + }, { "type": "connection", "named": true }, { - "type": "container_key", + "type": "container", "named": true }, { - "type": "dot", - "named": true - }, - { - "type": "label", - "named": true - }, - { - "type": "shape_key", + "type": "shape", "named": true } ] @@ -80,11 +100,38 @@ "named": true }, { - "type": "attr_key", + "type": "container_key", "named": true }, { - "type": "attr_value", + "type": "dot", + "named": true + }, + { + "type": "label", + "named": true + }, + { + "type": "shape_key", + "named": true + } + ] + } + }, + { + "type": "container", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "block", + "named": true + }, + { + "type": "container", "named": true }, { @@ -100,7 +147,7 @@ "named": true }, { - "type": "shape_key", + "type": "shape", "named": true } ] @@ -121,33 +168,6 @@ ] } }, - { - "type": "invalid", - "named": true, - "fields": {}, - "children": { - "multiple": true, - "required": false, - "types": [ - { - "type": "attr_key", - "named": true - }, - { - "type": "attr_value", - "named": true - }, - { - "type": "block", - "named": true - }, - { - "type": "dot", - "named": true - } - ] - } - }, { "type": "label", "named": true, @@ -163,6 +183,38 @@ ] } }, + { + "type": "reserved", + "named": true, + "fields": {} + }, + { + "type": "shape", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "attribute", + "named": true + }, + { + "type": "dot", + "named": true + }, + { + "type": "label", + "named": true + }, + { + "type": "shape_key", + "named": true + } + ] + } + }, { "type": "shape_key", "named": true, @@ -187,15 +239,7 @@ "required": false, "types": [ { - "type": "attr_key", - "named": true - }, - { - "type": "attr_value", - "named": true - }, - { - "type": "block", + "type": "attribute", "named": true }, { @@ -203,23 +247,11 @@ "named": true }, { - "type": "container_key", + "type": "container", "named": true }, { - "type": "dot", - "named": true - }, - { - "type": "invalid", - "named": true - }, - { - "type": "label", - "named": true - }, - { - "type": "shape_key", + "type": "shape", "named": true } ] @@ -230,22 +262,6 @@ "named": true, "fields": {} }, - { - "type": "\u0000", - "named": false - }, - { - "type": "\t", - "named": false - }, - { - "type": "\n", - "named": false - }, - { - "type": " ", - "named": false - }, { "type": "\"", "named": false @@ -274,6 +290,10 @@ "type": "animated", "named": false }, + { + "type": "arrow", + "named": true + }, { "type": "border-radius", "named": false @@ -282,6 +302,10 @@ "type": "constraint", "named": false }, + { + "type": "direction", + "named": false + }, { "type": "dot", "named": true diff --git a/src/parser.c b/src/parser.c index 00ca678..ab92f90 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 f48a55c..9017546 100644 --- a/test/corpus/attributes.txt +++ b/test/corpus/attributes.txt @@ -3,59 +3,75 @@ Root attribute ================================================================================ direction: value --------------------------------------------------------------------------------- - -(source_file - (attr_key) (attr_value) -) - -================================================================================ -Block shape attribute -================================================================================ -foo: { - shape: oval - - foo.bar.baz: { - shape: oval - } -} +shape: oval +label: ten +constraint: utehu +icon: huell +opacity: 1 +fill: red +stroke: red +stroke-width: 5 +stroke-dash: 4 +border-radius: 1 +font-color: red +shadow: true +multiple: true +animated: true +link: https://to +near: abc -------------------------------------------------------------------------------- (source_file - (shape_key) - (block - (attr_key) (attr_value) - (container_key) (dot) (container_key) (dot) (shape_key) - (block - (attr_key) (attr_value) - ) - ) -) + (attribute (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) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) + (attribute (attr_key (reserved)) (attr_value)) ) ================================================================================ -Inline style attribute +Style attribute ================================================================================ foo.style.opacity: 5 +foo.style.fill: red +foo.style.stroke: red +foo.style.stroke-width: 5 +foo.style.stroke-dash: 4 +foo.style.border-radius: 1 +foo.style.font-color: red +foo.style.shadow: true +foo.style.multiple: true +foo.style.animated: true +foo.style.link: https://to -------------------------------------------------------------------------------- (source_file - (shape_key) (dot) (attr_key) (dot) (attr_key) (attr_value) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) + (shape (shape_key) (dot) (attribute (attr_key) (dot) (attribute (attr_key) (attr_value)))) ) ================================================================================ @@ -63,48 +79,150 @@ Block style attributes ================================================================================ foo.style: { opacity: 5 - - fill: red; + fill: red + stroke: red + stroke-width: 5 + stroke-dash: 4 + border-radius: 1 + font-color: red + shadow: true + multiple: true + animated: true + link: https://to } -------------------------------------------------------------------------------- (source_file - (shape_key) (dot) (attr_key) - (block - (attr_key) (attr_value) - (attr_key) (attr_value) + (shape + (shape_key) (dot) + (attribute + (attr_key) + (block + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + ) + ) ) ) ================================================================================ -Inline block style attributes +Container attributes ================================================================================ -foo.style: { opacity: 5; fill: red; } -foo.style: { opacity: 5 } +foo: { + shape: oval + label: Baz + constraint: primary-key + icon: pathto + width: 100 + height: 200 +} -------------------------------------------------------------------------------- (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) + (container + (container_key) + (block + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + ) ) ) ================================================================================ -Color in string +Container style attributes ================================================================================ -foo.style.fill: "#ffffff"; +foo: { + style.opacity: 5 + style.fill: red + style.stroke: red + style.stroke-width: 5 + style.stroke-dash: 4 + style.border-radius: 1 + style.font-color: red + style.shadow: true + style.multiple: true + style.animated: true + style.link: https://to +} -------------------------------------------------------------------------------- (source_file - (shape_key) (dot) (attr_key) (dot) (attr_key) (attr_value (string)) + (container + (container_key) + (block + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + (attribute (attr_key) (dot) (attribute (attr_key) (attr_value))) + ) + ) +) + +================================================================================ +Block style attributes inside a container +================================================================================ +foo: { + style: { + opacity: 5 + fill: red + stroke: red + stroke-width: 5 + stroke-dash: 4 + border-radius: 1 + font-color: red + shadow: true + multiple: true + animated: true + link: https://to + } +} + +-------------------------------------------------------------------------------- + +(source_file + (container + (container_key) + (block + (attribute + (attr_key) + (block + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + (attribute (attr_key) (attr_value)) + ) + ) + ) + ) ) diff --git a/test/corpus/connection.txt b/test/corpus/connection.txt index f1d8b64..50ef980 100644 --- a/test/corpus/connection.txt +++ b/test/corpus/connection.txt @@ -196,3 +196,29 @@ foo.biz.baz -> bar.baz.biz: Label (label) ) ) + +================================================================================ +Connection of shapes inside a block of container +================================================================================ +foo.baz: { + foo -> biz: Label +} + +-------------------------------------------------------------------------------- + +(source_file + (container + (container_key) (dot) + (container + (container_key) + (block + (connection + (shape_key) + (arrow) + (shape_key) + (label) + ) + ) + ) + ) +) diff --git a/test/corpus/container.txt b/test/corpus/container.txt index d0b5691..704d2b5 100644 --- a/test/corpus/container.txt +++ b/test/corpus/container.txt @@ -2,22 +2,36 @@ Declare a shape inside a container ================================================================================ foo.baz +foo.bar.biz -------------------------------------------------------------------------------- (source_file - (container_key) (dot) (shape_key) + (container + (container_key) (dot) + (shape (shape_key)) + ) + (container + (container_key) (dot) + (container + (container_key) (dot) + (shape (shape_key)) + ) + ) ) ================================================================================ -Use quoted string as a shape key +Use quoted string as keys ================================================================================ 'foo'.'baz' -------------------------------------------------------------------------------- (source_file - (container_key (string)) (dot) (shape_key (string)) + (container + (container_key (string)) (dot) + (shape (shape_key (string))) + ) ) ================================================================================ @@ -35,20 +49,26 @@ foo: { -------------------------------------------------------------------------------- (source_file - (shape_key) - (block - (shape_key) + (container + (container_key) (block - (shape_key) - (block - (shape_key) + (container + (container_key) + (block + (container + (container_key) + (block + (shape (shape_key)) + ) + ) + ) ) ) ) ) ================================================================================ -Declare labaled container inside a labeled container using block +Declare labeled container inside a labeled container using block ================================================================================ foo: Foo { @@ -62,13 +82,22 @@ foo: Foo { -------------------------------------------------------------------------------- (source_file - (shape_key) (label) - (block - (shape_key) (label) + (container + (container_key) + (label) (block - (shape_key) (label) - (block - (shape_key) (label) + (container + (container_key) + (label) + (block + (container + (container_key) + (label) + (block + (shape (shape_key) (label)) + ) + ) + ) ) ) ) @@ -87,11 +116,111 @@ foo: { -------------------------------------------------------------------------------- (source_file - (shape_key) - (block - (shape_key) - (shape_key) - (shape_key) + (container + (container_key) + (block + (shape (shape_key)) + (shape (shape_key)) + (shape (shape_key)) + ) ) ) + +================================================================================ +Declare a container with complex keys +================================================================================ + +Foo biz bar: { + bar biz baz: { + -biz-baz-Baz-: { + Helo world + } + } +} + +-------------------------------------------------------------------------------- + +(source_file + (container + (container_key) + (block + (container + (container_key) + (block + (container + (container_key) + (block + (shape (shape_key)) + ) + ) + ) + ) + ) + ) +) + +================================================================================ +Declare a container with complex keys and labels +================================================================================ + +Foo biz bar: Biz biz Bar { + bar biz baz: baz baz biz { + -biz-baz-Baz-: Biz buz Baz { + Helo world + } + } +} + +-------------------------------------------------------------------------------- + +(source_file + (container + (container_key) + (label) + (block + (container + (container_key) + (label) + (block + (container + (container_key) + (label) + (block + (shape (shape_key)) + ) + ) + ) + ) + ) + ) +) + +================================================================================ +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/corpus/shape.txt b/test/corpus/shape.txt index c73d2f7..cf52896 100644 --- a/test/corpus/shape.txt +++ b/test/corpus/shape.txt @@ -7,8 +7,8 @@ bar -------------------------------------------------------------------------------- (source_file - (shape_key) - (shape_key) + (shape (shape_key)) + (shape (shape_key)) ) ================================================================================ @@ -20,8 +20,8 @@ Complex shape key -------------------------------------------------------------------------------- (source_file - (shape_key) - (shape_key) + (shape (shape_key)) + (shape (shape_key)) ) ================================================================================ @@ -32,7 +32,7 @@ Use quoted string as a shape key -------------------------------------------------------------------------------- (source_file - (shape_key (string)) + (shape (shape_key (string))) ) ================================================================================ @@ -43,9 +43,9 @@ a;b;c -------------------------------------------------------------------------------- (source_file - (shape_key) - (shape_key) - (shape_key) + (shape (shape_key)) + (shape (shape_key)) + (shape (shape_key)) ) ================================================================================ @@ -57,9 +57,9 @@ a: Foo Bar; b: Biz Baz -------------------------------------------------------------------------------- (source_file - (shape_key) (label) - (shape_key) (label) - (shape_key) (label) + (shape (shape_key) (label)) + (shape (shape_key) (label)) + (shape (shape_key) (label)) ) ================================================================================ @@ -73,62 +73,19 @@ bar : Foo Bar; baz -------------------------------------------------------------------------------- (source_file - (shape_key) - (shape_key) (label) - (shape_key) + (shape (shape_key)) + (shape (shape_key) (label)) + (shape (shape_key)) ) ================================================================================ -Shape block +Use quoted string as shape key and label ================================================================================ - -foo: { - bar: { - baz: { - biz - } - } -} +'foo': "Label" -------------------------------------------------------------------------------- (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) - ) - ) - ) + (shape (shape_key (string)) (label (string))) ) diff --git a/test/highlight/keyword.d2 b/test/highlight/attribute.d2 similarity index 95% rename from test/highlight/keyword.d2 rename to test/highlight/attribute.d2 index 72044c1..58f878a 100644 --- a/test/highlight/keyword.d2 +++ b/test/highlight/attribute.d2 @@ -6,7 +6,7 @@ direction: right; shape: oval # <- error -# ^ error +# ^ string baaaz.style: { # <- variable 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 c9f5b7d..609a011 100755 Binary files a/tree-sitter-d2.wasm and b/tree-sitter-d2.wasm differ