[refactoring] update use ast structure

This commit is contained in:
Vadym Slizov
2020-08-03 21:22:53 +02:00
parent 88dfd32d9e
commit feebb017c4
21 changed files with 4604 additions and 3695 deletions

View File

@@ -437,12 +437,6 @@ func (v *Dump) StmtGoto(n *ast.StmtGoto) {
v.printNode(n.GetNode())
}
func (v *Dump) StmtGroupUse(n *ast.StmtGroupUse) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtGroupUse{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtHaltCompiler(n *ast.StmtHaltCompiler) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtHaltCompiler{\n")
@@ -596,12 +590,30 @@ func (v *Dump) StmtUse(n *ast.StmtUse) {
v.printNode(n.GetNode())
}
func (v *Dump) StmtGroupUseList(n *ast.StmtGroupUseList) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtGroupUseList{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtUseList(n *ast.StmtUseList) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUseList{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtUseDeclaration(n *ast.StmtUseDeclaration) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUseDeclaration{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtUseType(n *ast.StmtUseType) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUseType{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtWhile(n *ast.StmtWhile) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtWhile{\n")

View File

@@ -13,7 +13,9 @@ type NamespaceResolver struct {
Namespace *Namespace
ResolvedNames map[ast.Vertex]string
goDeep bool
goDeep bool
useType string
usePrefix []ast.Vertex
}
// NewNamespaceResolver NamespaceResolver type constructor
@@ -45,28 +47,28 @@ func (nsr *NamespaceResolver) StmtNamespace(n *ast.StmtNamespace) {
}
}
func (nsr *NamespaceResolver) StmtUseList(n *ast.StmtUseList) {
useType := ""
if n.UseType != nil {
useType = string(n.UseType.(*ast.Identifier).Value)
func (nsr *NamespaceResolver) StmtUseType(n *ast.StmtUseType) {
if n.Type != nil {
nsr.useType = string(n.Type.(*ast.Identifier).Value)
}
for _, nn := range n.Uses {
nsr.AddAlias(useType, nn, nil)
}
nsr.goDeep = false
}
func (nsr *NamespaceResolver) StmtGroupUse(n *ast.StmtGroupUse) {
useType := ""
if n.UseType != nil {
useType = string(n.UseType.(*ast.Identifier).Value)
func (nsr *NamespaceResolver) StmtGroupUseList(n *ast.StmtGroupUseList) {
if n.Prefix != nil {
nsr.usePrefix = n.Prefix.(*ast.NameName).Parts
}
}
func (nsr *NamespaceResolver) StmtUseDeclaration(n *ast.StmtUseDeclaration) {
useNameParts := n.Use.(*ast.NameName).Parts
var alias string
if n.Alias == nil {
alias = string(useNameParts[len(useNameParts)-1].(*ast.NameNamePart).Value)
} else {
alias = string(n.Alias.(*ast.Identifier).Value)
}
for _, nn := range n.UseList {
nsr.AddAlias(useType, nn, n.Prefix.(*ast.NameName).Parts)
}
nsr.Namespace.AddAlias(nsr.useType, concatNameParts(nsr.usePrefix, useNameParts), alias)
nsr.goDeep = false
}
@@ -213,26 +215,10 @@ func (nsr *NamespaceResolver) LeaveNode(n ast.Vertex) {
if nn.Stmts != nil {
nsr.Namespace = NewNamespace("")
}
}
}
// AddAlias adds a new alias
func (nsr *NamespaceResolver) AddAlias(useType string, nn ast.Vertex, prefix []ast.Vertex) {
switch use := nn.(type) {
case *ast.StmtUse:
if use.UseType != nil {
useType = string(use.UseType.(*ast.Identifier).Value)
}
useNameParts := use.Use.(*ast.NameName).Parts
var alias string
if use.Alias == nil {
alias = string(useNameParts[len(useNameParts)-1].(*ast.NameNamePart).Value)
} else {
alias = string(use.Alias.(*ast.Identifier).Value)
}
nsr.Namespace.AddAlias(useType, concatNameParts(prefix, useNameParts), alias)
case *ast.StmtUseType:
nsr.useType = ""
case *ast.StmtGroupUseList:
nsr.usePrefix = nil
}
}

View File

@@ -16,10 +16,12 @@ func TestResolveStaticCall(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUseList{
Uses: []ast.Vertex{
&ast.StmtUse{
Use: nameAB,
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
},
@@ -48,10 +50,12 @@ func TestResolveStaticPropertyFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUseList{
Uses: []ast.Vertex{
&ast.StmtUse{
Use: nameAB,
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
},
@@ -79,10 +83,12 @@ func TestResolveClassConstFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUseList{
Uses: []ast.Vertex{
&ast.StmtUse{
Use: nameAB,
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
},
@@ -110,10 +116,12 @@ func TestResolveNew(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUseList{
Uses: []ast.Vertex{
&ast.StmtUse{
Use: nameAB,
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
},
@@ -141,10 +149,12 @@ func TestResolveInstanceOf(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUseList{
Uses: []ast.Vertex{
&ast.StmtUse{
Use: nameAB,
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
},
@@ -175,14 +185,15 @@ func TestResolveInstanceCatch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUseList{
Uses: []ast.Vertex{
&ast.StmtUse{
Use: nameAB,
},
&ast.StmtUse{
Use: nameDE,
Alias: &ast.Identifier{Value: []byte("F")},
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
}, &ast.StmtUseDeclaration{
Use: nameDE,
Alias: &ast.Identifier{Value: []byte("F")},
},
},
},
},
@@ -220,11 +231,15 @@ func TestResolveFunctionCall(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUseList{
UseType: &ast.Identifier{Value: []byte("function")},
Uses: []ast.Vertex{
&ast.StmtUse{
Use: nameAB,
&ast.StmtUse{
UseList: &ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
},
},
@@ -252,11 +267,15 @@ func TestResolveConstFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUseList{
UseType: &ast.Identifier{Value: []byte("const")},
Uses: []ast.Vertex{
&ast.StmtUse{
Use: nameAB,
&ast.StmtUse{
UseList: &ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("const")},
Use: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
},
},
@@ -286,25 +305,39 @@ func TestResolveGroupUse(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtGroupUse{
Prefix: nameAB,
UseList: []ast.Vertex{
&ast.StmtUse{
UseType: &ast.Identifier{Value: []byte("Function")},
Use: nameF,
},
&ast.StmtUse{
UseType: &ast.Identifier{Value: []byte("const")},
Use: nameC,
&ast.StmtUse{
UseList: &ast.StmtGroupUseList{
Prefix: nameAB,
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("Function")},
Use: &ast.StmtUseDeclaration{
Use: nameF,
},
},
&ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("const")},
Use: &ast.StmtUseDeclaration{
Use: nameC,
},
},
},
},
},
},
&ast.StmtGroupUse{
Prefix: nameBD,
UseType: &ast.Identifier{Value: []byte("Function")},
UseList: []ast.Vertex{
&ast.StmtUse{
Use: nameE,
&ast.StmtUse{
UseList: &ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("Function")},
Use: &ast.StmtGroupUseList{
Prefix: nameBD,
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameE,
},
},
},
},
},
},
@@ -347,10 +380,12 @@ func TestResolveTraitUse(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUseList{
Uses: []ast.Vertex{
&ast.StmtUse{
Use: nameAB,
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
},
@@ -668,10 +703,12 @@ func TestResolveNamespaces(t *testing.T) {
&ast.StmtNamespace{
NamespaceName: namespaceCD,
Stmts: []ast.Vertex{
&ast.StmtUseList{
Uses: []ast.Vertex{
&ast.StmtUse{
Use: nameAC,
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAC,
},
},
},
},

View File

@@ -182,10 +182,6 @@ func (v *Null) StmtGoto(_ *ast.StmtGoto) {
// do nothing
}
func (v *Null) StmtGroupUse(_ *ast.StmtGroupUse) {
// do nothing
}
func (v *Null) StmtHaltCompiler(_ *ast.StmtHaltCompiler) {
// do nothing
}
@@ -286,10 +282,22 @@ func (v *Null) StmtUse(_ *ast.StmtUse) {
// do nothing
}
func (v *Null) StmtGroupUseList(_ *ast.StmtGroupUseList) {
// do nothing
}
func (v *Null) StmtUseList(_ *ast.StmtUseList) {
// do nothing
}
func (v *Null) StmtUseDeclaration(_ *ast.StmtUseDeclaration) {
// do nothing
}
func (v *Null) StmtUseType(_ *ast.StmtUseType) {
// do nothing
}
func (v *Null) StmtWhile(_ *ast.StmtWhile) {
// do nothing
}