if elseif else nodes

This commit is contained in:
z7zmey 2017-12-07 00:53:03 +02:00
parent 1bc9802382
commit 9608147c51
5 changed files with 455 additions and 299 deletions

34
node/stmt/else.go Normal file
View File

@ -0,0 +1,34 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Else struct {
node.SimpleNode
token token.Token
stmts []node.Node
}
func NewElse(token token.Token, stmts []node.Node) node.Node {
return Else{
node.SimpleNode{Name: "Else", Attributes: make(map[string]string)},
token,
stmts,
}
}
func (n Else) 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.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
nn.Print(out, indent+" ")
}
}
}

41
node/stmt/else_if.go Normal file
View File

@ -0,0 +1,41 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type ElseIf struct {
node.SimpleNode
token token.Token
cond node.Node
stmts []node.Node
}
func NewElseIf(token token.Token, cond node.Node, stmts []node.Node) node.Node {
return ElseIf{
node.SimpleNode{Name: "ElseIf", Attributes: make(map[string]string)},
token,
cond,
stmts,
}
}
func (n ElseIf) 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.cond != nil {
fmt.Fprintf(out, "\n%vcond:", indent+" ")
n.cond.Print(out, indent+" ")
}
if n.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
nn.Print(out, indent+" ")
}
}
}

72
node/stmt/if.go Normal file
View File

@ -0,0 +1,72 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type If struct {
node.SimpleNode
token token.Token
cond node.Node
stmts []node.Node
elseIf []node.Node
_else node.Node
}
func NewIf(token token.Token, cond node.Node, stmts []node.Node) node.Node {
return If{
node.SimpleNode{Name: "If", Attributes: make(map[string]string)},
token,
cond,
stmts,
nil,
nil,
}
}
func (n If) AddElseIf(elseIf node.Node) node.Node {
if (n.elseIf == nil) {
n.elseIf = make([]node.Node, 0)
}
n.elseIf = append(n.elseIf, elseIf)
return n
}
func (n If) SetElse(_else node.Node) node.Node {
n._else = _else
return n
}
func (n If) 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.cond != nil {
fmt.Fprintf(out, "\n%vcond:", indent+" ")
n.cond.Print(out, indent+" ")
}
if n.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
nn.Print(out, indent+" ")
}
}
if n.elseIf != nil {
fmt.Fprintf(out, "\n%velseIfs:", indent+" ")
for _, nn := range n.elseIf {
nn.Print(out, indent+" ")
}
}
if n._else != nil {
fmt.Fprintf(out, "\n%velse:", indent+" ")
n._else.Print(out, indent+" ")
}
}

File diff suppressed because it is too large Load Diff

View File

@ -549,13 +549,11 @@ while_statement:
; ;
if_stmt_without_else: if_stmt_without_else:
T_IF '(' expr ')' statement T_IF '(' expr ')' statement { $$ = stmt.NewIf($1, $3, $5.(node.SimpleNode).Children) }
{
$$ = node.NewSimpleNode("If").Append(node.NewSimpleNode("expr").Append($3)).Append(node.NewSimpleNode("stmt").Append($5))
}
| if_stmt_without_else T_ELSEIF '(' expr ')' statement | if_stmt_without_else T_ELSEIF '(' expr ')' statement
{ {
$$ = $1.Append(node.NewSimpleNode("ElseIf").Append(node.NewSimpleNode("expr").Append($4)).Append(node.NewSimpleNode("stmt").Append($6))) _elseIf := stmt.NewElseIf($2, $4, $6.(node.SimpleNode).Children)
$$ = $1.(stmt.If).AddElseIf(_elseIf)
} }
; ;
@ -563,18 +561,23 @@ if_stmt:
if_stmt_without_else %prec T_NOELSE { $$ = $1; } if_stmt_without_else %prec T_NOELSE { $$ = $1; }
| if_stmt_without_else T_ELSE statement | if_stmt_without_else T_ELSE statement
{ {
$$ = $1.Append(node.NewSimpleNode("Else").Append(node.NewSimpleNode("stmt").Append($3))) _else := stmt.NewElse($2, $3.(node.SimpleNode).Children)
$$ = $1.(stmt.If).SetElse(_else)
} }
; ;
alt_if_stmt_without_else: alt_if_stmt_without_else:
T_IF '(' expr ')' ':' inner_statement_list T_IF '(' expr ')' ':' inner_statement_list
{ {
$$ = node.NewSimpleNode("AltIf").Append(node.NewSimpleNode("expr").Append($3)).Append(node.NewSimpleNode("stmt").Append($6)) $$ = node.NewSimpleNode("AltIf").
Append(node.NewSimpleNode("expr").Append($3)).
Append(node.NewSimpleNode("stmt").Append($6))
} }
| alt_if_stmt_without_else T_ELSEIF '(' expr ')' ':' inner_statement_list | alt_if_stmt_without_else T_ELSEIF '(' expr ')' ':' inner_statement_list
{ {
$$ = $1.Append(node.NewSimpleNode("AltElseIf").Append(node.NewSimpleNode("expr").Append($4)).Append(node.NewSimpleNode("stmt").Append($7))) $$ = $1.Append(node.NewSimpleNode("AltElseIf").
Append(node.NewSimpleNode("expr").Append($4)).
Append(node.NewSimpleNode("stmt").Append($7)))
} }
; ;