tree-sitter-dust/grammar.js

204 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-08-22 21:02:40 +00:00
module.exports = grammar({
2023-08-24 17:01:12 +00:00
name: 'dust',
2023-08-22 21:02:40 +00:00
2023-09-29 13:17:38 +00:00
word: $ => $.identifier,
2023-08-22 21:02:40 +00:00
rules: {
2023-09-29 11:17:47 +00:00
root: $ => repeat1($.item),
item: $ => choice(
2023-09-29 02:55:47 +00:00
$.comment,
2023-10-05 12:03:37 +00:00
$.statement,
2023-09-29 11:17:47 +00:00
),
2023-08-22 21:02:40 +00:00
2023-10-01 01:12:35 +00:00
comment: $ => seq(token('#'), /.*/),
2023-09-29 02:55:47 +00:00
2023-09-30 19:47:21 +00:00
statement: $ => prec.right(choice(
2023-10-01 02:59:27 +00:00
$.expression,
$.yield,
2023-09-30 19:47:21 +00:00
)),
2023-09-29 04:17:16 +00:00
2023-10-01 10:09:29 +00:00
yield: $ => prec.left(
seq(
$.expression,
'->',
$.expression,
repeat(prec.left(seq('->', $.expression)))
)
),
2023-09-27 23:10:21 +00:00
expression: $ => choice(
2023-10-01 01:12:35 +00:00
$._expression_kind,
seq('(', $._expression_kind, ')')
),
_expression_kind: $ => prec.right(choice(
2023-09-29 12:59:27 +00:00
$.value,
$.identifier,
2023-09-29 13:17:38 +00:00
$.control_flow,
2023-09-30 20:06:01 +00:00
$.select,
2023-09-30 20:17:09 +00:00
$.insert,
2023-10-01 01:12:35 +00:00
$.function_call,
$.assignment,
$.math,
$.logic,
2023-10-01 05:13:29 +00:00
$.loop,
$.match,
2023-10-01 01:12:35 +00:00
)),
2023-09-27 21:41:39 +00:00
2023-09-30 19:50:20 +00:00
identifier: $ => /[a-z|_|.]+[0-9]?/,
2023-08-24 14:12:08 +00:00
2023-10-02 22:54:49 +00:00
value: $ => choice(
2023-08-22 22:28:34 +00:00
$.integer,
2023-08-24 13:31:26 +00:00
$.float,
2023-08-22 22:28:34 +00:00
$.string,
2023-08-24 16:53:42 +00:00
$.boolean,
2023-10-05 12:03:37 +00:00
$.list,
2023-09-22 10:38:25 +00:00
$.function,
2023-09-29 18:32:18 +00:00
$.table,
2023-09-29 19:25:10 +00:00
$.map,
2023-10-02 22:54:49 +00:00
),
2023-08-24 14:12:08 +00:00
2023-10-01 04:01:02 +00:00
float: $ => /[-]*[0-9]*[.]{1}[0-9]+/,
2023-09-29 18:32:18 +00:00
2023-10-01 04:01:02 +00:00
integer: $ => /[-]*[0-9]+[.]{0}/,
2023-08-22 22:28:34 +00:00
2023-10-01 02:59:27 +00:00
string: $ => /(".*?")|('.*?')|(`.*?`)/,
2023-08-22 22:28:34 +00:00
2023-08-24 16:53:42 +00:00
boolean: $ => choice(
2023-09-27 21:41:39 +00:00
'true',
'false',
2023-08-24 16:53:42 +00:00
),
2023-08-22 22:28:34 +00:00
list: $ => seq(
2023-09-29 20:34:20 +00:00
'[',
2023-10-05 12:03:37 +00:00
repeat1(seq($.value, optional(','))),
2023-09-29 20:34:20 +00:00
']'
2023-08-22 22:28:34 +00:00
),
2023-09-27 21:41:39 +00:00
2023-09-29 18:32:18 +00:00
function: $ => seq(
'function',
optional(seq('<', repeat(seq($.identifier, optional(','))), '>')),
'{',
2023-09-29 19:25:10 +00:00
repeat1($.statement),
2023-09-29 18:32:18 +00:00
'}',
),
table: $ => seq(
'table',
2023-09-29 19:25:10 +00:00
seq('<', repeat1(seq($.identifier, optional(','))), '>'),
2023-09-29 18:32:18 +00:00
'{',
2023-09-29 19:25:10 +00:00
repeat($.list),
'}',
),
map: $ => seq(
'map',
'{',
2023-10-05 12:03:37 +00:00
repeat(seq($.identifier, "=", $.value)),
2023-09-29 18:32:18 +00:00
'}',
),
2023-10-01 01:12:35 +00:00
math: $ => prec.right(seq(
$.expression,
$.math_operator,
$.expression,
)),
math_operator: $ => choice(
2023-09-27 21:41:39 +00:00
'+',
'-',
2023-09-30 19:47:21 +00:00
'*',
'/',
'%',
2023-10-01 01:12:35 +00:00
),
logic: $ => prec.right(seq(
$.expression,
$.logic_operator,
$.expression,
)),
logic_operator: $ => choice(
2023-09-29 13:17:38 +00:00
'==',
2023-09-30 19:47:21 +00:00
'&&',
'||',
'and',
'or',
2023-09-30 20:06:01 +00:00
),
2023-09-28 20:57:30 +00:00
2023-10-01 01:12:35 +00:00
assignment: $ => prec.right(seq(
$.identifier,
choice("=", "+=", "-="),
2023-10-01 06:43:42 +00:00
$.statement,
2023-09-28 20:57:30 +00:00
)),
2023-09-29 07:52:21 +00:00
2023-09-30 20:06:01 +00:00
select: $ => prec.right(seq(
'select',
$.identifier,
'from',
$.identifier,
optional(
seq('where', $.expression)
),
)),
2023-09-30 20:17:09 +00:00
insert: $ => prec.right(seq(
'insert',
repeat1($.list),
'into',
$.identifier,
optional(
seq('where', $.expression)
),
)),
2023-09-29 07:52:21 +00:00
control_flow: $ => prec.right(seq(
'if',
2023-10-02 22:54:49 +00:00
field('if_expression', $.expression),
2023-09-29 07:52:21 +00:00
'then',
2023-10-02 22:54:49 +00:00
field('then_statement', $.statement),
optional(
seq('else',
field('else_statement', $.statement),
),
),
2023-09-29 07:52:21 +00:00
)),
2023-09-29 13:53:53 +00:00
2023-10-01 01:12:35 +00:00
function_call: $ => prec.right(seq(
$.identifier,
'<',
2023-10-01 16:17:52 +00:00
repeat(seq($.expression, optional(','))),
2023-10-01 01:12:35 +00:00
'>',
)),
2023-10-01 05:13:29 +00:00
loop: $ => choice(
$.break_loop,
$.while_loop,
),
while_loop: $ => seq(
'while',
$.expression,
'{',
$.statement,
'}',
),
break_loop: $ => seq(
'loop',
'{',
$.statement,
'}',
),
match: $ => seq(
'match',
$.expression,
'{',
repeat1(seq($.expression, '=>', $.statement)),
'}',
),
2023-08-22 21:02:40 +00:00
}
});