@@ -22,8 +22,8 @@ import (
2222 "path/filepath"
2323 "strings"
2424
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 "
2727)
2828
2929const (
@@ -61,9 +61,12 @@ func NewFileParser() *FileParser {
6161// It prints a warning if parsing fails.
6262func ParseCode (code []byte , path string ) (* sitter.Node , error ) {
6363 parser := sitter .NewParser ()
64- parser .SetLanguage (sitter . NewLanguage ( python .Language () ))
64+ parser .SetLanguage (python .GetLanguage ( ))
6565
66- tree := parser .Parse (code , nil )
66+ tree , err := parser .ParseCtx (context .Background (), nil , code )
67+ if err != nil {
68+ return nil , err
69+ }
6770
6871 root := tree .RootNode ()
6972 if ! root .HasError () {
@@ -79,16 +82,16 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
7982 return root , nil
8083 }
8184
82- for i := uint ( 0 ) ; i < root .ChildCount (); i ++ {
85+ for i := 0 ; i < int ( root .ChildCount () ); i ++ {
8386 child := root .Child (i )
8487 if child .IsError () {
8588 // Example logs:
8689 // gazelle: Parse error at {Row:1 Column:0}:
8790 // 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 ))
8992 // Log the internal tree-sitter representation of what was parsed. Eg:
9093 // 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 ())
9295 }
9396 }
9497
@@ -98,21 +101,21 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
98101// parseMain returns true if the python file has an `if __name__ == "__main__":` block,
99102// which is a common idiom for python scripts/binaries.
100103func (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 ++ {
102105 if err := ctx .Err (); err != nil {
103106 return false
104107 }
105108 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 () == "==" {
108111 statement := child .Child (1 )
109112 a , b := statement .Child (0 ), statement .Child (2 )
110113 // convert "'__main__' == __name__" to "__name__ == '__main__'"
111- if b .Kind () == sitterNodeTypeIdentifier {
114+ if b .Type () == sitterNodeTypeIdentifier {
112115 a , b = b , a
113116 }
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__" {
116119 return true
117120 }
118121 }
@@ -123,18 +126,18 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
123126// parseImportStatement parses a node for an import statement, returning a `module` and a boolean
124127// representing if the parse was OK or not.
125128func parseImportStatement (node * sitter.Node , code []byte ) (module , bool ) {
126- switch node .Kind () {
129+ switch node .Type () {
127130 case sitterNodeTypeDottedName :
128131 return module {
129- Name : node .Utf8Text (code ),
130- LineNumber : node .StartPosition ().Row + 1 ,
132+ Name : node .Content (code ),
133+ LineNumber : node .StartPoint ().Row + 1 ,
131134 }, true
132135 case sitterNodeTypeAliasedImport :
133136 return parseImportStatement (node .Child (0 ), code )
134137 case sitterNodeTypeWildcardImport :
135138 return module {
136139 Name : "*" ,
137- LineNumber : node .StartPosition ().Row + 1 ,
140+ LineNumber : node .StartPoint ().Row + 1 ,
138141 }, true
139142 }
140143 return module {}, false
@@ -144,8 +147,8 @@ func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
144147// an import statement. It updates FileParser.output.Modules with the `module` that the
145148// import represents.
146149func (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 ++ {
149152 m , ok := parseImportStatement (node .Child (j ), p .code )
150153 if ! ok {
151154 continue
@@ -156,12 +159,12 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
156159 }
157160 p .output .Modules = append (p .output .Modules , m )
158161 }
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 )
161164 if strings .HasPrefix (from , "." ) {
162165 return true
163166 }
164- for j := uint ( 3 ) ; j < node .ChildCount (); j ++ {
167+ for j := 3 ; j < int ( node .ChildCount () ); j ++ {
165168 m , ok := parseImportStatement (node .Child (j ), p .code )
166169 if ! ok {
167170 continue
@@ -180,8 +183,8 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
180183// parseComments parses a node for comments, returning true if the node is a comment.
181184// It updates FileParser.output.Comments with the parsed comment.
182185func (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 )))
185188 return true
186189 }
187190 return false
@@ -197,7 +200,7 @@ func (p *FileParser) parse(ctx context.Context, node *sitter.Node) {
197200 if node == nil {
198201 return
199202 }
200- for i := uint ( 0 ) ; i < node .ChildCount (); i ++ {
203+ for i := 0 ; i < int ( node .ChildCount () ); i ++ {
201204 if err := ctx .Err (); err != nil {
202205 return
203206 }
0 commit comments