[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

@@ -59,7 +59,6 @@ type NodeVisitor interface {
StmtFunction(n *StmtFunction)
StmtGlobal(n *StmtGlobal)
StmtGoto(n *StmtGoto)
StmtGroupUse(n *StmtGroupUse)
StmtHaltCompiler(n *StmtHaltCompiler)
StmtIf(n *StmtIf)
StmtInlineHtml(n *StmtInlineHtml)
@@ -85,7 +84,10 @@ type NodeVisitor interface {
StmtTry(n *StmtTry)
StmtUnset(n *StmtUnset)
StmtUse(n *StmtUse)
StmtGroupUseList(n *StmtGroupUseList)
StmtUseList(n *StmtUseList)
StmtUseDeclaration(n *StmtUseDeclaration)
StmtUseType(n *StmtUseType)
StmtWhile(n *StmtWhile)
ExprArray(n *ExprArray)

View File

@@ -532,18 +532,6 @@ func (n *StmtGoto) Accept(v NodeVisitor) {
v.StmtGoto(n)
}
// StmtGroupUse node
type StmtGroupUse struct {
Node
UseType Vertex
Prefix Vertex
UseList []Vertex
}
func (n *StmtGroupUse) Accept(v NodeVisitor) {
v.StmtGroupUse(n)
}
// StmtHaltCompiler node
type StmtHaltCompiler struct {
Node
@@ -804,26 +792,56 @@ func (n *StmtUnset) Accept(v NodeVisitor) {
// StmtUse node
type StmtUse struct {
Node
UseType Vertex
Use Vertex
Alias Vertex
UseList Vertex
}
func (n *StmtUse) Accept(v NodeVisitor) {
v.StmtUse(n)
}
// StmtGroupUseList node
type StmtGroupUseList struct {
Node
Prefix Vertex
UseList Vertex
}
func (n *StmtGroupUseList) Accept(v NodeVisitor) {
v.StmtGroupUseList(n)
}
// StmtUseList node
type StmtUseList struct {
Node
UseType Vertex
Uses []Vertex
UseDeclarations []Vertex
}
func (n *StmtUseList) Accept(v NodeVisitor) {
v.StmtUseList(n)
}
// StmtUseDeclaration node
type StmtUseDeclaration struct {
Node
Use Vertex
Alias Vertex
}
func (n *StmtUseDeclaration) Accept(v NodeVisitor) {
v.StmtUseDeclaration(n)
}
// StmtUseType node
type StmtUseType struct {
Node
Type Vertex
Use Vertex
}
func (n *StmtUseType) Accept(v NodeVisitor) {
v.StmtUseType(n)
}
// StmtWhile node
type StmtWhile struct {
Node

View File

@@ -745,30 +745,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Label)
t.visitor.Leave("Label", true)
}
case *ast.StmtGroupUse:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.UseType != nil {
t.visitor.Enter("UseType", true)
t.Traverse(nn.UseType)
t.visitor.Leave("UseType", true)
}
if nn.Prefix != nil {
t.visitor.Enter("Prefix", true)
t.Traverse(nn.Prefix)
t.visitor.Leave("Prefix", true)
}
if nn.UseList != nil {
t.visitor.Enter("UseList", false)
for _, c := range nn.UseList {
t.Traverse(c)
}
t.visitor.Leave("UseList", false)
}
case *ast.StmtHaltCompiler:
if nn == nil {
return
@@ -1174,10 +1150,48 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) {
return
}
if nn.UseType != nil {
t.visitor.Enter("UseType", true)
t.Traverse(nn.UseType)
t.visitor.Leave("UseType", true)
if nn.UseList != nil {
t.visitor.Enter("UseList", true)
t.Traverse(nn.UseList)
t.visitor.Leave("UseList", true)
}
case *ast.StmtGroupUseList:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Prefix != nil {
t.visitor.Enter("Prefix", true)
t.Traverse(nn.Prefix)
t.visitor.Leave("Prefix", true)
}
if nn.UseList != nil {
t.visitor.Enter("UseList", true)
t.Traverse(nn.UseList)
t.visitor.Leave("UseList", true)
}
case *ast.StmtUseList:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.UseDeclarations != nil {
t.visitor.Enter("UseDeclarations", false)
for _, c := range nn.UseDeclarations {
t.Traverse(c)
}
t.visitor.Leave("UseDeclarations", false)
}
case *ast.StmtUseDeclaration:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Use != nil {
t.visitor.Enter("Use", true)
@@ -1189,24 +1203,22 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Alias)
t.visitor.Leave("Alias", true)
}
case *ast.StmtUseList:
case *ast.StmtUseType:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.UseType != nil {
t.visitor.Enter("UseType", true)
t.Traverse(nn.UseType)
t.visitor.Leave("UseType", true)
if nn.Type != nil {
t.visitor.Enter("Type", true)
t.Traverse(nn.Type)
t.visitor.Leave("Type", true)
}
if nn.Uses != nil {
t.visitor.Enter("Uses", false)
for _, c := range nn.Uses {
t.Traverse(c)
}
t.visitor.Leave("Uses", false)
if nn.Use != nil {
t.visitor.Enter("Use", true)
t.Traverse(nn.Use)
t.visitor.Leave("Use", true)
}
case *ast.StmtWhile:
if nn == nil {

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
}