2023-10-18 21:52:56 +00:00
|
|
|
module.exports = grammar({
|
|
|
|
name: 'dust',
|
|
|
|
|
|
|
|
word: $ => $.identifier,
|
|
|
|
|
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
|
|
|
block: $ =>
|
|
|
|
seq(
|
|
|
|
optional('async'),
|
|
|
|
'{',
|
|
|
|
repeat1($.statement),
|
|
|
|
'}',
|
|
|
|
),
|
2023-10-31 05:09:29 +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,
|
|
|
|
$.use,
|
|
|
|
$.while,
|
|
|
|
),
|
|
|
|
optional(';'),
|
|
|
|
),
|
2023-10-31 20:25:13 +00:00
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
|
|
|
|
expression: $ =>
|
|
|
|
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
|
|
|
|
|
|
|
identifier: $ =>
|
|
|
|
/[_a-zA-Z]+[_a-zA-Z0-9]?/,
|
|
|
|
|
|
|
|
value: $ =>
|
|
|
|
choice(
|
|
|
|
$.integer,
|
|
|
|
$.float,
|
2023-12-02 03:54:25 +00:00
|
|
|
$.function,
|
2023-11-30 00:23:42 +00:00
|
|
|
$.string,
|
|
|
|
$.boolean,
|
|
|
|
$.list,
|
|
|
|
$.map,
|
|
|
|
),
|
|
|
|
|
|
|
|
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(
|
|
|
|
'{',
|
|
|
|
repeat(
|
|
|
|
seq(
|
|
|
|
$.identifier,
|
|
|
|
'=',
|
|
|
|
$.statement,
|
|
|
|
optional(','),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'}',
|
|
|
|
),
|
|
|
|
|
|
|
|
index: $ =>
|
|
|
|
prec.left(
|
|
|
|
1,
|
|
|
|
seq(
|
|
|
|
$.expression,
|
|
|
|
':',
|
|
|
|
$.expression,
|
|
|
|
optional(
|
|
|
|
seq('..', $.expression),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
math: $ =>
|
|
|
|
prec.left(
|
2023-11-30 16:05:09 +00:00
|
|
|
choice(
|
|
|
|
seq(
|
|
|
|
$.expression,
|
|
|
|
$.math_operator,
|
|
|
|
$.expression,
|
|
|
|
),
|
|
|
|
seq(
|
|
|
|
'(',
|
|
|
|
$.expression,
|
|
|
|
$.math_operator,
|
|
|
|
$.expression,
|
|
|
|
')',
|
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
math_operator: $ =>
|
|
|
|
choice('+', '-', '*', '/', '%'),
|
|
|
|
|
|
|
|
logic: $ =>
|
|
|
|
prec.right(
|
2023-11-30 16:05:09 +00:00
|
|
|
choice(
|
|
|
|
seq(
|
|
|
|
$.expression,
|
|
|
|
$.logic_operator,
|
|
|
|
$.expression,
|
|
|
|
),
|
|
|
|
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,
|
|
|
|
repeat1(
|
|
|
|
seq(
|
|
|
|
$.expression,
|
|
|
|
'=>',
|
|
|
|
$.block,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
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(
|
|
|
|
seq('return', $.expression),
|
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
|
|
|
|
use: $ => seq('use', $.string),
|
|
|
|
|
|
|
|
type_definition: $ =>
|
|
|
|
seq('<', $.type, '>'),
|
|
|
|
|
|
|
|
type: $ =>
|
|
|
|
prec.right(
|
|
|
|
choice(
|
|
|
|
'any',
|
|
|
|
'bool',
|
2023-11-30 14:30:25 +00:00
|
|
|
'float',
|
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)),
|
|
|
|
),
|
|
|
|
'int',
|
2023-11-30 14:48:56 +00:00
|
|
|
seq('[', $.type, ']'),
|
2023-11-30 00:23:42 +00:00
|
|
|
'map',
|
|
|
|
'num',
|
|
|
|
'str',
|
|
|
|
),
|
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-05 21:42:11 +00:00
|
|
|
'fn',
|
2023-12-02 07:34:23 +00:00
|
|
|
'|',
|
|
|
|
repeat(
|
|
|
|
seq(
|
|
|
|
$.identifier,
|
|
|
|
optional(','),
|
2023-11-30 00:23:42 +00:00
|
|
|
),
|
|
|
|
),
|
2023-12-02 07:34:23 +00:00
|
|
|
'|',
|
|
|
|
$.block,
|
2023-10-31 22:18:39 +00:00
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
|
|
|
|
function_call: $ =>
|
|
|
|
prec.right(
|
|
|
|
seq(
|
|
|
|
'(',
|
2023-12-02 07:34:23 +00:00
|
|
|
$.expression,
|
2023-11-30 00:23:42 +00:00
|
|
|
optional($._expression_list),
|
|
|
|
')',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
yield: $ =>
|
|
|
|
prec.left(
|
|
|
|
seq(
|
|
|
|
$.expression,
|
|
|
|
'->',
|
|
|
|
'(',
|
2023-12-02 07:34:23 +00:00
|
|
|
$.expression,
|
2023-11-30 00:23:42 +00:00
|
|
|
optional($._expression_list),
|
|
|
|
')',
|
|
|
|
),
|
2023-11-27 15:27:44 +00:00
|
|
|
),
|
2023-11-30 00:23:42 +00:00
|
|
|
},
|
2023-11-28 17:24:17 +00:00
|
|
|
});
|