[refactoring] update ast structure of "ExprMethodCall" and "ExprPropertyFetch" nodes

This commit is contained in:
Vadym Slizov 2020-12-03 21:42:16 +02:00
parent b5ef30eb36
commit b1e9f5e167
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
9 changed files with 182 additions and 119 deletions

View File

@ -17236,6 +17236,8 @@ func TestExprPrint(t *testing.T) {
assert.DeepEqual(t, expected, actual) assert.DeepEqual(t, expected, actual)
} }
// TODO add test for `echo $a->b["c"]()->d["e"]();`
func TestExprPropertyFetch(t *testing.T) { func TestExprPropertyFetch(t *testing.T) {
src := `<? $a->foo;` src := `<? $a->foo;`

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -3744,24 +3744,26 @@ expr_without_variable:
for _, n := range($4) { for _, n := range($4) {
switch nn := n.(type) { switch nn := n.(type) {
case *ast.ExprFunctionCall:
nn.Function = $$
nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn
case *ast.ExprArrayDimFetch: case *ast.ExprArrayDimFetch:
nn.Var = $$ nn.Var = $$
nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
case *ast.ExprPropertyFetch: case *ast.ExprPropertyFetch:
nn.Var = $$ nn.Var = $$
nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
case *ast.ExprMethodCall: case *ast.ExprMethodCall:
nn.Var = $$ nn.Var = $$
nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
} }
// save position
$$.GetNode().Position = position.NewNodesPosition($$, n)
} }
} }
| expr '?' expr ':' expr | expr '?' expr ':' expr
@ -5448,55 +5450,75 @@ variable:
{ {
$$ = $1 $$ = $1
if $4 != nil { $3[0].(*ast.ExprPropertyFetch).ObjectOperatorTkn = $2
$4[0].(*ast.ExprMethodCall).Method = $3[len($3)-1].(*ast.ExprPropertyFetch).Property
$3 = append($3[:len($3)-1], $4...)
}
// save comments if $4 != nil {
yylex.(*Parser).setFreeFloating($3[0], token.Var, $2.SkippedTokens) last := $3[len($3)-1]
switch l := last.(type) {
case *ast.ExprArrayDimFetch:
mc := $4[0].(*ast.ExprMethodCall)
$3 = append($3, &ast.ExprFunctionCall{
Node: ast.Node{
Position: position.NewNodePosition(mc),
},
OpenParenthesisTkn: mc.OpenParenthesisTkn,
Arguments: mc.Arguments,
CloseParenthesisTkn: mc.OpenParenthesisTkn,
},
)
$3 = append($3, $4[1:len($4)]...)
case *ast.ExprPropertyFetch:
$4[0].(*ast.ExprMethodCall).Method = l.Property
$4[0].(*ast.ExprMethodCall).ObjectOperatorTkn = l.ObjectOperatorTkn
$3 = append($3[:len($3)-1], $4...)
}
}
for _, n := range($3) { for _, n := range($3) {
switch nn := n.(type) { switch nn := n.(type) {
case *ast.ExprFunctionCall:
nn.Function = $$
nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn
case *ast.ExprArrayDimFetch: case *ast.ExprArrayDimFetch:
nn.Var = $$ nn.Var = $$
nn.GetNode().Position = position.NewNodesPosition($$, nn) nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
case *ast.ExprPropertyFetch: case *ast.ExprPropertyFetch:
nn.Var = $$ nn.Var = $$
nn.GetNode().Position = position.NewNodesPosition($$, nn) nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
case *ast.ExprMethodCall: case *ast.ExprMethodCall:
nn.Var = $$ nn.Var = $$
nn.GetNode().Position = position.NewNodesPosition($$, nn) nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
} }
} }
for _, n := range($5) { for _, n := range($5) {
switch nn := n.(type) { switch nn := n.(type) {
case *ast.ExprFunctionCall:
nn.Function = $$
nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn
case *ast.ExprArrayDimFetch: case *ast.ExprArrayDimFetch:
nn.Var = $$ nn.Var = $$
nn.GetNode().Position = position.NewNodesPosition($$, nn) nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
case *ast.ExprPropertyFetch: case *ast.ExprPropertyFetch:
nn.Var = $$ nn.Var = $$
nn.GetNode().Position = position.NewNodesPosition($$, nn) nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
case *ast.ExprMethodCall: case *ast.ExprMethodCall:
nn.Var = $$ nn.Var = $$
nn.GetNode().Position = position.NewNodesPosition($$, nn) nn.Node.Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
} }
} }
} }
@ -5520,16 +5542,32 @@ variable_properties:
variable_property: variable_property:
T_OBJECT_OPERATOR object_property method_or_not T_OBJECT_OPERATOR object_property method_or_not
{ {println("FOOFOOFOOFOOFOOFOOFOOFOOFOO")
$2[0].(*ast.ExprPropertyFetch).ObjectOperatorTkn = $1
if $3 != nil { if $3 != nil {
$3[0].(*ast.ExprMethodCall).Method = $2[len($2)-1].(*ast.ExprPropertyFetch).Property last := $2[len($2)-1]
$2 = append($2[:len($2)-1], $3...) switch l := last.(type) {
case *ast.ExprArrayDimFetch:
mc := $3[0].(*ast.ExprMethodCall)
$2 = append($2, &ast.ExprFunctionCall{
Node: ast.Node{
Position: position.NewNodePosition(mc),
},
OpenParenthesisTkn: mc.OpenParenthesisTkn,
Arguments: mc.Arguments,
CloseParenthesisTkn: mc.OpenParenthesisTkn,
},
)
$2 = append($2, $3[1:len($3)]...)
case *ast.ExprPropertyFetch:
$3[0].(*ast.ExprMethodCall).Method = l.Property
$3[0].(*ast.ExprMethodCall).ObjectOperatorTkn = l.ObjectOperatorTkn
$2 = append($2[:len($2)-1], $3...)
}
} }
$$ = $2 $$ = $2
// save comments
yylex.(*Parser).setFreeFloating($2[0], token.Var, $1.SkippedTokens)
} }
; ;
@ -5567,10 +5605,14 @@ array_method_dereference:
method: method:
function_call_parameter_list function_call_parameter_list
{ {
$$ = &ast.ExprMethodCall{ast.Node{}, nil, nil, $1.(*ast.ArgumentList)} $$ = &ast.ExprMethodCall{
Node: ast.Node{
// save position Position: position.NewNodePosition($1),
$$.GetNode().Position = position.NewNodePosition($1) },
OpenParenthesisTkn: $1.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $1.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $1.(*ast.ArgumentList).CloseParenthesisTkn,
}
} }
; ;
@ -5785,11 +5827,14 @@ object_property:
} }
| variable_without_objects | variable_without_objects
{ {
fetch := &ast.ExprPropertyFetch{ast.Node{}, nil, $1} $$ = []ast.Vertex{
$$ = []ast.Vertex{fetch} &ast.ExprPropertyFetch{
Node: ast.Node{
// save position Position: position.NewNodePosition($1),
fetch.GetNode().Position = position.NewNodePosition($1) },
Property: $1,
},
}
} }
; ;
@ -5824,11 +5869,14 @@ object_dim_list:
} }
| variable_name | variable_name
{ {
fetch := &ast.ExprPropertyFetch{ast.Node{}, nil, $1} $$ = []ast.Vertex{
$$ = []ast.Vertex{fetch} &ast.ExprPropertyFetch{
Node: ast.Node{
// save position Position: position.NewNodePosition($1),
fetch.GetNode().Position = position.NewNodePosition($1) },
Property: $1,
},
}
} }
; ;
@ -6179,29 +6227,31 @@ encaps_var:
} }
| T_VARIABLE T_OBJECT_OPERATOR T_STRING | T_VARIABLE T_OBJECT_OPERATOR T_STRING
{ {
identifier := &ast.Identifier{ $$ = &ast.ExprPropertyFetch{
Node: ast.Node{ Node: ast.Node{
Position: position.NewTokenPosition($1), Position: position.NewTokensPosition($1, $3),
}, },
IdentifierTkn: $1, Var: &ast.ExprVariable{
Value: $1.Value, Node: ast.Node{
} Position: position.NewTokenPosition($1),
variable := &ast.ExprVariable{ast.Node{}, identifier} },
fetch := &ast.Identifier{ VarName: &ast.Identifier{
Node: ast.Node{ Node: ast.Node{
Position: position.NewTokenPosition($3), Position: position.NewTokenPosition($1),
},
IdentifierTkn: $1,
Value: $1.Value,
},
},
ObjectOperatorTkn: $2,
Property: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($3),
},
IdentifierTkn: $3,
Value: $3.Value,
}, },
IdentifierTkn: $3,
Value: $3.Value,
} }
$$ = &ast.ExprPropertyFetch{ast.Node{}, variable, fetch}
// save position
variable.GetNode().Position = position.NewTokenPosition($1)
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens)
} }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
{ {

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -4274,14 +4274,17 @@ callable_variable:
} }
| dereferencable T_OBJECT_OPERATOR property_name argument_list | dereferencable T_OBJECT_OPERATOR property_name argument_list
{ {
$$ = &ast.ExprMethodCall{ast.Node{}, $1, $3, $4.(*ast.ArgumentList)} $$ = &ast.ExprMethodCall{
Node: ast.Node{
// save position Position: position.NewNodesPosition($1, $4),
$$.GetNode().Position = position.NewNodesPosition($1, $4) },
Var: $1,
// save comments ObjectOperatorTkn: $2,
yylex.(*Parser).MoveFreeFloating($1, $$) Method: $3,
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens) OpenParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $4.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
}
} }
| function_call | function_call
{ {
@ -4300,14 +4303,14 @@ variable:
} }
| dereferencable T_OBJECT_OPERATOR property_name | dereferencable T_OBJECT_OPERATOR property_name
{ {
$$ = &ast.ExprPropertyFetch{ast.Node{}, $1, $3} $$ = &ast.ExprPropertyFetch{
Node: ast.Node{
// save position Position: position.NewNodesPosition($1, $3),
$$.GetNode().Position = position.NewNodesPosition($1, $3) },
Var: $1,
// save comments ObjectOperatorTkn: $2,
yylex.(*Parser).MoveFreeFloating($1, $$) Property: $3,
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens) }
} }
; ;
@ -4409,14 +4412,14 @@ new_variable:
} }
| new_variable T_OBJECT_OPERATOR property_name | new_variable T_OBJECT_OPERATOR property_name
{ {
$$ = &ast.ExprPropertyFetch{ast.Node{}, $1, $3} $$ = &ast.ExprPropertyFetch{
Node: ast.Node{
// save position Position: position.NewNodesPosition($1, $3),
$$.GetNode().Position = position.NewNodesPosition($1, $3) },
Var: $1,
// save comments ObjectOperatorTkn: $2,
yylex.(*Parser).MoveFreeFloating($1, $$) Property: $3,
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens) }
} }
| class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable | class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable
{ {
@ -4714,29 +4717,31 @@ encaps_var:
} }
| T_VARIABLE T_OBJECT_OPERATOR T_STRING | T_VARIABLE T_OBJECT_OPERATOR T_STRING
{ {
identifier := &ast.Identifier{ $$ = &ast.ExprPropertyFetch{
Node: ast.Node{ Node: ast.Node{
Position: position.NewTokenPosition($1), Position: position.NewTokensPosition($1, $3),
}, },
IdentifierTkn: $1, Var: &ast.ExprVariable{
Value: $1.Value, Node: ast.Node{
} Position: position.NewTokenPosition($1),
variable := &ast.ExprVariable{ast.Node{}, identifier} },
fetch := &ast.Identifier{ VarName: &ast.Identifier{
Node: ast.Node{ Node: ast.Node{
Position: position.NewTokenPosition($3), Position: position.NewTokenPosition($1),
},
IdentifierTkn: $1,
Value: $1.Value,
},
},
ObjectOperatorTkn: $2,
Property: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($3),
},
IdentifierTkn: $3,
Value: $3.Value,
}, },
IdentifierTkn: $3,
Value: $3.Value,
} }
$$ = &ast.ExprPropertyFetch{ast.Node{}, variable, fetch}
// save position
variable.GetNode().Position = position.NewTokenPosition($1)
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens)
} }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
{ {

View File

@ -1215,9 +1215,12 @@ func (n *ExprList) Accept(v NodeVisitor) {
// ExprMethodCall node // ExprMethodCall node
type ExprMethodCall struct { type ExprMethodCall struct {
Node Node
Var Vertex Var Vertex
Method Vertex ObjectOperatorTkn *token.Token
ArgumentList *ArgumentList Method Vertex
OpenParenthesisTkn *token.Token
Arguments []Vertex
CloseParenthesisTkn *token.Token
} }
func (n *ExprMethodCall) Accept(v NodeVisitor) { func (n *ExprMethodCall) Accept(v NodeVisitor) {
@ -1288,8 +1291,9 @@ func (n *ExprPrint) Accept(v NodeVisitor) {
// ExprPropertyFetch node // ExprPropertyFetch node
type ExprPropertyFetch struct { type ExprPropertyFetch struct {
Node Node
Var Vertex Var Vertex
Property Vertex ObjectOperatorTkn *token.Token
Property Vertex
} }
func (n *ExprPropertyFetch) Accept(v NodeVisitor) { func (n *ExprPropertyFetch) Accept(v NodeVisitor) {

View File

@ -1396,10 +1396,12 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Method) t.Traverse(nn.Method)
t.visitor.Leave("Method", true) t.visitor.Leave("Method", true)
} }
if nn.ArgumentList != nil { if nn.Arguments != nil {
t.visitor.Enter("ArgumentList", true) t.visitor.Enter("Arguments", false)
t.Traverse(nn.ArgumentList) for _, c := range nn.Arguments {
t.visitor.Leave("ArgumentList", true) t.Traverse(c)
}
t.visitor.Leave("Arguments", false)
} }
case *ast.ExprNew: case *ast.ExprNew:
if nn == nil { if nn == nil {

View File

@ -1133,7 +1133,7 @@ func (p *PrettyPrinter) printExprMethodCall(n ast.Vertex) {
io.WriteString(p.w, "->") io.WriteString(p.w, "->")
p.Print(nn.Method) p.Print(nn.Method)
io.WriteString(p.w, "(") io.WriteString(p.w, "(")
p.joinPrint(", ", nn.ArgumentList.Arguments) p.joinPrint(", ", nn.Arguments)
io.WriteString(p.w, ")") io.WriteString(p.w, ")")
} }

View File

@ -1695,9 +1695,9 @@ func (p *Printer) printExprMethodCall(n ast.Vertex) {
p.write([]byte("->")) p.write([]byte("->"))
p.Print(nn.Method) p.Print(nn.Method)
p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(") p.printToken(nn.OpenParenthesisTkn, "(")
p.joinPrint(",", nn.ArgumentList.Arguments) p.joinPrint(",", nn.Arguments)
p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")") p.printToken(nn.CloseParenthesisTkn, ")")
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }