[refactoring] update ast structure of "Constant" nodes

This commit is contained in:
Vadym Slizov
2020-08-24 23:28:44 +03:00
parent 0285900fe5
commit c63213630a
19 changed files with 1457 additions and 1418 deletions

View File

@@ -1603,7 +1603,7 @@ func (p *PrettyPrinter) printStmtClassConstList(n ast.Vertex) {
func (p *PrettyPrinter) printStmtConstant(n ast.Vertex) {
nn := n.(*ast.StmtConstant)
p.Print(nn.ConstantName)
p.Print(nn.Name)
io.WriteString(p.w, " = ")
p.Print(nn.Expr)
}

View File

@@ -2530,8 +2530,8 @@ func TestPrintStmtClass(t *testing.T) {
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
},
@@ -2585,8 +2585,8 @@ func TestPrintStmtAnonymousClass(t *testing.T) {
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
},
@@ -2616,12 +2616,12 @@ func TestPrintStmtClassConstList(t *testing.T) {
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
},
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
Name: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
},
},
})
@@ -2639,8 +2639,8 @@ func TestPrintStmtConstant(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
})
expected := "FOO = 'BAR'"
@@ -2676,8 +2676,8 @@ func TestPrintStmtDeclareStmts(t *testing.T) {
&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtStmtList{
@@ -2710,8 +2710,8 @@ func TestPrintStmtDeclareExpr(t *testing.T) {
&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}},
@@ -2737,8 +2737,8 @@ func TestPrintStmtDeclareNop(t *testing.T) {
p.Print(&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtNop{},

View File

@@ -2421,69 +2421,27 @@ func (p *Printer) printStmtClass(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtClassConstList(n ast.Vertex) {
nn := n.(*ast.StmtClassConstList)
p.printFreeFloating(nn, token.Start)
if nn.Modifiers != nil {
for k, m := range nn.Modifiers {
if k > 0 && m.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(m)
}
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
}
p.printFreeFloating(nn, token.ModifierList)
io.WriteString(p.w, "const")
if nn.Consts[0].GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.joinPrint(",", nn.Consts)
p.printFreeFloating(nn, token.ConstList)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtClassConstList(n *ast.StmtClassConstList) {
p.joinPrintRefactored(" ", n.Modifiers)
p.bufStart = " "
p.printToken(n.ConstTkn, "const")
p.bufStart = " "
p.joinPrintRefactored(",", n.Consts)
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtConstList(n ast.Vertex) {
nn := n.(*ast.StmtConstList)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "const")
if nn.Consts[0].GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.joinPrint(",", nn.Consts)
p.printFreeFloating(nn, token.Stmts)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtConstList(n *ast.StmtConstList) {
p.printToken(n.ConstTkn, "const")
p.bufStart = " "
p.joinPrintRefactored(",", n.Consts)
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtConstant(n ast.Vertex) {
nn := n.(*ast.StmtConstant)
p.printFreeFloating(nn, token.Start)
p.Print(nn.ConstantName)
p.printFreeFloating(nn, token.Name)
io.WriteString(p.w, "=")
p.Print(nn.Expr)
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtConstant(n *ast.StmtConstant) {
p.Print(n.Name)
p.printToken(n.EqualTkn, "=")
p.Print(n.Expr)
p.printToken(n.CommaTkn, "")
}
func (p *Printer) printStmtContinue(n ast.Vertex) {
@@ -2515,7 +2473,7 @@ func (p *Printer) printStmtDeclare(n ast.Vertex) {
io.WriteString(p.w, "declare")
p.printFreeFloating(nn, token.Declare)
io.WriteString(p.w, "(")
p.joinPrint(",", nn.Consts)
p.joinPrintRefactored(",", nn.Consts)
p.printFreeFloating(nn, token.ConstList)
io.WriteString(p.w, ")")

View File

@@ -896,7 +896,8 @@ func TestParseAndPrintPhp5ClassConstList(t *testing.T) {
}
func TestParseAndPrintPhp5ConstList(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
const FOO = 1 , BAR = 2 ;
`
@@ -924,7 +925,7 @@ func TestParseAndPrintPhp5Continue(t *testing.T) {
func TestParseAndPrintPhp5Declare(t *testing.T) {
src := `<?php
declare ( FOO = 'bar' ) ;
declare ( FOO = 'bar' , BAR = 'foo' ) ;
declare ( FOO = 'bar' ) $a ;
declare ( FOO = 'bar' ) { }

View File

@@ -1012,7 +1012,8 @@ func TestParseAndPrintClassConstList(t *testing.T) {
}
func TestParseAndPrintConstList(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
const FOO = 1 , BAR = 2 ;
`
@@ -1040,7 +1041,7 @@ func TestParseAndPrintContinue(t *testing.T) {
func TestParseAndPrintDeclare(t *testing.T) {
src := `<?php
declare ( FOO = 'bar' ) ;
declare ( FOO = 'bar' , BAR = "foo" ) ;
declare ( FOO = 'bar' ) $a ;
declare ( FOO = 'bar' ) { }

View File

@@ -3028,8 +3028,8 @@ func TestPrinterPrintStmtClass(t *testing.T) {
},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
},
@@ -3078,8 +3078,8 @@ func TestPrinterPrintStmtAnonymousClass(t *testing.T) {
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
},
@@ -3102,12 +3102,12 @@ func TestPrinterPrintStmtClassConstList(t *testing.T) {
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
},
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
Name: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
},
},
})
@@ -3127,12 +3127,12 @@ func TestPrinterPrintStmtConstList(t *testing.T) {
p.Print(&ast.StmtConstList{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
},
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
Name: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
},
},
})
@@ -3150,8 +3150,8 @@ func TestPrinterPrintStmtConstant(t *testing.T) {
p := printer.NewPrinter(o)
p.Print(&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
})
expected := "FOO='BAR'"
@@ -3187,8 +3187,8 @@ func TestPrinterPrintStmtDeclareStmts(t *testing.T) {
p.Print(&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtStmtList{
@@ -3213,8 +3213,8 @@ func TestPrinterPrintStmtDeclareExpr(t *testing.T) {
p.Print(&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}},
@@ -3235,8 +3235,8 @@ func TestPrinterPrintStmtDeclareNop(t *testing.T) {
p.Print(&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtNop{},