module.exports = grammar({ name: 'dust', word: $ => $.identifier, rules: { root: $ => repeat1($.item), item: $ => choice( $.comment, $.statement, ), comment: $ => seq('#', /.*/), statement: $ => choice( $.assignment, $.expression, $.if_else, $.yield, $.insert, $.select, $.loop, $.match, ), yield: $ => seq( $.expression, '->', $.expression, repeat(prec.left(seq('->', $.expression))) ), expression: $ => 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]+/, 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("=", "+=", "-="), $.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)), ')', )), while: $ => seq( 'while', $.expression, '{', $.statement, '}', ), loop: $ => seq( 'loop', '{', repeat($.statement), 'break', optional($.value), '}', ), match: $ => prec.right(seq( 'match', $.expression, repeat1(seq($.expression, '=>', $.statement)), 'catch', $.statement )), } });