dust/tree-sitter-dust/grammar.js

333 lines
5.2 KiB
JavaScript
Raw Normal View History

module.exports = grammar({
name: 'dust',
word: $ => $.identifier,
rules: {
root: $ => repeat1($.item),
item: $ => prec.left(repeat1($.statement)),
2023-10-29 23:31:06 +00:00
statement: $ => prec.left(choice(
$.comment,
$.assignment,
$.expression,
$.if_else,
$.insert,
$.select,
$.while,
$.async,
$.for,
$.transform,
$.filter,
2023-10-18 22:18:41 +00:00
$.find,
2023-10-19 01:50:45 +00:00
$.remove,
2023-10-29 23:31:06 +00:00
)),
comment: $ => seq(/[#]+.*/),
expression: $ => choice(
$._expression_kind,
seq('(', $._expression_kind, ')'),
),
2023-10-29 23:31:06 +00:00
_expression_kind: $ => choice(
$.value,
$.identifier,
2023-10-29 23:31:06 +00:00
$.index,
$.math,
$.logic,
2023-10-29 23:31:06 +00:00
$.function_call,
$.tool,
),
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-10-29 23:31:06 +00:00
_numeric: $ => token(repeat1(
choice('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')
)),
integer: $ => prec.left(seq(
optional(token.immediate('-')),
$._numeric,
)),
2023-10-29 23:31:06 +00:00
float: $ => prec.left(seq(
optional(token.immediate('-')),
$._numeric,
token.immediate('.'),
$._numeric,
)),
string: $ => /("[^"]*?")|('[^']*?')|(`[^`]*?`)/,
boolean: $ => choice(
'true',
'false',
),
list: $ => seq(
'[',
repeat(seq($.expression, optional(','))),
2023-10-28 14:28:43 +00:00
']',
),
2023-10-29 23:31:06 +00:00
map: $ => seq(
'{',
repeat(seq($.identifier, "=", $.expression)),
'}',
),
index: $ => prec.left(seq(
$.expression,
'.',
'{',
$.expression,
optional(seq(
'..',
$.expression,
)),
'}'
)),
function: $ => seq(
'function',
optional(seq('<', repeat(seq($.identifier, optional(','))), '>')),
'{',
$.item,
'}',
),
2023-10-29 23:31:06 +00:00
table: $ => prec.right(seq(
'table',
seq('<', repeat1(seq($.identifier, optional(','))), '>'),
2023-10-18 23:26:49 +00:00
$.expression,
)),
2023-10-29 23:31:06 +00:00
math: $ => prec.left(1, seq(
$.expression,
$.math_operator,
$.expression,
)),
math_operator: $ => choice(
'+',
'-',
'*',
'/',
'%',
),
2023-10-29 23:31:06 +00:00
logic: $ => prec.right(1, seq(
$.expression,
$.logic_operator,
$.expression,
)),
logic_operator: $ => choice(
'==',
'!=',
'&&',
'||',
'>',
'<',
">=",
"<=",
),
assignment: $ => prec.right(seq(
$.identifier,
$.assignment_operator,
$.statement,
)),
assignment_operator: $ => choice(
"=",
"+=",
"-=",
),
if_else: $ => prec.left(seq(
$.if,
repeat(seq($.else_if)),
optional(seq($.else)),
)),
if: $ => seq(
'if',
$.expression,
'{',
$.statement,
'}',
),
else_if: $ => seq(
'else if',
$.expression,
'{',
$.statement,
'}',
),
else: $ => seq(
'else',
'{',
$.statement,
'}',
),
2023-10-29 23:31:06 +00:00
function_call: $ => prec(1, seq(
'(',
2023-10-21 17:04:17 +00:00
$.identifier,
repeat(seq($.expression, optional(','))),
')',
)),
while: $ => seq(
'while',
$.expression,
'{',
$.item,
'}',
),
for: $ => seq(
2023-10-28 14:28:43 +00:00
choice('for', 'async for'),
$.identifier,
'in',
$.expression,
'{',
$.item,
'}',
),
transform: $ => seq(
'transform',
$.identifier,
'in',
$.expression,
'{',
$.item,
'}',
),
filter: $ => seq(
'filter',
$.identifier,
'in',
$.expression,
'{',
$.item,
'}',
),
2023-10-18 22:18:41 +00:00
find: $ => seq(
'find',
$.identifier,
'in',
$.expression,
'{',
$.item,
'}',
),
2023-10-19 01:50:45 +00:00
remove: $ => seq(
'remove',
$.identifier,
'in',
$.expression,
'{',
$.item,
'}',
),
select: $ => prec.right(seq(
'select',
2023-10-21 17:04:17 +00:00
'<',
repeat(seq($.identifier, optional(','))),
'>',
'from',
2023-10-21 17:04:17 +00:00
$.expression,
optional(seq('{', $.item, '}')),
)),
insert: $ => prec.right(seq(
'insert',
'into',
$.identifier,
$.expression,
)),
async: $ => seq(
'async',
'{',
repeat($.statement),
'}'
),
2023-10-21 17:19:01 +00:00
tool: $ => prec.right(seq(
'(',
$._tool_kind,
repeat(seq($.expression, optional(','))),
')',
)),
_tool_kind: $ => 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',
2023-10-21 20:38:20 +00:00
'remove',
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
),
}
});