diff --git a/experiments/tree-sitter-test/grammar.js b/experiments/tree-sitter-test/grammar.js index 5fb773f..2de25d1 100644 --- a/experiments/tree-sitter-test/grammar.js +++ b/experiments/tree-sitter-test/grammar.js @@ -12,14 +12,14 @@ module.exports = grammar({ "fn", $.identifier, $.parameter_list, - $._type, + optional($._type), $.block, ), parameter_list: $ => seq("(", /* TODO */ ")"), block: $ => seq( "{", - repeat($._statement), + repeat(optional($._statement)), "}" ), @@ -29,7 +29,10 @@ module.exports = grammar({ _return_statement: $ => seq("return", $._expression, ";"), - _expression: $ => choice($.identifier), + _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", diff --git a/experiments/tree-sitter-test/test/corpus/test.txt b/experiments/tree-sitter-test/test/corpus/test.txt index 3c0a61c..ab26e36 100644 --- a/experiments/tree-sitter-test/test/corpus/test.txt +++ b/experiments/tree-sitter-test/test/corpus/test.txt @@ -1,8 +1,9 @@ ============= Initial test ============= +fn main() { -hello +} ---- @@ -13,7 +14,7 @@ hello Another ==== -fn main() { } +fn main() bool { } ------ ()