Skip to content

zend_language_scanner: Lex clone as identifier when followed by ( #18981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 5 additions & 39 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
%type <ast> unprefixed_use_declarations const_decl inner_statement
%type <ast> expr optional_expr while_statement for_statement foreach_variable
%type <ast> foreach_statement declare_statement finally_statement unset_variable variable
%type <ast> extends_from parameter optional_type_without_static argument argument_no_expr global_var
%type <ast> extends_from parameter optional_type_without_static argument global_var
%type <ast> static_var class_statement trait_adaptation trait_precedence trait_alias
%type <ast> absolute_trait_method_reference trait_method_reference property echo_expr
%type <ast> new_dereferenceable new_non_dereferenceable anonymous_class class_name class_name_reference simple_variable
Expand Down Expand Up @@ -287,7 +287,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
%type <ast> enum_declaration_statement enum_backing_type enum_case enum_case_expr
%type <ast> function_name non_empty_member_modifiers
%type <ast> property_hook property_hook_list optional_property_hook_list hooked_property property_hook_body
%type <ast> optional_parameter_list clone_argument_list non_empty_clone_argument_list
%type <ast> optional_parameter_list

%type <num> returns_ref function fn is_reference is_variadic property_modifiers property_hook_modifiers
%type <num> method_modifiers class_const_modifiers member_modifier optional_cpp_modifiers
Expand Down Expand Up @@ -914,42 +914,13 @@ non_empty_argument_list:
{ $$ = zend_ast_list_add($1, $3); }
;

/* `clone_argument_list` is necessary to resolve a parser ambiguity (shift-reduce conflict)
* of `clone($expr)`, which could either be parsed as a function call with `$expr` as the first
* argument or as a use of the `clone` language construct with an expression with useless
* parenthesis. Both would be valid and result in the same AST / the same semantics.
* `clone_argument_list` is defined in a way that an `expr` in the first position needs to
* be followed by a `,` which is not valid syntax for a parenthesized `expr`, ensuring
* that calling `clone()` with a single unnamed parameter is handled by the language construct
* syntax.
*/
clone_argument_list:
'(' ')' { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); }
| '(' non_empty_clone_argument_list possible_comma ')' { $$ = $2; }
| '(' expr ',' ')' { $$ = zend_ast_create_list(1, ZEND_AST_ARG_LIST, $2); }
| '(' T_ELLIPSIS ')' { $$ = zend_ast_create_fcc(); }
;

non_empty_clone_argument_list:
expr ',' argument
{ $$ = zend_ast_create_list(2, ZEND_AST_ARG_LIST, $1, $3); }
| argument_no_expr
{ $$ = zend_ast_create_list(1, ZEND_AST_ARG_LIST, $1); }
| non_empty_clone_argument_list ',' argument
{ $$ = zend_ast_list_add($1, $3); }
;

argument_no_expr:
identifier ':' expr
argument:
expr { $$ = $1; }
| identifier ':' expr
{ $$ = zend_ast_create(ZEND_AST_NAMED_ARG, $1, $3); }
| T_ELLIPSIS expr { $$ = zend_ast_create(ZEND_AST_UNPACK, $2); }
;

argument:
expr { $$ = $1; }
| argument_no_expr { $$ = $1; }
;

global_var_list:
global_var_list ',' global_var { $$ = zend_ast_list_add($1, $3); }
| global_var { $$ = zend_ast_create_list(1, ZEND_AST_STMT_LIST, $1); }
Expand Down Expand Up @@ -1257,11 +1228,6 @@ expr:
{ $$ = zend_ast_create(ZEND_AST_ASSIGN, $1, $3); }
| variable '=' ampersand variable
{ $$ = zend_ast_create(ZEND_AST_ASSIGN_REF, $1, $4); }
| T_CLONE clone_argument_list {
zend_ast *name = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_CLONE));
name->attr = ZEND_NAME_FQ;
$$ = zend_ast_create(ZEND_AST_CALL, name, $2);
}
| T_CLONE expr {
zend_ast *name = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_CLONE));
name->attr = ZEND_NAME_FQ;
Expand Down
5 changes: 5 additions & 0 deletions Zend/zend_language_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,11 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
RETURN_TOKEN_WITH_IDENT(T_NEW);
}

<ST_IN_SCRIPTING>"clone"{OPTIONAL_WHITESPACE_OR_COMMENTS}"(" {
yyless(5);
RETURN_TOKEN_WITH_STR(T_NAME_FULLY_QUALIFIED, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would lexing it as T_STRING instead work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work, but “require” a special case in the compiler to treat clone($x, $y) in a namespace as a fully-qualified call.

}

<ST_IN_SCRIPTING>"clone" {
RETURN_TOKEN_WITH_IDENT(T_CLONE);
}
Expand Down
Loading