Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Unable to use a declaration in assignments and if conditions(Unable to use a declaration in assignments and if conditions)
Unable to use a declaration in assignments and if conditions [message #1744158] Thu, 22 September 2016 22:02 Go to next message
Edward Mallia is currently offline Edward MalliaFriend
Messages: 8
Registered: September 2016
Junior Member
I have the following grammar (snippets of):

Condition:
	'condition' name=ID '(' conditionFormalParameters=FormalParameters ')' '=' conditionExpression=ConditionExpression;

ConditionExpression:
	ref=ConditionRefInvocation | block=ConditionBlock;

ConditionBlock:
	simple=SimpleCondition | complex=ComplexCondition;

SimpleCondition returns xbase::XBlockExpression:
	'{{'
	{SimpleCondition} 
	(expressions+=ConditionBlockStatement ';'?)*
	'}}';

ComplexCondition returns xbase::XBlockExpression:
	'{'
	{ComplexCondition} 
	(expressions+=ConditionBlockStatement ';'?)*
	'}';

ConditionBlockStatement returns xbase::XExpression:
	ConditionRefInvocation | XExpressionOrVarDeclaration;

ConditionRefInvocation:
	{ConditionRefInvocation} ref=ConditionRef '(' params=ActualParameters ')';



In other words, I can define conditions and conditions are reusable. For example, the following is a snippet from my DSL that works.
   condition isEmpty(String s) = {
	s.empty
   }
   
   condition isAbcEmpty() = {
   	#isEmpty("abc")
   }


However, when I try to us a Condition reference in an assignment (see below) I get a "no viable alternative at input '#'" error.
condition isAbcEmpty() = {
 		var b = #isEmpty("abc") 
               return b
 }


And when I try to use a Condition reference in an if condition (see below) I get a list of errors:

  • mismatched input ')' expecting '}'
  • missing EOF at 'else'
  • no viable alternative at input '#'
  • The method isEmpty(String) is undefined

 condition isAbcEmpty() = {
	if (#isEmpty("abc")){
		return true;
	}
	else{
		return false;
	}
   }


My TypeComputer and relevant compiler methods are show below together with my compiler

  //type computer
   def dispatch computeTypes(ConditionRefInvocation literal, ITypeComputationState state) {
        for (XExpression ap : literal.params.getParameters()) {
			state.withNonVoidExpectation().computeTypes(ap);
		}

     	val booleanLightWeightRef = getTypeForName(Boolean.TYPE, state)
     	
        state.withExpectation(booleanLightWeightRef)
        state.acceptActualType(booleanLightWeightRef)
    }

   //compiler
  override void _toJavaExpression(XExpression obj, ITreeAppendable appendable) {
		switch (obj) {
			ConditionRefInvocation: _toJavaExpression(obj as ConditionRefInvocation, appendable)
			default: super._toJavaExpression(obj, appendable)
		}
	}
	
	def _toJavaExpression(ConditionRefInvocation obj, ITreeAppendable appendable) {
			appendable.trace(obj)
			appendable.newLine
			
			val condition = obj.ref.ref
			val conditionParameters = obj.params
			val conditionClass = condition.jvmElements.filter(JvmGenericType).filter[t|!t.isInterface].head
			
			appendable.append('''new ┬źconditionClass.fullyQualifiedName┬╗().apply(''')
			appendable.newLine
			appendArguments(conditionParameters.parameters, appendable)
			appendable.newLine
			appendable.append(")")
			appendable.newLine
			
			appendable.newLine
	}



Could you please help?


Thanks

Edward
Re: Unable to use a declaration in assignments and if conditions [message #1744172 is a reply to message #1744158] Fri, 23 September 2016 07:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13876
Registered: July 2009
Senior Member
The interesting part of the grammar seems missing

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unable to use a declaration in assignments and if conditions [message #1744174 is a reply to message #1744172] Fri, 23 September 2016 07:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13876
Registered: July 2009
Senior Member
(Your stuff does not include into xbase stuff

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unable to use a declaration in assignments and if conditions [message #1744305 is a reply to message #1744174] Sat, 24 September 2016 15:58 Go to previous messageGo to next message
Edward Mallia is currently offline Edward MalliaFriend
Messages: 8
Registered: September 2016
Junior Member
Sorry about that, forgot to add ConditionRef, FormalParameters and ActualParameters to my grammar's snippet. See the missing declarations below including the imports in the xtext script.

import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase
import "http://www.eclipse.org/xtext/xbase/Xtype"

 ConditionRef hidden():
	'#' ref=[Condition];

 FormalParameters:
	{FormalParameters}(parameters+=FullJvmFormalParameter (',' parameters+=FullJvmFormalParameter)*)?;

 ActualParameters:
	{ActualParameters}(parameters+=XExpression (',' parameters+=XExpression)*)?;



Not sure what you mean by your comment "Your stuff does not include into xbase stuff". Aren't XExpression, XBlockExpression, XExpressionOrVarDeclaration and FullJvmFormalParameter all imported from xbase ?
Re: Unable to use a declaration in assignments and if conditions [message #1744310 is a reply to message #1744305] Sat, 24 September 2016 17:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13876
Registered: July 2009
Senior Member
ni if you want to use the stuff at the places of an xexpression you have to include your expressions into the xexpression hierachy

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unable to use a declaration in assignments and if conditions [message #1744313 is a reply to message #1744310] Sat, 24 September 2016 20:28 Go to previous messageGo to next message
Edward Mallia is currently offline Edward MalliaFriend
Messages: 8
Registered: September 2016
Junior Member
Not sure I understand what you mean by "include your expressions into the xexpression hierachy". Could you kindly elaborate or point me to some resources ?

Thanks

Edward
Re: Unable to use a declaration in assignments and if conditions [message #1744318 is a reply to message #1744313] Sun, 25 September 2016 05:24 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13876
Registered: July 2009
Senior Member
from the Xbase grammar

XIfExpression returns XExpression:
	{XIfExpression}
	'if' '(' if=XExpression ')'
	then=XExpression
	(=>'else' else=XExpression)?;


=> if you want to use The ConditionRef you have to call it somewhere inside XExpression

likely at a place where it makes sense likely

XPrimaryExpression:
YourExpression | super or something likw that


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Xtext double hash comment
Next Topic:xtext rules starts at new line
Goto Forum:
  


Current Time: Sat Apr 10 13:03:56 GMT 2021

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

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

Back to the top