tree-sitter-dust/grammar.js
2023-09-29 09:53:53 -04:00

95 lines
1.4 KiB
JavaScript

module.exports = grammar({
name: 'dust',
word: $ => $.identifier,
rules: {
root: $ => repeat1($.item),
item: $ => choice(
$.comment,
$.statement,
),
comment: $ => prec.left(seq(token('#'), /.*/)),
statement: $ => choice(
$.closed_statement,
$.open_statement,
),
closed_statement: $ => seq($.expression, $.close),
open_statement: $ => prec.left(seq($.expression)),
expression: $ => choice(
$.value,
$.identifier,
$.operation,
$.control_flow,
),
close: $ => ";",
identifier: $ => /[a-z|_|.]+/,
value: $ => choice(
$.integer,
$.float,
$.string,
$.list,
$.empty,
$.boolean,
$.function,
),
float: $ => /\d+\.\d*/,
integer: $ => /\d+/,
string: $ => /("|'|`)(.*?)("|'|`)/,
function: $ => /{(.*?)}/,
empty: $ => '()',
boolean: $ => choice(
'true',
'false',
),
list: $ => seq(
'(',
repeat1(seq($.value, optional(','))),
')'
),
operator: $ => choice(
'+',
'-',
'=',
'==',
),
operation: $ => prec.left(seq(
$.expression,
$.operator,
$.expression,
)),
control_flow: $ => prec.right(seq(
'if',
$.expression,
'then',
$.statement,
optional(seq('else', $.statement))
)),
_keywords: $ => choice(
'if',
'then',
'else',
),
}
});