Xtext grammar to Java Code [message #1823622] |
Mon, 30 March 2020 12:39  |
Muhammad Salman Tayyab Messages: 26 Registered: March 2020 Location: Germany |
Junior Member |
|
|
I have a simple grammar which is listed below
Model:
(videos+=video)*;
video:
name=ID "=" STRING ";";
AND
I want to generate the java code for this.i have read the documentation but could not get it. Please guide me on how to do it.
Salman
|
|
|
|
|
Re: Xtext grammar to Java Code [message #1823630 is a reply to message #1823626] |
Mon, 30 March 2020 14:05   |
|
this should be straight forward from the tutorial
i change the grammar to assign the string. am not sure if this is a C&P error
video:
name=ID "=" value=STRING
class MyDslGenerator extends AbstractGenerator {
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
for (m : resource.allContents.toIterable.filter(Model)) {
fsa.generateFile("sample/Demo.java", '''
package sample;
public class Demo {
«FOR v : m.videos»
public static final String «v.name» = "«v.value»";
«ENDFOR»
}
''')
}
}
}
Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Xtext grammar to Java Code [message #1827246 is a reply to message #1827238] |
Mon, 11 May 2020 12:04   |
|
Frankly speaking we can help you how to generate Java code, but first you have to decide it yourself what java code to generate.
To come up with some ideas, maybe you can write some Java code by hand, belonging to different 'videos dsl' files, than you can generalize them into a template that you integrate into your MyDslGenerator.
Hope that helps,
Tamás
|
|
|
Re: Xtext grammar to Java Code [message #1827249 is a reply to message #1827246] |
Mon, 11 May 2020 13:05   |
Muhammad Salman Tayyab Messages: 26 Registered: March 2020 Location: Germany |
Junior Member |
|
|
Now let me explain in detail.
I have different variables and all are in video declaration(see the grammar and attached picture). Now I want to create a function that takes the variables as in the same order they are defined. I am finding difficulty in writing the code for the function. You can also see the grammar and modify it as per your need.
Attachment: xtext.PNG
(Size: 12.10KB, Downloaded 19 times)
Salman
[Updated on: Mon, 11 May 2020 13:45] Report message to a moderator
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02368 seconds