Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Functions with parameter types(How to check for the types?)
Functions with parameter types [message #986235] Mon, 19 November 2012 14:53
Gaspar DinFriend
Messages: 21
Registered: September 2012
Junior Member

I'd like to use functions in my xtext grammar. The functions shall have a return type and an arbirtary number of arguments of different types.
A simplified grammar:

grammar com.functions.xtext.Functions with org.eclipse.xtext.common.Terminals
generate intTerm ""


	Definition | Evaluation;
	'def' returnType=Type name=ID ('(' types+=Type args+=DeclaredParameter (',' types+=Type args+=DeclaredParameter)* ')')? ';';

	IntegerType | StringType;

	{IntegerType} type="Integer";

	{StringType} type="String";
	expression=Expression ';';

Expression returns Type:
	FunctionCall | Constant | IntegerAtom | StringAtom;
IntegerAtom returns IntegerType:
	{IntegerAtom } INT ;

StringAtom returns StringType:

FunctionCall returns Type:
	{FunctionCall} source=DefinitionRef '(' ( args+=Expression (',' args+=Expression)* )? ')';

DefinitionRef returns Type:
	{DefinitionRef} refName=[Definition];
	'const' type=Type constName=CONSTNAME;

terminal A_STRING:
	'"' ('a'..'z')+ '"';

	{VarName} value=A_VARNAME;
terminal A_VARNAME:

terminal CONSTNAME:

Now, it's possible to define new functions and call them:

def Integer IntFunc1 (Integer parama, Integer paramb);
def Integer IntFunc2 (Integer param);
def String StringFunc (Integer param);

IntFunc1(2, 3); // okay
IntFunc1(5, IntFunc2(3)); // okay
IntFunc1(1, 2, 3); // not okay, but no error marker
IntFunc1(StringFunc(3)); // not okay, but no error marker

With this grammar, it is neither possible to check the correct types of the arguments nor the correct number of arguments.
I have tried to add a ScopeProvider to check for the types, but I don't know how to get the actual parameter or to match any parameter with its expected type.
Neither getScope(..) nor scope_DefinitionRef_refName(..) know about the actual parameter (I've also checked the context and references variables):

    public IScope getScope(EObject context, EReference reference) {
        if (context instanceof FunctionCall && "refName".equals(reference.getName())) {
            IScope scope = super.getScope(context, reference);
            Predicate<IEObjectDescription> filter = new Predicate<IEObjectDescription>() {
                public boolean apply(IEObjectDescription input) {
                    if (input instanceof EObjectDescription) {
                        EObject eobj = ((EObjectDescription)input).getEObjectOrProxy();
                        if (eobj instanceof Definition) {
                            Type returnType = ((Definition)eobj).getReturnType();
                            // check, if returnType matches the corresponding parameter
                            // how to access the right parameter?							
                            //if (returnType matches parameter) {
                            //    return true;
                    return false;
            return new FilteringScope(scope, filter);
        return super.getScope(context, reference);

    IScope scope_DefinitionRef_refName(EObject context, EReference ref) {
        return new FilteringScope(delegateGetScope(context, ref), new Predicate<IEObjectDescription>() {

            public boolean apply(IEObjectDescription input) {
		// how to access the right parameter?
                return true;

Here are my questions:
1. How can this checks be performed to raise an error for wrong type parameters? Is it only possible to perform the needed checks with a JavaValidator class?
2. Does the proposal provider also evaluate the checks made by the scope provider to limit its proposal list?
3. Is it okay to use "returns Type" in the grammar for the type checking or is there any better solution?


[Updated on: Tue, 20 November 2012 07:19]

Report message to a moderator

Previous Topic:terminal ID with prefix!
Next Topic:Resolving Crossreferences in Eclipse context
Goto Forum:

Current Time: Thu Oct 19 16:41:03 GMT 2017

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

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