Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to embed new line character in string in xtend?
How to embed new line character in string in xtend? [message #915949] Tue, 18 September 2012 09:21 Go to next message
Marcus Mathioudakis is currently offline Marcus MathioudakisFriend
Messages: 18
Registered: June 2012
Junior Member
I am generating a class field in my JvmModelinferrer in xtend:

val exp = NodeModelUtils::getNode(rule.expression);
members+=rule.toField("text",rule.newTypeRef('java.lang.String'))[
^static = true
val Procedure1<ITreeAppendable> b = [
append('''"«exp.text.replace('"','').replaceAll("\n"," \\n ")»"''')
]
initializer = b
setFinal(true)
setVisibility(JvmVisibility::PUBLIC)
]


given that the initial exp.text was

'a
b'

I would like the generated field to look like this:

String text = "a \n b";

but instead it look like this:

String text = "a n b";

where as if I dont replace the newline characters at all then I get:

String text = "a
b";

which of course doesnt compile. The problem seems to be that xtend is evaluating the java string, so even though "\\n" gets compiled to "\n" in java, in xtend it evaluates to "n"

How can I get xtend to not evaluate the \n so that it is preserved in the generated java string?
Re: How to embed new line character in string in xtend? [message #915981 is a reply to message #915949] Tue, 18 September 2012 10:51 Go to previous messageGo to next message
Vlad Dumitrescu is currently offline Vlad DumitrescuFriend
Messages: 431
Registered: July 2009
Location: Gothenburg
Senior Member
Hi,

Did you try to replace with "\\\\n"? The source code you're writing will get through the lexer too and the backslash needs one more escape.

regards,
Vlad
Re: How to embed new line character in string in xtend? [message #915993 is a reply to message #915981] Tue, 18 September 2012 11:27 Go to previous message
Marcus Mathioudakis is currently offline Marcus MathioudakisFriend
Messages: 18
Registered: June 2012
Junior Member
worked like a charm Smile, many thanks!
Previous Topic:15 Minute Tutorial Extended
Next Topic:Label Decorators Trace - Common Navigator for Xtext-based Project
Goto Forum:
  


Current Time: Thu Mar 28 21:29:26 GMT 2024

Powered by FUDForum. Page generated in 0.11133 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top