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 (root
(item (item
(statement (statement
(expression (control_flow
(control_flow (expression
(value
(boolean)))
(statement
(expression (expression
(value (value
(boolean))) (string))))))))
(statement
(expression
(value
(string)))))))))
================== ==================
If/Then Assignment If/Then Assignment
@ -29,18 +27,89 @@ x = if true then 1
--- ---
(root (root
(item
(statement
(assignment
(identifier)
(expression
(identifier)))))
(item (item
(statement (statement
(expression (expression
(assignment (value
(identifier) (boolean)))))
(statement (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 (expression
(control_flow (value
(expression (integer)))
(value (logic_operator)
(boolean))) (expression
(statement (value
(expression (integer)))))
(value (statement
(integer)))))))))))) (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 (root
(item (item
(statement (statement
(expression (assignment
(assignment (identifier)
(identifier) (expression
(statement (value
(expression (list
(value (value
(list (string))
(value (value
(string)) (integer)))))))))
(value
(integer)))))))))))
================== ==================
List Nesting List Nesting
@ -64,3 +62,28 @@ List Nesting
(list (list
(value (value
(integer)))))))))))) (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 Complex Equality
================== ==================
(1 + 1) == 2 1 + 1 + 1 != 3
--- ---
@ -35,8 +35,14 @@ Complex Equality
(expression (expression
(math (math
(expression (expression
(value (math
(integer))) (expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(math_operator) (math_operator)
(expression (expression
(value (value

View File

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

View File

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

View File

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

View File

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

View File

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

24863
src/parser.c

File diff suppressed because it is too large Load Diff