[Acceleo3]Generating a C macro definition [message #654865] |
Thu, 17 February 2011 13:29 |
Wilbert Alberts Messages: 212 Registered: June 2010 |
Senior Member |
|
|
Hi,
I'm working on an Acceleo 3 model to text generator that will output 'C' code. In this, I need to generate macro definitions. You know that if a macro definition spans multiple lines, it needs to end each line with the '/' line continuation character.
As the templates I invoke are also used outside the macro definition generation, I thought about adding the '/' at the end of the lines by using a post directive in the macro definition template.
The post (like post(trim())) directive should take the resulting string and replace each '\n' occurrence into a '\\\n'. However this does not seem to work as the replacements don't take place in results or internal template invocations.
Any ideas how to solve this elegantly?
Greetings,
Wilbert.
[Updated on: Thu, 17 February 2011 13:34] Report message to a moderator
|
|
|
|
|
|
|
Re: [Acceleo3]Generating a C macro definition [message #655465 is a reply to message #655101] |
Mon, 21 February 2011 09:54 |
|
This is a multi-part message in MIME format.
--------------010706050700010900040008
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Hi Wilbert,
Acceleo's replaceAll operation relies on the
java.lang.String.replaceAll() implementation. The "replacement" part
thus treats backslashes specially. Furthermore, OCL, Acceleo, Java and
regex all use the backslash as an escape character ... so you might get
some pretty unusual results.
First things first, what do you have here?
replaceAll('\n', ' -\\- \n')
1) '\n' : this is interpreted by Acceleo before it is sent to Java, the
content of the String is then the -character- for a carriage return, not
the escape sequence. This will work just as well when passed to the
regex engine
2) ' -\\- \n' : once again, this will be interpreted by Acceleo. The
content of the Java String will then be " -\- <carriage return>". That
single backslash lying before a "-" cannot be interpreted by the regex
engine and will then be trimmed out of the result. However, the "\n"
will be interpreted as a carriage return and should give you separate
lines in the result.
Regex trivia aside, you should use :
[generateMacroStatement(m.statement).replaceAll('(\r\n|\r|\n )', ' -\\\\-
$1')/]
Notice the use of a quadruple backslash and "$1" instead of a plain "\n"...
'(\r\n|\r|\n)' : match any "line separator" sequence according to the OS
' -\\\\- $1' : "\\\\" will be interpreted by Acceleo and passed to Java
a simple "\\" which means, in a java.lang.String.replaceAll()
replacement string, "a single backslash". "$1" means "the first captured
group" which is equal to the line separator we matched in the first place.
I won't go into any more details here. Please read otherwise on Java
regular expressions and patterns.
Laurent Goubet
Obeo
On 18/02/2011 11:29, Wilbert Alberts wrote:
> Hi Stephane,
>
> First of all thanks for pointing me into the replaceAll direction.
>
> However, it does not seem to work. It seems that '\\' is not recognized as:
>
> [generateMacroStatement(m.statement).replaceAll('\n', ' -\\- \n')/] \
>
> results in:
> #define INIT_RESULT() \
> { \
> result = OK; -- if (bla) -- { -- result = !OK; -- } -- else -- { --
> FALSE = TRUE; -- } \
> }
>
> Any ideas?
>
> Greetings,
> Wilbert
--------------010706050700010900040008
Content-Type: text/x-vcard; charset=utf-8;
name="laurent_goubet.vcf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="laurent_goubet.vcf"
YmVnaW46dmNhcmQNCmZuOkxhdXJlbnQgR291YmV0DQpuOkdvdWJldDtMYXVy ZW50DQpvcmc6
PGEgaHJlZj0iaHR0cDovL3d3dy5vYmVvLmZyIj5PYmVvPC9hPg0KZW1haWw7 aW50ZXJuZXQ6
bGF1cmVudC5nb3ViZXRAb2Jlby5mcg0KdXJsOmh0dHA6Ly93d3cub2Jlby5m cg0KdmVyc2lv
bjoyLjENCmVuZDp2Y2FyZA0KDQo=
--------------010706050700010900040008--
|
|
|
|
Powered by
FUDForum. Page generated in 0.03116 seconds