Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Overriding terminal StRING
Overriding terminal StRING [message #542495] Fri, 25 June 2010 06:01 Go to next message
Alex is currently offline AlexFriend
Messages: 114
Registered: June 2010
Senior Member
Hi,

I had to override the terminal rule STRING because in my grammar there's no string with line breaks in it allowed and also a '$' should be escape-able. So I defined the rules:
terminal STRING:
	'"' (STR_ESC | !('"'|'\r'|'\n'|'\\'))* '"'
	;
	
terminal STR_ESC:
	'\\'('b'|'t'|'n'|'f'|'r'|'"'|'\\'|'$')
	;


The line break thing works fine but an input
"foo\$bar"
evokes a syntax error "Illegal escape character \$"

Is there anything that I should configure in the parser?

- Alex
Re: Overriding terminal StRING [message #542507 is a reply to message #542495] Fri, 25 June 2010 06:46 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Alex,

if you override the terminal rule, you should consider to override the
value converter, too. Please refer to the documentation at
http://www.eclipse.org/Xtext/documentation/1_0_0/xtext.html# valueconverter
for details.

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

Am 25.06.10 08:01, schrieb Alex:
> Hi,
>
> I had to override the terminal rule STRING because in my grammar there's
> no string with line breaks in it allowed and also a '$' should be
> escape-able. So I defined the rules:
>
> terminal STRING:
> '"' (STR_ESC | !('"'|'\r'|'\n'|'\\'))* '"'
> ;
>
> terminal STR_ESC:
> '\\'('b'|'t'|'n'|'f'|'r'|'"'|'\\'|'$')
> ;
>
> The line break thing works fine but an input "foo\$bar" evokes a syntax
> error "Illegal escape character \$"
>
> Is there anything that I should configure in the parser?
> - Alex
Re: Overriding terminal StRING [message #542524 is a reply to message #542507] Fri, 25 June 2010 08:19 Go to previous messageGo to next message
Alex is currently offline AlexFriend
Messages: 114
Registered: June 2010
Senior Member
Thanks for that hint!

I now added a binding to the MyDslRuntimeModule and tried to implement a MyDslStringValueConverter.
	@Override
	public Class<? extends IValueConverterService> bindIValueConverterService() {
	  return MyDslStringValueConverter.class;
	}


import org.eclipse.xtext.conversion.IValueConverterService;
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.parsetree.AbstractNode;

public class MyDslStringValueConverter implements IValueConverterService {

	@Override
	public Object toValue(String string, String lexerRule, AbstractNode node)
			throws ValueConverterException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String toString(Object value, String lexerRule) {
		// TODO Auto-generated method stub
		return null;
	}
}


But where do I configure that my rule STRING should only return its value?

Do I now have to implement a value converter for each of my rules in the overridden method toValue() ?

I thought I could rather implement something like that:
public class MyDslStringValueConverter extends AbstractLexerBasedConverter<String> {

	@Override
	public String toValue(String string, AbstractNode node)
			throws ValueConverterException {
		return node.getElement().toString();
	}
}


But I don't know how to bind it...

[Updated on: Fri, 25 June 2010 08:25]

Report message to a moderator

Re: Overriding terminal StRING [message #542532 is a reply to message #542524] Fri, 25 June 2010 08:38 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Extend the DefaultTerminalConverters (that class gives you an idea what
to do) and see how it is bound in the DefaultRuntimeModule.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Overriding terminal STRING [message #542987 is a reply to message #542532] Mon, 28 June 2010 06:51 Go to previous message
Alex is currently offline AlexFriend
Messages: 114
Registered: June 2010
Senior Member
I did following:
public class MyDslRuntimeModule extends com.bosch.AbstractTpsRuntimeModule {

	@Override
	public Class<? extends org.eclipse.xtext.conversion.IValueConverterService> bindIValueConverterService() {
		return MyTerminalConverters.class;
	}
}

public class MyTerminalConverters extends DefaultTerminalConverters {
	
	
	public static class STRINGValueConverter extends AbstractValueConverter<String> {
		public String toString(String value) {
			if (value == null)
				throw new ValueConverterException("STRING-value may not be null.", null, null);
			return '"' + Strings.convertToJavaString(value, false) + '"';
		}

		public String toValue(String string, AbstractNode node) {
			if (string == null)
				return null;
			try {
				return string.substring(1,string.length()-1);	// return value of string (without '"')
			} catch (IllegalArgumentException e) {
				throw new ValueConverterException(e.getMessage(), node, e);
			}
		}
	}
	
	@Inject
	private org.eclipse.xtext.conversion.impl.STRINGValueConverter stringValueConverter;
	
	@ValueConverter(rule = "STRING")
	public IValueConverter<String> STRING() {
		return stringValueConverter;
	}
	
}


But nevertheless I get the parser error "illegal escape character \$" for a input a = "hello\$world"

the rules for Strings are
terminal STRING:
	'"' (STR_ESC | !('"'|'\r'|'\n'|'\\'))* '"'
	;
terminal STR_ESC:
	'\\'('b'|'t'|'n'|'f'|'r'|'"'|'\\'|'$')
	;




EDIT:
I forgot to change
	@Inject
	private org.eclipse.xtext.conversion.impl.STRINGValueConverter stringValueConverter;
to my own STRINGValueConverter

Sorry for that!


[Updated on: Mon, 28 June 2010 07:04]

Report message to a moderator

Previous Topic:Building with Maven xText 1.0
Next Topic:how to get cross reference with Standalone mode
Goto Forum:
  


Current Time: Fri Sep 20 09:50:01 GMT 2024

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

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

Back to the top