|
Re: Using XExpression somewhere else than in operation bodies [message #1160785 is a reply to message #1159104] |
Tue, 29 October 2013 10:51 |
Hallvard Traetteberg Messages: 673 Registered: July 2009 Location: Trondheim, Norway |
Senior Member |
|
|
Victor,
To make Xbase handle scoping for you, you need to provide a context for
each expression, which in practice means associating it to an operation,
e.g. setting it as the operation's body. I think there is a limitation
that two expressions cannot have the same container, so you need to
generate a method for each, with suitable/appropriate formal parameters
and call these methods in the main operation, e.g.
private _expr1(...) { <expr1> }
private _expr2(...) { <expr2> }
private _expr3(...) { <expr3> }
public realMethod() {
// use the _expr methods
_expr1() ... expr2() ... expr3() ...
}
The method names will typically be generated as a kind of model path
like the URI, to make them unique. If you generate names starting with _
they won't be visible for completion etc.
If you set the body of each method to the expression, the default code
generator will write the textual code for you. You only need to take
care to compose the text for calling the methods.
Hallvard
On 28.10.13 10:46, Victor Noël wrote:
> Hi,
>
> I have a grammar where I want to use an XExpression for writing
> method-like call parameters.
> But I don't plan to just associate the XExpression to the body of an
> JvmOperation as in the examples of xtext.
>
> When I run my language, it seems that apart from pure syntax, no
> validation is done on the XExpression and that it can't see
> JvmFormalParameter declared in one of the containing model.
>
> The grammar is defined as (I simplified it):
>
> Feature returns JvmFormalParameter:
> name=ValidID ":" parameterType=JvmTypeReference;
> Species:
> "species" name=ValidID
> ("(" (parameters+=Feature ("," parameters+=Feature)*)? ")")?
> "{"
> parts+=SpeciesPart*
> "}";
> SpeciesPart:
> ("use") name=ValidID ":" speciesReference=SpeciesReference;
> SpeciesReference:
> species=[Species|ValidID] ("(" (arguments+=XExpression (","
> arguments+=XExpression)*)? ")")? ;
>
>
> Examples models are:
>
> species A(p: String) {
> use B(p)
> }
>
> species A(p: String) {
> use B("blabla")
> }
>
> species A(p: Collection<String>) {
> use B(new Test(p))
> }
>
>
> And so on...
>
> What am I missing? I guess it is related to the way it is exploited by
> the JvmModelInferrer maybe...
>
> The information from the XExpression is used to generate the body of a
> method like that:
>
> // str is a type reference
> c.members += newMethod("createImplementationOf"+species.name, str) [
> parameters += species.parameters.map[p|newParameter(p.name,
> p.parameterType)]
> body = [
> for(use: species.parts) {
> append('''
> assert this.selfComponent.implem_«use.speciesReference.part.name»
> != null;
> assert implem.use_«use.name» == null;
> implem.use_«use.name» =
> this.selfComponent.implem_«use.speciesReference.part.name».createImplementationOf«use.speciesReference.species.name»(''')
>
> for(a: use.speciesReference.arguments) {
> a.toJavaExpression(it)
> }
> append(''');
> ''')
> }
> append('''return implem;''')
> ]
> ]
>
|
|
|
|
Powered by
FUDForum. Page generated in 0.03037 seconds