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

513 lines
8.5 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(
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,
2024-01-06 06:05:13 +00:00
$.new,
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
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(
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,
$.built_in_value,
2024-01-06 06:05:13 +00:00
$.structure,
2024-01-23 22:35:12 +00:00
$.range,
),
range: $ =>
seq(
$.integer,
token.immediate('..'),
$.integer,
2024-01-06 06:05:13 +00:00
),
structure: $ =>
seq(
'struct',
'{',
repeat(
choice(
seq(
$.identifier,
$.type_specification,
2024-01-06 06:05:13 +00:00
),
seq(
$.identifier,
'=',
$.statement,
),
seq(
$.identifier,
$.type_specification,
2024-01-06 06:05:13 +00:00
'=',
$.statement,
),
),
),
'}',
),
new: $ =>
seq(
'new',
$.identifier,
'{',
repeat(
choice(
seq(
$.identifier,
'=',
$.statement,
),
seq(
$.identifier,
$.type_specification,
2024-01-06 06:05:13 +00:00
'=',
$.statement,
),
),
),
'}',
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,
2024-01-23 22:08:26 +00:00
optional(
$.type_specification,
),
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(
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(
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_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',
'map',
2023-12-26 22:19:12 +00:00
'none',
'num',
'str',
2024-01-06 06:05:13 +00:00
$.identifier,
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,
$.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 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
2024-01-01 09:59:27 +00:00
built_in_value: $ =>
2023-12-11 16:17:37 +00:00
choice(
2024-01-01 09:59:27 +00:00
'args',
'assert_equal',
2024-01-01 09:59:27 +00:00
'env',
2024-01-01 12:46:47 +00:00
'fs',
'json',
'length',
'output',
'random',
2024-01-01 13:52:25 +00:00
'string',
2023-12-11 16:17:37 +00:00
),
2023-11-30 00:23:42 +00:00
},
2023-11-28 17:24:17 +00:00
});