diff options
Diffstat (limited to 'core/vm/sqlvm/ast/printer.go')
-rw-r--r-- | core/vm/sqlvm/ast/printer.go | 55 |
1 files changed, 35 insertions, 20 deletions
diff --git a/core/vm/sqlvm/ast/printer.go b/core/vm/sqlvm/ast/printer.go index 25f04e863..e8c612637 100644 --- a/core/vm/sqlvm/ast/printer.go +++ b/core/vm/sqlvm/ast/printer.go @@ -3,10 +3,11 @@ package ast import ( "fmt" "reflect" + "unicode/utf8" ) // PrintAST prints ast to stdout. -func PrintAST(n interface{}, indent string) { +func PrintAST(n interface{}, indent string, detail bool) { if n == nil { fmt.Printf("%snil\n", indent) return @@ -25,38 +26,28 @@ func PrintAST(n interface{}, indent string) { } name = name + typeOf.Name() - if op, ok := n.(Optional); ok { - fmt.Printf("%s%s", indent, name) - m := op.GetOption() - if m == nil { - fmt.Printf("\n") - return - } - fmt.Printf(":\n") - for k := range m { - fmt.Printf("%s %s:\n", indent, k) - PrintAST(m[k], indent+" ") - } - return - } if op, ok := n.(UnaryOperator); ok { fmt.Printf("%s%s:\n", indent, name) fmt.Printf("%s Target:\n", indent) - PrintAST(op.GetTarget(), indent+" ") + PrintAST(op.GetTarget(), indent+" ", detail) return } if op, ok := n.(BinaryOperator); ok { fmt.Printf("%s%s:\n", indent, name) fmt.Printf("%s Object:\n", indent) - PrintAST(op.GetObject(), indent+" ") + PrintAST(op.GetObject(), indent+" ", detail) fmt.Printf("%s Subject:\n", indent) - PrintAST(op.GetSubject(), indent+" ") + PrintAST(op.GetSubject(), indent+" ", detail) return } if arr, ok := n.([]interface{}); ok { + if len(arr) == 0 { + fmt.Printf("%s[]\n", indent) + return + } fmt.Printf("%s[\n", indent) for idx := range arr { - PrintAST(arr[idx], indent+" ") + PrintAST(arr[idx], indent+" ", detail) } fmt.Printf("%s]\n", indent) return @@ -65,5 +56,29 @@ func PrintAST(n interface{}, indent string) { fmt.Printf("%s%s: %s\n", indent, name, stringer.String()) return } - fmt.Printf("%s%s: %+v\n", indent, name, valueOf.Interface()) + if typeOf.Kind() == reflect.Struct { + fmt.Printf("%s%s", indent, name) + l := typeOf.NumField() + if l == 0 { + fmt.Printf(" {}\n") + return + } + fmt.Printf(" {\n") + for i := 0; i < l; i++ { + if !detail && typeOf.Field(i).Tag.Get("print") == "-" { + continue + } + fmt.Printf("%s %s:\n", indent, typeOf.Field(i).Name) + PrintAST(valueOf.Field(i).Interface(), indent+" ", detail) + } + fmt.Printf("%s}\n", indent) + return + } + if bs, ok := n.([]byte); ok { + if utf8.Valid(bs) { + fmt.Printf("%s%s\n", indent, bs) + return + } + } + fmt.Printf("%s%+v\n", indent, valueOf.Interface()) } |