Delimited strings

Timon Gehr timon.gehr at gmx.ch
Tue Feb 14 16:46:51 PST 2012


On 02/15/2012 01:38 AM, H. S. Teoh wrote:
> I've finally got my lexer to the point where it can successfully
> tokenize /usr/include/d2/4.3.2/std/*.d. Yay! :-) Now I'm going back to
> fill in the gaps that still haven't been implemented yet. Among which
> are delimited strings.
>
> According to the online specs, delimited strings start with q" followed
> by the delimiter, whether a character or an identifier. There are some
> ambiguities here:
>
> 1) Does this:
>
> 	q"abca"
>
> represent the string "bc"? Is it the same as:
>
> 	q"a
> 	bc
> 	a"
>
> ?

No, q"abca" is illegal. The pattern is

q"identifier
string
identifier"

(The terminating new line is kept, so the string in this case is "string\n")

>
> What do delimiter characters refer to? Are they restricted to
> non-identifier symbols, like this:
>
> 	q"%abc%"
>
> representing "abc"?
>

Yes. (all non identifier-starting symbols, digits are ok)

>
> 2) Among the possible delimiters are "nesting delimiters", so you can
> write stuff like:
>
> 	q"(abc)"
>
> which is the same as "abc". Now it's a bit confusing that the specs use
> this as an example:
>
> 	q"(foo(xxx))"
>
> as though the '(' and ')' inside the string matter.

They do matter.

> So does this mean
> that you can write:
>
> 	q"(foo(q"(xxx)"))"
>
> and have it represent the string
>
> 	foo(q"(xxx)")
>
> ?

Yes.

> If not, then why are these called "nested delimiters", since any ')'
> not immediately followed by " is obviously not the end of the literal?
> For example, this:
>
> 	q"(a)b)"
>
> obviously is equal to "a)b" since the first ) can't possibly terminate
> the literal.
>
>
> T
>

It is illegal because the parens do not match.


More information about the Digitalmars-d-learn mailing list