我需要以編程方式將符合XText語法的文本轉換爲符合由XText從同一語法生成的Ecore元模型的AST。XText以編程方式將DSL腳本解析爲Ecore模型
我知道XText也生成實現這種解析器的Java類,但我不知道它們在哪裏以及如何使用它。
我需要以編程方式將符合XText語法的文本轉換爲符合由XText從同一語法生成的Ecore元模型的AST。XText以編程方式將DSL腳本解析爲Ecore模型
我知道XText也生成實現這種解析器的Java類,但我不知道它們在哪裏以及如何使用它。
有關此問題的完整答案可以在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
)更改爲您自己的語言擴展。
下面的代碼:
@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見。