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:
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.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