同じ文法からXTextで生成されたEcoreメタモデルに準拠するASTに、プログラムでXText文法に準拠したテキストを変換する必要があります。XTextは、DSLスクリプトをEcoreモデルにプログラムで解析します。
私はXTextもこのようなパーサーを実装するJavaクラスを生成することを知っていますが、どこにあるのか、どのように使用するのかはわかりません。ここで
同じ文法からXTextで生成されたEcoreメタモデルに準拠するASTに、プログラムでXText文法に準拠したテキストを変換する必要があります。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参照してください。