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

317 lines
4.9 KiB
JavaScript
Raw Normal View History

module.exports = grammar({
name: 'dust',
word: $ => $.identifier,
rules: {
root: $ => repeat1($.item),
item: $ => prec.left(repeat1($.statement)),
statement: $ => 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,
),
comment: $ => seq(/[#]+.*/),
expression: $ => choice(
$._expression_kind,
seq('(', $._expression_kind, ')'),
),
_expression_kind: $ => prec.right(choice(
$.value,
$.identifier,
$.function_call,
2023-10-21 17:19:01 +00:00
$.tool,
$.math,
$.logic,
2023-10-28 14:28:43 +00:00
$.sublist,
)),
2023-10-23 20:12:43 +00:00
identifier: $ => /[a-zA-Z|_]+[._a-zA-Z0-9]*/,
value: $ => choice(
$.integer,
$.float,
$.string,
$.boolean,
$.list,
$.function,
$.table,
$.map,
),
integer: $ => /[-]?[0-9]+/,
2023-10-28 14:28:43 +00:00
float: $ => /[-]?[0-9]+[.]{1}[0-9]+/,
string: $ => /("[^"]*?")|('[^']*?')|(`[^`]*?`)/,
boolean: $ => choice(
'true',
'false',
),
list: $ => seq(
'[',
repeat(seq($.expression, optional(','))),
2023-10-28 14:28:43 +00:00
']',
),
function: $ => seq(
'function',
optional(seq('<', repeat(seq($.identifier, optional(','))), '>')),
'{',
$.item,
'}',
),
2023-10-28 14:28:43 +00:00
table: $ => prec(2, seq(
'table',
seq('<', repeat1(seq($.identifier, optional(','))), '>'),
2023-10-18 23:26:49 +00:00
$.expression,
)),
map: $ => seq(
'{',
repeat(seq($.identifier, "=", $.expression)),
'}',
),
2023-10-28 14:28:43 +00:00
sublist: $ => prec.right(seq(
$.expression,
'.',
$.expression,
'..',
$.expression,
)),
math: $ => prec.left(seq(
$.expression,
$.math_operator,
$.expression,
)),
math_operator: $ => choice(
'+',
'-',
'*',
'/',
'%',
),
logic: $ => prec.right(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,
'}',
),
function_call: $ => prec.right(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
),
}
});