closure node

This commit is contained in:
z7zmey 2017-12-16 22:45:05 +02:00
parent df662aaa01
commit 84aefef316
3 changed files with 632 additions and 590 deletions

63
node/expr/closure.go Normal file
View File

@ -0,0 +1,63 @@
package expr
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
)
type Closure struct {
node.SimpleNode
params []node.Node
uses []node.Node
returnType node.Node
stmts []node.Node
isReturnRef bool
isStatic bool
}
func NewClosure(params []node.Node, uses []node.Node, returnType node.Node, stmts []node.Node, isStatic bool, isReturnRef bool) node.Node {
return Closure{
node.SimpleNode{Name: "Closure", Attributes: make(map[string]string)},
params,
uses,
returnType,
stmts,
isReturnRef,
isStatic,
}
}
func (n Closure) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [- -]", indent, n.Name)
fmt.Fprintf(out, "\n%vis static: %t", indent+" ", n.isStatic)
fmt.Fprintf(out, "\n%vis return ref: %t", indent+" ", n.isReturnRef)
if n.params != nil {
fmt.Fprintf(out, "\n%vparams:", indent+" ")
for _, nn := range n.params {
nn.Print(out, indent+" ")
}
}
if n.uses != nil {
fmt.Fprintf(out, "\n%vuses:", indent+" ")
for _, nn := range n.uses {
nn.Print(out, indent+" ")
}
}
if n.returnType != nil {
fmt.Fprintf(out, "\n%vreturn type:", indent+" ")
n.returnType.Print(out, indent+" ")
}
if n.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
nn.Print(out, indent+" ")
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -212,8 +212,7 @@ func Parse(src io.Reader, fName string) node.Node {
%type <node> implements_list if_stmt_without_else
%type <node> non_empty_parameter_list argument_list non_empty_argument_list
%type <node> class_const_decl name_list method_body
%type <node> ctor_arguments alt_if_stmt_without_else lexical_vars
%type <node> lexical_var_list
%type <node> ctor_arguments alt_if_stmt_without_else
%type <node> array_pair non_empty_array_pair_list array_pair_list possible_array_pair
%type <node> isset_variable type return_type type_expr
@ -226,7 +225,7 @@ func Parse(src io.Reader, fName string) node.Node {
%type <list> const_list echo_expr_list for_exprs non_empty_for_exprs global_var_list
%type <list> unprefixed_use_declarations inline_use_declarations property_list static_var_list
%type <list> switch_case_list case_list trait_adaptation_list trait_adaptations unset_variables
%type <list> use_declarations
%type <list> use_declarations lexical_var_list lexical_vars
%%
@ -888,21 +887,11 @@ expr_without_variable:
| T_YIELD_FROM expr { $$ = node.NewSimpleNode("YieldFrom").Append($2); }
| T_FUNCTION returns_ref '(' parameter_list ')' lexical_vars return_type '{' inner_statement_list '}'
{
$$ = node.NewSimpleNode("Closure").
Attribute("returns_ref", $2).
Append($4).
Append($6).
Append($7).
Append($9);
$$ = expr.NewClosure($4.(node.SimpleNode).Children, $6, $7, $9.(node.SimpleNode).Children, false, $2 == "true")
}
| T_STATIC T_FUNCTION returns_ref '(' parameter_list ')' lexical_vars return_type '{' inner_statement_list '}'
{
$$ = node.NewSimpleNode("StaticClosure").
Attribute("returns_ref", $3).
Append($5).
Append($7).
Append($8).
Append($10);
$$ = expr.NewClosure($5.(node.SimpleNode).Children, $7, $8, $10.(node.SimpleNode).Children, true, $3 == "true")
}
;
@ -912,13 +901,13 @@ returns_ref:
;
lexical_vars:
/* empty */ { $$ = node.NewSimpleNode("") }
/* empty */ { $$ = []node.Node{} }
| T_USE '(' lexical_var_list ')' { $$ = $3; }
;
lexical_var_list:
lexical_var_list ',' lexical_var { $$ = $1.Append($3) }
| lexical_var { $$ = node.NewSimpleNode("ClosureUses").Append($1) }
lexical_var_list ',' lexical_var { $$ = append($1, $3) }
| lexical_var { $$ = []node.Node{$1} }
;
lexical_var: