2023-10-18 21:52:56 +00:00
|
|
|
module.exports = grammar({
|
|
|
|
name: 'dust',
|
|
|
|
|
|
|
|
word: $ => $.identifier,
|
|
|
|
|
2023-10-31 07:17:58 +00:00
|
|
|
extras: $ => [ /\s/, $.comment ],
|
|
|
|
|
2023-10-31 17:04:22 +00:00
|
|
|
conflicts: $ => [
|
|
|
|
[$.map, $.assignment_operator],
|
|
|
|
],
|
|
|
|
|
2023-10-18 21:52:56 +00:00
|
|
|
rules: {
|
2023-10-31 13:31:10 +00:00
|
|
|
root: $ => repeat1($.block),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-31 07:17:58 +00:00
|
|
|
comment: $ => /[#][^#\n]*[#|\n]/,
|
|
|
|
|
2023-10-31 13:31:10 +00:00
|
|
|
block: $ => prec.right(choice(
|
|
|
|
repeat1($.statement),
|
|
|
|
seq('{', repeat1($.statement), '}'),
|
2023-10-31 07:17:58 +00:00
|
|
|
)),
|
2023-10-31 05:09:29 +00:00
|
|
|
|
2023-10-31 17:04:22 +00:00
|
|
|
statement: $ => prec.right(seq(
|
|
|
|
$._statement_kind,
|
|
|
|
optional(';'),
|
|
|
|
)),
|
|
|
|
|
|
|
|
_statement_kind: $ => prec.right(choice(
|
2023-10-18 21:52:56 +00:00
|
|
|
$.assignment,
|
2023-10-31 05:09:29 +00:00
|
|
|
$.async,
|
2023-10-18 21:52:56 +00:00
|
|
|
$.expression,
|
2023-10-31 05:09:29 +00:00
|
|
|
$.filter,
|
|
|
|
$.find,
|
|
|
|
$.for,
|
2023-10-18 21:52:56 +00:00
|
|
|
$.if_else,
|
|
|
|
$.insert,
|
2023-10-31 05:09:29 +00:00
|
|
|
$.match,
|
|
|
|
$.reduce,
|
|
|
|
$.remove,
|
2023-10-18 21:52:56 +00:00
|
|
|
$.select,
|
|
|
|
$.transform,
|
2023-10-31 05:09:29 +00:00
|
|
|
$.while,
|
2023-10-29 23:31:06 +00:00
|
|
|
)),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-31 17:04:22 +00:00
|
|
|
expression: $ => prec.left(choice(
|
2023-10-18 21:52:56 +00:00
|
|
|
$._expression_kind,
|
|
|
|
seq('(', $._expression_kind, ')'),
|
2023-10-31 17:04:22 +00:00
|
|
|
)),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-31 17:04:22 +00:00
|
|
|
_expression_kind: $ => prec.left(1, choice(
|
2023-10-31 05:09:29 +00:00
|
|
|
$.function_call,
|
2023-10-18 21:52:56 +00:00
|
|
|
$.identifier,
|
2023-10-29 23:31:06 +00:00
|
|
|
$.index,
|
2023-10-18 21:52:56 +00:00
|
|
|
$.logic,
|
2023-10-31 05:09:29 +00:00
|
|
|
$.math,
|
|
|
|
$.value,
|
2023-10-31 07:17:58 +00:00
|
|
|
)),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-29 23:31:06 +00:00
|
|
|
identifier: $ => /[_a-zA-Z]+[_a-zA-Z0-9]?/,
|
2023-10-18 21:52:56 +00:00
|
|
|
|
|
|
|
value: $ => choice(
|
|
|
|
$.integer,
|
|
|
|
$.float,
|
|
|
|
$.string,
|
|
|
|
$.boolean,
|
|
|
|
$.list,
|
|
|
|
$.function,
|
|
|
|
$.table,
|
|
|
|
$.map,
|
|
|
|
),
|
|
|
|
|
2023-10-31 09:51:37 +00:00
|
|
|
integer: $ => prec.left(token(seq(
|
|
|
|
optional('-'),
|
|
|
|
repeat1(
|
|
|
|
choice('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')
|
|
|
|
),
|
|
|
|
))),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-31 09:51:37 +00:00
|
|
|
float: $ => prec.left(token(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')),
|
|
|
|
))),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
|
|
|
string: $ => /("[^"]*?")|('[^']*?')|(`[^`]*?`)/,
|
|
|
|
|
|
|
|
boolean: $ => choice(
|
|
|
|
'true',
|
|
|
|
'false',
|
|
|
|
),
|
|
|
|
|
|
|
|
list: $ => seq(
|
|
|
|
'[',
|
2023-10-31 09:51:37 +00:00
|
|
|
repeat(prec.left(seq($.expression, optional(',')))),
|
2023-10-28 14:28:43 +00:00
|
|
|
']',
|
2023-10-18 21:52:56 +00:00
|
|
|
),
|
|
|
|
|
2023-10-29 23:31:06 +00:00
|
|
|
map: $ => seq(
|
|
|
|
'{',
|
2023-10-31 17:04:22 +00:00
|
|
|
repeat(seq(
|
2023-10-31 09:51:37 +00:00
|
|
|
$.identifier,
|
2023-10-31 17:04:22 +00:00
|
|
|
"=",
|
|
|
|
$.statement,
|
2023-10-31 09:51:37 +00:00
|
|
|
optional(',')
|
2023-10-31 17:04:22 +00:00
|
|
|
)),
|
2023-10-29 23:31:06 +00:00
|
|
|
'}',
|
|
|
|
),
|
2023-10-31 17:04:22 +00:00
|
|
|
|
2023-10-29 23:31:06 +00:00
|
|
|
index: $ => prec.left(seq(
|
|
|
|
$.expression,
|
2023-10-30 21:11:06 +00:00
|
|
|
':',
|
2023-10-29 23:31:06 +00:00
|
|
|
$.expression,
|
|
|
|
optional(seq(
|
|
|
|
'..',
|
|
|
|
$.expression,
|
|
|
|
)),
|
|
|
|
)),
|
|
|
|
|
2023-10-18 21:52:56 +00:00
|
|
|
function: $ => seq(
|
|
|
|
'function',
|
|
|
|
optional(seq('<', repeat(seq($.identifier, optional(','))), '>')),
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-18 21:52:56 +00:00
|
|
|
),
|
|
|
|
|
2023-10-30 21:11:06 +00:00
|
|
|
table: $ => prec.left(seq(
|
2023-10-18 21:52:56 +00:00
|
|
|
'table',
|
|
|
|
seq('<', repeat1(seq($.identifier, optional(','))), '>'),
|
2023-10-18 23:26:49 +00:00
|
|
|
$.expression,
|
|
|
|
)),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-30 21:11:06 +00:00
|
|
|
math: $ => prec.left(seq(
|
2023-10-18 21:52:56 +00:00
|
|
|
$.expression,
|
|
|
|
$.math_operator,
|
|
|
|
$.expression,
|
|
|
|
)),
|
|
|
|
|
2023-10-31 13:31:10 +00:00
|
|
|
math_operator: $ => choice(
|
2023-10-18 21:52:56 +00:00
|
|
|
'+',
|
|
|
|
'-',
|
|
|
|
'*',
|
|
|
|
'/',
|
|
|
|
'%',
|
2023-10-31 13:31:10 +00:00
|
|
|
),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-30 21:11:06 +00:00
|
|
|
logic: $ => prec.right(seq(
|
2023-10-18 21:52:56 +00:00
|
|
|
$.expression,
|
|
|
|
$.logic_operator,
|
|
|
|
$.expression,
|
|
|
|
)),
|
|
|
|
|
2023-10-31 13:31:10 +00:00
|
|
|
logic_operator: $ => choice(
|
2023-10-18 21:52:56 +00:00
|
|
|
'==',
|
|
|
|
'!=',
|
|
|
|
'&&',
|
|
|
|
'||',
|
|
|
|
'>',
|
|
|
|
'<',
|
|
|
|
">=",
|
|
|
|
"<=",
|
2023-10-31 13:31:10 +00:00
|
|
|
),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-31 13:31:10 +00:00
|
|
|
assignment: $ => seq(
|
2023-10-18 21:52:56 +00:00
|
|
|
$.identifier,
|
|
|
|
$.assignment_operator,
|
|
|
|
$.statement,
|
2023-10-31 13:31:10 +00:00
|
|
|
),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-31 13:31:10 +00:00
|
|
|
assignment_operator: $ => choice(
|
2023-10-18 21:52:56 +00:00
|
|
|
"=",
|
|
|
|
"+=",
|
|
|
|
"-=",
|
2023-10-31 13:31:10 +00:00
|
|
|
),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
|
|
|
if_else: $ => prec.left(seq(
|
|
|
|
$.if,
|
2023-10-31 17:04:22 +00:00
|
|
|
repeat($.else_if),
|
2023-10-30 21:11:06 +00:00
|
|
|
optional($.else),
|
2023-10-18 21:52:56 +00:00
|
|
|
)),
|
|
|
|
|
2023-10-31 17:04:22 +00:00
|
|
|
if: $ => prec.left(seq(
|
2023-10-18 21:52:56 +00:00
|
|
|
'if',
|
|
|
|
$.expression,
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-31 17:04:22 +00:00
|
|
|
)),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-31 17:04:22 +00:00
|
|
|
else_if: $ => prec.left(seq(
|
2023-10-18 21:52:56 +00:00
|
|
|
'else if',
|
|
|
|
$.expression,
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-31 17:04:22 +00:00
|
|
|
)),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-31 17:04:22 +00:00
|
|
|
else: $ => prec.left(seq(
|
2023-10-18 21:52:56 +00:00
|
|
|
'else',
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-31 17:04:22 +00:00
|
|
|
)),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-31 17:04:22 +00:00
|
|
|
function_call: $ => prec.right(seq(
|
|
|
|
choice($.identifier, $.built_in_function),
|
|
|
|
repeat(prec.right(seq($.expression, optional(',')))),
|
|
|
|
)),
|
2023-10-18 21:52:56 +00:00
|
|
|
|
2023-10-31 17:04:22 +00:00
|
|
|
match: $ => prec.right(seq(
|
2023-10-31 05:09:29 +00:00
|
|
|
'match',
|
|
|
|
$.expression,
|
|
|
|
repeat1(seq(
|
|
|
|
$.expression,
|
|
|
|
'=>',
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-31 05:09:29 +00:00
|
|
|
)),
|
2023-10-31 17:04:22 +00:00
|
|
|
)),
|
2023-10-31 05:09:29 +00:00
|
|
|
|
2023-10-18 21:52:56 +00:00
|
|
|
while: $ => seq(
|
|
|
|
'while',
|
|
|
|
$.expression,
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-18 21:52:56 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
for: $ => seq(
|
2023-10-30 21:11:06 +00:00
|
|
|
'for',
|
2023-10-18 21:52:56 +00:00
|
|
|
$.identifier,
|
|
|
|
'in',
|
|
|
|
$.expression,
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-18 21:52:56 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
transform: $ => seq(
|
|
|
|
'transform',
|
|
|
|
$.identifier,
|
|
|
|
'in',
|
|
|
|
$.expression,
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-18 21:52:56 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
filter: $ => seq(
|
|
|
|
'filter',
|
2023-10-31 05:09:29 +00:00
|
|
|
field('count', optional($.expression)),
|
|
|
|
field('statement_id', $.identifier),
|
2023-10-18 21:52:56 +00:00
|
|
|
'in',
|
2023-10-31 05:09:29 +00:00
|
|
|
field('collection', $.expression),
|
2023-10-31 13:31:10 +00:00
|
|
|
field('predicate', $.block),
|
2023-10-18 21:52:56 +00:00
|
|
|
),
|
|
|
|
|
2023-10-18 22:18:41 +00:00
|
|
|
find: $ => seq(
|
|
|
|
'find',
|
|
|
|
$.identifier,
|
|
|
|
'in',
|
|
|
|
$.expression,
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-18 22:18:41 +00:00
|
|
|
),
|
|
|
|
|
2023-10-19 01:50:45 +00:00
|
|
|
remove: $ => seq(
|
|
|
|
'remove',
|
|
|
|
$.identifier,
|
2023-10-31 05:09:29 +00:00
|
|
|
'from',
|
|
|
|
$.expression,
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-31 05:09:29 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
reduce: $ => seq(
|
|
|
|
'reduce',
|
|
|
|
$.identifier,
|
|
|
|
'to',
|
|
|
|
$.identifier,
|
2023-10-19 01:50:45 +00:00
|
|
|
'in',
|
|
|
|
$.expression,
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-19 01:50:45 +00:00
|
|
|
),
|
|
|
|
|
2023-10-18 21:52:56 +00:00
|
|
|
select: $ => prec.right(seq(
|
|
|
|
'select',
|
2023-10-21 17:04:17 +00:00
|
|
|
'<',
|
|
|
|
repeat(seq($.identifier, optional(','))),
|
|
|
|
'>',
|
2023-10-18 21:52:56 +00:00
|
|
|
'from',
|
2023-10-21 17:04:17 +00:00
|
|
|
$.expression,
|
2023-10-31 13:31:10 +00:00
|
|
|
optional($.block),
|
2023-10-18 21:52:56 +00:00
|
|
|
)),
|
|
|
|
|
|
|
|
insert: $ => prec.right(seq(
|
|
|
|
'insert',
|
|
|
|
'into',
|
|
|
|
$.identifier,
|
2023-10-22 17:55:56 +00:00
|
|
|
$.expression,
|
2023-10-18 21:52:56 +00:00
|
|
|
)),
|
|
|
|
|
|
|
|
async: $ => seq(
|
|
|
|
'async',
|
2023-10-31 13:31:10 +00:00
|
|
|
$.block,
|
2023-10-18 21:52:56 +00:00
|
|
|
),
|
2023-10-21 17:19:01 +00:00
|
|
|
|
2023-10-31 17:04:22 +00:00
|
|
|
built_in_function: $ => choice(
|
2023-10-21 20:38:20 +00:00
|
|
|
// General
|
2023-10-21 17:19:01 +00:00
|
|
|
'assert',
|
|
|
|
'assert_equal',
|
2023-10-22 20:50:09 +00:00
|
|
|
'download',
|
2023-10-21 20:38:20 +00:00
|
|
|
'help',
|
2023-10-21 17:19:01 +00:00
|
|
|
'length',
|
2023-10-21 20:38:20 +00:00
|
|
|
'output',
|
2023-10-22 18:27:18 +00:00
|
|
|
'output_error',
|
|
|
|
'type',
|
2023-10-22 20:41:37 +00:00
|
|
|
'workdir',
|
2023-10-21 20:38:20 +00:00
|
|
|
|
|
|
|
// Filesystem
|
|
|
|
'append',
|
|
|
|
'metadata',
|
|
|
|
'move',
|
2023-10-21 17:19:01 +00:00
|
|
|
'read',
|
|
|
|
'write',
|
2023-10-21 20:38:20 +00:00
|
|
|
|
|
|
|
// Format conversion
|
|
|
|
'from_json',
|
|
|
|
'to_json',
|
|
|
|
'to_string',
|
2023-10-28 14:28:43 +00:00
|
|
|
'to_float',
|
2023-10-21 20:38:20 +00:00
|
|
|
|
|
|
|
// Command
|
|
|
|
'bash',
|
|
|
|
'fish',
|
|
|
|
'raw',
|
|
|
|
'sh',
|
|
|
|
'zsh',
|
2023-10-22 18:48:34 +00:00
|
|
|
|
|
|
|
// Random
|
|
|
|
'random',
|
|
|
|
'random_boolean',
|
|
|
|
'random_float',
|
|
|
|
'random_integer',
|
2023-10-22 20:32:55 +00:00
|
|
|
|
|
|
|
// Tables
|
|
|
|
'columns',
|
2023-10-22 20:33:27 +00:00
|
|
|
'rows',
|
2023-10-28 14:28:43 +00:00
|
|
|
|
|
|
|
// Lists
|
|
|
|
'reverse',
|
2023-10-21 17:19:01 +00:00
|
|
|
),
|
2023-10-18 21:52:56 +00:00
|
|
|
}
|
|
|
|
});
|