Hi Gervasius,

Do you have the final solution of your work? 

Thanks,


On Thursday, August 22, 2019 at 10:18:21 AM UTC-7, Gervasius Ishuuwa wrote:
>
> Hi Guys, 
>
> I am trying to replicate the following solution (
> https://stackoverflow.com/questions/49116223/convert-antlr-parse-tree-to-json)
>  
> in Golang. 
> The expected ouput is a nested JSON with this structure:
>
> {
>     "VeloContext": [
>         {
>             "AuthorContext": [
>                 {
>                     "...": [
>                         {
>                             "type": 7,
>                             "text": "..."
>                         }
>                     ]
>                 }
>             ],
>             "OrchestrationContext":[
>                 {...}
>             ]
>         }
>     ]}
>
> But getting the root node only, instead.
>
> {
>     "VeloContext":{}
>
> }
>
>
> I have attached a snapshot of my parse tree and debug output. Below is my 
> code thus far:
>
> package main
>
> import (
> "encoding/json"
> "reflect"
> "strconv"
> "strings"
> "time"
> "../parser"
> "github.com/antlr/antlr4/runtime/Go/antlr"
> "github.com/emirpasic/gods/lists/arraylist"
> "github.com/emirpasic/gods/maps/linkedhashmap"
> )
>
> func toMap(tree antlr.Tree) *linkedhashmap.Map {
> m := linkedhashmap.New()
> traverseMap(tree, m)
> return m
> }
>
> func traverseMap(tree antlr.Tree, m *linkedhashmap.Map) {
> if (reflect.TypeOf(tree) == reflect.TypeOf(&antlr.TerminalNodeImpl{})) {
> token := tree.(antlr.TerminalNode).GetSymbol()
> m.Put("type", token.GetTokenType())
> m.Put("text", token.GetText())
>
> } else {
> children := arraylist.New()
> s := reflect.ValueOf(tree).Type().Elem().Name()
> m.Put(s, children)
> for i := 0; i < tree.GetChildCount(); i++ {
> nested := linkedhashmap.New()
> children.Add(nested)
> traverseMap(tree.GetChild(i), nested)
> }
> }
> }
>
> func main() {
> input, _ := antlr.NewFileStream("../input/sample-mvbag-rich.toml")
> lexer := parser.NewVeloLexer(input)
> stream := antlr.NewCommonTokenStream(lexer, 0)
> p := parser.NewVeloParser(stream)
> p.RemoveErrorListeners()
> p.AddErrorListener(errorListiner)
> p.BuildParseTrees = true
> tree := p.Velo()
> j := toMap(tree)
> println(j.Size())
> strB, _ := j.ToJSON()
> print(string(strB))
> //antlr.ParseTreeWalkerDefault.Walk(NewVeloListener(), tree)
> }
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/d0ef75e5-4dee-4967-b2b4-cb58bcb6216bo%40googlegroups.com.

Reply via email to