Add tests and grammar for control flow

This commit is contained in:
Jeff 2023-09-29 03:52:21 -04:00
parent 4b0c658a2b
commit c7bfba7767
7 changed files with 1830 additions and 691 deletions

View File

@ -1,7 +1,7 @@
{
"targets": [
{
"target_name": "tree_sitter_Dust_binding",
"target_name": "tree_sitter_dust_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"

View File

@ -18,18 +18,18 @@ Partial Line Comments
==================
# comment # 1;
#comment# "one";
#comment# "one"
---
(source
(comment)
(statement
(closed_statment
(closed_statement
(value
(integer))))
(comment)
(statement
(closed_statment
(open_statement
(value
(string)))))

50
corpus/control_flow.txt Normal file
View File

@ -0,0 +1,50 @@
==================
If/Then
==================
if true then "True";
---
(source
(statement
(closed_statement
(expression
(control_flow
(expression
(value
(boolean)))
(statement
(open_statement
(expression
(value
(string)))))))
(close))))
==================
If/Then Assignment
==================
x = if true then 1;
---
(source
(statement
(closed_statement
(expression
(operation
(expression
(identifier))
(operator)
(expression
(control_flow
(expression
(value
(boolean)))
(statement
(open_statement
(expression
(value
(integer)))))))))
(close))))

View File

@ -7,7 +7,7 @@ module.exports = grammar({
$.statement,
)),
comment: $ => prec.right(seq('#', /.*/, optional('#'))),
comment: $ => prec.left(seq(token('#'), /.*/, optional(token('#')))),
statement: $ => choice(
$.closed_statement,
@ -16,12 +16,13 @@ module.exports = grammar({
closed_statement: $ => seq($.expression, $.close),
open_statement: $ => seq($.expression),
open_statement: $ => prec.left(seq($.expression)),
expression: $ => choice(
prec(0, $.value),
prec(1, $.identifier),
prec(2, $.operation),
prec(3, $.control_flow),
),
close: $ => ";",
@ -59,16 +60,24 @@ module.exports = grammar({
')'
),
operator: $ => choice(
operator: $ => token(choice(
'+',
'-',
'=',
),
)),
operation: $ => prec.left(seq(
$.expression,
$.operator,
$.expression,
)),
control_flow: $ => prec.right(seq(
'if',
$.expression,
'then',
$.statement,
optional(seq('else', $.statement))
)),
}
});

View File

@ -18,14 +18,17 @@
}
},
"comment": {
"type": "PREC_RIGHT",
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "#"
"type": "TOKEN",
"content": {
"type": "STRING",
"value": "#"
}
},
{
"type": "PATTERN",
@ -35,8 +38,11 @@
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "#"
"type": "TOKEN",
"content": {
"type": "STRING",
"value": "#"
}
},
{
"type": "BLANK"
@ -73,13 +79,17 @@
]
},
"open_statement": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
}
]
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
}
]
}
},
"expression": {
"type": "CHOICE",
@ -107,6 +117,14 @@
"type": "SYMBOL",
"name": "operation"
}
},
{
"type": "PREC",
"value": 3,
"content": {
"type": "SYMBOL",
"name": "control_flow"
}
}
]
},
@ -222,21 +240,24 @@
]
},
"operator": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "+"
},
{
"type": "STRING",
"value": "-"
},
{
"type": "STRING",
"value": "="
}
]
"type": "TOKEN",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "+"
},
{
"type": "STRING",
"value": "-"
},
{
"type": "STRING",
"value": "="
}
]
}
},
"operation": {
"type": "PREC_LEFT",
@ -258,6 +279,52 @@
}
]
}
},
"control_flow": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "if"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": "then"
},
{
"type": "SYMBOL",
"name": "statement"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "SYMBOL",
"name": "statement"
}
]
},
{
"type": "BLANK"
}
]
}
]
}
}
},
"extras": [

View File

@ -28,6 +28,25 @@
"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,
@ -36,6 +55,10 @@
"multiple": false,
"required": true,
"types": [
{
"type": "control_flow",
"named": true
},
{
"type": "identifier",
"named": true
@ -100,11 +123,6 @@
]
}
},
{
"type": "operator",
"named": true,
"fields": {}
},
{
"type": "source",
"named": true,
@ -194,26 +212,18 @@
"type": ")",
"named": false
},
{
"type": "+",
"named": false
},
{
"type": ",",
"named": false
},
{
"type": "-",
"named": false
},
{
"type": "=",
"named": false
},
{
"type": "close",
"named": true
},
{
"type": "else",
"named": false
},
{
"type": "empty",
"named": true
@ -234,14 +244,26 @@
"type": "identifier",
"named": true
},
{
"type": "if",
"named": false
},
{
"type": "integer",
"named": true
},
{
"type": "operator",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "then",
"named": false
},
{
"type": "true",
"named": false

File diff suppressed because it is too large Load Diff