Translate Template Proposal Pattern into Xtext Model [message #1820591] |
Thu, 23 January 2020 10:16  |
Eclipse User |
|
|
|
I followed https://www.eclipse.org/Xtext/documentation/310_eclipse_support.html#templates to add default template proposals to my Xtext plugin. Those templates and probably templates added by the user at some point should be used during the creation of a new model, which is done via a wizard. There the user enters some information and new model files are created.
I produced a minimal example:
The Grammar
Model:
lastnames+=LastName*
greetings+=Greeting*;
Greeting:
'Hello' name=ID lastname=[LastName] '!';
LastName:
'lastname' name=ID;
Example Template:
<template autoinsert="true" context="MyDsl.Greeting" deleted="false" description="Just to explain the question" enabled="true" name="MinimalExample" id="1">
Hello ${name} Smith!
</template>
As you can see I don't use a reference to another object "LastName" but just write text. I know that for this to work, there has to be a "lastname Smith" in my model, there is.
I can get all the templates for a specific context with this:
TemplateStore templateStore = Activator.getDefault().getInjector("MyDSL").getInstance(TemplateStore.class);
Template[] templates = templateStore.getTemplates("MyDSL.Greetings"); and access their pattern. But that is just a String, I need the actual model objects to e.g. give the Greeting "Hello ${name} Smith!" a name programmatically.
Now finally the question:
Is it possible to extract the model entities from this pattern, so that I have a LastName object with name:=Smith and a Greeting object with name:=null (or sth else?) and lastname:=referenceToLastNameWith(name=Smith)?
What I thought of so far is: Replacing all variables in the template pattern String by something random, save it as a .mydsl resource and load it with
(Model) resourceSet.getResource(uri, true).getContents().get(0);
but it feels like a big detour.
Thanks for your help,
Saphira
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Translate Template Proposal Pattern into Xtext Model [message #1821103 is a reply to message #1821012] |
Wed, 05 February 2020 11:08  |
Eclipse User |
|
|
|
So I resolved the variables with
TemplateTranslator templateTranslator = new TemplateTranslator();
TemplateBuffer buffer = null;
try {
buffer = templateTranslator.translate(template);
} catch (TemplateException e) {
e.printStackTrace();
}
The buffer contains the template pattern as a String where all variables (e.g. ${name}) are replaced by their names (here: name) and a TemplateVariable array. The String can be parsed as above.
The crossreference to the dbtype (which will never exist) is resolved by hand (buffer.getString().contains(dbTypeName)), a dbType is created and added to the parsed model. It's not perfect but sufficient.
Thanks for your help!
[Updated on: Wed, 05 February 2020 11:13] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.05946 seconds