Skip to content

Commit

Permalink
Added support for output format as json
Browse files Browse the repository at this point in the history
  • Loading branch information
shivasurya committed May 6, 2024
1 parent 1c1809d commit db6443a
Showing 1 changed file with 54 additions and 31 deletions.
85 changes: 54 additions & 31 deletions sourcecode-parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
}
}
}

0 comments on commit db6443a

Please sign in to comment.