[refactoring] update ast structure of "Static", "Global" and "StaticVar" nodes

This commit is contained in:
Vadym Slizov
2020-09-09 22:53:51 +03:00
parent 59ef622082
commit 3f12ada311
10 changed files with 1416 additions and 1324 deletions

View File

@@ -65,6 +65,26 @@ func (p *Printer) joinPrintRefactored(glue string, nn []ast.Vertex) {
}
}
func (p *Printer) printSeparatedList(nodeList []ast.Vertex, separatorList []*token.Token, def string) {
var separators []*token.Token
if cap(separatorList) >= len(nodeList) {
separators = separatorList[:len(nodeList)]
} else {
separators = make([]*token.Token, len(nodeList))
copy(separators, separatorList)
}
for k, n := range nodeList {
p.Print(n)
if k < len(nodeList)-1 {
p.printToken(separators[k], def)
} else {
p.printToken(separators[k], "")
}
}
}
func (p *Printer) printNodes(nn []ast.Vertex) {
for _, n := range nn {
p.Print(n)
@@ -2476,20 +2496,11 @@ func (p *Printer) printStmtFunction(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtGlobal(n ast.Vertex) {
nn := n.(*ast.StmtGlobal)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "global")
p.joinPrint(",", nn.Vars)
p.printFreeFloating(nn, token.VarList)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtGlobal(n *ast.StmtGlobal) {
p.printToken(n.GlobalTkn, "global")
p.bufStart = " "
p.printSeparatedList(n.Vars, n.SeparatorTkns, ",")
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtGoto(n ast.Vertex) {
@@ -2701,35 +2712,20 @@ func (p *Printer) printStmtReturn(n *ast.StmtReturn) {
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtStaticVar(n ast.Vertex) {
nn := n.(*ast.StmtStaticVar)
p.printFreeFloating(nn, token.Start)
func (p *Printer) printStmtStaticVar(n *ast.StmtStaticVar) {
p.Print(n.Var)
p.Print(nn.Var)
if nn.Expr != nil {
p.printFreeFloating(nn, token.Var)
io.WriteString(p.w, "=")
p.Print(nn.Expr)
if n.Expr != nil {
p.printToken(n.EqualTkn, "=")
p.Print(n.Expr)
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtStatic(n ast.Vertex) {
nn := n.(*ast.StmtStatic)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "static")
p.joinPrint(",", nn.Vars)
p.printFreeFloating(nn, token.VarList)
p.printFreeFloating(nn, token.SemiColon)
if n.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtStatic(n *ast.StmtStatic) {
p.printToken(n.StaticTkn, "static")
p.bufStart = " "
p.printSeparatedList(n.Vars, n.SeparatorTkns, ",")
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtStmtList(n *ast.StmtStmtList) {

View File

@@ -1036,7 +1036,8 @@ func TestParseAndPrintPhp5Function(t *testing.T) {
}
func TestParseAndPrintPhp5Global(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
global $a , $b ;`
actual := printPhp5(parsePhp5(src))
@@ -1208,7 +1209,8 @@ func TestParseAndPrintPhp5Return(t *testing.T) {
}
func TestParseAndPrintPhp5StaticVar(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
static $a , $b = ' ' ;
`
@@ -1363,7 +1365,8 @@ func TestParseAndPrintPhp5While(t *testing.T) {
// other
func TestParseAndPrintPhp5Parentheses(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
global $ { $b } ;
$b = (($a));
$b = ( ($a) );

View File

@@ -1154,7 +1154,8 @@ func TestParseAndPrintFunction(t *testing.T) {
}
func TestParseAndPrintGlobal(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
global $a , $b ;`
actual := print(parse(src))
@@ -1341,7 +1342,8 @@ func TestParseAndPrintReturn(t *testing.T) {
}
func TestParseAndPrintStaticVar(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
static $a , $b = foo ( ) ;
`

View File

@@ -3658,7 +3658,7 @@ func TestPrinterPrintStmtGlobal(t *testing.T) {
},
})
expected := `global$a,$b;`
expected := `global $a,$b;`
actual := o.String()
if expected != actual {
@@ -4029,7 +4029,7 @@ func TestPrinterPrintStatic(t *testing.T) {
},
})
expected := `static$a,$b;`
expected := `static $a,$b;`
actual := o.String()
if expected != actual {