-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathparse_statement.go
More file actions
62 lines (55 loc) · 1.98 KB
/
parse_statement.go
File metadata and controls
62 lines (55 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package java
import (
"github.com/shivasurya/code-pathfinder/sourcecode-parser/model"
sitter "github.com/smacker/go-tree-sitter"
)
func ParseBreakStatement(node *sitter.Node, sourcecode []byte) *model.BreakStmt {
breakStmt := &model.BreakStmt{}
// get identifier if present child
for i := 0; i < int(node.ChildCount()); i++ {
if node.Child(i).Type() == "identifier" {
breakStmt.Label = node.Child(i).Content(sourcecode)
}
}
return breakStmt
}
func ParseContinueStatement(node *sitter.Node, sourcecode []byte) *model.ContinueStmt {
continueStmt := &model.ContinueStmt{}
// get identifier if present child
for i := 0; i < int(node.ChildCount()); i++ {
if node.Child(i).Type() == "identifier" {
continueStmt.Label = node.Child(i).Content(sourcecode)
}
}
return continueStmt
}
func ParseYieldStatement(node *sitter.Node, sourcecode []byte) *model.YieldStmt {
yieldStmt := &model.YieldStmt{}
yieldStmtExpr := &model.Expr{NodeString: node.Child(1).Content(sourcecode)}
yieldStmt.Value = yieldStmtExpr
return yieldStmt
}
func ParseAssertStatement(node *sitter.Node, sourcecode []byte) *model.AssertStmt {
assertStmt := &model.AssertStmt{}
assertStmt.Expr = &model.Expr{NodeString: node.Child(1).Content(sourcecode)}
if node.Child(3) != nil && node.Child(3).Type() == "string_literal" {
assertStmt.Message = &model.Expr{NodeString: node.Child(3).Content(sourcecode)}
}
return assertStmt
}
func ParseReturnStatement(node *sitter.Node, sourcecode []byte) *model.ReturnStmt {
returnStmt := &model.ReturnStmt{}
if node.Child(1) != nil {
returnStmt.Result = &model.Expr{NodeString: node.Child(1).Content(sourcecode)}
}
return returnStmt
}
func ParseBlockStatement(node *sitter.Node, sourcecode []byte) *model.BlockStmt {
blockStmt := &model.BlockStmt{}
for i := 0; i < int(node.ChildCount()); i++ {
singleBlockStmt := &model.Stmt{}
singleBlockStmt.NodeString = node.Child(i).Content(sourcecode)
blockStmt.Stmts = append(blockStmt.Stmts, *singleBlockStmt)
}
return blockStmt
}