@@ -22,8 +22,8 @@ import (
22
22
"path/filepath"
23
23
"strings"
24
24
25
- sitter "github.com/smacker /go-tree-sitter"
26
- "github.com/smacker/go- tree-sitter/python"
25
+ sitter "github.com/tree-sitter /go-tree-sitter"
26
+ python "github.com/tree-sitter/tree-sitter- python/bindings/go "
27
27
)
28
28
29
29
const (
@@ -61,12 +61,9 @@ func NewFileParser() *FileParser {
61
61
// It prints a warning if parsing fails.
62
62
func ParseCode (code []byte , path string ) (* sitter.Node , error ) {
63
63
parser := sitter .NewParser ()
64
- parser .SetLanguage (python .GetLanguage ( ))
64
+ parser .SetLanguage (sitter . NewLanguage ( python .Language () ))
65
65
66
- tree , err := parser .ParseCtx (context .Background (), nil , code )
67
- if err != nil {
68
- return nil , err
69
- }
66
+ tree := parser .Parse (code , nil )
70
67
71
68
root := tree .RootNode ()
72
69
if ! root .HasError () {
@@ -82,16 +79,16 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
82
79
return root , nil
83
80
}
84
81
85
- for i := 0 ; i < int ( root .ChildCount () ); i ++ {
82
+ for i := uint ( 0 ) ; i < root .ChildCount (); i ++ {
86
83
child := root .Child (i )
87
84
if child .IsError () {
88
85
// Example logs:
89
86
// gazelle: Parse error at {Row:1 Column:0}:
90
87
// def search_one_more_level[T]():
91
- log .Printf ("Parse error at %+v:\n %+v" , child .StartPoint (), child .Content (code ))
88
+ log .Printf ("Parse error at %+v:\n %+v" , child .StartPosition (), child .Utf8Text (code ))
92
89
// Log the internal tree-sitter representation of what was parsed. Eg:
93
90
// gazelle: The above was parsed as: (ERROR (identifier) (call function: (list (identifier)) arguments: (argument_list)))
94
- log .Printf ("The above was parsed as: %v" , child .String ())
91
+ log .Printf ("The above was parsed as: %v" , child .Kind ())
95
92
}
96
93
}
97
94
@@ -101,21 +98,21 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
101
98
// parseMain returns true if the python file has an `if __name__ == "__main__":` block,
102
99
// which is a common idiom for python scripts/binaries.
103
100
func (p * FileParser ) parseMain (ctx context.Context , node * sitter.Node ) bool {
104
- for i := 0 ; i < int ( node .ChildCount () ); i ++ {
101
+ for i := uint ( 0 ) ; i < node .ChildCount (); i ++ {
105
102
if err := ctx .Err (); err != nil {
106
103
return false
107
104
}
108
105
child := node .Child (i )
109
- if child .Type () == sitterNodeTypeIfStatement &&
110
- child .Child (1 ).Type () == sitterNodeTypeComparisonOperator && child .Child (1 ).Child (1 ).Type () == "==" {
106
+ if child .Kind () == sitterNodeTypeIfStatement &&
107
+ child .Child (1 ).Kind () == sitterNodeTypeComparisonOperator && child .Child (1 ).Child (1 ).Kind () == "==" {
111
108
statement := child .Child (1 )
112
109
a , b := statement .Child (0 ), statement .Child (2 )
113
110
// convert "'__main__' == __name__" to "__name__ == '__main__'"
114
- if b .Type () == sitterNodeTypeIdentifier {
111
+ if b .Kind () == sitterNodeTypeIdentifier {
115
112
a , b = b , a
116
113
}
117
- if a .Type () == sitterNodeTypeIdentifier && a .Content (p .code ) == "__name__" &&
118
- b .Type () == sitterNodeTypeString && string (p .code [b .StartByte ()+ 1 :b .EndByte ()- 1 ]) == "__main__" {
114
+ if a .Kind () == sitterNodeTypeIdentifier && a .Utf8Text (p .code ) == "__name__" &&
115
+ b .Kind () == sitterNodeTypeString && string (p .code [b .StartByte ()+ 1 :b .EndByte ()- 1 ]) == "__main__" {
119
116
return true
120
117
}
121
118
}
@@ -126,18 +123,18 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
126
123
// parseImportStatement parses a node for an import statement, returning a `module` and a boolean
127
124
// representing if the parse was OK or not.
128
125
func parseImportStatement (node * sitter.Node , code []byte ) (module , bool ) {
129
- switch node .Type () {
126
+ switch node .Kind () {
130
127
case sitterNodeTypeDottedName :
131
128
return module {
132
- Name : node .Content (code ),
133
- LineNumber : node .StartPoint ().Row + 1 ,
129
+ Name : node .Utf8Text (code ),
130
+ LineNumber : node .StartPosition ().Row + 1 ,
134
131
}, true
135
132
case sitterNodeTypeAliasedImport :
136
133
return parseImportStatement (node .Child (0 ), code )
137
134
case sitterNodeTypeWildcardImport :
138
135
return module {
139
136
Name : "*" ,
140
- LineNumber : node .StartPoint ().Row + 1 ,
137
+ LineNumber : node .StartPosition ().Row + 1 ,
141
138
}, true
142
139
}
143
140
return module {}, false
@@ -147,8 +144,8 @@ func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
147
144
// an import statement. It updates FileParser.output.Modules with the `module` that the
148
145
// import represents.
149
146
func (p * FileParser ) parseImportStatements (node * sitter.Node ) bool {
150
- if node .Type () == sitterNodeTypeImportStatement {
151
- for j := 1 ; j < int ( node .ChildCount () ); j ++ {
147
+ if node .Kind () == sitterNodeTypeImportStatement {
148
+ for j := uint ( 1 ) ; j < node .ChildCount (); j ++ {
152
149
m , ok := parseImportStatement (node .Child (j ), p .code )
153
150
if ! ok {
154
151
continue
@@ -159,12 +156,12 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
159
156
}
160
157
p .output .Modules = append (p .output .Modules , m )
161
158
}
162
- } else if node .Type () == sitterNodeTypeImportFromStatement {
163
- from := node .Child (1 ).Content (p .code )
159
+ } else if node .Kind () == sitterNodeTypeImportFromStatement {
160
+ from := node .Child (1 ).Utf8Text (p .code )
164
161
if strings .HasPrefix (from , "." ) {
165
162
return true
166
163
}
167
- for j := 3 ; j < int ( node .ChildCount () ); j ++ {
164
+ for j := uint ( 3 ) ; j < node .ChildCount (); j ++ {
168
165
m , ok := parseImportStatement (node .Child (j ), p .code )
169
166
if ! ok {
170
167
continue
@@ -183,8 +180,8 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
183
180
// parseComments parses a node for comments, returning true if the node is a comment.
184
181
// It updates FileParser.output.Comments with the parsed comment.
185
182
func (p * FileParser ) parseComments (node * sitter.Node ) bool {
186
- if node .Type () == sitterNodeTypeComment {
187
- p .output .Comments = append (p .output .Comments , comment (node .Content (p .code )))
183
+ if node .Kind () == sitterNodeTypeComment {
184
+ p .output .Comments = append (p .output .Comments , comment (node .Utf8Text (p .code )))
188
185
return true
189
186
}
190
187
return false
@@ -200,7 +197,7 @@ func (p *FileParser) parse(ctx context.Context, node *sitter.Node) {
200
197
if node == nil {
201
198
return
202
199
}
203
- for i := 0 ; i < int ( node .ChildCount () ); i ++ {
200
+ for i := uint ( 0 ) ; i < node .ChildCount (); i ++ {
204
201
if err := ctx .Err (); err != nil {
205
202
return
206
203
}
0 commit comments