xtend code [message #1833775] |
Fri, 23 October 2020 16:23 |
Muhammad Salman Tayyab Messages: 36 Registered: March 2020 Location: Germany |
Member |
|
|
Hey, I have written the Java code and the grammar also. Now, I am facing difficulty in writing the xtend code. I am attaching everything please have a look and help. Thanks in advance.
Grammar
Model:
parts+=ModelPart*;
ModelPart:
Video | Videouse;
Video:
"video_list" "{" videos+=Videodef* "}";
Videodef:
name=ID "=" value=STRING ";";
Videouse:
"actions" "{" playvideo+=Videoshow* "}";
Videoshow:
"show(" vid=[Videodef] ");";
Java Code
import java.util.Scanner;
public class grammarcode {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
String[] videos= new String[2];
// String[] actions;
System.out.println("Please enter the videos name to play");
for(int i=0;i<(videos.length);i++) {
videos[i]=input.nextLine();
}
System.out.println("video_list{");
for(int i=0;i<(videos.length);i++) {
System.out.println(videos[i] +";");
}
System.out.println("}");
System.out.println("actions{");
for(int i=0;i<(videos.length);i++){
System.out.println("show("+ videos[i]+ ");");
}
System.out.println("}");
}
}
[Updated on: Fri, 23 October 2020 16:24] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: xtend code [message #1833784 is a reply to message #1833783] |
Fri, 23 October 2020 17:39 |
|
i still dont see the java code you want to generate.
i just see
- grammar
- sample model
- some generator code
but what is the code given the sample model you want to output
e.g.
package org.xtext.example.mydsl5.generator;
public class videodecl {
public static void main(String[] args) {
System.out.println("There is the video \"This is a vid\" with name oneVideo");
System.out.println("There is the video \"This is the second vid\" with name twoVideo");
System.out.println("There is the video \"This is another vid\" with name anotherVid");
System.out.println("playing oneVideo");
System.out.println("playing anotherVid");
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Fri, 23 October 2020 17:50] Report message to a moderator
|
|
|
|
|
Re: xtend code [message #1833787 is a reply to message #1833786] |
Fri, 23 October 2020 18:19 |
|
in the code you want to generate there is no
oneVideo , twoVideo
there is no "This is another vid"
and there is no play anotherVid
so how does your code relate to the model
and how does it change if you delete
twoVideo = "This is the second vid";
from the model
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Re: xtend code [message #1833789 is a reply to message #1833788] |
Fri, 23 October 2020 18:37 |
|
i still dont understand.
you show a java app and a use interacts with it
how does this relate to xtext generator.
if you use this
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
fsa.generateFile('greetings.txt', '''
import java.util.Scanner;
public class grammarcode {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
String[] videos= new String[2];
// String[] actions;
System.out.println("Please enter the videos name to play");
for(int i=0;i<(videos.length);i++) {
videos[i]=input.nextLine();
}
System.out.println("video_list{");
for(int i=0;i<(videos.length);i++) {
System.out.println(videos[i] +";");
}
System.out.println("}");
System.out.println("actions{");
for(int i=0;i<(videos.length);i++){
System.out.println("show("+ videos[i]+ ");");
}
System.out.println("}");
}
}
''')
}
the generator will generate your java code
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Re: xtend code [message #1833791 is a reply to message #1833790] |
Fri, 23 October 2020 18:49 |
|
i said: i can do if you tell me the java code which you want to generate for this model
video_list {
oneVideo = "This is a vid";
twoVideo = "This is the second vid";
anotherVid = "This is another vid";
}
actions {
show(oneVideo);
show(anotherVid);
}
i am asking you:
how shall the generated java code for this model file look like
you said
public class videodecl {
public static void main(String[] args) {
System.out.println("There is the video \"This is a vid\" with name oneVideo");
System.out.println("There is the video \"This is the second vid\" with name twoVideo");
System.out.println("There is the video \"This is another vid\" with name anotherVid");
System.out.println("playing oneVideo");
System.out.println("playing anotherVid");
}
}
is wrong.
so which code should be generated instead?
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
Re: xtend code [message #1833795 is a reply to message #1833794] |
Fri, 23 October 2020 20:10 |
|
that is easy
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
for (m : resource.allContents.toIterable.filter(Model)) {
fsa.generateFile("video/videodecl.txt", '''
video_list{
«FOR v : m.parts.filter(Video)»
«FOR vd : v.videos»
name="«Strings.convertToJavaString(vd.value)»";
«ENDFOR»
«ENDFOR»
}
action{
«FOR u : m.parts.filter(Videouse)»
«FOR play : u.playvideo»
show(«play.vid.name»);
«ENDFOR»
«ENDFOR»
}
''')
}
}
but this aint java code that is generated
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Fri, 23 October 2020 20:11] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.05712 seconds