array and array_dim_fetch nodes

This commit is contained in:
z7zmey 2017-12-16 20:19:17 +02:00
parent d7a593ef3b
commit 9d17cc7477
4 changed files with 586 additions and 507 deletions

39
node/expr/array.go Normal file
View File

@ -0,0 +1,39 @@
package expr
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Array struct {
node.SimpleNode
opentToken token.Token
closeToken token.Token
items []node.Node
isShortSyntax bool
}
func NewArray(opentToken token.Token, closeToken token.Token, items []node.Node, isShortSyntax bool) node.Node {
return Array{
node.SimpleNode{Name: "Array", Attributes: make(map[string]string)},
opentToken,
closeToken,
items,
isShortSyntax,
}
}
func (n Array) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [%d %d]", indent, n.Name, n.opentToken.StartLine, n.closeToken.EndLine)
fmt.Fprintf(out, "\n%visShortSyntax: %t", indent+" ", n.isShortSyntax)
if n.items != nil {
fmt.Fprintf(out, "\n%vitems:", indent+" ")
for _, nn := range n.items {
nn.Print(out, indent+" ")
}
}
}

View File

@ -0,0 +1,36 @@
package expr
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
)
type ArrayDimFetch struct {
node.SimpleNode
variable node.Node
dim node.Node
}
func NewArrayDimFetch(variable node.Node, dim node.Node) node.Node {
return ArrayDimFetch{
node.SimpleNode{Name: "ArrayDimFetch", Attributes: make(map[string]string)},
variable,
dim,
}
}
func (n ArrayDimFetch) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [- -]", indent, n.Name)
if n.variable != nil {
fmt.Fprintf(out, "\n%vvariable:", indent+" ")
n.variable.Print(out, indent+" ")
}
if n.dim != nil {
fmt.Fprintf(out, "\n%vdim:", indent+" ")
n.dim.Print(out, indent+" ")
}
}

File diff suppressed because it is too large Load Diff

View File

@ -175,6 +175,10 @@ func Parse(src io.Reader, fName string) node.Node {
%token <token> '{' %token <token> '{'
%token <token> '}' %token <token> '}'
%token <token> ';' %token <token> ';'
%token <token> '('
%token <token> ')'
%token <token> '['
%token <token> ']'
%type <value> is_reference %type <value> is_reference
%type <value> is_variadic %type <value> is_variadic
@ -958,8 +962,8 @@ ctor_arguments:
; ;
dereferencable_scalar: dereferencable_scalar:
T_ARRAY '(' array_pair_list ')' { $$ = $3; } T_ARRAY '(' array_pair_list ')' { $$ = expr.NewArray($1, $4, $3.(node.SimpleNode).Children, false) }
| '[' array_pair_list ']' { $$ = $2; } | '[' array_pair_list ']' { $$ = expr.NewArray($1, $3, $2.(node.SimpleNode).Children, true) }
| T_CONSTANT_ENCAPSED_STRING { $$ = scalar.NewString($1) } | T_CONSTANT_ENCAPSED_STRING { $$ = scalar.NewString($1) }
; ;
@ -997,7 +1001,7 @@ expr:
; ;
optional_expr: optional_expr:
/* empty */ { $$ = node.NewSimpleNode("") } /* empty */ { $$ = node.NewSimpleNode("optional node. TODO: must be nil") }
| expr { $$ = $1; } | expr { $$ = $1; }
; ;
@ -1019,9 +1023,9 @@ callable_expr:
callable_variable: callable_variable:
simple_variable { $$ = $1; } simple_variable { $$ = $1; }
| dereferencable '[' optional_expr ']' { $$ = node.NewSimpleNode("Dim").Append($1).Append($3)} | dereferencable '[' optional_expr ']' { $$ = expr.NewArrayDimFetch($1, $3) }
| constant '[' optional_expr ']' { $$ = node.NewSimpleNode("Dim").Append($1).Append($3)} | constant '[' optional_expr ']' { $$ = expr.NewArrayDimFetch($1, $3) }
| dereferencable '{' expr '}' { $$ = node.NewSimpleNode("Dim").Append($1).Append($3)} | dereferencable '{' expr '}' { $$ = expr.NewArrayDimFetch($1, $3) }
| dereferencable T_OBJECT_OPERATOR property_name argument_list | dereferencable T_OBJECT_OPERATOR property_name argument_list
{ $$ = node.NewSimpleNode("MethodCall").Append($1).Append($3).Append($4)} { $$ = node.NewSimpleNode("MethodCall").Append($1).Append($3).Append($4)}
| function_call { $$ = $1; } | function_call { $$ = $1; }
@ -1048,8 +1052,8 @@ static_member:
new_variable: new_variable:
simple_variable { $$ = $1 } simple_variable { $$ = $1 }
| new_variable '[' optional_expr ']' { $$ = node.NewSimpleNode("Dim").Append($1).Append($3) } | new_variable '[' optional_expr ']' { $$ = expr.NewArrayDimFetch($1, $3) }
| new_variable '{' expr '}' { $$ = node.NewSimpleNode("Dim").Append($1).Append($3) } | new_variable '{' expr '}' { $$ = expr.NewArrayDimFetch($1, $3) }
| new_variable T_OBJECT_OPERATOR property_name { $$ = node.NewSimpleNode("Property").Append($1).Append($3) } | new_variable T_OBJECT_OPERATOR property_name { $$ = node.NewSimpleNode("Property").Append($1).Append($3) }
| class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable | class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable
{ $$ = node.NewSimpleNode("StaticProperty").Append($1).Append($3) } { $$ = node.NewSimpleNode("StaticProperty").Append($1).Append($3) }
@ -1111,12 +1115,12 @@ encaps_list:
encaps_var: encaps_var:
T_VARIABLE { $$ = node.NewSimpleNode("Variable").Attribute("value", $1.String()) } T_VARIABLE { $$ = node.NewSimpleNode("Variable").Attribute("value", $1.String()) }
| T_VARIABLE '[' encaps_var_offset ']' { $$ = node.NewSimpleNode("Variable").Attribute("value", $1.String()).Append(node.NewSimpleNode("offset").Append($3)) } | T_VARIABLE '[' encaps_var_offset ']' { $$ = expr.NewArrayDimFetch(expr.NewVariable($1), $3) }
| T_VARIABLE T_OBJECT_OPERATOR T_STRING { $$ = node.NewSimpleNode("Variable").Attribute("value", $1.String()).Append(node.NewSimpleNode("property").Attribute("value", $3.String())) } | T_VARIABLE T_OBJECT_OPERATOR T_STRING { $$ = node.NewSimpleNode("Variable").Attribute("value", $1.String()).Append(node.NewSimpleNode("property").Attribute("value", $3.String())) }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = node.NewSimpleNode("Variable").Append(node.NewSimpleNode("expr").Append($2)) } | 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_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' { $$ = node.NewSimpleNode("Variable").Attribute("value", $2.String()) }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}' | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
{ $$ = node.NewSimpleNode("Variable").Attribute("value", $2.String()).Append(node.NewSimpleNode("offset").Append($4)) } { $$ = expr.NewArrayDimFetch(expr.NewVariable($2), $4) }
| T_CURLY_OPEN variable '}' { $$ = $2; } | T_CURLY_OPEN variable '}' { $$ = $2; }
; ;
encaps_var_offset: encaps_var_offset: