[refactoring] update ast structure of "Return" node

This commit is contained in:
Vadym Slizov 2020-09-06 13:09:00 +03:00
parent e817f8dd8d
commit 59ef622082
7 changed files with 45 additions and 49 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -992,39 +992,35 @@ unticked_statement:
}
| T_RETURN ';'
{
$$ = &ast.StmtReturn{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.StmtReturn{
Node: ast.Node{
Position: position.NewTokensPosition($1, $2),
},
ReturnTkn: $1,
SemiColonTkn: $2,
}
}
| T_RETURN expr_without_variable ';'
{
$$ = &ast.StmtReturn{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.StmtReturn{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
ReturnTkn: $1,
Expr: $2,
SemiColonTkn: $3,
}
}
| T_RETURN variable ';'
{
$$ = &ast.StmtReturn{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.StmtReturn{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
ReturnTkn: $1,
Expr: $2,
SemiColonTkn: $3,
}
}
| yield_expr ';'
{

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -941,15 +941,14 @@ statement:
}
| T_RETURN optional_expr ';'
{
$$ = &ast.StmtReturn{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.StmtReturn{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
ReturnTkn: $1,
Expr: $2,
SemiColonTkn: $3,
}
}
| T_GLOBAL global_var_list ';'
{

View File

@ -618,7 +618,9 @@ func (n *StmtPropertyList) Accept(v NodeVisitor) {
// StmtReturn node
type StmtReturn struct {
Node
Expr Vertex
ReturnTkn *token.Token
Expr Vertex
SemiColonTkn *token.Token
}
func (n *StmtReturn) Accept(v NodeVisitor) {

View File

@ -173,3 +173,8 @@ func (v *FilterTokens) StmtContinue(n *ast.StmtContinue) {
n.ContinueTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtReturn(n *ast.StmtReturn) {
n.ReturnTkn = nil
n.SemiColonTkn = nil
}

View File

@ -2690,21 +2690,15 @@ func (p *Printer) printStmtProperty(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtReturn(n ast.Vertex) {
nn := n.(*ast.StmtReturn)
p.printFreeFloating(nn, token.Start)
func (p *Printer) printStmtReturn(n *ast.StmtReturn) {
p.printToken(n.ReturnTkn, "return")
io.WriteString(p.w, "return")
p.bufStart = " "
p.Print(nn.Expr)
p.printFreeFloating(nn, token.Expr)
p.printFreeFloating(nn, token.SemiColon)
if n.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
if n.Expr != nil {
p.bufStart = " "
}
p.Print(n.Expr)
p.printFreeFloating(nn, token.End)
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtStaticVar(n ast.Vertex) {