[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 ';'
{
$$ = &ast.StmtBreak{ast.Node{}, nil}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Expr, $2.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $2.SkippedTokens)
$$ = &ast.StmtBreak{
Node: ast.Node{
Position: position.NewTokensPosition($1, $2),
},
BreakTkn: $1,
SemiColonTkn: $2,
}
}
| T_BREAK expr ';'
{
$$ = &ast.StmtBreak{ast.Node{}, $2}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Expr, $3.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $3.SkippedTokens)
$$ = &ast.StmtBreak{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
BreakTkn: $1,
Expr: $2,
SemiColonTkn: $3,
}
}
| T_CONTINUE ';'
{
$$ = &ast.StmtContinue{ast.Node{}, nil}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Expr, $2.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $2.SkippedTokens)
$$ = &ast.StmtContinue{
Node: ast.Node{
Position: position.NewTokensPosition($1, $2),
},
ContinueTkn: $1,
SemiColonTkn: $2,
}
}
| T_CONTINUE expr ';'
{
$$ = &ast.StmtContinue{ast.Node{}, $2}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Expr, $3.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $3.SkippedTokens)
$$ = &ast.StmtContinue{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
ContinueTkn: $1,
Expr: $2,
SemiColonTkn: $3,
}
}
| T_RETURN ';'
{

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

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

View File

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

View File

@ -163,3 +163,13 @@ func (v *FilterTokens) StmtDefault(n *ast.StmtDefault) {
n.DefaultTkn = 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)
}
func (p *Printer) printStmtBreak(n ast.Vertex) {
nn := n.(*ast.StmtBreak)
p.printFreeFloating(nn, token.Start)
func (p *Printer) printStmtBreak(n *ast.StmtBreak) {
p.printToken(n.BreakTkn, "break")
io.WriteString(p.w, "break")
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, ";")
if n.Expr != nil {
p.bufStart = " "
}
p.printFreeFloating(nn, token.End)
p.Print(n.Expr)
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtCase(n *ast.StmtCase) {
@ -2202,26 +2192,15 @@ func (p *Printer) printStmtConstant(n *ast.StmtConstant) {
p.printToken(n.CommaTkn, "")
}
func (p *Printer) printStmtContinue(n ast.Vertex) {
nn := n.(*ast.StmtContinue)
p.printFreeFloating(nn, token.Start)
func (p *Printer) printStmtContinue(n *ast.StmtContinue) {
p.printToken(n.ContinueTkn, "continue")
io.WriteString(p.w, "continue")
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, ";")
if n.Expr != nil {
p.bufStart = " "
}
p.printFreeFloating(nn, token.End)
p.Print(n.Expr)
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtDeclare(n ast.Vertex) {

View File

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

View File

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