package parse
import (
)
const (
maxL = 10
maxR = 10
indentInc = 2
)
func ( Node, io.Writer) {
pprintASTRec(, , 0, "")
}
type field struct {
name string
tag reflect.StructTag
value interface{}
}
var zeroValue reflect.Value
func ( Node, io.Writer, int, string) {
:= reflect.TypeOf((*Node)(nil)).Elem()
var , , []field
:= reflect.TypeOf().Elem()
:= reflect.ValueOf().Elem()
for := 0; < .NumField(); ++ {
:= .Field()
if .Anonymous {
continue
}
:= .Type
:= .Field()
if .Kind() == reflect.Slice {
if .Elem().Implements() {
= append(,
field{.Name, .Tag, .Interface()})
continue
}
} else if , := .Interface().(Node); {
if reflect.Indirect() != zeroValue {
= append(,
field{.Name, .Tag, })
}
continue
}
= append(,
field{.Name, .Tag, .Interface()})
}
if len(Children()) == 1 &&
SourceText(Children()[0]) == SourceText() {
(Children()[0], , , +.Name()+"/")
return
}
fmt.Fprintf(, "%*s%s%s", , "", , .Name())
for , := range {
:= .tag.Get("fmt")
if len() > 0 {
fmt.Fprintf(, " %s="+, .name, .value)
} else {
:= .value
if , := .(string); {
= compactQuote()
}
fmt.Fprintf(, " %s=%v", .name, )
}
}
fmt.Fprint(, "\n")
for , := range {
(.value.(Node), , +indentInc, "")
}
for , := range {
:= reflect.ValueOf(.value)
if .Len() == 0 {
continue
}
for := 0; < .Len(); ++ {
:= .Index().Interface().(Node)
(, , +indentInc, "")
}
}
}
func ( Node, io.Writer) {
pprintParseTreeRec(, , 0)
}
func ( Node, io.Writer, int) {
:= ""
for len(Children()) == 1 {
+= reflect.TypeOf().Elem().Name() + "/"
= Children()[0]
}
fmt.Fprintf(, "%*s%s%s\n", , "", , summary())
for , := range Children() {
(, , +indentInc)
}
}
func ( Node) string {
return fmt.Sprintf("%s %s %d-%d", reflect.TypeOf().Elem().Name(),
compactQuote(SourceText()), .Range().From, .Range().To)
}
func ( string) string {
if len() > maxL+maxR+3 {
= [0:maxL] + "..." + [len()-maxR:]
}
return strconv.Quote()
}