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 09:53
Gaspar Din
Messages: 21
Registered: September 2012
Junior Member
Hi,

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 "http://www.functions.com/xtext/Functions"

Module:
	(statements+=Statement)*;

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

DeclaredParameter:
	name=VarName;
	
Type:
	IntegerType | StringType;

IntegerType:
	{IntegerType} type="Integer";

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

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

StringAtom returns StringType:
	value=A_STRING;

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

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

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

VarName:
	{VarName} value=A_VARNAME;
	
terminal A_VARNAME:
	('a'..'z')('a'..'z')+;

terminal CONSTNAME:
	'$'('A'..'Z')('a'..'z')+;



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):

    @Override
    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>() {

            //@Override
            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?

Regards,
Gaspar

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

Report message to a moderator

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


Current Time: Wed Aug 27 17:09:49 EDT 2014

Powered by FUDForum. Page generated in 0.02150 seconds