@@ -15,6 +15,28 @@ pub mut:
1515// Source represent the possible types of V source code to parse.
1616type Source = []byte | string
1717
18+ // Parser is a wrapper around the Tree-sitter V parser.
19+ pub struct Parser {
20+ mut :
21+ binding_parser & bindings.Parser[bindings.NodeType] = unsafe { nil }
22+ }
23+
24+ // new creates a new Parser instance.
25+ pub fn Parser .new () & Parser {
26+ mut bp := bindings.new_parser[bindings.NodeType](bindings.type_factory)
27+ bp.set_language (bindings.language)
28+ return & Parser{
29+ binding_parser: bp
30+ }
31+ }
32+
33+ // free frees the Tree-sitter parser.
34+ pub fn (p &Parser) free () {
35+ unsafe {
36+ p.binding_parser.free ()
37+ }
38+ }
39+
1840// parse_file parses a V source file and returns the corresponding `tree_sitter.Tree` and `Rope`.
1941// If the file could not be read, an error is returned.
2042// If the file was read successfully, but could not be parsed, the result
@@ -25,18 +47,19 @@ type Source = []byte | string
2547// import parser
2648//
2749// fn main() {
28- // res := parser.parse_file('foo.v') or {
50+ // mut p := parser.Parser.new()
51+ // res := p.parse_file('foo.v') or {
2952// eprintln('Error: could not parse file: ${err}')
3053// return
3154// }
3255// println(res.tree)
3356// }
3457// ```
35- pub fn parse_file (filename string ) ! ParseResult {
36- mut file := os.read_file (filename) or {
37- return error ( 'could not read file ${filename} : ${err} ' )
38- }
39- return parse_source (file)
58+ pub fn (mut p Parser) parse_file (filename string ) ! ParseResult {
59+ content := os.read_file (filename) or { return error ( 'could not read file ${filename} : ${err} ' ) }
60+ mut res := p. parse_source (content )
61+ res. path = filename
62+ return res
4063}
4164
4265// parse_source parses a V code and returns the corresponding `tree_sitter.Tree` and `Rope`.
@@ -48,14 +71,15 @@ pub fn parse_file(filename string) !ParseResult {
4871// import parser
4972//
5073// fn main() {
51- // res := parser.parse_source('fn main() { println("Hello, World!") }') or {
74+ // mut p := parser.Parser.new()
75+ // res := p.parse_source('fn main() { println("Hello, World!") }') or {
5276// eprintln('Error: could not parse source: ${err}')
5377// return
5478// }
5579// println(res.tree)
5680// }
5781// ```
58- pub fn parse_source (source Source) ParseResult {
82+ pub fn (mut p Parser) parse_source (source Source) ParseResult {
5983 code := match source {
6084 string {
6185 source
@@ -64,14 +88,18 @@ pub fn parse_source(source Source) ParseResult {
6488 source.str ()
6589 }
6690 }
67- return parse_code (code)
91+ return p. parse_code (code)
6892}
6993
7094// parse_code parses a V code and returns the corresponding `tree_sitter.Tree` and `Rope`.
7195// Unlike `parse_file` and `parse_source`, `parse_code` don't return an error since
7296// the source is always valid.
73- pub fn parse_code (code string ) ParseResult {
74- return parse_code_with_tree (code, unsafe { nil })
97+ pub fn (mut p Parser) parse_code (code string ) ParseResult {
98+ tree := p.binding_parser.parse_string (source: code)
99+ return ParseResult{
100+ tree: tree
101+ source_text: code
102+ }
75103}
76104
77105// parse_code_with_tree parses a V code and returns the corresponding `tree_sitter.Tree` and `Rope`.
@@ -86,19 +114,18 @@ pub fn parse_code(code string) ParseResult {
86114// import parser
87115//
88116// fn main() {
117+ // mut p := parser.Parser.new()
89118// code := 'fn main() { println("Hello, World!") }'
90- // res := parser .parse_code_with_tree(code, unsafe { nil })
119+ // res := p .parse_code_with_tree(code, unsafe { nil })
91120// println(res.tree)
92121// // some changes in code
93122// code2 := 'fn foo() { println("Hello, World!") }'
94- // res2 = parser .parse_code_with_tree(code2, res.tree)
123+ // res2 = p .parse_code_with_tree(code2, res.tree)
95124// println(res2.tree
96125// }
97- pub fn parse_code_with_tree (code string , old_tree & bindings.Tree[bindings.NodeType]) ParseResult {
98- mut parser := bindings.new_parser[bindings.NodeType](bindings.type_factory)
99- parser.set_language (bindings.language)
126+ pub fn (mut p Parser) parse_code_with_tree (code string , old_tree & bindings.Tree[bindings.NodeType]) ParseResult {
100127 raw_tree := if isnil (old_tree) { unsafe { nil } } else { old_tree.raw_tree }
101- tree := parser .parse_string (source: code, tree: raw_tree)
128+ tree := p.binding_parser .parse_string (source: code, tree: raw_tree)
102129 return ParseResult{
103130 tree: tree
104131 source_text: code
0 commit comments