[refactoring] update ast structure of "name" nodes

This commit is contained in:
Vadym Slizov
2020-08-24 14:20:20 +03:00
parent 767187ff85
commit c1b3e6f5b2
19 changed files with 1659 additions and 1509 deletions

View File

@@ -1931,9 +1931,9 @@ func (p *PrettyPrinter) printStmtNamespace(n ast.Vertex) {
io.WriteString(p.w, "namespace")
if nn.NamespaceName != nil {
if nn.Name != nil {
io.WriteString(p.w, " ")
p.Print(nn.NamespaceName)
p.Print(nn.Name)
}
if nn.Stmts != nil {

View File

@@ -22,7 +22,7 @@ abstract class Bar extends Baz
rootNode := &ast.Root{
Stmts: []ast.Vertex{
&ast.StmtNamespace{
NamespaceName: &ast.NameName{
Name: &ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{Value: []byte("Foo")},
},
@@ -3454,7 +3454,7 @@ func TestPrintNamespace(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtNamespace{
NamespaceName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Name: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
})
expected := `namespace Foo;`
@@ -3472,7 +3472,7 @@ func TestPrintNamespaceWithStmts(t *testing.T) {
p.Print(&ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtNamespace{
NamespaceName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Name: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}},
},

View File

@@ -55,6 +55,16 @@ func (p *Printer) joinPrint(glue string, nn []ast.Vertex) {
}
}
func (p *Printer) joinPrintRefactored(glue string, nn []ast.Vertex) {
for k, n := range nn {
if k > 0 {
p.bufStart = glue
}
p.Print(n)
}
}
func (p *Printer) printNodes(nn []ast.Vertex) {
for _, n := range nn {
p.Print(n)
@@ -558,50 +568,36 @@ func (p *Printer) printNodeArgument(n ast.Vertex) {
// name
func (p *Printer) printNameNamePart(n ast.Vertex) {
nn := n.(*ast.NameNamePart)
p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart)
p.bufStart = ""
io.WriteString(p.w, string(nn.Value))
p.printFreeFloating(nn, token.End)
func (p *Printer) printNameNamePart(n *ast.NameNamePart) {
p.printToken(n.NsSeparatorTkn, "")
p.printToken(n.StringTkn, string(n.Value))
}
func (p *Printer) printNameName(n ast.Vertex) {
nn := n.(*ast.NameName)
p.printFreeFloating(nn, token.Start)
func (p *Printer) printNameName(n *ast.NameName) {
p.printFreeFloating(n, token.Start)
p.joinPrint("\\", nn.Parts)
p.joinPrintRefactored("\\", n.Parts)
p.printFreeFloating(nn, token.End)
p.printToken(n.ListSeparatorTkn, "")
}
func (p *Printer) printNameFullyQualified(n ast.Vertex) {
nn := n.(*ast.NameFullyQualified)
p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart)
p.bufStart = ""
func (p *Printer) printNameFullyQualified(n *ast.NameFullyQualified) {
p.printFreeFloating(n, token.Start)
p.printToken(n.NsSeparatorTkn, "\\")
io.WriteString(p.w, "\\")
p.joinPrint("\\", nn.Parts)
p.joinPrintRefactored("\\", n.Parts)
p.printFreeFloating(nn, token.End)
p.printToken(n.ListSeparatorTkn, "")
}
func (p *Printer) printNameRelative(n ast.Vertex) {
nn := n.(*ast.NameRelative)
p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart)
p.bufStart = ""
func (p *Printer) printNameRelative(n *ast.NameRelative) {
p.printFreeFloating(n, token.Start)
p.printToken(n.NsTkn, "namespace")
p.printToken(n.NsSeparatorTkn, "\\")
io.WriteString(p.w, "namespace")
p.printFreeFloating(nn, token.Namespace)
p.joinPrintRefactored("\\", n.Parts)
for _, part := range nn.Parts {
io.WriteString(p.w, "\\")
p.Print(part)
}
p.printFreeFloating(nn, token.End)
p.printToken(n.ListSeparatorTkn, "")
}
// scalar
@@ -2299,7 +2295,9 @@ func (p *Printer) printStmtCatch(n ast.Vertex) {
io.WriteString(p.w, "catch")
p.printFreeFloating(nn, token.Catch)
io.WriteString(p.w, "(")
p.joinPrint("|", nn.Types)
p.joinPrintRefactored("|", nn.Types)
p.Print(nn.Var)
p.printFreeFloating(nn, token.Var)
io.WriteString(p.w, ")")
@@ -2409,10 +2407,9 @@ func (p *Printer) printStmtClass(n ast.Vertex) {
io.WriteString(p.w, " ")
}
io.WriteString(p.w, "implements")
if nn.Implements.InterfaceNames[0].GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.joinPrint(",", nn.Implements.InterfaceNames)
p.bufStart = " "
p.joinPrintRefactored(",", nn.Implements.InterfaceNames)
}
p.printFreeFloating(nn, token.Name)
@@ -2896,10 +2893,8 @@ func (p *Printer) printStmtInterface(n ast.Vertex) {
io.WriteString(p.w, " ")
}
io.WriteString(p.w, "extends")
if nn.Extends.InterfaceNames[0].GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.joinPrint(",", nn.Extends.InterfaceNames)
p.bufStart = " "
p.joinPrintRefactored(",", nn.Extends.InterfaceNames)
}
p.printFreeFloating(nn, token.Name)
@@ -2923,32 +2918,28 @@ func (p *Printer) printStmtLabel(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtNamespace(n ast.Vertex) {
nn := n.(*ast.StmtNamespace)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "namespace")
func (p *Printer) printStmtNamespace(n *ast.StmtNamespace) {
p.printToken(n.NsTkn, "namespace")
if nn.NamespaceName != nil {
if nn.NamespaceName.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(nn.NamespaceName)
if n.Name != nil {
p.bufStart = " "
p.Print(n.Name)
}
if nn.Stmts != nil {
p.printFreeFloating(nn, token.Namespace)
io.WriteString(p.w, "{")
p.printNodes(nn.Stmts)
p.printFreeFloating(nn, token.Stmts)
io.WriteString(p.w, "}")
} else {
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
if n.Stmts != nil {
p.printToken(n.OpenCurlyBracket, "{")
p.printNodes(n.Stmts)
p.printToken(n.CloseCurlyBracket, "}")
return
}
p.printFreeFloating(nn, token.End)
if n.OpenCurlyBracket != nil {
p.printToken(n.OpenCurlyBracket, "{")
p.printToken(n.CloseCurlyBracket, "}")
return
}
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtNop(n ast.Vertex) {
@@ -3202,10 +3193,8 @@ func (p *Printer) printStmtTraitUse(n ast.Vertex) {
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "use")
if nn.Traits[0].GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.joinPrint(",", nn.Traits)
p.bufStart = " "
p.joinPrintRefactored(",", nn.Traits)
p.Print(nn.TraitAdaptationList)
@@ -3276,11 +3265,13 @@ func (p *Printer) printStmtUnset(n ast.Vertex) {
func (p *Printer) printStmtUse(n *ast.StmtUse) {
p.printToken(n.UseTkn, "use")
p.bufStart = " "
p.Print(n.Type)
if n.Type != nil {
p.bufStart = " "
p.Print(n.Type)
}
p.bufStart = " "
p.joinPrint(",", n.UseDeclarations)
p.joinPrintRefactored(",", n.UseDeclarations)
p.printToken(n.SemiColonTkn, ";")
}
@@ -3298,16 +3289,7 @@ func (p *Printer) printStmtGroupUse(n *ast.StmtGroupUse) {
p.printToken(n.NsSeparatorTkn, "\\")
p.printToken(n.OpenCurlyBracketTkn, "{")
for k, v := range n.UseDeclarations {
p.Print(v)
var def string
if k != len(n.UseDeclarations)-1 {
def = ","
}
if decl, ok := v.(*ast.StmtUseDeclaration); ok {
p.printToken(decl.CommaTkn, def)
}
}
p.joinPrintRefactored(",", n.UseDeclarations)
p.printToken(n.CloseCurlyBracketTkn, "}")
p.printToken(n.SemiColonTkn, ";")
@@ -3325,6 +3307,7 @@ func (p *Printer) printStmtUseDeclaration(n *ast.StmtUseDeclaration) {
p.Print(n.Use)
if n.Alias == nil {
p.printToken(n.CommaTkn, "")
return
}
@@ -3333,6 +3316,8 @@ func (p *Printer) printStmtUseDeclaration(n *ast.StmtUseDeclaration) {
p.bufStart = " "
p.Print(n.Alias)
p.printToken(n.CommaTkn, "")
}
func (p *Printer) printStmtWhile(n ast.Vertex) {

View File

@@ -45,8 +45,8 @@ func TestParseAndPrintPhp5Root(t *testing.T) {
}
func TestParseAndPrintPhp5Identifier(t *testing.T) {
src := `<?
// TODO: remove ; after <?php
src := `<? ;
/* Foo */
Foo ( ) ;
`
@@ -73,8 +73,8 @@ func TestParseAndPrintPhp5Parameter(t *testing.T) {
}
func TestParseAndPrintPhp5Argument(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( $a , $b
, $c
) ; `
@@ -89,7 +89,8 @@ func TestParseAndPrintPhp5Argument(t *testing.T) {
// test name
func TestParseAndPrintPhp5Names(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( ) ;
\ foo ( ) ;
namespace \ foo ( ) ;
@@ -168,7 +169,8 @@ func TestParseAndPrintPhp5String(t *testing.T) {
}
func TestParseAndPrintPhp5Heredoc(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo(<<<EAP
test
EAP
@@ -289,7 +291,8 @@ func TestParseAndPrintPhp5Cast(t *testing.T) {
// test expr
func TestParseAndPrintPhp5ArrayDimFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
FOO [ ] ;
FOO [ 1 ] ;
$a [ ] ;
@@ -415,7 +418,8 @@ func TestParseAndPrintPhp5Closure(t *testing.T) {
}
func TestParseAndPrintPhp5ConstFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
null ;
`
@@ -481,7 +485,8 @@ func TestParseAndPrintPhp5Exit(t *testing.T) {
}
func TestParseAndPrintPhp5FunctionCall(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( ) ;
$var ( $a , ... $b , $c ) ;
`
@@ -551,7 +556,8 @@ func TestParseAndPrintPhp5MethodCall(t *testing.T) {
}
func TestParseAndPrintPhp5New(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
new Foo ;
@@ -664,7 +670,8 @@ func TestParseAndPrintPhp5ShortArray(t *testing.T) {
}
func TestParseAndPrintPhp5StaticCall(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
Foo :: bar ( $a , $b ) ;`
actual := printPhp5(parsePhp5(src))
@@ -675,7 +682,8 @@ func TestParseAndPrintPhp5StaticCall(t *testing.T) {
}
func TestParseAndPrintPhp5StaticPropertyFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
Foo :: $bar ;`
actual := printPhp5(parsePhp5(src))
@@ -1117,7 +1125,8 @@ func TestParseAndPrintPhp5GotoLabel(t *testing.T) {
}
func TestParseAndPrintPhp5Namespace(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
namespace Foo \ Bar ;
namespace Baz {

View File

@@ -13,7 +13,8 @@ import (
)
func ExamplePrinter() {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
namespace Foo;
@@ -37,7 +38,7 @@ abstract class Bar extends Baz
// change namespace
parts := &rootNode.(*ast.Root).Stmts[0].(*ast.StmtNamespace).NamespaceName.(*ast.NameName).Parts
parts := &rootNode.(*ast.Root).Stmts[1].(*ast.StmtNamespace).Name.(*ast.NameName).Parts
*parts = append(*parts, &ast.NameNamePart{Value: []byte("Quuz")})
// print
@@ -46,7 +47,7 @@ abstract class Bar extends Baz
p.Print(rootNode)
// Output:
//<?php
//<?php ;
//
// namespace Foo\Quuz;
//
@@ -94,8 +95,8 @@ func TestParseAndPrintRoot(t *testing.T) {
}
func TestParseAndPrintIdentifier(t *testing.T) {
src := `<?
// TODO: remove ; after <?php
src := `<? ;
/* Foo */
Foo ( ) ;
`
@@ -151,8 +152,8 @@ func TestParseAndPrintNullable(t *testing.T) {
}
func TestParseAndPrintArgument(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( $a , $b
, ... $c ,
) ; `
@@ -167,7 +168,8 @@ func TestParseAndPrintArgument(t *testing.T) {
// test name
func TestParseAndPrintNames(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( ) ;
\ foo ( ) ;
namespace \ foo ( ) ;
@@ -246,7 +248,8 @@ func TestParseAndPrintString(t *testing.T) {
}
func TestParseAndPrintHeredoc(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo(<<<EAP
test
EAP
@@ -370,7 +373,8 @@ func TestParseAndPrintCast(t *testing.T) {
// test expr
func TestParseAndPrintArrayDimFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
FOO [ ] ;
FOO [ 1 ] ;
$a [ ] ;
@@ -509,7 +513,8 @@ func TestParseAndPrintArrowFunction(t *testing.T) {
}
func TestParseAndPrintConstFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
null ;
`
@@ -575,7 +580,8 @@ func TestParseAndPrintExit(t *testing.T) {
}
func TestParseAndPrintFunctionCall(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( ) ;
$var ( $a , ... $b , $c ) ;
`
@@ -776,7 +782,8 @@ func TestParseAndPrintShortList(t *testing.T) {
}
func TestParseAndPrintStaticCall(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
Foo :: bar ( $a , $b ) ;`
actual := print(parse(src))
@@ -787,7 +794,8 @@ func TestParseAndPrintStaticCall(t *testing.T) {
}
func TestParseAndPrintStaticPropertyFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
Foo :: $bar ;`
actual := print(parse(src))
@@ -1250,7 +1258,8 @@ func TestParseAndPrintGotoLabel(t *testing.T) {
}
func TestParseAndPrintNamespace(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
namespace Foo \ Bar ;
namespace Baz {

View File

@@ -17,7 +17,7 @@ func TestPrinterPrintFile(t *testing.T) {
p.Print(&ast.Root{
Stmts: []ast.Vertex{
&ast.StmtNamespace{
NamespaceName: &ast.NameName{
Name: &ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{Value: []byte("Foo")},
},
@@ -3862,7 +3862,7 @@ func TestPrinterPrintNamespace(t *testing.T) {
p := printer.NewPrinter(o)
p.Print(&ast.StmtNamespace{
NamespaceName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Name: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
})
expected := `namespace Foo;`
@@ -3878,7 +3878,7 @@ func TestPrinterPrintNamespaceWithStmts(t *testing.T) {
p := printer.NewPrinter(o)
p.Print(&ast.StmtNamespace{
NamespaceName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Name: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},