Home » Modeling » TMF (Xtext) » Creating a wildcard reference
Creating a wildcard reference [message #1402895] |
Fri, 18 July 2014 05:17  |
Eclipse User |
|
|
|
Hello everyone,
I don't know if my title here is quite clear, basically what I'm trying to do is adding a reference to an array that was not defined specifically by the user, but rather is provided by the DSL.
My rule is the following :
Function returns Function:
'func' name=EString '{'
'parameters' '(' (parameters+=[Type|EString] ( "," parameters+=[Type|EString])* )? ')'
'}';
Parameters are references to a Type that are defined by the user, and I would like to be able to specify some kind of wildcard rather than having to reference one that has been defined.
The idea of the field 'parameters' is to specify the types and implicitly the number of parameters of a function.
There are several ways I could do that by modifying my meta-model and adding a wildcard type of parameter that is actually contained by the function rather than being a reference, but by doing so I would probably lose the order of the parameters as they are defined (e.g doing something like parameters(*, specificType, *) ).
So I was wondering if there were ways to have such null references or some kind of cool trick to pull that off.
Thank you, here's to hoping that my question was clear enough =)
Yann
|
|
| | | |
Re: Creating a wildcard reference [message #1403850 is a reply to message #1403839] |
Mon, 28 July 2014 05:38   |
Eclipse User |
|
|
|
Well, when writing :
Param: TypeRef | Wildcard;
TypeRef: type[Type|EString];
Wildcard: {Wildcard}'*';
You're allowing the Param rule to return both a reference to an Object or an Object directly. This doesn't work for me (maybe I'm just missing something), but since parameters is not a containment relation, I can't use a rule such as Wildcard that would return an Object. (And it's not that I wouldn't, it just causes an error ^^ )
The aim of my DSL is to define types, then the prototypes of functions that take those types as parameters. The parameters relation is a simple reference, which allows me to refer to the types that were created just before (e.g type a; type b; type c; parameters(a, b, c); ), however, I would like to add wildcard parameters to the mix (e.g parameters(*, a, * , *, b, * , c , *) ). * has to exist in order to be referenced as a parameter, so I guess one of the options would be to have a rule that returns a reference to a new object rather than an object (returning null would be enough), but I don't know if that's possible.
The other possible way I see would be to make parameters a containment reference. Doing so, having a rule that returns a Wildcard would be very easy, but then I wouldn't be able to add references to a Type to parameters, I would have to instantiate specific types for each function, which I obviously don't want to.
Here's a sample that may help you understand :
type mass {
frames 2 // These are fields to the type Type, and are of no matter for this problem
fields(weight) // These are fields to the type Type, and are of no matter for this problem
}
type floor {
frames 2
fields(weight)
}
function connect {
parameters(mass, floor) // This I can do, but it won't cut it, as I want to be able to connect any types together
}
function trueConnect {
parameters(*, *) // Basically do this
}
function somethingElse {
parameters(mass, *, floor, *) // I still want to do this though
}
In terms of what I'm trying to achieve, we are still quite abstract, I am only defining function prototypes, so it matters very little if * is just an alias for null, as long as it is still referenced in the parameters relation and I can get the right number of parameters for a prototype. When actually using the functions defined here, my program will only check if types match when a type is actually defined and is not a *, but I guess that's kind of obvious.
I hope this helps, thank you for your help.
Yann
[Updated on: Mon, 28 July 2014 05:47] by Moderator
|
|
| | | | | | |
Re: Creating a wildcard reference [message #1403945 is a reply to message #1403935] |
Mon, 28 July 2014 12:38   |
Eclipse User |
|
|
|
I will send you the complete grammar and models when I get to work tomorrow (I currently live in Japan, so it is 1 am for me at the moment).
I only want * to be a token, I don't want there to be any intelligent behaviour behind it. Think of it as, say I create the following Java method :
void foo(Integer a, Object b);
This allows me to call foo with any kind of object as b, I don't want to check if this object implements any interfaces or extends any class, it is just a wildcard, allowing me to call this function with any parameter.
By saying one of the parameters of a function is *, I basically only want to say that when I call this function, I won't have to check if the type of this parameter matches the type that was defined in the prototype.
By defining :
type mass;
function foo {
parameters(mass, *)
}
The important thing is to know that foo takes two parameters, the first one is a mass, the second is any of the types that the user defined.
It becomes tricky in the sense that types are user-defined, in the very same file that functions are defined.
This means that the parameters have to be a referencing relation, in order to be able to reference types such as mass that were created just above, but still be able to reference a special type, one that was not created by the user like mass was.
In that sense, adding * to the parameters could mean adding a reference to a special object, one that was not created by the user, and that us developers are aware of. This however (even if it's possible, which I doubt), seems like an unclean way of working things out.
More than ever, thank you for your help, and sorry for the trouble. I'm especially sorry if I'm not very clear with my explanations...
Yann
|
|
| | | | | |
Re: Creating a wildcard reference [message #1404008 is a reply to message #1404004] |
Tue, 29 July 2014 03:15   |
Eclipse User |
|
|
|
I tried both ways and neither seem to work for me.
Yann Bondue:If parameters is a simple reference, it will fail on "parameters+=Wildcard" because "parameters is not a containment reference".
If parameters is a containment reference, it will fail on "parameters+=[Type|EString]" because "parameters is a containment reference."
The aim of parameters is to hold references to a type that was defined by the user, so that we can create various prototypes that use the same types. If I made parameters a containment reference, I would have to recreate an actual instance of type for each prototype that uses it, OR there is a way to put a reference in a containment and that would solve the problem.
The other way around, which seems better the way I see it, is to keep parameters as as simple reference and have some kind of hidden reference to the wildcard type that can be used by the user.
We can look at it this way :
The following code would do the trick, but the reference is created by the user :
type mass {
// type related definitions
}
type floor {
// Type related defintions
}
type * {
// No definitions required, this is the wildcard
}
prototype connect {
parameters(mass, floor, *, *)
}
Now, if I had a way to simply have the type * exist, without asking the user to define it, I would be able to reference it, and the problem would be solved.
All I'd need is that global reference.
[Updated on: Tue, 29 July 2014 03:16] by Moderator
|
|
| | | | | |
Re: Creating a wildcard reference [message #1404191 is a reply to message #1404171] |
Wed, 30 July 2014 06:31  |
Eclipse User |
|
|
|
you unfortuately have to decide for containment or not containment so adding this extra layer is the only chance. ive seen this beeing done multiples times. id not worry about the extra object since xtext creates and deletes object anyway while you are typing in the editor
|
|
|
Goto Forum:
Current Time: Sun Jul 27 16:15:15 EDT 2025
Powered by FUDForum. Page generated in 0.07630 seconds
|