Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for output format as json #17

Merged
merged 1 commit into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
}
Loading