Working simple tree-sitter grammar
This commit is contained in:
parent
45c4d08fb9
commit
dc771fc7ad
@ -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]+/,
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
_default:
|
||||
just --list
|
||||
|
||||
# Test out the grammar
|
||||
test-grammar:
|
||||
#!/usr/bin/env bash
|
||||
tree-sitter generate
|
||||
tree-sitter test
|
||||
|
@ -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)))
|
||||
|
Loading…
Reference in New Issue
Block a user