Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo3]Generating a C macro definition(adding end of line continuation)
icon5.gif  [Acceleo3]Generating a C macro definition [message #654865] Thu, 17 February 2011 08:29 Go to next message
Wilbert Alberts is currently offline Wilbert Alberts
Messages: 88
Registered: June 2010
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 08:34]

Report message to a moderator

Re: Generating a C macro definition [message #654867 is a reply to message #654865] Thu, 17 February 2011 08:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6131
Registered: July 2009
Senior Member
Hi,

which m2t framework do you use?

~Christian
Re: Generating a C macro definition [message #654869 is a reply to message #654867] Thu, 17 February 2011 08:35 Go to previous messageGo to next message
Wilbert Alberts is currently offline Wilbert Alberts
Messages: 88
Registered: June 2010
Member
Acceleo 3
Embarrassed changed the subject

Thanks,
Wilbert.
Re: [Acceleo3]Generating a C macro definition [message #655090 is a reply to message #654865] Fri, 18 February 2011 05:00 Go to previous messageGo to next message
Stephane Begaudeau is currently offline Stephane Begaudeau
Messages: 406
Registered: April 2010
Location: Nantes (France)
Senior Member

Hi,

If [template public myTemplate() post(replaceAll('\n', '\\\n'))] [/template] does not work:
- are you on linux ? (on windows, you would need \r\n for example)
- does [myTemplate().replace('\n', '\\\n')/] instead works ?

Stephane Begaudeau, Obeo

--
Twitter: @sbegaudeau
Acceleo wiki: http://wiki.eclipse.org/Acceleo
Blogs: http://stephanebegaudeau.tumblr.com & http://sbegaudeau.tumblr.com



Re: [Acceleo3]Generating a C macro definition [message #655101 is a reply to message #655090] Fri, 18 February 2011 05:29 Go to previous messageGo to next message
Wilbert Alberts is currently offline Wilbert Alberts
Messages: 88
Registered: June 2010
Member
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
Re: [Acceleo3]Generating a C macro definition [message #655465 is a reply to message #655101] Mon, 21 February 2011 04:54 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent Goubet
Messages: 1621
Registered: July 2009
Senior Member
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--
Re: [Acceleo3]Generating a C macro definition [message #655473 is a reply to message #655465] Mon, 21 February 2011 05:09 Go to previous message
Wilbert Alberts is currently offline Wilbert Alberts
Messages: 88
Registered: June 2010
Member
Hi Laurent,

This does the trick! Thanks. (The '-' were only there for 'debugging' purposes.)

Greetings,
Wilbert.
Previous Topic:[Acceleo] ClassNotFoundException if Runner is Acceleo Plug-in Application
Next Topic:oAW 4 migration to oAW5
Goto Forum:
  


Current Time: Sat Jul 26 01:21:27 EDT 2014

Powered by FUDForum. Page generated in 0.44668 seconds