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