|
|
Re: Overriding terminal StRING [message #542524 is a reply to message #542507] |
Fri, 25 June 2010 08:19 |
Alex 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 #542987 is a reply to message #542532] |
Mon, 28 June 2010 06:51 |
Alex 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
|
|
|
Powered by
FUDForum. Page generated in 0.04321 seconds