Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generating code automatically on the basis of Xtext grammar(Generate DSL language calling Java or Xtend methods, using grammar written in Xtext)
Generating code automatically on the basis of Xtext grammar [message #1778134] Mon, 11 December 2017 07:40 Go to next message
Albert Houston is currently offline Albert HoustonFriend
Messages: 8
Registered: December 2017
Junior Member
Hello.

I use Xtext to create my own DSL, and it works fine to define its structure and validate it.
Very useful function would be possibility to automatically generate DSL code regarding grammar. I mean, if I have following grammar written in Xtext, for example a rule:

Protocol :
     'protocol' '=' (protocol = PROTOCOL_TYPE)+ ';'

where PROTOCOL_TYPE is terminal with some types like 'http', 'ssh', etc., I want to create new Protocol instance calling something like that:

protocol = new Protocol("http","https");


to obtain following output/generated code:

 
protocol = http, https;


Is there any possibility to obtain such effect? Is it possible using Xtext Java classes/interfaces?
Re: Generating code automatically on the basis of Xtext grammar [message #1778177 is a reply to message #1778134] Mon, 11 December 2017 13:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you can use ISerializer or Resource.save to serialize a ast to a model file.
can you give more context "where" you want to do such a transformation?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating code automatically on the basis of Xtext grammar [message #1778183 is a reply to message #1778177] Mon, 11 December 2017 13:56 Go to previous messageGo to next message
Albert Houston is currently offline Albert HoustonFriend
Messages: 8
Registered: December 2017
Junior Member
Thanks for reply, I will check what did you described.
I have Eclipse SWT user interface to collect data via textboxes, checkboxes etc., and then, it is necessary to convert that data into my DSL syntax. So, I plan to connect my SWT application with Xtext application. in other words, not manually hard-coding my DSL content, but "coding" by entering data into GUI and then, Xtext dependencies would "wrap" it with grammar and DSL stuff.
Re: Generating code automatically on the basis of Xtext grammar [message #1778184 is a reply to message #1778183] Mon, 11 December 2017 13:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
creating a resource, adding the ast object to it, and call save should do the trick

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating code automatically on the basis of Xtext grammar [message #1778227 is a reply to message #1778184] Tue, 12 December 2017 07:15 Go to previous messageGo to next message
Albert Houston is currently offline Albert HoustonFriend
Messages: 8
Registered: December 2017
Junior Member
Can you pass some example how to correctly create such resource? Considering ISerializer, I found that class is rather deprecated.
Re: Generating code automatically on the basis of Xtext grammar [message #1778229 is a reply to message #1778227] Tue, 12 December 2017 07:32 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
// pseudo code
URI uri = URI.createURI("platform:/resource/myproject/myfolder/demo.mydsl")
IResourceSetProvider resourceSetProvider = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(uri).get(IResourceSetProvider.class);
ResourceSet resourceSet = resourceSetProvider.get(project);
Resource r = resourceSet.createResource(uri);


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Can I modify a java file using XTend?
Next Topic:UnitTest fails when running through Gradle
Goto Forum:
  


Current Time: Fri Apr 19 22:24:55 GMT 2024

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

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

Back to the top