type resolving

This commit is contained in:
z7zmey 2018-02-26 23:29:36 +02:00
parent fbeffe7d29
commit 7976de4d11
2 changed files with 154 additions and 4 deletions

View File

@ -42,8 +42,6 @@ func main() {
nsResolver := visitor.NewNsResolver() nsResolver := visitor.NewNsResolver()
nodes.Walk(nsResolver) nodes.Walk(nsResolver)
fmt.Printf("%+v \n", nsResolver.ResolvedNames)
dumper := visitor.Dumper{ dumper := visitor.Dumper{
Indent: " | ", Indent: " | ",
Comments: comments, Comments: comments,

View File

@ -5,6 +5,8 @@ import (
"errors" "errors"
"strings" "strings"
"github.com/z7zmey/php-parser/node/expr"
"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"
@ -155,11 +157,148 @@ func (nsr *NsResolver) EnterNode(w walker.Walkable) bool {
nsr.resolveName(n.Extends, "") nsr.resolveName(n.Extends, "")
} }
for _, _ = range n.Implements { for _, interfaceName := range n.Implements {
// todo resolve inteface name nsr.resolveName(interfaceName, "")
} }
nsr.addNamespacedName(n, n.ClassName.(*node.Identifier).Value) nsr.addNamespacedName(n, n.ClassName.(*node.Identifier).Value)
case *stmt.Interface:
for _, interfaceName := range n.Extends {
nsr.resolveName(interfaceName, "")
}
nsr.addNamespacedName(n, n.InterfaceName.(*node.Identifier).Value)
case *stmt.Trait:
nsr.addNamespacedName(n, n.TraitName.(*node.Identifier).Value)
case *stmt.Function:
nsr.addNamespacedName(n, n.FunctionName.(*node.Identifier).Value)
for _, parameter := range n.Params {
nsr.resolveType(parameter.(*node.Parameter).VariableType)
}
if n.ReturnType != nil {
nsr.resolveType(n.ReturnType)
}
case *stmt.ClassMethod:
for _, parameter := range n.Params {
nsr.resolveType(parameter.(*node.Parameter).VariableType)
}
if n.ReturnType != nil {
nsr.resolveType(n.ReturnType)
}
case *expr.Closure:
for _, parameter := range n.Params {
nsr.resolveType(parameter.(*node.Parameter).VariableType)
}
if n.ReturnType != nil {
nsr.resolveType(n.ReturnType)
}
case *stmt.ConstList:
for _, constant := range n.Consts {
nsr.addNamespacedName(constant, constant.(*stmt.Constant).ConstantName.(*node.Identifier).Value)
}
case *expr.StaticCall:
switch nn := n.Class.(type) {
case *name.Name:
nsr.resolveName(nn, "")
case *name.Relative:
nsr.resolveName(nn, "")
case *name.FullyQualified:
nsr.resolveName(nn, "")
}
case *expr.StaticPropertyFetch:
switch nn := n.Class.(type) {
case *name.Name:
nsr.resolveName(nn, "")
case *name.Relative:
nsr.resolveName(nn, "")
case *name.FullyQualified:
nsr.resolveName(nn, "")
}
case *expr.ClassConstFetch:
switch nn := n.Class.(type) {
case *name.Name:
nsr.resolveName(nn, "")
case *name.Relative:
nsr.resolveName(nn, "")
case *name.FullyQualified:
nsr.resolveName(nn, "")
}
case *expr.New:
switch nn := n.Class.(type) {
case *name.Name:
nsr.resolveName(nn, "")
case *name.Relative:
nsr.resolveName(nn, "")
case *name.FullyQualified:
nsr.resolveName(nn, "")
}
case *expr.InstanceOf:
switch nn := n.Class.(type) {
case *name.Name:
nsr.resolveName(nn, "")
case *name.Relative:
nsr.resolveName(nn, "")
case *name.FullyQualified:
nsr.resolveName(nn, "")
}
case *stmt.Catch:
for _, t := range n.Types {
nsr.resolveName(t, "")
}
case *expr.FunctionCall:
switch nn := n.Function.(type) {
case *name.Name:
nsr.resolveName(nn, "function")
case *name.Relative:
nsr.resolveName(nn, "function")
case *name.FullyQualified:
nsr.resolveName(nn, "function")
}
case *expr.ConstFetch:
nsr.resolveName(n.Constant, "const")
case *stmt.TraitUse:
for _, t := range n.Traits {
nsr.resolveName(t, "")
}
for _, a := range n.Adaptations {
switch aa := a.(type) {
case *stmt.TraitUsePrecedence:
refTrait := aa.Ref.(*stmt.TraitMethodRef).Trait
if refTrait != nil {
nsr.resolveName(refTrait, "")
}
for _, insteadOf := range aa.Insteadof {
nsr.resolveName(insteadOf, "")
}
case *stmt.TraitUseAlias:
refTrait := aa.Ref.(*stmt.TraitMethodRef).Trait
if refTrait != nil {
nsr.resolveName(refTrait, "")
}
}
}
} }
return true return true
@ -210,3 +349,16 @@ func (nsr *NsResolver) addNamespacedName(nn node.Node, nodeName string) {
func (nsr *NsResolver) resolveName(nameNode node.Node, aliasType string) { func (nsr *NsResolver) resolveName(nameNode node.Node, aliasType string) {
nsr.ResolvedNames[nameNode] = nsr.Namespace.resolveName(nameNode, aliasType) nsr.ResolvedNames[nameNode] = nsr.Namespace.resolveName(nameNode, aliasType)
} }
func (nsr *NsResolver) resolveType(n node.Node) {
switch nn := n.(type) {
case *node.Nullable:
nsr.resolveType(nn.Expr)
case *name.Name:
nsr.resolveName(n, "")
case *name.Relative:
nsr.resolveName(n, "")
case *name.FullyQualified:
nsr.resolveName(n, "")
}
}