|
|
Re: Get CharSequence from JvmOperation object [message #1071099 is a reply to message #1071093] |
Fri, 19 July 2013 10:50 |
Priya Sabut Messages: 41 Registered: September 2012 |
Member |
|
|
Thanks for your reply ...
I am struggling here... This is becoming a dead-end for me...
Could you please let me know what is that information you need to understand the problem ? I mean where am I not clear in describing the problem ?
I would like to provide all the information ...
In simple I am trying to get java equivalent statements from XEpression which is my function body using MyGenerator that implements IGenerator.
DSL :
-----
function getXXX(String abc, String anc): String{
if(anc.endsWith("A")){
return "my"+anc.toLowerCase;
}
else {
return anc.toUpperCase;
}
}
Generated Code what I am expecting :
------------------------------------
public String getXXX(final String abc, final String anc) {
boolean _endsWith = anc.endsWith("A");
if (_endsWith) {
String _lowerCase = anc.toLowerCase();
return ("my" + _lowerCase);
} else {
return anc.toUpperCase();
}
}
I am getting the same using Inferrer but my requirement is to get it done through MyGenerator.
[Updated on: Fri, 19 July 2013 11:00] Report message to a moderator
|
|
|
|
Re: Get CharSequence from JvmOperation object [message #1071225 is a reply to message #1071110] |
Fri, 19 July 2013 15:39 |
|
HI,
the following works for me (if your would use jvmmodelinferrer completely it would even be more easy)
package org.xtext.example.mydsl.jvmmodel
import com.google.inject.Inject
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.xtext.example.mydsl.myDsl.Function
import org.xtext.example.mydsl.myDsl.Model
class MyDslJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(
element.toClass("DontCare")
).initializeLater [
for (f : element.functs) {
val operation = f.toMethod(f.name, f.type ?: inferredType) [
documentation = f.documentation
for (p : f.params) {
parameters += p.toParameter(p.name, p.parameterType)
}
body = f.body
]
members += operation
}
]
}
}
package org.xtext.example.mydsl
import javax.inject.Inject
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.common.types.JvmOperation
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.trace.ITraceURIConverter
import org.eclipse.xtext.resource.ILocationInFileProvider
import org.eclipse.xtext.xbase.compiler.GeneratorConfig
import org.eclipse.xtext.xbase.compiler.ImportManager
import org.eclipse.xtext.xbase.compiler.JvmModelGenerator
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable
import org.eclipse.xtext.xbase.compiler.output.TreeAppendable
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations
import org.xtext.example.mydsl.myDsl.Function
import org.xtext.example.mydsl.myDsl.Model
class DummyGen implements IGenerator {
@Inject JvmModelGenerator jmg
@Inject extension IJvmModelAssociations jma
@Inject
ILocationInFileProvider locationProvider
@Inject
ITraceURIConverter converter
override doGenerate(Resource input, IFileSystemAccess fsa) {
for (o : input.contents) {
o.compile(fsa)
}
}
def dispatch void compile(EObject o, IFileSystemAccess fsa) {
}
def dispatch void compile(Model o, IFileSystemAccess fsa) {
val im = new ImportManager()
val app = new TreeAppendable(im, converter, locationProvider, jma, o, "", "\n")
for (f : o.functs) {
f.compileFunction(app)
}
fsa.generateFile("test/Test.java",
'''
package test;
«im.imports.map["import " + it + ";"].join(("\n"))»
public class Test {
«app.toString»
}
''')
}
def compileFunction(Function f, ITreeAppendable app) {
val cfg = new GeneratorConfig
val operation = f.jvmElements.get(0) as JvmOperation
jmg.generateMember(operation, app, cfg)
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
|
Re: Get CharSequence from JvmOperation object [message #1075542 is a reply to message #1075536] |
Mon, 29 July 2013 16:00 |
Priya Sabut Messages: 41 Registered: September 2012 |
Member |
|
|
I have tried the following as per your suggestion.
MyDsl.xtext
-----------
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
DomainModel:
(functions += Function)+
;
Function:
{Function}
'function' name=QualifiedName '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')'
':' type = JvmTypeReference
body=XBlockExpression
;
MyDslGenerator.xtend //Generator
----------------------
/*
* generated by Xtext
*/
package org.xtext.example.mydsl2.generator
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.common.types.JvmOperation
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.trace.ITraceURIConverter
import org.eclipse.xtext.resource.ILocationInFileProvider
import org.eclipse.xtext.xbase.compiler.GeneratorConfig
import org.eclipse.xtext.xbase.compiler.ImportManager
import org.eclipse.xtext.xbase.compiler.JvmModelGenerator
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable
import org.eclipse.xtext.xbase.compiler.output.TreeAppendable
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations
import org.xtext.example.mydsl2.myDsl.Function
import org.xtext.example.mydsl2.myDsl.DomainModel
import org.eclipse.xtext.xbase.compiler.XbaseCompiler
import com.google.inject.Inject
/**
* Generates code from your model files on save.
*
* see http://www.eclipse.org/Xtext/documentation.html#TutorialCodeGeneration
*/
class MyDslGenerator implements IGenerator {
@Inject protected XbaseCompiler compiler
@Inject JvmModelGenerator jmg
@Inject extension IJvmModelAssociations jma
@Inject
ILocationInFileProvider locationProvider
@Inject
ITraceURIConverter converter
override doGenerate(Resource input, IFileSystemAccess fsa) {
for (o : input.contents) {
o.compile(fsa)
}
}
def dispatch void compile(EObject o, IFileSystemAccess fsa) {
}
def dispatch void compile(DomainModel o, IFileSystemAccess fsa) {
val im = new ImportManager()
val app = new TreeAppendable(im, converter, locationProvider, jma, o, "", "\n")
for (f : o.functions) {
val a =f.compileFunction(app)
println(a)
}
fsa.generateFile("test/Test.java",
'''
package test;
«im.imports.map["import " + it + ";"].join(("\n"))»
public class Test {
«app.toString»
}
''')
}
def compileFunction(Function f, ITreeAppendable app) {
val cfg = new GeneratorConfig
val operation = f.jvmElements.get(0) as JvmOperation; // Exception ArrayIndexOutOfBound as jvmElements returning an empty string
jmg.generateMember(operation, app, cfg)
}
}
MyDslRuntimeModule.java //Binding IGenerator
------------------------
/*
* generated by Xtext
*/
package org.xtext.example.mydsl2;
import org.eclipse.xtext.generator.IGenerator;
import org.xtext.example.mydsl2.generator.MyDslGenerator;
/**
* Use this class to register components to be used at runtime / without the Equinox extension registry.
*/
public class MyDslRuntimeModule extends org.xtext.example.mydsl2.AbstractMyDslRuntimeModule {
@Override
public Class<? extends IGenerator> bindIGenerator() {
// TODO Auto-generated method stub
return MyDslGenerator.class;
}
}
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05828 seconds