From 77257d0eb7c35a934969655f1327dddb9a3d46d3 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Tue, 23 Apr 2024 02:13:44 -0700 Subject: [PATCH] Messing with treesitter grammar doesn't work yet --- experiments/tree-sitter-test/grammar.js | 9 ++++++--- experiments/tree-sitter-test/test/corpus/test.txt | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) 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 { } ------ ()