Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Backslash in string
Backslash in string [message #876959] Fri, 25 May 2012 15:43 Go to next message
Bryce Brinkley is currently offline Bryce BrinkleyFriend
Messages: 8
Registered: October 2011
Junior Member
Hello Xtext community!

I'm trying to define a terminal rule which allows strings to contain single backslashes, but with no success so far...
Example token: "c:\some\windows\path\file.txt"

This seems to be a logical solution for me
terminal STRING returns ecore::EString:
	'"' (!'"')* '"';

or also this
terminal STRING returns ecore::EString:
	'"' -> '"';

However when parsing an example file I'm getting the following error:
Error at line 7 at '"D:\myFolder123\ELMN.ed"': Illegal escape character \m


Could anybody give me a hint how to handle the \ character?
Your help is greatly appreciated!

Bryce
Re: Backslash in string [message #876976 is a reply to message #876959] Fri, 25 May 2012 16:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13724
Registered: July 2009
Senior Member
Hi you need a custom value converter (or simply use double back
slashes as usual in java)


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Backslash in string [message #876980 is a reply to message #876959] Fri, 25 May 2012 16:08 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3108
Registered: July 2009
Senior Member
Hi Bryce,

you have to register a value converter for your variant of STRING.
Please refer to the docs for details on value conversion.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 25.05.12 17:43, schrieb Bryce Brinkley:
> Hello Xtext community!
>
> I'm trying to define a terminal rule which allows strings to contain
> single backslashes, but with no success so far...
> Example token: "c:\some\windows\path\file.txt"
>
> This seems to be a logical solution for me
> terminal STRING returns ecore::EString:
> '"' (!'"')* '"';
> or also this
> terminal STRING returns ecore::EString:
> '"' -> '"';
> However when parsing an example file I'm getting the following error:
> Error at line 7 at '"D:\myFolder123\ELMN.ed"': Illegal escape character \m
>
> Could anybody give me a hint how to handle the \ character?
> Your help is greatly appreciated!
>
> Bryce
Re: Backslash in string [message #1736205 is a reply to message #876980] Mon, 27 June 2016 13:26 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
class CustomTerminalConverters extends DefaultTerminalConverters {
@Inject
private STRINGValueConverter stringValueConverter;

@ValueConverter(rule = "STRING")
override IValueConverter<String> STRING() {
return stringValueConverter;
}
}
class STRINGValueConverter extends AbstractLexerBasedConverter<String> {

override toValue(String string, INode node) throws ValueConverterException {
println("String value converter is called::::::::::::::::::"+node+" :::: "+string)
return string.replaceAll("[a-z]", "");
}

}
Re: Backslash in string [message #1736209 is a reply to message #1736205] Mon, 27 June 2016 13:45 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13724
Registered: July 2009
Senior Member
simply subclass STRINGValueConverter and add a binding for STRINGValueConverter to your subclass should be enhough

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Binding ClusteringBuilderState
Next Topic:[Xtext] NPE in ResourceDescriptionsProvider.createResourceDescriptions() after update to 2.9 / 2.10
Goto Forum:
  


Current Time: Thu Jan 28 06:24:51 GMT 2021

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

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

Back to the top