Skip to main content



      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 18:02 Go to next message
Eclipse UserFriend
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 03:12 Go to previous messageGo to next message
Eclipse UserFriend
The interesting part of the grammar seems missing
Re: Unable to use a declaration in assignments and if conditions [message #1744174 is a reply to message #1744172] Fri, 23 September 2016 03:18 Go to previous messageGo to next message
Eclipse UserFriend
(Your stuff does not include into xbase stuff
Re: Unable to use a declaration in assignments and if conditions [message #1744305 is a reply to message #1744174] Sat, 24 September 2016 11:58 Go to previous messageGo to next message
Eclipse UserFriend
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 13:55 Go to previous messageGo to next message
Eclipse UserFriend
ni if you want to use the stuff at the places of an xexpression you have to include your expressions into the xexpression hierachy
Re: Unable to use a declaration in assignments and if conditions [message #1744313 is a reply to message #1744310] Sat, 24 September 2016 16:28 Go to previous messageGo to next message
Eclipse UserFriend
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 01:24 Go to previous message
Eclipse UserFriend
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
Previous Topic:Xtext double hash comment
Next Topic:xtext rules starts at new line
Goto Forum:
  


Current Time: Sun Jul 27 09:42:53 EDT 2025

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

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

Back to the top