Skip to content

Commit b844dc1

Browse files
Add comments explaining what every function does, and helper notes to better understand how parser works
1 parent b0123b0 commit b844dc1

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

test-crawler/extractor/extractor.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ type FileData struct {
4141
Functions []c.Function
4242
}
4343

44+
type FunctionAnnotationNode struct {
45+
Node *sitter.Node
46+
Function c.FunctionAnnotation
47+
}
48+
4449
type hasNoTests bool
4550

4651
func ExtractInfo(file c.TestFile, ctx context.Context, fileID c.FileID) (*FileData, hasNoTests, error) {
@@ -98,7 +103,7 @@ func GetExportedFunctions(ctx context.Context, filePath string) ([]c.FunctionAnn
98103
}
99104

100105
cursor := sitter.NewTreeCursor(tree.RootNode())
101-
fnsAnno, err := parseContentForPublicFunctions(content, cursor)
106+
fnsAnno, err := parseContentForFunctions(content, cursor)
102107
if err != nil {
103108
return nil, err
104109
}
@@ -130,15 +135,15 @@ func parseContent(content string, treeCursor *sitter.TreeCursor, filePath string
130135
fileData.Metadata = getMetadata(content, treeCursor, &annotationParser)
131136
funcNodes := getFunctionNodes(content, treeCursor, &annotationParser)
132137

133-
for _, function := range funcNodes {
138+
for _, funcNode := range funcNodes {
134139

135-
behaviors := findBehaviorsFromNode(content, function.Node)
140+
behaviors := findBehaviorsFromNode(content, funcNode.Node)
136141
var callExpressions []string = nil
137142
if len(behaviors) > 0 {
138-
callExpressions = findCallExprFromNode(content, function.Node)
143+
callExpressions = findCallExprFromNode(content, funcNode.Node)
139144
}
140145

141-
fileData.Functions = append(fileData.Functions, makeCollectorScenario(filePath, function.Function.Name, behaviors, callExpressions))
146+
fileData.Functions = append(fileData.Functions, makeCollectorScenario(filePath, funcNode.Function.Name, behaviors, callExpressions))
142147

143148
}
144149

@@ -188,13 +193,11 @@ func getMetadata(content string, treeCursor *sitter.TreeCursor, parser *a.Parser
188193
return &meta
189194
}
190195

191-
// this method extracts functions and methods.
196+
// getFunctionNodes method extracts functions and methods.
192197
// Function can have 2 types: function_declaration (for example contructor)
193-
// and method_declaration (can be exported and unexported)
194-
func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.Parser) (funcAnnoPair []struct {
195-
Node *sitter.Node
196-
Function c.FunctionAnnotation
197-
}) {
198+
// and method_declaration (can be exported and unexported).
199+
// Return value is a slice of FunctionAnnotationNode, where each node holds function's annotation.
200+
func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.Parser) (funcAnnoPair []FunctionAnnotationNode) {
198201

199202
numChildsRootNode := treeCursor.CurrentNode().ChildCount()
200203
node := &sitter.Node{}
@@ -231,10 +234,7 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
231234
continue
232235
}
233236

234-
funcAnnoPair = append(funcAnnoPair, struct {
235-
Node *sitter.Node
236-
Function c.FunctionAnnotation
237-
}{
237+
funcAnnoPair = append(funcAnnoPair, FunctionAnnotationNode{
238238
Node: node,
239239
Function: c.FunctionAnnotation{
240240
Name: funcName,
@@ -245,6 +245,23 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
245245
funcName = ""
246246
isIgnored = false
247247
}
248+
/*
249+
Take a look at this function for example:
250+
*********
251+
func (e *ProofEventStream) ListenProofEvent(
252+
ctx context.Context,
253+
policy *types2.ProofRegisterPolicy)
254+
(<-chan *types2.RequestEvent, error){...}
255+
*********
256+
First iteration through children nodes:
257+
Child.Type = method_declaration
258+
Child.Value = whole function body func(*x)(a,b)c{...}
259+
260+
child.Child(1) is a first part of function declaration, and has type of parameter_list: (e *ProofEventStream)
261+
child.Child(2): has type of field_identifier and represents function's name: ListenProofEvent
262+
child.Child(3): has type parameter_list and represents input parameters: (ctx context.Context, policy *types2.ProofRegisterPolicy)
263+
child.Child(4): has type parameter_list and represent return parameters: (<-chan *types2.RequestEvent, error)
264+
*/
248265
if child.Type() == string(METHOD_DECLARATION) {
249266
funcName = content[child.Child(2).StartByte():child.Child(2).EndByte()]
250267
public := unicode.IsUpper(rune(funcName[0]))
@@ -254,10 +271,7 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
254271
params = content[child.Child(3).StartByte():child.Child(3).EndByte()]
255272
returnValues = content[child.Child(4).StartByte():child.Child(4).EndByte()]
256273
}
257-
funcAnnoPair = append(funcAnnoPair, struct {
258-
Node *sitter.Node
259-
Function c.FunctionAnnotation
260-
}{
274+
funcAnnoPair = append(funcAnnoPair, FunctionAnnotationNode{
261275
Node: child,
262276
Function: c.FunctionAnnotation{
263277
Name: funcName,
@@ -354,13 +368,15 @@ func makeID(filePath string, funcName string, behavior string) string {
354368
return string(hex.EncodeToString(hash[:]))
355369
}
356370

357-
func parseContentForPublicFunctions(content string, cursor *sitter.TreeCursor) ([]c.FunctionAnnotation, error) {
371+
// parseContentForFunctions accepts a content which represents whole golang file as a string,
372+
// parses it and returns a slice of function annotations (including exported and unexported ones).
373+
func parseContentForFunctions(content string, cursor *sitter.TreeCursor) ([]c.FunctionAnnotation, error) {
358374
var annotationParser a.Parser
359375

360376
var fnsAnno []c.FunctionAnnotation
361-
functions := getFunctionNodes(content, cursor, &annotationParser)
362-
for _, f := range functions {
363-
fnsAnno = append(fnsAnno, f.Function)
377+
funcNodes := getFunctionNodes(content, cursor, &annotationParser)
378+
for _, funcNode := range funcNodes {
379+
fnsAnno = append(fnsAnno, funcNode.Function)
364380
}
365381

366382
return fnsAnno, nil

0 commit comments

Comments
 (0)