add text block

Closes #3
This commit is contained in:
Dmitriy Pleshevskiy 2022-12-09 16:08:41 +03:00
parent 612b7fb871
commit d230155d48
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
6 changed files with 226 additions and 4 deletions

View file

@ -1,6 +1,7 @@
const PREC = { const PREC = {
COMMENT: -2, COMMENT: -2,
EOL: -1, EOL: -1,
TEXT_BLOCK_CONTENT: -1,
UNQUOTED_STRING: 0, UNQUOTED_STRING: 0,
CONTAINER: 2, CONTAINER: 2,
CONNECTION: 2, CONNECTION: 2,
@ -107,7 +108,7 @@ module.exports = grammar({
optional( optional(
choice( choice(
seq($.dot, alias($._style_attribute, $.attribute)), 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 // attributes
_root_attribute: ($) => _root_attribute: ($) =>
@ -260,7 +276,7 @@ module.exports = grammar({
dot: ($) => token("."), dot: ($) => token("."),
_unquoted_string: ($) => _unquoted_string: ($) =>
token(prec(PREC.UNQUOTED_STRING, /[^'"`\n\s;{}]([^\n;{}]*[^\n\s;{}])?/)), token(prec(PREC.UNQUOTED_STRING, /[^'"`|\n\s;{}]([^\n;{}]*[^\n\s;{}])?/)),
string: ($) => string: ($) =>
choice( choice(

View file

@ -442,7 +442,8 @@
"name": "label" "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": { "_root_attribute": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -1103,7 +1194,7 @@
"value": 0, "value": 0,
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^'\"`\\n\\s;{}]([^\\n;{}]*[^\\n\\s;{}])?" "value": "[^'\"`|\\n\\s;{}]([^\\n;{}]*[^\\n\\s;{}])?"
} }
} }
}, },

View file

@ -219,6 +219,10 @@
{ {
"type": "shape_key", "type": "shape_key",
"named": true "named": true
},
{
"type": "text_block",
"named": true
} }
] ]
} }
@ -270,6 +274,30 @@
"named": true, "named": true,
"fields": {} "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", "type": "\u0000",
"named": false "named": false
@ -298,6 +326,10 @@
"type": "`", "type": "`",
"named": false "named": false
}, },
{
"type": "`|",
"named": false
},
{ {
"type": "animated", "type": "animated",
"named": false "named": false
@ -342,6 +374,10 @@
"type": "label", "type": "label",
"named": false "named": false
}, },
{
"type": "language",
"named": true
},
{ {
"type": "line_comment", "type": "line_comment",
"named": true "named": true
@ -394,6 +430,14 @@
"type": "{", "type": "{",
"named": false "named": false
}, },
{
"type": "|",
"named": false
},
{
"type": "|`",
"named": false
},
{ {
"type": "}", "type": "}",
"named": false "named": false

Binary file not shown.

View file

@ -109,3 +109,74 @@ Use quoted string as shape key and label
(shape (shape_key (string)) (label (string))) (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)
)
)
)

Binary file not shown.