Skip to main content



      Home
Home » Modeling » TMF (Xtext) » [newbie] Return JvmTypeReference from data type rules(Mapping arbitrarily to existing Java classes)
[newbie] Return JvmTypeReference from data type rules [message #857602] Thu, 26 April 2012 13:43 Go to next message
Eclipse UserFriend
Hi all,

I've chosen a "protocol specification" pet-project to teach myself XText. After going through all the tutorials, I've added XBase to my (ridiculously simple Embarrassed) grammar that looks like this:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

ProtocolDeclaration:
	'protocol' name=QualifiedName
	'specification' '{'
	messages+=MessageDeclaration+
	'}';
MessageDeclaration:
	'message' name=ValidID '{'
	fields+=FieldDeclaration+
	'}';
FieldDeclaration:
//	name=ValidID ':' type=JvmTypeReference;  // this works for java types
	name=ValidID ':' type=FieldType; // but I want my own names (below)
FieldType:
        // my very own type names, should transform into existing classes:
	'TypeA' | // --> my.existing.TypeNamedX
	'TypeB'   // --> my.other.existing.TypeNamedY
;
The goal is to have my own "type names" that actually map to existing Java classes (like java.lang.String) which I've written myself.

My next baby step is to generate Java code from the above updated grammar using my the generated MyDslJvmModelInferrer, which I (boldly Twisted Evil) started writing as follows:

   	def dispatch void infer(MessageDeclaration element, IAcceptor<JvmDeclaredType> acceptor, boolean isPrelinkingPhase) {
   		acceptor.accept(element.toClass(element.name) [
   			documentation = element.documentation
   			for (field : element.fields) {
// 				field.toField(field.name, field.type) [
 				field.toField(field.name, [WHAT_CAN_I_DO_HERE?]) [
   					makePublic
   				]
   			}
   		])
   	}


...but I've no idea what to provide create the field type reference to my actual classes. I'm looking for a way to do something like this:

   				var myTypeRef;
   				if (field.name == 'TypeA') {
   					myTypeRef = 'my.existing.TypeNamedX';
   				} else if  (field.name == 'TypeB') {
   					myTypeRef = 'my.other.existing.TypeNamedY';
   				}
				field.toField(field.name, myTypeRef) [
					makePublic
				])

Re: [newbie] Return JvmTypeReference from data type rules [message #857665 is a reply to message #857602] Thu, 26 April 2012 15:02 Go to previous messageGo to next message
Eclipse UserFriend
I've been digging around and just found this old post (2009). It doesn't seem to work for me. Is this approach still valid?

http://www.eclipse.org/forums/index.php/m/759385/?srch=findDeclaredType#msg_759385
Re: [newbie] Return JvmTypeReference from data type rules [message #857670 is a reply to message #857665] Thu, 26 April 2012 15:06 Go to previous messageGo to next message
Eclipse UserFriend
yes it is.
Re: [newbie] Return JvmTypeReference from data type rules [message #858888 is a reply to message #857670] Fri, 27 April 2012 06:50 Go to previous message
Eclipse UserFriend
Yep, it does still work... Embarrassed
Previous Topic:METADATA
Next Topic:F1 Help for Xtext based editor.
Goto Forum:
  


Current Time: Tue Jul 15 04:44:16 EDT 2025

Powered by FUDForum. Page generated in 0.10189 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top