2023-10-18 21:52:56 +00:00
|
|
|
module.exports = grammar({
|
|
|
|
name: 'dust',
|
|
|
|
|
2024-01-01 09:59:27 +00:00
|
|
|
word: $ => $.identifier,
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
extras: $ => [/\s/, $._comment],
|
2023-10-31 07:17:58 +00:00
|
|
|
|
2023-10-18 21:52:56 +00:00
|
|
|
rules: {
|
2023-11-30 00:23:42 +00:00
|
|
|
root: $ =>
|
|
|
|
prec(1, repeat1($.statement)),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-11-04 10:02:27 +00:00
|
|
|
_comment: $ => /[#][^#\n]*[#|\n]/,
|
2023-10-31 07:17:58 +00:00
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
statement: $ =>
|
|
|
|
prec.left(
|
|
|
|
seq(
|
|
|
|
choice(
|
|
|
|
$.assignment,
|
|
|
|
$.block,
|
|
|
|
$.expression,
|
|
|
|
$.for,
|
|
|
|
$.if_else,
|
|
|
|
$.index_assignment,
|
|
|
|
$.match,
|
|
|
|
$.return,
|
|
|
|
$.while,
|
|
|
|
),
|
|
|
|
optional(';'),
|
|
|
|
),
|
2023-10-31 20:25:13 +00:00
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
|
|
|
|
expression: $ =>
|
2023-12-30 02:53:26 +00:00
|
|
|
choice(
|
|
|
|
$._expression_kind,
|
|
|
|
seq(
|
|
|
|
'(',
|
|
|
|
$._expression_kind,
|
|
|
|
')',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
_expression_kind: $ =>
|
2023-11-30 00:23:42 +00:00
|
|
|
prec.right(
|
|
|
|
choice(
|
|
|
|
$.function_call,
|
|
|
|
$.identifier,
|
|
|
|
$.index,
|
|
|
|
$.logic,
|
|
|
|
$.math,
|
|
|
|
$.value,
|
|
|
|
$.yield,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
_expression_list: $ =>
|
2023-10-31 09:51:37 +00:00
|
|
|
repeat1(
|
2023-11-30 00:23:42 +00:00
|
|
|
prec.right(
|
|
|
|
seq(
|
|
|
|
$.expression,
|
|
|
|
optional(','),
|
|
|
|
),
|
|
|
|
),
|
2023-10-31 09:51:37 +00:00
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
|
2023-12-31 16:46:56 +00:00
|
|
|
block: $ =>
|
|
|
|
seq(
|
|
|
|
optional('async'),
|
|
|
|
'{',
|
|
|
|
repeat($.statement),
|
|
|
|
'}',
|
|
|
|
),
|
|
|
|
|
2023-12-17 02:15:36 +00:00
|
|
|
identifier: $ =>
|
2023-12-31 16:03:33 +00:00
|
|
|
/[_a-zA-Z]+[_a-zA-Z0-9]*[_a-zA-Z]?/,
|
2023-11-30 00:23:42 +00:00
|
|
|
|
|
|
|
value: $ =>
|
|
|
|
choice(
|
2023-12-12 23:21:16 +00:00
|
|
|
$.function,
|
2023-11-30 00:23:42 +00:00
|
|
|
$.integer,
|
|
|
|
$.float,
|
|
|
|
$.string,
|
|
|
|
$.boolean,
|
|
|
|
$.list,
|
|
|
|
$.map,
|
2023-12-30 01:14:03 +00:00
|
|
|
$.option,
|
2024-01-01 04:38:09 +00:00
|
|
|
$.built_in_value,
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
integer: $ =>
|
|
|
|
token(
|
|
|
|
prec.left(
|
|
|
|
seq(
|
|
|
|
optional('-'),
|
|
|
|
repeat1(
|
|
|
|
choice(
|
|
|
|
'1',
|
|
|
|
'2',
|
|
|
|
'3',
|
|
|
|
'4',
|
|
|
|
'5',
|
|
|
|
'6',
|
|
|
|
'7',
|
|
|
|
'8',
|
|
|
|
'9',
|
|
|
|
'0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
float: $ =>
|
|
|
|
token(
|
|
|
|
prec.left(
|
|
|
|
seq(
|
|
|
|
optional('-'),
|
|
|
|
repeat1(
|
|
|
|
choice(
|
|
|
|
'1',
|
|
|
|
'2',
|
|
|
|
'3',
|
|
|
|
'4',
|
|
|
|
'5',
|
|
|
|
'6',
|
|
|
|
'7',
|
|
|
|
'8',
|
|
|
|
'9',
|
|
|
|
'0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'.',
|
|
|
|
repeat1(
|
|
|
|
choice(
|
|
|
|
'1',
|
|
|
|
'2',
|
|
|
|
'3',
|
|
|
|
'4',
|
|
|
|
'5',
|
|
|
|
'6',
|
|
|
|
'7',
|
|
|
|
'8',
|
|
|
|
'9',
|
|
|
|
'0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
string: $ =>
|
|
|
|
/("[^"]*?")|('[^']*?')|(`[^`]*?`)/,
|
|
|
|
|
|
|
|
boolean: $ =>
|
|
|
|
choice('true', 'false'),
|
|
|
|
|
|
|
|
list: $ =>
|
|
|
|
seq(
|
|
|
|
'[',
|
|
|
|
repeat(
|
|
|
|
prec.left(
|
|
|
|
seq(
|
|
|
|
$.expression,
|
|
|
|
optional(','),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
']',
|
|
|
|
),
|
|
|
|
|
|
|
|
map: $ =>
|
|
|
|
seq(
|
|
|
|
'{',
|
2023-12-31 16:46:56 +00:00
|
|
|
repeat1(
|
2023-11-30 00:23:42 +00:00
|
|
|
seq(
|
|
|
|
$.identifier,
|
2023-12-20 23:29:18 +00:00
|
|
|
optional($.type_definition),
|
2023-11-30 00:23:42 +00:00
|
|
|
'=',
|
|
|
|
$.statement,
|
|
|
|
optional(','),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'}',
|
|
|
|
),
|
|
|
|
|
2023-12-22 20:02:22 +00:00
|
|
|
option: $ =>
|
|
|
|
choice(
|
|
|
|
'none',
|
|
|
|
seq(
|
|
|
|
'some',
|
|
|
|
'(',
|
|
|
|
$.expression,
|
|
|
|
')',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
index: $ =>
|
|
|
|
prec.left(
|
2024-01-01 02:46:45 +00:00
|
|
|
1,
|
2023-11-30 00:23:42 +00:00
|
|
|
seq(
|
2024-01-01 04:38:09 +00:00
|
|
|
$.index_expression,
|
2023-11-30 00:23:42 +00:00
|
|
|
':',
|
2024-01-01 04:38:09 +00:00
|
|
|
$.index_expression,
|
2023-11-30 00:23:42 +00:00
|
|
|
optional(
|
|
|
|
seq('..', $.expression),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
2024-01-01 04:38:09 +00:00
|
|
|
index_expression: $ =>
|
|
|
|
prec(
|
|
|
|
1,
|
|
|
|
choice(
|
|
|
|
seq(
|
|
|
|
'(',
|
|
|
|
$.function_call,
|
|
|
|
')',
|
|
|
|
),
|
|
|
|
$.identifier,
|
|
|
|
$.index,
|
|
|
|
$.value,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
math: $ =>
|
|
|
|
prec.left(
|
2023-12-30 02:15:03 +00:00
|
|
|
seq(
|
|
|
|
$.expression,
|
|
|
|
$.math_operator,
|
|
|
|
$.expression,
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
math_operator: $ =>
|
|
|
|
choice('+', '-', '*', '/', '%'),
|
|
|
|
|
|
|
|
logic: $ =>
|
2023-12-30 02:53:26 +00:00
|
|
|
prec.left(
|
2023-12-30 02:15:03 +00:00
|
|
|
seq(
|
|
|
|
$.expression,
|
|
|
|
$.logic_operator,
|
|
|
|
$.expression,
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
logic_operator: $ =>
|
2023-12-02 03:54:25 +00:00
|
|
|
prec.left(
|
|
|
|
choice(
|
|
|
|
'==',
|
|
|
|
'!=',
|
|
|
|
'&&',
|
|
|
|
'||',
|
|
|
|
'>',
|
|
|
|
'<',
|
|
|
|
'>=',
|
|
|
|
'<=',
|
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
assignment: $ =>
|
|
|
|
seq(
|
2023-11-30 14:30:25 +00:00
|
|
|
$.identifier,
|
|
|
|
optional($.type_definition),
|
|
|
|
$.assignment_operator,
|
|
|
|
$.statement,
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
index_assignment: $ =>
|
|
|
|
seq(
|
|
|
|
$.index,
|
|
|
|
$.assignment_operator,
|
2023-10-31 17:04:22 +00:00
|
|
|
$.statement,
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
assignment_operator: $ =>
|
|
|
|
prec.right(
|
|
|
|
choice('=', '+=', '-='),
|
|
|
|
),
|
|
|
|
|
|
|
|
if_else: $ =>
|
|
|
|
prec.right(
|
|
|
|
seq(
|
|
|
|
$.if,
|
|
|
|
repeat($.else_if),
|
|
|
|
optional($.else),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
if: $ =>
|
|
|
|
seq('if', $.expression, $.block),
|
|
|
|
|
|
|
|
else_if: $ =>
|
|
|
|
seq(
|
|
|
|
'else if',
|
|
|
|
$.expression,
|
|
|
|
$.block,
|
|
|
|
),
|
|
|
|
|
|
|
|
else: $ => seq('else', $.block),
|
|
|
|
|
|
|
|
match: $ =>
|
|
|
|
prec.right(
|
|
|
|
seq(
|
|
|
|
'match',
|
|
|
|
$.expression,
|
2023-12-06 18:48:38 +00:00
|
|
|
'{',
|
2023-11-30 00:23:42 +00:00
|
|
|
repeat1(
|
|
|
|
seq(
|
2023-12-17 02:15:36 +00:00
|
|
|
choice($.expression, '*'),
|
2023-11-30 00:23:42 +00:00
|
|
|
'=>',
|
2023-12-06 18:48:38 +00:00
|
|
|
$.statement,
|
2023-12-11 15:18:46 +00:00
|
|
|
optional(','),
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
),
|
2023-12-06 18:48:38 +00:00
|
|
|
'}',
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
while: $ =>
|
|
|
|
seq(
|
|
|
|
'while',
|
2023-11-14 23:56:44 +00:00
|
|
|
$.expression,
|
2023-11-30 00:23:42 +00:00
|
|
|
$.block,
|
|
|
|
),
|
|
|
|
|
|
|
|
for: $ =>
|
|
|
|
seq(
|
|
|
|
choice('for', 'async for'),
|
|
|
|
$.identifier,
|
|
|
|
'in',
|
2023-10-31 05:09:29 +00:00
|
|
|
$.expression,
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
return: $ =>
|
2023-12-02 03:54:25 +00:00
|
|
|
prec.right(
|
2023-12-31 19:04:10 +00:00
|
|
|
seq('return', $.statement),
|
2023-12-02 03:54:25 +00:00
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
|
|
|
|
type_definition: $ =>
|
|
|
|
seq('<', $.type, '>'),
|
|
|
|
|
|
|
|
type: $ =>
|
|
|
|
prec.right(
|
|
|
|
choice(
|
|
|
|
'any',
|
|
|
|
'bool',
|
2024-01-01 10:20:11 +00:00
|
|
|
'collection',
|
2023-11-30 14:30:25 +00:00
|
|
|
'float',
|
2023-12-22 20:02:22 +00:00
|
|
|
'int',
|
|
|
|
'map',
|
2023-12-26 22:19:12 +00:00
|
|
|
'none',
|
2023-12-22 20:02:22 +00:00
|
|
|
'num',
|
|
|
|
'str',
|
|
|
|
seq('[', $.type, ']'),
|
2024-01-04 00:57:06 +00:00
|
|
|
seq(
|
|
|
|
'{',
|
|
|
|
$.identifier,
|
|
|
|
$.type_definition,
|
|
|
|
'}',
|
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
seq(
|
2023-12-05 21:42:11 +00:00
|
|
|
'(',
|
2023-11-30 00:23:42 +00:00
|
|
|
repeat(
|
|
|
|
seq(
|
|
|
|
$.type,
|
|
|
|
optional(','),
|
|
|
|
),
|
|
|
|
),
|
2023-12-05 21:42:11 +00:00
|
|
|
')',
|
2023-11-30 00:23:42 +00:00
|
|
|
optional(seq('->', $.type)),
|
|
|
|
),
|
2023-12-22 20:02:22 +00:00
|
|
|
seq(
|
|
|
|
'option',
|
|
|
|
'(',
|
|
|
|
$.type,
|
|
|
|
')',
|
2023-12-30 01:14:03 +00:00
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
2023-11-04 10:02:27 +00:00
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
|
|
|
|
function: $ =>
|
2023-10-31 22:18:39 +00:00
|
|
|
seq(
|
2023-12-12 23:21:16 +00:00
|
|
|
'(',
|
2023-12-02 07:34:23 +00:00
|
|
|
repeat(
|
|
|
|
seq(
|
|
|
|
$.identifier,
|
2023-12-12 23:21:16 +00:00
|
|
|
$.type_definition,
|
2023-12-02 07:34:23 +00:00
|
|
|
optional(','),
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
),
|
2023-12-12 23:21:16 +00:00
|
|
|
')',
|
|
|
|
$.type_definition,
|
2023-12-02 07:34:23 +00:00
|
|
|
$.block,
|
2023-10-31 22:18:39 +00:00
|
|
|
),
|
2023-12-30 01:14:03 +00:00
|
|
|
|
2023-12-29 23:59:15 +00:00
|
|
|
function_expression: $ =>
|
2024-01-01 02:46:45 +00:00
|
|
|
choice(
|
|
|
|
$._function_expression_kind,
|
|
|
|
seq(
|
|
|
|
'(',
|
|
|
|
$._function_expression_kind,
|
|
|
|
')',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
_function_expression_kind: $ =>
|
2023-12-30 01:14:03 +00:00
|
|
|
prec(
|
2024-01-01 04:38:09 +00:00
|
|
|
2,
|
2023-12-30 01:14:03 +00:00
|
|
|
choice(
|
|
|
|
$.function_call,
|
|
|
|
$.identifier,
|
|
|
|
$.index,
|
2024-01-01 09:59:27 +00:00
|
|
|
$.value,
|
2023-12-30 02:15:03 +00:00
|
|
|
$.yield,
|
2023-12-30 01:14:03 +00:00
|
|
|
),
|
2023-12-29 23:59:15 +00:00
|
|
|
),
|
2024-01-01 04:38:09 +00:00
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
function_call: $ =>
|
|
|
|
prec.right(
|
|
|
|
seq(
|
2023-12-29 23:59:15 +00:00
|
|
|
$.function_expression,
|
2023-12-30 01:14:03 +00:00
|
|
|
'(',
|
2023-11-30 00:23:42 +00:00
|
|
|
optional($._expression_list),
|
|
|
|
')',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
yield: $ =>
|
|
|
|
prec.left(
|
2023-12-30 02:15:03 +00:00
|
|
|
1,
|
2023-11-30 00:23:42 +00:00
|
|
|
seq(
|
|
|
|
$.expression,
|
|
|
|
'->',
|
2023-12-29 23:59:15 +00:00
|
|
|
$.function_expression,
|
2023-12-30 02:15:03 +00:00
|
|
|
optional(
|
|
|
|
seq(
|
|
|
|
'(',
|
|
|
|
$._expression_list,
|
|
|
|
')',
|
|
|
|
),
|
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
2023-11-27 15:27:44 +00:00
|
|
|
),
|
2023-12-11 16:17:37 +00:00
|
|
|
|
2024-01-01 09:59:27 +00:00
|
|
|
built_in_value: $ =>
|
2023-12-11 16:17:37 +00:00
|
|
|
choice(
|
2024-01-01 09:59:27 +00:00
|
|
|
'args',
|
2023-12-17 02:15:36 +00:00
|
|
|
'assert_equal',
|
2024-01-01 09:59:27 +00:00
|
|
|
'env',
|
2024-01-01 12:46:47 +00:00
|
|
|
'fs',
|
|
|
|
'json',
|
2023-12-17 02:15:36 +00:00
|
|
|
'length',
|
|
|
|
'output',
|
|
|
|
'random',
|
2024-01-01 13:52:25 +00:00
|
|
|
'string',
|
2023-12-11 16:17:37 +00:00
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
},
|
2023-11-28 17:24:17 +00:00
|
|
|
});
|