diff options
author | Adam Scarr <adam@vektah.net> | 2017-08-13 19:50:41 +1000 |
---|---|---|
committer | Adam Scarr <adam@vektah.net> | 2017-08-13 19:50:41 +1000 |
commit | a733d0ae1391dc8642d21f8784696e95047b7923 (patch) | |
tree | e958612b45f6a9af0a122e763385651adf4342ff /result.go | |
parent | a4677a5834210c85e050de6dad13911a85297499 (diff) |
Improve debugging output
Diffstat (limited to 'result.go')
-rw-r--r-- | result.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/result.go b/result.go new file mode 100644 index 0000000..cdad054 --- /dev/null +++ b/result.go @@ -0,0 +1,36 @@ +package goparsify + +import ( + "fmt" + "strings" +) + +var TrashResult = &Result{} + +// Result is the output of a parser. Usually only one of its fields will be set and should be though of +// more as a union type. having it avoids interface{} littered all through the parsing code and makes +// the it easy to do the two most common operations, getting a token and finding a child. +type Result struct { + Token string + Child []Result + Result interface{} +} + +func (r Result) String() string { + if r.Result != nil { + if rs, ok := r.Result.(fmt.Stringer); ok { + return rs.String() + } + return fmt.Sprintf("%#v", r.Result) + } + + if len(r.Child) > 0 { + children := []string{} + for _, child := range r.Child { + children = append(children, child.String()) + } + return "[" + strings.Join(children, ",") + "]" + } + + return r.Token +} |