Eclipse XText以编程方式将DSL脚本解析为Ecore模型

Eclipse XText以编程方式将DSL脚本解析为Ecore模型,eclipse,eclipse-emf,xtext,Eclipse,Eclipse Emf,Xtext,我需要以编程方式将符合XText语法的文本转换为符合XText从同一语法生成的Ecore元模型的AST 我知道XText也会生成实现这种解析器的Java类,但我也不知道它们在哪里以及如何使用它。下面是代码: @Inject ParseHelper<Domainmodel> parser def void parseDomainmodel() { // When in a vanilla Java application (i.e. not within Eclipse),

我需要以编程方式将符合XText语法的文本转换为符合XText从同一语法生成的Ecore元模型的AST

我知道XText也会生成实现这种解析器的Java类,但我也不知道它们在哪里以及如何使用它。

下面是代码:

@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)
}
@Inject
ParseHelper解析器
def void parseDomainmodel(){
//在普通Java应用程序中(即不在Eclipse中)时,
//您需要运行全局设置:
val injector=new MyDslStandaloneSetup()。createInjectorAndDoEMFRegistration
injector.injectMembers(this)//设置字段“parser”
//您可以这样使用它:
val model=parser.parse(
“我的实体”{
家长:MyEntity
}")
val实体=model.elements.head作为实体
assertSame(实体,实体.特征.头.类型)
}

另请参见。

此问题的完整答案可以在Eclipse wiki的上找到

neworg.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri(“/”);
Injector Injector=新的MyDSLSStandalonesetup()。CreateInjector和DoemfRegistration();
XtextResourceSet resourceSet=injector.getInstance(XtextResourceSet.class);
addLoadOption(XtextResource.OPTION\u RESOLVE\u ALL,Boolean.TRUE);
Resource Resource=resourceSet.createResource(URI.createURI(“dummy:/example.mydsl”);
InputStream in=newbytearrayinputstream(“type foo type bar.getBytes());
load(在,resourceSet.getLoadOptions()中);
Model Model=(Model)resource.getContents().get(0);
将文件扩展名(
mydsl
)更改为您自己的语言扩展名