diff --git a/grammar.js b/grammar.js index 3a5f5bd..202bb67 100644 --- a/grammar.js +++ b/grammar.js @@ -19,11 +19,13 @@ module.exports = grammar({ $._text_block_start, $._text_block_end, $._text_block_raw_text, + $.block_comment, ], extras: ($) => [ /[ \f\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/, $.line_comment, + $.block_comment, ], conflicts: ($) => [[$._connection_path, $.container]], diff --git a/src/grammar.json b/src/grammar.json index df716d9..597d872 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -1393,6 +1393,10 @@ { "type": "SYMBOL", "name": "line_comment" + }, + { + "type": "SYMBOL", + "name": "block_comment" } ], "conflicts": [ @@ -1414,6 +1418,10 @@ { "type": "SYMBOL", "name": "_text_block_raw_text" + }, + { + "type": "SYMBOL", + "name": "block_comment" } ], "inline": [], diff --git a/src/node-types.json b/src/node-types.json index ee14e4c..b0e05bb 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -360,6 +360,10 @@ "type": "arrow", "named": true }, + { + "type": "block_comment", + "named": true + }, { "type": "border-radius", "named": false diff --git a/src/parser.c b/src/parser.c index a33d6fd..552efcd 100644 Binary files a/src/parser.c and b/src/parser.c differ diff --git a/src/scanner.cc b/src/scanner.cc index c9803ce..f94038d 100644 --- a/src/scanner.cc +++ b/src/scanner.cc @@ -10,6 +10,7 @@ namespace { TEXT_BLOCK_START, TEXT_BLOCK_END, TEXT_BLOCK_RAW_TEXT, + BLOCK_COMMENT, }; struct Scanner { @@ -68,6 +69,17 @@ namespace { return true; } + bool is_triple_double_quote(TSLexer *lexer) { + for (int i = 0; i < 3; ++i) { + if (lexer->lookahead != '"') { + return false; + } + advance(lexer); + } + + return true; + } + bool scan(TSLexer *lexer, const bool *valid_symbols) { if (valid_symbols[TEXT_BLOCK_START] && escape_char_stack.empty()) { lexer->result_symbol = TEXT_BLOCK_START; @@ -120,6 +132,25 @@ namespace { lexer->mark_end(lexer); } + return true; + } else if (valid_symbols[BLOCK_COMMENT]) { + lexer->result_symbol = BLOCK_COMMENT; + lexer->mark_end(lexer); + + // Check start of block comment + if (!is_triple_double_quote(lexer)) { + return false; + } + + // Search end of block comment + while (!is_triple_double_quote(lexer)) { + // d2 expects closed tag for block comment + if (lexer->lookahead == 0) return false; + advance(lexer); + } + + lexer->mark_end(lexer); + return true; } diff --git a/test/corpus/shape.txt b/test/corpus/shape.txt index 0011321..a0e25b5 100644 --- a/test/corpus/shape.txt +++ b/test/corpus/shape.txt @@ -228,3 +228,38 @@ bar: |##md ## hello world ##| ) ) ) + +================================================================================ +Shape with oneline comment +================================================================================ +foo # hello +# world +bar + +-------------------------------------------------------------------------------- + +(source_file + (shape (shape_key)) + (line_comment) + (line_comment) + (shape (shape_key)) +) + +================================================================================ +Shape with block comment +================================================================================ +foo + +""" +hello world +""" + +bar + +-------------------------------------------------------------------------------- + +(source_file + (shape (shape_key)) + (block_comment) + (shape (shape_key)) +)