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( choice(
$.identifier, seq(alias($._shape_attr_key, $.attr_key), $._colon, $.attr_value),
$.dot, $._style_attribute
choice(
seq(alias($._shape_attr_key, $.attr_key), ":", $.attr_value, $._end),
$._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

@ -13,17 +13,8 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "CHOICE", "type": "SYMBOL",
"members": [ "name": "_root_attribute"
{
"type": "SYMBOL",
"name": "_root_attribute"
},
{
"type": "SYMBOL",
"name": "_shape_attribute"
}
]
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -60,8 +51,8 @@
"name": "identifier" "name": "identifier"
}, },
{ {
"type": "STRING", "type": "SYMBOL",
"value": ":" "name": "_colon"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -127,20 +118,46 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SEQ", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "STRING", "type": "SEQ",
"value": ":" "members": [
{
"type": "SYMBOL",
"name": "dot"
},
{
"type": "SYMBOL",
"name": "_shape_attribute"
}
]
}, },
{ {
"type": "SYMBOL", "type": "BLANK"
"name": "label"
} }
] ]
}, },
{ {
"type": "BLANK" "type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "SYMBOL",
"name": "label"
}
]
},
{
"type": "BLANK"
}
]
} }
] ]
}, },
@ -193,17 +210,12 @@
"value": "attr_key" "value": "attr_key"
}, },
{ {
"type": "STRING", "type": "SYMBOL",
"value": ":" "name": "_colon"
}, },
{ {
"type": "ALIAS", "type": "SYMBOL",
"content": { "name": "attr_value"
"type": "SYMBOL",
"name": "label"
},
"named": true,
"value": "attr_value"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -241,64 +253,43 @@
"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", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SEQ",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "dot"
},
{
"type": "CHOICE",
"members": [ "members": [
{ {
"type": "SEQ", "type": "ALIAS",
"members": [ "content": {
{ "type": "SYMBOL",
"type": "ALIAS", "name": "_shape_attr_key"
"content": { },
"type": "SYMBOL", "named": true,
"name": "_shape_attr_key" "value": "attr_key"
},
"named": true,
"value": "attr_key"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "SYMBOL",
"name": "attr_value"
},
{
"type": "SYMBOL",
"name": "_end"
}
]
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_style_attribute" "name": "_colon"
},
{
"type": "SYMBOL",
"name": "attr_value"
} }
] ]
},
{
"type": "SYMBOL",
"name": "_style_attribute"
} }
] ]
}, },
@ -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": "PATTERN", "type": "IMMEDIATE_TOKEN",
"value": "[^\\n;{]+" "content": {
"type": "PATTERN",
"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))
) )