2018-02-04 19:35:46 +00:00
|
|
|
package visitor
|
2018-01-12 08:04:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2018-01-17 16:58:45 +00:00
|
|
|
"github.com/z7zmey/php-parser/node"
|
2018-01-12 08:04:31 +00:00
|
|
|
"github.com/z7zmey/php-parser/position"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/comment"
|
2018-01-17 16:58:45 +00:00
|
|
|
"github.com/z7zmey/php-parser/walker"
|
2018-01-12 08:04:31 +00:00
|
|
|
)
|
|
|
|
|
2018-02-20 17:52:07 +00:00
|
|
|
func isWalkerImplementsNodeInterface(w walker.Walkable) bool {
|
2018-01-17 16:58:45 +00:00
|
|
|
switch w.(type) {
|
|
|
|
case node.Node:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 08:16:09 +00:00
|
|
|
// Dumper prints ast hierarchy to stdout
|
|
|
|
// Also prints comments and positions attached to nodes
|
2018-01-12 08:04:31 +00:00
|
|
|
type Dumper struct {
|
|
|
|
Indent string
|
|
|
|
Comments comment.Comments
|
|
|
|
Positions position.Positions
|
|
|
|
}
|
|
|
|
|
2018-01-12 08:16:09 +00:00
|
|
|
// EnterNode is invoked at every node in heirerchy
|
2018-02-20 17:52:07 +00:00
|
|
|
func (d Dumper) EnterNode(w walker.Walkable) bool {
|
2018-01-17 16:58:45 +00:00
|
|
|
if !isWalkerImplementsNodeInterface(w) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
n := w.(node.Node)
|
2018-01-12 08:04:31 +00:00
|
|
|
|
|
|
|
fmt.Printf("%v%v", d.Indent, reflect.TypeOf(n))
|
|
|
|
if p := d.Positions[n]; p != nil {
|
|
|
|
fmt.Printf(" %v", *p)
|
|
|
|
}
|
|
|
|
if a := n.Attributes(); len(a) > 0 {
|
|
|
|
fmt.Printf(" %v", a)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
if c := d.Comments[n]; len(c) > 0 {
|
|
|
|
fmt.Printf("%vComments:\n", d.Indent+" ")
|
|
|
|
for _, cc := range c {
|
|
|
|
fmt.Printf("%v%q\n", d.Indent+" ", cc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-01-12 08:16:09 +00:00
|
|
|
// GetChildrenVisitor is invoked at every node parameter that contains children nodes
|
2018-01-17 16:58:45 +00:00
|
|
|
func (d Dumper) GetChildrenVisitor(key string) walker.Visitor {
|
2018-01-12 08:04:31 +00:00
|
|
|
fmt.Printf("%v%q:\n", d.Indent+" ", key)
|
|
|
|
return Dumper{d.Indent + " ", d.Comments, d.Positions}
|
|
|
|
}
|
|
|
|
|
2018-01-12 08:16:09 +00:00
|
|
|
// LeaveNode is invoked after node process
|
2018-02-20 17:52:07 +00:00
|
|
|
func (d Dumper) LeaveNode(n walker.Walkable) {
|
2018-01-12 08:04:31 +00:00
|
|
|
// do nothing
|
|
|
|
}
|