Skip to content

Commit

Permalink
Merge pull request #19 from shivasurya/shiva/variable-declaration
Browse files Browse the repository at this point in the history
Added variable declaration support 🧰
  • Loading branch information
shivasurya authored May 7, 2024
2 parents 6ae7f80 + 7bf1b26 commit eb6c6a1
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions sourcecode-parser/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type GraphNode struct {
ImportPackage []string
SuperClass string
Interface []string
DataType string
Scope string
VariableValue string
}

type GraphEdge struct {
Expand Down Expand Up @@ -193,6 +196,60 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
Interface: implementedInterface,
}
graph.AddNode(classNode)
case "local_variable_declaration", "field_declaration":
// Extract variable name, type, and modifiers
variableName := ""
variableType := ""
variableModifier := ""
variableValue := ""
var scope string
if node.Type() == "local_variable_declaration" {
scope = "local"
} else {
scope = "field"
}
for i := 0; i < int(node.ChildCount()); i++ {
child := node.Child(i)
switch child.Type() {
case "variable_declarator":
variableName = child.Content(sourceCode)
for j := 0; j < int(child.ChildCount()); j++ {
if child.Child(j).Type() == "identifier" {
variableName = child.Child(j).Content(sourceCode)
}
// if child type contains =, iterate through and get remaining content
if child.Child(j).Type() == "=" {
for k := j + 1; k < int(child.ChildCount()); k++ {
variableValue += child.Child(k).Content(sourceCode)
}
}

}
// remove spaces from variable value
variableValue = strings.ReplaceAll(variableValue, " ", "")
// remove new line from variable value
variableValue = strings.ReplaceAll(variableValue, "\n", "")
case "modifiers":
variableModifier = child.Content(sourceCode)
}
// if child type contains type, get the type of variable
if strings.Contains(child.Type(), "type") {
variableType = child.Content(sourceCode)
}
}
// Create a new node for the variable
variableNode := &GraphNode{
ID: generateMethodID(variableName, []string{}),
Type: "variable_declaration",
Name: variableName,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
Modifier: extractVisibilityModifier(variableModifier),
DataType: variableType,
Scope: scope,
VariableValue: variableValue,
}
graph.AddNode(variableNode)
}

// Recursively process child nodes
Expand Down

0 comments on commit eb6c6a1

Please sign in to comment.