foreach node

This commit is contained in:
vadim 2017-12-07 18:27:56 +02:00
parent 93a46477ed
commit 4394c3997d
3 changed files with 397 additions and 362 deletions

53
node/stmt/foreach.go Normal file
View File

@ -0,0 +1,53 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Foreach struct {
node.SimpleNode
token token.Token
expr node.Node
key node.Node
variable node.Node
stmt node.Node
}
func NewForeach(token token.Token, expr node.Node, key node.Node, variable node.Node, stmt node.Node) node.Node {
return Foreach{
node.SimpleNode{Name: "Foreach", Attributes: make(map[string]string)},
token,
expr,
key,
variable,
stmt,
}
}
func (n Foreach) 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+" ")
}
if n.key != nil {
fmt.Fprintf(out, "\n%vkey:", indent+" ")
n.key.Print(out, indent+" ")
}
if n.variable != nil {
fmt.Fprintf(out, "\n%vvariable:", indent+" ")
n.variable.Print(out, indent+" ")
}
if n.stmt != nil {
fmt.Fprintf(out, "\n%vstmt:", indent+" ")
n.stmt.Print(out, indent+" ")
}
}

File diff suppressed because it is too large Load Diff

View File

@ -377,20 +377,9 @@ statement:
| T_UNSET '(' unset_variables possible_comma ')' ';' | T_UNSET '(' unset_variables possible_comma ')' ';'
{ $$ = node.NewSimpleNode("Unset").Append($3); } { $$ = node.NewSimpleNode("Unset").Append($3); }
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement | T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
{ { $$ = stmt.NewForeach($1, $3, nil, $5, $7); }
$$ = node.NewSimpleNode("Foreach").
Append(node.NewSimpleNode("expr").Append($3)).
Append(node.NewSimpleNode("ForeachVariable").Append($5)).
Append($7);
}
| T_FOREACH '(' expr T_AS foreach_variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement | T_FOREACH '(' expr T_AS foreach_variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
{ { $$ = stmt.NewForeach($1, $3, $5, $7, $9); }
$$ = node.NewSimpleNode("Foreach").
Append(node.NewSimpleNode("expr").Append($3)).
Append(node.NewSimpleNode("ForeachKey").Append($5)).
Append(node.NewSimpleNode("ForeachVariable").Append($7)).
Append($9);
}
| T_DECLARE '(' const_list ')' declare_statement { $$ = stmt.NewDeclare($1, $3, $5) } | T_DECLARE '(' const_list ')' declare_statement { $$ = stmt.NewDeclare($1, $3, $5) }
| ';' /* empty statement */ { $$ = node.NewSimpleNode(""); } | ';' /* empty statement */ { $$ = node.NewSimpleNode(""); }
| T_TRY '{' inner_statement_list '}' catch_list finally_statement | T_TRY '{' inner_statement_list '}' catch_list finally_statement