Marcus Mathioudakis
Marcus Mathioudakis

Reputation: 837

generating classes for evaluating Xbase expressions in Xtext?

I am trying to design a little DSL for checking constraints on variables. My grammar at the moment looks like this:

Start:
    varDeclarations += XVariableDeclaration*
    rules+=Constraint*;

Constraint:
    {Constraint}
    'FOR' 'PAYLOAD' payload=PAYLOAD 'ELEMENT' element=ID 'CONSTRAINED BY' constraint=XExpression;



PAYLOAD:
    "SimulationSessionEvents"
    |"stacons"
    |"any"
;

I want to generate instances of a class containing just one method that takes as input a value, maps it to the only variable contained in the constraint(which is also the only variable declared), and checks if the constraint is satisfied.

These instances will then be used by another class, which passes a value through each instance, checking if it's constraint is satisfied.

As I see it I have 2 options:

  1. Explicitly generate code for a constraint class, in which case I can just use XBaseCompiler to generate the expression evaluation code. However I would then have to load these classes somehow, which seems inelegant, if there is a way to create in memory objects directly.

  2. Use the ModelInferrer to directly generate in memory objects which can be passed to the other class, so no class loading is required. I am not sure how to generate the xbase expression evaluation code in this case.

After reading all the xtext documentation/tutorials, and playing around with the examples, I am left with the following questions:

Which is the 'best' approach in terms of scalabilty (I may later want to extend the grammar, and/or the functions of he generated classes)? How exactly would I go about things if I were to follow the ModelInferrer approach? Is there any other way of doing this?

Any help would be much appreciated

Upvotes: 0

Views: 706

Answers (1)

Sebastian Zarnekow
Sebastian Zarnekow

Reputation: 6729

The best approach is to use the model inferer to create the Java representation of your DSL elements. The expression is usually assigned by means of the JvmTypeBuilder#setBody. Have a look at the domain model example where you'll find the the assignment to the operation body:

members += f.toMethod(f.name, f.type) [
    for (p : f.params) {
        parameters += p.toParameter(p.name, p.parameterType)
    }
    body = f.body
]

Another option is to create the code manually:

body = [
    append(varName).append(' = new ').append(typeName).append('();')
]

The inferer approach allows for powerful Eclipse integration since type hierarchy, call hierarchy or go-to-declaration will all honor the derived Java things.

Upvotes: 2

Related Questions