[refactoring] update ast structure of "Do" node

This commit is contained in:
Vadym Slizov
2020-09-04 10:53:07 +03:00
parent 3b85f5e82b
commit c274c4f92f
12 changed files with 900 additions and 930 deletions

View File

@@ -378,8 +378,13 @@ func (n *StmtDefault) Accept(v NodeVisitor) {
// StmtDo node
type StmtDo struct {
Node
Stmt Vertex
Cond Vertex
DoTkn *token.Token
Stmt Vertex
WhileTkn *token.Token
OpenParenthesisTkn *token.Token
Cond Vertex
CloseParenthesisTkn *token.Token
SemiColonTkn *token.Token
}
func (n *StmtDo) Accept(v NodeVisitor) {

View File

@@ -13,16 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
return true
}
func (v *FilterParserNodes) StmtDo(n *ast.StmtDo) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
n.Cond = nn.Child
} else {
break
}
}
}
func (v *FilterParserNodes) StmtSwitch(n *ast.StmtSwitch) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {

View File

@@ -122,3 +122,11 @@ func (v *FilterTokens) StmtWhile(n *ast.StmtWhile) {
n.EndWhileTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtDo(n *ast.StmtDo) {
n.DoTkn = nil
n.WhileTkn = nil
n.OpenParenthesisTkn = nil
n.CloseParenthesisTkn = nil
n.SemiColonTkn = nil
}

View File

@@ -2343,7 +2343,7 @@ func TestPrintAltWhile(t *testing.T) {
p.Print(&ast.StmtNamespace{
Stmts: []ast.Vertex{
&ast.StmtWhile{
Alt: true,
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{

View File

@@ -2361,42 +2361,17 @@ func (p *Printer) printStmtDefault(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtDo(n ast.Vertex) {
nn := n.(*ast.StmtDo)
p.printFreeFloating(nn, token.Start)
func (p *Printer) printStmtDo(n *ast.StmtDo) {
p.printToken(n.DoTkn, "do")
p.bufStart = " "
io.WriteString(p.w, "do")
p.Print(n.Stmt)
if _, ok := nn.Stmt.(*ast.StmtStmtList); !ok {
if nn.Stmt.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
}
p.Print(nn.Stmt)
p.printFreeFloating(nn, token.Stmts)
io.WriteString(p.w, "while")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, "(")
}
p.Print(nn.Cond)
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, ")")
}
p.printFreeFloating(nn, token.Cond)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
p.printToken(n.WhileTkn, "while")
p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtEcho(n ast.Vertex) {

View File

@@ -945,7 +945,8 @@ func TestParseAndPrintPhp5Declare(t *testing.T) {
}
func TestParseAndPrintPhp5DoWhile(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
do {
;
} while ( $a ) ;

View File

@@ -1061,7 +1061,8 @@ func TestParseAndPrintDeclare(t *testing.T) {
}
func TestParseAndPrintDoWhile(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
do {
;
} while ( $a ) ;

View File

@@ -3331,7 +3331,7 @@ func TestPrinterPrintStmtDo_StmtList(t *testing.T) {
},
})
expected := `do{$a;}while(1);`
expected := `do {$a;}while(1);`
actual := o.String()
if expected != actual {