Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Bind JVM object reference to a grammar element
Bind JVM object reference to a grammar element [message #1699755] Fri, 26 June 2015 11:22 Go to next message
Zeljko Vukovic is currently offline Zeljko VukovicFriend
Messages: 5
Registered: June 2015
Junior Member
Let there be a Java class Foo, having a public method m(). I would like to have a DSL construct (Bar) that enables the user to access an instance of Foo from within the DSL, without having to declare that instance in the DSL code. So, in the DSL one could write

Bar.m()


and have m() available in auto-complete. How could this be done in Xtext?

NOTE: the actuall class has many more methods and some of them return objects that have other methods, that I would also like to be able to have in auto-complete, so rewriting them all as grammar terms isn't really an option.
Re: Bind JVM object reference to a grammar element [message #1699838 is a reply to message #1699755] Sat, 27 June 2015 04:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Do you stick to exacly that syntax

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Bind JVM object reference to a grammar element [message #1699849 is a reply to message #1699838] Sat, 27 June 2015 06:47 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
maybe

grammar org.xtext.example.mydsl1.MyDsl with org.eclipse.xtext.xbase.Xbase

generate myDsl "http://www.xtext.org/example/mydsl1/MyDsl"
import "http://www.eclipse.org/xtext/common/JavaVMTypes" as types
import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase

Model:
	imports=XImportSection?
	elements+=Element*;
	
Element:
	'element' name=ID '{'
		thingys+=Thingy*
	'}';

	
Thingy:
	type=NonQualifiedJvmParameterizedTypeReference body=XReducedFeatureCall";"	
;

NonQualifiedJvmParameterizedTypeReference returns types::JvmParameterizedTypeReference:
  type=[types::JvmType|ValidID] (
  	=>'<' arguments+=JvmArgumentTypeReference (',' arguments+=JvmArgumentTypeReference)* '>'
  	(=>({JvmInnerTypeReference.outer=current} '.') type=[types::JvmType|ValidID] (=>'<' arguments+=JvmArgumentTypeReference (',' arguments+=JvmArgumentTypeReference)* '>')?)*
  )?;
  
XReducedFeatureCall returns xbase::XExpression:
	{xbase::XFeatureCall}"."
	feature=[types::JvmIdentifiableElement|IdOrSuper] 
	(=>explicitOperationCall?='(' 
		(
		    featureCallArguments+=XShortClosure
		  |	featureCallArguments+=XExpression (',' featureCallArguments+=XExpression)*
		)? 
	')')?;  


class MyDslJvmModelInferrer extends AbstractModelInferrer {

	@Inject extension JvmTypesBuilder
   	def dispatch void infer(Element element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
   		acceptor.accept(element.toClass("test."+element.name)) [
   			var counter = 0
   			for (t : element.thingys) {
   				members += t.toMethod("thingy"+counter++,Void.TYPE.typeRef) [
   					parameters += t.toParameter("it", t.type)
   					body = t.body
   				]
   			}
   		]
   	}
}


you have to adapt XExpressionHelper regarding has side effect


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:[Formatting2] rendering Region
Next Topic:Mapping Xtext to Java code
Goto Forum:
  


Current Time: Tue Mar 19 08:05:41 GMT 2024

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

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

Back to the top