php-parser/dumper.go

48 lines
904 B
Go
Raw Normal View History

2017-12-27 23:23:32 +00:00
package main
import (
"fmt"
2018-01-04 18:29:07 +00:00
"reflect"
2017-12-27 23:23:32 +00:00
2018-01-09 13:51:32 +00:00
"github.com/z7zmey/php-parser/position"
2018-01-08 22:30:28 +00:00
"github.com/z7zmey/php-parser/comment"
2017-12-27 23:23:32 +00:00
"github.com/z7zmey/php-parser/node"
)
type dumper struct {
2018-01-09 13:51:32 +00:00
indent string
comments comment.Comments
positions position.Positions
2017-12-27 23:23:32 +00:00
}
2017-12-28 11:36:27 +00:00
func (d dumper) EnterNode(n node.Node) bool {
2017-12-29 15:20:24 +00:00
2018-01-04 18:29:07 +00:00
fmt.Printf("%v%v", d.indent, reflect.TypeOf(n))
2018-01-09 13:51:32 +00:00
if p := d.positions[n]; p != nil {
2017-12-31 18:53:55 +00:00
fmt.Printf(" %v", *p)
}
2017-12-29 15:53:13 +00:00
if a := n.Attributes(); len(a) > 0 {
2017-12-29 15:20:24 +00:00
fmt.Printf(" %v", a)
}
fmt.Println()
2017-12-27 23:23:32 +00:00
2018-01-08 22:30:28 +00:00
if c := d.comments[n]; len(c) > 0 {
2018-01-05 15:03:59 +00:00
fmt.Printf("%vcomments:\n", d.indent+" ")
2018-01-08 19:50:39 +00:00
for _, cc := range c {
2018-01-05 15:03:59 +00:00
fmt.Printf("%v%q\n", d.indent+" ", cc)
}
}
2017-12-27 23:23:32 +00:00
return true
}
2017-12-28 11:36:27 +00:00
func (d dumper) GetChildrenVisitor(key string) node.Visitor {
2017-12-29 15:20:24 +00:00
fmt.Printf("%v%q:\n", d.indent+" ", key)
2018-01-09 13:51:32 +00:00
return dumper{d.indent + " ", d.comments, d.positions}
2017-12-27 23:23:32 +00:00
}
2017-12-28 11:36:27 +00:00
func (d dumper) LeaveNode(n node.Node) {
// do nothing
}