Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 17:43 Go to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
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 19:02 Go to previous messageGo to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
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 19:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
yes it is.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [newbie] Return JvmTypeReference from data type rules [message #858888 is a reply to message #857670] Fri, 27 April 2012 10:50 Go to previous message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Yep, it does still work... Embarrassed
Previous Topic:METADATA
Next Topic:F1 Help for Xtext based editor.
Goto Forum:
  


Current Time: Tue Apr 23 08:55:51 GMT 2024

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

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

Back to the top