209 lines
3.5 KiB
JavaScript
209 lines
3.5 KiB
JavaScript
module.exports = grammar({
|
|
name: 'dust',
|
|
|
|
word: $ => $.identifier,
|
|
|
|
rules: {
|
|
root: $ => repeat1($.item),
|
|
|
|
item: $ => choice(
|
|
$.comment,
|
|
$.statement,
|
|
),
|
|
|
|
comment: $ => seq('#', /.*/), // TODO: New comment that can be used anyhere.
|
|
// Keep this comment type for later use.
|
|
|
|
statement: $ => choice(
|
|
$.assignment,
|
|
$.expression,
|
|
$.if_else,
|
|
$.yield,
|
|
$.insert,
|
|
$.select,
|
|
$.loop,
|
|
$.match,
|
|
$.while,
|
|
),
|
|
|
|
yield: $ => seq(
|
|
$.expression,
|
|
'->',
|
|
$.expression,
|
|
repeat(prec.left(seq('->', $.expression)))
|
|
),
|
|
|
|
expression: $ => prec.right(choice(
|
|
$.value,
|
|
$.identifier,
|
|
$.function_call,
|
|
$.tool_call,
|
|
$.math,
|
|
$.logic,
|
|
)),
|
|
|
|
identifier: $ => /[a-z|_|.]+[0-9]?/,
|
|
|
|
value: $ => choice(
|
|
$.integer,
|
|
$.float,
|
|
$.string,
|
|
$.boolean,
|
|
$.list,
|
|
$.function,
|
|
$.table,
|
|
$.map,
|
|
),
|
|
|
|
integer: $ => /[-]?[0-9]+/,
|
|
|
|
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($.item),
|
|
'}',
|
|
),
|
|
|
|
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.right(seq(
|
|
$.expression,
|
|
$.logic_operator,
|
|
$.expression,
|
|
)),
|
|
|
|
logic_operator: $ => choice(
|
|
'==',
|
|
'!=',
|
|
'&&',
|
|
'||',
|
|
),
|
|
|
|
assignment: $ => prec.right(seq(
|
|
$.identifier,
|
|
choice("=", "+=", "-="),
|
|
$.statement,
|
|
)),
|
|
|
|
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)
|
|
),
|
|
)),
|
|
|
|
if_else: $ => prec.left(1, seq(
|
|
'if',
|
|
$.expression,
|
|
'then',
|
|
$.statement,
|
|
optional(seq(
|
|
'else',
|
|
$.statement,
|
|
)),
|
|
)),
|
|
|
|
function_call: $ => prec.right(seq(
|
|
'(',
|
|
$.identifier,
|
|
repeat(seq($.expression, optional(','))),
|
|
')',
|
|
)),
|
|
|
|
while: $ => seq(
|
|
'while',
|
|
$.expression,
|
|
'{',
|
|
$.item,
|
|
'}',
|
|
),
|
|
|
|
loop: $ => seq(
|
|
'loop',
|
|
'{',
|
|
repeat($.statement),
|
|
'break',
|
|
optional($.value),
|
|
'}',
|
|
),
|
|
|
|
match: $ => prec.right(seq(
|
|
'match',
|
|
$.expression,
|
|
repeat1(seq($.expression, '=>', $.statement)),
|
|
'catch',
|
|
$.statement
|
|
)),
|
|
|
|
tool_call: $ => prec.right(1, seq(
|
|
'(',
|
|
$._tool,
|
|
')',
|
|
)),
|
|
|
|
_tool: $ => choice(
|
|
$.input,
|
|
$.output,
|
|
),
|
|
|
|
input: $ => seq('input', $.expression),
|
|
output: $ => seq('output', $.expression),
|
|
read: $ => seq('read', $.expression),
|
|
}
|
|
});
|