2012-08-08 2 views
7

동일한 문법의 XText로 생성 된 Ecore 메타 모델을 준수하는 AST로 XText 문법을 준수하는 프로그래밍 방식으로 텍스트를 프로그래밍해야합니다.XText는 프로그래밍 방식으로 DSL 스크립트를 Ecore 모델로 구문 분석합니다.

나는 XText가 파서를 구현하는 자바 클래스도 생성한다는 것을 알고 있지만, 어디에서 어떻게 사용하는지 알지 못한다.

@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 참조 :

답변

7

이 질문에 대한 완전한 대답은 Eclipse 위키의 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

여기에 코드입니다.