[stringtemplate-interest] Replacing double quotes by single quotes in output strings?

Terence Parr parrt at cs.usfca.edu
Thu Dec 17 11:48:05 PST 2009


either you need a renderer or modify the objcts going into the template so toString flips the " and '.
T
On Dec 17, 2009, at 9:27 AM, Thierry Coq wrote:

> Hello Jim,
> 
> thanks for your interest.
> 
> Yes I'm using ANTLR to generate a Pascal Parser and Lexer, and I'm using 
> a small expression calculator grammar as an example. In order to do that 
> I need to write another set of ST rules to change the target language.
> 
> the grammar start like this: (Expr.g)
> grammar Expr;
> options {language=Lazarus;}
> ...
> //the rest of the grammar is like existing grammars in ANTLR.
> 
> The resulting code is like this (for the tokens table):
> TOKENS_NAMES: array[0..TOKEN_COUNT] of String = (
> "<invalid>",
> "<EOR>",
> "<DOWN>",
> "<UP>",
> "WS",
> //..
> "'+'",
> //..
> );
> 
> This doesn't work in Pascal. I'm trying to get:
> TOKENS_NAMES: array[0..TOKEN_COUNT] of String = (
> '<invalid>',
> '<EOR>',
> '<DOWN>',
> '<UP>',
> 'WS",
> //..
> '''+''',
> //..
> );
> 
> The rule I use for ST is this one:
> <if(grammar.IsGrammarRoot)>
> const
> TOKEN_COUNT = <length(tokenNames)>+3;
> TOKENS_NAMES: array[0..TOKEN_COUNT] of String = (
> '<invalid>',
> '<EOR>',
> '<DOWN>',
> '<UP>',
> <tokenNames:{<it>}; separator=",\n">
> );
> <endif>
> 
> I'm wondering how I can introduce a regexp expression in the tokenNames 
> rules above to replace the chars ' by '' and " by '.
> 
> Best regards,
> Thierry
> 
> Jim Idle wrote:
>> Thierry,
>> 
>> Sorry but I am still not quite following you. Are you saying that your parser is generated in Java and you want to generate it in Pascal? Or that you have a parser whose output is created by a StringTemplate? Changing the string delimiters should be trivial for the latter case, but there is no way to make the Java output of ANTLR be Pascal without writing a Pascal language target. 
>> 
>> Can you post a small example of what you are trying to achieve?
>> 
>> Jim
>> 
>> 
>>> -----Original Message-----
>>> From: Thierry Coq [mailto:tcoq at free.fr]
>>> Sent: Tuesday, December 15, 2009 9:41 PM
>>> To: Jim Idle
>>> Subject: Re: [stringtemplate-interest] Replacing double quotes by
>>> single quotes in output strings?
>>> 
>>> Jim,
>>> 
>>> thank you for your prompt answer.
>>> 
>>> I'm working with ANTLR which is using StringTemplate.
>>> 
>>> ANTLR generates code using the StringTemplate. Of course, I don't want
>>> to touch ANTLR or StringTemplate to use them to generate code. So I'm
>>> limited to using string template *.st and *.stg files to do the job.
>>> 
>>> In ANTLR, the tokens are declared as Java strings and output to the
>>> generated code as java strings, as-is something rather uncompilable for
>>> Pascal. I need to be able, once the tokens have been analyzed by ANTLR
>>> to generate them in a format understandable by pascal, aka replacing
>>> the
>>> double quotes by simple one, and doubling the simple quotes within the
>>> strings, if any.
>>> 
>>> Is this possible?
>>> 
>>> Best regards,
>>> Thierry
>>> 
>>> Jim Idle wrote:
>>> 
>>>> Not sure what you are asking for to be honest. Are just wanting to
>>>> 
>>> escape characters? Do you mean StringTemplate and not ANTLR? If you
>>> means StringTemplate then perhaps you need a renderer method, but
>>> generally you should do such replacements in the model and not in the
>>> template.
>>> 
>>>> Jim
>>>> 
>>>> 
>>>> 
>>>>> -----Original Message-----
>>>>> From: stringtemplate-interest-bounces at antlr.org
>>>>> 
>>> [mailto:stringtemplate-
>>> 
>>>>> interest-bounces at antlr.org] On Behalf Of Thierry Coq
>>>>> Sent: Tuesday, December 15, 2009 12:52 PM
>>>>> To: stringtemplate-interest at antlr.org
>>>>> Subject: [stringtemplate-interest] Replacing double quotes by single
>>>>> quotes in output strings?
>>>>> 
>>>>> Hello,
>>>>> 
>>>>> is it possible to replace a double-quoted string, for example "HS"
>>>>> 
>>> by
>>> 
>>>>> single quotes: 'HS', and double single quotes when needed: for
>>>>> 
>>> example:
>>> 
>>>>> "'='" becomes '''=''', in the ANTLR code generator?
>>>>> 
>>>>> I'm looking for a regexp, but I can't seem to find it.
>>>>> 
>>>>> Best regards,
>>>>> Thierry
>>>>> _______________________________________________
>>>>> stringtemplate-interest mailing list
>>>>> stringtemplate-interest at antlr.org
>>>>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>>>>> 
>>>>> 
>>>> 
>>>> _______________________________________________
>>>> stringtemplate-interest mailing list
>>>> stringtemplate-interest at antlr.org
>>>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>>>> 
>>>> 
>>>> 
>>>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
> 
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list