Update grammar and tests

This commit is contained in:
Jeff 2023-09-22 06:38:25 -04:00
parent b7cecd46a0
commit 10564a0a6a
5 changed files with 1069 additions and 1047 deletions

View File

@ -4,15 +4,13 @@ Comments
# x = 1; # x = 1;
# unassigned_variable # unassigned_variable
x # xyz #xyz
--- ---
(source_file (source
(comment) (comment)
(comment) (comment)
(expression
(identifier))
(comment)) (comment))
================== ==================
@ -28,19 +26,13 @@ x.x
--- ---
(source_file (source
(expression (identifier)
(identifier)) (identifier)
(expression (identifier)
(identifier)) (identifier)
(expression (identifier)
(identifier)) (identifier))
(expression
(identifier))
(expression
(identifier))
(expression
(identifier)))
================== ==================
Operators Operators
@ -50,56 +42,58 @@ x = y + y;
--- ---
(source_file (source
(expression (identifier)
(identifier) (operator)
(operator) (identifier)
(expression (operator)
(identifier) (identifier)
(operator) (operator))
(expression
(identifier))))
(chain))
================== ==================
String String
================== ==================
"string" "string"
'string'
--- ---
(source_file (source
(expression (value
(value (string))
(string)))) (value
(string)))
================== ==================
Integer Integer
================== ==================
1 1
123
--- ---
(source_file (source
(expression (value
(value (integer))
(integer)))) (value
(integer)))
================== ==================
Float Float
================== ==================
1.0 1.0
123.123
--- ---
(source_file (source
(expression (value
(value (float))
(float)))) (value
(float)))
================== ==================
List List
@ -109,16 +103,13 @@ List
--- ---
(source_file (source
(expression (value
(value (list
(list (value
(expression (integer))
(value (value
(integer))) (integer)))))
(expression
(value
(integer)))))))
================== ==================
Empty Empty
@ -128,10 +119,9 @@ Empty
--- ---
(source_file (source
(expression (value
(value (empty)))
(empty))))
================== ==================
Tool Tool
@ -141,13 +131,11 @@ random_boolean();
--- ---
(source_file (source
(expression (tool)
(tool)) (value
(expression (empty))
(value (operator))
(empty)))
(chain))
================== ==================
Boolean Boolean
@ -157,10 +145,8 @@ true false
--- ---
(source_file (source
(expression (value
(value (boolean))
(boolean))) (value
(expression (boolean)))
(value
(boolean))))

View File

@ -2,19 +2,17 @@ module.exports = grammar({
name: 'dust', name: 'dust',
rules: { rules: {
source_file: $ => repeat(choice( source: $ => repeat(choice(
$.comment, $.comment,
$.expression, $.identifier,
$.yield, $.value,
$.chain $.tool,
$.operator,
)), )),
expression: $ => choice( comment: $ => seq('#', /.*/),
prec(2, $.value),
prec(1, $.identifier), identifier: $ => /[a-zA-Z|_|.]+(_[a-zA-Z]+)*/,
$.tool,
seq($.identifier, $.operator, $.expression),
),
value: $ => choice( value: $ => choice(
$.integer, $.integer,
@ -23,23 +21,7 @@ module.exports = grammar({
$.list, $.list,
$.empty, $.empty,
$.boolean, $.boolean,
), $.function,
comment: $ => /(#)(.+?)([\n\r])/,
yield: $ => "->",
chain: $ => ";",
identifier: $ => /[a-zA-Z|_|.]+(_[a-zA-Z]+)*/,
operator: $ => choice(
'=',
'-',
'+',
'/',
'|',
'&'
), ),
tool: $ => choice( tool: $ => choice(
@ -47,16 +29,27 @@ module.exports = grammar({
"random_boolean", "random_boolean",
"random_integer", "random_integer",
"random_string", "random_string",
"random_float" "random_float",
),
operator: $ => choice(
'=',
'-',
'+',
'/',
'|',
'&',
';',
"->",
), ),
float: $ => /\d+\.\d*/, float: $ => /\d+\.\d*/,
integer: $ => /\d+/, integer: $ => /\d+/,
string: $ => /"(.*?)\"/, string: $ => /("|')(.*?)("|')/,
function: $ => /'(.*?)\'/, function: $ => /{(.*?)}/,
empty: $ => "()", empty: $ => "()",
@ -67,7 +60,7 @@ module.exports = grammar({
list: $ => seq( list: $ => seq(
'(', '(',
repeat1(seq($.expression, optional(','))), repeat1(seq($.value, optional(','))),
')' ')'
), ),
} }

View File

@ -1,7 +1,7 @@
{ {
"name": "dust", "name": "dust",
"rules": { "rules": {
"source_file": { "source": {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "CHOICE", "type": "CHOICE",
@ -12,61 +12,40 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "expression" "name": "identifier"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "yield" "name": "value"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "chain" "name": "tool"
},
{
"type": "SYMBOL",
"name": "operator"
} }
] ]
} }
}, },
"expression": { "comment": {
"type": "CHOICE", "type": "SEQ",
"members": [ "members": [
{ {
"type": "PREC", "type": "STRING",
"value": 2, "value": "#"
"content": {
"type": "SYMBOL",
"name": "value"
}
}, },
{ {
"type": "PREC", "type": "PATTERN",
"value": 1, "value": ".*"
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "SYMBOL",
"name": "tool"
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "operator"
},
{
"type": "SYMBOL",
"name": "expression"
}
]
} }
] ]
}, },
"identifier": {
"type": "PATTERN",
"value": "[a-zA-Z|_|.]+(_[a-zA-Z]+)*"
},
"value": { "value": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -93,51 +72,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "boolean" "name": "boolean"
}
]
},
"comment": {
"type": "PATTERN",
"value": "(#)(.+?)([\\n\\r])"
},
"yield": {
"type": "STRING",
"value": "->"
},
"chain": {
"type": "STRING",
"value": ";"
},
"identifier": {
"type": "PATTERN",
"value": "[a-zA-Z|_|.]+(_[a-zA-Z]+)*"
},
"operator": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "="
}, },
{ {
"type": "STRING", "type": "SYMBOL",
"value": "-" "name": "function"
},
{
"type": "STRING",
"value": "+"
},
{
"type": "STRING",
"value": "/"
},
{
"type": "STRING",
"value": "|"
},
{
"type": "STRING",
"value": "&"
} }
] ]
}, },
@ -166,6 +104,43 @@
} }
] ]
}, },
"operator": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "STRING",
"value": "-"
},
{
"type": "STRING",
"value": "+"
},
{
"type": "STRING",
"value": "/"
},
{
"type": "STRING",
"value": "|"
},
{
"type": "STRING",
"value": "&"
},
{
"type": "STRING",
"value": ";"
},
{
"type": "STRING",
"value": "->"
}
]
},
"float": { "float": {
"type": "PATTERN", "type": "PATTERN",
"value": "\\d+\\.\\d*" "value": "\\d+\\.\\d*"
@ -176,11 +151,11 @@
}, },
"string": { "string": {
"type": "PATTERN", "type": "PATTERN",
"value": "\"(.*?)\\\"" "value": "(\"|')(.*?)(\"|')"
}, },
"function": { "function": {
"type": "PATTERN", "type": "PATTERN",
"value": "'(.*?)\\'" "value": "{(.*?)}"
}, },
"empty": { "empty": {
"type": "STRING", "type": "STRING",
@ -213,7 +188,7 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "expression" "name": "value"
}, },
{ {
"type": "CHOICE", "type": "CHOICE",

View File

@ -5,7 +5,12 @@
"fields": {} "fields": {}
}, },
{ {
"type": "expression", "type": "comment",
"named": true,
"fields": {}
},
{
"type": "list",
"named": true, "named": true,
"fields": {}, "fields": {},
"children": { "children": {
@ -13,7 +18,27 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "expression", "type": "value",
"named": true
}
]
}
},
{
"type": "operator",
"named": true,
"fields": {}
},
{
"type": "source",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "comment",
"named": true "named": true
}, },
{ {
@ -35,53 +60,6 @@
] ]
} }
}, },
{
"type": "list",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
}
},
{
"type": "operator",
"named": true,
"fields": {}
},
{
"type": "source_file",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "chain",
"named": true
},
{
"type": "comment",
"named": true
},
{
"type": "expression",
"named": true
},
{
"type": "yield",
"named": true
}
]
}
},
{ {
"type": "tool", "type": "tool",
"named": true, "named": true,
@ -107,6 +85,10 @@
"type": "float", "type": "float",
"named": true "named": true
}, },
{
"type": "function",
"named": true
},
{ {
"type": "integer", "type": "integer",
"named": true "named": true
@ -122,6 +104,10 @@
] ]
} }
}, },
{
"type": "#",
"named": false
},
{ {
"type": "&", "type": "&",
"named": false "named": false
@ -146,22 +132,22 @@
"type": "-", "type": "-",
"named": false "named": false
}, },
{
"type": "->",
"named": false
},
{ {
"type": "/", "type": "/",
"named": false "named": false
}, },
{
"type": ";",
"named": false
},
{ {
"type": "=", "type": "=",
"named": false "named": false
}, },
{
"type": "chain",
"named": true
},
{
"type": "comment",
"named": true
},
{ {
"type": "empty", "type": "empty",
"named": true "named": true
@ -174,6 +160,10 @@
"type": "float", "type": "float",
"named": true "named": true
}, },
{
"type": "function",
"named": true
},
{ {
"type": "identifier", "type": "identifier",
"named": true "named": true
@ -210,10 +200,6 @@
"type": "true", "type": "true",
"named": false "named": false
}, },
{
"type": "yield",
"named": true
},
{ {
"type": "|", "type": "|",
"named": false "named": false

File diff suppressed because it is too large Load Diff