grammar: add support of near attribute

grammar: add more utils

Closes #2
This commit is contained in:
Dmitriy Pleshevskiy 2023-06-16 06:37:19 +03:00
parent d113cea211
commit 8aac147e8e
Signed by: pleshevskiy
GPG Key ID: 79C4487B44403985
6 changed files with 233 additions and 98 deletions

View File

@ -1,7 +1,12 @@
# Actors # Actors
hans: Hans Niemann hans: Hans Niemann
defendants: { defendants: |md
# Defendants
hello world
| {
shape: text
mc: Magnus Carlsen mc: Magnus Carlsen
playmagnus: Play Magnus Group playmagnus: Play Magnus Group
chesscom: Chess.com chesscom: Chess.com

View File

@ -1,15 +1,3 @@
// mkWrapCont :: string -> string -> ($ -> Rule) -> $ -> Rule
const mkWrapCont = (start, end) => (getRule) => ($) =>
seq(
start,
repeat(choice($._eol, seq(getRule($), $._end))),
optional(seq(getRule($), optional($._end))),
end
);
const mkBlock = mkWrapCont("{", "}");
const mkList = mkWrapCont("[", "]");
const PREC = { const PREC = {
COMMENT: -2, COMMENT: -2,
EOL: -1, EOL: -1,
@ -25,6 +13,28 @@ const PREC = {
ATTRIBUTE_KEY: 0, ATTRIBUTE_KEY: 0,
}; };
// mkWrapCont :: string -> string -> ($ -> Rule) -> $ -> Rule
const mkWrapCont = (start, end) => (onDefinition) => ($) =>
seq(
start,
repeat(choice($._eol, seq(onDefinition($), $._end))),
optional(seq(onDefinition($), optional($._end))),
end
);
const mkBlock = mkWrapCont("{", "}");
const mkList = mkWrapCont("[", "]");
// mkAlias :: ($ -> Rule) -> ($ -> Rule) -> $ -> Rule
const mkAlias = (onAlias) => (onValue) => ($) => alias(onValue($), onAlias($));
const attrKeyAlias = mkAlias(($) => $.attr_key);
const attrAlias = mkAlias(($) => $.attribute);
// mkAttrCont :: ($ -> Rule) -> ($ -> Rule) -> $ -> Rule
const mkAttrCont = (onValue) => (onKey) => ($) =>
seq(onKey($), $._colon, onValue($));
const mkBaseAttr = (onKey) =>
mkAttrCont(($) => $.attr_value)(attrKeyAlias(onKey));
module.exports = grammar({ module.exports = grammar({
name: "d2", name: "d2",
@ -49,6 +59,7 @@ module.exports = grammar({
[$._classes_block], [$._classes_block],
[$._classes_item_block], [$._classes_item_block],
[$.class_list], [$.class_list],
[$._text_block_attrs],
], ],
rules: { rules: {
@ -126,11 +137,9 @@ module.exports = grammar({
_classes_item_block: mkBlock(($) => $._classes_item_attribute), _classes_item_block: mkBlock(($) => $._classes_item_attribute),
_classes_item_attribute: ($) => _classes_item_attribute: attrAlias(($) =>
choice( choice($._base_shape_attribute, $._style_attribute)
alias($._base_shape_attribute, $.attribute), ),
alias($._style_attribute, $.attribute)
),
// containers // containers
@ -164,7 +173,16 @@ module.exports = grammar({
optional( optional(
choice( choice(
seq($.dot, $._shape_attribute), seq($.dot, $._shape_attribute),
seq($._colon, choice($.label, $.text_block)) seq(
$._colon,
choice(
$.label,
seq(
$.text_block,
optional(alias($._text_block_attrs, $.block))
)
)
)
) )
) )
) )
@ -194,12 +212,13 @@ module.exports = grammar({
alias($._text_block_end, "|") alias($._text_block_end, "|")
), ),
_text_block_attrs: mkBlock(attrAlias(($) => $._text_shape_attribute)),
language: ($) => /\w+/, language: ($) => /\w+/,
// attributes // attributes
_root_attribute: ($) => _root_attribute: mkBaseAttr(($) => $._root_attr_key),
seq(alias($._root_attr_key, $.attr_key), $._colon, $.attr_value),
_root_attr_key: ($) => _root_attr_key: ($) =>
choice( choice(
@ -220,21 +239,19 @@ module.exports = grammar({
) )
), ),
_shape_attribute: ($) => _shape_attribute: attrAlias(($) =>
alias( choice($._class_attribute, $._base_shape_attribute, $._style_attribute)
choice($._class_attribute, $._base_shape_attribute, $._style_attribute), ),
$.attribute
),
_class_attribute: ($) => _class_attribute: mkAttrCont(($) => choice($.class_list, $._class_name))(
seq($.keyword_class, $._colon, choice($.class_list, $._class_name)), ($) => $.keyword_class
),
class_list: mkList(($) => $._class_name), class_list: mkList(($) => $._class_name),
_class_name: ($) => alias($.shape_key, $.class_name), _class_name: ($) => alias($.shape_key, $.class_name),
_base_shape_attribute: ($) => _base_shape_attribute: mkBaseAttr(($) => $._shape_attr_key),
seq(alias($._shape_attr_key, $.attr_key), $._colon, $.attr_value),
_shape_attr_key: ($) => _shape_attr_key: ($) =>
prec( prec(
@ -251,6 +268,7 @@ module.exports = grammar({
"icon", "icon",
"width", "width",
"height", "height",
$._text_attr_key,
$._grid_attr_key $._grid_attr_key
) )
), ),
@ -267,15 +285,9 @@ module.exports = grammar({
) )
), ),
_style_attribute_block: mkBlock(($) => _style_attribute_block: mkBlock(attrAlias(($) => $._inner_style_attribute)),
alias($._inner_style_attribute, $.attribute)
),
_inner_style_attribute: ($) => _inner_style_attribute: mkBaseAttr(($) => $._style_attr_key),
prec(
PREC.ATTRIBUTE,
seq(alias($._style_attr_key, $.attr_key), $._colon, $.attr_value)
),
_grid_attr_key: ($) => _grid_attr_key: ($) =>
choice( choice(
@ -311,13 +323,13 @@ module.exports = grammar({
"text-transform" "text-transform"
), ),
_text_shape_attribute: mkBaseAttr(($) => $._text_attr_key),
_text_attr_key: ($) => "near", _text_attr_key: ($) => "near",
_connection_attribute: ($) => _connection_attribute: attrAlias(($) =>
choice( choice($._connection_arrowhead_attribute, $._style_attribute)
alias($._connection_arrowhead_attribute, $.attribute), ),
alias($._style_attribute, $.attribute)
),
_connection_arrowhead_attribute: ($) => _connection_arrowhead_attribute: ($) =>
seq( seq(

View File

@ -566,27 +566,22 @@
] ]
}, },
"_classes_item_attribute": { "_classes_item_attribute": {
"type": "CHOICE", "type": "ALIAS",
"members": [ "content": {
{ "type": "CHOICE",
"type": "ALIAS", "members": [
"content": { {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_base_shape_attribute" "name": "_base_shape_attribute"
}, },
"named": true, {
"value": "attribute"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_style_attribute" "name": "_style_attribute"
}, }
"named": true, ]
"value": "attribute" },
} "named": true,
] "value": "attribute"
}, },
"container": { "container": {
"type": "PREC", "type": "PREC",
@ -816,8 +811,30 @@
"name": "label" "name": "label"
}, },
{ {
"type": "SYMBOL", "type": "SEQ",
"name": "text_block" "members": [
{
"type": "SYMBOL",
"name": "text_block"
},
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_text_block_attrs"
},
"named": true,
"value": "block"
},
{
"type": "BLANK"
}
]
}
]
} }
] ]
} }
@ -959,6 +976,83 @@
} }
] ]
}, },
"_text_block_attrs": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_eol"
},
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_text_shape_attribute"
},
"named": true,
"value": "attribute"
},
{
"type": "SYMBOL",
"name": "_end"
}
]
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_text_shape_attribute"
},
"named": true,
"value": "attribute"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_end"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "}"
}
]
},
"language": { "language": {
"type": "PATTERN", "type": "PATTERN",
"value": "\\w+" "value": "\\w+"
@ -1224,6 +1318,10 @@
"type": "STRING", "type": "STRING",
"value": "height" "value": "height"
}, },
{
"type": "SYMBOL",
"name": "_text_attr_key"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_grid_attr_key" "name": "_grid_attr_key"
@ -1363,30 +1461,26 @@
] ]
}, },
"_inner_style_attribute": { "_inner_style_attribute": {
"type": "PREC", "type": "SEQ",
"value": 0, "members": [
"content": { {
"type": "SEQ", "type": "ALIAS",
"members": [ "content": {
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_style_attr_key"
},
"named": true,
"value": "attr_key"
},
{
"type": "SYMBOL", "type": "SYMBOL",
"name": "_colon" "name": "_style_attr_key"
}, },
{ "named": true,
"type": "SYMBOL", "value": "attr_key"
"name": "attr_value" },
} {
] "type": "SYMBOL",
} "name": "_colon"
},
{
"type": "SYMBOL",
"name": "attr_value"
}
]
}, },
"_grid_attr_key": { "_grid_attr_key": {
"type": "CHOICE", "type": "CHOICE",
@ -1507,33 +1601,50 @@
} }
] ]
}, },
"_text_attr_key": { "_text_shape_attribute": {
"type": "STRING", "type": "SEQ",
"value": "near"
},
"_connection_attribute": {
"type": "CHOICE",
"members": [ "members": [
{ {
"type": "ALIAS", "type": "ALIAS",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_connection_arrowhead_attribute" "name": "_text_attr_key"
}, },
"named": true, "named": true,
"value": "attribute" "value": "attr_key"
}, },
{ {
"type": "ALIAS", "type": "SYMBOL",
"content": { "name": "_colon"
"type": "SYMBOL", },
"name": "_style_attribute" {
}, "type": "SYMBOL",
"named": true, "name": "attr_value"
"value": "attribute"
} }
] ]
}, },
"_text_attr_key": {
"type": "STRING",
"value": "near"
},
"_connection_attribute": {
"type": "ALIAS",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_connection_arrowhead_attribute"
},
{
"type": "SYMBOL",
"name": "_style_attribute"
}
]
},
"named": true,
"value": "attribute"
},
"_connection_arrowhead_attribute": { "_connection_arrowhead_attribute": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -2055,6 +2166,9 @@
], ],
[ [
"class_list" "class_list"
],
[
"_text_block_attrs"
] ]
], ],
"precedences": [], "precedences": [],

View File

@ -357,6 +357,10 @@
"type": "attribute", "type": "attribute",
"named": true "named": true
}, },
{
"type": "block",
"named": true
},
{ {
"type": "dot", "type": "dot",
"named": true "named": true

Binary file not shown.

Binary file not shown.