112 lines
1.8 KiB
JavaScript
112 lines
1.8 KiB
JavaScript
module.exports = grammar({
|
|
name: 'dust',
|
|
|
|
word: $ => $.identifier,
|
|
|
|
rules: {
|
|
root: $ => repeat1($.item),
|
|
|
|
item: $ => choice(
|
|
$.comment,
|
|
$.statement,
|
|
),
|
|
|
|
comment: $ => prec.left(seq(token('#'), /.*/)),
|
|
|
|
statement: $ => choice(
|
|
$.open_statement,
|
|
),
|
|
|
|
open_statement: $ => prec.left(seq($.expression)),
|
|
|
|
expression: $ => choice(
|
|
$.value,
|
|
$.identifier,
|
|
$.operation,
|
|
$.control_flow,
|
|
$.tool,
|
|
),
|
|
|
|
identifier: $ => /[a-z|_|.]+/,
|
|
|
|
value: $ => 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)),
|
|
'}',
|
|
),
|
|
|
|
operator: $ => choice(
|
|
'+',
|
|
'-',
|
|
'=',
|
|
'==',
|
|
),
|
|
|
|
operation: $ => prec.left(seq(
|
|
$.expression,
|
|
$.operator,
|
|
$.expression,
|
|
)),
|
|
|
|
control_flow: $ => prec.right(seq(
|
|
'if',
|
|
$.expression,
|
|
'then',
|
|
$.statement,
|
|
optional(seq('else', $.statement))
|
|
)),
|
|
|
|
tool: $ => choice(
|
|
"output",
|
|
),
|
|
}
|
|
});
|