[refactoring] update ast structure of "While" node

This commit is contained in:
Vadym Slizov 2020-09-04 10:33:47 +03:00
parent 6976388a82
commit 3b85f5e82b
21 changed files with 140 additions and 213 deletions

View File

@ -12762,7 +12762,7 @@ func TestStmtBreak(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltWhile{ &ast.StmtWhile{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -12771,6 +12771,7 @@ func TestStmtBreak(t *testing.T) {
EndPos: 34, EndPos: 34,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -901,20 +901,13 @@ unticked_statement:
} }
| T_WHILE parenthesis_expr while_statement | T_WHILE parenthesis_expr while_statement
{ {
switch n := $3.(type) { $3.(*ast.StmtWhile).WhileTkn = $1
case *ast.StmtWhile : $3.(*ast.StmtWhile).OpenParenthesisTkn = $2.(*ast.ParserBrackets).OpenBracketTkn
n.Cond = $2 $3.(*ast.StmtWhile).Cond = $2.(*ast.ParserBrackets).Child
case *ast.StmtAltWhile : $3.(*ast.StmtWhile).CloseParenthesisTkn = $2.(*ast.ParserBrackets).CloseBracketTkn
n.Cond = $2 $3.(*ast.StmtWhile).Node.Position = position.NewTokenNodePosition($1, $3)
}
$$ = $3 $$ = $3
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
} }
| T_DO statement T_WHILE parenthesis_expr ';' | T_DO statement T_WHILE parenthesis_expr ';'
{ {
@ -1914,29 +1907,30 @@ case_separator:
while_statement: while_statement:
statement statement
{ {
$$ = &ast.StmtWhile{ast.Node{}, nil, $1} $$ = &ast.StmtWhile{
Node: ast.Node{
// save position Position: position.NewNodePosition($1),
$$.GetNode().Position = position.NewNodePosition($1) },
Stmt: $1,
}
} }
| ':' inner_statement_list T_ENDWHILE ';' | ':' inner_statement_list T_ENDWHILE ';'
{ {
stmtList := &ast.StmtStmtList{ $$ = &ast.StmtWhile{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
Alt: true,
ColonTkn: $1,
Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: position.NewNodeListPosition($2), Position: position.NewNodeListPosition($2),
}, },
Stmts: $2, Stmts: $2,
},
EndWhileTkn: $3,
SemiColonTkn: $4,
} }
$$ = &ast.StmtAltWhile{ast.Node{}, nil, stmtList}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Cond, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.AltEnd, $4.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $4.SkippedTokens)
} }
; ;

View File

@ -3049,7 +3049,7 @@ func TestPhp5(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltWhile{ &ast.StmtWhile{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 57, StartLine: 57,
@ -3058,6 +3058,7 @@ func TestPhp5(t *testing.T) {
EndPos: 1046, EndPos: 1046,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{

View File

@ -14441,7 +14441,7 @@ func TestStmtBreak(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltWhile{ &ast.StmtWhile{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -14450,6 +14450,7 @@ func TestStmtBreak(t *testing.T) {
EndPos: 34, EndPos: 34,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -870,31 +870,13 @@ statement:
} }
| T_WHILE '(' expr ')' while_statement | T_WHILE '(' expr ')' while_statement
{ {
exprBrackets := &ast.ParserBrackets{ $5.(*ast.StmtWhile).WhileTkn = $1
Node: ast.Node{ $5.(*ast.StmtWhile).OpenParenthesisTkn = $2
Position: position.NewTokensPosition($2, $4), $5.(*ast.StmtWhile).Cond = $3
}, $5.(*ast.StmtWhile).CloseParenthesisTkn = $4
OpenBracketTkn: $2, $5.(*ast.StmtWhile).Node.Position = position.NewTokenNodePosition($1, $5)
Child: $3,
CloseBracketTkn: $4,
}
switch n := $5.(type) {
case *ast.StmtWhile :
n.Cond = exprBrackets
case *ast.StmtAltWhile :
n.Cond = exprBrackets
}
$$ = $5 $$ = $5
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $5)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.SkippedTokens)
} }
| T_DO statement T_WHILE '(' expr ')' ';' | T_DO statement T_WHILE '(' expr ')' ';'
{ {
@ -1740,29 +1722,30 @@ case_separator:
while_statement: while_statement:
statement statement
{ {
$$ = &ast.StmtWhile{ast.Node{}, nil, $1} $$ = &ast.StmtWhile{
Node: ast.Node{
// save position Position: position.NewNodePosition($1),
$$.GetNode().Position = position.NewNodePosition($1) },
Stmt: $1,
}
} }
| ':' inner_statement_list T_ENDWHILE ';' | ':' inner_statement_list T_ENDWHILE ';'
{ {
stmtList := &ast.StmtStmtList{ $$ = &ast.StmtWhile{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
Alt: true,
ColonTkn: $1,
Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: position.NewNodeListPosition($2), Position: position.NewNodeListPosition($2),
}, },
Stmts: $2, Stmts: $2,
},
EndWhileTkn: $3,
SemiColonTkn: $4,
} }
$$ = &ast.StmtAltWhile{ast.Node{}, nil, stmtList}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Cond, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.AltEnd, $4.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $4.SkippedTokens)
} }
; ;

View File

@ -3821,7 +3821,7 @@ func TestPhp7(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltWhile{ &ast.StmtWhile{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 69, StartLine: 69,
@ -3830,6 +3830,7 @@ func TestPhp7(t *testing.T) {
EndPos: 1290, EndPos: 1290,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{

View File

@ -30,7 +30,6 @@ type NodeVisitor interface {
StmtAltFor(n *StmtAltFor) StmtAltFor(n *StmtAltFor)
StmtAltForeach(n *StmtAltForeach) StmtAltForeach(n *StmtAltForeach)
StmtAltSwitch(n *StmtAltSwitch) StmtAltSwitch(n *StmtAltSwitch)
StmtAltWhile(n *StmtAltWhile)
StmtBreak(n *StmtBreak) StmtBreak(n *StmtBreak)
StmtCase(n *StmtCase) StmtCase(n *StmtCase)
StmtCaseList(n *StmtCaseList) StmtCaseList(n *StmtCaseList)

View File

@ -212,17 +212,6 @@ func (n *StmtAltSwitch) Accept(v NodeVisitor) {
v.StmtAltSwitch(n) v.StmtAltSwitch(n)
} }
// StmtAltWhile node
type StmtAltWhile struct {
Node
Cond Vertex
Stmt Vertex
}
func (n *StmtAltWhile) Accept(v NodeVisitor) {
v.StmtAltWhile(n)
}
// StmtBreak node // StmtBreak node
type StmtBreak struct { type StmtBreak struct {
Node Node
@ -839,8 +828,15 @@ func (n *StmtUseDeclaration) Accept(v NodeVisitor) {
// StmtWhile node // StmtWhile node
type StmtWhile struct { type StmtWhile struct {
Node Node
Alt bool
WhileTkn *token.Token
OpenParenthesisTkn *token.Token
Cond Vertex Cond Vertex
CloseParenthesisTkn *token.Token
ColonTkn *token.Token
Stmt Vertex Stmt Vertex
EndWhileTkn *token.Token
SemiColonTkn *token.Token
} }
func (n *StmtWhile) Accept(v NodeVisitor) { func (n *StmtWhile) Accept(v NodeVisitor) {

View File

@ -196,23 +196,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.CaseList) t.Traverse(nn.CaseList)
t.visitor.Leave("CaseList", true) t.visitor.Leave("CaseList", true)
} }
case *ast.StmtAltWhile:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Cond != nil {
t.visitor.Enter("Cond", true)
t.Traverse(nn.Cond)
t.visitor.Leave("Cond", true)
}
if nn.Stmt != nil {
t.visitor.Enter("Stmt", true)
t.Traverse(nn.Stmt)
t.visitor.Leave("Stmt", true)
}
case *ast.StmtBreak: case *ast.StmtBreak:
if nn == nil { if nn == nil {
return return

View File

@ -270,12 +270,6 @@ func (v *Dump) StmtAltSwitch(n *ast.StmtAltSwitch) {
v.printNode(n.GetNode()) v.printNode(n.GetNode())
} }
func (v *Dump) StmtAltWhile(n *ast.StmtAltWhile) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtAltWhile{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtBreak(n *ast.StmtBreak) { func (v *Dump) StmtBreak(n *ast.StmtBreak) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtBreak{\n") v.print("&ast.StmtBreak{\n")
@ -637,6 +631,11 @@ func (v *Dump) StmtWhile(n *ast.StmtWhile) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtWhile{\n") v.print("&ast.StmtWhile{\n")
v.printNode(n.GetNode()) v.printNode(n.GetNode())
if n.Alt {
v.printIndent(v.indent)
v.print("Alt: true,\n")
}
} }
func (v *Dump) ExprArray(n *ast.ExprArray) { func (v *Dump) ExprArray(n *ast.ExprArray) {

View File

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

View File

@ -113,3 +113,12 @@ func (v *FilterTokens) ParserBrackets(n *ast.ParserBrackets) {
n.OpenBracketTkn = nil n.OpenBracketTkn = nil
n.CloseBracketTkn = nil n.CloseBracketTkn = nil
} }
func (v *FilterTokens) StmtWhile(n *ast.StmtWhile) {
n.WhileTkn = nil
n.OpenParenthesisTkn = nil
n.CloseParenthesisTkn = nil
n.ColonTkn = nil
n.EndWhileTkn = nil
n.SemiColonTkn = nil
}

View File

@ -66,10 +66,6 @@ func (v *Null) StmtAltSwitch(_ *ast.StmtAltSwitch) {
// do nothing // do nothing
} }
func (v *Null) StmtAltWhile(_ *ast.StmtAltWhile) {
// do nothing
}
func (v *Null) StmtBreak(_ *ast.StmtBreak) { func (v *Null) StmtBreak(_ *ast.StmtBreak) {
// do nothing // do nothing
} }

View File

@ -303,8 +303,6 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
p.printStmtAltForeach(n) p.printStmtAltForeach(n)
case *ast.StmtAltSwitch: case *ast.StmtAltSwitch:
p.printStmtAltSwitch(n) p.printStmtAltSwitch(n)
case *ast.StmtAltWhile:
p.printStmtAltWhile(n)
case *ast.StmtBreak: case *ast.StmtBreak:
p.printStmtBreak(n) p.printStmtBreak(n)
case *ast.StmtCase: case *ast.StmtCase:
@ -1450,21 +1448,6 @@ func (p *PrettyPrinter) printStmtAltSwitch(n ast.Vertex) {
io.WriteString(p.w, "endswitch;") io.WriteString(p.w, "endswitch;")
} }
func (p *PrettyPrinter) printStmtAltWhile(n ast.Vertex) {
nn := n.(*ast.StmtAltWhile)
io.WriteString(p.w, "while (")
p.Print(nn.Cond)
io.WriteString(p.w, ") :\n")
s := nn.Stmt.(*ast.StmtStmtList)
p.printNodes(s.Stmts)
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endwhile;")
}
func (p *PrettyPrinter) printStmtBreak(n ast.Vertex) { func (p *PrettyPrinter) printStmtBreak(n ast.Vertex) {
nn := n.(*ast.StmtBreak) nn := n.(*ast.StmtBreak)
@ -2190,6 +2173,11 @@ func (p *PrettyPrinter) printStmtUseDeclaration(n ast.Vertex) {
func (p *PrettyPrinter) printStmtWhile(n ast.Vertex) { func (p *PrettyPrinter) printStmtWhile(n ast.Vertex) {
nn := n.(*ast.StmtWhile) nn := n.(*ast.StmtWhile)
if nn.Alt {
p.printStmtAltWhile(nn)
return
}
io.WriteString(p.w, "while (") io.WriteString(p.w, "while (")
p.Print(nn.Cond) p.Print(nn.Cond)
io.WriteString(p.w, ")") io.WriteString(p.w, ")")
@ -2209,3 +2197,18 @@ func (p *PrettyPrinter) printStmtWhile(n ast.Vertex) {
p.indentDepth-- p.indentDepth--
} }
} }
func (p *PrettyPrinter) printStmtAltWhile(n ast.Vertex) {
nn := n.(*ast.StmtWhile)
io.WriteString(p.w, "while (")
p.Print(nn.Cond)
io.WriteString(p.w, ") :\n")
s := nn.Stmt.(*ast.StmtStmtList)
p.printNodes(s.Stmts)
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endwhile;")
}

View File

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

View File

@ -365,8 +365,6 @@ func (p *Printer) printNode(n ast.Vertex) {
p.printStmtAltForeach(n) p.printStmtAltForeach(n)
case *ast.StmtAltSwitch: case *ast.StmtAltSwitch:
p.printStmtAltSwitch(n) p.printStmtAltSwitch(n)
case *ast.StmtAltWhile:
p.printStmtAltWhile(n)
case *ast.StmtBreak: case *ast.StmtBreak:
p.printStmtBreak(n) p.printStmtBreak(n)
case *ast.StmtCase: case *ast.StmtCase:
@ -2091,39 +2089,6 @@ func (p *Printer) printStmtAltSwitch(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtAltWhile(n ast.Vertex) {
nn := n.(*ast.StmtAltWhile)
p.printFreeFloating(nn, token.Start)
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)
io.WriteString(p.w, ":")
s := nn.Stmt.(*ast.StmtStmtList)
p.printNodes(s.Stmts)
p.printFreeFloating(nn, token.Stmts)
io.WriteString(p.w, "endwhile")
p.printFreeFloating(nn, token.AltEnd)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtBreak(n ast.Vertex) { func (p *Printer) printStmtBreak(n ast.Vertex) {
nn := n.(*ast.StmtBreak) nn := n.(*ast.StmtBreak)
p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Start)
@ -3169,25 +3134,35 @@ func (p *Printer) printStmtUseDeclaration(n *ast.StmtUseDeclaration) {
p.printToken(n.CommaTkn, "") p.printToken(n.CommaTkn, "")
} }
func (p *Printer) printStmtWhile(n ast.Vertex) { func (p *Printer) printStmtWhile(n *ast.StmtWhile) {
nn := n.(*ast.StmtWhile) if n.Alt {
p.printFreeFloating(nn, token.Start) p.printStmtAltWhile(n)
return
io.WriteString(p.w, "while")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, "(")
} }
p.Print(nn.Cond) p.printToken(n.WhileTkn, "while")
p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { p.Print(n.Stmt)
io.WriteString(p.w, ")") }
func (p *Printer) printStmtAltWhile(n *ast.StmtWhile) {
p.printToken(n.WhileTkn, "while")
p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
p.printToken(n.ColonTkn, ":")
if stmtList, ok := n.Stmt.(*ast.StmtStmtList); ok {
p.printNodes(stmtList.Stmts)
} else {
p.Print(n.Stmt)
} }
p.Print(nn.Stmt) p.printToken(n.EndWhileTkn, "endwhile")
p.printToken(n.SemiColonTkn, ";")
p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printParserAs(n ast.Vertex) { func (p *Printer) printParserAs(n ast.Vertex) {

View File

@ -820,7 +820,8 @@ func TestParseAndPrintPhp5AltSwitch(t *testing.T) {
} }
func TestParseAndPrintPhp5AltWhile(t *testing.T) { func TestParseAndPrintPhp5AltWhile(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
while ( $a ) : while ( $a ) :
// do nothing // do nothing
@ -1338,7 +1339,8 @@ func TestParseAndPrintPhp5UseList(t *testing.T) {
} }
func TestParseAndPrintPhp5While(t *testing.T) { func TestParseAndPrintPhp5While(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
while ( $a ) echo '' ; while ( $a ) echo '' ;
while ( $a ) { } while ( $a ) { }
while ( $a ) ; while ( $a ) ;

View File

@ -933,7 +933,8 @@ func TestParseAndPrintAltSwitch(t *testing.T) {
} }
func TestParseAndPrintAltWhile(t *testing.T) { func TestParseAndPrintAltWhile(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
while ( $a ) : while ( $a ) :
// do nothing // do nothing
@ -1471,7 +1472,8 @@ func TestParseAndPrintUseList(t *testing.T) {
} }
func TestParseAndPrintWhile(t *testing.T) { func TestParseAndPrintWhile(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
while ( $a ) echo '' ; while ( $a ) echo '' ;
while ( $a ) { } while ( $a ) { }
while ( $a ) ; while ( $a ) ;

View File

@ -2815,7 +2815,8 @@ func TestPrinterPrintAltWhile(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o) p := printer.NewPrinter(o)
p.Print(&ast.StmtAltWhile{ p.Print(&ast.StmtWhile{
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")}, VarName: &ast.Identifier{Value: []byte("$a")},
}, },