Add structure for container and shapes #1

Merged
pleshevskiy merged 12 commits from rewrite-structure into main 2022-12-09 12:36:32 +03:00
5 changed files with 559 additions and 407 deletions
Showing only changes of commit fc2aec5dcd - Show all commits

View file

@ -1,24 +1,103 @@
const spaces = repeat(choice(" ", "\t")); const PREC = {
COMMENT: -2,
IDENTIFIER: 11,
SHAPE: 10,
CONTAINER: 9,
};
spaces = /[ \t]/;
module.exports = grammar({ module.exports = grammar({
name: "d2", name: "d2",
extras: ($) => [$.line_comment], extras: ($) => [
/[ \f\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/,
$.line_comment,
],
word: ($) => $._word, word: ($) => $._word,
conflicts: ($) => [ conflicts: ($) => [
[$.shape_key], //[$.shape_key],
/*
[$._shape_path], [$._shape_path],
[$._shape_block], [$._shape_block],
[$._shape_block_definition], [$._shape_block_definition],
[$._style_attr_block], [$._style_attr_block],
[$._inner_style_attribute], [$._inner_style_attribute],
[$._emptyline], [$._emptyline],
*/
], ],
rules: { rules: {
source_file: ($) => repeat($._root_definition), source_file: ($) => repeat($._new_root_definition),
_new_root_definition: ($) => choice($._eol, $.shape, $.container),
container: ($) =>
prec(
PREC.CONTAINER,
seq(
alias($.shape_key, $.container_key),
choice(
seq($._dot, $.shape),
seq(
seq(
optional(seq($._colon, optional($.label))),
optional(seq(alias($._new_container_block, $.block)))
)
)
)
)
),
shape: ($) =>
prec(
PREC.SHAPE,
seq(
$.shape_key,
optional(
seq(
optional(seq($._colon, optional($.label))),
optional(seq(alias($._new_shape_block, $.block)))
)
),
$._end
)
),
shape_key: ($) =>
choice(
$.string,
prec.left(
seq(
optional($._dash),
choice(
$._word,
token(prec(PREC.IDENTIFIER, /([\w\d]+( +|\-)[\w\d]+)+/))
),
optional($._dash)
)
)
),
_new_shape_block: ($) =>
prec(PREC.SHAPE, seq("{", repeat($._new_shape_block_definition), "}")),
_new_shape_block_definition: ($) => prec(PREC.SHAPE, choice($._eol)),
_new_container_block: ($) =>
prec(
PREC.CONTAINER,
seq("{", repeat($._new_container_block_definition), "}")
),
_new_container_block_definition: ($) =>
prec(PREC.CONTAINER, choice($._eol, $.shape, $.container)),
// --------------------------------------------
// source_file: ($) => repeat($._root_definition),
_root_definition: ($) => _root_definition: ($) =>
choice( choice(
@ -60,19 +139,6 @@ module.exports = grammar({
$.shape_key $.shape_key
), ),
shape_key: ($) =>
choice(
$.string,
seq(
optional($._dash),
choice(
$._word,
repeat1(seq($._word, choice(repeat1(" "), $._dash), $._word))
),
optional($._dash)
)
),
_dash: ($) => token.immediate("-"), _dash: ($) => token.immediate("-"),
label: ($) => choice($.string, $._unquoted_string), label: ($) => choice($.string, $._unquoted_string),
@ -169,15 +235,16 @@ module.exports = grammar({
_connection_attr_key: ($) => choice("source-arrowhead", "target-arrowhead"), _connection_attr_key: ($) => choice("source-arrowhead", "target-arrowhead"),
_colon: ($) => seq(spaces, ":"), _colon: ($) => seq(":"),
_arrow: ($) => seq(spaces, $.arrow), _arrow: ($) => seq(spaces, $.arrow),
arrow: ($) => choice(/-+>/, /--+/, /<-+/, /<-+>/), arrow: ($) => choice(/-+>/, /--+/, /<-+/, /<-+>/),
dot: ($) => token.immediate("."), _dot: ($) => seq($.dot),
dot: ($) => token("."),
_unquoted_string: ($) => token.immediate(/[^'"`\n;{}]+/), _unquoted_string: ($) => token(prec(5, /[\w\-]([^'"`\n;{}]*[\w\-])?/)),
string: ($) => string: ($) =>
choice( choice(
@ -186,12 +253,12 @@ module.exports = grammar({
seq("`", repeat(token.immediate(/[^`\n]+/)), "`") seq("`", repeat(token.immediate(/[^`\n]+/)), "`")
), ),
line_comment: ($) => token(prec(-2, seq("#", /.*/))), line_comment: ($) => token(prec(PREC.COMMENT, seq("#", /.*/))),
_word: ($) => /[\w\d]+/, _word: ($) => /[\w\d]+/,
_emptyline: ($) => seq(spaces, $._eof), _emptyline: ($) => prec(-1, seq(spaces, $._eol)),
_eof: ($) => choice("\n", "\0"), _eol: ($) => choice("\n", "\0"),
_end: ($) => seq(spaces, choice(";", $._eof)), _end: ($) => seq(choice(";", $._eol)),
}, },
}); });

View file

@ -6,7 +6,346 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_root_definition" "name": "_new_root_definition"
}
},
"_new_root_definition": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_eol"
},
{
"type": "SYMBOL",
"name": "shape"
},
{
"type": "SYMBOL",
"name": "container"
}
]
},
"container": {
"type": "PREC",
"value": 9,
"content": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "shape_key"
},
"named": true,
"value": "container_key"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_dot"
},
{
"type": "SYMBOL",
"name": "shape"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "label"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_new_container_block"
},
"named": true,
"value": "block"
}
]
},
{
"type": "BLANK"
}
]
}
]
}
]
}
]
}
]
}
},
"shape": {
"type": "PREC",
"value": 10,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "shape_key"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "label"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_new_shape_block"
},
"named": true,
"value": "block"
}
]
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_end"
}
]
}
},
"shape_key": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_dash"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_word"
},
{
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 11,
"content": {
"type": "PATTERN",
"value": "([\\w\\d]+( +|\\-)[\\w\\d]+)+"
}
}
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_dash"
},
{
"type": "BLANK"
}
]
}
]
}
}
]
},
"_new_shape_block": {
"type": "PREC",
"value": 10,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_new_shape_block_definition"
}
},
{
"type": "STRING",
"value": "}"
}
]
}
},
"_new_shape_block_definition": {
"type": "PREC",
"value": 10,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_eol"
}
]
}
},
"_new_container_block": {
"type": "PREC",
"value": 9,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_new_container_block_definition"
}
},
{
"type": "STRING",
"value": "}"
}
]
}
},
"_new_container_block_definition": {
"type": "PREC",
"value": 9,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_eol"
},
{
"type": "SYMBOL",
"name": "shape"
},
{
"type": "SYMBOL",
"name": "container"
}
]
} }
}, },
"_root_definition": { "_root_definition": {
@ -144,20 +483,8 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -213,20 +540,8 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@ -243,40 +558,16 @@
"value": "container_key" "value": "container_key"
}, },
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "dot" "name": "dot"
}, },
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
} }
] ]
} }
@ -287,85 +578,6 @@
} }
] ]
}, },
"shape_key": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_dash"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_word"
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_word"
},
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT1",
"content": {
"type": "STRING",
"value": " "
}
},
{
"type": "SYMBOL",
"name": "_dash"
}
]
},
{
"type": "SYMBOL",
"name": "_word"
}
]
}
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_dash"
},
{
"type": "BLANK"
}
]
}
]
}
]
},
"_dash": { "_dash": {
"type": "IMMEDIATE_TOKEN", "type": "IMMEDIATE_TOKEN",
"content": { "content": {
@ -390,20 +602,8 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -477,20 +677,8 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "STRING", "type": "STRING",
@ -509,20 +697,8 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -563,20 +739,8 @@
] ]
}, },
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "STRING", "type": "STRING",
@ -686,40 +850,16 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "{" "value": "{"
}, },
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@ -776,20 +916,8 @@
] ]
}, },
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "STRING", "type": "STRING",
@ -801,20 +929,8 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "ALIAS", "type": "ALIAS",
@ -959,22 +1075,6 @@
"_colon": { "_colon": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{ {
"type": "STRING", "type": "STRING",
"value": ":" "value": ":"
@ -985,20 +1085,8 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -1027,18 +1115,31 @@
} }
] ]
}, },
"_dot": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "dot"
}
]
},
"dot": { "dot": {
"type": "IMMEDIATE_TOKEN", "type": "TOKEN",
"content": { "content": {
"type": "STRING", "type": "STRING",
"value": "." "value": "."
} }
}, },
"_unquoted_string": { "_unquoted_string": {
"type": "IMMEDIATE_TOKEN", "type": "TOKEN",
"content": {
"type": "PREC",
"value": 5,
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^'\"`\\n;{}]+" "value": "[\\w\\-]([^'\"`\\n;{}]*[\\w\\-])?"
}
} }
}, },
"string": { "string": {
@ -1140,31 +1241,23 @@
"value": "[\\w\\d]+" "value": "[\\w\\d]+"
}, },
"_emptyline": { "_emptyline": {
"type": "PREC",
"value": -1,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "REPEAT", "type": "PATTERN",
"content": { "value": "[ \\t]"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_eof" "name": "_eol"
} }
] ]
}
}, },
"_eof": { "_eol": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -1180,22 +1273,6 @@
"_end": { "_end": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": " "
},
{
"type": "STRING",
"value": "\t"
}
]
}
},
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -1205,7 +1282,7 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_eof" "name": "_eol"
} }
] ]
} }
@ -1213,34 +1290,16 @@
} }
}, },
"extras": [ "extras": [
{
"type": "PATTERN",
"value": "[ \\f\\t\\v\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff]"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "line_comment" "name": "line_comment"
} }
], ],
"conflicts": [ "conflicts": [],
[
"shape_key"
],
[
"_shape_path"
],
[
"_shape_block"
],
[
"_shape_block_definition"
],
[
"_style_attr_block"
],
[
"_inner_style_attribute"
],
[
"_emptyline"
]
],
"precedences": [], "precedences": [],
"externals": [], "externals": [],
"inline": [], "inline": [],

View file

@ -48,6 +48,10 @@
"type": "connection", "type": "connection",
"named": true "named": true
}, },
{
"type": "container",
"named": true
},
{ {
"type": "container_key", "type": "container_key",
"named": true "named": true
@ -60,6 +64,10 @@
"type": "label", "type": "label",
"named": true "named": true
}, },
{
"type": "shape",
"named": true
},
{ {
"type": "shape_key", "type": "shape_key",
"named": true "named": true
@ -106,6 +114,37 @@
] ]
} }
}, },
{
"type": "container",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "block",
"named": true
},
{
"type": "container_key",
"named": true
},
{
"type": "dot",
"named": true
},
{
"type": "label",
"named": true
},
{
"type": "shape",
"named": true
}
]
}
},
{ {
"type": "container_key", "type": "container_key",
"named": true, "named": true,
@ -163,6 +202,29 @@
] ]
} }
}, },
{
"type": "shape",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "block",
"named": true
},
{
"type": "label",
"named": true
},
{
"type": "shape_key",
"named": true
}
]
}
},
{ {
"type": "shape_key", "type": "shape_key",
"named": true, "named": true,
@ -187,39 +249,11 @@
"required": false, "required": false,
"types": [ "types": [
{ {
"type": "attr_key", "type": "container",
"named": true "named": true
}, },
{ {
"type": "attr_value", "type": "shape",
"named": true
},
{
"type": "block",
"named": true
},
{
"type": "connection",
"named": true
},
{
"type": "container_key",
"named": true
},
{
"type": "dot",
"named": true
},
{
"type": "invalid",
"named": true
},
{
"type": "label",
"named": true
},
{
"type": "shape_key",
"named": true "named": true
} }
] ]
@ -234,18 +268,10 @@
"type": "\u0000", "type": "\u0000",
"named": false "named": false
}, },
{
"type": "\t",
"named": false
},
{ {
"type": "\n", "type": "\n",
"named": false "named": false
}, },
{
"type": " ",
"named": false
},
{ {
"type": "\"", "type": "\"",
"named": false "named": false

Binary file not shown.

Binary file not shown.