Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Overriding terminal StRING
Overriding terminal StRING [message #542495] Fri, 25 June 2010 02:01 Go to next message
Eclipse UserFriend
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 02:46 Go to previous messageGo to next message
Eclipse UserFriend
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 04:19 Go to previous messageGo to next message
Eclipse UserFriend
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 04:25] by Moderator

Re: Overriding terminal StRING [message #542532 is a reply to message #542524] Fri, 25 June 2010 04:38 Go to previous messageGo to next message
Eclipse UserFriend
Extend the DefaultTerminalConverters (that class gives you an idea what
to do) and see how it is bound in the DefaultRuntimeModule.

Alex
Re: Overriding terminal STRING [message #542987 is a reply to message #542532] Mon, 28 June 2010 02:51 Go to previous message
Eclipse UserFriend
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 03:04] by Moderator

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


Current Time: Sat Jul 19 17:28:06 EDT 2025

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

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

Back to the top