DevBooststLogotudLogorewerseLogo modelplexLogo
crimsonBG
EMFText
The fastest way to refinable, durable and evolution-resistant textual syntaxes for EMF models!

Contents

How do I change the look of my textual models?

There are two possibilities to change the highlighting:

  1. When you run your generated plugins in eclipse you can go to the preferences and to "EMFText Syntax Coloring". There you can define the look for each type of token and keyword in your language individually.
  2. The probably better solution is to use "TOKENSTYLES" in you CS specification. There you can define the default look for each of your token types and keywords. Look at this grammar (or at others in the zoo) for examples:

EMFText_Concrete_Syntax_Zoo_Text_Adventure

Note that some tokens are available by default (e.g., "TEXT" or "QUOTED_34_34" - for ['"','"']). You can see these tokens in the outline view.

How do I get an XMI version of my textual model?

This is simple: Open the file in the text editor generated for your language. Use File > Save as... and save the file at a new location with the extension "xmi". Done.

If you have two (or more) text syntaxes defined for your metamodel, which would have different file extensions, File > Save as... can also be used to convert models from one syntax to another.

If you want to do it programmatically, you can use code like this:

 // If you work outside Eclipse, you first have to register the different   
 // resource factories with EMF:
 
 //register <DSL>ResourceFactory for "dslExtension" file extension 
 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(
     ).put("dslExtension", new <DSL>ResourceFactory());
    
 //register XMI resource factory for all other extensions
 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(
   ).put(Resource.Factory.Registry.DEFAULT_EXTENSION,
         new XMIResourceFactoryImpl());
 
 // Then you can load and save resources in the different 
 // formats using a resource set:
 ResourceSet rs = new ResourceSetImpl();
 
 //load xmi resource
 Resource xmiResource = rs.getResource(
     URI.createFileURI("MyFile.xmi"),true);
 
 //create an empty DSL resource (will be an instance of <DSL>Resource)
 Resource dslResource = rs.createResource(
     URI.createFileURI("FileToPrintTo.dslExtension"));
 
 //transfer content from XMI to DSL resource
 dslResource.getContents().addAll(xmiResource.getContents());
 
 //save the dsl resource (this will automatically use the <DSL>Printer)
 dslResource.save(null); 

How can I use EMFText resources outside of Eclipse?

EMFText resources can be loaded without running Eclipse. To do so, the generated resource factory must be explicitly registered with the Ecore resource set. The following code shows how to load an EMFText resource and print all objects to System.out:

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

public class UseDSLOutsideEclipse {

	public static void main(String[] args) throws IOException {
		MyGeneratedResourceFactory factory = new MyGeneratedResourceFactory();

		ResourceSet rs = new ResourceSetImpl();
		rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
			"myFileExtension", factory
		);
		
		Resource resource = rs.getResource(URI.createFileURI(
                                        "MyFile.myFileExtension"), true);
		
		Iterator<EObject> i = resource.getAllContents();
		while (i.hasNext()) {
			Object o = i.next();
			System.out.println(o);
		}
	}
}

Can EMFText be used with the Eclipse Modeling Workflow Engine (MWE)?

EMFText implements the EMF resource API. Hence, in principle it is possible to transform models parsed by EMFText using MWE workflows and transformations based on Xpand and Xtend. By default, MWE workflows are not aware of generated resource factories. To register parsers generated by EMFText you may subclass the MWE Reader as follows:

public class Reader extends org.eclipse.emf.mwe.utils.Reader {
      
       public Reader(){
           super();

           //register your resource factory
           Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
               "<YOUR_FILE_EXTENSION>", 
               new <YOUR_LANGUAGE_NAME>ResourceFactory()
           );
           
           //register all ecore models needed for reference resolving as follows:	
           EPackage.Registry.INSTANCE.put(
               "<METAMODEL_NAMESPACE_URI>", 
               <METAMODEL_PACKAGE_NAME>Impl.init()
           );
       }
}

Note for EMFText 1.2.0: To parse xpt files, Xpand (as shipped with MWE 0.7.2) uses a version of the ANTLR runtime which is incompatible with the one used in EMFText 1.2.0. We have fixed this issue in later releases. If you really need to stick with EMFText 1.2.0, As a workaround, you may renamed the package names of the generated ANTLR runtime plugin to org.emftext.commons.antlr3_1_1.