refac connections

This commit is contained in:
Dmitriy Pleshevskiy 2022-12-09 09:00:07 +03:00
parent fc2aec5dcd
commit f7e1a9d597
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
11 changed files with 774 additions and 270 deletions

View file

@ -1,8 +1,10 @@
const PREC = { const PREC = {
COMMENT: -2, COMMENT: -2,
IDENTIFIER: 11,
SHAPE: 10,
CONTAINER: 9, CONTAINER: 9,
CONNECTION: 9,
SHAPE: 11,
IDENTIFIER: 12,
ARROW: 13,
}; };
spaces = /[ \t]/; spaces = /[ \t]/;
@ -18,6 +20,7 @@ module.exports = grammar({
word: ($) => $._word, word: ($) => $._word,
conflicts: ($) => [ conflicts: ($) => [
[$._connection_path, $.container],
//[$.shape_key], //[$.shape_key],
/* /*
[$._shape_path], [$._shape_path],
@ -32,7 +35,26 @@ module.exports = grammar({
rules: { rules: {
source_file: ($) => repeat($._new_root_definition), source_file: ($) => repeat($._new_root_definition),
_new_root_definition: ($) => choice($._eol, $.shape, $.container), _new_root_definition: ($) =>
choice($._eol, seq(choice($.shape, $.container, $.connection), $._end)),
connection: ($) =>
seq(
$._connection_path,
repeat1(seq($._arrow, $._connection_path)),
optional(seq($._colon, $.label))
),
_connection_path: ($) =>
seq(
repeat(
prec(
PREC.CONNECTION,
seq(alias($.shape_key, $.container_key), $._dot)
)
),
$.shape_key
),
container: ($) => container: ($) =>
prec( prec(
@ -40,7 +62,7 @@ module.exports = grammar({
seq( seq(
alias($.shape_key, $.container_key), alias($.shape_key, $.container_key),
choice( choice(
seq($._dot, $.shape), seq($._dot, choice($.shape, $.container)),
seq( seq(
seq( seq(
optional(seq($._colon, optional($.label))), optional(seq($._colon, optional($.label))),
@ -61,24 +83,17 @@ module.exports = grammar({
optional(seq($._colon, optional($.label))), optional(seq($._colon, optional($.label))),
optional(seq(alias($._new_shape_block, $.block))) optional(seq(alias($._new_shape_block, $.block)))
) )
), )
$._end
) )
), ),
shape_key: ($) => shape_key: ($) =>
choice( choice(
$.string, $.string,
prec.left(
seq( seq(
optional($._dash), token(prec(PREC.IDENTIFIER, /\-?([\w\d]+|([\w\d]+( +|\-)[\w\d]+)+)/)),
choice(
$._word,
token(prec(PREC.IDENTIFIER, /([\w\d]+( +|\-)[\w\d]+)+/))
),
optional($._dash) optional($._dash)
) )
)
), ),
_new_shape_block: ($) => _new_shape_block: ($) =>
@ -93,7 +108,10 @@ module.exports = grammar({
), ),
_new_container_block_definition: ($) => _new_container_block_definition: ($) =>
prec(PREC.CONTAINER, choice($._eol, $.shape, $.container)), prec(
PREC.CONTAINER,
choice($._eol, seq(choice($.shape, $.container), $._end))
),
// -------------------------------------------- // --------------------------------------------
@ -107,16 +125,6 @@ module.exports = grammar({
$._shape_definition $._shape_definition
), ),
connection: ($) =>
seq(
$._shape_path,
repeat1(seq($._arrow, $._shape_path)),
optional(
choice(seq($.dot, $._connection_attribute), seq($._colon, $.label))
),
$._end
),
_shape_definition: ($) => _shape_definition: ($) =>
seq( seq(
$._shape_path, $._shape_path,
@ -237,14 +245,14 @@ module.exports = grammar({
_colon: ($) => seq(":"), _colon: ($) => seq(":"),
_arrow: ($) => seq(spaces, $.arrow), _arrow: ($) => seq($.arrow),
arrow: ($) => choice(/-+>/, /--+/, /<-+/, /<-+>/), arrow: ($) => token(prec(PREC.ARROW, choice(/-+>/, /--+/, /<-+/, /<-+>/))),
_dot: ($) => seq($.dot), _dot: ($) => seq($.dot),
dot: ($) => token("."), dot: ($) => token("."),
_unquoted_string: ($) => token(prec(5, /[\w\-]([^'"`\n;{}]*[\w\-])?/)), _unquoted_string: ($) => token(prec(5, /[\w\-?!]([^'"`\n;{}]*[\w\-?!])?/)),
string: ($) => string: ($) =>
choice( choice(

View file

@ -16,6 +16,12 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "_eol" "name": "_eol"
}, },
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "shape" "name": "shape"
@ -23,6 +29,98 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "container" "name": "container"
},
{
"type": "SYMBOL",
"name": "connection"
}
]
},
{
"type": "SYMBOL",
"name": "_end"
}
]
}
]
},
"connection": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_connection_path"
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_arrow"
},
{
"type": "SYMBOL",
"name": "_connection_path"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "SYMBOL",
"name": "label"
}
]
},
{
"type": "BLANK"
}
]
}
]
},
"_connection_path": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "PREC",
"value": 9,
"content": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "shape_key"
},
"named": true,
"value": "container_key"
},
{
"type": "SYMBOL",
"name": "_dot"
}
]
}
}
},
{
"type": "SYMBOL",
"name": "shape_key"
} }
] ]
}, },
@ -51,9 +149,18 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "_dot" "name": "_dot"
}, },
{
"type": "CHOICE",
"members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "shape" "name": "shape"
},
{
"type": "SYMBOL",
"name": "container"
}
]
} }
] ]
}, },
@ -125,7 +232,7 @@
}, },
"shape": { "shape": {
"type": "PREC", "type": "PREC",
"value": 10, "value": 11,
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -196,10 +303,6 @@
"type": "BLANK" "type": "BLANK"
} }
] ]
},
{
"type": "SYMBOL",
"name": "_end"
} }
] ]
} }
@ -212,42 +315,18 @@
"name": "string" "name": "string"
}, },
{ {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_dash"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_word"
},
{ {
"type": "TOKEN", "type": "TOKEN",
"content": { "content": {
"type": "PREC", "type": "PREC",
"value": 11, "value": 12,
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "([\\w\\d]+( +|\\-)[\\w\\d]+)+" "value": "\\-?([\\w\\d]+|([\\w\\d]+( +|\\-)[\\w\\d]+)+)"
} }
} }
}
]
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -263,12 +342,11 @@
} }
] ]
} }
}
] ]
}, },
"_new_shape_block": { "_new_shape_block": {
"type": "PREC", "type": "PREC",
"value": 10, "value": 11,
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -292,7 +370,7 @@
}, },
"_new_shape_block_definition": { "_new_shape_block_definition": {
"type": "PREC", "type": "PREC",
"value": 10, "value": 11,
"content": { "content": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -337,6 +415,12 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "_eol" "name": "_eol"
}, },
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "shape" "name": "shape"
@ -346,6 +430,14 @@
"name": "container" "name": "container"
} }
] ]
},
{
"type": "SYMBOL",
"name": "_end"
}
]
}
]
} }
}, },
"_root_definition": { "_root_definition": {
@ -369,74 +461,6 @@
} }
] ]
}, },
"connection": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_shape_path"
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_arrow"
},
{
"type": "SYMBOL",
"name": "_shape_path"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "dot"
},
{
"type": "SYMBOL",
"name": "_connection_attribute"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "SYMBOL",
"name": "label"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_end"
}
]
},
"_shape_definition": { "_shape_definition": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -1084,10 +1108,6 @@
"_arrow": { "_arrow": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "PATTERN",
"value": "[ \\t]"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "arrow" "name": "arrow"
@ -1095,6 +1115,11 @@
] ]
}, },
"arrow": { "arrow": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 13,
"content": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -1114,6 +1139,8 @@
"value": "<-+>" "value": "<-+>"
} }
] ]
}
}
}, },
"_dot": { "_dot": {
"type": "SEQ", "type": "SEQ",
@ -1138,7 +1165,7 @@
"value": 5, "value": 5,
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[\\w\\-]([^'\"`\\n;{}]*[\\w\\-])?" "value": "[\\w\\-?!]([^'\"`\\n;{}]*[\\w\\-?!])?"
} }
} }
}, },
@ -1299,7 +1326,12 @@
"name": "line_comment" "name": "line_comment"
} }
], ],
"conflicts": [], "conflicts": [
[
"_connection_path",
"container"
]
],
"precedences": [], "precedences": [],
"externals": [], "externals": [],
"inline": [], "inline": [],

View file

@ -1,9 +1,4 @@
[ [
{
"type": "arrow",
"named": true,
"fields": {}
},
{ {
"type": "attr_key", "type": "attr_key",
"named": true, "named": true,
@ -87,14 +82,6 @@
"type": "arrow", "type": "arrow",
"named": true "named": true
}, },
{
"type": "attr_key",
"named": true
},
{
"type": "attr_value",
"named": true
},
{ {
"type": "container_key", "type": "container_key",
"named": true "named": true
@ -126,6 +113,10 @@
"type": "block", "type": "block",
"named": true "named": true
}, },
{
"type": "container",
"named": true
},
{ {
"type": "container_key", "type": "container_key",
"named": true "named": true
@ -248,6 +239,10 @@
"multiple": true, "multiple": true,
"required": false, "required": false,
"types": [ "types": [
{
"type": "connection",
"named": true
},
{ {
"type": "container", "type": "container",
"named": true "named": true
@ -300,6 +295,10 @@
"type": "animated", "type": "animated",
"named": false "named": false
}, },
{
"type": "arrow",
"named": true
},
{ {
"type": "border-radius", "type": "border-radius",
"named": false "named": false

Binary file not shown.

View file

@ -2,11 +2,22 @@
Declare a shape inside a container Declare a shape inside a container
================================================================================ ================================================================================
foo.baz foo.baz
foo.bar.biz
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(container_key) (dot) (shape_key) (container
(container_key) (dot)
(shape (shape_key))
)
(container
(container_key) (dot)
(container
(container_key) (dot)
(shape (shape_key))
)
)
) )
================================================================================ ================================================================================
@ -17,7 +28,10 @@ Use quoted string as a shape key
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(container_key (string)) (dot) (shape_key (string)) (container
(container_key (string)) (dot)
(shape (shape_key (string)))
)
) )
================================================================================ ================================================================================
@ -35,20 +49,59 @@ foo: {
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key) (container
(container_key)
(block (block
(shape_key) (container
(container_key)
(block (block
(shape_key) (container
(container_key)
(block (block
(shape_key) (shape (shape_key))
)
)
)
) )
) )
) )
) )
================================================================================ ================================================================================
Declare labaled container inside a labeled container using block Declare a container with complex keys
================================================================================
Foo biz bar: {
bar biz baz: {
-biz-baz-Baz-: {
Helo world
}
}
}
--------------------------------------------------------------------------------
(source_file
(container
(container_key)
(block
(container
(container_key)
(block
(container
(container_key)
(block
(shape (shape_key))
)
)
)
)
)
)
)
================================================================================
Declare labeled container inside a labeled container using block
================================================================================ ================================================================================
foo: Foo { foo: Foo {
@ -62,13 +115,22 @@ foo: Foo {
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key) (label) (container
(container_key)
(label)
(block (block
(shape_key) (label) (container
(container_key)
(label)
(block (block
(shape_key) (label) (container
(container_key)
(label)
(block (block
(shape_key) (label) (shape (shape_key) (label))
)
)
)
) )
) )
) )
@ -87,11 +149,13 @@ foo: {
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key) (container
(container_key)
(block (block
(shape_key) (shape (shape_key))
(shape_key) (shape (shape_key))
(shape_key) (shape (shape_key))
)
) )
) )

View file

@ -7,8 +7,8 @@ bar
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key) (shape (shape_key))
(shape_key) (shape (shape_key))
) )
================================================================================ ================================================================================
@ -20,8 +20,8 @@ Complex shape key
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key) (shape (shape_key))
(shape_key) (shape (shape_key))
) )
================================================================================ ================================================================================
@ -32,7 +32,7 @@ Use quoted string as a shape key
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key (string)) (shape (shape_key (string)))
) )
================================================================================ ================================================================================
@ -43,9 +43,9 @@ a;b;c
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key) (shape (shape_key))
(shape_key) (shape (shape_key))
(shape_key) (shape (shape_key))
) )
================================================================================ ================================================================================
@ -57,9 +57,9 @@ a: Foo Bar; b: Biz Baz
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key) (label) (shape (shape_key) (label))
(shape_key) (label) (shape (shape_key) (label))
(shape_key) (label) (shape (shape_key) (label))
) )
================================================================================ ================================================================================
@ -73,62 +73,34 @@ bar : Foo Bar; baz
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key) (shape (shape_key))
(shape_key) (label) (shape (shape_key) (label))
(shape_key) (shape (shape_key))
) )
================================================================================ ================================================================================
Shape block Shape block
================================================================================ ================================================================================
foo: { foo: {}
bar: { bar {}
baz: {
biz
}
}
}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key) (shape (shape_key) (block))
(block (shape (shape_key) (block))
(shape_key)
(block
(shape_key)
(block
(shape_key)
)
)
)
) )
================================================================================ ================================================================================
Aliased shape block Labeled shape block
================================================================================ ================================================================================
foo: Foo { foo: Foo {}
bar: Bar {
baz: Baz {
biz: Biz
}
}
}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(shape_key) (label) (shape (shape_key) (label) (block))
(block
(shape_key) (label)
(block
(shape_key) (label)
(block
(shape_key) (label)
)
)
)
) )

View file

@ -0,0 +1,198 @@
================================================================================
Simple connection
================================================================================
foo--bar
biz->baz
biz<->baz
biz<-baz
--------------------------------------------------------------------------------
(source_file
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
)
================================================================================
Formatted connection
================================================================================
foo -- bar
biz -> baz
biz <-> baz
biz <- baz
--------------------------------------------------------------------------------
(source_file
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
)
================================================================================
Connection with looooong arrow
================================================================================
foo ----------- bar
biz ----------> baz
biz <---------> baz
biz <---------- baz
--------------------------------------------------------------------------------
(source_file
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
)
================================================================================
Complex identifier connection
================================================================================
Foo Bar -- Biz Baz
-Bar-Foo- <- -Baz-Biz-
--------------------------------------------------------------------------------
(source_file
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
)
================================================================================
Inline connection
================================================================================
foo--bar->biz->baz
--------------------------------------------------------------------------------
(source_file
(connection
(shape_key)
(arrow)
(shape_key)
(arrow)
(shape_key)
(arrow)
(shape_key)
)
)
================================================================================
Many connections inline
================================================================================
foo--bar;biz->baz
--------------------------------------------------------------------------------
(source_file
(connection
(shape_key)
(arrow)
(shape_key)
)
(connection
(shape_key)
(arrow)
(shape_key)
)
)
================================================================================
Labeled connections
================================================================================
foo--bar: Question?
bar -> baz: Yes
--------------------------------------------------------------------------------
(source_file
(connection
(shape_key)
(arrow)
(shape_key)
(label)
)
(connection
(shape_key)
(arrow)
(shape_key)
(label)
)
)
================================================================================
Connection of shapes inside a containers
================================================================================
foo.biz.baz -> bar.baz.biz: Label
--------------------------------------------------------------------------------
(source_file
(connection
(container_key) (dot) (container_key) (dot) (shape_key)
(arrow)
(container_key) (dot) (container_key) (dot) (shape_key)
(label)
)
)

View file

@ -0,0 +1,97 @@
================================================================================
Declare a shape inside a container
================================================================================
foo.baz
--------------------------------------------------------------------------------
(source_file
(container_key) (dot) (shape_key)
)
================================================================================
Use quoted string as a shape key
================================================================================
'foo'.'baz'
--------------------------------------------------------------------------------
(source_file
(container_key (string)) (dot) (shape_key (string))
)
================================================================================
Declare container inside a container using block
================================================================================
foo: {
bar: {
baz: {
biz
}
}
}
--------------------------------------------------------------------------------
(source_file
(shape_key)
(block
(shape_key)
(block
(shape_key)
(block
(shape_key)
)
)
)
)
================================================================================
Declare labaled container inside a labeled container using block
================================================================================
foo: Foo {
bar: Bar {
baz: Baz {
biz: Biz
}
}
}
--------------------------------------------------------------------------------
(source_file
(shape_key) (label)
(block
(shape_key) (label)
(block
(shape_key) (label)
(block
(shape_key) (label)
)
)
)
)
================================================================================
Declare many shapes inside a container
================================================================================
foo: {
bar
biz
baz
}
--------------------------------------------------------------------------------
(source_file
(shape_key)
(block
(shape_key)
(shape_key)
(shape_key)
)
)

134
test/old_corpus/shape.txt Normal file
View file

@ -0,0 +1,134 @@
================================================================================
Simple shape key
================================================================================
foo
bar
--------------------------------------------------------------------------------
(source_file
(shape_key)
(shape_key)
)
================================================================================
Complex shape key
================================================================================
Foo bar
-Biz-baz-
--------------------------------------------------------------------------------
(source_file
(shape_key)
(shape_key)
)
================================================================================
Use quoted string as a shape key
================================================================================
'foo'
--------------------------------------------------------------------------------
(source_file
(shape_key (string))
)
================================================================================
Define multiple shapes using semicolons
================================================================================
a;b;c
--------------------------------------------------------------------------------
(source_file
(shape_key)
(shape_key)
(shape_key)
)
================================================================================
Labeled shapes
================================================================================
a: Foo Bar
a: Foo Bar; b: Biz Baz
--------------------------------------------------------------------------------
(source_file
(shape_key) (label)
(shape_key) (label)
(shape_key) (label)
)
================================================================================
It should skip white spaces
================================================================================
foo
bar : Foo Bar; baz
--------------------------------------------------------------------------------
(source_file
(shape_key)
(shape_key) (label)
(shape_key)
)
================================================================================
Shape block
================================================================================
foo: {
bar: {
baz: {
biz
}
}
}
--------------------------------------------------------------------------------
(source_file
(shape_key)
(block
(shape_key)
(block
(shape_key)
(block
(shape_key)
)
)
)
)
================================================================================
Aliased shape block
================================================================================
foo: Foo {
bar: Bar {
baz: Baz {
biz: Biz
}
}
}
--------------------------------------------------------------------------------
(source_file
(shape_key) (label)
(block
(shape_key) (label)
(block
(shape_key) (label)
(block
(shape_key) (label)
)
)
)
)

Binary file not shown.