Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to extend xbase::XBlockExpression?(How to extend xbase::XBlockExpression?)
How to extend xbase::XBlockExpression? [message #1273311] Wed, 19 March 2014 17:54 Go to next message
peter luthardt is currently offline peter luthardt
Messages: 6
Registered: March 2014
Junior Member
I have been following the instruction from template example. But now I need help.
I would like to extend xbase with a trace. Something like this:
grammar lutte.testDSL.DSL with org.eclipse.xtext.xbase.annotations.XbaseWithAnnotations

generate dSL "htt_p://www.testDSL.lutte/DSL"
import "htt_p://www.eclipse.org/xtext/xbase/Xbase" as xbase

Model:
	imports = XImportDeclaration?
	greetings+=Greeting*;
	
Greeting:
	annotations += XAnnotation*
	'Hello' name=ID '!'
	body = MyBlockExpression
	;
	
MyBlockExpression returns xbase::XBlockExpression:
	{MyBlockExpression}
	'{'
		(expressions+=MyExpressionInsideBlock(';')?)*
	'}'
;

MyExpressionInsideBlock returns xbase::XExpression:
	XExpressionInsideBlock|MyLog
;

MyLog returns xbase::XExpression:
	{MyLog}
	'Log :' '('log += XStringLiteral*')'  <--- Here I would like to enter expressions
;


I would like to write something like
Hello Lutte!
{
  val echo = new StringBuilder ('Echo')
  Log ('I am logging: ', echo.toString) 
}


Is there any simple way to get the parameters of Log and have the scoping for free?
I have looked for hours, but did not find anything. I hope it is to simple, so nobody bothered to give an example Razz
How to extend xbase::XBlockExpression? [message #1274472 is a reply to message #1273311] Fri, 21 March 2014 10:20 Go to previous messageGo to next message
peter luthardt is currently offline peter luthardt
Messages: 6
Registered: March 2014
Junior Member
I have changed the grammer a bit as it would be ok to create only a String
MyLog returns xbase::XExpression:
	{MyLog}
	'Log :' '('log = XExpression')' 
;


The grammer seems to be ok as nobody compiled when I entered
Hello Lutte!
{
  val test = new String("Hello")
  Log : (test.length)
  return ""
}


Now I changed the compiler to
...
override protected doInternalToJavaStatement (XExpression expr, ITreeAppendable it, boolean isReferenced) {
  switch (expr)
  {
...
     MyLog : {
	newLine.append ('''System.out.println ("logging in "+MN+" -> "''')
	super.doInternalToJavaStatement(expr.log, it, true)
	append(''');''')
    }
....


And now I get an exception
java.lang.IllegalStateException: name may not be null
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.getFavoriteVariableName(AbstractXbaseCompiler.java:472)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.declareFreshLocalVariable(AbstractXbaseCompiler.java:533)
	at org.eclipse.xtext.xbase.compiler.FeatureCallCompiler._toJavaStatement(FeatureCallCompiler.java:176)
	at org.eclipse.xtext.xbase.compiler.FeatureCallCompiler._toJavaStatement(FeatureCallCompiler.java:251)


If I try to log a String no exception is thrown, but in the generated code the String is missing.
So another day is gone and I still have no solution Crying or Very Sad


Xtext 2.5.2
Re: How to extend xbase::XBlockExpression? [message #1278322 is a reply to message #1274472] Thu, 27 March 2014 03:10 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo Bettini
Messages: 1324
Registered: July 2009
Senior Member
How does your model inferrer look like?

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book
Solved How to extend xbase::XBlockExpression? [message #1278384 is a reply to message #1278322] Thu, 27 March 2014 05:00 Go to previous messageGo to next message
peter luthardt is currently offline peter luthardt
Messages: 6
Registered: March 2014
Junior Member
I do not know if this is the best solution but now it works. Maybe it will help others.

I changed the copiler to
class MyDslCompiler extends XbaseCompiler {
	@Inject extension JvmTypesBuilder
	
	override protected doInternalToJavaStatement (XExpression expr, ITreeAppendable it, boolean isReferenced) {
		switch (expr)
		{
			MyBlockExpression:
			{
				val greeting = EcoreUtil2::getContainerOfType(expr, Greeting)
				if (greeting != null)
				{
					newLine.append('''final String MN = "«greeting.name»";''')
					newLine.append('''System.out.println("entering <«greeting.name»>");''')
				}
				super.doInternalToJavaStatement(expr as XBlockExpression, it, isReferenced)
			}
			MyLog : {
				_toJavaStatement(expr, it, isReferenced)
			}
			default : super.doInternalToJavaStatement(expr, it, isReferenced)
		}
	}

	def void _toJavaStatement(MyLog expr, ITreeAppendable b, boolean isReferenced) {
		val it = b.trace(expr, false);
		if (expr.getExpressions().isEmpty())
			return;
		if (isReferenced)
			declareSyntheticVariable(expr, it);
		newLine().append("if (1==1) {//trace is enabled")
		increaseIndentation();
		openPseudoScope();
		val expressions = expr.getExpressions();
		for (ex:expressions)
		{
			if (ex == expressions.last)
			{
				internalMyLogToJavaStatement(ex, it);
				if (isReferenced) {
					newLine().append(getVarName(expr, it)).append(" = ");
					internalToConvertedExpression(ex, it, getType(expr));
					append(";");
				}
			} else
			{
				internalMyLogToJavaStatement(ex, it)
			}
		}
		newLine.append ('''System.out.println ("logging in "+MN+" -> "«FOR varName :expressions.map[ex|ex.getVarName(it)]»+«varName»«ENDFOR»);''')
		it.closeScope();
		it.decreaseIndentation()
		newLine().append("}");
	}
	
	def void internalMyLogToJavaStatement (XExpression expr, ITreeAppendable it)
	{
		switch (expr)
		{
			XStringLiteral : declareFreshLocalVariable(expr, it, [append('''«_toJavaExpression(expr, it)»''')])
			XAbstractFeatureCall:{
				declareFreshLocalVariable(expr, it, [append('''«featureCalltoJavaExpression(expr, it, true)»''')])
			}
			default : internalToJavaStatement(expr, it, true)
		}
	}
}


Now I had to change my grammer to add the new Literal
...
XPrimaryExpression returns xbase::XExpression:
	XConstructorCall |
	XBlockExpression |
	XSwitchExpression |
	XFeatureCall |
	XLiteral |
	XIfExpression |
	XForLoopExpression |
	XWhileExpression |
	XDoWhileExpression |
	XThrowExpression |
	XReturnExpression |
	XTryCatchFinallyExpression |
	XParenthesizedExpression|
	MyLog;
...


Can anyone mark this as solved?


Xtext 2.5.2
Re: Solved How to extend xbase::XBlockExpression? [message #1312720 is a reply to message #1278384] Thu, 24 April 2014 09:09 Go to previous message
Md. Abul Bashar is currently offline Md. Abul Bashar
Messages: 1
Registered: July 2009
Junior Member
I am trying to do something very similar; can you please post your complete grammar? Thanks,
Previous Topic:Annyoing XText model (hyper)linking
Next Topic:Headless testing for xtext
Goto Forum:
  


Current Time: Wed Aug 27 21:16:32 EDT 2014

Powered by FUDForum. Page generated in 0.02278 seconds