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 foo.style: { opacity: 5; fill: red }
biz -> baz
biz <-> baz
biz <- baz

View file

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

View file

@ -180,15 +180,27 @@
] ]
}, },
"attr_value": { "attr_value": {
"type": "CHOICE", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "REPEAT",
"name": "string" "content": {
"type": "STRING",
"value": " "
}
}, },
{ {
"type": "SYMBOL", "type": "CHOICE",
"name": "_unquoted_string" "members": [
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "_unquoted_string"
}
]
} }
] ]
}, },
@ -222,41 +234,6 @@
"type": "STRING", "type": "STRING",
"value": "direction" "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": { "_shape_attribute": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "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": { "_connection_attribute": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -508,13 +652,6 @@
{ {
"type": "STRING", "type": "STRING",
"value": ":" "value": ":"
},
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
} }
] ]
}, },
@ -625,7 +762,7 @@
"type": "IMMEDIATE_TOKEN", "type": "IMMEDIATE_TOKEN",
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^'\"`\\n;{]+" "value": "[^'\"`\\n;{}]+"
} }
}, },
"string": { "string": {
@ -752,6 +889,12 @@
], ],
[ [
"arrow" "arrow"
],
[
"_style_attr_block"
],
[
"_inner_style_attribute"
] ]
], ],
"precedences": [], "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", "type": "connection",
"named": true, "named": true,
@ -95,6 +114,10 @@
"type": "attr_value", "type": "attr_value",
"named": true "named": true
}, },
{
"type": "block",
"named": true
},
{ {
"type": "dot", "type": "dot",
"named": true "named": true
@ -273,5 +296,13 @@
{ {
"type": "width", "type": "width",
"named": false "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)) (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)
)
)
)