@@ -22,8 +22,8 @@ import (
2222 "path/filepath"
2323 "strings"
2424
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 "
2727)
2828
2929const (
@@ -61,12 +61,9 @@ 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 (python .GetLanguage ( ))
64+ parser .SetLanguage (sitter . NewLanguage ( python .Language () ))
6565
66- tree , err := parser .ParseCtx (context .Background (), nil , code )
67- if err != nil {
68- return nil , err
69- }
66+ tree := parser .Parse (code , nil )
7067
7168 root := tree .RootNode ()
7269 if ! root .HasError () {
@@ -82,16 +79,16 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
8279 return root , nil
8380 }
8481
85- for i := 0 ; i < int ( root .ChildCount () ); i ++ {
82+ for i := uint ( 0 ) ; i < root .ChildCount (); i ++ {
8683 child := root .Child (i )
8784 if child .IsError () {
8885 // Example logs:
8986 // gazelle: Parse error at {Row:1 Column:0}:
9087 // 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 ))
9289 // Log the internal tree-sitter representation of what was parsed. Eg:
9390 // 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 ())
9592 }
9693 }
9794
@@ -101,21 +98,21 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
10198// parseMain returns true if the python file has an `if __name__ == "__main__":` block,
10299// which is a common idiom for python scripts/binaries.
103100func (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 ++ {
105102 if err := ctx .Err (); err != nil {
106103 return false
107104 }
108105 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 () == "==" {
111108 statement := child .Child (1 )
112109 a , b := statement .Child (0 ), statement .Child (2 )
113110 // convert "'__main__' == __name__" to "__name__ == '__main__'"
114- if b .Type () == sitterNodeTypeIdentifier {
111+ if b .Kind () == sitterNodeTypeIdentifier {
115112 a , b = b , a
116113 }
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__" {
119116 return true
120117 }
121118 }
@@ -126,18 +123,18 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
126123// parseImportStatement parses a node for an import statement, returning a `module` and a boolean
127124// representing if the parse was OK or not.
128125func parseImportStatement (node * sitter.Node , code []byte ) (module , bool ) {
129- switch node .Type () {
126+ switch node .Kind () {
130127 case sitterNodeTypeDottedName :
131128 return module {
132- Name : node .Content (code ),
133- LineNumber : node .StartPoint ().Row + 1 ,
129+ Name : node .Utf8Text (code ),
130+ LineNumber : node .StartPosition ().Row + 1 ,
134131 }, true
135132 case sitterNodeTypeAliasedImport :
136133 return parseImportStatement (node .Child (0 ), code )
137134 case sitterNodeTypeWildcardImport :
138135 return module {
139136 Name : "*" ,
140- LineNumber : node .StartPoint ().Row + 1 ,
137+ LineNumber : node .StartPosition ().Row + 1 ,
141138 }, true
142139 }
143140 return module {}, false
@@ -147,8 +144,8 @@ func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
147144// an import statement. It updates FileParser.output.Modules with the `module` that the
148145// import represents.
149146func (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 ++ {
152149 m , ok := parseImportStatement (node .Child (j ), p .code )
153150 if ! ok {
154151 continue
@@ -159,12 +156,12 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
159156 }
160157 p .output .Modules = append (p .output .Modules , m )
161158 }
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 )
164161 if strings .HasPrefix (from , "." ) {
165162 return true
166163 }
167- for j := 3 ; j < int ( node .ChildCount () ); j ++ {
164+ for j := uint ( 3 ) ; j < node .ChildCount (); j ++ {
168165 m , ok := parseImportStatement (node .Child (j ), p .code )
169166 if ! ok {
170167 continue
@@ -183,8 +180,8 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
183180// parseComments parses a node for comments, returning true if the node is a comment.
184181// It updates FileParser.output.Comments with the parsed comment.
185182func (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 )))
188185 return true
189186 }
190187 return false
@@ -200,7 +197,7 @@ func (p *FileParser) parse(ctx context.Context, node *sitter.Node) {
200197 if node == nil {
201198 return
202199 }
203- for i := 0 ; i < int ( node .ChildCount () ); i ++ {
200+ for i := uint ( 0 ) ; i < node .ChildCount (); i ++ {
204201 if err := ctx .Err (); err != nil {
205202 return
206203 }
0 commit comments