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 highlight: build
tree-sitter highlight -t test/highlight/*.d2 tree-sitter highlight -t test/highlight/*.d2
playground: build-wasm
tree-sitter playground --quiet
build-wasm: build
tree-sitter build-wasm
build: build:
tree-sitter generate tree-sitter generate
@ -18,8 +24,3 @@ init:
cat <<< $$(jq ".\"parser-directories\" |= . + [\"$$(dirname $(PWD))\"]" $(TS_CONF)) > $(TS_CONF); \ cat <<< $$(jq ".\"parser-directories\" |= . + [\"$$(dirname $(PWD))\"]" $(TS_CONF)) > $(TS_CONF); \
fi 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({ module.exports = grammar({
name: "d2", name: "d2",
@ -42,8 +42,7 @@ module.exports = grammar({
choice( choice(
seq($.dot, $._shape_attribute), seq($.dot, $._shape_attribute),
seq( seq(
$._colon, optional(seq($._colon, optional($.label))),
optional($.label),
optional(alias($._shape_block, $.block)) optional(alias($._shape_block, $.block))
) )
) )
@ -56,7 +55,15 @@ module.exports = grammar({
attr_value: ($) => seq(spaces, choice($.string, $._unquoted_string)), attr_value: ($) => seq(spaces, choice($.string, $._unquoted_string)),
_root_attribute: ($) => _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", _root_attr_key: ($) => "direction",
@ -64,8 +71,9 @@ module.exports = grammar({
seq( seq(
spaces, spaces,
"{", "{",
spaces, repeat(
repeat(choice($._emptyline, seq($._shape_block_definition, $._end))), choice($._emptyline, seq(spaces, $._shape_block_definition, $._end))
),
optional(seq($._shape_block_definition, optional($._end))), optional(seq($._shape_block_definition, optional($._end))),
spaces, spaces,
"}" "}"

View file

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

View file

@ -143,16 +143,29 @@
{ {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "_colon"
},
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SEQ",
"name": "label" "members": [
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "label"
},
{
"type": "BLANK"
}
]
}
]
}, },
{ {
"type": "BLANK" "type": "BLANK"
@ -210,8 +223,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -230,28 +252,51 @@
] ]
}, },
"_root_attribute": { "_root_attribute": {
"type": "SEQ", "type": "CHOICE",
"members": [ "members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_root_attr_key"
},
"named": true,
"value": "attr_key"
},
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "SYMBOL",
"name": "attr_value"
},
{
"type": "SYMBOL",
"name": "_end"
}
]
},
{ {
"type": "ALIAS", "type": "ALIAS",
"content": { "content": {
"type": "SYMBOL", "type": "SEQ",
"name": "_root_attr_key" "members": [
{
"type": "SYMBOL",
"name": "_shape_attribute"
},
{
"type": "SYMBOL",
"name": "_end"
}
]
}, },
"named": true, "named": true,
"value": "attr_key" "value": "invalid"
},
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "SYMBOL",
"name": "attr_value"
},
{
"type": "SYMBOL",
"name": "_end"
} }
] ]
}, },
@ -265,21 +310,23 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "{" "value": "{"
}, },
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
@ -292,6 +339,22 @@
{ {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_shape_block_definition" "name": "_shape_block_definition"
@ -337,8 +400,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -451,8 +523,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -462,8 +543,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -523,8 +613,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -539,8 +638,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -689,8 +797,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -736,8 +853,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -775,8 +901,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -791,8 +926,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -995,8 +1139,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {
@ -1024,8 +1177,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": " " "members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
} }
}, },
{ {

View file

@ -99,6 +99,33 @@
"named": true, "named": true,
"fields": {} "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", "type": "label",
"named": true, "named": true,
@ -169,6 +196,10 @@
"type": "connection", "type": "connection",
"named": true "named": true
}, },
{
"type": "invalid",
"named": true
},
{ {
"type": "shape", "type": "shape",
"named": true "named": true
@ -185,6 +216,10 @@
"type": "\u0000", "type": "\u0000",
"named": false "named": false
}, },
{
"type": "\t",
"named": false
},
{ {
"type": "\n", "type": "\n",
"named": false "named": false

Binary file not shown.

View file

@ -9,6 +9,29 @@ direction: value
(attr_key) (attr_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 Inline shape attribute
================================================================================ ================================================================================

View file

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

View file

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

Binary file not shown.