[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

@@ -353,8 +353,6 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
p.printStmtGlobal(n)
case *ast.StmtGoto:
p.printStmtGoto(n)
case *ast.StmtGroupUse:
p.printStmtGroupUse(n)
case *ast.StmtHaltCompiler:
p.printStmtHaltCompiler(n)
case *ast.StmtIf:
@@ -399,10 +397,16 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
p.printStmtTry(n)
case *ast.StmtUnset:
p.printStmtUnset(n)
case *ast.StmtUseList:
p.printStmtUseList(n)
case *ast.StmtUse:
p.printStmtUse(n)
case *ast.StmtGroupUseList:
p.printStmtGroupUseList(n)
case *ast.StmtUseList:
p.printStmtUseList(n)
case *ast.StmtUseDeclaration:
p.printStmtUseDeclaration(n)
case *ast.StmtUseType:
p.printStmtUseType(n)
case *ast.StmtWhile:
p.printStmtWhile(n)
}
@@ -1847,22 +1851,6 @@ func (p *PrettyPrinter) printStmtGoto(n ast.Vertex) {
io.WriteString(p.w, ";")
}
func (p *PrettyPrinter) printStmtGroupUse(n ast.Vertex) {
nn := n.(*ast.StmtGroupUse)
io.WriteString(p.w, "use ")
if nn.UseType != nil {
p.Print(nn.UseType)
io.WriteString(p.w, " ")
}
p.Print(nn.Prefix)
io.WriteString(p.w, "\\{")
p.joinPrint(", ", nn.UseList)
io.WriteString(p.w, "};")
}
func (p *PrettyPrinter) printStmtHaltCompiler(n ast.Vertex) {
io.WriteString(p.w, "__halt_compiler();")
}
@@ -2146,27 +2134,34 @@ func (p *PrettyPrinter) printStmtUnset(n ast.Vertex) {
io.WriteString(p.w, ");")
}
func (p *PrettyPrinter) printStmtUseList(n ast.Vertex) {
nn := n.(*ast.StmtUseList)
io.WriteString(p.w, "use ")
if nn.UseType != nil {
p.Print(nn.UseType)
io.WriteString(p.w, " ")
}
p.joinPrint(", ", nn.Uses)
io.WriteString(p.w, ";")
}
func (p *PrettyPrinter) printStmtUse(n ast.Vertex) {
nn := n.(*ast.StmtUse)
if nn.UseType != nil {
p.Print(nn.UseType)
io.WriteString(p.w, " ")
}
io.WriteString(p.w, "use ")
p.Print(nn.UseList)
io.WriteString(p.w, ";")
}
func (p *PrettyPrinter) printStmtGroupUseList(n ast.Vertex) {
nn := n.(*ast.StmtGroupUseList)
p.Print(nn.Prefix)
io.WriteString(p.w, "\\{")
p.Print(nn.UseList)
io.WriteString(p.w, "}")
}
func (p *PrettyPrinter) printStmtUseList(n ast.Vertex) {
nn := n.(*ast.StmtUseList)
p.joinPrint(", ", nn.UseDeclarations)
}
func (p *PrettyPrinter) printStmtUseDeclaration(n ast.Vertex) {
nn := n.(*ast.StmtUseDeclaration)
p.Print(nn.Use)
@@ -2176,6 +2171,15 @@ func (p *PrettyPrinter) printStmtUse(n ast.Vertex) {
}
}
func (p *PrettyPrinter) printStmtUseType(n ast.Vertex) {
nn := n.(*ast.StmtUseType)
p.Print(nn.Type)
io.WriteString(p.w, " ")
p.Print(nn.Use)
}
func (p *PrettyPrinter) printStmtWhile(n ast.Vertex) {
nn := n.(*ast.StmtWhile)

View File

@@ -3256,32 +3256,6 @@ func TestPrintStmtGoto(t *testing.T) {
}
}
func TestPrintStmtGroupUse(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtGroupUse{
UseType: &ast.Identifier{Value: []byte("function")},
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
UseList: []ast.Vertex{
&ast.StmtUse{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
Alias: &ast.Identifier{Value: []byte("Baz")},
},
&ast.StmtUse{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Quuz")}}},
},
},
})
expected := `use function Foo\{Bar as Baz, Quuz};`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintHaltCompiler(t *testing.T) {
o := bytes.NewBufferString("")
@@ -3975,24 +3949,15 @@ func TestPrintStmtUset(t *testing.T) {
}
}
func TestPrintStmtUseList(t *testing.T) {
func TestPrintUse(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUseList{
UseType: &ast.Identifier{Value: []byte("function")},
Uses: []ast.Vertex{
&ast.StmtUse{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
&ast.StmtUse{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
},
},
p.Print(&ast.StmtUse{
UseList: &ast.StmtUseList{},
})
expected := `use function Foo as Bar, Baz;`
expected := `use ;`
actual := o.String()
if expected != actual {
@@ -4000,14 +3965,74 @@ func TestPrintStmtUseList(t *testing.T) {
}
}
func TestPrintUse(t *testing.T) {
func TestPrintStmtGroupUseList(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUse{
UseType: &ast.Identifier{Value: []byte("function")},
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
p.Print(&ast.StmtGroupUseList{
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
UseList: &ast.StmtUseList{},
})
expected := `Foo\{}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtUseList(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
},
},
})
expected := `Foo as Bar, Baz`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintUseDeclaration(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
})
expected := `Foo as Bar`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintUseType(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
})
expected := `function Foo as Bar`

View File

@@ -16,8 +16,10 @@ const (
)
type Printer struct {
w io.Writer
s printerState
w io.Writer
s printerState
bufStart string
bufEnd string
}
// NewPrinter - Constructor for Printer
@@ -60,6 +62,21 @@ func (p *Printer) printNodes(nn []ast.Vertex) {
}
}
func (p *Printer) printFreeFloatingOrDefault(n ast.Vertex, pos token.Position, def string) {
if n == nil {
return
}
if len(n.GetNode().Tokens[pos]) == 0 {
io.WriteString(p.w, def)
return
}
for _, m := range n.GetNode().Tokens[pos] {
io.WriteString(p.w, string(m.Value))
}
}
func (p *Printer) printFreeFloating(n ast.Vertex, pos token.Position) {
if n == nil {
return
@@ -374,8 +391,6 @@ func (p *Printer) printNode(n ast.Vertex) {
p.printStmtGlobal(n)
case *ast.StmtGoto:
p.printStmtGoto(n)
case *ast.StmtGroupUse:
p.printStmtGroupUse(n)
case *ast.StmtHaltCompiler:
p.printStmtHaltCompiler(n)
case *ast.StmtIf:
@@ -422,10 +437,16 @@ func (p *Printer) printNode(n ast.Vertex) {
p.printStmtTry(n)
case *ast.StmtUnset:
p.printStmtUnset(n)
case *ast.StmtUseList:
p.printStmtUseList(n)
case *ast.StmtUse:
p.printStmtUse(n)
case *ast.StmtGroupUseList:
p.printStmtGroupUseList(n)
case *ast.StmtUseList:
p.printStmtUseList(n)
case *ast.StmtUseDeclaration:
p.printStmtUseDeclaration(n)
case *ast.StmtUseType:
p.printStmtUseType(n)
case *ast.StmtWhile:
p.printStmtWhile(n)
}
@@ -443,8 +464,11 @@ func (p *Printer) printNodeRoot(n ast.Vertex) {
func (p *Printer) printNodeIdentifier(n ast.Vertex) {
nn := n.(*ast.Identifier)
p.printFreeFloating(nn, token.Start)
p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart)
p.bufStart = ""
io.WriteString(p.w, string(nn.Value))
p.printFreeFloating(nn, token.End)
}
@@ -519,7 +543,8 @@ func (p *Printer) printNodeArgument(n ast.Vertex) {
func (p *Printer) printNameNamePart(n ast.Vertex) {
nn := n.(*ast.NameNamePart)
p.printFreeFloating(nn, token.Start)
p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart)
p.bufStart = ""
io.WriteString(p.w, string(nn.Value))
@@ -532,22 +557,26 @@ func (p *Printer) printNameName(n ast.Vertex) {
p.joinPrint("\\", nn.Parts)
p.printFreeFloating(nn, token.End)
p.printFreeFloatingOrDefault(nn, token.End, p.bufEnd)
p.bufEnd = ""
}
func (p *Printer) printNameFullyQualified(n ast.Vertex) {
nn := n.(*ast.NameFullyQualified)
p.printFreeFloating(nn, token.Start)
p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart)
p.bufStart = ""
io.WriteString(p.w, "\\")
p.joinPrint("\\", nn.Parts)
p.printFreeFloating(nn, token.End)
p.printFreeFloatingOrDefault(nn, token.End, p.bufEnd)
p.bufEnd = ""
}
func (p *Printer) printNameRelative(n ast.Vertex) {
nn := n.(*ast.NameRelative)
p.printFreeFloating(nn, token.Start)
p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart)
p.bufStart = ""
io.WriteString(p.w, "namespace")
p.printFreeFloating(nn, token.Namespace)
@@ -557,7 +586,8 @@ func (p *Printer) printNameRelative(n ast.Vertex) {
p.Print(part)
}
p.printFreeFloating(nn, token.End)
p.printFreeFloatingOrDefault(nn, token.End, p.bufEnd)
p.bufEnd = ""
}
// scalar
@@ -2710,41 +2740,6 @@ func (p *Printer) printStmtGoto(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtGroupUse(n ast.Vertex) {
nn := n.(*ast.StmtGroupUse)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "use")
p.printFreeFloating(nn, token.Use)
if nn.UseType != nil {
if nn.UseType.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(nn.UseType)
}
if nn.Prefix.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(nn.Prefix)
io.WriteString(p.w, "\\")
p.printFreeFloating(nn, token.Slash)
io.WriteString(p.w, "{")
p.joinPrint(",", nn.UseList)
p.printFreeFloating(nn, token.Stmts)
io.WriteString(p.w, "}")
p.printFreeFloating(nn, token.UseDeclarationList)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtHaltCompiler(n ast.Vertex) {
nn := n.(*ast.StmtHaltCompiler)
p.printFreeFloating(nn, token.Start)
@@ -3197,59 +3192,78 @@ func (p *Printer) printStmtUnset(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtUseList(n ast.Vertex) {
nn := n.(*ast.StmtUseList)
func (p *Printer) printStmtUse(n ast.Vertex) {
nn := n.(*ast.StmtUse)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "use")
if nn.UseType != nil {
if nn.UseType.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(nn.UseType)
p.bufStart = " "
p.Print(nn.UseList)
p.printFreeFloatingOrDefault(nn, token.End, ";")
}
func (p *Printer) printStmtGroupUseList(n ast.Vertex) {
nn := n.(*ast.StmtGroupUseList)
p.printFreeFloating(nn, token.Start)
p.Print(nn.Prefix)
p.bufStart = "\\{"
p.bufEnd = "}"
p.Print(nn.UseList)
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtUseList(n ast.Vertex) {
nn := n.(*ast.StmtUseList)
bufEnd := p.bufEnd
p.bufEnd = ""
if p.bufStart == "\\{" {
p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart)
p.bufStart = ""
} else {
p.printFreeFloating(nn, token.Start)
}
if nn.Uses[0].GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.joinPrint(",", nn.Uses)
p.printFreeFloating(nn, token.UseDeclarationList)
p.joinPrint(",", nn.UseDeclarations)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
p.printFreeFloatingOrDefault(nn, token.End, bufEnd)
}
func (p *Printer) printStmtUseDeclaration(n ast.Vertex) {
nn := n.(*ast.StmtUseDeclaration)
p.printFreeFloating(nn, token.Start)
if nn.Alias != nil {
p.bufEnd = " "
}
p.Print(nn.Use)
if nn.Alias != nil {
io.WriteString(p.w, "as")
p.bufStart = " "
p.Print(nn.Alias)
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtUse(n ast.Vertex) {
nn := n.(*ast.StmtUse)
func (p *Printer) printStmtUseType(n ast.Vertex) {
nn := n.(*ast.StmtUseType)
p.printFreeFloating(nn, token.Start)
if nn.UseType != nil {
p.Print(nn.UseType)
if nn.UseType.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
}
p.printFreeFloating(nn, token.Slash)
p.Print(nn.Type)
p.bufStart = " "
p.Print(nn.Use)
if nn.Alias != nil {
if nn.Alias.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
io.WriteString(p.w, "as")
if nn.Alias.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(nn.Alias)
}
p.printFreeFloating(nn, token.End)
}

View File

@@ -3673,32 +3673,6 @@ func TestPrinterPrintStmtGoto(t *testing.T) {
}
}
func TestPrinterPrintStmtGroupUse(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtGroupUse{
UseType: &ast.Identifier{Value: []byte("function")},
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
UseList: []ast.Vertex{
&ast.StmtUse{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
Alias: &ast.Identifier{Value: []byte("Baz")},
},
&ast.StmtUse{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Quuz")}}},
},
},
})
expected := `use function Foo\{Bar as Baz,Quuz};`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintHaltCompiler(t *testing.T) {
o := bytes.NewBufferString("")
@@ -4412,24 +4386,21 @@ func TestPrinterPrintStmtUnset(t *testing.T) {
}
}
func TestPrinterPrintStmtUseList(t *testing.T) {
func TestPrinterPrintUse(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtUseList{
UseType: &ast.Identifier{Value: []byte("function")},
Uses: []ast.Vertex{
&ast.StmtUse{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
&ast.StmtUse{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
p.Print(&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
},
},
},
})
expected := `use function Foo as Bar,Baz;`
expected := `use Foo;`
actual := o.String()
if expected != actual {
@@ -4437,14 +4408,74 @@ func TestPrinterPrintStmtUseList(t *testing.T) {
}
}
func TestPrinterPrintUse(t *testing.T) {
func TestPrinterPrintStmtGroupUseList(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtUse{
UseType: &ast.Identifier{Value: []byte("function")},
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
p.Print(&ast.StmtGroupUseList{
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
UseList: &ast.StmtUseList{},
})
expected := `Foo\{}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtUseList(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
},
},
})
expected := `Foo as Bar,Baz`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintUseDeclaration(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
})
expected := `Foo as Bar`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintUseType(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
})
expected := `function Foo as Bar`