Add syntax for functions and tables

This commit is contained in:
Jeff 2023-09-29 14:32:18 -04:00
parent 9aef6712fe
commit 0fa436832d
6 changed files with 1953 additions and 667 deletions

62
corpus/functions.txt Normal file
View File

@ -0,0 +1,62 @@
==================
Simple Function
==================
function { output "Hiya" }
---
(root
(item
(statement
(open_statement
(expression
(value
(function
(statement
(open_statement
(expression
(tool))))
(statement
(open_statement
(expression
(value
(string))))))))))))
==================
Complex Function
==================
function <message, number> {
output message
output number
}
---
(root
(item
(statement
(open_statement
(expression
(value
(function
(identifier)
(identifier)
(statement
(open_statement
(expression
(tool))))
(statement
(open_statement
(expression
(identifier))))
(statement
(open_statement
(expression
(tool))))
(statement
(open_statement
(expression
(identifier)))))))))))

24
corpus/tables.txt Normal file
View File

@ -0,0 +1,24 @@
==================
Table Declarations
==================
table <text, number> {
("answer", 42)
}
---
(root
(item
(statement
(open_statement
(expression
(value
(table
(identifier)
(identifier)
(list
(value
(string))
(value
(integer))))))))))

View File

@ -24,6 +24,7 @@ module.exports = grammar({
$.identifier,
$.operation,
$.control_flow,
$.tool,
),
identifier: $ => /[a-z|_|.]+/,
@ -36,6 +37,7 @@ module.exports = grammar({
$.empty,
$.boolean,
$.function,
$.table,
),
float: $ => /\d+\.\d*/,
@ -44,8 +46,6 @@ module.exports = grammar({
string: $ => /("|'|`)(.*?)("|'|`)/,
function: $ => /{(.*?)}/,
empty: $ => '()',
boolean: $ => choice(
@ -59,6 +59,22 @@ module.exports = grammar({
')'
),
function: $ => seq(
'function',
optional(seq('<', repeat(seq($.identifier, optional(','))), '>')),
'{',
repeat($.statement),
'}',
),
table: $ => seq(
'table',
seq('<', repeat(seq($.identifier, optional(','))), '>'),
'{',
optional(repeat($.list)),
'}',
),
operator: $ => choice(
'+',
'-',
@ -80,10 +96,8 @@ module.exports = grammar({
optional(seq('else', $.statement))
)),
_keywords: $ => choice(
'if',
'then',
'else',
tool: $ => choice(
"output",
),
}
});

View File

@ -82,6 +82,10 @@
{
"type": "SYMBOL",
"name": "control_flow"
},
{
"type": "SYMBOL",
"name": "tool"
}
]
},
@ -119,6 +123,10 @@
{
"type": "SYMBOL",
"name": "function"
},
{
"type": "SYMBOL",
"name": "table"
}
]
},
@ -134,10 +142,6 @@
"type": "PATTERN",
"value": "(\"|'|`)(.*?)(\"|'|`)"
},
"function": {
"type": "PATTERN",
"value": "{(.*?)}"
},
"empty": {
"type": "STRING",
"value": "()"
@ -192,6 +196,144 @@
}
]
},
"function": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "function"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ">"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "statement"
}
},
{
"type": "STRING",
"value": "}"
}
]
},
"table": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "table"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ">"
}
]
},
{
"type": "STRING",
"value": "{"
},
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "list"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "}"
}
]
},
"operator": {
"type": "CHOICE",
"members": [
@ -280,20 +422,12 @@
]
}
},
"_keywords": {
"tool": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "if"
},
{
"type": "STRING",
"value": "then"
},
{
"type": "STRING",
"value": "else"
"value": "output"
}
]
}

View File

@ -48,6 +48,10 @@
"type": "operation",
"named": true
},
{
"type": "tool",
"named": true
},
{
"type": "value",
"named": true
@ -55,6 +59,25 @@
]
}
},
{
"type": "function",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "statement",
"named": true
}
]
}
},
{
"type": "item",
"named": true,
@ -158,6 +181,30 @@
]
}
},
{
"type": "table",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "list",
"named": true
}
]
}
},
{
"type": "tool",
"named": true,
"fields": {}
},
{
"type": "value",
"named": true,
@ -193,6 +240,10 @@
{
"type": "string",
"named": true
},
{
"type": "table",
"named": true
}
]
}
@ -221,6 +272,10 @@
"type": "-",
"named": false
},
{
"type": "<",
"named": false
},
{
"type": "=",
"named": false
@ -229,6 +284,10 @@
"type": "==",
"named": false
},
{
"type": ">",
"named": false
},
{
"type": "else",
"named": false
@ -247,7 +306,7 @@
},
{
"type": "function",
"named": true
"named": false
},
{
"type": "identifier",
@ -261,10 +320,18 @@
"type": "integer",
"named": true
},
{
"type": "output",
"named": false
},
{
"type": "string",
"named": true
},
{
"type": "table",
"named": false
},
{
"type": "then",
"named": false
@ -272,5 +339,13 @@
{
"type": "true",
"named": false
},
{
"type": "{",
"named": false
},
{
"type": "}",
"named": false
}
]

File diff suppressed because it is too large Load Diff