ecore - Xtext: Use EClass in XExpression -
i writing on xtext grammar uses xexpressions , operates on eclasses. want able access eclasses xexpression, example write expression this:
eclass1.attribute1 = eclass2.attribute1
i know, how can use eclass within xexpression?
grammar
grammar org.xtext.example.mydsl.mydsl org.eclipse.xtext.xbase.xbase import "http://www.eclipse.org/emf/2002/ecore" ecore generate mydsl "http://www.xtext.org/example/mydsl/mydsl" model: (operations += operation)*; terminal attr : id ('.' id)+; operation: 'operation' left=[ecore::eclass|attr] 'and' right= [ecore::eclass|attr] 'defined' 'as' condition=xexpression ;
inferrer/ infer method
def dispatch void infer(model element, ijvmdeclaredtypeacceptor acceptor, boolean ispreindexingphase) { acceptor.accept(element.toclass("example.mydsl")) [ (operation : element.operations) { left = operation.left right = operation.right if (left.eisproxy()) { left = ecoreutil.resolve(left, operation) eclass } if (right.eisproxy()) { right = ecoreutil.resolve(right, operation) eclass } //field right class left out, works same members += left.tofield(left.name,typeref(left.epackage.name+"."+left.name)) members += operation.tomethod("conditionexpr", typeref(void.type)) [ body = operation.condition ] } ] }
runtimemodule
class mydslruntimemodule extends abstractmydslruntimemodule { def class<? extends implicitlyimportedfeatures> bindimplicitlyimportedtypes() { return myimportfeature } }
myimportfeature
class myimportfeature extends implicitlyimportedfeatures{ override protected getstaticimportclasses() { (super.getstaticimportclasses() + #[packagefromworkspace]).tolist } }
i not sure if question. ususally emf generates constants eattributes if want access attributes themselfs either mydslpackage.literals.greeting__name
or mydslpackage.einstance.getgreeting_name()
can give more hints on want do
update: here snippet on how java class reference eclass
thingy:{ val eclass eclazz = f.clazz val uri = ecoreplugin.getepackagensuritogenmodellocationmap(true).get(eclazz.epackage.nsuri) val rs = new resourcesetimpl val r = rs.getresource(uri, true) r.load(null) val p = r.contents.head if (p instanceof genmodel) { val genclass = p.findgenclassifier(eclazz) if (genclass instanceof genclass) { println(genclass.qualifiedinterfacename) members+=f.tofield(eclazz.name, genclass.qualifiedinterfacename.typeref) } } }
Comments
Post a Comment