[refactoring] update ast structure for "Use" and "GroupUse" nodes

This commit is contained in:
Vadym Slizov
2020-08-22 16:59:26 +03:00
parent 97747c5ac0
commit 767187ff85
23 changed files with 4134 additions and 5001 deletions

View File

@@ -169,6 +169,28 @@ func (v *Dump) printNode(n *ast.Node) {
v.print("},\n")
}
func (v *Dump) printToken(key string, t *token.Token) {
if t == nil {
return
}
v.printIndent(v.indent)
v.print(key)
v.print(": &token.Token{\n")
v.printIndent(v.indent + 1)
v.print("ID: token." + t.ID.String() + ",\n")
v.printIndent(v.indent + 1)
v.print("Value: []byte(" + strconv.Quote(string(t.Value)) + "),\n")
v.printIndent(v.indent + 1)
v.print("Skipped: []byte(" + strconv.Quote(string(t.Skipped)) + "),\n")
v.printIndent(v.indent)
v.print("},\n")
}
func (v *Dump) Root(n *ast.Root) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.Root{\n")
@@ -588,30 +610,30 @@ func (v *Dump) StmtUse(n *ast.StmtUse) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUse{\n")
v.printNode(n.GetNode())
v.printToken("UseTkn", n.UseTkn)
v.printToken("SemiColonTkn", n.SemiColonTkn)
}
func (v *Dump) StmtGroupUseList(n *ast.StmtGroupUseList) {
func (v *Dump) StmtGroupUse(n *ast.StmtGroupUse) {
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.print("&ast.StmtGroupUse{\n")
v.printNode(n.GetNode())
v.printToken("UseTkn", n.UseTkn)
v.printToken("LeadingNsSeparatorTkn", n.LeadingNsSeparatorTkn)
v.printToken("NsSeparatorTkn", n.NsSeparatorTkn)
v.printToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
v.printToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
v.printToken("SemiColonTkn", n.SemiColonTkn)
}
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())
v.printToken("NsSeparatorTkn", n.NsSeparatorTkn)
v.printToken("AsTkn", n.AsTkn)
v.printToken("CommaTkn", n.CommaTkn)
}
func (v *Dump) StmtWhile(n *ast.StmtWhile) {

View File

@@ -13,34 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
return true
}
func (v *FilterParserNodes) StmtGroupUseList(n *ast.StmtGroupUseList) {
if nn, ok := n.Prefix.(*ast.ParserNsSeparator); ok {
n.Prefix = nn.Child
}
if nn, ok := n.UseList.(*ast.ParserNsSeparator); ok {
n.UseList = nn.Child
}
if nn, ok := n.UseList.(*ast.ParserBrackets); ok {
n.UseList = nn.Child
}
}
func (v *FilterParserNodes) StmtUseList(n *ast.StmtUseList) {
for k, v := range n.UseDeclarations {
if nn, ok := v.(*ast.ParserNsSeparator); ok {
n.UseDeclarations[k] = nn.Child
}
}
}
func (v *FilterParserNodes) StmtUseDeclaration(n *ast.StmtUseDeclaration) {
if nn, ok := n.Alias.(*ast.ParserAs); ok {
n.Alias = nn.Child
}
}
func (v *FilterParserNodes) StmtAltIf(n *ast.StmtAltIf) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {

View File

@@ -10,5 +10,26 @@ type FilterTokens struct {
func (v *FilterTokens) EnterNode(n ast.Vertex) bool {
n.GetNode().Tokens = nil
n.Accept(v)
return true
}
func (v *FilterTokens) StmtUse(n *ast.StmtUse) {
n.UseTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtGroupUse(n *ast.StmtGroupUse) {
n.UseTkn = nil
n.LeadingNsSeparatorTkn = nil
n.NsSeparatorTkn = nil
n.OpenCurlyBracketTkn = nil
n.CloseCurlyBracketTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtUseDeclaration(n *ast.StmtUseDeclaration) {
n.NsSeparatorTkn = nil
n.AsTkn = nil
n.CommaTkn = nil
}

View File

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

View File

@@ -17,11 +17,9 @@ func TestResolveStaticCall(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@@ -51,11 +49,9 @@ func TestResolveStaticPropertyFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@@ -84,11 +80,9 @@ func TestResolveClassConstFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@@ -117,11 +111,9 @@ func TestResolveNew(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@@ -150,11 +142,9 @@ func TestResolveInstanceOf(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@@ -186,14 +176,13 @@ func TestResolveInstanceCatch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
}, &ast.StmtUseDeclaration{
Use: nameDE,
Alias: &ast.Identifier{Value: []byte("F")},
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
&ast.StmtUseDeclaration{
Use: nameDE,
Alias: &ast.Identifier{Value: []byte("F")},
},
},
},
@@ -232,14 +221,10 @@ func TestResolveFunctionCall(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
Type: &ast.Identifier{Value: []byte("function")},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@@ -268,14 +253,10 @@ func TestResolveConstFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("const")},
Use: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
Type: &ast.Identifier{Value: []byte("const")},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@@ -305,39 +286,25 @@ func TestResolveGroupUse(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&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: nameAB,
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Type: &ast.Identifier{Value: []byte("Function")},
Use: nameF,
},
&ast.StmtUseDeclaration{
Type: &ast.Identifier{Value: []byte("const")},
Use: nameC,
},
},
},
&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,
},
},
},
&ast.StmtGroupUse{
Prefix: nameBD,
Type: &ast.Identifier{Value: []byte("Function")},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameE,
},
},
},
@@ -381,11 +348,9 @@ func TestResolveTraitUse(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@@ -704,11 +669,9 @@ func TestResolveNamespaces(t *testing.T) {
NamespaceName: namespaceCD,
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAC,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAC,
},
},
},

View File

@@ -282,11 +282,7 @@ func (v *Null) StmtUse(_ *ast.StmtUse) {
// do nothing
}
func (v *Null) StmtGroupUseList(_ *ast.StmtGroupUseList) {
// do nothing
}
func (v *Null) StmtUseList(_ *ast.StmtUseList) {
func (v *Null) StmtGroupUse(_ *ast.StmtGroupUse) {
// do nothing
}
@@ -294,10 +290,6 @@ func (v *Null) StmtUseDeclaration(_ *ast.StmtUseDeclaration) {
// do nothing
}
func (v *Null) StmtUseType(_ *ast.StmtUseType) {
// do nothing
}
func (v *Null) StmtWhile(_ *ast.StmtWhile) {
// do nothing
}