refactoring: update ast structure of "ArrayItem" node

This commit is contained in:
Vadym Slizov 2020-12-26 20:13:50 +02:00
parent 8bf1fa822d
commit 0f5f5e7dc7
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
10 changed files with 149 additions and 174 deletions

View File

@ -30786,61 +30786,53 @@ func TestExprArray_Items(t *testing.T) {
StartPos: 15,
EndPos: 18,
},
Val: &ast.ExprReference{
AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 18,
EndPos: 16,
},
AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 16,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 15,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 15,
},
},
},
Var: &ast.ExprVariable{
},
Val: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
VarName: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
VarName: &ast.Identifier{
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$b"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$b"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
},
Value: []byte("$b"),
},
Value: []byte("$b"),
},
},
},
@ -39025,61 +39017,53 @@ func TestExprShortArray_Items(t *testing.T) {
StartPos: 10,
EndPos: 13,
},
Val: &ast.ExprReference{
AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 13,
EndPos: 11,
},
AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 11,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 10,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 10,
},
},
},
Var: &ast.ExprVariable{
},
Val: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 13,
},
VarName: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 13,
},
VarName: &ast.Identifier{
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$b"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 13,
},
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$b"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 13,
},
},
Value: []byte("$b"),
},
Value: []byte("$b"),
},
},
},

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -5255,14 +5255,11 @@ non_empty_array_pair_list:
| non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable
{
arrayItem := &ast.ExprArrayItem{
Position: yylex.(*Parser).builder.NewNodesPosition($3, $6),
Position: yylex.(*Parser).builder.NewNodesPosition($3, $6),
Key: $3,
DoubleArrowTkn: $4,
Val: &ast.ExprReference{
Position: yylex.(*Parser).builder.NewTokenNodePosition($5, $6),
AmpersandTkn: $5,
Var: $6,
},
AmpersandTkn: $5,
Val: $6,
}
$1.(*ast.ParserSeparatedList).SeparatorTkns = append($1.(*ast.ParserSeparatedList).SeparatorTkns, $2)
@ -5273,12 +5270,9 @@ non_empty_array_pair_list:
| non_empty_array_pair_list ',' '&' w_variable
{
arrayItem := &ast.ExprArrayItem{
Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4),
Val: &ast.ExprReference{
Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4),
AmpersandTkn: $3,
Var: $4,
},
Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4),
AmpersandTkn: $3,
Val: $4,
}
$1.(*ast.ParserSeparatedList).SeparatorTkns = append($1.(*ast.ParserSeparatedList).SeparatorTkns, $2)
@ -5291,14 +5285,11 @@ non_empty_array_pair_list:
$$ = &ast.ParserSeparatedList{
Items: []ast.Vertex{
&ast.ExprArrayItem{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Key: $1,
DoubleArrowTkn: $2,
Val: &ast.ExprReference{
Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4),
AmpersandTkn: $3,
Var: $4,
},
AmpersandTkn: $3,
Val: $4,
},
},
}
@ -5308,12 +5299,9 @@ non_empty_array_pair_list:
$$ = &ast.ParserSeparatedList{
Items: []ast.Vertex{
&ast.ExprArrayItem{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
Val: &ast.ExprReference{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
AmpersandTkn: $1,
Var: $2,
},
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
AmpersandTkn: $1,
Val: $2,
},
},
}

View File

@ -34718,61 +34718,53 @@ func TestExprArray_Items(t *testing.T) {
StartPos: 15,
EndPos: 18,
},
Val: &ast.ExprReference{
AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 18,
EndPos: 16,
},
AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 16,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 15,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 15,
},
},
},
Var: &ast.ExprVariable{
},
Val: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
VarName: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
VarName: &ast.Identifier{
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$b"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$b"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
},
Value: []byte("$b"),
},
Value: []byte("$b"),
},
},
},
@ -43983,61 +43975,53 @@ func TestExprShortArray_Items(t *testing.T) {
StartPos: 10,
EndPos: 13,
},
Val: &ast.ExprReference{
AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 13,
EndPos: 11,
},
AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 11,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 10,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 10,
},
},
},
Var: &ast.ExprVariable{
},
Val: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 13,
},
VarName: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 13,
},
VarName: &ast.Identifier{
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$b"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 13,
},
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$b"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 13,
},
},
Value: []byte("$b"),
},
Value: []byte("$b"),
},
},
},

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -3994,25 +3994,19 @@ array_pair:
| expr T_DOUBLE_ARROW '&' variable
{
$$ = &ast.ExprArrayItem{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Key: $1,
DoubleArrowTkn: $2,
Val: &ast.ExprReference{
Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4),
AmpersandTkn: $3,
Var: $4,
},
AmpersandTkn: $3,
Val: $4,
}
}
| '&' variable
{
$$ = &ast.ExprArrayItem{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
Val: &ast.ExprReference{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
AmpersandTkn: $1,
Var: $2,
},
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
AmpersandTkn: $1,
Val: $2,
}
}
| T_ELLIPSIS expr

View File

@ -1177,6 +1177,7 @@ type ExprArrayItem struct {
EllipsisTkn *token.Token
Key Vertex
DoubleArrowTkn *token.Token
AmpersandTkn *token.Token
Val Vertex
}

View File

@ -1056,6 +1056,7 @@ func (v *Dumper) ExprArrayItem(n *ast.ExprArrayItem) {
v.dumpToken("EllipsisTkn", n.EllipsisTkn)
v.dumpVertex("Key", n.Key)
v.dumpToken("DoubleArrowTkn", n.DoubleArrowTkn)
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
v.dumpVertex("Val", n.Val)
v.indent--

View File

@ -641,6 +641,7 @@ func (p *printer) ExprArrayItem(n *ast.ExprArrayItem) {
p.printToken(n.EllipsisTkn, nil)
p.printNode(n.Key)
p.printToken(n.DoubleArrowTkn, p.ifNode(n.Key, []byte("=>")))
p.printToken(n.AmpersandTkn, nil)
p.printNode(n.Val)
}

View File

@ -1559,9 +1559,31 @@ func TestPrinterPrintExprArrayItem(t *testing.T) {
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.ExprArrayItem{
Val: &ast.ExprReference{Var: &ast.ExprVariable{
Val: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$world")},
}},
},
}
n.Accept(p)
expected := `$world`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprArrayItem_Reference(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.ExprArrayItem{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Val: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$world")},
},
}
n.Accept(p)