add root attributes

This commit is contained in:
Dmitriy Pleshevskiy 2022-12-09 10:40:58 +03:00
parent 1ea16740eb
commit f279551465
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
8 changed files with 415 additions and 752 deletions

19
examples/all.d2 Normal file
View File

@ -0,0 +1,19 @@
shape: oval
label: ten
constraint: utehu
icon: huell
opacity: 1
fill: red
stroke: red
stroke-width: 5
stroke-dash: 4
border-radius: 1
font-color: red
shadow: true
multiple: true
animated: true
link: https://to
near: abc
hello

View File

@ -1,11 +1,13 @@
const PREC = {
COMMENT: -2,
UNQUOTED_STRING: 5,
CONTAINER: 9,
CONNECTION: 9,
SHAPE: 11,
IDENTIFIER: 12,
ARROW: 13,
UNQUOTED_STRING: 0,
CONTAINER: 2,
CONNECTION: 2,
SHAPE: 3,
IDENTIFIER: 0,
ARROW: 0,
ATTRIBUTE: 0,
ATTRIBUTE_KEY: 0,
};
spaces = /[ \t]/;
@ -18,7 +20,7 @@ module.exports = grammar({
$.line_comment,
],
word: ($) => $._word,
word: ($) => $._identifier,
conflicts: ($) => [
[$._connection_path, $.container],
@ -37,7 +39,20 @@ module.exports = grammar({
source_file: ($) => repeat($._new_root_definition),
_new_root_definition: ($) =>
choice($._eol, seq(choice($.shape, $.container, $.connection), $._end)),
choice(
$._eol,
seq(
choice(
alias($._new_root_attribute, $.attribute),
$.shape,
$.container,
$.connection
),
$._end
)
),
// connections
connection: ($) =>
seq(
@ -57,6 +72,8 @@ module.exports = grammar({
$.shape_key
),
// containers
container: ($) =>
prec(
PREC.CONTAINER,
@ -74,6 +91,20 @@ module.exports = grammar({
)
),
_new_container_block: ($) =>
prec(
PREC.CONTAINER,
seq("{", repeat($._new_container_block_definition), "}")
),
_new_container_block_definition: ($) =>
prec(
PREC.CONTAINER,
choice($._eol, seq(choice($.shape, $.container, $.connection), $._end))
),
// shapes
shape: ($) =>
prec(
PREC.SHAPE,
@ -88,35 +119,87 @@ module.exports = grammar({
)
),
shape_key: ($) =>
choice(
$.string,
seq(
token(prec(PREC.IDENTIFIER, /\-?([\w\d]+|([\w\d]+( +|\-)[\w\d]+)+)/)),
optional($._dash)
)
),
shape_key: ($) => choice($.string, seq($._identifier, optional($._dash))),
_identifier: ($) =>
token(prec(PREC.IDENTIFIER, /\-?([\w\d]+|([\w\d]+( +|\-)[\w\d]+)+)/)),
_new_shape_block: ($) =>
prec(PREC.SHAPE, seq("{", repeat($._new_shape_block_definition), "}")),
_new_shape_block_definition: ($) => prec(PREC.SHAPE, choice($._eol)),
_new_container_block: ($) =>
// attributes
_new_root_attribute: ($) =>
prec(
PREC.CONTAINER,
seq("{", repeat($._new_container_block_definition), "}")
PREC.ATTRIBUTE,
seq(alias($._root_attr_key, $.attr_key), $._colon, $.attr_value)
),
_new_container_block_definition: ($) =>
prec(
PREC.CONTAINER,
choice($._eol, seq(choice($.shape, $.container, $.connection), $._end))
_root_attr_key: ($) =>
choice(
"direction",
// reserved but doesn't affected for root
alias(
choice(
"shape",
"label",
"constraint",
"icon",
$._common_style_attr_key,
$._text_attr_key
),
$.reserved
)
),
_shape_attr_key: ($) =>
prec(
PREC.ATTRIBUTE_KEY,
choice(
"shape",
"label",
// sql
"constraint",
// image
"icon",
"width",
"height"
)
),
_style_attr_key: ($) => choice($._common_style_attr_key, "3d"),
_common_style_attr_key: ($) =>
choice(
"opacity",
"fill",
"stroke",
"stroke-width",
"stroke-dash",
"border-radius",
"font-color",
"shadow",
"multiple",
"animated",
"link"
),
_text_attr_key: ($) => "near",
_connection_attr_key: ($) => choice("source-arrowhead", "target-arrowhead"),
//
label: ($) => choice($.string, $._unquoted_string),
attr_value: ($) => seq(choice($.string, $._unquoted_string)),
// --------------------------------------------
// source_file: ($) => repeat($._root_definition),
/*
_root_definition: ($) =>
choice(
@ -148,24 +231,6 @@ module.exports = grammar({
$.shape_key
),
_dash: ($) => token.immediate("-"),
label: ($) => choice($.string, $._unquoted_string),
attr_value: ($) => seq(spaces, choice($.string, $._unquoted_string)),
_root_attribute: ($) =>
choice(
seq(
alias($._root_attr_key, $.attr_key),
$._colon,
$.attr_value,
$._end
),
alias(seq($._shape_attribute, $._end), $.invalid)
),
_root_attr_key: ($) => "direction",
_shape_block: ($) =>
seq(
@ -211,38 +276,9 @@ module.exports = grammar({
_connection_attribute: ($) =>
seq(alias($._connection_attr_key, $.attr_key), $._colon, $.attr_value),
*/
_shape_attr_key: ($) =>
choice(
"shape",
"label",
// sql
"constraint",
// image
"icon",
"width",
"height"
),
_style_attr_key: ($) =>
choice(
"opacity",
"fill",
"stroke",
"stroke-width",
"stroke-dash",
"border-radius",
"font-color",
"shadow",
"multiple",
"animated",
"3d",
"link"
),
_text_attr_key: ($) => "near",
_connection_attr_key: ($) => choice("source-arrowhead", "target-arrowhead"),
_dash: ($) => token.immediate("-"),
_colon: ($) => seq(":"),

View File

@ -2,7 +2,7 @@
;-------------------------------------------------------------------------------
(ERROR) @error
(invalid (_) @error)
; (invalid (_) @error)
;-------------------------------------------------------------------------------
(container_key) @constant
@ -15,7 +15,7 @@
(container_key (string) @string.special)
(shape_key (string) @string)
(label) @string
(attr_value) @string
; (attr_value) @string
; Comments
;-------------------------------------------------------------------------------
@ -42,4 +42,4 @@
;-------------------------------------------------------------------------------
(ERROR) @error
(invalid (_) @error)
; (invalid (_) @error)

View File

@ -1,6 +1,6 @@
{
"name": "d2",
"word": "_word",
"word": "_identifier",
"rules": {
"source_file": {
"type": "REPEAT",
@ -22,6 +22,15 @@
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_new_root_attribute"
},
"named": true,
"value": "attribute"
},
{
"type": "SYMBOL",
"name": "shape"
@ -97,7 +106,7 @@
"type": "REPEAT",
"content": {
"type": "PREC",
"value": 9,
"value": 2,
"content": {
"type": "SEQ",
"members": [
@ -126,7 +135,7 @@
},
"container": {
"type": "PREC",
"value": 9,
"value": 2,
"content": {
"type": "SEQ",
"members": [
@ -230,9 +239,72 @@
]
}
},
"_new_container_block": {
"type": "PREC",
"value": 2,
"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": 2,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_eol"
},
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "shape"
},
{
"type": "SYMBOL",
"name": "container"
},
{
"type": "SYMBOL",
"name": "connection"
}
]
},
{
"type": "SYMBOL",
"name": "_end"
}
]
}
]
}
},
"shape": {
"type": "PREC",
"value": 11,
"value": 3,
"content": {
"type": "SEQ",
"members": [
@ -318,15 +390,8 @@
"type": "SEQ",
"members": [
{
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 12,
"content": {
"type": "PATTERN",
"value": "\\-?([\\w\\d]+|([\\w\\d]+( +|\\-)[\\w\\d]+)+)"
}
}
"type": "SYMBOL",
"name": "_identifier"
},
{
"type": "CHOICE",
@ -344,9 +409,20 @@
}
]
},
"_identifier": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 0,
"content": {
"type": "PATTERN",
"value": "\\-?([\\w\\d]+|([\\w\\d]+( +|\\-)[\\w\\d]+)+)"
}
}
},
"_new_shape_block": {
"type": "PREC",
"value": 11,
"value": 3,
"content": {
"type": "SEQ",
"members": [
@ -370,7 +446,7 @@
},
"_new_shape_block_definition": {
"type": "PREC",
"value": 11,
"value": 3,
"content": {
"type": "CHOICE",
"members": [
@ -381,656 +457,122 @@
]
}
},
"_new_container_block": {
"_new_root_attribute": {
"type": "PREC",
"value": 9,
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_new_container_block_definition"
}
"name": "_root_attr_key"
},
"named": true,
"value": "attr_key"
},
{
"type": "STRING",
"value": "}"
}
]
}
},
"_new_container_block_definition": {
"type": "PREC",
"value": 9,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_eol"
"name": "_colon"
},
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "shape"
},
{
"type": "SYMBOL",
"name": "container"
},
{
"type": "SYMBOL",
"name": "connection"
}
]
},
{
"type": "SYMBOL",
"name": "_end"
}
]
"type": "SYMBOL",
"name": "attr_value"
}
]
}
},
"_root_definition": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_emptyline"
},
{
"type": "SYMBOL",
"name": "_root_attribute"
},
{
"type": "SYMBOL",
"name": "connection"
},
{
"type": "SYMBOL",
"name": "_shape_definition"
}
]
},
"_shape_definition": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_shape_path"
},
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "dot"
},
{
"type": "SYMBOL",
"name": "_shape_attribute"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]"
},
{
"type": "SYMBOL",
"name": "label"
}
]
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_shape_block"
},
"named": true,
"value": "block"
},
{
"type": "BLANK"
}
]
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_end"
}
]
},
"_shape_path": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "shape_key"
},
"named": true,
"value": "container_key"
},
{
"type": "PATTERN",
"value": "[ \\t]"
},
{
"type": "SYMBOL",
"name": "dot"
},
{
"type": "PATTERN",
"value": "[ \\t]"
}
]
}
},
{
"type": "SYMBOL",
"name": "shape_key"
}
]
},
"_dash": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "-"
}
},
"label": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "_unquoted_string"
}
]
},
"attr_value": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "_unquoted_string"
}
]
}
]
},
"_root_attribute": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_root_attr_key"
},
"named": true,
"value": "attr_key"
},
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "SYMBOL",
"name": "attr_value"
},
{
"type": "SYMBOL",
"name": "_end"
}
]
},
{
"type": "ALIAS",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_shape_attribute"
},
{
"type": "SYMBOL",
"name": "_end"
}
]
},
"named": true,
"value": "invalid"
}
]
},
"_root_attr_key": {
"type": "STRING",
"value": "direction"
},
"_shape_block": {
"type": "SEQ",
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]"
},
{
"type": "STRING",
"value": "{"
"value": "direction"
},
{
"type": "REPEAT",
"type": "ALIAS",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_emptyline"
"type": "STRING",
"value": "shape"
},
{
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]"
},
{
"type": "SYMBOL",
"name": "_shape_block_definition"
}
]
"type": "STRING",
"value": "label"
},
{
"type": "STRING",
"value": "constraint"
},
{
"type": "STRING",
"value": "icon"
},
{
"type": "SYMBOL",
"name": "_common_style_attr_key"
},
{
"type": "SYMBOL",
"name": "_text_attr_key"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_shape_block_definition"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_end"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "PATTERN",
"value": "[ \\t]"
},
{
"type": "STRING",
"value": "}"
}
]
},
"_shape_block_definition": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "connection"
},
{
"type": "SYMBOL",
"name": "_shape_definition"
},
{
"type": "SYMBOL",
"name": "_shape_attribute"
}
]
},
"_shape_attribute": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_shape_attr_key"
},
"named": true,
"value": "attr_key"
},
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "SYMBOL",
"name": "attr_value"
}
]
},
{
"type": "SYMBOL",
"name": "_style_attribute"
}
]
},
"_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": "PATTERN",
"value": "[ \\t]"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "PATTERN",
"value": "[ \\t]"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_emptyline"
},
{
"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": "PATTERN",
"value": "[ \\t]"
},
{
"type": "STRING",
"value": "}"
}
]
},
"_inner_style_attribute": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]"
},
{
"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": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_connection_attr_key"
},
"named": true,
"value": "attr_key"
},
{
"type": "SYMBOL",
"name": "_colon"
},
{
"type": "SYMBOL",
"name": "attr_value"
"value": "reserved"
}
]
},
"_shape_attr_key": {
"type": "PREC",
"value": 0,
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "shape"
},
{
"type": "STRING",
"value": "label"
},
{
"type": "STRING",
"value": "constraint"
},
{
"type": "STRING",
"value": "icon"
},
{
"type": "STRING",
"value": "width"
},
{
"type": "STRING",
"value": "height"
}
]
}
},
"_style_attr_key": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "shape"
"type": "SYMBOL",
"name": "_common_style_attr_key"
},
{
"type": "STRING",
"value": "label"
},
{
"type": "STRING",
"value": "constraint"
},
{
"type": "STRING",
"value": "icon"
},
{
"type": "STRING",
"value": "width"
},
{
"type": "STRING",
"value": "height"
"value": "3d"
}
]
},
"_style_attr_key": {
"_common_style_attr_key": {
"type": "CHOICE",
"members": [
{
@ -1073,10 +615,6 @@
"type": "STRING",
"value": "animated"
},
{
"type": "STRING",
"value": "3d"
},
{
"type": "STRING",
"value": "link"
@ -1100,6 +638,44 @@
}
]
},
"label": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "_unquoted_string"
}
]
},
"attr_value": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "_unquoted_string"
}
]
}
]
},
"_dash": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "-"
}
},
"_colon": {
"type": "SEQ",
"members": [
@ -1122,7 +698,7 @@
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 13,
"value": 0,
"content": {
"type": "CHOICE",
"members": [
@ -1166,7 +742,7 @@
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 5,
"value": 0,
"content": {
"type": "PATTERN",
"value": "[\\w\\-?!]([^'\"`\\n;{}]*[\\w\\-?!])?"

View File

@ -2,7 +2,17 @@
{
"type": "attr_key",
"named": true,
"fields": {}
"fields": {},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "reserved",
"named": true
}
]
}
},
{
"type": "attr_value",
@ -20,12 +30,12 @@
}
},
{
"type": "block",
"type": "attribute",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"required": true,
"types": [
{
"type": "attr_key",
@ -34,11 +44,18 @@
{
"type": "attr_value",
"named": true
},
{
"type": "block",
"named": true
},
}
]
}
},
{
"type": "block",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "connection",
"named": true
@ -47,25 +64,9 @@
"type": "container",
"named": true
},
{
"type": "container_key",
"named": true
},
{
"type": "dot",
"named": true
},
{
"type": "label",
"named": true
},
{
"type": "shape",
"named": true
},
{
"type": "shape_key",
"named": true
}
]
}
@ -151,33 +152,6 @@
]
}
},
{
"type": "invalid",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "attr_key",
"named": true
},
{
"type": "attr_value",
"named": true
},
{
"type": "block",
"named": true
},
{
"type": "dot",
"named": true
}
]
}
},
{
"type": "label",
"named": true,
@ -193,6 +167,11 @@
]
}
},
{
"type": "reserved",
"named": true,
"fields": {}
},
{
"type": "shape",
"named": true,
@ -239,6 +218,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "attribute",
"named": true
},
{
"type": "connection",
"named": true
@ -307,6 +290,10 @@
"type": "constraint",
"named": false
},
{
"type": "direction",
"named": false
},
{
"type": "dot",
"named": true

Binary file not shown.

View File

@ -0,0 +1,45 @@
================================================================================
Root attribute
================================================================================
direction: value
shape: oval
label: ten
constraint: utehu
icon: huell
opacity: 1
fill: red
stroke: red
stroke-width: 5
stroke-dash: 4
border-radius: 1
font-color: red
shadow: true
multiple: true
animated: true
link: https://to
near: abc
--------------------------------------------------------------------------------
(source_file
(attribute (attr_key) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
(attribute (attr_key (reserved)) (attr_value))
)

Binary file not shown.