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"
@ -9,7 +9,7 @@ if true then "True"
(root
(item
(statement
(control_flow
(if_else
(expression
(value
(boolean)))
@ -19,7 +19,7 @@ if true then "True"
(string))))))))
==================
If/Then Assignment
If Assignment
==================
x = if true then 1
@ -31,25 +31,18 @@ x = if true then 1
(statement
(assignment
(identifier)
(expression
(identifier)))))
(item
(statement
(expression
(value
(boolean)))))
(item
(statement
(expression
(identifier))))
(item
(statement
(expression
(value
(integer))))))
(statement
(if_else
(expression
(value
(boolean)))
(statement
(expression
(value
(integer))))))))))
==================
If/Else
If Else
==================
if false then "True" else "False"
@ -59,7 +52,7 @@ if false then "True" else "False"
(root
(item
(statement
(control_flow
(if_else
(expression
(value
(boolean)))
@ -73,7 +66,7 @@ if false then "True" else "False"
(string))))))))
==================
If/Else If
If Else If
==================
if 1 == 1
@ -86,7 +79,7 @@ else if 4 == 9
(root
(item
(statement
(control_flow
(if_else
(expression
(logic
(expression
@ -113,3 +106,60 @@ else if 4 == 9
(expression
(value
(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
==================
function <> { "Hiya" }
function { "Hiya" }
---
@ -22,7 +22,7 @@ function <> { "Hiya" }
Function Call
==================
foobar {"Hiya"}
(foobar "Hiya")
---
@ -40,7 +40,7 @@ foobar {"Hiya"}
Complex Function
==================
function <message, number> {
function <message number> {
output message
output number
}

View File

@ -30,13 +30,14 @@ foobar = ['answer', 42]
(statement
(assignment
(identifier)
(expression
(value
(list
(value
(string))
(value
(integer)))))))))
(statement
(expression
(value
(list
(value
(string))
(value
(integer))))))))))
==================
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
(item
(statement
(statement
(assignment
(identifier)
(expression
(value
(integer))))))
(statement
(expression
(value
(integer)))))))
(item
(statement
(statement
(assignment
(identifier)
(expression
(value
(string)))))))
(statement
(expression
(value
(string))))))))
==================
Complex Assignment

View File

@ -37,16 +37,17 @@ foobar = table <text, number> {
(statement
(assignment
(identifier)
(expression
(value
(table
(identifier)
(identifier)
(list
(value
(string))
(value
(integer))))))))))
(statement
(expression
(value
(table
(identifier)
(identifier)
(list
(value
(string))
(value
(integer)))))))))))
==================
Table Access

View File

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

View File

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

View File

@ -8,11 +8,11 @@
"required": true,
"types": [
{
"type": "expression",
"type": "identifier",
"named": true
},
{
"type": "identifier",
"type": "statement",
"named": true
}
]
@ -23,49 +23,11 @@
"named": true,
"fields": {}
},
{
"type": "break_loop",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "statement",
"named": true
},
{
"type": "value",
"named": true
}
]
}
},
{
"type": "comment",
"named": true,
"fields": {}
},
{
"type": "control_flow",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "statement",
"named": true
}
]
}
},
{
"type": "expression",
"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",
"named": true,
@ -221,15 +202,15 @@
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"multiple": true,
"required": false,
"types": [
{
"type": "break_loop",
"type": "statement",
"named": true
},
{
"type": "while_loop",
"type": "value",
"named": true
}
]
@ -344,11 +325,11 @@
"named": true
},
{
"type": "control_flow",
"type": "expression",
"named": true
},
{
"type": "expression",
"type": "if_else",
"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",
"named": true,
@ -486,6 +448,14 @@
"type": "&&",
"named": false
},
{
"type": "(",
"named": false
},
{
"type": ")",
"named": false
},
{
"type": "*",
"named": false
@ -554,6 +524,10 @@
"type": "break",
"named": false
},
{
"type": "catch",
"named": false
},
{
"type": "else",
"named": false

17987
src/parser.c

File diff suppressed because it is too large Load Diff