trait node

This commit is contained in:
vadim 2017-12-09 12:11:56 +02:00
parent 1b0f341c97
commit d33afa9b38
4 changed files with 71 additions and 4 deletions

32
node/stmt/throw.go Normal file
View File

@ -0,0 +1,32 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Throw struct {
node.SimpleNode
token token.Token
expr node.Node
}
func NewThrow(token token.Token, expr node.Node) node.Node {
return Throw{
node.SimpleNode{Name: "Throw", Attributes: make(map[string]string)},
token,
expr,
}
}
func (n Throw) 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+" ")
}
}

35
node/stmt/trait.go Normal file
View File

@ -0,0 +1,35 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Trait struct {
node.SimpleNode
token token.Token
stmts []node.Node
}
//TODO: stmts myst be []node.Node
func NewTrait(token token.Token, stmts []node.Node) node.Node {
return Trait{
node.SimpleNode{Name: "Trait", Attributes: make(map[string]string)},
token,
stmts,
}
}
func (n Trait) 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+" ")
}
}
}

View File

@ -2932,7 +2932,7 @@ yydefault:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]
//line parser/parser.y:403 //line parser/parser.y:403
{ {
yyVAL.node = node.NewSimpleNode("Throw").Append(yyDollar[2].node) yyVAL.node = stmt.NewThrow(yyDollar[1].token, yyDollar[2].node)
} }
case 151: case 151:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]
@ -3070,7 +3070,7 @@ yydefault:
yyDollar = yyS[yypt-5 : yypt+1] yyDollar = yyS[yypt-5 : yypt+1]
//line parser/parser.y:466 //line parser/parser.y:466
{ {
yyVAL.node = node.NewSimpleNode("Trait").Attribute("name", yyDollar[2].token.String()).Append(yyDollar[4].node) yyVAL.node = stmt.NewTrait(yyDollar[2].token, yyDollar[4].node.(node.SimpleNode).Children)
} }
case 174: case 174:
yyDollar = yyS[yypt-6 : yypt+1] yyDollar = yyS[yypt-6 : yypt+1]

View File

@ -400,7 +400,7 @@ statement:
{ {
$$ = stmt.NewTry($1, $3.(node.SimpleNode).Children, $5, $6) $$ = stmt.NewTry($1, $3.(node.SimpleNode).Children, $5, $6)
} }
| T_THROW expr ';' { $$ = node.NewSimpleNode("Throw").Append($2) } | T_THROW expr ';' { $$ = stmt.NewThrow($1, $2) }
| T_GOTO T_STRING ';' { $$ = stmt.NewGoto($1, $2) } | T_GOTO T_STRING ';' { $$ = stmt.NewGoto($1, $2) }
| T_STRING ':' { $$ = stmt.NewLabel($1) } | T_STRING ':' { $$ = stmt.NewLabel($1) }
@ -463,7 +463,7 @@ class_modifier:
; ;
trait_declaration_statement: trait_declaration_statement:
T_TRAIT T_STRING '{' class_statement_list '}' { $$ = node.NewSimpleNode("Trait").Attribute("name", $2.String()).Append($4) } T_TRAIT T_STRING '{' class_statement_list '}' { $$ = stmt.NewTrait($2, $4.(node.SimpleNode).Children) }
; ;
interface_declaration_statement: interface_declaration_statement: