tree-sitter-dust/grammar.js
2023-10-05 22:34:53 -04:00

206 lines
3.4 KiB
JavaScript

module.exports = grammar({
name: 'dust',
word: $ => $.identifier,
rules: {
root: $ => repeat1($.item),
item: $ => choice(
$.comment,
$.statement,
),
comment: $ => seq('#', /.*/),
statement: $ => prec.right(1, choice(
$.assignment,
$.expression,
$.control_flow,
$.yield,
$.insert,
$.select,
$.loop,
$.match,
)),
yield: $ => prec.right(2,
seq(
$.expression,
'->',
$.expression,
repeat(prec.left(seq('->', $.expression)))
)
),
expression: $ => prec.right(choice(
$.value,
$.identifier,
$.function_call,
$.math,
$.logic,
)),
identifier: $ => /[a-z|_|.]+[0-9]?/,
value: $ => choice(
$.integer,
$.float,
$.string,
$.boolean,
$.list,
$.function,
$.table,
$.map,
),
integer: $ => /[-]*[0-9]+[.]{0}/,
float: $ => /[-]*[0-9]*[.]{1}[0-9]+/,
string: $ => /("[^"]*?")|('[^']*?')|(`[^`]*?`)/,
boolean: $ => choice(
'true',
'false',
),
list: $ => seq(
'[',
repeat1(seq($.value, optional(','))),
']'
),
function: $ => seq(
'function',
optional(seq('<', repeat(seq($.identifier, optional(','))), '>')),
'{',
repeat1($.statement),
'}',
),
table: $ => seq(
'table',
seq('<', repeat1(seq($.identifier, optional(','))), '>'),
'{',
repeat($.list),
'}',
),
map: $ => seq(
'{',
repeat(seq($.identifier, "=", $.value)), // TODO: Replace value with expression
'}',
),
math: $ => prec.left(seq(
$.expression,
$.math_operator,
$.expression,
)),
math_operator: $ => choice(
'+',
'-',
'*',
'/',
'%',
),
logic: $ => prec.left(seq(
$.expression,
$.logic_operator,
$.expression,
)),
logic_operator: $ => choice(
'==',
'!=',
'&&',
'||',
'and',
'or',
),
assignment: $ => prec.right(seq(
$.identifier,
choice("=", "+=", "-="),
$.expression,
)),
select: $ => prec.right(seq(
'select',
$.identifier,
'from',
$.identifier,
optional(
seq('where', $.expression)
),
)),
insert: $ => prec.right(1, seq(
'insert',
repeat1($.list),
'into',
$.identifier,
optional(
seq('where', $.logic)
),
)),
control_flow: $ => prec.right(seq(
'if',
$.expression,
'then',
$.statement,
prec.left(repeat(seq(
'else if',
$.expression,
'then',
$.statement,
))),
optional(seq(
'else',
$.statement,
)),
)),
function_call: $ => prec.right(seq(
$.identifier,
'{',
repeat(seq($.expression)),
'}',
)),
loop: $ => choice(
$.break_loop,
$.while_loop,
),
while_loop: $ => seq(
'while',
$.expression,
'{',
$.statement,
'}',
),
break_loop: $ => seq(
'loop',
'{',
repeat($.statement),
'break',
optional($.value),
'}',
),
match: $ => seq(
'match',
$.expression,
'{',
repeat1(seq($.expression, '=>', $.statement)),
'}',
),
}
});