dust/tree-sitter-dust/grammar.js

443 lines
7.4 KiB
JavaScript
Raw Normal View History

module.exports = grammar({
name: 'dust',
2023-12-11 16:17:37 +00:00
word: $ => $._identifier_pattern,
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(
choice(
$.assignment,
$.block,
$.expression,
$.for,
$.if_else,
$.index_assignment,
$.match,
$.return,
$.while,
),
optional(';'),
),
2023-10-31 20:25:13 +00:00
),
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(
$.function_call,
$.identifier,
$.index,
$.logic,
$.math,
$.value,
$.yield,
),
),
_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
block: $ =>
seq(
optional('async'),
'{',
repeat($.statement),
'}',
),
identifier: $ =>
choice(
$._identifier_pattern,
$.built_in_function,
),
2023-12-11 16:17:37 +00:00
_identifier_pattern: $ =>
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(
2023-12-12 23:21:16 +00:00
$.function,
2023-11-30 00:23:42 +00:00
$.integer,
$.float,
$.string,
$.boolean,
$.list,
$.map,
2023-12-30 01:14:03 +00:00
$.option,
2023-11-30 00:23:42 +00:00
),
integer: $ =>
token(
prec.left(
seq(
optional('-'),
repeat1(
choice(
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'0',
),
),
),
),
),
float: $ =>
token(
prec.left(
seq(
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(
'[',
repeat(
prec.left(
seq(
$.expression,
optional(','),
),
),
),
']',
),
map: $ =>
seq(
'{',
repeat1(
2023-11-30 00:23:42 +00:00
seq(
$.identifier,
2023-12-20 23:29:18 +00:00
optional($.type_definition),
2023-11-30 00:23:42 +00:00
'=',
$.statement,
optional(','),
),
),
'}',
),
option: $ =>
choice(
'none',
seq(
'some',
'(',
$.expression,
')',
),
),
2023-11-30 00:23:42 +00:00
index: $ =>
prec.left(
2023-12-30 01:14:03 +00:00
2,
2023-11-30 00:23:42 +00:00
seq(
$.expression,
':',
$.expression,
optional(
seq('..', $.expression),
),
),
),
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_definition),
$.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(
choice($.expression, '*'),
2023-11-30 00:23:42 +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
),
),
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
),
return: $ =>
2023-12-02 03:54:25 +00:00
prec.right(
2023-12-31 19:04:10 +00:00
seq('return', $.statement),
2023-12-02 03:54:25 +00:00
),
2023-11-30 00:23:42 +00:00
type_definition: $ =>
seq('<', $.type, '>'),
type: $ =>
prec.right(
choice(
'any',
'bool',
2023-11-30 14:30:25 +00:00
'float',
'int',
'map',
2023-12-26 22:19:12 +00:00
'none',
'num',
'str',
seq('[', $.type, ']'),
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)),
),
seq(
'option',
'(',
$.type,
')',
2023-12-30 01:14:03 +00:00
),
2023-11-30 00:23:42 +00:00
),
2023-11-04 10:02:27 +00:00
),
2023-11-30 00:23:42 +00:00
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,
2023-12-12 23:21:16 +00:00
$.type_definition,
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_definition,
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: $ =>
2023-12-30 01:14:03 +00:00
prec(
1,
choice(
$.function,
$.function_call,
$.identifier,
$.index,
2023-12-30 02:15:03 +00:00
$.yield,
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),
')',
),
),
yield: $ =>
prec.left(
2023-12-30 02:15:03 +00:00
1,
2023-11-30 00:23:42 +00:00
seq(
$.expression,
'->',
$.function_expression,
2023-12-30 02:15:03 +00:00
optional(
seq(
'(',
$._expression_list,
')',
),
),
2023-11-30 00:23:42 +00:00
),
2023-11-27 15:27:44 +00:00
),
2023-12-11 16:17:37 +00:00
built_in_function: $ =>
choice(
'assert',
'assert_equal',
'bash',
'download',
2023-12-27 01:05:19 +00:00
'either_or',
'fish',
'from_json',
2023-12-27 01:05:19 +00:00
'is_none',
'is_some',
'length',
'metadata',
'output',
'output_error',
'random',
'random_boolean',
'random_float',
'random_integer',
'read',
'to_json',
'write',
2023-12-11 16:17:37 +00:00
),
2023-11-30 00:23:42 +00:00
},
2023-11-28 17:24:17 +00:00
});