Skip to content
108 changes: 108 additions & 0 deletions sourcecode-parser/graph/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
JavaDoc *model.Javadoc
BinaryExpr *model.BinaryExpr
ClassInstanceExpr *model.ClassInstanceExpr
IfStmt *model.IfStmt
WhileStmt *model.WhileStmt
DoStmt *model.DoStmt
ForStmt *model.ForStmt
}

type Edge struct {
Expand Down Expand Up @@ -172,6 +176,110 @@
func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, currentContext *Node, file string) {
isJavaSourceFile := isJavaSourceFile(file)
switch node.Type() {
case "if_statement":
ifNode := model.IfStmt{}
// get the condition of the if statement
conditionNode := node.Child(1)
if conditionNode != nil {
ifNode.Condition = &model.Expr{Node: *conditionNode, NodeString: conditionNode.Content(sourceCode)}
}
// get the then block of the if statement
thenNode := node.Child(2)
if thenNode != nil {
ifNode.Then = model.Stmt{NodeString: thenNode.Content(sourceCode)}
}
// get the else block of the if statement
elseNode := node.Child(4)
if elseNode != nil {
ifNode.Else = model.Stmt{NodeString: elseNode.Content(sourceCode)}
}

methodID := fmt.Sprintf("ifstmt_%d_%d_%s", node.StartPoint().Row+1, node.StartPoint().Column+1, file)
// add node to graph
ifStmtNode := &Node{
ID: GenerateSha256(methodID),
Type: "IfStmt",
Name: "IfStmt",
IsExternal: true,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
File: file,
isJavaSourceFile: isJavaSourceFile,
IfStmt: &ifNode,
}
graph.AddNode(ifStmtNode)
case "while_statement":
whileNode := model.WhileStmt{}
// get the condition of the while statement
conditionNode := node.Child(1)
if conditionNode != nil {
whileNode.Condition = &model.Expr{Node: *conditionNode, NodeString: conditionNode.Content(sourceCode)}
}
methodID := fmt.Sprintf("while_stmt_%d_%d_%s", node.StartPoint().Row+1, node.StartPoint().Column+1, file)
// add node to graph
whileStmtNode := &Node{
ID: GenerateSha256(methodID),
Type: "WhileStmt",
Name: "WhileStmt",
IsExternal: true,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
File: file,
isJavaSourceFile: isJavaSourceFile,
WhileStmt: &whileNode,
}
graph.AddNode(whileStmtNode)
case "do_statement":
doWhileNode := model.DoStmt{}
// get the condition of the while statement
conditionNode := node.Child(2)
if conditionNode != nil {
doWhileNode.Condition = &model.Expr{Node: *conditionNode, NodeString: conditionNode.Content(sourceCode)}
}
methodID := fmt.Sprintf("dowhile_stmt_%d_%d_%s", node.StartPoint().Row+1, node.StartPoint().Column+1, file)
// add node to graph
doWhileStmtNode := &Node{
ID: GenerateSha256(methodID),
Type: "DoStmt",
Name: "DoStmt",
IsExternal: true,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
File: file,
isJavaSourceFile: isJavaSourceFile,
DoStmt: &doWhileNode,
}
graph.AddNode(doWhileStmtNode)
case "for_statement":
forNode := model.ForStmt{}
// get the condition of the while statement
initNode := node.ChildByFieldName("init")
if initNode != nil {
forNode.Init = &model.Expr{Node: *initNode, NodeString: initNode.Content(sourceCode)}
}
conditionNode := node.ChildByFieldName("condition")
if conditionNode != nil {
forNode.Condition = &model.Expr{Node: *conditionNode, NodeString: conditionNode.Content(sourceCode)}
}
incrementNode := node.ChildByFieldName("increment")
if incrementNode != nil {
forNode.Increment = &model.Expr{Node: *incrementNode, NodeString: incrementNode.Content(sourceCode)}
}

Check warning on line 267 in sourcecode-parser/graph/construct.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/graph/construct.go#L266-L267

Added lines #L266 - L267 were not covered by tests

methodID := fmt.Sprintf("for_stmt_%d_%d_%s", node.StartPoint().Row+1, node.StartPoint().Column+1, file)
// add node to graph
forStmtNode := &Node{
ID: GenerateSha256(methodID),
Type: "ForStmt",
Name: "ForStmt",
IsExternal: true,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
File: file,
isJavaSourceFile: isJavaSourceFile,
ForStmt: &forNode,
}
graph.AddNode(forStmtNode)
case "binary_expression":
leftNode := node.ChildByFieldName("left")
rightNode := node.ChildByFieldName("right")
Expand Down
20 changes: 17 additions & 3 deletions sourcecode-parser/graph/construct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,13 +753,27 @@ func TestBuildGraphFromAST(t *testing.T) {
int i = 1 | 1;
int j = 1 ^ 1;
int l = 1 >>> 1;
while (a > 0) {
a--;
}
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
do {
System.out.println("Hello, World!");
} while (a > 0);
if (a < 0) {
System.out.println("Negative number");
} else {
System.out.println("Positive number");
}
return (5 > 3) && (10 <= 20) || (15 != 12) || (20 > 15);
}
}
`,
expectedNodes: 49,
expectedEdges: 0,
expectedTypes: []string{"class_declaration", "method_declaration", "binary_expression", "comp_expression", "and_expression", "or_expression"},
expectedNodes: 63,
expectedEdges: 4,
expectedTypes: []string{"class_declaration", "method_declaration", "binary_expression", "comp_expression", "and_expression", "or_expression", "IfStmt", "ForStmt", "WhileStmt", "DoStmt"},
unexpectedTypes: []string{""},
},
{
Expand Down
44 changes: 44 additions & 0 deletions sourcecode-parser/graph/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@
return env.Node.ClassInstanceExpr.ClassName
}

func (env *Env) GetIfStmt() *model.IfStmt {
return env.Node.IfStmt

Check warning on line 123 in sourcecode-parser/graph/query.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/graph/query.go#L122-L123

Added lines #L122 - L123 were not covered by tests
}

func (env *Env) GetWhileStmt() *model.WhileStmt {
return env.Node.WhileStmt

Check warning on line 127 in sourcecode-parser/graph/query.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/graph/query.go#L126-L127

Added lines #L126 - L127 were not covered by tests
}

func (env *Env) GetDoStmt() *model.DoStmt {
return env.Node.DoStmt

Check warning on line 131 in sourcecode-parser/graph/query.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/graph/query.go#L130-L131

Added lines #L130 - L131 were not covered by tests
}

func (env *Env) GetForStmt() *model.ForStmt {
return env.Node.ForStmt

Check warning on line 135 in sourcecode-parser/graph/query.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/graph/query.go#L134-L135

Added lines #L134 - L135 were not covered by tests
}

func QueryEntities(graph *CodeGraph, query parser.Query) (nodes [][]*Node, output [][]interface{}) {
result := make([][]*Node, 0)

Expand Down Expand Up @@ -290,6 +306,10 @@
unsignedRightShiftExpression := "unsigned_right_shift_expression"
xorBitwsieExpression := "xor_bitwise_expression"
classInstanceExpression := "ClassInstanceExpr"
ifStmt := "IfStmt"
whileStmt := "WhileStmt"
doStmt := "DoStmt"
forStmt := "ForStmt"

// print query select list
for _, entity := range query.SelectList {
Expand Down Expand Up @@ -338,6 +358,14 @@
xorBitwsieExpression = entity.Alias
case "ClassInstanceExpr":
classInstanceExpression = entity.Alias
case "IfStmt":
ifStmt = entity.Alias
case "WhileStmt":
whileStmt = entity.Alias
case "DoStmt":
doStmt = entity.Alias
case "ForStmt":
forStmt = entity.Alias

Check warning on line 368 in sourcecode-parser/graph/query.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/graph/query.go#L361-L368

Added lines #L361 - L368 were not covered by tests
}
}
env := map[string]interface{}{
Expand Down Expand Up @@ -468,6 +496,22 @@
"toString": proxyenv.ToString,
"getClassInstanceExpr": proxyenv.GetClassInstanceExpr,
},
ifStmt: map[string]interface{}{
"getIfStmt": proxyenv.GetIfStmt,
"toString": proxyenv.ToString,
},
whileStmt: map[string]interface{}{
"getWhileStmt": proxyenv.GetWhileStmt,
"toString": proxyenv.ToString,
},
doStmt: map[string]interface{}{
"getDoStmt": proxyenv.GetDoStmt,
"toString": proxyenv.ToString,
},
forStmt: map[string]interface{}{
"getForStmt": proxyenv.GetForStmt,
"toString": proxyenv.ToString,
},
}
return env
}
Expand Down
118 changes: 118 additions & 0 deletions sourcecode-parser/model/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package model

type Container struct {
Top
}

func (c *Container) ToString() string {
return ""

Check warning on line 8 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L7-L8

Added lines #L7 - L8 were not covered by tests
}

type File struct {
Container
File string
}

func (f *File) IsSourceFile() bool {
// check if file extension is .java
if f.File[len(f.File)-5:] == ".java" || f.File[len(f.File)-4:] == ".kt" {
return true
}
return false
}

func (f *File) IsJavaSourceFile() bool {
return f.File[len(f.File)-5:] == ".java"
}

func (f *File) IsKotlinSourceFile() bool {
return f.File[len(f.File)-5:] == ".kt"
}

func (f *File) GetAPrimaryQlClass() string {
return "File"

Check warning on line 33 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L32-L33

Added lines #L32 - L33 were not covered by tests
}

type CompilationUnit struct {
File
module Module
CuPackage Package
Name string
}

func (c *CompilationUnit) GetAPrimaryQlClass() string {
return "CompilationUnit"

Check warning on line 44 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L43-L44

Added lines #L43 - L44 were not covered by tests
}

func (c *CompilationUnit) GetModule() Module {
return c.module

Check warning on line 48 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L47-L48

Added lines #L47 - L48 were not covered by tests
}

func (c *CompilationUnit) GetName() string {
return c.Name

Check warning on line 52 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L51-L52

Added lines #L51 - L52 were not covered by tests
}

func (c *CompilationUnit) GetPackage() Package {
return c.CuPackage

Check warning on line 56 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L55-L56

Added lines #L55 - L56 were not covered by tests
}

func (c *CompilationUnit) HasName(name string) bool {
return c.Name == name
}

func (c *CompilationUnit) ToString() string {
return c.Name

Check warning on line 64 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L63-L64

Added lines #L63 - L64 were not covered by tests
}

type JarFile struct {
File
JarFile string
ImplementationVersion string
ManifestEntryAttributes map[string]map[string]string
ManifestMainAttributes map[string]string
SpecificationVersion string
}

func (j *JarFile) GetAPrimaryQlClass() string {
return "JarFile"

Check warning on line 77 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L76-L77

Added lines #L76 - L77 were not covered by tests
}

func (j *JarFile) GetJarFile() string {
return j.JarFile

Check warning on line 81 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L80-L81

Added lines #L80 - L81 were not covered by tests
}

func (j *JarFile) GetImplementationVersion() string {
return j.ImplementationVersion

Check warning on line 85 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L84-L85

Added lines #L84 - L85 were not covered by tests
}

func (j *JarFile) GetManifestEntryAttributes(entry, key string) (string, bool) {
if attributes, exists := j.ManifestEntryAttributes[entry]; exists {
if value, keyExists := attributes[key]; keyExists {
return value, true
}
}
return "", false
}

func (j *JarFile) GetManifestMainAttributes(key string) (string, bool) {
if value, exists := j.ManifestMainAttributes[key]; exists {
return value, true
}
return "", false
}

func (j *JarFile) GetSpecificationVersion() string {
return j.SpecificationVersion

Check warning on line 105 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L104-L105

Added lines #L104 - L105 were not covered by tests
}

type Package struct {
Package string
}

func (p *Package) GetAPrimaryQlClass() string {
return "Package"

Check warning on line 113 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L112-L113

Added lines #L112 - L113 were not covered by tests
}

func (p *Package) GetURL() string {
return p.Package

Check warning on line 117 in sourcecode-parser/model/container.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/model/container.go#L116-L117

Added lines #L116 - L117 were not covered by tests
}
Loading
Loading