schala/experiments/tree-sitter-test/grammar.js
Greg Shuflin 77257d0eb7 Messing with treesitter grammar
doesn't work yet
2024-04-23 02:13:44 -07:00

42 lines
1.1 KiB
JavaScript

module.exports = grammar({
name: "TestLang",
rules: {
source_file: $ => repeat($._definition),
_definition: $ => choice(
$.function_definition
//TODO others
),
function_definition: $ => seq(
"fn",
$.identifier,
$.parameter_list,
optional($._type),
$.block,
),
parameter_list: $ => seq("(", /* TODO */ ")"),
block: $ => seq(
"{",
repeat(optional($._statement)),
"}"
),
_statement: $ => choice(
$._return_statement
),
_return_statement: $ => seq("return", $._expression, ";"),
_expression: $ => choice($.identifier, $.unary, $.binary),
unary: $ => prec(4, choice(seq("-", $._expression), seq("!", $._expression))),
binary: $ => choice(prec.left(2, seq($._expression, "*", $._expression)), prec.left(1, seq($._expression, "+", $._expression))),
_type: $ => "bool",
identifier: $ => /[a-z]+/,
}
});