1
0
dust/tree-sitter-dust/grammar.js

355 lines
5.8 KiB
JavaScript
Raw Normal View History

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: $ => [
2023-10-31 22:18:39 +00:00
[$.block],
2023-10-31 17:04:22 +00:00
],
rules: {
2023-11-03 23:39:34 +00:00
root: $ => $.block,
2023-10-31 07:17:58 +00:00
comment: $ => /[#][^#\n]*[#|\n]/,
2023-11-03 23:39:34 +00:00
block: $ => prec.right(choice(
2023-10-31 13:31:10 +00:00
repeat1($.statement),
seq('{', repeat1($.statement), '}'),
2023-11-03 23:39:34 +00:00
)),
2023-10-31 05:09:29 +00:00
2023-10-31 20:25:13 +00:00
statement: $ => prec.right(seq(
choice(
$.assignment,
2023-11-04 01:16:55 +00:00
$.await,
2023-10-31 20:25:13 +00:00
$.expression,
$.filter,
$.find,
$.for,
$.if_else,
$.insert,
$.match,
$.reduce,
$.remove,
$.select,
$.transform,
$.while,
),
optional(';'),
2023-10-29 23:31:06 +00:00
)),
2023-10-31 20:25:13 +00:00
expression: $ => prec.right(choice(
$._expression_kind,
seq('(', $._expression_kind, ')'),
2023-10-31 17:04:22 +00:00
)),
2023-11-04 01:16:55 +00:00
_expression_kind: $ => prec.right(1, choice(
2023-10-31 05:09:29 +00:00
$.function_call,
$.identifier,
2023-10-29 23:31:06 +00:00
$.index,
$.logic,
2023-10-31 05:09:29 +00:00
$.math,
$.value,
2023-10-31 07:17:58 +00:00
)),
2023-10-31 20:25:13 +00:00
_expression_list: $ => repeat1(prec.right(seq($.expression, optional(',')))),
2023-10-29 23:31:06 +00:00
identifier: $ => /[_a-zA-Z]+[_a-zA-Z0-9]?/,
value: $ => choice(
$.integer,
$.float,
$.string,
$.boolean,
$.list,
$.function,
$.table,
$.map,
2023-11-03 23:39:34 +00:00
$.future,
),
2023-10-31 19:21:13 +00:00
integer: $ => token(prec.left(seq(
2023-10-31 09:51:37 +00:00
optional('-'),
repeat1(
choice('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')
),
))),
2023-10-31 19:21:13 +00:00
float: $ => token(prec.left(seq(
2023-10-31 09:51:37 +00:00
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(
'[',
2023-11-04 01:16:55 +00:00
repeat(prec.right(seq($.expression, optional(',')))),
2023-10-28 14:28:43 +00:00
']',
),
2023-10-29 23:31:06 +00:00
map: $ => seq(
2023-11-04 01:16:55 +00:00
'map',
$.block,
2023-10-29 23:31:06 +00:00
),
2023-11-03 23:39:34 +00:00
future: $ => seq(
'async',
$.block,
),
2023-11-04 01:16:55 +00:00
await: $ => seq(
'await',
'{',
$._expression_list,
'}',
),
2023-10-31 17:04:22 +00:00
2023-10-29 23:31:06 +00:00
index: $ => prec.left(seq(
$.expression,
':',
2023-10-29 23:31:06 +00:00
$.expression,
optional(seq(
'..',
$.expression,
)),
)),
math: $ => prec.left(seq(
$.expression,
$.math_operator,
$.expression,
)),
2023-10-31 13:31:10 +00:00
math_operator: $ => choice(
'+',
'-',
'*',
'/',
'%',
2023-10-31 13:31:10 +00:00
),
logic: $ => prec.right(seq(
$.expression,
$.logic_operator,
$.expression,
)),
2023-10-31 13:31:10 +00:00
logic_operator: $ => choice(
'==',
'!=',
'&&',
'||',
'>',
'<',
">=",
"<=",
2023-10-31 13:31:10 +00:00
),
2023-10-31 13:31:10 +00:00
assignment: $ => seq(
$.identifier,
$.assignment_operator,
$.statement,
2023-10-31 13:31:10 +00:00
),
2023-10-31 13:31:10 +00:00
assignment_operator: $ => choice(
"=",
"+=",
"-=",
2023-10-31 13:31:10 +00:00
),
2023-10-31 20:25:13 +00:00
if_else: $ => prec.right(seq(
$.if,
2023-10-31 17:04:22 +00:00
repeat($.else_if),
optional($.else),
)),
2023-10-31 20:25:13 +00:00
if: $ => seq(
'if',
$.expression,
2023-10-31 13:31:10 +00:00
$.block,
2023-10-31 20:25:13 +00:00
),
2023-10-31 20:25:13 +00:00
else_if: $ => seq(
'else if',
$.expression,
2023-10-31 13:31:10 +00:00
$.block,
2023-10-31 20:25:13 +00:00
),
2023-10-31 20:25:13 +00:00
else: $ => seq(
'else',
2023-10-31 13:31:10 +00:00
$.block,
2023-10-31 20:25:13 +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
while: $ => seq(
'while',
$.expression,
2023-10-31 13:31:10 +00:00
$.block,
),
for: $ => seq(
'for',
$.identifier,
'in',
$.expression,
2023-10-31 13:31:10 +00:00
$.block,
),
transform: $ => seq(
'transform',
$.identifier,
'in',
$.expression,
2023-10-31 13:31:10 +00:00
$.block,
),
filter: $ => seq(
'filter',
2023-10-31 05:09:29 +00:00
field('count', optional($.expression)),
field('statement_id', $.identifier),
'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 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
),
select: $ => prec.right(seq(
'select',
2023-10-31 22:18:39 +00:00
$.identifier_list,
'from',
2023-10-21 17:04:17 +00:00
$.expression,
2023-10-31 13:31:10 +00:00
optional($.block),
)),
insert: $ => prec.right(seq(
'insert',
'into',
$.identifier,
$.expression,
)),
2023-10-31 22:18:39 +00:00
identifier_list: $ => prec.right(choice(
seq(
'|',
repeat(seq($.identifier, optional(','))),
'|',
),
)),
table: $ => prec.right(seq(
'table',
$.identifier_list,
$.expression,
)),
2023-10-31 19:21:13 +00:00
function: $ => seq(
2023-10-31 22:18:39 +00:00
field('parameters', optional($.identifier_list)),
'=>',
field('body', $.block),
2023-10-31 19:21:13 +00:00
),
function_call: $ => choice(
$.built_in_function,
$._context_defined_function,
),
_context_defined_function: $ => prec.right(seq(
$.identifier,
2023-10-31 20:25:13 +00:00
optional($._expression_list),
2023-10-31 19:21:13 +00:00
)),
2023-10-21 17:19:01 +00:00
2023-10-31 19:21:13 +00:00
built_in_function: $ => prec.right(seq(
$._built_in_function_name,
2023-10-31 20:25:13 +00:00
optional($._expression_list),
2023-10-31 19:21:13 +00:00
)),
_built_in_function_name: $ => 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-21 20:38:20 +00:00
// Filesystem
'append',
'metadata',
'move',
2023-10-21 17:19:01 +00:00
'read',
2023-10-31 19:21:13 +00:00
'workdir',
2023-10-21 17:19:01 +00:00
'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
),
}
});