Value converters are registered to convert the parsed text into a certain data type instance and vice versa. The primary hook is called IValueConverterService and the concrete implementation can be registered via the runtime Guice module. To do so override the corresponding binding in your runtime module like shown in this example:
@Override
public Class<? extends IValueConverterService> bindIValueConverterService() {
return MySpecialValueConverterService.class;
}
The most simple way to register additional value converters is to make use of AbstractDeclarativeValueConverterService, which allows to declaratively register an IValueConverter by means of an annotated method.
If you use the common terminals grammar org.eclipse.xtext.common.Terminals you should subclass DefaultTerminalConverters and override or add additional value converters by adding the respective methods. In addition to the explicitly defined converters in the default implementation, a delegating converter is registered for each available EDataType that reuses the functionality of the corresponding EMF EFactory.
As qualified names – i.e. names composed of namespaces separated by a delimiter – are expected to occur often, we’ve added a QualifiedNameValueConverter. This one removes comments and whitespaces and delegates to another value converter for each segment, allowing individually quoted segments. The domainmodel example shows how to use it.
The protocol of an IValueConverter allows to throw a ValueConverterException if something went wrong. The exception is propagated as a syntax error by the parser or as a validation problem by the ConcreteSyntaxValidator if the value cannot be converted to a valid string. The AbstractLexerBasedConverter is useful when implementing a custom value converter. If the converter needs to know about the rule that it currently works with, it may implement the interface IValueConverter.RuleSpecific. The framework will set the rule such as the implementation may use it afterwards.