php/php-langspec

dereferencable-expression with strings

Hywan opened this issue · 4 comments

Hywan commented

Hello,

The dereferencable-expression rule is defined as:

dereferencable-expression:
   variable
   (   expression   )
   array-creation-expression
   string-literal

The string-literal rule is defined as:

string-literal:
   single-quoted-string-literal
   double-quoted-string-literal
   heredoc-string-literal
   nowdoc-string-literal

So all the following expressions are valid:

C::FOO
$c::FOO
($c = 'C')::FOO
'C'::FOO
"C"::FOO

but the following should be valid too:

<<<HN
C
HN;
::FOO

but it's not, of course.

I suggest two proposals. The first one is to update the dereferencable-expression directly:

  dereferencable-expression:
     variable
     (   expression   )
     array-creation-expression
-    string-literal
+    single-quoted-string-literal
+    double-quoted-string-literal

The second one is to add a quoted-string-literal rule:

+ quoted-string-literal:
+     single-quoted-string-literal
+     double-quoted-string-literal
+

  string-literal:
-   single-quoted-string-literal
-   double-quoted-string-literal
+   quoted-string-literal
    heredoc-string-literal
    nowdoc-string-literal

  dereferencable-expression:
     variable
     (   expression   )
     array-creation-expression
-    string-literal
+    quoted-string-literal

This last proposal is my favorite because this bug can be present somewhere else.

Thought?

Hywan commented

Do you want to see something like the quoted-string-literal rule at least?

nikic commented

I think it would be good to directly describe this in the grammar, but quoted-string-literal is not sufficient for that. Right now we only allow "constant encapsed strings" to be dereferenced, that is "foo"[0] is fine but "$foo"[0] is not (which would still be allowed by quoted-string-literal). (Though I think this is more a defect in PHPs grammar than anything -- we shouldn't be making that distinction here.)

Hywan commented

I will re-open this issue once I will work on it in the parser. Sorry for the delay :-).