Add tests and grammar for control flow
This commit is contained in:
parent
4b0c658a2b
commit
c7bfba7767
@ -1,7 +1,7 @@
|
||||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "tree_sitter_Dust_binding",
|
||||
"target_name": "tree_sitter_dust_binding",
|
||||
"include_dirs": [
|
||||
"<!(node -e \"require('nan')\")",
|
||||
"src"
|
||||
|
@ -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
50
corpus/control_flow.txt
Normal 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))))
|
17
grammar.js
17
grammar.js
@ -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))
|
||||
)),
|
||||
}
|
||||
});
|
||||
|
@ -18,14 +18,17 @@
|
||||
}
|
||||
},
|
||||
"comment": {
|
||||
"type": "PREC_RIGHT",
|
||||
"type": "PREC_LEFT",
|
||||
"value": 0,
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "#"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "PATTERN",
|
||||
@ -35,8 +38,11 @@
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "#"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
@ -73,6 +79,9 @@
|
||||
]
|
||||
},
|
||||
"open_statement": {
|
||||
"type": "PREC_LEFT",
|
||||
"value": 0,
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
@ -80,6 +89,7 @@
|
||||
"name": "expression"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "CHOICE",
|
||||
@ -107,6 +117,14 @@
|
||||
"type": "SYMBOL",
|
||||
"name": "operation"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "PREC",
|
||||
"value": 3,
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "control_flow"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -222,6 +240,8 @@
|
||||
]
|
||||
},
|
||||
"operator": {
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
@ -237,6 +257,7 @@
|
||||
"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": [
|
||||
|
@ -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
|
||||
|
2269
src/parser.c
2269
src/parser.c
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user