Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Unability to serialize an Xtext grammar created with EMF(Xtext Serializer for Xtext grammar creation)
Unability to serialize an Xtext grammar created with EMF [message #1853849] Tue, 19 July 2022 17:00 Go to next message
Jules BOYER is currently offline Jules BOYERFriend
Messages: 2
Registered: July 2022
Junior Member
Dear all,

I am looking for help in using the Xtext serializer to save an Xtext grammar that was programmatically created as an EMF resource.

My various attempts always fail when it comes to using the save instruction from the XtextResource (log error in screenshot below).

My project consists in creating an Xtext grammar resulting from the merge of several grammars, after which this new grammar needs to be serialized. While the creation of the new grammar as well as the imports of the rules coming from the other grammars work perfectly, exceptions are triggered when it comes to saving the resource. I have been looking for examples that showcase the use of the resource and serializer in similar cases, but did not manage to find anything relevant.

Please find below other pieces of information and screenshots featuring how I use the serializer and what error is triggered in the process.

I use eclipse 2022-06 and org.eclipse.justj.openjdk.hotspot.jre.full.linux.x86_64_18.0.1.v20220515-1614 as a JRE.

Here is the constructor in wich you can see how the serializer is created starting from line 132:

index.php/fa/42315/0/
index.php/fa/42316/0/

Here is how I generate the new Xtext Grammar using the previous serializer and options:

index.php/fa/42317/0/

Here is the log error printed when trying to run it:

index.php/fa/42318/0/

[Updated on: Thu, 21 July 2022 13:02]

Report message to a moderator

Re: Unability to serialize an Xtext grammer created with EMF [message #1853854 is a reply to message #1853849] Wed, 20 July 2022 04:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14678
Registered: July 2009
Senior Member
The code looks very strange
Normally stuff is created using injectors and not using new


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unability to serialize an Xtext grammer created with EMF [message #1853895 is a reply to message #1853854] Thu, 21 July 2022 13:02 Go to previous messageGo to next message
Jules BOYER is currently offline Jules BOYERFriend
Messages: 2
Registered: July 2022
Junior Member
Hello,

Thanks for answering, if you know how to make a "normal" code used to generate an Xtext grammar programatically, do you mind sharing an example ?
Or if you don't have one, can you maybe further explain the procedure in making it ?

Unfortunately, I won't be able to progress much just by knowing I need to use injectors.

Thanks in advance ,
Re: Unability to serialize an Xtext grammer created with EMF [message #1853900 is a reply to message #1853895] Thu, 21 July 2022 15:33 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14678
Registered: July 2009
Senior Member
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

[Updated on: Thu, 21 July 2022 15:40]

Report message to a moderator

Previous Topic:Formatting inserts spaces despite hidden WS
Next Topic:Xtext & Xtend Release 2.27.0 is available now
Goto Forum:
  


Current Time: Wed May 22 03:20:49 GMT 2024

Powered by FUDForum. Page generated in 0.03365 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top