[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 51 additions and 76 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -911,16 +911,18 @@ unticked_statement:
}
| T_DO statement T_WHILE parenthesis_expr ';'
{
$$ = &ast.StmtDo{ast.Node{}, $2, $4}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $5)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating($4, token.End, $5.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $5.SkippedTokens)
$$ = &ast.StmtDo{
Node: ast.Node{
Position: position.NewTokensPosition($1, $5),
},
DoTkn: $1,
Stmt: $2,
WhileTkn: $3,
OpenParenthesisTkn: $4.(*ast.ParserBrackets).OpenBracketTkn,
Cond: $4.(*ast.ParserBrackets).Child,
CloseParenthesisTkn: $4.(*ast.ParserBrackets).CloseBracketTkn,
SemiColonTkn: $5,
}
}
| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
{

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -880,25 +880,18 @@ statement:
}
| T_DO statement T_WHILE '(' expr ')' ';'
{
exprBrackets := &ast.ParserBrackets{
$$ = &ast.StmtDo{
Node: ast.Node{
Position: position.NewTokensPosition($4, $6),
Position: position.NewTokensPosition($1, $7),
},
OpenBracketTkn: $4,
Child: $5,
CloseBracketTkn: $6,
DoTkn: $1,
Stmt: $2,
WhileTkn: $3,
OpenParenthesisTkn: $4,
Cond: $5,
CloseParenthesisTkn: $6,
SemiColonTkn: $7,
}
$$ = &ast.StmtDo{ast.Node{}, $2, exprBrackets}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $7)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $4.SkippedTokens)
yylex.(*Parser).setFreeFloating(exprBrackets, token.End, append($6.SkippedTokens, $7.SkippedTokens...))
yylex.(*Parser).setToken($$, token.SemiColon, $7.SkippedTokens)
}
| T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement
{

View File

@ -378,8 +378,13 @@ func (n *StmtDefault) Accept(v NodeVisitor) {
// StmtDo node
type StmtDo struct {
Node
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

@ -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 {