yield yield_from nodes

This commit is contained in:
z7zmey 2017-12-18 20:45:18 +02:00
parent 92b9453de7
commit 3ec45dc334
4 changed files with 73 additions and 8 deletions

36
node/expr/yield.go Normal file
View File

@ -0,0 +1,36 @@
package expr
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
)
type Yield struct {
node.SimpleNode
key node.Node
value node.Node
}
func NewYield(key node.Node, value node.Node) node.Node {
return Yield{
node.SimpleNode{Name: "Yield", Attributes: make(map[string]string)},
key,
value,
}
}
func (n Yield) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [- -]", indent, n.Name)
if n.key != nil {
fmt.Fprintf(out, "\n%vkey:", indent+" ")
n.key.Print(out, indent+" ")
}
if n.value != nil {
fmt.Fprintf(out, "\n%vvalue:", indent+" ")
n.value.Print(out, indent+" ")
}
}

29
node/expr/yield_from.go Normal file
View File

@ -0,0 +1,29 @@
package expr
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
)
type YieldFrom struct {
node.SimpleNode
expr node.Node
}
func NewYieldFrom(expression node.Node) node.Node {
return YieldFrom{
node.SimpleNode{Name: "YieldFrom", Attributes: make(map[string]string)},
expression,
}
}
func (n YieldFrom) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [- -]", indent, n.Name)
if n.expr != nil {
fmt.Fprintf(out, "\n%vexpr:", indent+" ")
n.expr.Print(out, indent+" ")
}
}

View File

@ -4221,25 +4221,25 @@ yydefault:
yyDollar = yyS[yypt-1 : yypt+1] yyDollar = yyS[yypt-1 : yypt+1]
//line parser/parser.y:894 //line parser/parser.y:894
{ {
yyVAL.node = node.NewSimpleNode("Yield") yyVAL.node = expr.NewYield(nil, nil)
} }
case 364: case 364:
yyDollar = yyS[yypt-2 : yypt+1] yyDollar = yyS[yypt-2 : yypt+1]
//line parser/parser.y:895 //line parser/parser.y:895
{ {
yyVAL.node = node.NewSimpleNode("Yield").Append(yyDollar[2].node) yyVAL.node = expr.NewYield(nil, yyDollar[2].node)
} }
case 365: case 365:
yyDollar = yyS[yypt-4 : yypt+1] yyDollar = yyS[yypt-4 : yypt+1]
//line parser/parser.y:896 //line parser/parser.y:896
{ {
yyVAL.node = node.NewSimpleNode("Yield").Append(yyDollar[2].node).Append(yyDollar[4].node) yyVAL.node = expr.NewYield(yyDollar[2].node, yyDollar[4].node)
} }
case 366: case 366:
yyDollar = yyS[yypt-2 : yypt+1] yyDollar = yyS[yypt-2 : yypt+1]
//line parser/parser.y:897 //line parser/parser.y:897
{ {
yyVAL.node = node.NewSimpleNode("YieldFrom").Append(yyDollar[2].node) yyVAL.node = expr.NewYieldFrom(yyDollar[2].node)
} }
case 367: case 367:
yyDollar = yyS[yypt-10 : yypt+1] yyDollar = yyS[yypt-10 : yypt+1]

View File

@ -891,10 +891,10 @@ expr_without_variable:
| scalar { $$ = $1; } | scalar { $$ = $1; }
| '`' backticks_expr '`' { $$ = expr.NewShellExec($2) } | '`' backticks_expr '`' { $$ = expr.NewShellExec($2) }
| T_PRINT expr { $$ = expr.NewPrint($2) } | T_PRINT expr { $$ = expr.NewPrint($2) }
| T_YIELD { $$ = node.NewSimpleNode("Yield"); } | T_YIELD { $$ = expr.NewYield(nil, nil) }
| T_YIELD expr { $$ = node.NewSimpleNode("Yield").Append($2); } | T_YIELD expr { $$ = expr.NewYield(nil, $2) }
| T_YIELD expr T_DOUBLE_ARROW expr { $$ = node.NewSimpleNode("Yield").Append($2).Append($4); } | T_YIELD expr T_DOUBLE_ARROW expr { $$ = expr.NewYield($2, $4) }
| T_YIELD_FROM expr { $$ = node.NewSimpleNode("YieldFrom").Append($2); } | T_YIELD_FROM expr { $$ = expr.NewYieldFrom($2) }
| T_FUNCTION returns_ref '(' parameter_list ')' lexical_vars return_type '{' inner_statement_list '}' | T_FUNCTION returns_ref '(' parameter_list ')' lexical_vars return_type '{' inner_statement_list '}'
{ {
$$ = expr.NewClosure($4.(node.SimpleNode).Children, $6, $7, $9.(node.SimpleNode).Children, false, $2 == "true") $$ = expr.NewClosure($4.(node.SimpleNode).Children, $6, $7, $9.(node.SimpleNode).Children, false, $2 == "true")