[#82] array item unpack

This commit is contained in:
z7zmey 2019-12-29 22:42:52 +02:00
parent dc7aa7302d
commit d552681a7d
10 changed files with 982 additions and 863 deletions

View File

@ -13,14 +13,16 @@ type ArrayItem struct {
Position *position.Position
Key node.Node
Val node.Node
Unpack bool
}
// NewArrayItem node constructor
func NewArrayItem(Key node.Node, Val node.Node) *ArrayItem {
func NewArrayItem(Key node.Node, Val node.Node, Unpack bool) *ArrayItem {
return &ArrayItem{
FreeFloating: nil,
Key: Key,
Val: Val,
Unpack: Unpack,
}
}
@ -40,7 +42,9 @@ func (n *ArrayItem) GetFreeFloating() *freefloating.Collection {
// Attributes returns node attributes as map
func (n *ArrayItem) Attributes() map[string]interface{} {
return nil
return map[string]interface{}{
"Unpack": n.Unpack,
}
}
// Walk traverses nodes

View File

@ -218,3 +218,67 @@ func TestArrayItems(t *testing.T) {
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}
func TestArrayItemUnpack(t *testing.T) {
src := `<? array(...$b);`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 16,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 16,
},
Expr: &expr.Array{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 15,
},
Items: []node.Node{
&expr.ArrayItem{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 14,
},
Unpack: true,
Val: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
Value: "b",
},
},
},
},
},
},
},
}
php7parser := php7.NewParser([]byte(src), "7.4")
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}

View File

@ -33,9 +33,10 @@ var nodesToTest = []struct {
&expr.ArrayItem{
Key: &scalar.String{Value: "key"},
Val: &scalar.Lnumber{Value: "1"},
Unpack: true,
},
[]string{"Key", "Val"},
nil,
map[string]interface{}{"Unpack": true},
},
{
&expr.Array{

View File

@ -8469,7 +8469,7 @@ yydefault:
yyDollar = yyS[yypt-5 : yypt+1]
//line php5/php5.y:5919
{
arrayItem := expr.NewArrayItem(yyDollar[3].node, yyDollar[5].node)
arrayItem := expr.NewArrayItem(yyDollar[3].node, yyDollar[5].node, false)
yyVAL.list = append(yyDollar[1].list, arrayItem)
// save position
@ -8486,7 +8486,7 @@ yydefault:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:5934
{
arrayItem := expr.NewArrayItem(nil, yyDollar[3].node)
arrayItem := expr.NewArrayItem(nil, yyDollar[3].node, false)
yyVAL.list = append(yyDollar[1].list, arrayItem)
// save position
@ -8502,7 +8502,7 @@ yydefault:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:5948
{
arrayItem := expr.NewArrayItem(yyDollar[1].node, yyDollar[3].node)
arrayItem := expr.NewArrayItem(yyDollar[1].node, yyDollar[3].node, false)
yyVAL.list = []node.Node{arrayItem}
// save position
@ -8518,7 +8518,7 @@ yydefault:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:5962
{
arrayItem := expr.NewArrayItem(nil, yyDollar[1].node)
arrayItem := expr.NewArrayItem(nil, yyDollar[1].node, false)
yyVAL.list = []node.Node{arrayItem}
// save position
@ -9142,7 +9142,7 @@ yydefault:
//line php5/php5.y:6570
{
if len(yyDollar[1].list) == 0 {
yyDollar[1].list = []node.Node{expr.NewArrayItem(nil, nil)}
yyDollar[1].list = []node.Node{expr.NewArrayItem(nil, nil, false)}
}
yyVAL.list = append(yyDollar[1].list, yyDollar[3].node)
@ -9168,7 +9168,7 @@ yydefault:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:6597
{
yyVAL.node = expr.NewArrayItem(nil, yyDollar[1].node)
yyVAL.node = expr.NewArrayItem(nil, yyDollar[1].node, false)
// save position
yyVAL.node.SetPosition(yylex.(*Parser).positionBuilder.NewNodePosition(yyDollar[1].node))
@ -9183,7 +9183,7 @@ yydefault:
//line php5/php5.y:6609
{
listNode := expr.NewList(yyDollar[3].list)
yyVAL.node = expr.NewArrayItem(nil, listNode)
yyVAL.node = expr.NewArrayItem(nil, listNode, false)
// save position
listNode.SetPosition(yylex.(*Parser).positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
@ -9200,7 +9200,7 @@ yydefault:
yyDollar = yyS[yypt-0 : yypt+1]
//line php5/php5.y:6625
{
yyVAL.node = expr.NewArrayItem(nil, nil)
yyVAL.node = expr.NewArrayItem(nil, nil, false)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -9219,7 +9219,7 @@ yydefault:
yyVAL.list = yyDollar[1].list
if yyDollar[2].token != nil {
yyVAL.list = append(yyDollar[1].list, expr.NewArrayItem(nil, nil))
yyVAL.list = append(yyDollar[1].list, expr.NewArrayItem(nil, nil, false))
}
// save comments
@ -9233,7 +9233,7 @@ yydefault:
yyDollar = yyS[yypt-5 : yypt+1]
//line php5/php5.y:6659
{
arrayItem := expr.NewArrayItem(yyDollar[3].node, yyDollar[5].node)
arrayItem := expr.NewArrayItem(yyDollar[3].node, yyDollar[5].node, false)
yyVAL.list = append(yyDollar[1].list, arrayItem)
// save position
@ -9250,7 +9250,7 @@ yydefault:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:6674
{
arrayItem := expr.NewArrayItem(nil, yyDollar[3].node)
arrayItem := expr.NewArrayItem(nil, yyDollar[3].node, false)
yyVAL.list = append(yyDollar[1].list, arrayItem)
// save position
@ -9266,7 +9266,7 @@ yydefault:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:6688
{
arrayItem := expr.NewArrayItem(yyDollar[1].node, yyDollar[3].node)
arrayItem := expr.NewArrayItem(yyDollar[1].node, yyDollar[3].node, false)
yyVAL.list = []node.Node{arrayItem}
// save position
@ -9282,7 +9282,7 @@ yydefault:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:6702
{
arrayItem := expr.NewArrayItem(nil, yyDollar[1].node)
arrayItem := expr.NewArrayItem(nil, yyDollar[1].node, false)
yyVAL.list = []node.Node{arrayItem}
// save position
@ -9298,7 +9298,7 @@ yydefault:
//line php5/php5.y:6715
{
reference := expr.NewReference(yyDollar[6].node)
arrayItem := expr.NewArrayItem(yyDollar[3].node, reference)
arrayItem := expr.NewArrayItem(yyDollar[3].node, reference, false)
yyVAL.list = append(yyDollar[1].list, arrayItem)
// save position
@ -9318,7 +9318,7 @@ yydefault:
//line php5/php5.y:6733
{
reference := expr.NewReference(yyDollar[4].node)
arrayItem := expr.NewArrayItem(nil, reference)
arrayItem := expr.NewArrayItem(nil, reference, false)
yyVAL.list = append(yyDollar[1].list, arrayItem)
// save position
@ -9336,7 +9336,7 @@ yydefault:
//line php5/php5.y:6749
{
reference := expr.NewReference(yyDollar[4].node)
arrayItem := expr.NewArrayItem(yyDollar[1].node, reference)
arrayItem := expr.NewArrayItem(yyDollar[1].node, reference, false)
yyVAL.list = []node.Node{arrayItem}
// save position
@ -9355,7 +9355,7 @@ yydefault:
//line php5/php5.y:6766
{
reference := expr.NewReference(yyDollar[2].node)
arrayItem := expr.NewArrayItem(nil, reference)
arrayItem := expr.NewArrayItem(nil, reference, false)
yyVAL.list = []node.Node{arrayItem}
// save position

View File

@ -5917,7 +5917,7 @@ possible_comma:
non_empty_static_array_pair_list:
non_empty_static_array_pair_list ',' static_scalar_value T_DOUBLE_ARROW static_scalar_value
{
arrayItem := expr.NewArrayItem($3, $5)
arrayItem := expr.NewArrayItem($3, $5, false)
$$ = append($1, arrayItem)
// save position
@ -5932,7 +5932,7 @@ non_empty_static_array_pair_list:
}
| non_empty_static_array_pair_list ',' static_scalar_value
{
arrayItem := expr.NewArrayItem(nil, $3)
arrayItem := expr.NewArrayItem(nil, $3, false)
$$ = append($1, arrayItem)
// save position
@ -5946,7 +5946,7 @@ non_empty_static_array_pair_list:
}
| static_scalar_value T_DOUBLE_ARROW static_scalar_value
{
arrayItem := expr.NewArrayItem($1, $3)
arrayItem := expr.NewArrayItem($1, $3, false)
$$ = []node.Node{arrayItem}
// save position
@ -5960,7 +5960,7 @@ non_empty_static_array_pair_list:
}
| static_scalar_value
{
arrayItem := expr.NewArrayItem(nil, $1)
arrayItem := expr.NewArrayItem(nil, $1, false)
$$ = []node.Node{arrayItem}
// save position
@ -6569,7 +6569,7 @@ assignment_list:
assignment_list ',' assignment_list_element
{
if len($1) == 0 {
$1 = []node.Node{expr.NewArrayItem(nil, nil)}
$1 = []node.Node{expr.NewArrayItem(nil, nil, false)}
}
$$ = append($1, $3)
@ -6595,7 +6595,7 @@ assignment_list:
assignment_list_element:
variable
{
$$ = expr.NewArrayItem(nil, $1)
$$ = expr.NewArrayItem(nil, $1, false)
// save position
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodePosition($1))
@ -6608,7 +6608,7 @@ assignment_list_element:
| T_LIST '(' assignment_list ')'
{
listNode := expr.NewList($3)
$$ = expr.NewArrayItem(nil, listNode)
$$ = expr.NewArrayItem(nil, listNode, false)
// save position
listNode.SetPosition(yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4))
@ -6623,7 +6623,7 @@ assignment_list_element:
}
| /* empty */
{
$$ = expr.NewArrayItem(nil, nil)
$$ = expr.NewArrayItem(nil, nil, false)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -6642,7 +6642,7 @@ array_pair_list:
$$ = $1
if $2 != nil {
$$ = append($1, expr.NewArrayItem(nil, nil))
$$ = append($1, expr.NewArrayItem(nil, nil, false))
}
// save comments
@ -6657,7 +6657,7 @@ array_pair_list:
non_empty_array_pair_list:
non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr
{
arrayItem := expr.NewArrayItem($3, $5)
arrayItem := expr.NewArrayItem($3, $5, false)
$$ = append($1, arrayItem)
// save position
@ -6672,7 +6672,7 @@ non_empty_array_pair_list:
}
| non_empty_array_pair_list ',' expr
{
arrayItem := expr.NewArrayItem(nil, $3)
arrayItem := expr.NewArrayItem(nil, $3, false)
$$ = append($1, arrayItem)
// save position
@ -6686,7 +6686,7 @@ non_empty_array_pair_list:
}
| expr T_DOUBLE_ARROW expr
{
arrayItem := expr.NewArrayItem($1, $3)
arrayItem := expr.NewArrayItem($1, $3, false)
$$ = []node.Node{arrayItem}
// save position
@ -6700,7 +6700,7 @@ non_empty_array_pair_list:
}
| expr
{
arrayItem := expr.NewArrayItem(nil, $1)
arrayItem := expr.NewArrayItem(nil, $1, false)
$$ = []node.Node{arrayItem}
// save position
@ -6714,7 +6714,7 @@ non_empty_array_pair_list:
| non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable
{
reference := expr.NewReference($6)
arrayItem := expr.NewArrayItem($3, reference)
arrayItem := expr.NewArrayItem($3, reference, false)
$$ = append($1, arrayItem)
// save position
@ -6732,7 +6732,7 @@ non_empty_array_pair_list:
| non_empty_array_pair_list ',' '&' w_variable
{
reference := expr.NewReference($4)
arrayItem := expr.NewArrayItem(nil, reference)
arrayItem := expr.NewArrayItem(nil, reference, false)
$$ = append($1, arrayItem)
// save position
@ -6748,7 +6748,7 @@ non_empty_array_pair_list:
| expr T_DOUBLE_ARROW '&' w_variable
{
reference := expr.NewReference($4)
arrayItem := expr.NewArrayItem($1, reference)
arrayItem := expr.NewArrayItem($1, reference, false)
$$ = []node.Node{arrayItem}
// save position
@ -6765,7 +6765,7 @@ non_empty_array_pair_list:
| '&' w_variable
{
reference := expr.NewReference($2)
arrayItem := expr.NewArrayItem(nil, reference)
arrayItem := expr.NewArrayItem(nil, reference, false)
$$ = []node.Node{arrayItem}
// save position

File diff suppressed because it is too large Load Diff

View File

@ -5162,7 +5162,7 @@ array_pair_list:
possible_array_pair:
/* empty */
{
$$ = expr.NewArrayItem(nil, nil)
$$ = expr.NewArrayItem(nil, nil, false)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -5178,7 +5178,7 @@ non_empty_array_pair_list:
non_empty_array_pair_list ',' possible_array_pair
{
if len($1) == 0 {
$1 = []node.Node{expr.NewArrayItem(nil, nil)}
$1 = []node.Node{expr.NewArrayItem(nil, nil, false)}
}
$$ = append($1, $3)
@ -5203,7 +5203,7 @@ non_empty_array_pair_list:
array_pair:
expr T_DOUBLE_ARROW expr
{
$$ = expr.NewArrayItem($1, $3)
$$ = expr.NewArrayItem($1, $3, false)
// save position
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodesPosition($1, $3))
@ -5216,7 +5216,7 @@ array_pair:
}
| expr
{
$$ = expr.NewArrayItem(nil, $1)
$$ = expr.NewArrayItem(nil, $1, false)
// save position
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodePosition($1))
@ -5229,7 +5229,7 @@ array_pair:
| expr T_DOUBLE_ARROW '&' variable
{
reference := expr.NewReference($4)
$$ = expr.NewArrayItem($1, reference)
$$ = expr.NewArrayItem($1, reference, false)
// save position
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodesPosition($1, $4))
@ -5245,7 +5245,7 @@ array_pair:
| '&' variable
{
reference := expr.NewReference($2)
$$ = expr.NewArrayItem(nil, reference)
$$ = expr.NewArrayItem(nil, reference, false)
// save position
$$.SetPosition(yylex.(*Parser).positionBuilder.NewTokenNodePosition($1, $2))
@ -5254,13 +5254,25 @@ array_pair:
// save comments
yylex.(*Parser).setFreeFloating($$, freefloating.Start, $1.FreeFloating)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
| T_ELLIPSIS expr
{
$$ = expr.NewArrayItem(nil, $2, true)
// save position
$$.SetPosition(yylex.(*Parser).positionBuilder.NewTokenNodePosition($1, $2))
// save comments
yylex.(*Parser).setFreeFloating($$, freefloating.Start, $1.FreeFloating)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
| expr T_DOUBLE_ARROW T_LIST '(' array_pair_list ')'
{
// TODO: Cannot use list() as standalone expression
listNode := expr.NewList($5)
$$ = expr.NewArrayItem($1, listNode)
$$ = expr.NewArrayItem($1, listNode, false)
// save position
listNode.SetPosition(yylex.(*Parser).positionBuilder.NewTokensPosition($3, $6))
@ -5279,7 +5291,7 @@ array_pair:
{
// TODO: Cannot use list() as standalone expression
listNode := expr.NewList($3)
$$ = expr.NewArrayItem(nil, listNode)
$$ = expr.NewArrayItem(nil, listNode, false)
// save position
listNode.SetPosition(yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4))

View File

@ -1304,6 +1304,10 @@ func (p *Printer) printExprArrayItem(n node.Node) {
nn := n.(*expr.ArrayItem)
p.printFreeFloating(nn, freefloating.Start)
if nn.Unpack {
io.WriteString(p.w, "...")
}
if nn.Key != nil {
p.Print(nn.Key)
p.printFreeFloating(nn, freefloating.Expr)

View File

@ -384,6 +384,7 @@ func TestParseAndPrintArrayItem(t *testing.T) {
$world ,
& $world ,
'Hello' => $world ,
... $unpack
] ;
`

View File

@ -1551,6 +1551,25 @@ func TestPrinterPrintExprArrayItem(t *testing.T) {
}
}
func TestPrinterPrintExprArrayItemUnpack(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&expr.ArrayItem{
Unpack: true,
Val: &expr.Variable{
VarName: &node.Identifier{Value: "world"},
},
})
expected := `...$world`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprArray(t *testing.T) {
o := bytes.NewBufferString("")