From db6443a6a7e6c947a6a2dfd9e99264eb03292202 Mon Sep 17 00:00:00 2001 From: Shivasurya Date: Sun, 5 May 2024 22:30:48 -0400 Subject: [PATCH] Added support for output format as json --- sourcecode-parser/main.go | 85 +++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 31 deletions(-) diff --git a/sourcecode-parser/main.go b/sourcecode-parser/main.go index 75e35ff..878beb4 100644 --- a/sourcecode-parser/main.go +++ b/sourcecode-parser/main.go @@ -2,46 +2,34 @@ package main import ( "bufio" + "encoding/json" + "flag" "fmt" "os" "queryparser" "strings" ) -func main() { - // accept command line param optional path to source code - var sourceDirectory string - graph := NewCodeGraph() - if len(os.Args) > 1 { - sourceDirectory = os.Args[1] - graph = Initialize(sourceDirectory) - } - // loop to accept queries - for { - var input string - fmt.Print("Path-Finder Query Console: \n>") - in := bufio.NewReader(os.Stdin) - - input, err := in.ReadString('\n') - if err != nil { - fmt.Println("Error reading input") - return - } - // if input starts with :quit string - if strings.HasPrefix(input, ":quit") { - return +func processQuery(input string, graph *CodeGraph, output string) { + lex := queryparser.NewLexer(input) + pars := queryparser.NewParser(lex) + query := pars.ParseQuery() + if query == nil { + fmt.Println("Failed to parse query:") + for _, err := range pars.Errors() { + fmt.Println(err) } - fmt.Print("Executing query: " + input) - lex := queryparser.NewLexer(input) - pars := queryparser.NewParser(lex) - query := pars.ParseQuery() - if query == nil { - fmt.Println("Failed to parse query:") - for _, err := range pars.Errors() { - fmt.Println(err) + } else { + entities := QueryEntities(graph, query) + if output == "json" { + // convert struct to query_results + queryResults, err := json.Marshal(entities) + if err != nil { + fmt.Println("Error processing query results") + } else { + fmt.Println(string(queryResults)) } } else { - entities := QueryEntities(graph, query) fmt.Println("------Query Results------") for _, entity := range entities { fmt.Println(entity.CodeSnippet) @@ -50,3 +38,38 @@ func main() { } } } + +func main() { + // accept command line param optional path to source code + graph := NewCodeGraph() + output := flag.String("output", "", "Supported output format: json") + query := flag.String("query", "", "Query to execute") + project := flag.String("project", "", "Project to analyze") + flag.Parse() + // loop to accept queries + if project != nil { + graph = Initialize(*project) + } + // check if output and query are provided + if output != nil && query != nil && *output != "" && *query != "" { + processQuery(*query, graph, *output) + } else { + for { + var input string + fmt.Print("Path-Finder Query Console: \n>") + in := bufio.NewReader(os.Stdin) + + input, err := in.ReadString('\n') + if err != nil { + fmt.Println("Error reading input") + return + } + // if input starts with :quit string + if strings.HasPrefix(input, ":quit") { + return + } + fmt.Print("Executing query: " + input) + processQuery(input, graph, "text") + } + } +}