foreach node
This commit is contained in:
parent
93a46477ed
commit
4394c3997d
53
node/stmt/foreach.go
Normal file
53
node/stmt/foreach.go
Normal 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+" ")
|
||||
}
|
||||
}
|
691
parser/parser.go
691
parser/parser.go
File diff suppressed because it is too large
Load Diff
@ -377,20 +377,9 @@ statement:
|
||||
| T_UNSET '(' unset_variables possible_comma ')' ';'
|
||||
{ $$ = node.NewSimpleNode("Unset").Append($3); }
|
||||
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
|
||||
{
|
||||
$$ = node.NewSimpleNode("Foreach").
|
||||
Append(node.NewSimpleNode("expr").Append($3)).
|
||||
Append(node.NewSimpleNode("ForeachVariable").Append($5)).
|
||||
Append($7);
|
||||
}
|
||||
{ $$ = stmt.NewForeach($1, $3, nil, $5, $7); }
|
||||
| T_FOREACH '(' expr T_AS foreach_variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
|
||||
{
|
||||
$$ = node.NewSimpleNode("Foreach").
|
||||
Append(node.NewSimpleNode("expr").Append($3)).
|
||||
Append(node.NewSimpleNode("ForeachKey").Append($5)).
|
||||
Append(node.NewSimpleNode("ForeachVariable").Append($7)).
|
||||
Append($9);
|
||||
}
|
||||
{ $$ = stmt.NewForeach($1, $3, $5, $7, $9); }
|
||||
| T_DECLARE '(' const_list ')' declare_statement { $$ = stmt.NewDeclare($1, $3, $5) }
|
||||
| ';' /* empty statement */ { $$ = node.NewSimpleNode(""); }
|
||||
| T_TRY '{' inner_statement_list '}' catch_list finally_statement
|
||||
|
Loading…
Reference in New Issue
Block a user