Skip to main content



      Home
Home » Modeling » TMF (Xtext) » STRING serialization not producing quotes if no whitespace
STRING serialization not producing quotes if no whitespace [message #885083] Tue, 12 June 2012 08:40 Go to next message
Eclipse UserFriend
Hello,

I would like to serialize ecore files conforming to an xtext grammar. I bound the ecore EString type to the STRING terminal (not STRING | ID, as automatically generated when deriving ecore with xtext) because I want strings to be single/double quoted.

The grammar works as expected with the plugin. The problem is that when I tried to serialize an xmi resource through my grammar, EString are not quoted if they do not contain whitespaces (as if they were serialized to ID, despite my grammar).

Any hint on this? Thanks.

[Updated on: Tue, 12 June 2012 08:42] by Moderator

Re: STRING serialization not producing quotes if no whitespace [message #885291 is a reply to message #885083] Tue, 12 June 2012 14:53 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

hmm can you share you grammar? and maybe a unit test?
if org.eclipse.xtext.conversion.impl.STRINGValueConverter is used the quotes seem to be added always.

~Christian
Re: STRING serialization not producing quotes if no whitespace [message #885684 is a reply to message #885291] Wed, 13 June 2012 08:40 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian,

After some more research, I found that Xtext calls org.eclipse.xtext.common.services.Ecore2XtextTerminalConverters when it encounters an EString. And it effectively tries to match the ID_PATTERN before trying to make it fit into a STRING (see Ecore2XtextTerminalConverters). Quite logical.

The problem is the following rule I would like to work when serializing:
EString returns ecore::EString:
	STRING; // instead of ID | STRING


But it seems that xtext does not allow ecore data type to be "redefined".
When serializing ecore data types, xtext simply ignore such rules by considering ecore data types as terminals. Making serialized ecore to not conform to the grammar.

Any way to override Ecore2XtextTerminalConverters?

[Updated on: Wed, 13 June 2012 08:43] by Moderator

Re: STRING serialization not producing quotes if no whitespace [message #885702 is a reply to message #885684] Wed, 13 June 2012 08:58 Go to previous messageGo to next message
Eclipse UserFriend
Hi you should be able to exchange it with Google guice
Re: STRING serialization not producing quotes if no whitespace [message #885710 is a reply to message #885702] Wed, 13 June 2012 09:16 Go to previous message
Eclipse UserFriend
Done it!

Here is the solution in case anyone run into a similar situation.

I extended Ecore2XtextTerminalConverters to always convert ecore::EString to xtext::STRING:
package your.package;

import org.eclipse.xtext.common.services.Ecore2XtextTerminalConverters;
import org.eclipse.xtext.conversion.IValueConverter;
import org.eclipse.xtext.conversion.ValueConverter;
import org.eclipse.xtext.conversion.impl.AbstractNullSafeConverter;
import org.eclipse.xtext.nodemodel.INode;

public class EString2XtextConverter extends Ecore2XtextTerminalConverters {
	
	@ValueConverter(rule = "EString")
	public IValueConverter<String> EString() {
		return new AbstractNullSafeConverter<String>() {
			@Override
			protected String internalToValue(String string, INode node) {
				return STRING().toValue(string, node);
			}

			@Override
			protected String internalToString(String value) {
				return STRING().toString(value);
			}
		};
	}
}


And registered it in the RuntimeModule:

public class YourDslRuntimeModule extends your.package.AbstractYourDslRuntimeModule {
	
	@Override
	public Class<? extends IValueConverterService> bindIValueConverterService() {
		return EString2XtextConverter.class;
	}
	
}


Thx Christian.
Previous Topic:XTend 2.2.1 installation broken? (NPE at BuilderParticipant.java:184)
Next Topic:Trying to include templates.xml in xtext plugin
Goto Forum:
  


Current Time: Wed Jul 23 15:56:15 EDT 2025

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

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

Back to the top