variable node

This commit is contained in:
z7zmey 2017-12-18 20:35:29 +02:00
parent ef39b9111e
commit 92b9453de7
3 changed files with 33 additions and 29 deletions

View File

@ -5,21 +5,25 @@ import (
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Variable struct {
node.SimpleNode
token token.Token
name node.Node
}
func NewVariable(token token.Token) node.Node {
func NewVariable(name node.Node) node.Node {
return Variable{
node.SimpleNode{Name: "Variable", Attributes: make(map[string]string)},
token,
name,
}
}
func (n Variable) 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%v%v [- -]", indent, n.Name)
if n.name != nil {
fmt.Fprintf(out, "\n%vname:", indent+" ")
n.name.Print(out, indent+" ")
}
}

View File

@ -2958,7 +2958,7 @@ yydefault:
yyDollar = yyS[yypt-9 : yypt+1]
//line parser/parser.y:415
{
yyVAL.list = append(yyDollar[1].list, stmt.NewCatch(yyDollar[2].token, yyDollar[4].list, expr.NewVariable(yyDollar[5].token), yyDollar[8].node.(node.SimpleNode).Children))
yyVAL.list = append(yyDollar[1].list, stmt.NewCatch(yyDollar[2].token, yyDollar[4].list, expr.NewVariable(node.NewIdentifier(yyDollar[5].token)), yyDollar[8].node.(node.SimpleNode).Children))
}
case 155:
yyDollar = yyS[yypt-1 : yypt+1]
@ -4293,13 +4293,13 @@ yydefault:
yyDollar = yyS[yypt-1 : yypt+1]
//line parser/parser.y:924
{
yyVAL.node = expr.NewClusureUse(expr.NewVariable(yyDollar[1].token), false)
yyVAL.node = expr.NewClusureUse(expr.NewVariable(node.NewIdentifier(yyDollar[1].token)), false)
}
case 376:
yyDollar = yyS[yypt-2 : yypt+1]
//line parser/parser.y:925
{
yyVAL.node = expr.NewClusureUse(expr.NewVariable(yyDollar[2].token), true)
yyVAL.node = expr.NewClusureUse(expr.NewVariable(node.NewIdentifier(yyDollar[2].token)), true)
}
case 377:
yyDollar = yyS[yypt-2 : yypt+1]
@ -4647,19 +4647,19 @@ yydefault:
yyDollar = yyS[yypt-1 : yypt+1]
//line parser/parser.y:1040
{
yyVAL.node = node.NewSimpleNode("Variable").Attribute("name", yyDollar[1].token.String())
yyVAL.node = expr.NewVariable(node.NewIdentifier(yyDollar[1].token))
}
case 435:
yyDollar = yyS[yypt-4 : yypt+1]
//line parser/parser.y:1041
{
yyVAL.node = node.NewSimpleNode("Variable").Append(yyDollar[3].node)
yyVAL.node = expr.NewVariable(yyDollar[3].node)
}
case 436:
yyDollar = yyS[yypt-2 : yypt+1]
//line parser/parser.y:1042
{
yyVAL.node = node.NewSimpleNode("Variable").Append(yyDollar[2].node)
yyVAL.node = expr.NewVariable(yyDollar[2].node)
}
case 437:
yyDollar = yyS[yypt-3 : yypt+1]
@ -4847,37 +4847,37 @@ yydefault:
yyDollar = yyS[yypt-1 : yypt+1]
//line parser/parser.y:1124
{
yyVAL.node = node.NewSimpleNode("Variable").Attribute("value", yyDollar[1].token.String())
yyVAL.node = expr.NewVariable(node.NewIdentifier(yyDollar[1].token))
}
case 467:
yyDollar = yyS[yypt-4 : yypt+1]
//line parser/parser.y:1125
{
yyVAL.node = expr.NewArrayDimFetch(expr.NewVariable(yyDollar[1].token), yyDollar[3].node)
yyVAL.node = expr.NewArrayDimFetch(expr.NewVariable(node.NewIdentifier(yyDollar[1].token)), yyDollar[3].node)
}
case 468:
yyDollar = yyS[yypt-3 : yypt+1]
//line parser/parser.y:1126
{
yyVAL.node = expr.NewPropertyFetch(expr.NewVariable(yyDollar[1].token), node.NewIdentifier(yyDollar[3].token))
yyVAL.node = expr.NewPropertyFetch(expr.NewVariable(node.NewIdentifier(yyDollar[1].token)), node.NewIdentifier(yyDollar[3].token))
}
case 469:
yyDollar = yyS[yypt-3 : yypt+1]
//line parser/parser.y:1127
{
yyVAL.node = node.NewSimpleNode("Variable").Append(node.NewSimpleNode("expr").Append(yyDollar[2].node))
yyVAL.node = expr.NewVariable(yyDollar[2].node)
}
case 470:
yyDollar = yyS[yypt-3 : yypt+1]
//line parser/parser.y:1128
{
yyVAL.node = node.NewSimpleNode("Variable").Attribute("value", yyDollar[2].token.String())
yyVAL.node = expr.NewVariable(node.NewIdentifier(yyDollar[2].token))
}
case 471:
yyDollar = yyS[yypt-6 : yypt+1]
//line parser/parser.y:1130
{
yyVAL.node = expr.NewArrayDimFetch(expr.NewVariable(yyDollar[2].token), yyDollar[4].node)
yyVAL.node = expr.NewArrayDimFetch(expr.NewVariable(node.NewIdentifier(yyDollar[2].token)), yyDollar[4].node)
}
case 472:
yyDollar = yyS[yypt-3 : yypt+1]

View File

@ -412,7 +412,7 @@ statement:
catch_list:
/* empty */ { $$ = []node.Node{} }
| catch_list T_CATCH '(' catch_name_list T_VARIABLE ')' '{' inner_statement_list '}'
{ $$ = append($1, stmt.NewCatch($2, $4, expr.NewVariable($5), $8.(node.SimpleNode).Children)) }
{ $$ = append($1, stmt.NewCatch($2, $4, expr.NewVariable(node.NewIdentifier($5)), $8.(node.SimpleNode).Children)) }
;
catch_name_list:
name { $$ = []node.Node{$1} }
@ -921,8 +921,8 @@ lexical_var_list:
;
lexical_var:
T_VARIABLE { $$ = expr.NewClusureUse(expr.NewVariable($1), false) }
| '&' T_VARIABLE { $$ = expr.NewClusureUse(expr.NewVariable($2), true) }
T_VARIABLE { $$ = expr.NewClusureUse(expr.NewVariable(node.NewIdentifier($1)), false) }
| '&' T_VARIABLE { $$ = expr.NewClusureUse(expr.NewVariable(node.NewIdentifier($2)), true) }
;
function_call:
@ -1037,9 +1037,9 @@ variable:
;
simple_variable:
T_VARIABLE { $$ = node.NewSimpleNode("Variable").Attribute("name", $1.String()); }
| '$' '{' expr '}' { $$ = node.NewSimpleNode("Variable").Append($3); }
| '$' simple_variable { $$ = node.NewSimpleNode("Variable").Append($2); }
T_VARIABLE { $$ = expr.NewVariable(node.NewIdentifier($1)) }
| '$' '{' expr '}' { $$ = expr.NewVariable($3) }
| '$' simple_variable { $$ = expr.NewVariable($2) }
;
static_member:
@ -1121,13 +1121,13 @@ encaps_list:
;
encaps_var:
T_VARIABLE { $$ = node.NewSimpleNode("Variable").Attribute("value", $1.String()) }
| T_VARIABLE '[' encaps_var_offset ']' { $$ = expr.NewArrayDimFetch(expr.NewVariable($1), $3) }
| T_VARIABLE T_OBJECT_OPERATOR T_STRING { $$ = expr.NewPropertyFetch(expr.NewVariable($1), node.NewIdentifier($3)) }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = node.NewSimpleNode("Variable").Append(node.NewSimpleNode("expr").Append($2)) }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' { $$ = node.NewSimpleNode("Variable").Attribute("value", $2.String()) }
T_VARIABLE { $$ = expr.NewVariable(node.NewIdentifier($1)) }
| T_VARIABLE '[' encaps_var_offset ']' { $$ = expr.NewArrayDimFetch(expr.NewVariable(node.NewIdentifier($1)), $3) }
| T_VARIABLE T_OBJECT_OPERATOR T_STRING { $$ = expr.NewPropertyFetch(expr.NewVariable(node.NewIdentifier($1)), node.NewIdentifier($3)) }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = expr.NewVariable($2) }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' { $$ = expr.NewVariable(node.NewIdentifier($2)) }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
{ $$ = expr.NewArrayDimFetch(expr.NewVariable($2), $4) }
{ $$ = expr.NewArrayDimFetch(expr.NewVariable(node.NewIdentifier($2)), $4) }
| T_CURLY_OPEN variable '}' { $$ = $2; }
;
encaps_var_offset: