refactoring: remove StmtTraitAdaptationList node

This commit is contained in:
Vadym Slizov 2020-12-20 23:04:23 +02:00
parent 74b0949255
commit 03c7979ccd
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
17 changed files with 1221 additions and 1413 deletions

File diff suppressed because it is too large Load Diff

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -2150,13 +2150,23 @@ class_statement:
trait_use_statement:
T_USE trait_list trait_adaptations
{
$$ = &ast.StmtTraitUse{
traitUse := &ast.StmtTraitUse{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $3),
UseTkn: $1,
Traits: $2.(*ast.ParserSeparatedList).Items,
SeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns,
Adaptations: $3,
}
switch n := $3.(type) {
case *ast.TraitAdaptationList :
traitUse.OpenCurlyBracketTkn = n.OpenCurlyBracketTkn
traitUse.Adaptations = n.Adaptations
traitUse.CloseCurlyBracketTkn = n.CloseCurlyBracketTkn
case *ast.StmtNop :
traitUse.SemiColonTkn = n.SemiColonTkn
};
$$ = traitUse
}
;
@ -2186,7 +2196,7 @@ trait_adaptations:
}
| '{' trait_adaptation_list '}'
{
$$ = &ast.StmtTraitAdaptationList{
$$ = &ast.TraitAdaptationList{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenCurlyBracketTkn: $1,
Adaptations: $2,

File diff suppressed because it is too large Load Diff

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -1970,13 +1970,23 @@ class_statement:
}
| T_USE name_list trait_adaptations
{
$$ = &ast.StmtTraitUse{
traitUse := &ast.StmtTraitUse{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $3),
UseTkn: $1,
Traits: $2.(*ast.ParserSeparatedList).Items,
SeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns,
Adaptations: $3,
}
switch n := $3.(type) {
case *ast.TraitAdaptationList :
traitUse.OpenCurlyBracketTkn = n.OpenCurlyBracketTkn
traitUse.Adaptations = n.Adaptations
traitUse.CloseCurlyBracketTkn = n.CloseCurlyBracketTkn
case *ast.StmtNop :
traitUse.SemiColonTkn = n.SemiColonTkn
};
$$ = traitUse
}
| method_modifiers T_FUNCTION returns_ref identifier backup_doc_comment '(' parameter_list ')' return_type method_body
{
@ -2032,7 +2042,7 @@ trait_adaptations:
}
| '{' '}'
{
$$ = &ast.StmtTraitAdaptationList{
$$ = &ast.TraitAdaptationList{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $2),
OpenCurlyBracketTkn: $1,
CloseCurlyBracketTkn: $2,
@ -2040,7 +2050,7 @@ trait_adaptations:
}
| '{' trait_adaptation_list '}'
{
$$ = &ast.StmtTraitAdaptationList{
$$ = &ast.TraitAdaptationList{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenCurlyBracketTkn: $1,
Adaptations: $2,

View File

@ -67,7 +67,6 @@ type NodeVisitor interface {
StmtSwitch(n *StmtSwitch)
StmtThrow(n *StmtThrow)
StmtTrait(n *StmtTrait)
StmtTraitAdaptationList(n *StmtTraitAdaptationList)
StmtTraitMethodRef(n *StmtTraitMethodRef)
StmtTraitUse(n *StmtTraitUse)
StmtTraitUseAlias(n *StmtTraitUseAlias)

View File

@ -945,22 +945,6 @@ func (n *StmtTrait) GetPosition() *position.Position {
return n.Position
}
// StmtTraitAdaptationList node
type StmtTraitAdaptationList struct {
Position *position.Position
OpenCurlyBracketTkn *token.Token
Adaptations []Vertex
CloseCurlyBracketTkn *token.Token
}
func (n *StmtTraitAdaptationList) Accept(v NodeVisitor) {
v.StmtTraitAdaptationList(n)
}
func (n *StmtTraitAdaptationList) GetPosition() *position.Position {
return n.Position
}
// StmtTraitMethodRef node
type StmtTraitMethodRef struct {
Position *position.Position
@ -979,11 +963,14 @@ func (n *StmtTraitMethodRef) GetPosition() *position.Position {
// StmtTraitUse node
type StmtTraitUse struct {
Position *position.Position
UseTkn *token.Token
Traits []Vertex
SeparatorTkns []*token.Token
Adaptations Vertex
Position *position.Position
UseTkn *token.Token
Traits []Vertex
SeparatorTkns []*token.Token
OpenCurlyBracketTkn *token.Token
Adaptations []Vertex
CloseCurlyBracketTkn *token.Token
SemiColonTkn *token.Token
}
func (n *StmtTraitUse) Accept(v NodeVisitor) {
@ -2699,6 +2686,22 @@ func (n *ParserSeparatedList) GetPosition() *position.Position {
return n.Position
}
// TraitAdaptationList node
type TraitAdaptationList struct {
Position *position.Position
OpenCurlyBracketTkn *token.Token
Adaptations []Vertex
CloseCurlyBracketTkn *token.Token
}
func (n *TraitAdaptationList) Accept(v NodeVisitor) {
// do nothing
}
func (n *TraitAdaptationList) GetPosition() *position.Position {
return n.Position
}
// ArgumentList node
type ArgumentList struct {
Position *position.Position

View File

@ -812,20 +812,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
}
t.visitor.Leave("Stmts", false)
}
case *ast.StmtTraitAdaptationList:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Adaptations != nil {
t.visitor.Enter("Adaptations", false)
for _, c := range nn.Adaptations {
t.Traverse(c)
}
t.visitor.Leave("Adaptations", false)
}
case *ast.StmtTraitMethodRef:
if nn == nil {
return
@ -858,9 +844,11 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.visitor.Leave("Traits", false)
}
if nn.Adaptations != nil {
t.visitor.Enter("Adaptations", true)
t.Traverse(nn.Adaptations)
t.visitor.Leave("Adaptations", true)
t.visitor.Enter("Adaptations", false)
for _, c := range nn.Adaptations {
t.Traverse(c)
}
t.visitor.Leave("Adaptations", false)
}
case *ast.StmtTraitUseAlias:
if nn == nil {

View File

@ -858,19 +858,6 @@ func (v *Dumper) StmtTrait(n *ast.StmtTrait) {
v.print(v.indent, "},\n")
}
func (v *Dumper) StmtTraitAdaptationList(n *ast.StmtTraitAdaptationList) {
v.print(0, "&ast.StmtTraitAdaptationList{\n")
v.indent++
v.dumpPosition(n.Position)
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
v.dumpVertexList("Adaptations", n.Adaptations)
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
v.indent--
v.print(v.indent, "},\n")
}
func (v *Dumper) StmtTraitMethodRef(n *ast.StmtTraitMethodRef) {
v.print(0, "&ast.StmtTraitMethodRef{\n")
v.indent++
@ -892,7 +879,10 @@ func (v *Dumper) StmtTraitUse(n *ast.StmtTraitUse) {
v.dumpToken("UseTkn", n.UseTkn)
v.dumpVertexList("Traits", n.Traits)
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
v.dumpVertex("Adaptations", n.Adaptations)
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
v.dumpVertexList("Adaptations", n.Adaptations)
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
v.dumpToken("SemiColonTkn", n.SemiColonTkn)
v.indent--
v.print(v.indent, "},\n")

View File

@ -886,21 +886,6 @@ func (f *formatter) StmtTrait(n *ast.StmtTrait) {
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
}
func (f *formatter) StmtTraitAdaptationList(n *ast.StmtTraitAdaptationList) {
n.OpenCurlyBracketTkn = f.newToken('{', []byte("{"))
if len(n.Adaptations) > 0 {
f.indent++
f.formatStmts(&n.Adaptations)
f.indent--
f.addFreeFloating(token.T_WHITESPACE, []byte("\n"))
f.addIndent()
}
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
}
func (f *formatter) StmtTraitMethodRef(n *ast.StmtTraitMethodRef) {
if n.Trait != nil {
n.Trait.Accept(f)
@ -916,11 +901,27 @@ func (f *formatter) StmtTraitUse(n *ast.StmtTraitUse) {
n.SeparatorTkns = f.formatList(n.Traits, ',')
if _, ok := n.Adaptations.(*ast.StmtTraitAdaptationList); ok {
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
}
n.OpenCurlyBracketTkn = nil
n.CloseCurlyBracketTkn = nil
n.SemiColonTkn = nil
n.Adaptations.Accept(f)
if len(n.Adaptations) > 0 {
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
n.OpenCurlyBracketTkn = f.newToken('{', []byte("{"))
if len(n.Adaptations) > 0 {
f.indent++
f.formatStmts(&n.Adaptations)
f.indent--
f.addFreeFloating(token.T_WHITESPACE, []byte("\n"))
f.addIndent()
}
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
} else {
n.SemiColonTkn = f.newToken(';', []byte(";"))
}
}
func (f *formatter) StmtTraitUseAlias(n *ast.StmtTraitUseAlias) {

View File

@ -2503,76 +2503,6 @@ func TestFormatter_StmtTrait_Implements(t *testing.T) {
}
}
func TestFormatter_StmtTraitAdaptationList(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtTraitAdaptationList{}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
n.Accept(f)
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `{}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_StmtTraitAdaptationList_List(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtTraitAdaptationList{
Adaptations: []ast.Vertex{
&ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Method: &ast.Identifier{
Value: []byte("foo"),
},
},
Alias: &ast.Identifier{
Value: []byte("bar"),
},
},
&ast.StmtTraitUsePrecedence{
Ref: &ast.StmtTraitMethodRef{
Method: &ast.Identifier{
Value: []byte("foo"),
},
},
Insteadof: []ast.Vertex{
&ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{
Value: []byte("bar"),
},
},
},
},
},
},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
n.Accept(f)
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `{
foo as bar;
foo insteadof bar;
}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_StmtTraitMethodRef(t *testing.T) {
o := bytes.NewBufferString("")
@ -2646,7 +2576,6 @@ func TestFormatter_StmtTraitUse(t *testing.T) {
},
},
},
Adaptations: &ast.StmtNop{},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
@ -2683,7 +2612,18 @@ func TestFormatter_StmtTraitUse_Adaptations(t *testing.T) {
},
},
},
Adaptations: &ast.StmtTraitAdaptationList{},
Adaptations: []ast.Vertex{
&ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Method: &ast.Identifier{
Value: []byte("foo"),
},
},
Alias: &ast.Identifier{
Value: []byte("baz"),
},
},
},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
@ -2692,7 +2632,9 @@ func TestFormatter_StmtTraitUse_Adaptations(t *testing.T) {
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `use foo, bar {}`
expected := `use foo, bar {
foo as baz;
}`
actual := o.String()
if expected != actual {

View File

@ -184,23 +184,21 @@ func (nsr *NamespaceResolver) StmtTraitUse(n *ast.StmtTraitUse) {
nsr.ResolveName(t, "")
}
if adaptationList, ok := n.Adaptations.(*ast.StmtTraitAdaptationList); ok {
for _, a := range adaptationList.Adaptations {
switch aa := a.(type) {
case *ast.StmtTraitUsePrecedence:
refTrait := aa.Ref.(*ast.StmtTraitMethodRef).Trait
if refTrait != nil {
nsr.ResolveName(refTrait, "")
}
for _, insteadOf := range aa.Insteadof {
nsr.ResolveName(insteadOf, "")
}
for _, a := range n.Adaptations {
switch aa := a.(type) {
case *ast.StmtTraitUsePrecedence:
refTrait := aa.Ref.(*ast.StmtTraitMethodRef).Trait
if refTrait != nil {
nsr.ResolveName(refTrait, "")
}
for _, insteadOf := range aa.Insteadof {
nsr.ResolveName(insteadOf, "")
}
case *ast.StmtTraitUseAlias:
refTrait := aa.Ref.(*ast.StmtTraitMethodRef).Trait
if refTrait != nil {
nsr.ResolveName(refTrait, "")
}
case *ast.StmtTraitUseAlias:
refTrait := aa.Ref.(*ast.StmtTraitMethodRef).Trait
if refTrait != nil {
nsr.ResolveName(refTrait, "")
}
}
}

View File

@ -354,22 +354,20 @@ func TestResolveTraitUse(t *testing.T) {
nameB,
relativeNameB,
},
Adaptations: &ast.StmtTraitAdaptationList{
Adaptations: []ast.Vertex{
&ast.StmtTraitUsePrecedence{
Ref: &ast.StmtTraitMethodRef{
Trait: fullyQualifiedNameB,
Method: &ast.Identifier{Value: []byte("foo")},
},
Insteadof: []ast.Vertex{fullyQualifiedNameBC},
Adaptations: []ast.Vertex{
&ast.StmtTraitUsePrecedence{
Ref: &ast.StmtTraitMethodRef{
Trait: fullyQualifiedNameB,
Method: &ast.Identifier{Value: []byte("foo")},
},
&ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Trait: relativeNameBC,
Method: &ast.Identifier{Value: []byte("foo")},
},
Alias: &ast.Identifier{Value: []byte("bar")},
Insteadof: []ast.Vertex{fullyQualifiedNameBC},
},
&ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Trait: relativeNameBC,
Method: &ast.Identifier{Value: []byte("foo")},
},
Alias: &ast.Identifier{Value: []byte("bar")},
},
},
},

View File

@ -206,10 +206,6 @@ func (v *Null) StmtTrait(_ *ast.StmtTrait) {
// do nothing
}
func (v *Null) StmtTraitAdaptationList(_ *ast.StmtTraitAdaptationList) {
// do nothing
}
func (v *Null) StmtTraitMethodRef(_ *ast.StmtTraitMethodRef) {
// do nothing
}

View File

@ -531,12 +531,6 @@ func (p *printer) StmtTrait(n *ast.StmtTrait) {
p.printToken(n.CloseCurlyBracketTkn, []byte("}"))
}
func (p *printer) StmtTraitAdaptationList(n *ast.StmtTraitAdaptationList) {
p.printToken(n.OpenCurlyBracketTkn, []byte("{"))
p.printList(n.Adaptations)
p.printToken(n.CloseCurlyBracketTkn, []byte("}"))
}
func (p *printer) StmtTraitMethodRef(n *ast.StmtTraitMethodRef) {
p.printNode(n.Trait)
p.printToken(n.DoubleColonTkn, p.ifNode(n.Trait, []byte("::")))
@ -546,7 +540,10 @@ func (p *printer) StmtTraitMethodRef(n *ast.StmtTraitMethodRef) {
func (p *printer) StmtTraitUse(n *ast.StmtTraitUse) {
p.printToken(n.UseTkn, []byte("use"))
p.printSeparatedList(n.Traits, n.SeparatorTkns, []byte(","))
p.printNode(n.Adaptations)
p.printToken(n.OpenCurlyBracketTkn, p.ifNodeList(n.Adaptations, []byte("{")))
p.printList(n.Adaptations)
p.printToken(n.CloseCurlyBracketTkn, p.ifNodeList(n.Adaptations, []byte("}")))
p.printToken(n.SemiColonTkn, p.ifNotToken(n.OpenCurlyBracketTkn, p.ifNotNodeList(n.Adaptations, []byte(";"))))
}
func (p *printer) StmtTraitUseAlias(n *ast.StmtTraitUseAlias) {

View File

@ -4339,31 +4339,6 @@ func TestPrinterPrintStmtThrow(t *testing.T) {
}
}
func TestPrinterPrintStmtTraitAdaptationList(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.StmtTraitAdaptationList{
Adaptations: []ast.Vertex{
&ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Method: &ast.Identifier{Value: []byte("a")},
},
Alias: &ast.Identifier{Value: []byte("b")},
},
},
}
n.Accept(p)
expected := `{Foo::a as b;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtTraitMethodRef(t *testing.T) {
o := bytes.NewBufferString("")
@ -4454,7 +4429,6 @@ func TestPrinterPrintStmtTraitUse(t *testing.T) {
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
},
Adaptations: &ast.StmtNop{},
}
n.Accept(p)
@ -4475,15 +4449,13 @@ func TestPrinterPrintStmtTraitAdaptations(t *testing.T) {
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
},
Adaptations: &ast.StmtTraitAdaptationList{
Adaptations: []ast.Vertex{
&ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Method: &ast.Identifier{Value: []byte("a")},
},
Alias: &ast.Identifier{Value: []byte("b")},
Adaptations: []ast.Vertex{
&ast.StmtTraitUseAlias{
Ref: &ast.StmtTraitMethodRef{
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Method: &ast.Identifier{Value: []byte("a")},
},
Alias: &ast.Identifier{Value: []byte("b")},
},
},
}