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": [ "targets": [
{ {
"target_name": "tree_sitter_Dust_binding", "target_name": "tree_sitter_dust_binding",
"include_dirs": [ "include_dirs": [
"<!(node -e \"require('nan')\")", "<!(node -e \"require('nan')\")",
"src" "src"

View File

@ -18,18 +18,18 @@ Partial Line Comments
================== ==================
# comment # 1; # comment # 1;
#comment# "one"; #comment# "one"
--- ---
(source (source
(comment) (comment)
(statement (statement
(closed_statment (closed_statement
(value (value
(integer)))) (integer))))
(comment) (comment)
(statement (statement
(closed_statment (open_statement
(value (value
(string))))) (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, $.statement,
)), )),
comment: $ => prec.right(seq('#', /.*/, optional('#'))), comment: $ => prec.left(seq(token('#'), /.*/, optional(token('#')))),
statement: $ => choice( statement: $ => choice(
$.closed_statement, $.closed_statement,
@ -16,12 +16,13 @@ module.exports = grammar({
closed_statement: $ => seq($.expression, $.close), closed_statement: $ => seq($.expression, $.close),
open_statement: $ => seq($.expression), open_statement: $ => prec.left(seq($.expression)),
expression: $ => choice( expression: $ => choice(
prec(0, $.value), prec(0, $.value),
prec(1, $.identifier), prec(1, $.identifier),
prec(2, $.operation), prec(2, $.operation),
prec(3, $.control_flow),
), ),
close: $ => ";", close: $ => ";",
@ -59,16 +60,24 @@ module.exports = grammar({
')' ')'
), ),
operator: $ => choice( operator: $ => token(choice(
'+', '+',
'-', '-',
'=', '=',
), )),
operation: $ => prec.left(seq( operation: $ => prec.left(seq(
$.expression, $.expression,
$.operator, $.operator,
$.expression, $.expression,
)), )),
control_flow: $ => prec.right(seq(
'if',
$.expression,
'then',
$.statement,
optional(seq('else', $.statement))
)),
} }
}); });

View File

@ -18,14 +18,17 @@
} }
}, },
"comment": { "comment": {
"type": "PREC_RIGHT", "type": "PREC_LEFT",
"value": 0, "value": 0,
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "TOKEN",
"content": {
"type": "STRING", "type": "STRING",
"value": "#" "value": "#"
}
}, },
{ {
"type": "PATTERN", "type": "PATTERN",
@ -35,8 +38,11 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "TOKEN",
"content": {
"type": "STRING", "type": "STRING",
"value": "#" "value": "#"
}
}, },
{ {
"type": "BLANK" "type": "BLANK"
@ -73,6 +79,9 @@
] ]
}, },
"open_statement": { "open_statement": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -80,6 +89,7 @@
"name": "expression" "name": "expression"
} }
] ]
}
}, },
"expression": { "expression": {
"type": "CHOICE", "type": "CHOICE",
@ -107,6 +117,14 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "operation" "name": "operation"
} }
},
{
"type": "PREC",
"value": 3,
"content": {
"type": "SYMBOL",
"name": "control_flow"
}
} }
] ]
}, },
@ -222,6 +240,8 @@
] ]
}, },
"operator": { "operator": {
"type": "TOKEN",
"content": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -237,6 +257,7 @@
"value": "=" "value": "="
} }
] ]
}
}, },
"operation": { "operation": {
"type": "PREC_LEFT", "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": [ "extras": [

View File

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

File diff suppressed because it is too large Load Diff