fix multi inline shape attrs

This commit is contained in:
Dmitriy Pleshevskiy 2022-12-05 00:26:24 +03:00
parent 2bd0ded807
commit 975e4c299a
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
6 changed files with 125 additions and 116 deletions

View file

@ -1,3 +1,3 @@
Foo Bar -- Biz Baz foo.shape: 'oval'
-Bar-Foo- <- -Baz-Biz- foo.bar.baz.shape: oval

View file

@ -7,18 +7,13 @@ module.exports = grammar({
// TODO: add the actual grammar rules // TODO: add the actual grammar rules
source_file: ($) => repeat($._definition), source_file: ($) => repeat($._definition),
_definition: ($) => _definition: ($) => choice($._root_attribute, $.connection, $.shape),
choice(
choice($._root_attribute, $._shape_attribute),
$.connection,
$.shape
),
connection: ($) => connection: ($) =>
seq( seq(
$.identifier, $.identifier,
choice( choice(
seq(seq($.arrow, $.identifier, ":", $.label)), seq(seq($.arrow, $.identifier, $._colon, $.label)),
seq(repeat1(seq($.arrow, $.identifier))) seq(repeat1(seq($.arrow, $.identifier)))
), ),
$._end $._end
@ -28,7 +23,10 @@ module.exports = grammar({
seq( seq(
$.identifier, $.identifier,
repeat(seq($.dot, $.identifier)), repeat(seq($.dot, $.identifier)),
optional(seq(":", $.label)), choice(
optional(seq($.dot, $._shape_attribute)),
optional(seq($._colon, $.label))
),
$._end $._end
), ),
@ -39,12 +37,7 @@ module.exports = grammar({
attr_value: ($) => choice($.string, $._unquoted_string), attr_value: ($) => choice($.string, $._unquoted_string),
_root_attribute: ($) => _root_attribute: ($) =>
seq( seq(alias($._root_attr_key, $.attr_key), $._colon, $.attr_value, $._end),
alias($._root_attr_key, $.attr_key),
":",
alias($.label, $.attr_value),
$._end
),
_root_attr_key: ($) => "direction", _root_attr_key: ($) => "direction",
@ -53,19 +46,14 @@ module.exports = grammar({
alias("style", $.attr_key), alias("style", $.attr_key),
$.dot, $.dot,
alias($._style_attr_key, $.attr_key), alias($._style_attr_key, $.attr_key),
":", $._colon,
$.attr_value, $.attr_value
$._end
), ),
_shape_attribute: ($) => _shape_attribute: ($) =>
seq(
$.identifier,
$.dot,
choice( choice(
seq(alias($._shape_attr_key, $.attr_key), ":", $.attr_value, $._end), seq(alias($._shape_attr_key, $.attr_key), $._colon, $.attr_value),
$._style_attribute $._style_attribute
)
), ),
_shape_attr_key: ($) => _shape_attr_key: ($) =>
@ -114,6 +102,8 @@ module.exports = grammar({
) )
), ),
_colon: ($) => seq(repeat(" "), ":", repeat(" ")),
_dash: ($) => token.immediate("-"), _dash: ($) => token.immediate("-"),
_word: ($) => /[\w\d]+/, _word: ($) => /[\w\d]+/,
@ -128,13 +118,13 @@ module.exports = grammar({
) )
), ),
_unquoted_string: ($) => /[^\n;{]+/, _unquoted_string: ($) => token.immediate(/[^'"`\n;{]+/),
string: ($) => string: ($) =>
choice( choice(
seq("'", repeat(token(/[^'\n]+/)), "'"), seq("'", repeat(token.immediate(/[^'\n]+/)), "'"),
seq('"', repeat(token(/[^'\n]+/)), '"'), seq('"', repeat(token.immediate(/[^"\n]+/)), '"'),
seq("`", repeat(token(/[^'\n]+/)), "`") seq("`", repeat(token.immediate(/[^`\n]+/)), "`")
), ),
_end: ($) => choice(";", "\n", "\0"), _end: ($) => choice(";", "\n", "\0"),

View file

@ -10,21 +10,12 @@
} }
}, },
"_definition": { "_definition": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_root_attribute" "name": "_root_attribute"
}, },
{
"type": "SYMBOL",
"name": "_shape_attribute"
}
]
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "connection" "name": "connection"
@ -60,8 +51,8 @@
"name": "identifier" "name": "identifier"
}, },
{ {
"type": "STRING", "type": "SYMBOL",
"value": ":" "name": "_colon"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -123,6 +114,9 @@
] ]
} }
}, },
{
"type": "CHOICE",
"members": [
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -130,8 +124,29 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "STRING", "type": "SYMBOL",
"value": ":" "name": "dot"
},
{
"type": "SYMBOL",
"name": "_shape_attribute"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_colon"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -143,6 +158,8 @@
"type": "BLANK" "type": "BLANK"
} }
] ]
}
]
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -193,17 +210,12 @@
"value": "attr_key" "value": "attr_key"
}, },
{ {
"type": "STRING", "type": "SYMBOL",
"value": ":" "name": "_colon"
}, },
{ {
"type": "ALIAS",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "label" "name": "attr_value"
},
"named": true,
"value": "attr_value"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -241,31 +253,16 @@
"value": "attr_key" "value": "attr_key"
}, },
{ {
"type": "STRING", "type": "SYMBOL",
"value": ":" "name": "_colon"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "attr_value" "name": "attr_value"
},
{
"type": "SYMBOL",
"name": "_end"
} }
] ]
}, },
"_shape_attribute": { "_shape_attribute": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "dot"
},
{
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -281,16 +278,12 @@
"value": "attr_key" "value": "attr_key"
}, },
{ {
"type": "STRING", "type": "SYMBOL",
"value": ":" "name": "_colon"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "attr_value" "name": "attr_value"
},
{
"type": "SYMBOL",
"name": "_end"
} }
] ]
}, },
@ -299,8 +292,6 @@
"name": "_style_attribute" "name": "_style_attribute"
} }
] ]
}
]
}, },
"_shape_attr_key": { "_shape_attr_key": {
"type": "CHOICE", "type": "CHOICE",
@ -489,6 +480,29 @@
] ]
} }
}, },
"_colon": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "REPEAT",
"content": {
"type": "STRING",
"value": " "
}
}
]
},
"_dash": { "_dash": {
"type": "IMMEDIATE_TOKEN", "type": "IMMEDIATE_TOKEN",
"content": { "content": {
@ -578,8 +592,11 @@
} }
}, },
"_unquoted_string": { "_unquoted_string": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^\\n;{]+" "value": "[^'\"`\\n;{]+"
}
}, },
"string": { "string": {
"type": "CHOICE", "type": "CHOICE",
@ -594,7 +611,7 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "TOKEN", "type": "IMMEDIATE_TOKEN",
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^'\\n]+" "value": "[^'\\n]+"
@ -617,10 +634,10 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "TOKEN", "type": "IMMEDIATE_TOKEN",
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^'\\n]+" "value": "[^\"\\n]+"
} }
} }
}, },
@ -640,10 +657,10 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "TOKEN", "type": "IMMEDIATE_TOKEN",
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^'\\n]+" "value": "[^`\\n]+"
} }
} }
}, },

View file

@ -75,6 +75,14 @@
"multiple": true, "multiple": true,
"required": true, "required": true,
"types": [ "types": [
{
"type": "attr_key",
"named": true
},
{
"type": "attr_value",
"named": true
},
{ {
"type": "dot", "type": "dot",
"named": true "named": true
@ -110,14 +118,6 @@
"type": "connection", "type": "connection",
"named": true "named": true
}, },
{
"type": "dot",
"named": true
},
{
"type": "identifier",
"named": true
},
{ {
"type": "shape", "type": "shape",
"named": true "named": true

Binary file not shown.

View file

@ -15,11 +15,13 @@ Inline shape attribute
================== ==================
foo.shape: oval foo.shape: oval
foo.bar.baz.shape: oval
--- ---
(source_file (source_file
(identifier) (dot) (attr_key) (attr_value) (shape (identifier) (dot) (attr_key) (attr_value))
(shape (identifier) (dot) (identifier) (dot) (identifier) (dot) (attr_key) (attr_value))
) )
================== ==================
@ -31,7 +33,7 @@ foo.style.opacity: 5
--- ---
(source_file (source_file
(identifier) (dot) (attr_key) (dot) (attr_key) (attr_value) (shape (identifier) (dot) (attr_key) (dot) (attr_key) (attr_value))
) )