list short_list short_array nodes and fix array node

This commit is contained in:
z7zmey 2017-12-18 00:49:25 +02:00
parent 6d35bfff65
commit 661e8cc551
6 changed files with 714 additions and 591 deletions

View File

@ -13,22 +13,19 @@ type Array struct {
opentToken token.Token opentToken token.Token
closeToken token.Token closeToken token.Token
items []node.Node items []node.Node
isShortSyntax bool
} }
func NewArray(opentToken token.Token, closeToken token.Token, items []node.Node, isShortSyntax bool) node.Node { func NewArray(opentToken token.Token, closeToken token.Token, items []node.Node) node.Node {
return Array{ return Array{
node.SimpleNode{Name: "Array", Attributes: make(map[string]string)}, node.SimpleNode{Name: "Array", Attributes: make(map[string]string)},
opentToken, opentToken,
closeToken, closeToken,
items, items,
isShortSyntax,
} }
} }
func (n Array) Print(out io.Writer, indent string) { 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%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 { if n.items != nil {
fmt.Fprintf(out, "\n%vitems:", indent+" ") fmt.Fprintf(out, "\n%vitems:", indent+" ")

31
node/expr/list.go Normal file
View File

@ -0,0 +1,31 @@
package expr
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
)
type List struct {
node.SimpleNode
items []node.Node
}
func NewList(items []node.Node) node.Node {
return List{
node.SimpleNode{Name: "List", Attributes: make(map[string]string)},
items,
}
}
func (n List) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [- -]", indent, n.Name)
if n.items != nil {
fmt.Fprintf(out, "\n%vitems:", indent+" ")
for _, nn := range n.items {
nn.Print(out, indent+" ")
}
}
}

36
node/expr/short_array.go Normal file
View File

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

31
node/expr/short_list.go Normal file
View File

@ -0,0 +1,31 @@
package expr
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
)
type ShortList struct {
node.SimpleNode
items []node.Node
}
func NewShortList(items []node.Node) node.Node {
return ShortList{
node.SimpleNode{Name: "ShortList", Attributes: make(map[string]string)},
items,
}
}
func (n ShortList) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [- -]", indent, n.Name)
if n.items != nil {
fmt.Fprintf(out, "\n%vitems:", indent+" ")
for _, nn := range n.items {
nn.Print(out, indent+" ")
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -214,7 +214,7 @@ func Parse(src io.Reader, fName string) node.Node {
%type <node> non_empty_parameter_list argument_list non_empty_argument_list %type <node> non_empty_parameter_list argument_list non_empty_argument_list
%type <node> class_const_decl name_list method_body %type <node> class_const_decl name_list method_body
%type <node> ctor_arguments alt_if_stmt_without_else %type <node> ctor_arguments alt_if_stmt_without_else
%type <node> array_pair non_empty_array_pair_list array_pair_list possible_array_pair %type <node> array_pair possible_array_pair
%type <node> isset_variable type return_type type_expr %type <node> isset_variable type return_type type_expr
%type <node> variable_modifiers %type <node> variable_modifiers
@ -226,7 +226,8 @@ func Parse(src io.Reader, fName string) node.Node {
%type <list> const_list echo_expr_list for_exprs non_empty_for_exprs global_var_list %type <list> const_list echo_expr_list for_exprs non_empty_for_exprs global_var_list
%type <list> unprefixed_use_declarations inline_use_declarations property_list static_var_list %type <list> unprefixed_use_declarations inline_use_declarations property_list static_var_list
%type <list> switch_case_list case_list trait_adaptation_list trait_adaptations unset_variables %type <list> switch_case_list case_list trait_adaptation_list trait_adaptations unset_variables
%type <list> use_declarations lexical_var_list lexical_vars isset_variables %type <list> use_declarations lexical_var_list lexical_vars isset_variables non_empty_array_pair_list
%type <list> array_pair_list
%% %%
@ -493,8 +494,8 @@ implements_list:
foreach_variable: foreach_variable:
variable { $$ = $1; } variable { $$ = $1; }
| '&' variable { $$ = node.NewSimpleNode("Ref").Append($2); } | '&' variable { $$ = node.NewSimpleNode("Ref").Append($2); }
| T_LIST '(' array_pair_list ')' { $$ = node.NewSimpleNode("List").Append($3) } | T_LIST '(' array_pair_list ')' { $$ = expr.NewList($3) }
| '[' array_pair_list ']' { $$ = node.NewSimpleNode("ShortList").Append($2) } | '[' array_pair_list ']' { $$ = expr.NewShortList($2) }
; ;
for_statement: for_statement:
@ -812,8 +813,16 @@ new_expr:
; ;
expr_without_variable: expr_without_variable:
T_LIST '(' array_pair_list ')' '=' expr { $$ = node.NewSimpleNode("Assign").Append($3).Append($6); } T_LIST '(' array_pair_list ')' '=' expr
| '[' array_pair_list ']' '=' expr { $$ = node.NewSimpleNode("Assign").Append($2).Append($5); } {
list := expr.NewList($3)
$$ = assign_op.NewAssign(list, $6, false)
}
| '[' array_pair_list ']' '=' expr
{
shortList := expr.NewShortList($2)
$$ = assign_op.NewAssign(shortList, $5, false)
}
| variable '=' expr { $$ = assign_op.NewAssign($1, $3, false) } | variable '=' expr { $$ = assign_op.NewAssign($1, $3, false) }
| variable '=' '&' expr { $$ = assign_op.NewAssign($1, $4, true) } | variable '=' '&' expr { $$ = assign_op.NewAssign($1, $4, true) }
| T_CLONE expr { $$ = expr.NewClone($2) } | T_CLONE expr { $$ = expr.NewClone($2) }
@ -952,8 +961,8 @@ ctor_arguments:
; ;
dereferencable_scalar: dereferencable_scalar:
T_ARRAY '(' array_pair_list ')' { $$ = expr.NewArray($1, $4, $3.(node.SimpleNode).Children, false) } T_ARRAY '(' array_pair_list ')' { $$ = expr.NewArray($1, $4, $3) }
| '[' array_pair_list ']' { $$ = expr.NewArray($1, $3, $2.(node.SimpleNode).Children, true) } | '[' array_pair_list ']' { $$ = expr.NewShortArray($1, $3, $2) }
| T_CONSTANT_ENCAPSED_STRING { $$ = scalar.NewString($1) } | T_CONSTANT_ENCAPSED_STRING { $$ = scalar.NewString($1) }
; ;
@ -1064,18 +1073,25 @@ property_name:
; ;
array_pair_list: array_pair_list:
non_empty_array_pair_list { /* TODO: allow single trailing comma */ $$ = $1 } non_empty_array_pair_list
{
if ($1[len($1)-1] == nil) {
$$ = $1[:len($1)-1]
} else {
$$ = $1
}
}
; ;
possible_array_pair: possible_array_pair:
/* empty */ { $$ = node.NewSimpleNode("TODO: must be nil"); } /* empty */ { $$ = nil }
| array_pair { $$ = $1; } | array_pair { $$ = $1; }
; ;
non_empty_array_pair_list: non_empty_array_pair_list:
non_empty_array_pair_list ',' possible_array_pair non_empty_array_pair_list ',' possible_array_pair
{ $$ = $1.Append($3) } { $$ = append($1, $3) }
| possible_array_pair { $$ = node.NewSimpleNode("ArrayPairList").Append($1) } | possible_array_pair { $$ = []node.Node{$1} }
; ;
array_pair: array_pair:
@ -1085,12 +1101,15 @@ array_pair:
| '&' variable { $$ = expr.NewArrayItem(nil, $2, true) } | '&' variable { $$ = expr.NewArrayItem(nil, $2, true) }
| expr T_DOUBLE_ARROW T_LIST '(' array_pair_list ')' | expr T_DOUBLE_ARROW T_LIST '(' array_pair_list ')'
{ {
fmt.Println("\nHellow world\n") // TODO: Cannot use list() as standalone expression
$$ = expr.NewArrayItem($1, node.NewSimpleNode("List").Append($5), true) list := expr.NewList($5)
$$ = expr.NewArrayItem($1, list, false)
} }
| T_LIST '(' array_pair_list ')' | T_LIST '(' array_pair_list ')'
{ {
$$ = expr.NewArrayItem(nil, node.NewSimpleNode("List").Append($3), true) // TODO: Cannot use list() as standalone expression
list := expr.NewList($3)
$$ = expr.NewArrayItem(nil, list, false)
} }
; ;