add style attr block

This commit is contained in:
Dmitriy Pleshevskiy 2022-12-06 11:57:59 +03:00
parent 7a76b25c11
commit 41e2ce2e57
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
6 changed files with 301 additions and 65 deletions

View File

@ -1,5 +1,2 @@
foo -- bar
biz -> baz
biz <-> baz
biz <- baz
foo.style: { opacity: 5; fill: red }

View File

@ -7,7 +7,12 @@ module.exports = grammar({
word: ($) => $._word,
conflicts: ($) => [[$.identifier], [$.arrow]],
conflicts: ($) => [
[$.identifier],
[$.arrow],
[$._style_attr_block],
[$._inner_style_attribute],
],
rules: {
source_file: ($) => repeat($._definition),
@ -37,28 +42,42 @@ module.exports = grammar({
label: ($) => choice($.string, $._unquoted_string),
attr_value: ($) => choice($.string, $._unquoted_string),
attr_value: ($) => seq(spaces, choice($.string, $._unquoted_string)),
_root_attribute: ($) =>
seq(alias($._root_attr_key, $.attr_key), $._colon, $.attr_value, $._end),
_root_attr_key: ($) => "direction",
_style_attribute: ($) =>
seq(
alias("style", $.attr_key),
$.dot,
alias($._style_attr_key, $.attr_key),
$._colon,
$.attr_value
),
_shape_attribute: ($) =>
choice(
seq(alias($._shape_attr_key, $.attr_key), $._colon, $.attr_value),
$._style_attribute
),
_style_attribute: ($) =>
seq(
alias("style", $.attr_key),
choice(
seq($.dot, $._inner_style_attribute),
seq($._colon, alias($._style_attr_block, $.block))
)
),
_style_attr_block: ($) =>
seq(
spaces,
"{",
spaces,
repeat(choice($._eof, seq($._inner_style_attribute, $._end))),
optional(seq($._inner_style_attribute, optional($._end))),
spaces,
"}"
),
_inner_style_attribute: ($) =>
seq(spaces, alias($._style_attr_key, $.attr_key), $._colon, $.attr_value),
_connection_attribute: ($) =>
seq(alias($._connection_attr_key, $.attr_key), $._colon, $.attr_value),
@ -106,7 +125,7 @@ module.exports = grammar({
optional($._dash)
),
_colon: ($) => seq(spaces, ":", spaces),
_colon: ($) => seq(spaces, ":"),
_arrow: ($) => seq(spaces, $.arrow),
@ -122,7 +141,7 @@ module.exports = grammar({
dot: ($) => token.immediate("."),
_unquoted_string: ($) => token.immediate(/[^'"`\n;{]+/),
_unquoted_string: ($) => token.immediate(/[^'"`\n;{}]+/),
string: ($) =>
choice(

View File

@ -180,15 +180,27 @@
]
},
"attr_value": {
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "string"
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "SYMBOL",
"name": "_unquoted_string"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "_unquoted_string"
}
]
}
]
},
@ -222,41 +234,6 @@
"type": "STRING",
"value": "direction"
},
"_style_attribute": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "style"
},
"named": true,
"value": "attr_key"
},
{
"type": "SYMBOL",
"name": "dot"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_style_attr_key"
},
"named": true,
"value": "attr_key"
},
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "SYMBOL",
"name": "attr_value"
}
]
},
"_shape_attribute": {
"type": "CHOICE",
"members": [
@ -288,6 +265,173 @@
}
]
},
"_style_attribute": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "style"
},
"named": true,
"value": "attr_key"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "dot"
},
{
"type": "SYMBOL",
"name": "_inner_style_attribute"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_style_attr_block"
},
"named": true,
"value": "block"
}
]
}
]
}
]
},
"_style_attr_block": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_eof"
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_inner_style_attribute"
},
{
"type": "SYMBOL",
"name": "_end"
}
]
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_inner_style_attribute"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_end"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "STRING",
"value": "}"
}
]
},
"_inner_style_attribute": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_style_attr_key"
},
"named": true,
"value": "attr_key"
},
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "SYMBOL",
"name": "attr_value"
}
]
},
"_connection_attribute": {
"type": "SEQ",
"members": [
@ -508,13 +652,6 @@
{
"type": "STRING",
"value": ":"
},
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
}
]
},
@ -625,7 +762,7 @@
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[^'\"`\\n;{]+"
"value": "[^'\"`\\n;{}]+"
}
},
"string": {
@ -752,6 +889,12 @@
],
[
"arrow"
],
[
"_style_attr_block"
],
[
"_inner_style_attribute"
]
],
"precedences": [],

View File

@ -24,6 +24,25 @@
]
}
},
{
"type": "block",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "attr_key",
"named": true
},
{
"type": "attr_value",
"named": true
}
]
}
},
{
"type": "connection",
"named": true,
@ -95,6 +114,10 @@
"type": "attr_value",
"named": true
},
{
"type": "block",
"named": true
},
{
"type": "dot",
"named": true
@ -273,5 +296,13 @@
{
"type": "width",
"named": false
},
{
"type": "{",
"named": false
},
{
"type": "}",
"named": false
}
]

Binary file not shown.

View File

@ -33,6 +33,52 @@ foo.style.opacity: 5
(shape (identifier) (dot) (attr_key) (dot) (attr_key) (attr_value))
)
================================================================================
Block style attributes
================================================================================
foo.style: {
opacity: 5
fill: red;
}
--------------------------------------------------------------------------------
(source_file
(shape
(identifier) (dot) (attr_key)
(block
(attr_key) (attr_value)
(attr_key) (attr_value)
)
)
)
================================================================================
Inline block style attributes
================================================================================
foo.style: { opacity: 5; fill: red; }
foo.style: { opacity: 5 }
--------------------------------------------------------------------------------
(source_file
(shape
(identifier) (dot) (attr_key)
(block
(attr_key) (attr_value)
(attr_key) (attr_value)
)
)
(shape
(identifier) (dot) (attr_key)
(block
(attr_key) (attr_value)
)
)
)