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,16 +9,14 @@ if true then "True"
(root
(item
(statement
(expression
(control_flow
(control_flow
(expression
(value
(boolean)))
(statement
(expression
(value
(boolean)))
(statement
(expression
(value
(string)))))))))
(string))))))))
==================
If/Then Assignment
@ -29,18 +27,89 @@ x = if true then 1
---
(root
(item
(statement
(assignment
(identifier)
(expression
(identifier)))))
(item
(statement
(expression
(assignment
(identifier)
(statement
(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
(boolean)))
(statement
(expression
(value
(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
(control_flow
(expression
(value
(boolean)))
(statement
(expression
(value
(integer))))))))))))
(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
(assignment
(identifier)
(expression
(value
(list
(value
(list
(value
(string))
(value
(integer)))))))))))
(string))
(value
(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
---
@ -35,8 +35,14 @@ Complex Equality
(expression
(math
(expression
(value
(integer)))
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(math_operator)
(expression
(value

View File

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

View File

@ -35,21 +35,19 @@ foobar = table <text, number> {
(root
(item
(statement
(expression
(assignment
(identifier)
(statement
(expression
(value
(table
(identifier)
(identifier)
(list
(value
(string))
(value
(integer))))))))))))
(assignment
(identifier)
(expression
(value
(table
(identifier)
(identifier)
(list
(value
(string))
(value
(integer))))))))))
==================
Table Access
==================
@ -61,18 +59,17 @@ select number from foobar where text == 'answer'
(root
(item
(statement
(expression
(select
(identifier)
(identifier)
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(string))))))))))
(select
(identifier)
(identifier)
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(string)))))))))
==================
@ -86,11 +83,10 @@ insert ['bob was here', 0] into foobar
(root
(item
(statement
(expression
(insert
(list
(value
(string))
(value
(integer)))
(identifier))))))
(insert
(list
(value
(string))
(value
(integer)))
(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

@ -5,20 +5,26 @@ module.exports = grammar({
rules: {
root: $ => repeat1($.item),
item: $ => choice(
$.comment,
$.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": "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": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": "then"
},
{
"type": "FIELD",
"name": "then_statement",
"type": "SYMBOL",
"name": "statement"
},
{
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SYMBOL",
"name": "statement"
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else if"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": "then"
},
{
"type": "SYMBOL",
"name": "statement"
}
]
}
}
},
{
@ -703,12 +698,8 @@
"value": "else"
},
{
"type": "FIELD",
"name": "else_statement",
"content": {
"type": "SYMBOL",
"name": "statement"
}
"type": "SYMBOL",
"name": "statement"
}
]
},
@ -803,8 +794,27 @@
"value": "{"
},
{
"type": "SYMBOL",
"name": "statement"
"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,37 +50,20 @@
{
"type": "control_flow",
"named": true,
"fields": {
"else_statement": {
"multiple": false,
"required": false,
"types": [
{
"type": "statement",
"named": true
}
]
},
"if_expression": {
"multiple": false,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
},
"then_statement": {
"multiple": false,
"required": true,
"types": [
{
"type": "statement",
"named": true
}
]
}
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "statement",
"named": true
}
]
}
},
{
@ -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