[refactoring] update ast structure of "Break", "Continue" nodes

This commit is contained in:
Vadym Slizov 2020-09-06 13:02:13 +03:00
parent f6cb2bff4d
commit e817f8dd8d
9 changed files with 82 additions and 93 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -950,51 +950,45 @@ unticked_statement:
} }
| T_BREAK ';' | T_BREAK ';'
{ {
$$ = &ast.StmtBreak{ast.Node{}, nil} $$ = &ast.StmtBreak{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $2),
$$.GetNode().Position = position.NewTokensPosition($1, $2) },
BreakTkn: $1,
// save comments SemiColonTkn: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
yylex.(*Parser).setFreeFloating($$, token.Expr, $2.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $2.SkippedTokens)
} }
| T_BREAK expr ';' | T_BREAK expr ';'
{ {
$$ = &ast.StmtBreak{ast.Node{}, $2} $$ = &ast.StmtBreak{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
BreakTkn: $1,
// save comments Expr: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) SemiColonTkn: $3,
yylex.(*Parser).setFreeFloating($$, token.Expr, $3.SkippedTokens) }
yylex.(*Parser).setToken($$, token.SemiColon, $3.SkippedTokens)
} }
| T_CONTINUE ';' | T_CONTINUE ';'
{ {
$$ = &ast.StmtContinue{ast.Node{}, nil} $$ = &ast.StmtContinue{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $2),
$$.GetNode().Position = position.NewTokensPosition($1, $2) },
ContinueTkn: $1,
// save comments SemiColonTkn: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
yylex.(*Parser).setFreeFloating($$, token.Expr, $2.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $2.SkippedTokens)
} }
| T_CONTINUE expr ';' | T_CONTINUE expr ';'
{ {
$$ = &ast.StmtContinue{ast.Node{}, $2} $$ = &ast.StmtContinue{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
ContinueTkn: $1,
// save comments Expr: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) SemiColonTkn: $3,
yylex.(*Parser).setFreeFloating($$, token.Expr, $3.SkippedTokens) }
yylex.(*Parser).setToken($$, token.SemiColon, $3.SkippedTokens)
} }
| T_RETURN ';' | T_RETURN ';'
{ {

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -919,27 +919,25 @@ statement:
} }
| T_BREAK optional_expr ';' | T_BREAK optional_expr ';'
{ {
$$ = &ast.StmtBreak{ast.Node{}, $2} $$ = &ast.StmtBreak{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
BreakTkn: $1,
// save comments Expr: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) SemiColonTkn: $3,
yylex.(*Parser).setFreeFloating($$, token.Expr, $3.SkippedTokens) }
yylex.(*Parser).setToken($$, token.SemiColon, $3.SkippedTokens)
} }
| T_CONTINUE optional_expr ';' | T_CONTINUE optional_expr ';'
{ {
$$ = &ast.StmtContinue{ast.Node{}, $2} $$ = &ast.StmtContinue{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
ContinueTkn: $1,
// save comments Expr: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) SemiColonTkn: $3,
yylex.(*Parser).setFreeFloating($$, token.Expr, $3.SkippedTokens) }
yylex.(*Parser).setToken($$, token.SemiColon, $3.SkippedTokens)
} }
| T_RETURN optional_expr ';' | T_RETURN optional_expr ';'
{ {

View File

@ -191,7 +191,9 @@ func (n *StmtAltForeach) Accept(v NodeVisitor) {
// StmtBreak node // StmtBreak node
type StmtBreak struct { type StmtBreak struct {
Node Node
BreakTkn *token.Token
Expr Vertex Expr Vertex
SemiColonTkn *token.Token
} }
func (n *StmtBreak) Accept(v NodeVisitor) { func (n *StmtBreak) Accept(v NodeVisitor) {
@ -314,7 +316,9 @@ func (n *StmtConstant) Accept(v NodeVisitor) {
// StmtContinue node // StmtContinue node
type StmtContinue struct { type StmtContinue struct {
Node Node
ContinueTkn *token.Token
Expr Vertex Expr Vertex
SemiColonTkn *token.Token
} }
func (n *StmtContinue) Accept(v NodeVisitor) { func (n *StmtContinue) Accept(v NodeVisitor) {

View File

@ -163,3 +163,13 @@ func (v *FilterTokens) StmtDefault(n *ast.StmtDefault) {
n.DefaultTkn = nil n.DefaultTkn = nil
n.CaseSeparatorTkn = nil n.CaseSeparatorTkn = nil
} }
func (v *FilterTokens) StmtBreak(n *ast.StmtBreak) {
n.BreakTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtContinue(n *ast.StmtContinue) {
n.ContinueTkn = nil
n.SemiColonTkn = nil
}

View File

@ -2017,25 +2017,15 @@ func (p *Printer) printStmtAltForeach(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtBreak(n ast.Vertex) { func (p *Printer) printStmtBreak(n *ast.StmtBreak) {
nn := n.(*ast.StmtBreak) p.printToken(n.BreakTkn, "break")
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "break") if n.Expr != nil {
if nn.Expr != nil { p.bufStart = " "
if nn.Expr.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(nn.Expr)
}
p.printFreeFloating(nn, token.Expr)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
} }
p.printFreeFloating(nn, token.End) p.Print(n.Expr)
p.printToken(n.SemiColonTkn, ";")
} }
func (p *Printer) printStmtCase(n *ast.StmtCase) { func (p *Printer) printStmtCase(n *ast.StmtCase) {
@ -2202,26 +2192,15 @@ func (p *Printer) printStmtConstant(n *ast.StmtConstant) {
p.printToken(n.CommaTkn, "") p.printToken(n.CommaTkn, "")
} }
func (p *Printer) printStmtContinue(n ast.Vertex) { func (p *Printer) printStmtContinue(n *ast.StmtContinue) {
nn := n.(*ast.StmtContinue) p.printToken(n.ContinueTkn, "continue")
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "continue") if n.Expr != nil {
p.bufStart = " "
if nn.Expr != nil {
if nn.Expr.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(nn.Expr)
}
p.printFreeFloating(nn, token.Expr)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
} }
p.printFreeFloating(nn, token.End) p.Print(n.Expr)
p.printToken(n.SemiColonTkn, ";")
} }
func (p *Printer) printStmtDeclare(n ast.Vertex) { func (p *Printer) printStmtDeclare(n ast.Vertex) {

View File

@ -837,7 +837,8 @@ func TestParseAndPrintPhp5AltWhile(t *testing.T) {
} }
func TestParseAndPrintPhp5Break(t *testing.T) { func TestParseAndPrintPhp5Break(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
break ; break ;
break 1 ; break 1 ;
@ -913,7 +914,8 @@ func TestParseAndPrintPhp5ConstList(t *testing.T) {
} }
func TestParseAndPrintPhp5Continue(t *testing.T) { func TestParseAndPrintPhp5Continue(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
continue ; continue ;
continue 1 ; continue 1 ;

View File

@ -950,7 +950,8 @@ func TestParseAndPrintAltWhile(t *testing.T) {
} }
func TestParseAndPrintBreak(t *testing.T) { func TestParseAndPrintBreak(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
break ; break ;
break 1 ; break 1 ;
@ -1029,7 +1030,8 @@ func TestParseAndPrintConstList(t *testing.T) {
} }
func TestParseAndPrintContinue(t *testing.T) { func TestParseAndPrintContinue(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
continue ; continue ;
continue 1 ; continue 1 ;