improve strings and scanner for text block

This commit is contained in:
Dmitriy Pleshevskiy 2022-12-10 05:23:27 +03:00
parent 1f0e76c937
commit 24cdec0cb3
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
12 changed files with 194 additions and 41 deletions

View File

@ -137,7 +137,7 @@ module.exports = grammar({
_text_block_definition: ($) => _text_block_definition: ($) =>
seq( seq(
optional($.language), optional($.language),
$._eol, /\s/,
optional(alias($._text_block_raw, $.raw_text)) optional(alias($._text_block_raw, $.raw_text))
), ),
@ -285,8 +285,44 @@ module.exports = grammar({
string: ($) => string: ($) =>
choice( choice(
seq("'", repeat(token.immediate(/[^'\n]+/)), "'"), seq(
seq('"', repeat(token.immediate(/[^"\n]+/)), '"') "'",
repeat(
choice(
alias($._unescaped_single_string_fragment, $.string_fragment),
$.escape_sequence
)
),
"'"
),
seq(
'"',
repeat(
choice(
alias($._unescaped_double_string_fragment, $.string_fragment),
$.escape_sequence
)
),
'"'
)
),
_unescaped_single_string_fragment: ($) => token.immediate(/[^'\\\n]+/),
_unescaped_double_string_fragment: ($) => token.immediate(/[^"\\\n]+/),
escape_sequence: ($) =>
token.immediate(
seq(
"\\",
choice(
/[^xu0-7]/,
/[0-7]{1,3}/,
/x[0-9a-fA-F]{2}/,
/u[0-9a-fA-F]{4}/,
/u{[0-9a-fA-F]+}/
)
)
), ),
boolean: ($) => choice("true", "false"), boolean: ($) => choice("true", "false"),

View File

@ -8,10 +8,11 @@
; Literals ; Literals
;------------------------------------------------------------------------------- ;-------------------------------------------------------------------------------
(language) @type.qualifier (string) @string
(container_key (string) @string) (container_key (string (string_fragment) @string))
(shape_key (string) @string) (shape_key (string (string_fragment) @string))
(label) @string.special (escape_sequence) @string.escape
(label) @text.title
(attr_value) @string (attr_value) @string
(integer) @number (integer) @number
(float) @float (float) @float
@ -20,6 +21,7 @@
; Comments ; Comments
;------------------------------------------------------------------------------- ;-------------------------------------------------------------------------------
(language) @comment
(line_comment) @comment.line (line_comment) @comment.line
; Punctiation ; Punctiation

13
scripts/pre-push Normal file
View File

@ -0,0 +1,13 @@
#! /usr/bin/env bash
# Can be used as a pre-push hook
# Just symlink this file to .git/hooks/pre-push
set -xe
if [ ! -z $(git diff --cached --name-only | grep -e "^grammar.js$") ]
then
make build
make build-wasm
fi

View File

@ -592,8 +592,8 @@
] ]
}, },
{ {
"type": "SYMBOL", "type": "PATTERN",
"name": "_eol" "value": "\\s"
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -1244,11 +1244,22 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "IMMEDIATE_TOKEN", "type": "CHOICE",
"content": { "members": [
"type": "PATTERN", {
"value": "[^'\\n]+" "type": "ALIAS",
} "content": {
"type": "SYMBOL",
"name": "_unescaped_single_string_fragment"
},
"named": true,
"value": "string_fragment"
},
{
"type": "SYMBOL",
"name": "escape_sequence"
}
]
} }
}, },
{ {
@ -1267,11 +1278,22 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "IMMEDIATE_TOKEN", "type": "CHOICE",
"content": { "members": [
"type": "PATTERN", {
"value": "[^\"\\n]+" "type": "ALIAS",
} "content": {
"type": "SYMBOL",
"name": "_unescaped_double_string_fragment"
},
"named": true,
"value": "string_fragment"
},
{
"type": "SYMBOL",
"name": "escape_sequence"
}
]
} }
}, },
{ {
@ -1282,6 +1304,57 @@
} }
] ]
}, },
"_unescaped_single_string_fragment": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[^'\\\\\\n]+"
}
},
"_unescaped_double_string_fragment": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[^\"\\\\\\n]+"
}
},
"escape_sequence": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\\"
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^xu0-7]"
},
{
"type": "PATTERN",
"value": "[0-7]{1,3}"
},
{
"type": "PATTERN",
"value": "x[0-9a-fA-F]{2}"
},
{
"type": "PATTERN",
"value": "u[0-9a-fA-F]{4}"
},
{
"type": "PATTERN",
"value": "u{[0-9a-fA-F]+}"
}
]
}
]
}
},
"boolean": { "boolean": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [

View File

@ -289,7 +289,21 @@
{ {
"type": "string", "type": "string",
"named": true, "named": true,
"fields": {} "fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "escape_sequence",
"named": true
},
{
"type": "string_fragment",
"named": true
}
]
}
}, },
{ {
"type": "text_block", "type": "text_block",
@ -366,6 +380,10 @@
"type": "dot", "type": "dot",
"named": true "named": true
}, },
{
"type": "escape_sequence",
"named": true
},
{ {
"type": "false", "type": "false",
"named": false "named": false
@ -434,6 +452,10 @@
"type": "source-arrowhead", "type": "source-arrowhead",
"named": false "named": false
}, },
{
"type": "string_fragment",
"named": true
},
{ {
"type": "stroke", "type": "stroke",
"named": false "named": false

Binary file not shown.

View File

@ -31,20 +31,18 @@ static bool scan_raw_text(TSLexer *lexer) {
return has_content; return has_content;
} }
if (lexer->lookahead == '\n') { while (iswspace(lexer->lookahead)) {
advance(lexer); advance(lexer);
lexer->mark_end(lexer); lexer->mark_end(lexer);
}
while (iswspace(lexer->lookahead)) { if (lexer->lookahead == '`') {
advance(lexer); advance(lexer);
lexer->mark_end(lexer); }
}
if (lexer->lookahead == '`') { if (lexer->lookahead == '|') {
advance(lexer); advance(lexer);
} if (lexer->lookahead == '\n') {
if (lexer->lookahead == '|') {
return has_content; return has_content;
} }
} }

View File

@ -26,7 +26,7 @@ near: abc
(attribute (attr_key) (attr_value)) (attribute (attr_key) (attr_value))
(attribute (attr_key (reserved)) (attr_value)) (attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value (string))) (attribute (attr_key (reserved)) (attr_value (string (string_fragment))))
(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 (float))) (attribute (attr_key (reserved)) (attr_value (float)))
@ -134,7 +134,7 @@ foo: {
(container_key) (container_key)
(block (block
(attribute (attr_key) (attr_value)) (attribute (attr_key) (attr_value))
(attribute (attr_key) (attr_value (string))) (attribute (attr_key) (attr_value (string (string_fragment))))
(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 (integer))) (attribute (attr_key) (attr_value (integer)))
@ -381,7 +381,7 @@ foo -> bar: {
(attr_key) (attr_key)
(block (block
(attribute (attr_key) (attr_value)) (attribute (attr_key) (attr_value))
(attribute (attr_key) (attr_value (string))) (attribute (attr_key) (attr_value (string (string_fragment))))
(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 (integer))) (attribute (attr_key) (attr_value (integer)))

View File

@ -29,8 +29,8 @@ Use quoted string as keys
(source_file (source_file
(container (container
(container_key (string)) (dot) (container_key (string (string_fragment))) (dot)
(shape (shape_key (string))) (shape (shape_key (string (string_fragment))))
) )
) )

View File

@ -54,7 +54,7 @@ Use quoted string as a shape key
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape (shape_key (string))) (shape (shape_key (string (string_fragment))))
) )
================================================================================ ================================================================================
@ -108,7 +108,9 @@ Use quoted string as shape key and label
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape (shape_key (string)) (label (string))) (shape
(shape_key (string (string_fragment)))
(label (string (string_fragment))))
) )
================================================================================ ================================================================================

View File

@ -2,21 +2,28 @@ foo.'baz'.biz
# <- constant # <- constant
# ^ punctuation.delimiter # ^ punctuation.delimiter
# ^ string # ^ string
# ^ string
# ^ punctuation.delimiter # ^ punctuation.delimiter
# ^ variable # ^ variable
'biz': 'Baz' 'b\nz'
# ^ string.escape
# ^ string.escape
'biz': 'Baz\nBiz'
# <- string # <- string
# ^ string.special # ^ string
# ^ string
# ^ string.escape
foo: Foo Bar foo: Foo Bar
# <- variable # <- variable
# ^ punctuation.delimiter # ^ punctuation.delimiter
# ^ string.special # ^ text.title
foo: Foo Bar { foo: Foo Bar {
# <- constant # <- constant
# ^ string.special # ^ text.title
# ^ punctuation.bracket # ^ punctuation.bracket
bar.baz bar.baz

Binary file not shown.