2012-08-08 45 views

回答

7

有關此問題的完整答案可以在Eclipse wiki的Xtext page上找到。

new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../"); 
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration(); 
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class); 
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE); 
Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mydsl")); 
InputStream in = new ByteArrayInputStream("type foo type bar".getBytes()); 
resource.load(in, resourceSet.getLoadOptions()); 
Model model = (Model) resource.getContents().get(0); 

將文件擴展名(mydsl)更改爲您自己的語言擴展。

4

下面的代碼:

@Inject 
ParseHelper<Domainmodel> parser 

def void parseDomainmodel() { 
    // When in a vanilla Java application (i.e. not within Eclipse), 
    // you need to run a global setup: 
    val injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration 
    injector.injectMembers(this) // sets the field 'parser' 

    // this is how you can use it: 
    val model = parser.parse(
    "entity MyEntity { 
     parent: MyEntity 
    }") 
    val entity = model.elements.head as Entity 
    assertSame(entity, entity.features.head.type) 
} 

http://www.eclipse.org/Xtext/documentation.html#TutorialUnitTests見。