Xtext grammar to Java Code [message #1823622] |
Mon, 30 March 2020 12:39 |
Muhammad Salman Tayyab Messages: 36 Registered: March 2020 Location: Germany |
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»
}
''')
}
}
}
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: 36 Registered: March 2020 Location: Germany |
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 55 times)
Salman
[Updated on: Mon, 11 May 2020 13:45] Report message to a moderator
|
|
|
Re: Xtext grammar to Java Code [message #1827255 is a reply to message #1827249] |
Mon, 11 May 2020 14:58 |
|
i still dont understand your question:
both have a list
so you can ask a Video object for videos (getVideos())
and a Videouse for viduse (getViduse())
which are both ordered as declared.
starting from
Model:
parts+=ModelPart*
you can either filter the parts for Video or Videouse using model.getParts().filter(Video)
of you use instanceof
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Re: Xtext grammar to Java Code [message #1827260 is a reply to message #1827259] |
Mon, 11 May 2020 15:54 |
|
(1) before you can generate code you need to figure out how the generated code should look like. this is a manual process. the process is the same if you would write the code by hand without generator
(2) you identify static and dynamic parts in that code
(3) you take that over to the generator. if you dont feel well you can also write it in java instead of xtend
have a look at the tutorial part on code generation
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Powered by
FUDForum. Page generated in 0.06110 seconds