Working simple tree-sitter grammar

This commit is contained in:
Greg Shuflin 2024-04-23 02:37:01 -07:00
parent 45c4d08fb9
commit dc771fc7ad
3 changed files with 31 additions and 14 deletions

View File

@ -9,17 +9,20 @@ module.exports = grammar({
),
function_definition: $ => seq(
"fn",
'fn',
$.identifier,
$.parameter_list,
optional($._type),
field("return_type", optional($._type)),
$.block,
),
parameter_list: $ => seq("(", /* TODO */ ")"),
block: $ => seq(
"{",
repeat(optional($._statement)),
choice(
repeat($._statement),
"",
),
"}"
),
@ -35,6 +38,10 @@ module.exports = grammar({
binary: $ => choice(prec.left(2, seq($._expression, "*", $._expression)), prec.left(1, seq($._expression, "+", $._expression))),
_type: $ => "bool",
_type: $ => choice(
$.primitive_type,
),
primitive_type: $ => choice("bool", "int"),
identifier: $ => /[a-z]+/,
}

View File

@ -1,4 +1,8 @@
_default:
just --list
# Test out the grammar
test-grammar:
#!/usr/bin/env bash
tree-sitter generate
tree-sitter test

View File

@ -4,17 +4,23 @@ Initial test
fn main() {
}
----
(source_file
(function_definition
(identifier)
(parameter_list)
(block)
)
)
====
Another test
====
fn yolo() bool { }
----
(source_file)
=====
Another
====
fn main() bool { }
------
()
(source_file
(function_definition
(identifier) (parameter_list) (primitive_type) (block)))