schala/experiments/tree-sitter-test/grammar.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-04-21 02:25:56 -07:00
module.exports = grammar({
name: "TestLang",
rules: {
2024-04-21 03:01:13 -07:00
source_file: $ => repeat($._definition),
_definition: $ => choice(
$.function_definition
//TODO others
2024-04-21 03:08:05 -07:00
),
2024-04-21 03:01:13 -07:00
2024-04-21 03:08:05 -07:00
function_definition: $ => seq(
2024-04-23 02:37:01 -07:00
'fn',
2024-04-21 03:08:05 -07:00
$.identifier,
$.parameter_list,
2024-04-23 02:37:01 -07:00
field("return_type", optional($._type)),
2024-04-21 03:08:05 -07:00
$.block,
),
parameter_list: $ => seq("(", /* TODO */ ")"),
2024-04-21 03:01:13 -07:00
2024-04-21 03:08:05 -07:00
block: $ => seq(
"{",
2024-04-23 02:37:01 -07:00
choice(
repeat($._statement),
"",
),
2024-04-21 03:08:05 -07:00
"}"
),
2024-04-21 03:01:13 -07:00
2024-04-21 03:08:05 -07:00
_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))),
2024-04-21 03:08:05 -07:00
_type: $ => "bool",
2024-04-23 02:37:01 -07:00
_type: $ => choice(
$.primitive_type,
),
primitive_type: $ => choice("bool", "int"),
2024-04-21 03:08:05 -07:00
identifier: $ => /[a-z]+/,
2024-04-21 02:25:56 -07:00
}
});