tree-sitter-dust/grammar.js

269 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-08-22 21:02:40 +00:00
module.exports = grammar({
2023-08-24 17:01:12 +00:00
name: 'dust',
2023-08-22 21:02:40 +00:00
2023-09-29 13:17:38 +00:00
word: $ => $.identifier,
2023-08-22 21:02:40 +00:00
rules: {
2023-09-29 11:17:47 +00:00
root: $ => repeat1($.item),
2023-10-06 02:05:18 +00:00
2023-10-09 18:39:47 +00:00
item: $ => prec.left(repeat1($.statement)),
2023-09-29 02:55:47 +00:00
2023-10-06 10:18:02 +00:00
statement: $ => choice(
2023-10-09 18:39:47 +00:00
$.comment,
2023-10-06 02:05:18 +00:00
$.assignment,
2023-10-01 02:59:27 +00:00
$.expression,
2023-10-06 10:18:02 +00:00
$.if_else,
2023-10-06 02:05:18 +00:00
$.insert,
$.select,
2023-10-07 02:45:27 +00:00
$.while,
2023-10-16 20:36:07 +00:00
$.async,
2023-10-17 17:46:10 +00:00
$.for,
2023-10-17 20:39:54 +00:00
$.transform,
2023-10-17 21:52:23 +00:00
$.filter,
2023-10-06 10:18:02 +00:00
),
2023-10-06 02:05:18 +00:00
2023-10-09 18:39:47 +00:00
comment: $ => seq(/[#]+.*/),
2023-09-27 23:10:21 +00:00
expression: $ => choice(
$._expression_kind,
seq('(', $._expression_kind, ')'),
),
_expression_kind: $ => prec.right(choice(
2023-09-29 12:59:27 +00:00
$.value,
$.identifier,
2023-10-01 01:12:35 +00:00
$.function_call,
$.math,
$.logic,
2023-10-06 20:19:17 +00:00
)),
2023-09-27 21:41:39 +00:00
2023-09-30 19:50:20 +00:00
identifier: $ => /[a-z|_|.]+[0-9]?/,
2023-08-24 14:12:08 +00:00
2023-10-02 22:54:49 +00:00
value: $ => choice(
2023-08-22 22:28:34 +00:00
$.integer,
2023-08-24 13:31:26 +00:00
$.float,
2023-08-22 22:28:34 +00:00
$.string,
2023-08-24 16:53:42 +00:00
$.boolean,
2023-10-05 12:03:37 +00:00
$.list,
2023-09-22 10:38:25 +00:00
$.function,
2023-09-29 18:32:18 +00:00
$.table,
2023-09-29 19:25:10 +00:00
$.map,
2023-10-02 22:54:49 +00:00
),
2023-08-24 14:12:08 +00:00
2023-10-06 10:18:02 +00:00
integer: $ => /[-]?[0-9]+/,
2023-08-22 22:28:34 +00:00
2023-10-06 10:18:02 +00:00
float: $ => /[-]?[0-9]+[.]{1}[0-9]*/,
2023-10-06 02:05:18 +00:00
2023-10-05 20:49:03 +00:00
string: $ => /("[^"]*?")|('[^']*?')|(`[^`]*?`)/,
2023-08-22 22:28:34 +00:00
2023-08-24 16:53:42 +00:00
boolean: $ => choice(
2023-09-27 21:41:39 +00:00
'true',
'false',
2023-08-24 16:53:42 +00:00
),
2023-08-22 22:28:34 +00:00
list: $ => seq(
2023-09-29 20:34:20 +00:00
'[',
2023-10-09 18:39:47 +00:00
repeat(seq($.expression, optional(','))),
2023-09-29 20:34:20 +00:00
']'
2023-08-22 22:28:34 +00:00
),
2023-09-27 21:41:39 +00:00
2023-09-29 18:32:18 +00:00
function: $ => seq(
'function',
optional(seq('<', repeat(seq($.identifier, optional(','))), '>')),
'{',
2023-10-09 18:39:47 +00:00
$.item,
2023-09-29 18:32:18 +00:00
'}',
),
table: $ => seq(
'table',
2023-09-29 19:25:10 +00:00
seq('<', repeat1(seq($.identifier, optional(','))), '>'),
2023-09-29 18:32:18 +00:00
'{',
2023-10-09 18:39:47 +00:00
repeat($.expression),
2023-09-29 19:25:10 +00:00
'}',
),
map: $ => seq(
'{',
2023-10-09 18:39:47 +00:00
repeat(seq($.identifier, "=", $.expression)),
2023-09-29 18:32:18 +00:00
'}',
),
2023-10-06 02:05:18 +00:00
math: $ => prec.left(seq(
2023-10-01 01:12:35 +00:00
$.expression,
2023-10-06 20:19:17 +00:00
$.math_operator,
2023-10-01 01:12:35 +00:00
$.expression,
)),
math_operator: $ => choice(
2023-09-27 21:41:39 +00:00
'+',
'-',
2023-09-30 19:47:21 +00:00
'*',
'/',
'%',
2023-10-01 01:12:35 +00:00
),
2023-10-06 20:19:17 +00:00
logic: $ => prec.right(seq(
2023-10-01 01:12:35 +00:00
$.expression,
$.logic_operator,
2023-10-01 01:12:35 +00:00
$.expression,
)),
logic_operator: $ => choice(
2023-09-29 13:17:38 +00:00
'==',
2023-10-06 02:05:18 +00:00
'!=',
2023-09-30 19:47:21 +00:00
'&&',
'||',
2023-10-09 18:39:47 +00:00
'>',
'<',
">=",
"<=",
2023-09-30 20:06:01 +00:00
),
2023-09-28 20:57:30 +00:00
2023-10-01 01:12:35 +00:00
assignment: $ => prec.right(seq(
$.identifier,
2023-10-09 18:39:47 +00:00
$.assignment_operator,
2023-10-06 10:18:02 +00:00
$.statement,
2023-09-28 20:57:30 +00:00
)),
2023-09-29 07:52:21 +00:00
2023-10-09 18:39:47 +00:00
assignment_operator: $ => choice(
"=",
"+=",
"-=",
),
2023-09-30 20:06:01 +00:00
2023-10-09 18:39:47 +00:00
if_else: $ => prec.left(seq(
$.if,
repeat(seq($.else_if)),
optional(seq($.else)),
2023-09-30 20:17:09 +00:00
)),
2023-10-09 18:39:47 +00:00
if: $ => seq(
2023-09-29 07:52:21 +00:00
'if',
2023-10-06 02:05:18 +00:00
$.expression,
2023-10-09 18:39:47 +00:00
'{',
2023-10-06 02:05:18 +00:00
$.statement,
2023-10-09 18:39:47 +00:00
'}',
),
else_if: $ => seq(
'else if',
$.expression,
'{',
$.statement,
'}',
),
else: $ => seq(
'else',
'{',
$.statement,
'}',
),
2023-09-29 13:53:53 +00:00
2023-10-01 01:12:35 +00:00
function_call: $ => prec.right(seq(
2023-10-06 12:16:34 +00:00
'(',
2023-10-09 18:39:47 +00:00
choice($.identifier, $.tool),
2023-10-06 17:32:49 +00:00
repeat(seq($.expression, optional(','))),
2023-10-06 10:18:02 +00:00
')',
2023-10-01 01:12:35 +00:00
)),
2023-10-01 05:13:29 +00:00
2023-10-06 10:18:02 +00:00
while: $ => seq(
2023-10-01 05:13:29 +00:00
'while',
$.expression,
'{',
2023-10-07 02:51:04 +00:00
$.item,
2023-10-01 05:13:29 +00:00
'}',
),
2023-10-09 18:39:47 +00:00
for: $ => seq(
'for',
$.identifier,
'in',
$.expression,
2023-10-01 05:13:29 +00:00
'{',
2023-10-09 18:39:47 +00:00
$.item,
2023-10-06 10:18:02 +00:00
'}',
2023-10-01 05:13:29 +00:00
),
2023-10-17 20:39:54 +00:00
transform: $ => seq(
'transform',
$.identifier,
'in',
$.expression,
'{',
$.item,
'}',
),
2023-10-17 21:52:23 +00:00
filter: $ => seq(
'filter',
$.identifier,
'in',
$.expression,
'{',
$.item,
'}',
),
2023-10-09 18:39:47 +00:00
tool: $ => choice(
2023-10-11 16:07:21 +00:00
'assert',
'assert_equal',
2023-10-10 19:50:57 +00:00
'output',
2023-10-13 19:29:17 +00:00
2023-10-09 18:39:47 +00:00
'read',
2023-10-13 19:29:17 +00:00
'write',
'raw',
'sh',
'bash',
'fish',
'zsh',
2023-10-13 17:52:49 +00:00
'random',
2023-10-13 19:29:17 +00:00
'random_boolean',
'random_float',
'random_string',
2023-10-13 17:52:49 +00:00
'random_integer',
2023-10-13 19:29:17 +00:00
2023-10-17 16:42:44 +00:00
'length',
2023-10-13 19:29:17 +00:00
'sort',
'transform',
'filter',
'to_csv',
'from_csv',
'to_json',
'from_json',
2023-10-13 17:52:49 +00:00
'help',
2023-10-09 18:39:47 +00:00
),
2023-10-06 17:32:49 +00:00
2023-10-09 18:39:47 +00:00
select: $ => prec.right(seq(
'select',
$.identifier,
'from',
$.identifier,
optional(
seq('where', $.expression)
),
2023-10-06 17:32:49 +00:00
)),
2023-10-09 18:39:47 +00:00
insert: $ => prec.right(seq(
'insert',
repeat1($.list),
'into',
$.identifier,
optional(
seq('where', $.logic)
),
)),
2023-10-16 20:36:07 +00:00
2023-10-17 01:13:39 +00:00
async: $ => seq(
'async',
'{',
repeat($.statement),
'}'
),
2023-08-22 21:02:40 +00:00
}
2023-10-16 20:36:07 +00:00
});