add invalid attributes

This commit is contained in:
Dmitriy Pleshevskiy 2022-12-07 18:52:19 +03:00
parent fac4bde489
commit 867ed6662b
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
10 changed files with 307 additions and 66 deletions

View file

@ -6,6 +6,12 @@ test: build
highlight: build
tree-sitter highlight -t test/highlight/*.d2
playground: build-wasm
tree-sitter playground --quiet
build-wasm: build
tree-sitter build-wasm
build:
tree-sitter generate
@ -18,8 +24,3 @@ init:
cat <<< $$(jq ".\"parser-directories\" |= . + [\"$$(dirname $(PWD))\"]" $(TS_CONF)) > $(TS_CONF); \
fi
playground: build-wasm
tree-sitter playground
build-wasm:
tree-sitter build-wasm

View file

@ -1,4 +1,4 @@
const spaces = repeat(" ");
const spaces = repeat(choice(" ", "\t"));
module.exports = grammar({
name: "d2",
@ -42,8 +42,7 @@ module.exports = grammar({
choice(
seq($.dot, $._shape_attribute),
seq(
$._colon,
optional($.label),
optional(seq($._colon, optional($.label))),
optional(alias($._shape_block, $.block))
)
)
@ -56,7 +55,15 @@ module.exports = grammar({
attr_value: ($) => seq(spaces, choice($.string, $._unquoted_string)),
_root_attribute: ($) =>
seq(alias($._root_attr_key, $.attr_key), $._colon, $.attr_value, $._end),
choice(
seq(
alias($._root_attr_key, $.attr_key),
$._colon,
$.attr_value,
$._end
),
alias(seq($._shape_attribute, $._end), $.invalid)
),
_root_attr_key: ($) => "direction",
@ -64,8 +71,9 @@ module.exports = grammar({
seq(
spaces,
"{",
spaces,
repeat(choice($._emptyline, seq($._shape_block_definition, $._end))),
repeat(
choice($._emptyline, seq(spaces, $._shape_block_definition, $._end))
),
optional(seq($._shape_block_definition, optional($._end))),
spaces,
"}"

View file

@ -1,3 +1,8 @@
; Special (treesitter don't overwrite)
;-------------------------------------------------------------------------------
(ERROR) @error
(invalid (_) @error)
;-------------------------------------------------------------------------------
(identifier) @variable
@ -31,6 +36,8 @@
"}"
] @punctuation.bracket
; Special (nvim-treesitter overwrite previous rules)
;-------------------------------------------------------------------------------
(ERROR) @error
(invalid (_) @error)

View file

@ -140,6 +140,12 @@
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
@ -158,6 +164,13 @@
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
@ -210,8 +223,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -230,6 +252,9 @@
]
},
"_root_attribute": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
@ -255,6 +280,26 @@
}
]
},
{
"type": "ALIAS",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_shape_attribute"
},
{
"type": "SYMBOL",
"name": "_end"
}
]
},
"named": true,
"value": "invalid"
}
]
},
"_root_attr_key": {
"type": "STRING",
"value": "direction"
@ -265,21 +310,23 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "REPEAT",
"content": {
@ -292,6 +339,22 @@
{
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
"type": "SYMBOL",
"name": "_shape_block_definition"
@ -337,8 +400,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -451,8 +523,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -462,8 +543,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -523,8 +613,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -539,8 +638,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -689,8 +797,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -736,8 +853,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -775,8 +901,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -791,8 +926,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -995,8 +1139,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{
@ -1024,8 +1177,17 @@
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{

View file

@ -99,6 +99,33 @@
"named": true,
"fields": {}
},
{
"type": "invalid",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "attr_key",
"named": true
},
{
"type": "attr_value",
"named": true
},
{
"type": "block",
"named": true
},
{
"type": "dot",
"named": true
}
]
}
},
{
"type": "label",
"named": true,
@ -169,6 +196,10 @@
"type": "connection",
"named": true
},
{
"type": "invalid",
"named": true
},
{
"type": "shape",
"named": true
@ -185,6 +216,10 @@
"type": "\u0000",
"named": false
},
{
"type": "\t",
"named": false
},
{
"type": "\n",
"named": false

Binary file not shown.

View file

@ -9,6 +9,29 @@ direction: value
(attr_key) (attr_value)
)
================================================================================
Block shape attribute
================================================================================
foo: {
shape: oval
foo.bar.baz: {
shape: oval
}
}
--------------------------------------------------------------------------------
(source_file
(shape (identifier) (block
(attr_key) (attr_value)
(shape (identifier) (dot) (identifier) (dot) (identifier) (block
(attr_key) (attr_value)
))
))
)
================================================================================
Inline shape attribute
================================================================================

View file

@ -130,3 +130,4 @@ foo: Foo {
))
))
)

View file

@ -4,6 +4,10 @@ direction: right;
# ^ string
# ^ punctuation.delimiter
shape: oval;
# <- error
# ^ error
baaaz.style: {
# <- variable
# ^ keyword

Binary file not shown.