[refactoring] update ast structure of "For" node
This commit is contained in:
parent
c274c4f92f
commit
0e73cd8852
@ -6141,7 +6141,7 @@ func TestStmtFor_Alt(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{
|
||||||
&ast.StmtAltFor{
|
&ast.StmtFor{
|
||||||
Node: ast.Node{
|
Node: ast.Node{
|
||||||
Position: &position.Position{
|
Position: &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
@ -6150,6 +6150,7 @@ func TestStmtFor_Alt(t *testing.T) {
|
|||||||
EndPos: 33,
|
EndPos: 33,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Alt: true,
|
||||||
Cond: []ast.Vertex{
|
Cond: []ast.Vertex{
|
||||||
&ast.ExprBinarySmaller{
|
&ast.ExprBinarySmaller{
|
||||||
Node: ast.Node{
|
Node: ast.Node{
|
||||||
|
BIN
internal/php5/php5.go
generated
BIN
internal/php5/php5.go
generated
Binary file not shown.
@ -926,28 +926,17 @@ unticked_statement:
|
|||||||
}
|
}
|
||||||
| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
|
| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
|
||||||
{
|
{
|
||||||
switch n := $9.(type) {
|
$9.(*ast.StmtFor).ForTkn = $1
|
||||||
case *ast.StmtFor :
|
$9.(*ast.StmtFor).OpenParenthesisTkn = $2
|
||||||
n.Init = $3
|
$9.(*ast.StmtFor).Init = $3
|
||||||
n.Cond = $5
|
$9.(*ast.StmtFor).InitSemiColonTkn = $4
|
||||||
n.Loop = $7
|
$9.(*ast.StmtFor).Cond = $5
|
||||||
case *ast.StmtAltFor :
|
$9.(*ast.StmtFor).CondSemiColonTkn = $6
|
||||||
n.Init = $3
|
$9.(*ast.StmtFor).Loop = $7
|
||||||
n.Cond = $5
|
$9.(*ast.StmtFor).CloseParenthesisTkn = $8
|
||||||
n.Loop = $7
|
$9.(*ast.StmtFor).Node.Position = position.NewTokenNodePosition($1, $9)
|
||||||
}
|
|
||||||
|
|
||||||
$$ = $9
|
$$ = $9
|
||||||
|
|
||||||
// save position
|
|
||||||
$$.GetNode().Position = position.NewTokenNodePosition($1, $9)
|
|
||||||
|
|
||||||
// save comments
|
|
||||||
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
|
|
||||||
yylex.(*Parser).setFreeFloating($$, token.For, $2.SkippedTokens)
|
|
||||||
yylex.(*Parser).setFreeFloating($$, token.InitExpr, $4.SkippedTokens)
|
|
||||||
yylex.(*Parser).setFreeFloating($$, token.CondExpr, $6.SkippedTokens)
|
|
||||||
yylex.(*Parser).setFreeFloating($$, token.IncExpr, $8.SkippedTokens)
|
|
||||||
}
|
}
|
||||||
| T_SWITCH parenthesis_expr switch_case_list
|
| T_SWITCH parenthesis_expr switch_case_list
|
||||||
{
|
{
|
||||||
@ -1668,29 +1657,30 @@ foreach_variable:
|
|||||||
for_statement:
|
for_statement:
|
||||||
statement
|
statement
|
||||||
{
|
{
|
||||||
$$ = &ast.StmtFor{ast.Node{}, nil, nil, nil, $1}
|
$$ = &ast.StmtFor{
|
||||||
|
Node: ast.Node{
|
||||||
// save position
|
Position: position.NewNodePosition($1),
|
||||||
$$.GetNode().Position = position.NewNodePosition($1)
|
},
|
||||||
|
Stmt: $1,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
| ':' inner_statement_list T_ENDFOR ';'
|
| ':' inner_statement_list T_ENDFOR ';'
|
||||||
{
|
{
|
||||||
stmtList := &ast.StmtStmtList{
|
$$ = &ast.StmtFor{
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
EndForTkn: $3,
|
||||||
|
SemiColonTkn: $4,
|
||||||
}
|
}
|
||||||
$$ = &ast.StmtAltFor{ast.Node{}, nil, nil, 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)
|
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -4490,7 +4490,7 @@ func TestPhp5(t *testing.T) {
|
|||||||
Stmts: []ast.Vertex{},
|
Stmts: []ast.Vertex{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&ast.StmtAltFor{
|
&ast.StmtFor{
|
||||||
Node: ast.Node{
|
Node: ast.Node{
|
||||||
Position: &position.Position{
|
Position: &position.Position{
|
||||||
StartLine: 78,
|
StartLine: 78,
|
||||||
@ -4499,6 +4499,7 @@ func TestPhp5(t *testing.T) {
|
|||||||
EndPos: 1685,
|
EndPos: 1685,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Alt: true,
|
||||||
Cond: []ast.Vertex{
|
Cond: []ast.Vertex{
|
||||||
&ast.ExprBinarySmaller{
|
&ast.ExprBinarySmaller{
|
||||||
Node: ast.Node{
|
Node: ast.Node{
|
||||||
|
@ -6738,7 +6738,7 @@ func TestStmtFor_Alt(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{
|
||||||
&ast.StmtAltFor{
|
&ast.StmtFor{
|
||||||
Node: ast.Node{
|
Node: ast.Node{
|
||||||
Position: &position.Position{
|
Position: &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
@ -6747,6 +6747,7 @@ func TestStmtFor_Alt(t *testing.T) {
|
|||||||
EndPos: 33,
|
EndPos: 33,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Alt: true,
|
||||||
Cond: []ast.Vertex{
|
Cond: []ast.Vertex{
|
||||||
&ast.ExprBinarySmaller{
|
&ast.ExprBinarySmaller{
|
||||||
Node: ast.Node{
|
Node: ast.Node{
|
||||||
|
BIN
internal/php7/php7.go
generated
BIN
internal/php7/php7.go
generated
Binary file not shown.
@ -895,28 +895,17 @@ statement:
|
|||||||
}
|
}
|
||||||
| T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement
|
| T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement
|
||||||
{
|
{
|
||||||
switch n := $9.(type) {
|
$9.(*ast.StmtFor).ForTkn = $1
|
||||||
case *ast.StmtFor :
|
$9.(*ast.StmtFor).OpenParenthesisTkn = $2
|
||||||
n.Init = $3
|
$9.(*ast.StmtFor).Init = $3
|
||||||
n.Cond = $5
|
$9.(*ast.StmtFor).InitSemiColonTkn = $4
|
||||||
n.Loop = $7
|
$9.(*ast.StmtFor).Cond = $5
|
||||||
case *ast.StmtAltFor :
|
$9.(*ast.StmtFor).CondSemiColonTkn = $6
|
||||||
n.Init = $3
|
$9.(*ast.StmtFor).Loop = $7
|
||||||
n.Cond = $5
|
$9.(*ast.StmtFor).CloseParenthesisTkn = $8
|
||||||
n.Loop = $7
|
$9.(*ast.StmtFor).Node.Position = position.NewTokenNodePosition($1, $9)
|
||||||
}
|
|
||||||
|
|
||||||
$$ = $9
|
$$ = $9
|
||||||
|
|
||||||
// save position
|
|
||||||
$$.GetNode().Position = position.NewTokenNodePosition($1, $9)
|
|
||||||
|
|
||||||
// save comments
|
|
||||||
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
|
|
||||||
yylex.(*Parser).setFreeFloating($$, token.For, $2.SkippedTokens)
|
|
||||||
yylex.(*Parser).setFreeFloating($$, token.InitExpr, $4.SkippedTokens)
|
|
||||||
yylex.(*Parser).setFreeFloating($$, token.CondExpr, $6.SkippedTokens)
|
|
||||||
yylex.(*Parser).setFreeFloating($$, token.IncExpr, $8.SkippedTokens)
|
|
||||||
}
|
}
|
||||||
| T_SWITCH '(' expr ')' switch_case_list
|
| T_SWITCH '(' expr ')' switch_case_list
|
||||||
{
|
{
|
||||||
@ -1522,29 +1511,30 @@ foreach_variable:
|
|||||||
for_statement:
|
for_statement:
|
||||||
statement
|
statement
|
||||||
{
|
{
|
||||||
$$ = &ast.StmtFor{ast.Node{}, nil, nil, nil, $1}
|
$$ = &ast.StmtFor{
|
||||||
|
Node: ast.Node{
|
||||||
// save position
|
Position: position.NewNodePosition($1),
|
||||||
$$.GetNode().Position = position.NewNodePosition($1)
|
},
|
||||||
|
Stmt: $1,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
| ':' inner_statement_list T_ENDFOR ';'
|
| ':' inner_statement_list T_ENDFOR ';'
|
||||||
{
|
{
|
||||||
stmtList := &ast.StmtStmtList{
|
$$ = &ast.StmtFor{
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
EndForTkn: $3,
|
||||||
|
SemiColonTkn: $4,
|
||||||
}
|
}
|
||||||
$$ = &ast.StmtAltFor{ast.Node{}, nil, nil, 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)
|
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -5397,7 +5397,7 @@ func TestPhp7(t *testing.T) {
|
|||||||
Stmts: []ast.Vertex{},
|
Stmts: []ast.Vertex{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&ast.StmtAltFor{
|
&ast.StmtFor{
|
||||||
Node: ast.Node{
|
Node: ast.Node{
|
||||||
Position: &position.Position{
|
Position: &position.Position{
|
||||||
StartLine: 92,
|
StartLine: 92,
|
||||||
@ -5406,6 +5406,7 @@ func TestPhp7(t *testing.T) {
|
|||||||
EndPos: 1959,
|
EndPos: 1959,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Alt: true,
|
||||||
Cond: []ast.Vertex{
|
Cond: []ast.Vertex{
|
||||||
&ast.ExprBinarySmaller{
|
&ast.ExprBinarySmaller{
|
||||||
Node: ast.Node{
|
Node: ast.Node{
|
||||||
|
@ -27,7 +27,6 @@ type NodeVisitor interface {
|
|||||||
ArgumentList(n *ArgumentList)
|
ArgumentList(n *ArgumentList)
|
||||||
Argument(n *Argument)
|
Argument(n *Argument)
|
||||||
|
|
||||||
StmtAltFor(n *StmtAltFor)
|
|
||||||
StmtAltForeach(n *StmtAltForeach)
|
StmtAltForeach(n *StmtAltForeach)
|
||||||
StmtAltSwitch(n *StmtAltSwitch)
|
StmtAltSwitch(n *StmtAltSwitch)
|
||||||
StmtBreak(n *StmtBreak)
|
StmtBreak(n *StmtBreak)
|
||||||
|
@ -175,19 +175,6 @@ func (n *ScalarString) Accept(v NodeVisitor) {
|
|||||||
v.ScalarString(n)
|
v.ScalarString(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StmtAltFor node
|
|
||||||
type StmtAltFor struct {
|
|
||||||
Node
|
|
||||||
Init []Vertex
|
|
||||||
Cond []Vertex
|
|
||||||
Loop []Vertex
|
|
||||||
Stmt Vertex
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *StmtAltFor) Accept(v NodeVisitor) {
|
|
||||||
v.StmtAltFor(n)
|
|
||||||
}
|
|
||||||
|
|
||||||
// StmtAltForeach node
|
// StmtAltForeach node
|
||||||
type StmtAltForeach struct {
|
type StmtAltForeach struct {
|
||||||
Node
|
Node
|
||||||
@ -453,10 +440,19 @@ func (n *StmtFinally) Accept(v NodeVisitor) {
|
|||||||
// StmtFor node
|
// StmtFor node
|
||||||
type StmtFor struct {
|
type StmtFor struct {
|
||||||
Node
|
Node
|
||||||
|
Alt bool
|
||||||
|
ForTkn *token.Token
|
||||||
|
OpenParenthesisTkn *token.Token
|
||||||
Init []Vertex
|
Init []Vertex
|
||||||
|
InitSemiColonTkn *token.Token
|
||||||
Cond []Vertex
|
Cond []Vertex
|
||||||
|
CondSemiColonTkn *token.Token
|
||||||
Loop []Vertex
|
Loop []Vertex
|
||||||
|
CloseParenthesisTkn *token.Token
|
||||||
|
ColonTkn *token.Token
|
||||||
Stmt Vertex
|
Stmt Vertex
|
||||||
|
EndForTkn *token.Token
|
||||||
|
SemiColonTkn *token.Token
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *StmtFor) Accept(v NodeVisitor) {
|
func (n *StmtFor) Accept(v NodeVisitor) {
|
||||||
|
@ -119,39 +119,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
|
|||||||
t.Traverse(nn.Expr)
|
t.Traverse(nn.Expr)
|
||||||
t.visitor.Leave("Expr", true)
|
t.visitor.Leave("Expr", true)
|
||||||
}
|
}
|
||||||
case *ast.StmtAltFor:
|
|
||||||
if nn == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !t.visitor.EnterNode(nn) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if nn.Init != nil {
|
|
||||||
t.visitor.Enter("Init", false)
|
|
||||||
for _, c := range nn.Init {
|
|
||||||
t.Traverse(c)
|
|
||||||
}
|
|
||||||
t.visitor.Leave("Init", false)
|
|
||||||
}
|
|
||||||
if nn.Cond != nil {
|
|
||||||
t.visitor.Enter("Cond", false)
|
|
||||||
for _, c := range nn.Cond {
|
|
||||||
t.Traverse(c)
|
|
||||||
}
|
|
||||||
t.visitor.Leave("Cond", false)
|
|
||||||
}
|
|
||||||
if nn.Loop != nil {
|
|
||||||
t.visitor.Enter("Loop", false)
|
|
||||||
for _, c := range nn.Loop {
|
|
||||||
t.Traverse(c)
|
|
||||||
}
|
|
||||||
t.visitor.Leave("Loop", false)
|
|
||||||
}
|
|
||||||
if nn.Stmt != nil {
|
|
||||||
t.visitor.Enter("Stmt", true)
|
|
||||||
t.Traverse(nn.Stmt)
|
|
||||||
t.visitor.Leave("Stmt", true)
|
|
||||||
}
|
|
||||||
case *ast.StmtAltForeach:
|
case *ast.StmtAltForeach:
|
||||||
if nn == nil {
|
if nn == nil {
|
||||||
return
|
return
|
||||||
|
@ -252,12 +252,6 @@ func (v *Dump) Argument(n *ast.Argument) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Dump) StmtAltFor(n *ast.StmtAltFor) {
|
|
||||||
v.printIndentIfNotSingle(v.indent - 1)
|
|
||||||
v.print("&ast.StmtAltFor{\n")
|
|
||||||
v.printNode(n.GetNode())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *Dump) StmtAltForeach(n *ast.StmtAltForeach) {
|
func (v *Dump) StmtAltForeach(n *ast.StmtAltForeach) {
|
||||||
v.printIndentIfNotSingle(v.indent - 1)
|
v.printIndentIfNotSingle(v.indent - 1)
|
||||||
v.print("&ast.StmtAltForeach{\n")
|
v.print("&ast.StmtAltForeach{\n")
|
||||||
@ -414,6 +408,11 @@ func (v *Dump) StmtFor(n *ast.StmtFor) {
|
|||||||
v.printIndentIfNotSingle(v.indent - 1)
|
v.printIndentIfNotSingle(v.indent - 1)
|
||||||
v.print("&ast.StmtFor{\n")
|
v.print("&ast.StmtFor{\n")
|
||||||
v.printNode(n.GetNode())
|
v.printNode(n.GetNode())
|
||||||
|
|
||||||
|
if n.Alt {
|
||||||
|
v.printIndent(v.indent)
|
||||||
|
v.print("Alt: true,\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Dump) StmtForeach(n *ast.StmtForeach) {
|
func (v *Dump) StmtForeach(n *ast.StmtForeach) {
|
||||||
|
@ -209,44 +209,6 @@ func (v *FilterParserNodes) ExprVariable(n *ast.ExprVariable) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *FilterParserNodes) StmtFor(n *ast.StmtFor) {
|
|
||||||
for k, v := range n.Init {
|
|
||||||
for {
|
|
||||||
if nn, ok := v.(*ast.ParserBrackets); ok {
|
|
||||||
v = nn.Child
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n.Init[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range n.Cond {
|
|
||||||
for {
|
|
||||||
if nn, ok := v.(*ast.ParserBrackets); ok {
|
|
||||||
v = nn.Child
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n.Cond[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range n.Loop {
|
|
||||||
for {
|
|
||||||
if nn, ok := v.(*ast.ParserBrackets); ok {
|
|
||||||
v = nn.Child
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n.Loop[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *FilterParserNodes) ExprAssign(n *ast.ExprAssign) {
|
func (v *FilterParserNodes) ExprAssign(n *ast.ExprAssign) {
|
||||||
for {
|
for {
|
||||||
if nn, ok := n.Expr.(*ast.ParserBrackets); ok {
|
if nn, ok := n.Expr.(*ast.ParserBrackets); ok {
|
||||||
|
@ -130,3 +130,14 @@ func (v *FilterTokens) StmtDo(n *ast.StmtDo) {
|
|||||||
n.CloseParenthesisTkn = nil
|
n.CloseParenthesisTkn = nil
|
||||||
n.SemiColonTkn = nil
|
n.SemiColonTkn = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *FilterTokens) StmtFor(n *ast.StmtFor) {
|
||||||
|
n.ForTkn = nil
|
||||||
|
n.OpenParenthesisTkn = nil
|
||||||
|
n.InitSemiColonTkn = nil
|
||||||
|
n.CondSemiColonTkn = nil
|
||||||
|
n.CloseParenthesisTkn = nil
|
||||||
|
n.ColonTkn = nil
|
||||||
|
n.EndForTkn = nil
|
||||||
|
n.SemiColonTkn = nil
|
||||||
|
}
|
||||||
|
@ -54,10 +54,6 @@ func (v *Null) Argument(_ *ast.Argument) {
|
|||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Null) StmtAltFor(_ *ast.StmtAltFor) {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *Null) StmtAltForeach(_ *ast.StmtAltForeach) {
|
func (v *Null) StmtAltForeach(_ *ast.StmtAltForeach) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
@ -297,8 +297,6 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
|
|||||||
|
|
||||||
// stmt
|
// stmt
|
||||||
|
|
||||||
case *ast.StmtAltFor:
|
|
||||||
p.printStmtAltFor(n)
|
|
||||||
case *ast.StmtAltForeach:
|
case *ast.StmtAltForeach:
|
||||||
p.printStmtAltForeach(n)
|
p.printStmtAltForeach(n)
|
||||||
case *ast.StmtAltSwitch:
|
case *ast.StmtAltSwitch:
|
||||||
@ -1363,25 +1361,6 @@ func (p *PrettyPrinter) printStmtAltElse(n ast.Vertex) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PrettyPrinter) printStmtAltFor(n ast.Vertex) {
|
|
||||||
nn := n.(*ast.StmtAltFor)
|
|
||||||
|
|
||||||
io.WriteString(p.w, "for (")
|
|
||||||
p.joinPrint(", ", nn.Init)
|
|
||||||
io.WriteString(p.w, "; ")
|
|
||||||
p.joinPrint(", ", nn.Cond)
|
|
||||||
io.WriteString(p.w, "; ")
|
|
||||||
p.joinPrint(", ", nn.Loop)
|
|
||||||
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, "endfor;")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PrettyPrinter) printStmtAltForeach(n ast.Vertex) {
|
func (p *PrettyPrinter) printStmtAltForeach(n ast.Vertex) {
|
||||||
nn := n.(*ast.StmtAltForeach)
|
nn := n.(*ast.StmtAltForeach)
|
||||||
|
|
||||||
@ -1736,6 +1715,11 @@ func (p *PrettyPrinter) printStmtFinally(n ast.Vertex) {
|
|||||||
func (p *PrettyPrinter) printStmtFor(n ast.Vertex) {
|
func (p *PrettyPrinter) printStmtFor(n ast.Vertex) {
|
||||||
nn := n.(*ast.StmtFor)
|
nn := n.(*ast.StmtFor)
|
||||||
|
|
||||||
|
if nn.Alt {
|
||||||
|
p.printStmtAltFor(nn)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
io.WriteString(p.w, "for (")
|
io.WriteString(p.w, "for (")
|
||||||
p.joinPrint(", ", nn.Init)
|
p.joinPrint(", ", nn.Init)
|
||||||
io.WriteString(p.w, "; ")
|
io.WriteString(p.w, "; ")
|
||||||
@ -1760,6 +1744,25 @@ func (p *PrettyPrinter) printStmtFor(n ast.Vertex) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *PrettyPrinter) printStmtAltFor(n ast.Vertex) {
|
||||||
|
nn := n.(*ast.StmtFor)
|
||||||
|
|
||||||
|
io.WriteString(p.w, "for (")
|
||||||
|
p.joinPrint(", ", nn.Init)
|
||||||
|
io.WriteString(p.w, "; ")
|
||||||
|
p.joinPrint(", ", nn.Cond)
|
||||||
|
io.WriteString(p.w, "; ")
|
||||||
|
p.joinPrint(", ", nn.Loop)
|
||||||
|
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, "endfor;")
|
||||||
|
}
|
||||||
|
|
||||||
func (p *PrettyPrinter) printStmtForeach(n ast.Vertex) {
|
func (p *PrettyPrinter) printStmtForeach(n ast.Vertex) {
|
||||||
nn := n.(*ast.StmtForeach)
|
nn := n.(*ast.StmtForeach)
|
||||||
|
|
||||||
|
@ -2172,7 +2172,8 @@ func TestPrintAltFor(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.StmtAltFor{
|
&ast.StmtFor{
|
||||||
|
Alt: true,
|
||||||
Init: []ast.Vertex{
|
Init: []ast.Vertex{
|
||||||
&ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
|
&ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
|
||||||
},
|
},
|
||||||
|
@ -359,8 +359,6 @@ func (p *Printer) printNode(n ast.Vertex) {
|
|||||||
|
|
||||||
// stmt
|
// stmt
|
||||||
|
|
||||||
case *ast.StmtAltFor:
|
|
||||||
p.printStmtAltFor(n)
|
|
||||||
case *ast.StmtAltForeach:
|
case *ast.StmtAltForeach:
|
||||||
p.printStmtAltForeach(n)
|
p.printStmtAltForeach(n)
|
||||||
case *ast.StmtAltSwitch:
|
case *ast.StmtAltSwitch:
|
||||||
@ -1973,39 +1971,6 @@ func (p *Printer) printExprYield(n ast.Vertex) {
|
|||||||
|
|
||||||
// smtm
|
// smtm
|
||||||
|
|
||||||
func (p *Printer) printStmtAltFor(n ast.Vertex) {
|
|
||||||
nn := n.(*ast.StmtAltFor)
|
|
||||||
p.printFreeFloating(nn, token.Start)
|
|
||||||
|
|
||||||
io.WriteString(p.w, "for")
|
|
||||||
p.printFreeFloating(nn, token.For)
|
|
||||||
io.WriteString(p.w, "(")
|
|
||||||
p.joinPrint(",", nn.Init)
|
|
||||||
p.printFreeFloating(nn, token.InitExpr)
|
|
||||||
io.WriteString(p.w, ";")
|
|
||||||
p.joinPrint(",", nn.Cond)
|
|
||||||
p.printFreeFloating(nn, token.CondExpr)
|
|
||||||
io.WriteString(p.w, ";")
|
|
||||||
p.joinPrint(",", nn.Loop)
|
|
||||||
p.printFreeFloating(nn, token.IncExpr)
|
|
||||||
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, "endfor")
|
|
||||||
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) printStmtAltForeach(n ast.Vertex) {
|
func (p *Printer) printStmtAltForeach(n ast.Vertex) {
|
||||||
nn := n.(*ast.StmtAltForeach)
|
nn := n.(*ast.StmtAltForeach)
|
||||||
p.printFreeFloating(nn, token.Start)
|
p.printFreeFloating(nn, token.Start)
|
||||||
@ -2477,26 +2442,43 @@ func (p *Printer) printStmtFinally(n ast.Vertex) {
|
|||||||
p.printFreeFloating(nn, token.End)
|
p.printFreeFloating(nn, token.End)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Printer) printStmtFor(n ast.Vertex) {
|
func (p *Printer) printStmtFor(n *ast.StmtFor) {
|
||||||
nn := n.(*ast.StmtFor)
|
if n.Alt {
|
||||||
p.printFreeFloating(nn, token.Start)
|
p.printStmtAltFor(n)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
io.WriteString(p.w, "for")
|
p.printToken(n.ForTkn, "for")
|
||||||
p.printFreeFloating(nn, token.For)
|
p.printToken(n.OpenParenthesisTkn, "(")
|
||||||
io.WriteString(p.w, "(")
|
p.joinPrint(",", n.Init)
|
||||||
p.joinPrint(",", nn.Init)
|
p.printToken(n.InitSemiColonTkn, ";")
|
||||||
p.printFreeFloating(nn, token.InitExpr)
|
p.joinPrint(",", n.Cond)
|
||||||
io.WriteString(p.w, ";")
|
p.printToken(n.CondSemiColonTkn, ";")
|
||||||
p.joinPrint(",", nn.Cond)
|
p.joinPrint(",", n.Loop)
|
||||||
p.printFreeFloating(nn, token.CondExpr)
|
p.printToken(n.CloseParenthesisTkn, ")")
|
||||||
io.WriteString(p.w, ";")
|
|
||||||
p.joinPrint(",", nn.Loop)
|
|
||||||
p.printFreeFloating(nn, token.IncExpr)
|
|
||||||
io.WriteString(p.w, ")")
|
|
||||||
|
|
||||||
p.Print(nn.Stmt)
|
p.Print(n.Stmt)
|
||||||
|
}
|
||||||
|
|
||||||
p.printFreeFloating(nn, token.End)
|
func (p *Printer) printStmtAltFor(n *ast.StmtFor) {
|
||||||
|
p.printToken(n.ForTkn, "for")
|
||||||
|
p.printToken(n.OpenParenthesisTkn, "(")
|
||||||
|
p.joinPrint(",", n.Init)
|
||||||
|
p.printToken(n.InitSemiColonTkn, ";")
|
||||||
|
p.joinPrint(",", n.Cond)
|
||||||
|
p.printToken(n.CondSemiColonTkn, ";")
|
||||||
|
p.joinPrint(",", n.Loop)
|
||||||
|
p.printToken(n.CloseParenthesisTkn, ")")
|
||||||
|
p.printToken(n.ColonTkn, ":")
|
||||||
|
|
||||||
|
if stmtList, ok := n.Stmt.(*ast.StmtStmtList); ok {
|
||||||
|
p.printNodes(stmtList.Stmts)
|
||||||
|
} else {
|
||||||
|
p.printNode(n.Stmt)
|
||||||
|
}
|
||||||
|
|
||||||
|
p.printToken(n.EndForTkn, "endfor")
|
||||||
|
p.printToken(n.SemiColonTkn, ";")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Printer) printStmtForeach(n ast.Vertex) {
|
func (p *Printer) printStmtForeach(n ast.Vertex) {
|
||||||
|
@ -762,7 +762,8 @@ func TestParseAndPrintPhp5AltIf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseAndPrintPhp5AltFor(t *testing.T) {
|
func TestParseAndPrintPhp5AltFor(t *testing.T) {
|
||||||
src := `<?php
|
// TODO: remove ; after <?php
|
||||||
|
src := `<?php ;
|
||||||
for ( $a ; $b ; $c ) :
|
for ( $a ; $b ; $c ) :
|
||||||
endfor ;
|
endfor ;
|
||||||
|
|
||||||
@ -988,7 +989,8 @@ func TestParseAndPrintPhp5IfExpression(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseAndPrintPhp5For(t *testing.T) {
|
func TestParseAndPrintPhp5For(t *testing.T) {
|
||||||
src := `<?php
|
// TODO: remove ; after <?php
|
||||||
|
src := `<?php ;
|
||||||
for ( $i = 0 ; $i < 3 ; $i ++ )
|
for ( $i = 0 ; $i < 3 ; $i ++ )
|
||||||
echo $i . PHP_EOL;
|
echo $i . PHP_EOL;
|
||||||
|
|
||||||
|
@ -875,7 +875,8 @@ func TestParseAndPrintAltIf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseAndPrintAltFor(t *testing.T) {
|
func TestParseAndPrintAltFor(t *testing.T) {
|
||||||
src := `<?php
|
// TODO: remove ; after <?php
|
||||||
|
src := `<?php ;
|
||||||
for ( $a ; $b ; $c ) :
|
for ( $a ; $b ; $c ) :
|
||||||
endfor ;
|
endfor ;
|
||||||
|
|
||||||
@ -1106,7 +1107,8 @@ func TestParseAndPrintIfExpression(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseAndPrintFor(t *testing.T) {
|
func TestParseAndPrintFor(t *testing.T) {
|
||||||
src := `<?php
|
// TODO: remove ; after <?php
|
||||||
|
src := `<?php ;
|
||||||
for ( $i = 0 ; $i < 3 ; $i ++ )
|
for ( $i = 0 ; $i < 3 ; $i ++ )
|
||||||
echo $i . PHP_EOL;
|
echo $i . PHP_EOL;
|
||||||
|
|
||||||
|
@ -2651,7 +2651,8 @@ func TestPrinterPrintAltFor(t *testing.T) {
|
|||||||
o := bytes.NewBufferString("")
|
o := bytes.NewBufferString("")
|
||||||
|
|
||||||
p := printer.NewPrinter(o)
|
p := printer.NewPrinter(o)
|
||||||
p.Print(&ast.StmtAltFor{
|
p.Print(&ast.StmtFor{
|
||||||
|
Alt: true,
|
||||||
Init: []ast.Vertex{
|
Init: []ast.Vertex{
|
||||||
&ast.ExprVariable{
|
&ast.ExprVariable{
|
||||||
VarName: &ast.Identifier{Value: []byte("$a")},
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
||||||
|
Loading…
Reference in New Issue
Block a user