add block comment

Closes #10
This commit is contained in:
Dmitriy Pleshevskiy 2022-12-13 11:26:52 +03:00
parent a1a2091ebf
commit c8de4f83f0
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
6 changed files with 80 additions and 0 deletions

View file

@ -19,11 +19,13 @@ module.exports = grammar({
$._text_block_start, $._text_block_start,
$._text_block_end, $._text_block_end,
$._text_block_raw_text, $._text_block_raw_text,
$.block_comment,
], ],
extras: ($) => [ extras: ($) => [
/[ \f\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/, /[ \f\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/,
$.line_comment, $.line_comment,
$.block_comment,
], ],
conflicts: ($) => [[$._connection_path, $.container]], conflicts: ($) => [[$._connection_path, $.container]],

View file

@ -1393,6 +1393,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "line_comment" "name": "line_comment"
},
{
"type": "SYMBOL",
"name": "block_comment"
} }
], ],
"conflicts": [ "conflicts": [
@ -1414,6 +1418,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_text_block_raw_text" "name": "_text_block_raw_text"
},
{
"type": "SYMBOL",
"name": "block_comment"
} }
], ],
"inline": [], "inline": [],

View file

@ -360,6 +360,10 @@
"type": "arrow", "type": "arrow",
"named": true "named": true
}, },
{
"type": "block_comment",
"named": true
},
{ {
"type": "border-radius", "type": "border-radius",
"named": false "named": false

Binary file not shown.

View file

@ -10,6 +10,7 @@ namespace {
TEXT_BLOCK_START, TEXT_BLOCK_START,
TEXT_BLOCK_END, TEXT_BLOCK_END,
TEXT_BLOCK_RAW_TEXT, TEXT_BLOCK_RAW_TEXT,
BLOCK_COMMENT,
}; };
struct Scanner { struct Scanner {
@ -68,6 +69,17 @@ namespace {
return true; 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) { bool scan(TSLexer *lexer, const bool *valid_symbols) {
if (valid_symbols[TEXT_BLOCK_START] && escape_char_stack.empty()) { if (valid_symbols[TEXT_BLOCK_START] && escape_char_stack.empty()) {
lexer->result_symbol = TEXT_BLOCK_START; lexer->result_symbol = TEXT_BLOCK_START;
@ -120,6 +132,25 @@ namespace {
lexer->mark_end(lexer); 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; return true;
} }

View file

@ -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))
)