Continue syntax overhaul

This commit is contained in:
Jeff 2023-10-06 06:18:02 -04:00
parent 4ddc656455
commit dbe7991fc6
10 changed files with 7727 additions and 10966 deletions

View File

@ -1,5 +1,5 @@
================== ==================
If/Then If
================== ==================
if true then "True" if true then "True"
@ -9,7 +9,7 @@ if true then "True"
(root (root
(item (item
(statement (statement
(control_flow (if_else
(expression (expression
(value (value
(boolean))) (boolean)))
@ -19,7 +19,7 @@ if true then "True"
(string)))))))) (string))))))))
================== ==================
If/Then Assignment If Assignment
================== ==================
x = if true then 1 x = if true then 1
@ -31,25 +31,18 @@ x = if true then 1
(statement (statement
(assignment (assignment
(identifier) (identifier)
(expression (statement
(identifier))))) (if_else
(item (expression
(statement (value
(expression (boolean)))
(value (statement
(boolean))))) (expression
(item (value
(statement (integer))))))))))
(expression
(identifier))))
(item
(statement
(expression
(value
(integer))))))
================== ==================
If/Else If Else
================== ==================
if false then "True" else "False" if false then "True" else "False"
@ -59,7 +52,7 @@ if false then "True" else "False"
(root (root
(item (item
(statement (statement
(control_flow (if_else
(expression (expression
(value (value
(boolean))) (boolean)))
@ -73,7 +66,7 @@ if false then "True" else "False"
(string)))))))) (string))))))))
================== ==================
If/Else If If Else If
================== ==================
if 1 == 1 if 1 == 1
@ -86,7 +79,7 @@ else if 4 == 9
(root (root
(item (item
(statement (statement
(control_flow (if_else
(expression (expression
(logic (logic
(expression (expression
@ -113,3 +106,60 @@ else if 4 == 9
(expression (expression
(value (value
(string)))))))) (string))))))))
==================
If Else Else If Else
==================
if false
then "no"
else if false
then "no"
else if 1 + 1 == 9
then "not the answer"
else "42"
---
(root
(item
(statement
(if_else
(expression
(value
(boolean)))
(statement
(expression
(value
(string))))
(expression
(value
(boolean)))
(statement
(expression
(value
(string))))
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(integer)))))
(statement
(expression
(value
(string))))
(statement
(expression
(value
(string))))))))

View File

@ -2,7 +2,7 @@
Simple Function Simple Function
================== ==================
function <> { "Hiya" } function { "Hiya" }
--- ---
@ -22,7 +22,7 @@ function <> { "Hiya" }
Function Call Function Call
================== ==================
foobar {"Hiya"} (foobar "Hiya")
--- ---
@ -40,7 +40,7 @@ foobar {"Hiya"}
Complex Function Complex Function
================== ==================
function <message, number> { function <message number> {
output message output message
output number output number
} }

View File

@ -30,13 +30,14 @@ foobar = ['answer', 42]
(statement (statement
(assignment (assignment
(identifier) (identifier)
(expression (statement
(value (expression
(list (value
(value (list
(string)) (value
(value (string))
(integer))))))))) (value
(integer))))))))))
================== ==================
List Nesting List Nesting

34
corpus/match.txt Normal file
View File

@ -0,0 +1,34 @@
==================
Match
==================
match 21 + 21
42 => true
catch false
---
(root
(item
(statement
(match
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(expression
(value
(integer)))
(statement
(expression
(value
(boolean))))
(statement
(expression
(value
(boolean))))))))

View File

@ -35,19 +35,21 @@ y = "one"
(root (root
(item (item
(statement (statement
(assignment (assignment
(identifier) (identifier)
(expression (statement
(value (expression
(integer)))))) (value
(integer)))))))
(item (item
(statement (statement
(assignment (assignment
(identifier) (identifier)
(expression (statement
(value (expression
(string))))))) (value
(string))))))))
================== ==================
Complex Assignment Complex Assignment

View File

@ -37,16 +37,17 @@ foobar = table <text, number> {
(statement (statement
(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

View File

@ -13,33 +13,31 @@ module.exports = grammar({
comment: $ => seq('#', /.*/), comment: $ => seq('#', /.*/),
statement: $ => prec.right(1, choice( statement: $ => choice(
$.assignment, $.assignment,
$.expression, $.expression,
$.control_flow, $.if_else,
$.yield, $.yield,
$.insert, $.insert,
$.select, $.select,
$.loop, $.loop,
$.match, $.match,
)), ),
yield: $ => prec.right(2, yield: $ => seq(
seq( $.expression,
$.expression, '->',
'->', $.expression,
$.expression, repeat(prec.left(seq('->', $.expression)))
repeat(prec.left(seq('->', $.expression)))
)
), ),
expression: $ => prec.right(choice( expression: $ => choice(
$.value, $.value,
$.identifier, $.identifier,
$.function_call, $.function_call,
$.math, $.math,
$.logic, $.logic,
)), ),
identifier: $ => /[a-z|_|.]+[0-9]?/, identifier: $ => /[a-z|_|.]+[0-9]?/,
@ -54,9 +52,9 @@ module.exports = grammar({
$.map, $.map,
), ),
integer: $ => /[-]*[0-9]+[.]{0}/, integer: $ => /[-]?[0-9]+/,
float: $ => /[-]*[0-9]*[.]{1}[0-9]+/, float: $ => /[-]?[0-9]+[.]{1}[0-9]*/,
string: $ => /("[^"]*?")|('[^']*?')|(`[^`]*?`)/, string: $ => /("[^"]*?")|('[^']*?')|(`[^`]*?`)/,
@ -125,7 +123,7 @@ module.exports = grammar({
assignment: $ => prec.right(seq( assignment: $ => prec.right(seq(
$.identifier, $.identifier,
choice("=", "+=", "-="), choice("=", "+=", "-="),
$.expression, $.statement,
)), )),
select: $ => prec.right(seq( select: $ => prec.right(seq(
@ -148,17 +146,17 @@ module.exports = grammar({
), ),
)), )),
control_flow: $ => prec.right(seq( if_else: $ => prec.left(1, seq(
'if', 'if',
$.expression, $.expression,
'then', 'then',
$.statement, $.statement,
prec.left(repeat(seq( repeat(seq(
'else if', 'else if',
$.expression, $.expression,
'then', 'then',
$.statement, $.statement,
))), )),
optional(seq( optional(seq(
'else', 'else',
$.statement, $.statement,
@ -166,18 +164,13 @@ module.exports = grammar({
)), )),
function_call: $ => prec.right(seq( function_call: $ => prec.right(seq(
'(',
$.identifier, $.identifier,
'{',
repeat(seq($.expression)), repeat(seq($.expression)),
'}', ')',
)), )),
loop: $ => choice( while: $ => seq(
$.break_loop,
$.while_loop,
),
while_loop: $ => seq(
'while', 'while',
$.expression, $.expression,
'{', '{',
@ -185,21 +178,21 @@ module.exports = grammar({
'}', '}',
), ),
break_loop: $ => seq( loop: $ => seq(
'loop', 'loop',
'{', '{',
repeat($.statement), repeat($.statement),
'break', 'break',
optional($.value), optional($.value),
'}', '}',
), ),
match: $ => seq( match: $ => prec.right(seq(
'match', 'match',
$.expression, $.expression,
'{',
repeat1(seq($.expression, '=>', $.statement)), repeat1(seq($.expression, '=>', $.statement)),
'}', 'catch',
), $.statement
)),
} }
}); });

View File

@ -36,115 +36,103 @@
] ]
}, },
"statement": { "statement": {
"type": "PREC_RIGHT", "type": "CHOICE",
"value": 1, "members": [
"content": { {
"type": "CHOICE", "type": "SYMBOL",
"members": [ "name": "assignment"
{ },
"type": "SYMBOL", {
"name": "assignment" "type": "SYMBOL",
}, "name": "expression"
{ },
"type": "SYMBOL", {
"name": "expression" "type": "SYMBOL",
}, "name": "if_else"
{ },
"type": "SYMBOL", {
"name": "control_flow" "type": "SYMBOL",
}, "name": "yield"
{ },
"type": "SYMBOL", {
"name": "yield" "type": "SYMBOL",
}, "name": "insert"
{ },
"type": "SYMBOL", {
"name": "insert" "type": "SYMBOL",
}, "name": "select"
{ },
"type": "SYMBOL", {
"name": "select" "type": "SYMBOL",
}, "name": "loop"
{ },
"type": "SYMBOL", {
"name": "loop" "type": "SYMBOL",
}, "name": "match"
{ }
"type": "SYMBOL", ]
"name": "match"
}
]
}
}, },
"yield": { "yield": {
"type": "PREC_RIGHT", "type": "SEQ",
"value": 2, "members": [
"content": { {
"type": "SEQ", "type": "SYMBOL",
"members": [ "name": "expression"
{ },
"type": "SYMBOL", {
"name": "expression" "type": "STRING",
}, "value": "->"
{ },
"type": "STRING", {
"value": "->" "type": "SYMBOL",
}, "name": "expression"
{ },
"type": "SYMBOL", {
"name": "expression" "type": "REPEAT",
}, "content": {
{ "type": "PREC_LEFT",
"type": "REPEAT", "value": 0,
"content": { "content": {
"type": "PREC_LEFT", "type": "SEQ",
"value": 0, "members": [
"content": { {
"type": "SEQ", "type": "STRING",
"members": [ "value": "->"
{ },
"type": "STRING", {
"value": "->" "type": "SYMBOL",
}, "name": "expression"
{ }
"type": "SYMBOL", ]
"name": "expression"
}
]
}
} }
} }
] }
} ]
}, },
"expression": { "expression": {
"type": "PREC_RIGHT", "type": "CHOICE",
"value": 0, "members": [
"content": { {
"type": "CHOICE", "type": "SYMBOL",
"members": [ "name": "value"
{ },
"type": "SYMBOL", {
"name": "value" "type": "SYMBOL",
}, "name": "identifier"
{ },
"type": "SYMBOL", {
"name": "identifier" "type": "SYMBOL",
}, "name": "function_call"
{ },
"type": "SYMBOL", {
"name": "function_call" "type": "SYMBOL",
}, "name": "math"
{ },
"type": "SYMBOL", {
"name": "math" "type": "SYMBOL",
}, "name": "logic"
{ }
"type": "SYMBOL", ]
"name": "logic"
}
]
}
}, },
"identifier": { "identifier": {
"type": "PATTERN", "type": "PATTERN",
@ -189,11 +177,11 @@
}, },
"integer": { "integer": {
"type": "PATTERN", "type": "PATTERN",
"value": "[-]*[0-9]+[.]{0}" "value": "[-]?[0-9]+"
}, },
"float": { "float": {
"type": "PATTERN", "type": "PATTERN",
"value": "[-]*[0-9]*[.]{1}[0-9]+" "value": "[-]?[0-9]+[.]{1}[0-9]*"
}, },
"string": { "string": {
"type": "PATTERN", "type": "PATTERN",
@ -537,7 +525,7 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "expression" "name": "statement"
} }
] ]
} }
@ -637,9 +625,9 @@
] ]
} }
}, },
"control_flow": { "if_else": {
"type": "PREC_RIGHT", "type": "PREC_LEFT",
"value": 0, "value": 1,
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -660,31 +648,27 @@
"name": "statement" "name": "statement"
}, },
{ {
"type": "PREC_LEFT", "type": "REPEAT",
"value": 0,
"content": { "content": {
"type": "REPEAT", "type": "SEQ",
"content": { "members": [
"type": "SEQ", {
"members": [ "type": "STRING",
{ "value": "else if"
"type": "STRING", },
"value": "else if" {
}, "type": "SYMBOL",
{ "name": "expression"
"type": "SYMBOL", },
"name": "expression" {
}, "type": "STRING",
{ "value": "then"
"type": "STRING", },
"value": "then" {
}, "type": "SYMBOL",
{ "name": "statement"
"type": "SYMBOL", }
"name": "statement" ]
}
]
}
} }
}, },
{ {
@ -718,12 +702,12 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "STRING",
"name": "identifier" "value": "("
}, },
{ {
"type": "STRING", "type": "SYMBOL",
"value": "{" "name": "identifier"
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@ -739,25 +723,12 @@
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "}" "value": ")"
} }
] ]
} }
}, },
"loop": { "while": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "break_loop"
},
{
"type": "SYMBOL",
"name": "while_loop"
}
]
},
"while_loop": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -782,7 +753,7 @@
} }
] ]
}, },
"break_loop": { "loop": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -823,45 +794,49 @@
] ]
}, },
"match": { "match": {
"type": "SEQ", "type": "PREC_RIGHT",
"members": [ "value": 0,
{ "content": {
"type": "STRING", "type": "SEQ",
"value": "match" "members": [
}, {
{ "type": "STRING",
"type": "SYMBOL", "value": "match"
"name": "expression" },
}, {
{ "type": "SYMBOL",
"type": "STRING", "name": "expression"
"value": "{" },
}, {
{ "type": "REPEAT1",
"type": "REPEAT1", "content": {
"content": { "type": "SEQ",
"type": "SEQ", "members": [
"members": [ {
{ "type": "SYMBOL",
"type": "SYMBOL", "name": "expression"
"name": "expression" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "=>"
"value": "=>" },
}, {
{ "type": "SYMBOL",
"type": "SYMBOL", "name": "statement"
"name": "statement" }
} ]
] }
},
{
"type": "STRING",
"value": "catch"
},
{
"type": "SYMBOL",
"name": "statement"
} }
}, ]
{ }
"type": "STRING",
"value": "}"
}
]
} }
}, },
"extras": [ "extras": [

View File

@ -8,11 +8,11 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "expression", "type": "identifier",
"named": true "named": true
}, },
{ {
"type": "identifier", "type": "statement",
"named": true "named": true
} }
] ]
@ -23,49 +23,11 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "break_loop",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "statement",
"named": true
},
{
"type": "value",
"named": true
}
]
}
},
{ {
"type": "comment", "type": "comment",
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "control_flow",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "statement",
"named": true
}
]
}
},
{ {
"type": "expression", "type": "expression",
"named": true, "named": true,
@ -135,6 +97,25 @@
] ]
} }
}, },
{
"type": "if_else",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "statement",
"named": true
}
]
}
},
{ {
"type": "insert", "type": "insert",
"named": true, "named": true,
@ -221,15 +202,15 @@
"named": true, "named": true,
"fields": {}, "fields": {},
"children": { "children": {
"multiple": false, "multiple": true,
"required": true, "required": false,
"types": [ "types": [
{ {
"type": "break_loop", "type": "statement",
"named": true "named": true
}, },
{ {
"type": "while_loop", "type": "value",
"named": true "named": true
} }
] ]
@ -344,11 +325,11 @@
"named": true "named": true
}, },
{ {
"type": "control_flow", "type": "expression",
"named": true "named": true
}, },
{ {
"type": "expression", "type": "if_else",
"named": true "named": true
}, },
{ {
@ -436,25 +417,6 @@
] ]
} }
}, },
{
"type": "while_loop",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "statement",
"named": true
}
]
}
},
{ {
"type": "yield", "type": "yield",
"named": true, "named": true,
@ -486,6 +448,14 @@
"type": "&&", "type": "&&",
"named": false "named": false
}, },
{
"type": "(",
"named": false
},
{
"type": ")",
"named": false
},
{ {
"type": "*", "type": "*",
"named": false "named": false
@ -554,6 +524,10 @@
"type": "break", "type": "break",
"named": false "named": false
}, },
{
"type": "catch",
"named": false
},
{ {
"type": "else", "type": "else",
"named": false "named": false

17987
src/parser.c

File diff suppressed because it is too large Load Diff