[antlr-interest] getText()
Tim Poole
tim at poole.co.uk
Tue Oct 25 08:53:22 PDT 2011
Jim,
Thanks for this - I didn't reply immediately because this didn't work, I
think because the ESCAPEQUOTE and CHARNOQUOTE needed to be in the same
rule for some reason, so I resorted to a regular expression.
The good news is that I used this fragment technique for another
challenge in the same grammar, and it worked beautifully, so thank you!
Regards,
Tim.
On 21/10/2011 18:11, Jim Idle wrote:
> I wouldn't even bother doing this here in the lexer, it is extra work.
> Just process the token if you use it later. However:
>
> STRINGLITERAL
> : (QUOTE (ESCAPEQUOTE | CHARNOQUOTE)* QUOTE)
> | (APOS st=EC
> {setText("\"" + $st.text + "\"");}
> ;
>
> fragment EC : (ESCAPEAPOS | CHARNOAPOS)* ;
>
> Should do it (untested).
>
> Jim
>
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> bounces at antlr.org] On Behalf Of Tim Poole
>> Sent: Friday, October 21, 2011 9:39 AM
>> To: antlr-interest at antlr.org
>> Subject: [antlr-interest] getText()
>>
>> Hi,
>>
>> I have just had to use this to change the single quotes around a string
>> to double quotes:
>>
>> STRINGLITERAL
>> : (QUOTE (ESCAPEQUOTE | CHARNOQUOTE)* QUOTE)
>> | (APOS (ESCAPEAPOS | CHARNOAPOS)* APOS)
>> {setText(getText().replace('\'','\"'));}
>> ;
>>
>> It's an adaptation of:
>>
>> http://antlr.org/grammar/1264460091565/XPath2.g
>>
>> I tried to do the following, without success:
>>
>> STRINGLITERAL
>> : (QUOTE (ESCAPEQUOTE | CHARNOQUOTE)* QUOTE)
>> | (APOS st=(ESCAPEAPOS | CHARNOAPOS)* APOS) {setText("\"" +
>> $st.text + "\"");}
>> ;
>>
>> Any ideas on how I can implement something along the lines of the
>> second one? It was easy to rewrite this time, but next time might not
>> be so easy!
>>
>> Thanks in advance,
>>
>> Tim.
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
>> email-address
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 2012.0.1831 / Virus Database: 2092/4565 - Release Date: 10/21/11
More information about the antlr-interest
mailing list