collect aliases

This commit is contained in:
z7zmey 2018-02-23 23:59:26 +02:00
parent f2846dd859
commit b9f158e7f4

View File

@ -2,22 +2,50 @@
package visitor package visitor
import ( import (
"fmt"
"strings"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/name" "github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/walker" "github.com/z7zmey/php-parser/walker"
) )
type Namespace struct {
Namespace string
Aliases map[string]map[string]string
}
func NewNamespace(NSName string) *Namespace {
return &Namespace{
Namespace: "",
Aliases: map[string]map[string]string{
"": {},
"const": {},
"function": {},
},
}
}
func (ns *Namespace) AddAlias(aliasType string, aliasName string, alias string) {
aliasType = strings.ToLower(aliasType)
if aliasType == "const" {
ns.Aliases[aliasType][alias] = aliasName
} else {
ns.Aliases[aliasType][strings.ToLower(alias)] = aliasName
}
}
type NsResolver struct { type NsResolver struct {
Namespace string Namespace *Namespace
ResolvedNames map[node.Node]string ResolvedNames map[node.Node]string
Aliases map[string]map[string]string
} }
// NewNsResolver NsResolver type constructor // NewNsResolver NsResolver type constructor
func NewNsResolver() *NsResolver { func NewNsResolver() *NsResolver {
return &NsResolver{ return &NsResolver{
Namespace: "", Namespace: NewNamespace(""),
ResolvedNames: map[node.Node]string{}, ResolvedNames: map[node.Node]string{},
} }
} }
@ -26,9 +54,60 @@ func NewNsResolver() *NsResolver {
func (nsr *NsResolver) EnterNode(w walker.Walkable) bool { func (nsr *NsResolver) EnterNode(w walker.Walkable) bool {
switch n := w.(type) { switch n := w.(type) {
case *stmt.Namespace: case *stmt.Namespace:
nsr.Namespace = concatNameParts(n.NamespaceName.(*name.Name).Parts) if n.NamespaceName == nil {
case *stmt.Use: nsr.Namespace = NewNamespace("")
} else {
NSParts := n.NamespaceName.(*name.Name).Parts
nsr.Namespace = NewNamespace(concatNameParts(NSParts))
}
case *stmt.UseList:
useType := ""
if n.UseType != nil {
useType = n.UseType.(*node.Identifier).Value
}
for _, nn := range n.Uses {
switch use := nn.(type) {
case *stmt.Use:
if use.UseType != nil {
useType = use.UseType.(*node.Identifier).Value
}
useNameParts := use.Use.(*name.Name).Parts
var alias string
if use.Alias == nil {
alias = useNameParts[len(useNameParts)-1].(*name.NamePart).Value
} else {
alias = use.Alias.(*node.Identifier).Value
}
nsr.Namespace.AddAlias(useType, concatNameParts(useNameParts), alias)
}
}
case *stmt.GroupUse:
useType := ""
if n.UseType != nil {
useType = n.UseType.(*node.Identifier).Value
}
for _, nn := range n.UseList {
switch use := nn.(type) {
case *stmt.Use:
if use.UseType != nil {
useType = use.UseType.(*node.Identifier).Value
}
useNameParts := use.Use.(*name.Name).Parts
var alias string
if use.Alias == nil {
alias = useNameParts[len(useNameParts)-1].(*name.NamePart).Value
} else {
alias = use.Alias.(*node.Identifier).Value
}
aliasName := concatNameParts(n.Prefix.(*name.Name).Parts, useNameParts)
nsr.Namespace.AddAlias(useType, aliasName, alias)
}
}
} }
return true return true
@ -44,19 +123,22 @@ func (nsr *NsResolver) LeaveNode(w walker.Walkable) {
switch n := w.(type) { switch n := w.(type) {
case *stmt.Namespace: case *stmt.Namespace:
if n.Stmts != nil { if n.Stmts != nil {
nsr.Namespace = "" fmt.Printf("%+v \n", nsr.Namespace.Aliases)
nsr.Namespace = NewNamespace("")
} }
} }
} }
func concatNameParts(parts []node.Node) string { func concatNameParts(parts ...[]node.Node) string {
str := "" str := ""
for _, n := range parts { for _, p := range parts {
if str == "" { for _, n := range p {
str = n.(*name.NamePart).Value if str == "" {
} else { str = n.(*name.NamePart).Value
str = str + "\\" + n.(*name.NamePart).Value } else {
str = str + "\\" + n.(*name.NamePart).Value
}
} }
} }