add basic keywords

This commit is contained in:
Dmitriy Pleshevskiy 2022-12-04 18:35:50 +03:00
parent fc5a0fff47
commit 949e58bb56
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
5 changed files with 202 additions and 2 deletions

View file

@ -1,13 +1,33 @@
module.exports = grammar({ module.exports = grammar({
name: "d2", name: "d2",
word: ($) => $._word,
rules: { rules: {
// TODO: add the actual grammar rules // TODO: add the actual grammar rules
source_file: ($) => repeat($._definition), source_file: ($) => repeat($._definition),
_definition: ($) => choice($.connection, $.shape), _definition: ($) => choice($.attribute, $.connection, $.shape),
_end: ($) => choice(";", "\n"), _end: ($) => choice(";", "\n", "\0"),
attribute: ($) =>
choice(
seq(
$.identifier,
repeat1(seq($.dot, $.keyword)),
":",
alias($.label, $.attribute_value),
$._end
),
seq(
$.keyword,
repeat(seq($.dot, $.keyword)),
":",
alias($.label, $.attribute_value),
$._end
)
),
connection: ($) => connection: ($) =>
seq( seq(
@ -33,6 +53,8 @@ module.exports = grammar({
identifier: ($) => $._identifier, identifier: ($) => $._identifier,
keyword: ($) => choice("direction"),
_identifier: ($) => _identifier: ($) =>
prec.right( prec.right(
seq( seq(

View file

@ -1,5 +1,6 @@
{ {
"name": "d2", "name": "d2",
"word": "_word",
"rules": { "rules": {
"source_file": { "source_file": {
"type": "REPEAT", "type": "REPEAT",
@ -11,6 +12,10 @@
"_definition": { "_definition": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "attribute"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "connection" "name": "connection"
@ -31,6 +36,99 @@
{ {
"type": "STRING", "type": "STRING",
"value": "\n" "value": "\n"
},
{
"type": "STRING",
"value": "\u0000"
}
]
},
"attribute": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "dot"
},
{
"type": "SYMBOL",
"name": "keyword"
}
]
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "label"
},
"named": true,
"value": "attribute_value"
},
{
"type": "SYMBOL",
"name": "_end"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "dot"
},
{
"type": "SYMBOL",
"name": "keyword"
}
]
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "label"
},
"named": true,
"value": "attribute_value"
},
{
"type": "SYMBOL",
"name": "_end"
}
]
} }
] ]
}, },
@ -170,6 +268,15 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "_identifier" "name": "_identifier"
}, },
"keyword": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "direction"
}
]
},
"_identifier": { "_identifier": {
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
"value": 0, "value": 0,

View file

@ -4,6 +4,48 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "attribute",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "attribute_value",
"named": true
},
{
"type": "dot",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "keyword",
"named": true
}
]
}
},
{
"type": "attribute_value",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "string",
"named": true
}
]
}
},
{ {
"type": "connection", "type": "connection",
"named": true, "named": true,
@ -32,6 +74,11 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "keyword",
"named": true,
"fields": {}
},
{ {
"type": "label", "type": "label",
"named": true, "named": true,
@ -78,6 +125,10 @@
"multiple": true, "multiple": true,
"required": false, "required": false,
"types": [ "types": [
{
"type": "attribute",
"named": true
},
{ {
"type": "connection", "type": "connection",
"named": true "named": true
@ -94,6 +145,10 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "\u0000",
"named": false
},
{ {
"type": "\n", "type": "\n",
"named": false "named": false
@ -138,6 +193,10 @@
"type": "`", "type": "`",
"named": false "named": false
}, },
{
"type": "direction",
"named": false
},
{ {
"type": "dot", "type": "dot",
"named": true "named": true

Binary file not shown.

View file

@ -0,0 +1,12 @@
==================
Keywords
==================
direction: value
---
(source_file
(attribute (keyword) (attribute_value))
)