List tests

This commit is contained in:
z7zmey 2018-02-10 13:25:08 +02:00
parent 9b2650c906
commit 322acdcda4
3 changed files with 163 additions and 47 deletions

108
node/expr/t_list_test.go Normal file
View File

@ -0,0 +1,108 @@
package expr_test
import (
"bytes"
"testing"
"github.com/z7zmey/php-parser/node/expr/assign_op"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7"
)
func TestList(t *testing.T) {
src := `<? list($a) = $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Assign{
Variable: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestListArrayIndex(t *testing.T) {
src := `<? list($a[]) = $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Assign{
Variable: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.ArrayDimFetch{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestListList(t *testing.T) {
src := `<? list(list($a)) = $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Assign{
Variable: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
},
},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}

View File

@ -345,7 +345,7 @@ const yyEofCode = 1
const yyErrCode = 2
const yyInitialStackSize = 16
//line php5/php5.y:3756
//line php5/php5.y:3760
//line yacctab:1
var yyExca = [...]int{
@ -6282,31 +6282,35 @@ yydefault:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3442
{
yyVAL.node = expr.NewList(yyDollar[3].list)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
comments.AddComments(yyVAL.node, yyDollar[1].token.Comments())
item := expr.NewList(yyDollar[3].list)
positions.AddPosition(item, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
comments.AddComments(item, yyDollar[1].token.Comments())
yyVAL.node = expr.NewArrayItem(nil, item, false)
positions.AddPosition(yyVAL.node, positionBuilder.NewNodePosition(item))
comments.AddComments(yyVAL.node, comments[item])
}
case 478:
yyDollar = yyS[yypt-0 : yypt+1]
//line php5/php5.y:3448
//line php5/php5.y:3452
{
yyVAL.node = nil
}
case 479:
yyDollar = yyS[yypt-0 : yypt+1]
//line php5/php5.y:3454
//line php5/php5.y:3458
{
yyVAL.list = []node.Node{}
}
case 480:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3456
//line php5/php5.y:3460
{
yyVAL.list = yyDollar[1].list
}
case 481:
yyDollar = yyS[yypt-5 : yypt+1]
//line php5/php5.y:3461
//line php5/php5.y:3465
{
arrayItem := expr.NewArrayItem(yyDollar[3].node, yyDollar[5].node, false)
positions.AddPosition(arrayItem, positionBuilder.NewNodesPosition(yyDollar[3].node, yyDollar[5].node))
@ -6316,7 +6320,7 @@ yydefault:
}
case 482:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3469
//line php5/php5.y:3473
{
arrayItem := expr.NewArrayItem(nil, yyDollar[3].node, false)
positions.AddPosition(arrayItem, positionBuilder.NewNodePosition(yyDollar[3].node))
@ -6326,7 +6330,7 @@ yydefault:
}
case 483:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3477
//line php5/php5.y:3481
{
arrayItem := expr.NewArrayItem(yyDollar[1].node, yyDollar[3].node, false)
positions.AddPosition(arrayItem, positionBuilder.NewNodesPosition(yyDollar[1].node, yyDollar[3].node))
@ -6336,7 +6340,7 @@ yydefault:
}
case 484:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3485
//line php5/php5.y:3489
{
arrayItem := expr.NewArrayItem(nil, yyDollar[1].node, false)
positions.AddPosition(arrayItem, positionBuilder.NewNodePosition(yyDollar[1].node))
@ -6346,7 +6350,7 @@ yydefault:
}
case 485:
yyDollar = yyS[yypt-6 : yypt+1]
//line php5/php5.y:3493
//line php5/php5.y:3497
{
arrayItem := expr.NewArrayItem(yyDollar[3].node, yyDollar[6].node, true)
positions.AddPosition(arrayItem, positionBuilder.NewNodesPosition(yyDollar[3].node, yyDollar[6].node))
@ -6356,7 +6360,7 @@ yydefault:
}
case 486:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3501
//line php5/php5.y:3505
{
arrayItem := expr.NewArrayItem(nil, yyDollar[4].node, true)
positions.AddPosition(arrayItem, positionBuilder.NewTokenNodePosition(yyDollar[3].token, yyDollar[4].node))
@ -6366,7 +6370,7 @@ yydefault:
}
case 487:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3509
//line php5/php5.y:3513
{
arrayItem := expr.NewArrayItem(yyDollar[1].node, yyDollar[4].node, true)
positions.AddPosition(arrayItem, positionBuilder.NewNodesPosition(yyDollar[1].node, yyDollar[4].node))
@ -6376,7 +6380,7 @@ yydefault:
}
case 488:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3517
//line php5/php5.y:3521
{
arrayItem := expr.NewArrayItem(nil, yyDollar[2].node, true)
positions.AddPosition(arrayItem, positionBuilder.NewTokenNodePosition(yyDollar[1].token, yyDollar[2].node))
@ -6386,13 +6390,13 @@ yydefault:
}
case 489:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3528
//line php5/php5.y:3532
{
yyVAL.list = append(yyDollar[1].list, yyDollar[2].node)
}
case 490:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3530
//line php5/php5.y:3534
{
encapsed := scalar.NewEncapsedStringPart(yyDollar[2].token.Value)
positions.AddPosition(encapsed, positionBuilder.NewTokenPosition(yyDollar[2].token))
@ -6401,13 +6405,13 @@ yydefault:
}
case 491:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3537
//line php5/php5.y:3541
{
yyVAL.list = []node.Node{yyDollar[1].node}
}
case 492:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3539
//line php5/php5.y:3543
{
encapsed := scalar.NewEncapsedStringPart(yyDollar[1].token.Value)
positions.AddPosition(encapsed, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6416,7 +6420,7 @@ yydefault:
}
case 493:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3549
//line php5/php5.y:3553
{
name := node.NewIdentifier(yyDollar[1].token.Value)
positions.AddPosition(name, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6428,7 +6432,7 @@ yydefault:
}
case 494:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3559
//line php5/php5.y:3563
{
identifier := node.NewIdentifier(yyDollar[1].token.Value)
positions.AddPosition(identifier, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6443,7 +6447,7 @@ yydefault:
}
case 495:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3572
//line php5/php5.y:3576
{
identifier := node.NewIdentifier(yyDollar[1].token.Value)
positions.AddPosition(identifier, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6461,7 +6465,7 @@ yydefault:
}
case 496:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3588
//line php5/php5.y:3592
{
yyVAL.node = yyDollar[2].node
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[3].token))
@ -6469,7 +6473,7 @@ yydefault:
}
case 497:
yyDollar = yyS[yypt-6 : yypt+1]
//line php5/php5.y:3594
//line php5/php5.y:3598
{
identifier := node.NewIdentifier(yyDollar[2].token.Value)
positions.AddPosition(identifier, positionBuilder.NewTokenPosition(yyDollar[2].token))
@ -6484,13 +6488,13 @@ yydefault:
}
case 498:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3608
//line php5/php5.y:3612
{
yyVAL.node = yyDollar[2].node
}
case 499:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3613
//line php5/php5.y:3617
{
yyVAL.node = scalar.NewString(yyDollar[1].token.Value)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6498,7 +6502,7 @@ yydefault:
}
case 500:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3619
//line php5/php5.y:3623
{
// TODO: add option to handle 64 bit integer
if _, err := strconv.Atoi(yyDollar[1].token.Value); err == nil {
@ -6512,7 +6516,7 @@ yydefault:
}
case 501:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3631
//line php5/php5.y:3635
{
identifier := node.NewIdentifier(yyDollar[1].token.Value)
positions.AddPosition(identifier, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6524,7 +6528,7 @@ yydefault:
}
case 502:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3644
//line php5/php5.y:3648
{
yyVAL.node = expr.NewIsset(yyDollar[3].list)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
@ -6532,7 +6536,7 @@ yydefault:
}
case 503:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3650
//line php5/php5.y:3654
{
yyVAL.node = expr.NewEmpty(yyDollar[3].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
@ -6540,7 +6544,7 @@ yydefault:
}
case 504:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3656
//line php5/php5.y:3660
{
yyVAL.node = expr.NewEmpty(yyDollar[3].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
@ -6548,7 +6552,7 @@ yydefault:
}
case 505:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3662
//line php5/php5.y:3666
{
yyVAL.node = expr.NewInclude(yyDollar[2].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokenNodePosition(yyDollar[1].token, yyDollar[2].node))
@ -6556,7 +6560,7 @@ yydefault:
}
case 506:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3668
//line php5/php5.y:3672
{
yyVAL.node = expr.NewIncludeOnce(yyDollar[2].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokenNodePosition(yyDollar[1].token, yyDollar[2].node))
@ -6564,7 +6568,7 @@ yydefault:
}
case 507:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3674
//line php5/php5.y:3678
{
yyVAL.node = expr.NewEval(yyDollar[3].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
@ -6572,7 +6576,7 @@ yydefault:
}
case 508:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3680
//line php5/php5.y:3684
{
yyVAL.node = expr.NewRequire(yyDollar[2].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokenNodePosition(yyDollar[1].token, yyDollar[2].node))
@ -6580,7 +6584,7 @@ yydefault:
}
case 509:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3686
//line php5/php5.y:3690
{
yyVAL.node = expr.NewRequireOnce(yyDollar[2].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokenNodePosition(yyDollar[1].token, yyDollar[2].node))
@ -6588,31 +6592,31 @@ yydefault:
}
case 510:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3695
//line php5/php5.y:3699
{
yyVAL.list = []node.Node{yyDollar[1].node}
}
case 511:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3697
//line php5/php5.y:3701
{
yyVAL.list = append(yyDollar[1].list, yyDollar[3].node)
}
case 512:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3702
//line php5/php5.y:3706
{
yyVAL.node = yyDollar[1].node
}
case 513:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3704
//line php5/php5.y:3708
{
yyVAL.node = yyDollar[1].node
}
case 514:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3709
//line php5/php5.y:3713
{
target := node.NewIdentifier(yyDollar[3].token.Value)
positions.AddPosition(target, positionBuilder.NewTokenPosition(yyDollar[3].token))
@ -6624,7 +6628,7 @@ yydefault:
}
case 515:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3719
//line php5/php5.y:3723
{
target := node.NewIdentifier(yyDollar[3].token.Value)
positions.AddPosition(target, positionBuilder.NewTokenPosition(yyDollar[3].token))
@ -6636,7 +6640,7 @@ yydefault:
}
case 516:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3732
//line php5/php5.y:3736
{
target := node.NewIdentifier(yyDollar[3].token.Value)
positions.AddPosition(target, positionBuilder.NewTokenPosition(yyDollar[3].token))
@ -6648,7 +6652,7 @@ yydefault:
}
case 517:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3745
//line php5/php5.y:3749
{
target := node.NewIdentifier(yyDollar[3].token.Value)
positions.AddPosition(target, positionBuilder.NewTokenPosition(yyDollar[3].token))

View File

@ -3441,9 +3441,13 @@ assignment_list_element:
}
| T_LIST '(' assignment_list ')'
{
$$ = expr.NewList($3)
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $4))
comments.AddComments($$, $1.Comments())
item := expr.NewList($3)
positions.AddPosition(item, positionBuilder.NewTokensPosition($1, $4))
comments.AddComments(item, $1.Comments())
$$ = expr.NewArrayItem(nil, item, false)
positions.AddPosition($$, positionBuilder.NewNodePosition(item))
comments.AddComments($$, comments[item])
}
| /* empty */
{ $$ = nil }