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

579 lines
9.8 KiB
JavaScript
Raw Permalink Normal View History

module.exports = grammar({
name: 'dust',
2024-01-01 09:59:27 +00:00
word: $ => $.identifier,
2023-11-30 00:23:42 +00:00
extras: $ => [/\s/, $._comment],
2023-10-31 07:17:58 +00:00
rules: {
2023-11-30 00:23:42 +00:00
root: $ =>
prec(1, repeat1($.statement)),
2023-11-04 10:02:27 +00:00
_comment: $ => /[#][^#\n]*[#|\n]/,
2023-10-31 07:17:58 +00:00
2023-11-30 00:23:42 +00:00
statement: $ =>
prec.left(
seq(
2024-02-16 15:55:15 +00:00
$.statement_kind,
2023-11-30 00:23:42 +00:00
optional(';'),
),
2023-10-31 20:25:13 +00:00
),
2023-11-30 00:23:42 +00:00
break: $ =>
prec.left(
seq(
'break',
optional($.statement),
),
),
return: $ =>
prec.left(
seq(
'return',
optional($.statement),
),
),
2024-02-16 15:55:15 +00:00
statement_kind: $ =>
prec.left(
choice(
$.assignment,
$.block,
$.break,
2024-02-16 15:55:15 +00:00
$.expression,
$.for,
$.if_else,
$.index_assignment,
2024-02-19 20:24:44 +00:00
$.loop_node,
2024-02-16 15:55:15 +00:00
$.match,
$.pipe,
$.return,
2024-02-16 15:55:15 +00:00
$.while,
$.type_definition,
),
),
2023-11-30 00:23:42 +00:00
expression: $ =>
choice(
$._expression_kind,
seq(
'(',
$._expression_kind,
')',
),
),
_expression_kind: $ =>
2023-11-30 00:23:42 +00:00
prec.right(
choice(
2024-02-19 20:24:44 +00:00
$.as_node,
2023-11-30 00:23:42 +00:00
$.function_call,
$.identifier,
$.index,
$.logic,
$.math,
$.value,
2024-01-25 12:10:45 +00:00
$.command,
2023-11-30 00:23:42 +00:00
),
),
_expression_list: $ =>
2023-10-31 09:51:37 +00:00
repeat1(
2023-11-30 00:23:42 +00:00
prec.right(
seq(
$.expression,
optional(','),
),
),
2023-10-31 09:51:37 +00:00
),
2023-11-30 00:23:42 +00:00
2024-02-19 20:24:44 +00:00
as_node: $ =>
2024-02-13 14:19:23 +00:00
seq($.expression, 'as', $.type),
2024-01-25 13:27:24 +00:00
pipe: $ =>
prec(
1,
seq(
choice(
$.command,
$.function_call,
),
'|',
choice(
$.command,
$.pipe,
$.function_call,
),
),
),
2024-01-25 12:10:45 +00:00
command: $ =>
prec.right(
seq(
'^',
2024-01-26 20:23:24 +00:00
$.command_text,
repeat($.command_argument),
2024-01-25 12:10:45 +00:00
),
),
2024-02-18 16:38:35 +00:00
command_text: $ => /[^\s;]+/,
2024-01-26 20:23:24 +00:00
command_argument: $ =>
2024-01-25 13:57:55 +00:00
choice(
/[^^|;\s]+/,
2024-01-25 13:57:55 +00:00
/("[^"]*?")|('[^']*?')|(`[^`]*?`)/,
),
2024-01-25 12:10:45 +00:00
block: $ =>
seq(
optional('async'),
'{',
repeat($.statement),
'}',
),
identifier: $ =>
2023-12-31 16:03:33 +00:00
/[_a-zA-Z]+[_a-zA-Z0-9]*[_a-zA-Z]?/,
2023-11-30 00:23:42 +00:00
value: $ =>
choice(
$.anonymous_function,
2023-11-30 00:23:42 +00:00
$.integer,
$.float,
$.string,
$.boolean,
$.list,
$.map,
2024-01-23 22:35:12 +00:00
$.range,
$.struct_instance,
$.enum_instance,
2024-01-23 22:35:12 +00:00
),
range: $ => /\d+[.]{2}\d+/,
2024-01-06 06:05:13 +00:00
integer: $ => /[-]?\d+/,
2023-11-30 00:23:42 +00:00
float: $ =>
choice(
/[-|+]?\d*[.][\d|e|-]*/,
2024-02-13 14:19:23 +00:00
'Infinity',
'infinity',
'NaN',
'nan',
2023-11-30 00:23:42 +00:00
),
2024-02-13 14:19:23 +00:00
2023-11-30 00:23:42 +00:00
string: $ =>
/("[^"]*?")|('[^']*?')|(`[^`]*?`)/,
boolean: $ =>
choice('true', 'false'),
list: $ =>
seq(
'[',
repeat(
prec.left(
seq(
$.expression,
optional(','),
),
),
),
']',
),
map: $ =>
2024-01-29 22:36:21 +00:00
prec(
1,
seq(
'{',
repeat(
seq(
$.identifier,
optional(
$.type_specification,
),
'=',
$.statement,
optional(','),
2024-01-23 22:08:26 +00:00
),
2023-11-30 00:23:42 +00:00
),
2024-01-29 22:36:21 +00:00
'}',
2023-11-30 00:23:42 +00:00
),
),
index: $ =>
prec.left(
2024-01-01 02:46:45 +00:00
1,
2023-11-30 00:23:42 +00:00
seq(
$.index_expression,
2023-11-30 00:23:42 +00:00
':',
$.index_expression,
2023-11-30 00:23:42 +00:00
),
),
index_expression: $ =>
prec(
1,
choice(
seq(
'(',
$.function_call,
')',
),
$.identifier,
$.index,
$.value,
2024-01-23 22:35:12 +00:00
$.range,
),
),
2023-11-30 00:23:42 +00:00
math: $ =>
prec.left(
2023-12-30 02:15:03 +00:00
seq(
$.expression,
$.math_operator,
$.expression,
2023-11-30 00:23:42 +00:00
),
),
math_operator: $ =>
choice('+', '-', '*', '/', '%'),
logic: $ =>
prec.left(
2023-12-30 02:15:03 +00:00
seq(
$.expression,
$.logic_operator,
$.expression,
2023-11-30 00:23:42 +00:00
),
),
logic_operator: $ =>
2023-12-02 03:54:25 +00:00
prec.left(
choice(
'==',
'!=',
'&&',
'||',
'>',
'<',
'>=',
'<=',
),
2023-11-30 00:23:42 +00:00
),
assignment: $ =>
seq(
2023-11-30 14:30:25 +00:00
$.identifier,
optional($.type_specification),
2023-11-30 14:30:25 +00:00
$.assignment_operator,
$.statement,
2023-11-30 00:23:42 +00:00
),
index_assignment: $ =>
seq(
$.index,
$.assignment_operator,
2023-10-31 17:04:22 +00:00
$.statement,
2023-11-30 00:23:42 +00:00
),
assignment_operator: $ =>
prec.right(
choice('=', '+=', '-='),
),
if_else: $ =>
prec.right(
seq(
$.if,
repeat($.else_if),
optional($.else),
),
),
if: $ =>
seq('if', $.expression, $.block),
else_if: $ =>
seq(
'else if',
$.expression,
$.block,
),
else: $ => seq('else', $.block),
match: $ =>
prec.right(
seq(
'match',
$.expression,
'{',
2023-11-30 00:23:42 +00:00
repeat1(
seq(
2024-02-15 12:04:38 +00:00
$.match_pattern,
2024-02-18 15:34:59 +00:00
'->',
$.statement,
2023-12-11 15:18:46 +00:00
optional(','),
2023-11-30 00:23:42 +00:00
),
),
'}',
2023-11-30 00:23:42 +00:00
),
),
2024-02-15 12:04:38 +00:00
match_pattern: $ =>
choice(
$.enum_pattern,
$.value,
'*',
),
enum_pattern: $ =>
prec(
1,
seq(
$.identifier,
'::',
$.identifier,
optional(
seq('(', $.identifier, ')'),
),
),
),
2024-02-19 20:24:44 +00:00
loop_node: $ =>
seq('loop', $.block),
2023-11-30 00:23:42 +00:00
while: $ =>
seq(
'while',
2023-11-14 23:56:44 +00:00
$.expression,
2023-11-30 00:23:42 +00:00
$.block,
),
for: $ =>
seq(
choice('for', 'async for'),
$.identifier,
'in',
2023-10-31 05:09:29 +00:00
$.expression,
2023-10-31 13:31:10 +00:00
$.block,
2023-11-30 00:23:42 +00:00
),
type_specification: $ =>
2023-11-30 00:23:42 +00:00
seq('<', $.type, '>'),
type: $ =>
prec.right(
choice(
'any',
'bool',
2024-01-01 10:20:11 +00:00
'collection',
2023-11-30 14:30:25 +00:00
'float',
'int',
2024-02-18 20:07:53 +00:00
'list',
'map',
2024-02-18 20:07:53 +00:00
'none',
'num',
'str',
// Custom type
$.identifier,
// Custom type with arguments
seq(
$.identifier,
$.type_arguments,
2024-02-18 20:07:53 +00:00
),
// Map with exact fields
seq(
'{',
repeat1(
seq(
$.identifier,
$.type_specification,
),
),
'}',
),
2024-02-18 20:07:53 +00:00
// List of
seq('list', '<', $.type, '>'),
// Exact list
2024-02-15 06:51:05 +00:00
seq(
2024-02-18 20:07:53 +00:00
'[',
repeat(
2024-02-15 12:04:38 +00:00
seq(
2024-02-15 07:22:04 +00:00
$.type,
optional(','),
),
),
2024-02-18 20:07:53 +00:00
']',
2024-02-15 06:51:05 +00:00
),
2024-02-18 20:07:53 +00:00
// Function
2023-11-30 00:23:42 +00:00
seq(
2023-12-05 21:42:11 +00:00
'(',
2023-11-30 00:23:42 +00:00
repeat(
seq(
$.type,
optional(','),
),
),
2023-12-05 21:42:11 +00:00
')',
2023-11-30 00:23:42 +00:00
optional(seq('->', $.type)),
),
),
2023-11-04 10:02:27 +00:00
),
2023-11-30 00:23:42 +00:00
anonymous_function: $ =>
2023-10-31 22:18:39 +00:00
seq(
2023-12-12 23:21:16 +00:00
'(',
2023-12-02 07:34:23 +00:00
repeat(
seq(
$.identifier,
$.type_specification,
2023-12-02 07:34:23 +00:00
optional(','),
2023-11-30 00:23:42 +00:00
),
),
2023-12-12 23:21:16 +00:00
')',
$.type_specification,
2023-12-02 07:34:23 +00:00
$.block,
2023-10-31 22:18:39 +00:00
),
2023-12-30 01:14:03 +00:00
function_expression: $ =>
2024-01-01 02:46:45 +00:00
choice(
$._function_expression_kind,
seq(
'(',
$._function_expression_kind,
')',
),
),
_function_expression_kind: $ =>
2023-12-30 01:14:03 +00:00
prec(
2,
2023-12-30 01:14:03 +00:00
choice(
$.function_call,
$.identifier,
$.index,
2024-01-01 09:59:27 +00:00
$.value,
2023-12-30 01:14:03 +00:00
),
),
2023-11-30 00:23:42 +00:00
function_call: $ =>
prec.right(
seq(
$.function_expression,
2023-12-30 01:14:03 +00:00
'(',
2023-11-30 00:23:42 +00:00
optional($._expression_list),
')',
),
),
type_definition: $ =>
choice(
$.enum_definition,
2024-02-23 04:38:49 +00:00
$.function_definition,
$.struct_definition,
),
2024-02-18 20:07:53 +00:00
type_arguments: $ =>
seq(
'<',
repeat1(
seq($.type, optional(',')),
2024-02-18 20:07:53 +00:00
),
'>',
),
enum_definition: $ =>
prec.right(
2023-11-30 00:23:42 +00:00
seq(
'enum',
$.identifier,
2024-02-18 20:07:53 +00:00
optional($.type_arguments),
repeat(
seq(
'{',
repeat1(
seq(
$.identifier,
optional(
$.type_arguments,
),
optional(','),
),
),
'}',
),
),
),
),
enum_instance: $ =>
prec.right(
seq(
$.identifier,
2024-02-15 07:22:04 +00:00
'::',
$.identifier,
2023-12-30 02:15:03 +00:00
optional(
seq('(', $.expression, ')'),
),
),
),
struct_definition: $ =>
seq(
'struct',
$.identifier,
'{',
repeat(
choice(
2023-12-30 02:15:03 +00:00
seq(
$.identifier,
$.type_specification,
),
seq(
$.identifier,
'=',
$.statement,
),
seq(
$.identifier,
$.type_specification,
'=',
$.statement,
2023-12-30 02:15:03 +00:00
),
),
2023-11-30 00:23:42 +00:00
),
'}',
2023-11-27 15:27:44 +00:00
),
2023-12-11 16:17:37 +00:00
struct_instance: $ =>
2024-02-18 15:34:59 +00:00
seq($.identifier, '::', $.map),
2024-02-23 04:38:49 +00:00
function_definition: $ =>
seq(
'fn',
$.identifier,
optional($.type_arguments),
'(',
repeat(
seq(
$.identifier,
$.type_specification,
optional(','),
),
),
')',
$.type_specification,
$.block,
),
2023-11-30 00:23:42 +00:00
},
2023-11-28 17:24:17 +00:00
});