Little annoying things

Working on the Syntax definition is fun because you really learn to understand the language. But it can also be frustrating. This evening I was working on the syntax of double quoted strings. Double quoted string in PHP expand some variables when the string is parsed. See the documentation for more details.

It turns out that parsing a double quoted strings is a real challenge. An backslash is recognized differently depending on the context it is used in. I found this out when I tried to correctly parse the following string:
"Octal: \123"
In PHP this will be evaluated to
"Octal: S"
So the "\123" part should be parsed as a separate part. However, if it says "\1234" it is just a part of the string since octal characters can only be encoded by three digits.

Another example is that of the advanced string parsing features. Examine the following strings
$foo = "Hello {$name}";
$bar = "Hello { $name}";
In the variable $foo, the brackets are discarded and the variable $name is expanded. The variable $bar just contains the exact same string as stated above, including the brackets and the dollar sign.

These examples are a nightmare from the parsing point of view. So I will let this rest for now and move on to the statements of PHP in the following session. This should keep me from throwing my screen out of the window because of frustration.

No comments: