Overriding terminal StRING [message #542495] |
Fri, 25 June 2010 02:01  |
Eclipse User |
|
|
|
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 evokes a syntax error "Illegal escape character \$"
Is there anything that I should configure in the parser?
- Alex
|
|
|
|
|
|
Re: Overriding terminal STRING [message #542987 is a reply to message #542532] |
Mon, 28 June 2010 02:51  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.04268 seconds