ns resolver

This commit is contained in:
z7zmey 2018-02-23 20:26:13 +02:00
parent 7a0e5fc758
commit f2846dd859
2 changed files with 69 additions and 2 deletions

View File

@ -39,12 +39,15 @@ func main() {
nodes, comments, positions = php7.Parse(src, path)
}
visitor := visitor.Dumper{
nsResolver := visitor.NewNsResolver()
nodes.Walk(nsResolver)
dumper := visitor.Dumper{
Indent: " | ",
Comments: comments,
Positions: positions,
}
nodes.Walk(visitor)
nodes.Walk(dumper)
}
return nil
})

64
visitor/NsResolver.go Normal file
View File

@ -0,0 +1,64 @@
// Package visitor contains walker.visitor implementations
package visitor
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/walker"
)
type NsResolver struct {
Namespace string
ResolvedNames map[node.Node]string
Aliases map[string]map[string]string
}
// NewNsResolver NsResolver type constructor
func NewNsResolver() *NsResolver {
return &NsResolver{
Namespace: "",
ResolvedNames: map[node.Node]string{},
}
}
// EnterNode is invoked at every node in heirerchy
func (nsr *NsResolver) EnterNode(w walker.Walkable) bool {
switch n := w.(type) {
case *stmt.Namespace:
nsr.Namespace = concatNameParts(n.NamespaceName.(*name.Name).Parts)
case *stmt.Use:
}
return true
}
// GetChildrenVisitor is invoked at every node parameter that contains children nodes
func (nsr *NsResolver) GetChildrenVisitor(key string) walker.Visitor {
return nsr
}
// LeaveNode is invoked after node process
func (nsr *NsResolver) LeaveNode(w walker.Walkable) {
switch n := w.(type) {
case *stmt.Namespace:
if n.Stmts != nil {
nsr.Namespace = ""
}
}
}
func concatNameParts(parts []node.Node) string {
str := ""
for _, n := range parts {
if str == "" {
str = n.(*name.NamePart).Value
} else {
str = str + "\\" + n.(*name.NamePart).Value
}
}
return str
}