Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Automatic import of well-known functions
Automatic import of well-known functions [message #774636] Wed, 04 January 2012 10:22 Go to next message
Thomas Ponweiser is currently offline Thomas PonweiserFriend
Messages: 9
Registered: January 2012
Junior Member
Hy everybody!

I'm currently developing a simple DSL quite similar to the "XText Simple Arithmetic Example" which should support C-like numeric and boolean expressions with references to variables and functions.

Additionally to those variables and functions defined locally in the file, I would like to provide also well known functions ("sin", "cos",...) and (constant) variables ("pi", "e",...).

For that purpose, I don't want to use any "import" keyword or something like that - instead those names should be imported implicitely.

Moreover those names should be determined on runtime. The approach to define some functions and variables statically in a resource file is not enough for me, even though I'd also like to know how this could be done (automatically importing a file without explicitely mentioning it in an "import" statement).

My first approach was to costumize my ScopeProvider:
public class AODslScopeProvider extends AbstractDeclarativeScopeProvider {
	
	private List<String> functions = Arrays.asList("sin", "cos");
	private List<String> variables = Arrays.asList("pi", "e");
	private List<IEObjectDescription> knownObjects = new LinkedList<IEObjectDescription>();
	
	public AODslScopeProvider() {
		AodslFactory f = AodslFactory.eINSTANCE;
		for(String funcName : functions) {
			Function func = f.createFunction();
			func.setName(funcName);
			knownObjects.add(EObjectDescription.create(funcName, func));
		}
		
		for(String varName : variables) {
			Variable var = f.createVariable();
			var.setName(varName);
			knownObjects.add(EObjectDescription.create(varName, var));
		}
	}

	@Override
	public IScope getScope(EObject context, EReference reference) {
                // I guess this is not the way how SimpleScopes are meant to be used...
		return new SimpleScope(super.getScope(context, reference), knownObjects);
	}	
}

This workes so far - the functions and variables are available in the editors content assistent, but actually referring to them leads to the following error:
The feature 'name' of 'ao.explorer.dsl.aodsl.impl.FuncCallImpl@1b09ef0{platform:/resource/test/src/test.aodsl#//@statements.2}' contains a dangling reference 'ao.explorer.dsl.aodsl.impl.FunctionImpl@1e7501{#//}'

Should I rather use a GlobalScopeProvider for that purpose? How do I do that? (I didn't find something like that in the example projects.) Or am I completely on the wrong track?

I'd be grateful for any advise!

kind regards,
Thomas
Re: Automatic import of well-known functions [message #774642 is a reply to message #774636] Wed, 04 January 2012 10:33 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Your on-the-fly-created Function-s are not contained in any Resource(Set) which causes the dangling references error. Have a look at the following: http://blogs.itemis.de/stundzig/archives/795

Re: Automatic import of well-known functions [message #774661 is a reply to message #774642] Wed, 04 January 2012 11:04 Go to previous message
Thomas Ponweiser is currently offline Thomas PonweiserFriend
Messages: 9
Registered: January 2012
Junior Member
Thank you! This looks very helpful and answers the second part of my question. Default imports seem to be the best solution for built-in functions and constants.

Anyway, I'm facing the problem that some of the variables I want to refer to are defined dynamically by other modules of the application. Can you give me a reference on how to properly create EObjects on-the-fly and link them to my model?

Or would it better to handle such variables references as literals in the grammar and add some custom content-assist and validation code instead?
Previous Topic:Automatic validation is not performed
Next Topic:syntax sugar for creating EMF objects in Xbase
Goto Forum:
  


Current Time: Wed Apr 24 18:44:08 GMT 2024

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

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

Back to the top