2012-08-08 30 views
7
programlı bir metin aynı gramer dan Xtext tarafından oluşturulan bir Ecore meta modele uygun bir AST içine Xtext dilbilgisi uygun açmanız gerekir

içine bir DSL senaryoyu ayrıştırmak Xtext.programlı bir Ecore modeli

Ben Xtext ayrıca ayrıştırıcı uygulayan Java sınıfları oluşturmak biliyorum ama ya nerede ve nasıl kullanılacağını bilmiyorum.

@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) 
} 

da http://www.eclipse.org/Xtext/documentation.html#TutorialUnitTests bakınız:

cevap

7

Bu soruya tam bir cevap Eclipse wiki'nin Xtext page bulunabilir.

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); 

değiştir kendi dil uzatma dosya uzantısı (mydsl).

4

İşte kod.