function node

This commit is contained in:
vadim 2017-12-07 18:39:34 +02:00
parent 4394c3997d
commit 280ca04b50
3 changed files with 396 additions and 352 deletions

54
node/stmt/function.go Normal file
View File

@ -0,0 +1,54 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Function struct {
node.SimpleNode
token token.Token
isReturnRef bool
params []node.Node
returnType node.Node
stmts []node.Node
}
func NewFunction(token token.Token, isReturnRef bool, params []node.Node, returnType node.Node, stmts []node.Node) node.Node {
return Function{
node.SimpleNode{Name: "Function", Attributes: make(map[string]string)},
token,
isReturnRef,
params,
returnType,
stmts,
}
}
func (n Function) 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)
fmt.Fprintf(out, "\n%vreturn 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.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

@ -417,12 +417,7 @@ unset_variable:
function_declaration_statement:
T_FUNCTION returns_ref T_STRING '(' parameter_list ')' return_type '{' inner_statement_list '}'
{
$$ = node.NewSimpleNode("Function").
Attribute("name", $3.String()).
Attribute("returns_ref", $2).
Append($5).
Append($7).
Append($9);
$$ = stmt.NewFunction($3, $2 == "true", $5.(node.SimpleNode).Children, $7, $9.(node.SimpleNode).Children)
}
;