module.exports = grammar({ name: 'dust', word: $ => $.identifier, rules: { root: $ => repeat1($.item), item: $ => choice( $.comment, $.statement, ), comment: $ => seq(token('#'), /.*/), statement: $ => prec.right(choice( $.expression, $.yield, )), yield: $ => prec.left(seq($.statement, '->', $.statement)), expression: $ => choice( $._expression_kind, seq('(', $._expression_kind, ')') ), _expression_kind: $ => prec.right(choice( $.value, $.identifier, $.control_flow, $.select, $.insert, $.function_call, $.assignment, $.math, $.logic, )), identifier: $ => /[a-z|_|.]+[0-9]?/, value: $ => prec.left(1, choice( $.integer, $.float, $.string, $.list, $.empty, $.boolean, $.function, $.table, $.map, )), float: $ => /\d+\.\d*/, integer: $ => /\d+/, string: $ => /(".*?")|('.*?')|(`.*?`)/, empty: $ => '()', 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( 'map', '{', repeat(seq($.identifier, "=", $.value)), '}', ), math: $ => prec.right(seq( $.expression, $.math_operator, $.expression, )), math_operator: $ => choice( '+', '-', '*', '/', '%', ), logic: $ => prec.right(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(seq( 'insert', repeat1($.list), 'into', $.identifier, optional( seq('where', $.expression) ), )), control_flow: $ => prec.right(seq( 'if', $.expression, 'then', $.statement, optional(seq('else', $.statement)) )), function_call: $ => prec.right(seq( $.identifier, '<', repeat( choice($.identifier, $.value), ), '>', )), } });