internal: fixed parsing of expression new

1. Now, for the expression 'new A' the correct
values EndLine, EndPos, and not -1 will be set;
2. Also, for expressions from php5 '$a = &new Foo',
the condition for parsing is fixed when it is necessary
to set the Args values and the initialization of the
NewTkn field is added, in the case when this condition
is false.

## Problem description

The reason why the positions after parsing became
incorrect is that the check that is responsible for
separating expressions like 'new A' and 'new A (args)'
relied on comparison with nil, however, when the parser
was updated, 'ctor_arguments' began to return not nil,
but &ArgumentList{}, so the condition was always true,
and in this case, when calculating the position, the
second argument of the 'NewTokenNodePosition' function
was nil, which is why -1 was returned there.

For the second, the reasons are similar. In addition,
there was a mistake in the number that needs to be
checked. In the expression:

'variable' '=' '&' T_NEW class_name_reference ctor_arguments'

it is necessary to check not 3, but 6 elements for nil.
This commit is contained in:
i582 2021-02-04 07:12:56 +03:00
parent 4d6130d98d
commit 61523ab396
6 changed files with 16 additions and 15 deletions

View File

@ -38822,9 +38822,9 @@ func TestExprNew(t *testing.T) {
Expr: &ast.ExprNew{ Expr: &ast.ExprNew{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: -1, EndLine: 1,
StartPos: 3, StartPos: 3,
EndPos: -1, EndPos: 10,
}, },
NewTkn: &token.Token{ NewTkn: &token.Token{
ID: token.T_NEW, ID: token.T_NEW,
@ -43954,9 +43954,9 @@ func TestExprAssign(t *testing.T) {
Expr: &ast.ExprAssignReference{ Expr: &ast.ExprAssignReference{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: -1, EndLine: 4,
StartPos: 28, StartPos: 28,
EndPos: -1, EndPos: 41,
}, },
Var: &ast.ExprVariable{ Var: &ast.ExprVariable{
Position: &position.Position{ Position: &position.Position{
@ -44032,9 +44032,9 @@ func TestExprAssign(t *testing.T) {
Expr: &ast.ExprNew{ Expr: &ast.ExprNew{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: -1, EndLine: 4,
StartPos: 34, StartPos: 34,
EndPos: -1, EndPos: 41,
}, },
NewTkn: &token.Token{ NewTkn: &token.Token{
ID: token.T_NEW, ID: token.T_NEW,

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -2751,6 +2751,7 @@ new_expr:
} else { } else {
$$ = &ast.ExprNew{ $$ = &ast.ExprNew{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2), Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
NewTkn: $1,
Class: $2, Class: $2,
} }
} }
@ -2796,7 +2797,7 @@ expr_without_variable:
| variable '=' '&' T_NEW class_name_reference ctor_arguments | variable '=' '&' T_NEW class_name_reference ctor_arguments
{ {
var _new *ast.ExprNew var _new *ast.ExprNew
if $3 != nil { if $6 != nil {
_new = &ast.ExprNew{ _new = &ast.ExprNew{
Position: yylex.(*Parser).builder.NewTokenNodePosition($4, $6), Position: yylex.(*Parser).builder.NewTokenNodePosition($4, $6),
NewTkn: $4, NewTkn: $4,
@ -3984,7 +3985,7 @@ backticks_expr:
ctor_arguments: ctor_arguments:
/* empty */ /* empty */
{ {
$$ = &ArgumentList{} $$ = nil
} }
| function_call_parameter_list | function_call_parameter_list
{ {

View File

@ -43581,9 +43581,9 @@ func TestExprNew(t *testing.T) {
Expr: &ast.ExprNew{ Expr: &ast.ExprNew{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: -1, EndLine: 1,
StartPos: 3, StartPos: 3,
EndPos: -1, EndPos: 10,
}, },
NewTkn: &token.Token{ NewTkn: &token.Token{
ID: token.T_NEW, ID: token.T_NEW,
@ -49806,9 +49806,9 @@ func TestExprAssign_Assign(t *testing.T) {
Expr: &ast.ExprAssignReference{ Expr: &ast.ExprAssignReference{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: -1, EndLine: 4,
StartPos: 28, StartPos: 28,
EndPos: -1, EndPos: 41,
}, },
Var: &ast.ExprVariable{ Var: &ast.ExprVariable{
Position: &position.Position{ Position: &position.Position{
@ -49884,9 +49884,9 @@ func TestExprAssign_Assign(t *testing.T) {
Expr: &ast.ExprNew{ Expr: &ast.ExprNew{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: -1, EndLine: 4,
StartPos: 34, StartPos: 34,
EndPos: -1, EndPos: 41,
}, },
NewTkn: &token.Token{ NewTkn: &token.Token{
ID: token.T_NEW, ID: token.T_NEW,

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -3490,7 +3490,7 @@ backticks_expr:
ctor_arguments: ctor_arguments:
/* empty */ /* empty */
{ {
$$ = &ArgumentList{} $$ = nil
} }
| argument_list | argument_list
{ {