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

View File

@ -82,6 +82,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "control_flow" "name": "control_flow"
},
{
"type": "SYMBOL",
"name": "tool"
} }
] ]
}, },
@ -119,6 +123,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "function" "name": "function"
},
{
"type": "SYMBOL",
"name": "table"
} }
] ]
}, },
@ -134,10 +142,6 @@
"type": "PATTERN", "type": "PATTERN",
"value": "(\"|'|`)(.*?)(\"|'|`)" "value": "(\"|'|`)(.*?)(\"|'|`)"
}, },
"function": {
"type": "PATTERN",
"value": "{(.*?)}"
},
"empty": { "empty": {
"type": "STRING", "type": "STRING",
"value": "()" "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": { "operator": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -280,20 +422,12 @@
] ]
} }
}, },
"_keywords": { "tool": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "STRING", "type": "STRING",
"value": "if" "value": "output"
},
{
"type": "STRING",
"value": "then"
},
{
"type": "STRING",
"value": "else"
} }
] ]
} }

View File

@ -48,6 +48,10 @@
"type": "operation", "type": "operation",
"named": true "named": true
}, },
{
"type": "tool",
"named": true
},
{ {
"type": "value", "type": "value",
"named": true "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", "type": "item",
"named": true, "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", "type": "value",
"named": true, "named": true,
@ -193,6 +240,10 @@
{ {
"type": "string", "type": "string",
"named": true "named": true
},
{
"type": "table",
"named": true
} }
] ]
} }
@ -221,6 +272,10 @@
"type": "-", "type": "-",
"named": false "named": false
}, },
{
"type": "<",
"named": false
},
{ {
"type": "=", "type": "=",
"named": false "named": false
@ -229,6 +284,10 @@
"type": "==", "type": "==",
"named": false "named": false
}, },
{
"type": ">",
"named": false
},
{ {
"type": "else", "type": "else",
"named": false "named": false
@ -247,7 +306,7 @@
}, },
{ {
"type": "function", "type": "function",
"named": true "named": false
}, },
{ {
"type": "identifier", "type": "identifier",
@ -261,10 +320,18 @@
"type": "integer", "type": "integer",
"named": true "named": true
}, },
{
"type": "output",
"named": false
},
{ {
"type": "string", "type": "string",
"named": true "named": true
}, },
{
"type": "table",
"named": false
},
{ {
"type": "then", "type": "then",
"named": false "named": false
@ -272,5 +339,13 @@
{ {
"type": "true", "type": "true",
"named": false "named": false
},
{
"type": "{",
"named": false
},
{
"type": "}",
"named": false
} }
] ]

File diff suppressed because it is too large Load Diff