This commit is contained in:
vadim 2017-12-09 13:41:08 +02:00
parent 04ad158f78
commit b424e82812
3 changed files with 54 additions and 6 deletions

48
node/stmt/use.go Normal file
View File

@ -0,0 +1,48 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Use struct {
node.SimpleNode
useType node.Node
name node.Node
alias token.TokenInterface
}
func NewUse(useType node.Node, name node.Node, alias token.TokenInterface) node.Node {
return Use{
node.SimpleNode{Name: "Use", Attributes: make(map[string]string)},
useType,
name,
alias,
}
}
func (n Use) SetType(useType node.Node) node.Node {
n.useType = useType
return n
}
func (n Use) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [- -]", indent, n.Name)
if n.useType != nil {
fmt.Fprintf(out, "\n%vtype: %q", indent+" ", n.alias.GetValue())
n.useType.Print(out, indent+" ")
}
if n.name != nil {
fmt.Fprintf(out, "\n%vname:", indent+" ")
n.name.Print(out, indent+" ")
}
if n.alias != nil {
fmt.Fprintf(out, "\n%valias: %q", indent+" ", n.alias.GetValue())
}
}

View File

@ -2714,19 +2714,19 @@ yydefault:
yyDollar = yyS[yypt-2 : yypt+1] yyDollar = yyS[yypt-2 : yypt+1]
//line parser/parser.y:338 //line parser/parser.y:338
{ {
yyVAL.node = yyDollar[2].node.Append(yyDollar[1].node) yyVAL.node = yyDollar[2].node.(stmt.Use).SetType(yyDollar[1].node)
} }
case 115: case 115:
yyDollar = yyS[yypt-1 : yypt+1] yyDollar = yyS[yypt-1 : yypt+1]
//line parser/parser.y:342 //line parser/parser.y:342
{ {
yyVAL.node = node.NewSimpleNode("UseElem").Append(name.NewName(yyDollar[1].list)) yyVAL.node = stmt.NewUse(nil, name.NewName(yyDollar[1].list), nil)
} }
case 116: case 116:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]
//line parser/parser.y:343 //line parser/parser.y:343
{ {
yyVAL.node = node.NewSimpleNode("UseElem").Append(name.NewName(yyDollar[1].list)).Append(node.NewSimpleNode("as").Attribute("value", yyDollar[3].token.String())) yyVAL.node = stmt.NewUse(nil, name.NewName(yyDollar[1].list), yyDollar[3].token)
} }
case 117: case 117:
yyDollar = yyS[yypt-1 : yypt+1] yyDollar = yyS[yypt-1 : yypt+1]

View File

@ -335,12 +335,12 @@ use_declarations:
inline_use_declaration: inline_use_declaration:
unprefixed_use_declaration { $$ = $1; } unprefixed_use_declaration { $$ = $1; }
| use_type unprefixed_use_declaration { $$ = $2.Append($1) } | use_type unprefixed_use_declaration { $$ = $2.(stmt.Use).SetType($1) }
; ;
unprefixed_use_declaration: unprefixed_use_declaration:
namespace_name { $$ = node.NewSimpleNode("UseElem").Append(name.NewName($1)); } namespace_name { $$ = stmt.NewUse(nil, name.NewName($1), nil) }
| namespace_name T_AS T_STRING { $$ = node.NewSimpleNode("UseElem").Append(name.NewName($1)).Append(node.NewSimpleNode("as").Attribute("value", $3.String())); } | namespace_name T_AS T_STRING { $$ = stmt.NewUse(nil, name.NewName($1), $3) }
; ;
use_declaration: use_declaration: