tree-sitter-dust/grammar.js

112 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-08-22 21:02:40 +00:00
module.exports = grammar({
2023-08-24 17:01:12 +00:00
name: 'dust',
2023-08-22 21:02:40 +00:00
2023-09-29 13:17:38 +00:00
word: $ => $.identifier,
2023-08-22 21:02:40 +00:00
rules: {
2023-09-29 11:17:47 +00:00
root: $ => repeat1($.item),
item: $ => choice(
2023-09-29 02:55:47 +00:00
$.comment,
2023-09-27 21:41:39 +00:00
$.statement,
2023-09-29 11:17:47 +00:00
),
2023-08-22 21:02:40 +00:00
2023-09-29 11:17:47 +00:00
comment: $ => prec.left(seq(token('#'), /.*/)),
2023-09-29 02:55:47 +00:00
2023-09-29 04:17:16 +00:00
statement: $ => choice(
$.open_statement,
),
2023-09-29 07:52:21 +00:00
open_statement: $ => prec.left(seq($.expression)),
2023-09-27 23:10:21 +00:00
expression: $ => choice(
2023-09-29 12:59:27 +00:00
$.value,
$.identifier,
2023-09-29 13:17:38 +00:00
$.operation,
$.control_flow,
2023-09-29 18:32:18 +00:00
$.tool,
2023-09-27 21:41:39 +00:00
),
2023-09-29 13:53:53 +00:00
identifier: $ => /[a-z|_|.]+/,
2023-08-24 14:12:08 +00:00
value: $ => choice(
2023-08-22 22:28:34 +00:00
$.integer,
2023-08-24 13:31:26 +00:00
$.float,
2023-08-22 22:28:34 +00:00
$.string,
$.list,
2023-08-24 13:31:26 +00:00
$.empty,
2023-08-24 16:53:42 +00:00
$.boolean,
2023-09-22 10:38:25 +00:00
$.function,
2023-09-29 18:32:18 +00:00
$.table,
2023-09-29 19:25:10 +00:00
$.map,
2023-09-22 10:38:25 +00:00
),
2023-08-24 14:12:08 +00:00
2023-08-24 13:31:26 +00:00
float: $ => /\d+\.\d*/,
2023-09-29 18:32:18 +00:00
2023-08-24 13:31:26 +00:00
integer: $ => /\d+/,
2023-08-22 22:28:34 +00:00
2023-09-27 21:41:39 +00:00
string: $ => /("|'|`)(.*?)("|'|`)/,
2023-08-22 22:28:34 +00:00
2023-09-27 21:41:39 +00:00
empty: $ => '()',
2023-08-24 13:31:26 +00:00
2023-08-24 16:53:42 +00:00
boolean: $ => choice(
2023-09-27 21:41:39 +00:00
'true',
'false',
2023-08-24 16:53:42 +00:00
),
2023-08-22 22:28:34 +00:00
list: $ => seq(
'(',
2023-09-22 10:38:25 +00:00
repeat1(seq($.value, optional(','))),
2023-08-22 22:28:34 +00:00
')'
),
2023-09-27 21:41:39 +00:00
2023-09-29 18:32:18 +00:00
function: $ => seq(
'function',
optional(seq('<', repeat(seq($.identifier, optional(','))), '>')),
'{',
2023-09-29 19:25:10 +00:00
repeat1($.statement),
2023-09-29 18:32:18 +00:00
'}',
),
table: $ => seq(
'table',
2023-09-29 19:25:10 +00:00
seq('<', repeat1(seq($.identifier, optional(','))), '>'),
2023-09-29 18:32:18 +00:00
'{',
2023-09-29 19:25:10 +00:00
repeat($.list),
'}',
),
map: $ => seq(
'map',
'{',
repeat(seq($.identifier, "=", $.value)),
2023-09-29 18:32:18 +00:00
'}',
),
2023-09-29 11:17:47 +00:00
operator: $ => choice(
2023-09-27 21:41:39 +00:00
'+',
'-',
'=',
2023-09-29 13:17:38 +00:00
'==',
2023-09-29 11:17:47 +00:00
),
2023-09-28 20:57:30 +00:00
operation: $ => prec.left(seq(
$.expression,
$.operator,
$.expression,
)),
2023-09-29 07:52:21 +00:00
control_flow: $ => prec.right(seq(
'if',
$.expression,
'then',
$.statement,
optional(seq('else', $.statement))
)),
2023-09-29 13:53:53 +00:00
2023-09-29 18:32:18 +00:00
tool: $ => choice(
"output",
2023-09-29 13:53:53 +00:00
),
2023-08-22 21:02:40 +00:00
}
});