|
|
|
Re: Unability to serialize an Xtext grammer created with EMF [message #1853900 is a reply to message #1853895] |
Thu, 21 July 2022 15:33 |
|
what i dont understand is the copy part and how this one will/should survive the move to the new grammar.
so you do not only create a grammar programmatically but also do that one.
so how will the references from the old grammar be moved to the new grammar.
my proposal would be to just do it on text level.
this naive one is working for me
public class Snippet {
public static void main(String[] args) throws IOException {
// TerminalsStandaloneSetup.doSetup();
Injector i = new XtextStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet sourceRS = i.getInstance(ResourceSet.class);
Resource sourceR = sourceRS.getResource(URI.createURI("/home/dietrich/eclipse-workspaces/xxxxxxxxxx/org.xtext.example.mydsl/src/org/xtext/example/mydsl/MyDsl.xtext"), true);
sourceR.load(null);
EcoreUtil.resolveAll(sourceR);
System.out.println(sourceR.getErrors());
ResourceSet targetRS = i.getInstance(ResourceSet.class);
Resource targetR = targetRS.createResource(URI.createURI("MyDsl2.xtext"));
Grammar grammar = XtextFactory.eINSTANCE.createGrammar();
grammar.setName("Demo");
GeneratedMetamodel x = XtextFactory.eINSTANCE.createGeneratedMetamodel();
x.setName("bla");
EPackage p = EcoreFactory.eINSTANCE.createEPackage();
p.setName("bla");
p.setNsPrefix("bla");
p.setNsURI("http://demo");
x.setEPackage(p);
grammar.getMetamodelDeclarations().add(x);
targetR.getContents().add(grammar);
Grammar sourceG = (Grammar) sourceR.getContents().get(0);
List<AbstractRule> rulesList = new ArrayList<>();
for (AbstractRule r : sourceG.getRules()) {
rulesList.add(r);
}
for (AbstractRule r : rulesList) {
grammar.getRules().add(r);
}
for (Grammar ug : sourceG.getUsedGrammars()) {
targetRS.getResources().add(ug.eResource());
grammar.getUsedGrammars().add(ug);
}
ISerializer ser = i.getInstance(ISerializer.class);
System.out.println(ser.serialize(grammar));
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
[Updated on: Thu, 21 July 2022 15:40] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03123 seconds