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

View File

@ -378,8 +378,13 @@ func (n *StmtDefault) Accept(v NodeVisitor) {
// StmtDo node // StmtDo node
type StmtDo struct { type StmtDo struct {
Node Node
DoTkn *token.Token
Stmt Vertex Stmt Vertex
WhileTkn *token.Token
OpenParenthesisTkn *token.Token
Cond Vertex Cond Vertex
CloseParenthesisTkn *token.Token
SemiColonTkn *token.Token
} }
func (n *StmtDo) Accept(v NodeVisitor) { func (n *StmtDo) Accept(v NodeVisitor) {

View File

@ -13,16 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
return true 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) { func (v *FilterParserNodes) StmtSwitch(n *ast.StmtSwitch) {
for { for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok { 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.EndWhileTkn = nil
n.SemiColonTkn = 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) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtDo(n ast.Vertex) { func (p *Printer) printStmtDo(n *ast.StmtDo) {
nn := n.(*ast.StmtDo) p.printToken(n.DoTkn, "do")
p.printFreeFloating(nn, token.Start) p.bufStart = " "
io.WriteString(p.w, "do") p.Print(n.Stmt)
if _, ok := nn.Stmt.(*ast.StmtStmtList); !ok { p.printToken(n.WhileTkn, "while")
if nn.Stmt.GetNode().Tokens.IsEmpty() { p.printToken(n.OpenParenthesisTkn, "(")
io.WriteString(p.w, " ") p.Print(n.Cond)
} p.printToken(n.CloseParenthesisTkn, ")")
} p.printToken(n.SemiColonTkn, ";")
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)
} }
func (p *Printer) printStmtEcho(n ast.Vertex) { func (p *Printer) printStmtEcho(n ast.Vertex) {

View File

@ -945,7 +945,8 @@ func TestParseAndPrintPhp5Declare(t *testing.T) {
} }
func TestParseAndPrintPhp5DoWhile(t *testing.T) { func TestParseAndPrintPhp5DoWhile(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
do { do {
; ;
} while ( $a ) ; } while ( $a ) ;

View File

@ -1061,7 +1061,8 @@ func TestParseAndPrintDeclare(t *testing.T) {
} }
func TestParseAndPrintDoWhile(t *testing.T) { func TestParseAndPrintDoWhile(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
do { do {
; ;
} while ( $a ) ; } while ( $a ) ;