Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Calling parser/generator programmatically(How to call xtext parser and generator programmatically in a Web project)
Calling parser/generator programmatically [message #1220230] Tue, 10 December 2013 09:02 Go to next message
Eclipse UserFriend
Hi guys,

I am creating a new grammar and code generator but I don't want to use it in an Eclipse editor. I actually would like to create a REST service to receive the file the unit to be compiled and then code would be generated. For this, I would need to call the xtext parser and generator programmatically within a Java Web project (tomcat).

How can I do it?

In this same context, another question is: is it ok to return a JSON resource with the possible compilation errors present on the given unit from REST service call?

Thanks in advance,

[Updated on: Tue, 10 December 2013 09:50] by Moderator

Re: Calling parser/generator programmatically [message #1220243 is a reply to message #1220230] Tue, 10 December 2013 09:59 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

this should work straight forward

	Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		ResourceSet resourceSet = injector.getInstance(ResourceSet.class);
		Resource r = resourceSet.getResource(URI.createURI("x.mydsl"), true);
		r.load(null);
		IGenerator generator = injector.getInstance(IGenerator.class);
		InMemoryFileSystemAccess fsa = injector.getInstance(InMemoryFileSystemAccess.class);
		generator.doGenerate(r, fsa);
		for (CharSequence s : fsa.getTextFiles().values()) {
			System.out.println(s);
		}
Re: Calling parser/generator programmatically [message #1220274 is a reply to message #1220243] Tue, 10 December 2013 13:40 Go to previous messageGo to next message
Eclipse UserFriend
However, and unfortunately it does not work. I cannot access classes like "MyDslStandaloneSetup" while executing an instance of eclips with the language plugin installed. My idea is:

1) install the language plugin in an Eclipse instance, with built-in parser and generator
2) With the plugin installed, create a web project, which contains a REST service
3) Through the REST service I am going to receive a content following the described grammar.
4) Using the built-in parser and compiler (programmatically) I would like to compile and generate code for that language.
5) If possible I would like also to return compilation errors to the user.

Imagine that as an architeture of a web-based editor for a DSL that uses xtext in the back end. The remote editor should be able to generate a set of resorces, and return compilation errors to the remote user.

How can I do it?

Thanks in advance,
Re: Calling parser/generator programmatically [message #1220275 is a reply to message #1220274] Tue, 10 December 2013 14:00 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

sorry i still do not understand what you mean,
i thought you want to call this code from withing an web application (WAR) and not from within eclipse


but if you call this from an osgi envionment you can indeed not call standalonesetup. but you can something like the combination
- of my previous post
- http://koehnlein.blogspot.de/2012/11/xtext-tip-how-do-i-get-guice-injector.html
- http://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/

Re: Calling parser/generator programmatically [message #1220429 is a reply to message #1220275] Wed, 11 December 2013 12:13 Go to previous messageGo to next message
Eclipse UserFriend
I think it might work. Thanks
Re: Calling parser/generator programmatically [message #1721234 is a reply to message #1220429] Mon, 25 January 2016 23:43 Go to previous messageGo to next message
Eclipse UserFriend
Hi ,

I would like to ask this question again as the answers might be updated.?

Thanks In Advance
Re: Calling parser/generator programmatically [message #1721272 is a reply to message #1721234] Tue, 26 January 2016 04:05 Go to previous message
Eclipse UserFriend
In Lorenzo Bettinis famous book near the end of chapter 7, there is something about unit-testing eclipse functionality like opening editors etc.
Perhaps this may give you some hint how you con start re-generation for some DSL files.

And at the very end of chapter 5, there are some instructions how to get a "standalone command-line compiler" which takes the DSL file path as an argument for the generator.
So it's just a one-liner in a batch command file

Greetings from Frankfurt/Germany,
Uli

[Updated on: Tue, 26 January 2016 08:34] by Moderator

Previous Topic:Problems with code generator
Next Topic:Splitting a model into several files? Scoping?
Goto Forum:
  


Current Time: Sun Jul 27 11:30:25 EDT 2025

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

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

Back to the top