diff --git a/grammar.js b/grammar.js index 0f48377..8bff710 100644 --- a/grammar.js +++ b/grammar.js @@ -1,6 +1,7 @@ const PREC = { COMMENT: -2, EOL: -1, + TEXT_BLOCK_CONTENT: -1, UNQUOTED_STRING: 0, CONTAINER: 2, CONNECTION: 2, @@ -107,7 +108,7 @@ module.exports = grammar({ optional( choice( seq($.dot, alias($._style_attribute, $.attribute)), - seq(optional(seq($._colon, optional($.label)))) + seq(optional(seq($._colon, choice($.label, $.text_block)))) ) ) ) @@ -123,6 +124,21 @@ module.exports = grammar({ ) ), + text_block: ($) => + choice( + seq("|", $._text_block_definition, "|"), + // References: https://github.com/terrastruct/d2-vim + seq("|`", $._text_block_definition, "`|") + ), + + _text_block_definition: ($) => + seq(optional($.language), $._eol, optional($.text_block_content)), + + text_block_content: ($) => + repeat1(token(prec(PREC.TEXT_BLOCK_CONTENT, /.*?[^`|]/))), + + language: ($) => /\w+/, + // attributes _root_attribute: ($) => @@ -260,7 +276,7 @@ module.exports = grammar({ dot: ($) => token("."), _unquoted_string: ($) => - token(prec(PREC.UNQUOTED_STRING, /[^'"`\n\s;{}]([^\n;{}]*[^\n\s;{}])?/)), + token(prec(PREC.UNQUOTED_STRING, /[^'"`|\n\s;{}]([^\n;{}]*[^\n\s;{}])?/)), string: ($) => choice( diff --git a/src/grammar.json b/src/grammar.json index ad3b555..767bdf0 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -442,7 +442,8 @@ "name": "label" }, { - "type": "BLANK" + "type": "SYMBOL", + "name": "text_block" } ] } @@ -506,6 +507,96 @@ } } }, + "text_block": { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "|" + }, + { + "type": "SYMBOL", + "name": "_text_block_definition" + }, + { + "type": "STRING", + "value": "|" + } + ] + }, + { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "|`" + }, + { + "type": "SYMBOL", + "name": "_text_block_definition" + }, + { + "type": "STRING", + "value": "`|" + } + ] + } + ] + }, + "_text_block_definition": { + "type": "SEQ", + "members": [ + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "language" + }, + { + "type": "BLANK" + } + ] + }, + { + "type": "SYMBOL", + "name": "_eol" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "text_block_content" + }, + { + "type": "BLANK" + } + ] + } + ] + }, + "text_block_content": { + "type": "REPEAT1", + "content": { + "type": "TOKEN", + "content": { + "type": "PREC", + "value": -1, + "content": { + "type": "PATTERN", + "value": ".*?[^`|]" + } + } + } + }, + "language": { + "type": "PATTERN", + "value": "\\w+" + }, "_root_attribute": { "type": "SEQ", "members": [ @@ -1103,7 +1194,7 @@ "value": 0, "content": { "type": "PATTERN", - "value": "[^'\"`\\n\\s;{}]([^\\n;{}]*[^\\n\\s;{}])?" + "value": "[^'\"`|\\n\\s;{}]([^\\n;{}]*[^\\n\\s;{}])?" } } }, diff --git a/src/node-types.json b/src/node-types.json index 419c1eb..f39fe3c 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -219,6 +219,10 @@ { "type": "shape_key", "named": true + }, + { + "type": "text_block", + "named": true } ] } @@ -270,6 +274,30 @@ "named": true, "fields": {} }, + { + "type": "text_block", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "language", + "named": true + }, + { + "type": "text_block_content", + "named": true + } + ] + } + }, + { + "type": "text_block_content", + "named": true, + "fields": {} + }, { "type": "\u0000", "named": false @@ -298,6 +326,10 @@ "type": "`", "named": false }, + { + "type": "`|", + "named": false + }, { "type": "animated", "named": false @@ -342,6 +374,10 @@ "type": "label", "named": false }, + { + "type": "language", + "named": true + }, { "type": "line_comment", "named": true @@ -394,6 +430,14 @@ "type": "{", "named": false }, + { + "type": "|", + "named": false + }, + { + "type": "|`", + "named": false + }, { "type": "}", "named": false diff --git a/src/parser.c b/src/parser.c index e34cffa..bc09a54 100644 Binary files a/src/parser.c and b/src/parser.c differ diff --git a/test/corpus/shape.txt b/test/corpus/shape.txt index d0846f2..2ff0098 100644 --- a/test/corpus/shape.txt +++ b/test/corpus/shape.txt @@ -109,3 +109,74 @@ Use quoted string as shape key and label (shape (shape_key (string)) (label (string))) ) +================================================================================ +Basic text block +================================================================================ +foo: | + - hello +| + +-------------------------------------------------------------------------------- + +(source_file + (shape + (shape_key) + (text_block + (text_block_content) + ) + ) +) + +================================================================================ +Text block with specific language +================================================================================ +foo: |go + + awsSession := From(c.Request.Context()) + + client := s3.New(awsSession) + + + ctx, cancelFn := context.WithTimeout(c.Request.Context(), AWS_TIMEOUT) + + defer cancelFn() +| + +-------------------------------------------------------------------------------- + +(source_file + (shape + (shape_key) + (text_block + (language) + (text_block_content) + ) + ) +) + +================================================================================ +Text block with quotes (d2-vim style) +================================================================================ +foo: |`go + + awsSession := From(c.Request.Context()) + + client := s3.New(awsSession) + + + ctx, cancelFn := context.WithTimeout(c.Request.Context(), AWS_TIMEOUT) + + defer cancelFn() +`| + +-------------------------------------------------------------------------------- + +(source_file + (shape + (shape_key) + (text_block + (language) + (text_block_content) + ) + ) +) diff --git a/tree-sitter-d2.wasm b/tree-sitter-d2.wasm index b1090db..024038d 100755 Binary files a/tree-sitter-d2.wasm and b/tree-sitter-d2.wasm differ