Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Using XExpression somewhere else than in operation bodies
Using XExpression somewhere else than in operation bodies [message #1159104] Mon, 28 October 2013 05:46 Go to next message
Eclipse UserFriend
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;''')
  ]
]
Re: Using XExpression somewhere else than in operation bodies [message #1160785 is a reply to message #1159104] Tue, 29 October 2013 06:51 Go to previous messageGo to next message
Eclipse UserFriend
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;''')
> ]
> ]
>
Re: Using XExpression somewhere else than in operation bodies [message #1160820 is a reply to message #1160785] Tue, 29 October 2013 07:24 Go to previous message
Eclipse UserFriend
Ok, great, I get the idea, thank you, I will play with it!

I didn't know about the "_" thing making method not visible for completion, that's perfect Smile
Previous Topic:Scoping in Xtext
Next Topic:OutOfMemoryError at org.eclipse.xtext.ui.editor.syntaxcoloring
Goto Forum:
  


Current Time: Wed Jul 23 13:46:27 EDT 2025

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

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

Back to the top