From b04fd3e6c9fe11e11732bceef176692c96e1dbff Mon Sep 17 00:00:00 2001 From: Shivasurya Date: Fri, 19 Apr 2024 12:50:08 -0400 Subject: [PATCH] Added method query like interface Path-Finder Query Console: >FIND method WHERE name = 'onCreate' yields results of methods named as oncreate --- .gitignore | 2 ++ sourcecode-parser/construct.go | 3 ++- sourcecode-parser/main.go | 10 ++++++++-- sourcecode-parser/query_test.go | 20 -------------------- sourcecode-parser/source_sink.go | 14 ++++++++++++++ 5 files changed, 26 insertions(+), 23 deletions(-) delete mode 100644 sourcecode-parser/query_test.go diff --git a/.gitignore b/.gitignore index d8295f13..0925bf72 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ example-java-project/ main +sourcecode-parser/sourcecode-parser +.idea/ diff --git a/sourcecode-parser/construct.go b/sourcecode-parser/construct.go index 25f4155d..0f06e2d4 100644 --- a/sourcecode-parser/construct.go +++ b/sourcecode-parser/construct.go @@ -232,7 +232,7 @@ func readFile(path string) ([]byte, error) { return content, nil } -func Initialize(directory string) { +func Initialize(directory string) *CodeGraph { // Initialize the parser parser := sitter.NewParser() defer parser.Close() @@ -271,4 +271,5 @@ func Initialize(directory string) { // go StartServer(codeGraph) // select {} + return codeGraph } diff --git a/sourcecode-parser/main.go b/sourcecode-parser/main.go index c905c383..81970dd1 100644 --- a/sourcecode-parser/main.go +++ b/sourcecode-parser/main.go @@ -11,9 +11,10 @@ import ( func main() { // accept command line param optional path to source code sourceDirectory := "." + graph := NewCodeGraph() if len(os.Args) > 1 { sourceDirectory = os.Args[1] - Initialize(sourceDirectory) + graph = Initialize(sourceDirectory) } var input string fmt.Print("Path-Finder Query Console: \n>") @@ -34,6 +35,11 @@ func main() { fmt.Println(err) } } else { - fmt.Println(query) + entities := QueryEntities(graph, query) + fmt.Println("------Query Results------") + for _, entity := range entities { + fmt.Println(entity.CodeSnippet) + } + fmt.Println("-------------------") } } diff --git a/sourcecode-parser/query_test.go b/sourcecode-parser/query_test.go deleted file mode 100644 index 4e6d6210..00000000 --- a/sourcecode-parser/query_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "queryparser" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestParseQuery(t *testing.T) { - input := "FIND class WHERE name = 'MyClass' AND visibility = 'public' AND method = 'main'" - - lex := queryparser.NewLexer(input) - pars := queryparser.NewParser(lex) - - query := pars.ParseQuery() - - assert.NotNil(t, query) - assert.Equal(t, "MyClass", query.Conditions[0].Value) -} diff --git a/sourcecode-parser/source_sink.go b/sourcecode-parser/source_sink.go index 489d3e13..9faf8913 100644 --- a/sourcecode-parser/source_sink.go +++ b/sourcecode-parser/source_sink.go @@ -3,6 +3,7 @@ package main import ( "fmt" + "queryparser" ) type SourceSinkPath struct { @@ -68,3 +69,16 @@ func AnalyzeSourceSinkPatterns(graph *CodeGraph, sourceMethodName, sinkMethodNam // Return true if sourceNode is connected to sinkNode as a result of the DFS return Result{IsConnected: isConnected, SourceMethod: sourceNode.CodeSnippet, SinkMethod: sinkNode.CodeSnippet, SourceLine: sourceNode.LineNumber, SinkLine: sinkNode.LineNumber} } + +func QueryEntities(graph *CodeGraph, query *queryparser.Query) []*GraphNode { + result := make([]*GraphNode, 0) + if query.Entity == "method" { + for _, node := range graph.Nodes { + // create array of nodes that match the query + if node.Type == "method_declaration" && node.Name == query.Conditions[0].Value { + result = append(result, node) + } + } + } + return result +}