Restructure language grammar

This commit is contained in:
Jeff 2023-10-05 22:05:18 -04:00
parent e09bc20198
commit d0806c2694
10 changed files with 11344 additions and 14189 deletions

View File

@ -9,7 +9,6 @@ if true then "True"
(root
(item
(statement
(expression
(control_flow
(expression
(value
@ -17,8 +16,7 @@ if true then "True"
(statement
(expression
(value
(string)))))))))
(string))))))))
==================
If/Then Assignment
@ -31,11 +29,36 @@ x = if true then 1
(root
(item
(statement
(expression
(assignment
(identifier)
(expression
(identifier)))))
(item
(statement
(expression
(value
(boolean)))))
(item
(statement
(expression
(identifier))))
(item
(statement
(expression
(value
(integer))))))
==================
If/Else
==================
if false then "True" else "False"
---
(root
(item
(statement
(control_flow
(expression
(value
@ -43,4 +66,50 @@ x = if true then 1
(statement
(expression
(value
(integer))))))))))))
(string))))
(statement
(expression
(value
(string))))))))
==================
If/Else If
==================
if 1 == 1
then "math is fun"
else if 4 == 9
then "math is broken"
---
(root
(item
(statement
(control_flow
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))
(statement
(expression
(value
(string))))
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))
(statement
(expression
(value
(string))))))))

View File

@ -28,17 +28,15 @@ foobar = ['answer', 42]
(root
(item
(statement
(expression
(assignment
(identifier)
(statement
(expression
(value
(list
(value
(string))
(value
(integer)))))))))))
(integer)))))))))
==================
List Nesting
@ -64,3 +62,28 @@ List Nesting
(list
(value
(integer))))))))))))
==================
Optional Commas
==================
['answers' [42 [666]]]
---
(root
(item
(statement
(expression
(value
(list
(value
(string))
(value
(list
(value
(integer))
(value
(list
(value
(integer))))))))))))

View File

@ -23,7 +23,7 @@ Simple Equality
Complex Equality
==================
(1 + 1) == 2
1 + 1 + 1 != 3
---
@ -32,6 +32,8 @@ Complex Equality
(statement
(expression
(logic
(expression
(math
(expression
(math
(expression
@ -41,6 +43,10 @@ Complex Equality
(expression
(value
(integer)))))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value

View File

@ -36,22 +36,18 @@ y = "one"
(root
(item
(statement
(expression
(assignment
(identifier)
(statement
(expression
(value
(integer))))))))
(integer))))))
(item
(statement
(expression
(assignment
(identifier)
(statement
(expression
(value
(string)))))))))
(string)))))))
==================
Complex Assignment
@ -59,15 +55,13 @@ Complex Assignment
x = 1 + 1
---
--
(root
(item
(statement
(expression
(assignment
(identifier)
(statement
(expression
(math
(expression
@ -76,4 +70,4 @@ x = 1 + 1
(math_operator)
(expression
(value
(integer)))))))))))
(integer)))))))))

View File

@ -35,10 +35,8 @@ foobar = table <text, number> {
(root
(item
(statement
(expression
(assignment
(identifier)
(statement
(expression
(value
(table
@ -48,7 +46,7 @@ foobar = table <text, number> {
(value
(string))
(value
(integer))))))))))))
(integer))))))))))
==================
Table Access
@ -61,7 +59,6 @@ select number from foobar where text == 'answer'
(root
(item
(statement
(expression
(select
(identifier)
(identifier)
@ -72,7 +69,7 @@ select number from foobar where text == 'answer'
(logic_operator)
(expression
(value
(string))))))))))
(string)))))))))
==================
@ -86,11 +83,10 @@ insert ['bob was here', 0] into foobar
(root
(item
(statement
(expression
(insert
(list
(value
(string))
(value
(integer)))
(identifier))))))
(identifier)))))

View File

@ -41,31 +41,3 @@ Complex Yield
(identifier))
(expression
(identifier))))))
==================
Yield Assignment
==================
x = 1 + 1 -> to_string
---
(root
(item
(statement
(expression
(assignment
(identifier)
(statement
(yield
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(expression
(identifier)))))))))

View File

@ -11,14 +11,20 @@ module.exports = grammar({
$.statement,
),
comment: $ => seq(token('#'), /.*/),
comment: $ => seq('#', /.*/),
statement: $ => prec.right(choice(
statement: $ => prec.right(1, choice(
$.assignment,
$.expression,
$.control_flow,
$.yield,
$.insert,
$.select,
$.loop,
$.match,
)),
yield: $ => prec.left(
yield: $ => prec.right(2,
seq(
$.expression,
'->',
@ -27,23 +33,12 @@ module.exports = grammar({
)
),
expression: $ => choice(
$._expression_kind,
seq('(', $._expression_kind, ')')
),
_expression_kind: $ => prec.right(choice(
expression: $ => prec.right(choice(
$.value,
$.identifier,
$.control_flow,
$.select,
$.insert,
$.function_call,
$.assignment,
$.math,
$.logic,
$.loop,
$.match,
)),
identifier: $ => /[a-z|_|.]+[0-9]?/,
@ -59,10 +54,10 @@ module.exports = grammar({
$.map,
),
float: $ => /[-]*[0-9]*[.]{1}[0-9]+/,
integer: $ => /[-]*[0-9]+[.]{0}/,
float: $ => /[-]*[0-9]*[.]{1}[0-9]+/,
string: $ => /("[^"]*?")|('[^']*?')|(`[^`]*?`)/,
boolean: $ => choice(
@ -94,11 +89,11 @@ module.exports = grammar({
map: $ => seq(
'{',
repeat(seq($.identifier, "=", $.value)),
repeat(seq($.identifier, "=", $.value)), // TODO: Replace value with expression
'}',
),
math: $ => prec.right(seq(
math: $ => prec.left(seq(
$.expression,
$.math_operator,
$.expression,
@ -112,7 +107,7 @@ module.exports = grammar({
'%',
),
logic: $ => prec.right(seq(
logic: $ => prec.left(seq(
$.expression,
$.logic_operator,
$.expression,
@ -120,6 +115,7 @@ module.exports = grammar({
logic_operator: $ => choice(
'==',
'!=',
'&&',
'||',
'and',
@ -129,7 +125,7 @@ module.exports = grammar({
assignment: $ => prec.right(seq(
$.identifier,
choice("=", "+=", "-="),
$.statement,
$.expression,
)),
select: $ => prec.right(seq(
@ -154,14 +150,19 @@ module.exports = grammar({
control_flow: $ => prec.right(seq(
'if',
field('if_expression', $.expression),
$.expression,
'then',
field('then_statement', $.statement),
optional(
seq('else',
field('else_statement', $.statement),
),
),
$.statement,
prec.left(repeat(seq(
'else if',
$.expression,
'then',
$.statement,
))),
optional(seq(
'else',
$.statement,
)),
)),
function_call: $ => prec.right(seq(
@ -187,7 +188,9 @@ module.exports = grammar({
break_loop: $ => seq(
'loop',
'{',
$.statement,
repeat($.statement),
'break',
optional($.value),
'}',
),

View File

@ -26,11 +26,8 @@
"type": "SEQ",
"members": [
{
"type": "TOKEN",
"content": {
"type": "STRING",
"value": "#"
}
},
{
"type": "PATTERN",
@ -40,24 +37,48 @@
},
"statement": {
"type": "PREC_RIGHT",
"value": 0,
"value": 1,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "assignment"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "control_flow"
},
{
"type": "SYMBOL",
"name": "yield"
},
{
"type": "SYMBOL",
"name": "insert"
},
{
"type": "SYMBOL",
"name": "select"
},
{
"type": "SYMBOL",
"name": "loop"
},
{
"type": "SYMBOL",
"name": "match"
}
]
}
},
"yield": {
"type": "PREC_LEFT",
"value": 0,
"type": "PREC_RIGHT",
"value": 2,
"content": {
"type": "SEQ",
"members": [
@ -97,32 +118,6 @@
}
},
"expression": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression_kind"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SYMBOL",
"name": "_expression_kind"
},
{
"type": "STRING",
"value": ")"
}
]
}
]
},
"_expression_kind": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
@ -136,26 +131,10 @@
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "control_flow"
},
{
"type": "SYMBOL",
"name": "select"
},
{
"type": "SYMBOL",
"name": "insert"
},
{
"type": "SYMBOL",
"name": "function_call"
},
{
"type": "SYMBOL",
"name": "assignment"
},
{
"type": "SYMBOL",
"name": "math"
@ -163,14 +142,6 @@
{
"type": "SYMBOL",
"name": "logic"
},
{
"type": "SYMBOL",
"name": "loop"
},
{
"type": "SYMBOL",
"name": "match"
}
]
}
@ -216,14 +187,14 @@
}
]
},
"float": {
"type": "PATTERN",
"value": "[-]*[0-9]*[.]{1}[0-9]+"
},
"integer": {
"type": "PATTERN",
"value": "[-]*[0-9]+[.]{0}"
},
"float": {
"type": "PATTERN",
"value": "[-]*[0-9]*[.]{1}[0-9]+"
},
"string": {
"type": "PATTERN",
"value": "(\"[^\"]*?\")|('[^']*?')|(`[^`]*?`)"
@ -442,7 +413,7 @@
]
},
"math": {
"type": "PREC_RIGHT",
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
@ -488,7 +459,7 @@
]
},
"logic": {
"type": "PREC_RIGHT",
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
@ -515,6 +486,10 @@
"type": "STRING",
"value": "=="
},
{
"type": "STRING",
"value": "!="
},
{
"type": "STRING",
"value": "&&"
@ -562,7 +537,7 @@
},
{
"type": "SYMBOL",
"name": "statement"
"name": "expression"
}
]
}
@ -673,23 +648,43 @@
"value": "if"
},
{
"type": "FIELD",
"name": "if_expression",
"content": {
"type": "SYMBOL",
"name": "expression"
}
},
{
"type": "STRING",
"value": "then"
},
{
"type": "FIELD",
"name": "then_statement",
"content": {
"type": "SYMBOL",
"name": "statement"
},
{
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else if"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": "then"
},
{
"type": "SYMBOL",
"name": "statement"
}
]
}
}
},
{
@ -703,13 +698,9 @@
"value": "else"
},
{
"type": "FIELD",
"name": "else_statement",
"content": {
"type": "SYMBOL",
"name": "statement"
}
}
]
},
{
@ -803,8 +794,27 @@
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "statement"
}
},
{
"type": "STRING",
"value": "break"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "value"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",

View File

@ -8,11 +8,11 @@
"required": true,
"types": [
{
"type": "identifier",
"type": "expression",
"named": true
},
{
"type": "statement",
"type": "identifier",
"named": true
}
]
@ -28,12 +28,16 @@
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"multiple": true,
"required": false,
"types": [
{
"type": "statement",
"named": true
},
{
"type": "value",
"named": true
}
]
}
@ -46,38 +50,21 @@
{
"type": "control_flow",
"named": true,
"fields": {
"else_statement": {
"multiple": false,
"required": false,
"types": [
{
"type": "statement",
"named": true
}
]
},
"if_expression": {
"multiple": false,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
},
"then_statement": {
"multiple": false,
"required": true,
"types": [
{
"type": "statement",
"named": true
}
]
}
}
},
{
"type": "expression",
@ -87,14 +74,6 @@
"multiple": false,
"required": true,
"types": [
{
"type": "assignment",
"named": true
},
{
"type": "control_flow",
"named": true
},
{
"type": "function_call",
"named": true
@ -103,30 +82,14 @@
"type": "identifier",
"named": true
},
{
"type": "insert",
"named": true
},
{
"type": "logic",
"named": true
},
{
"type": "loop",
"named": true
},
{
"type": "match",
"named": true
},
{
"type": "math",
"named": true
},
{
"type": "select",
"named": true
},
{
"type": "value",
"named": true
@ -376,10 +339,34 @@
"multiple": false,
"required": true,
"types": [
{
"type": "assignment",
"named": true
},
{
"type": "control_flow",
"named": true
},
{
"type": "expression",
"named": true
},
{
"type": "insert",
"named": true
},
{
"type": "loop",
"named": true
},
{
"type": "match",
"named": true
},
{
"type": "select",
"named": true
},
{
"type": "yield",
"named": true
@ -483,6 +470,10 @@
]
}
},
{
"type": "!=",
"named": false
},
{
"type": "#",
"named": false
@ -495,14 +486,6 @@
"type": "&&",
"named": false
},
{
"type": "(",
"named": false
},
{
"type": ")",
"named": false
},
{
"type": "*",
"named": false
@ -567,10 +550,18 @@
"type": "and",
"named": false
},
{
"type": "break",
"named": false
},
{
"type": "else",
"named": false
},
{
"type": "else if",
"named": false
},
{
"type": "false",
"named": false

24863
src/parser.c

File diff suppressed because it is too large Load Diff