2012-08-08 6 views
7

Potrzebuję programowo przekształcić tekst zgodny z gramatyką XText w AST zgodny z meta-modelem Ecore wygenerowanym przez XText z tej samej gramatyki.XText programowo analizuje skrypt DSL w modelu Ecore

Wiem, że XText również generuje klasy Java implementujące taki parser, ale nie wiem, gdzie są i jak z nich korzystać.

Odpowiedz

7

Pełna odpowiedź na to pytanie znajduje się na stronie wiki Eclipse pod adresem 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); 

Zmień rozszerzenie pliku (mydsl) do własnego numeru języka.

4

Oto kod:

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

Zobacz także http://www.eclipse.org/Xtext/documentation.html#TutorialUnitTests.