diff --git a/main.go b/main.go index c24f112..4010add 100644 --- a/main.go +++ b/main.go @@ -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 }) diff --git a/visitor/NsResolver.go b/visitor/NsResolver.go new file mode 100644 index 0000000..9047ec3 --- /dev/null +++ b/visitor/NsResolver.go @@ -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 +}