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 (root
(item (item
(statement (statement
(expression
(control_flow (control_flow
(expression (expression
(value (value
@ -17,8 +16,7 @@ if true then "True"
(statement (statement
(expression (expression
(value (value
(string))))))))) (string))))))))
================== ==================
If/Then Assignment If/Then Assignment
@ -31,11 +29,36 @@ x = if true then 1
(root (root
(item (item
(statement (statement
(expression
(assignment (assignment
(identifier) (identifier)
(expression
(identifier)))))
(item
(statement (statement
(expression (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 (control_flow
(expression (expression
(value (value
@ -43,4 +66,50 @@ x = if true then 1
(statement (statement
(expression (expression
(value (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 (root
(item (item
(statement (statement
(expression
(assignment (assignment
(identifier) (identifier)
(statement
(expression (expression
(value (value
(list (list
(value (value
(string)) (string))
(value (value
(integer))))))))))) (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
--- ---
@ -32,6 +32,8 @@ Complex Equality
(statement (statement
(expression (expression
(logic (logic
(expression
(math
(expression (expression
(math (math
(expression (expression
@ -41,6 +43,10 @@ Complex Equality
(expression (expression
(value (value
(integer))))) (integer)))))
(math_operator)
(expression
(value
(integer)))))
(logic_operator) (logic_operator)
(expression (expression
(value (value

View File

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

View File

@ -35,10 +35,8 @@ foobar = table <text, number> {
(root (root
(item (item
(statement (statement
(expression
(assignment (assignment
(identifier) (identifier)
(statement
(expression (expression
(value (value
(table (table
@ -48,7 +46,7 @@ foobar = table <text, number> {
(value (value
(string)) (string))
(value (value
(integer)))))))))))) (integer))))))))))
================== ==================
Table Access Table Access
@ -61,7 +59,6 @@ select number from foobar where text == 'answer'
(root (root
(item (item
(statement (statement
(expression
(select (select
(identifier) (identifier)
(identifier) (identifier)
@ -72,7 +69,7 @@ select number from foobar where text == 'answer'
(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

@ -11,14 +11,20 @@ module.exports = grammar({
$.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",
"content": {
"type": "STRING", "type": "STRING",
"value": "#" "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",
"name": "if_expression",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "expression" "name": "expression"
}
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "then" "value": "then"
}, },
{ {
"type": "FIELD",
"name": "then_statement",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "statement" "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" "value": "else"
}, },
{ {
"type": "FIELD",
"name": "else_statement",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "statement" "name": "statement"
} }
}
] ]
}, },
{ {
@ -803,8 +794,27 @@
"value": "{" "value": "{"
}, },
{ {
"type": "REPEAT",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "statement" "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,38 +50,21 @@
{ {
"type": "control_flow", "type": "control_flow",
"named": true, "named": true,
"fields": { "fields": {},
"else_statement": { "children": {
"multiple": false, "multiple": true,
"required": false,
"types": [
{
"type": "statement",
"named": true
}
]
},
"if_expression": {
"multiple": false,
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "expression", "type": "expression",
"named": true "named": true
}
]
}, },
"then_statement": {
"multiple": false,
"required": true,
"types": [
{ {
"type": "statement", "type": "statement",
"named": true "named": true
} }
] ]
} }
}
}, },
{ {
"type": "expression", "type": "expression",
@ -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