return node

This commit is contained in:
vadim 2017-12-08 17:19:33 +02:00
parent ed49675a35
commit 5fe3c4071d
3 changed files with 34 additions and 2 deletions

32
node/stmt/return.go Normal file
View File

@ -0,0 +1,32 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Return struct {
node.SimpleNode
token token.Token
expr node.Node
}
func NewReturn(token token.Token, expr node.Node) node.Node {
return Return{
node.SimpleNode{Name: "Return", Attributes: make(map[string]string)},
token,
expr,
}
}
func (n Return) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.Name, n.token.StartLine, n.token.EndLine, n.token.Value)
if n.expr != nil {
fmt.Fprintf(out, "\n%vexpr:", indent+" ")
n.expr.Print(out, indent+" ")
}
}

View File

@ -2860,7 +2860,7 @@ yydefault:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]
//line parser/parser.y:384 //line parser/parser.y:384
{ {
yyVAL.node = node.NewSimpleNode("Return").Append(yyDollar[2].node) yyVAL.node = stmt.NewReturn(yyDollar[1].token, yyDollar[2].node)
} }
case 139: case 139:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]

View File

@ -381,7 +381,7 @@ statement:
| T_SWITCH '(' expr ')' switch_case_list { $$ = node.NewSimpleNode("Switch").Append(node.NewSimpleNode("expr").Append($3)).Append($5); } | T_SWITCH '(' expr ')' switch_case_list { $$ = node.NewSimpleNode("Switch").Append(node.NewSimpleNode("expr").Append($3)).Append($5); }
| T_BREAK optional_expr ';' { $$ = stmt.NewBreak($1, $2) } | T_BREAK optional_expr ';' { $$ = stmt.NewBreak($1, $2) }
| T_CONTINUE optional_expr ';' { $$ = stmt.NewContinue($1, $2) } | T_CONTINUE optional_expr ';' { $$ = stmt.NewContinue($1, $2) }
| T_RETURN optional_expr ';' { $$ = node.NewSimpleNode("Return").Append($2) } | T_RETURN optional_expr ';' { $$ = stmt.NewReturn($1, $2) }
| T_GLOBAL global_var_list ';' { $$ = stmt.NewGlobal($1, $2) } | T_GLOBAL global_var_list ';' { $$ = stmt.NewGlobal($1, $2) }
| T_STATIC static_var_list ';' { $$ = $2; } | T_STATIC static_var_list ';' { $$ = $2; }
| T_ECHO echo_expr_list ';' { $$ = stmt.NewEcho($1, $2) } | T_ECHO echo_expr_list ';' { $$ = stmt.NewEcho($1, $2) }