tree-sitter-dust/grammar.js

193 lines
3.1 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),
2023-10-06 02:05:18 +00:00
2023-09-29 11:17:47 +00:00
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-06 02:05:18 +00:00
comment: $ => seq('#', /.*/),
2023-09-29 02:55:47 +00:00
2023-10-06 10:18:02 +00:00
statement: $ => choice(
2023-10-06 02:05:18 +00:00
$.assignment,
2023-10-01 02:59:27 +00:00
$.expression,
2023-10-06 10:18:02 +00:00
$.if_else,
2023-10-01 02:59:27 +00:00
$.yield,
2023-10-06 02:05:18 +00:00
$.insert,
$.select,
$.loop,
$.match,
2023-10-06 10:18:02 +00:00
),
2023-10-06 02:05:18 +00:00
2023-10-06 10:18:02 +00:00
yield: $ => seq(
$.expression,
'->',
$.expression,
repeat(prec.left(seq('->', $.expression)))
2023-10-01 10:09:29 +00:00
),
2023-09-27 23:10:21 +00:00
2023-10-06 10:18:02 +00:00
expression: $ => choice(
2023-09-29 12:59:27 +00:00
$.value,
$.identifier,
2023-10-01 01:12:35 +00:00
$.function_call,
$.math,
$.logic,
2023-10-06 10:18:02 +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-06 10:18:02 +00:00
integer: $ => /[-]?[0-9]+/,
2023-08-22 22:28:34 +00:00
2023-10-06 10:18:02 +00:00
float: $ => /[-]?[0-9]+[.]{1}[0-9]*/,
2023-10-06 02:05:18 +00:00
2023-10-05 20:49:03 +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(
'{',
2023-10-06 02:05:18 +00:00
repeat(seq($.identifier, "=", $.value)), // TODO: Replace value with expression
2023-09-29 18:32:18 +00:00
'}',
),
2023-10-06 02:05:18 +00:00
math: $ => prec.left(seq(
2023-10-01 01:12:35 +00:00
$.expression,
$.math_operator,
2023-10-01 01:12:35 +00:00
$.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
),
2023-10-06 02:05:18 +00:00
logic: $ => prec.left(seq(
2023-10-01 01:12:35 +00:00
$.expression,
$.logic_operator,
2023-10-01 01:12:35 +00:00
$.expression,
)),
logic_operator: $ => choice(
2023-09-29 13:17:38 +00:00
'==',
2023-10-06 02:05:18 +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-06 10:18:02 +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-10-05 20:49:03 +00:00
insert: $ => prec.right(1, seq(
2023-09-30 20:17:09 +00:00
'insert',
repeat1($.list),
'into',
$.identifier,
optional(
2023-10-05 20:49:03 +00:00
seq('where', $.logic)
2023-09-30 20:17:09 +00:00
),
)),
2023-10-06 10:18:02 +00:00
if_else: $ => prec.left(1, seq(
2023-09-29 07:52:21 +00:00
'if',
2023-10-06 02:05:18 +00:00
$.expression,
2023-09-29 07:52:21 +00:00
'then',
2023-10-06 02:05:18 +00:00
$.statement,
optional(seq(
'else',
$.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-06 12:16:34 +00:00
'(',
2023-10-05 20:49:03 +00:00
repeat(seq($.expression)),
2023-10-06 10:18:02 +00:00
')',
2023-10-01 01:12:35 +00:00
)),
2023-10-01 05:13:29 +00:00
2023-10-06 10:18:02 +00:00
while: $ => seq(
2023-10-01 05:13:29 +00:00
'while',
$.expression,
'{',
$.statement,
'}',
),
2023-10-06 10:18:02 +00:00
loop: $ => seq(
2023-10-01 05:13:29 +00:00
'loop',
'{',
2023-10-06 02:05:18 +00:00
repeat($.statement),
'break',
optional($.value),
2023-10-06 10:18:02 +00:00
'}',
2023-10-01 05:13:29 +00:00
),
2023-10-06 10:18:02 +00:00
match: $ => prec.right(seq(
2023-10-01 05:13:29 +00:00
'match',
$.expression,
repeat1(seq($.expression, '=>', $.statement)),
2023-10-06 10:18:02 +00:00
'catch',
$.statement
)),
2023-08-22 21:02:40 +00:00
}
});