Why does Javac reject text blocks ending with four or more " ?

Deepak Vohra dvohra16 at gmail.com
Thu Jul 30 14:04:56 UTC 2020


Sequences of three characters are reserved for the opening and closing
delimiter and should not occur by user code or incidentally in a text
block. As an example, an empty string literal within a text block, while
apparently permissible, adds three consecutive double quotes as follows:

 String s ="""

                 """"

                 """;

On Thu, Jul 30, 2020 at 1:13 AM Alex Buckley <alex.buckley at oracle.com>
wrote:

> " and "" can be used anywhere in a text block except immediately prior
> to the closing delimiter. If a text block needs to end with " or "",
> then the " and "" may be escaped:
>
> String s1 = """
>    ...
>    \"""";
> String s2 = """
>    ...
>    \"\"""";
>
> Writing just """" or """"" runs afoul of the longstanding JLS 3.2 rule
> about lexical translation choosing the longest possible token. That is,
> during the process of translating characters into the TextBlock token of
> the lexical grammar, the discovery of """ is translated as the closing
> delimiter of TextBlock, even if a further " follows immediately.
>
> Alex
>
> On 7/29/2020 10:05 PM, Jayaprakash Artanareeswaran wrote:
> > Hello,
> >
> > The following code is rejected by Javac in JDK 15, although JEP 378
> doesn't say anything about it.
> >
> >      static final String TEXT_BLOCK = """
> >                1
> >              """"; // Notice the four quotes here?
> >
> > In fact, the JEP says this:
> >
> > "The use of the escape sequences \" and \n is permitted in a text block,
> but not necessary or recommended. However, representing the sequence """ in
> a text block requires the escaping of at least one " character, to avoid
> mimicking the closing delimiter."
> >
> > So, I would imagine it's perfectly okay for up to two consequent quotes
> to be used anywhere in a text block.
> >
> > If this is indeed backed by the spec, can the experts please point me to
> the relevant section?
> > Hopefully, I am asking this in the right mailing list.
> >
> > Thanks,
> > Jay
> >
>


More information about the amber-dev mailing list