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({
name: "d2",
word: ($) => $._word,
rules: {
// TODO: add the actual grammar rules
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: ($) =>
seq(
@ -33,6 +53,8 @@ module.exports = grammar({
identifier: ($) => $._identifier,
keyword: ($) => choice("direction"),
_identifier: ($) =>
prec.right(
seq(

View file

@ -1,5 +1,6 @@
{
"name": "d2",
"word": "_word",
"rules": {
"source_file": {
"type": "REPEAT",
@ -11,6 +12,10 @@
"_definition": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "attribute"
},
{
"type": "SYMBOL",
"name": "connection"
@ -31,6 +36,99 @@
{
"type": "STRING",
"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",
"name": "_identifier"
},
"keyword": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "direction"
}
]
},
"_identifier": {
"type": "PREC_RIGHT",
"value": 0,

View file

@ -4,6 +4,48 @@
"named": true,
"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",
"named": true,
@ -32,6 +74,11 @@
"named": true,
"fields": {}
},
{
"type": "keyword",
"named": true,
"fields": {}
},
{
"type": "label",
"named": true,
@ -78,6 +125,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "attribute",
"named": true
},
{
"type": "connection",
"named": true
@ -94,6 +145,10 @@
"named": true,
"fields": {}
},
{
"type": "\u0000",
"named": false
},
{
"type": "\n",
"named": false
@ -138,6 +193,10 @@
"type": "`",
"named": false
},
{
"type": "direction",
"named": false
},
{
"type": "dot",
"named": true

Binary file not shown.

View file

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