Dynamically extend parts of the grammar [message #921587] |
Mon, 24 September 2012 08:45 |
Gaspar Din Messages: 21 Registered: September 2012 |
Junior Member |
|
|
Hi all,
supposing an xtext definition with the following snippet:
IntegerTerm:
IntegerLeaf | StringGetLength | FloatRound | ... |
IntegerTermExternal
;
IntegerTerm returns an int value.
IntegerLeaf is an int value like '0', '1', '2', ...
IntegerTermExternal is not yet known. It has to be generated from java classes.
For example:
At runtime I have a list of java functions (from a library) which will return an int value. Some of them will have parameters. They're collected via reflection api.
How do I have to specify IntegerTermExternal (in .xtext and in some(which?) java file) to support this java functions, e.g.:
java function:
Goal: Supported xtext definition :
IntegerTermExternal: Mult | ... ;
Mult: {Mult} "Mult" "(" int1=IntegerTerm "," int2=IntegerTerm ")";
The editor now should accept entries like
Mult (5, StringGetLength("abc"))
[Updated on: Tue, 25 September 2012 09:20] Report message to a moderator
|
|
|
|
|
Re: Dynamically extend parts of the grammar [message #923854 is a reply to message #923031] |
Wed, 26 September 2012 08:49 |
Gaspar Din Messages: 21 Registered: September 2012 |
Junior Member |
|
|
Hi Aaron,
okay, I've now written a ProposalProvider to introduce external function calls for the suggestion list.
I also had to wrote a Validator to only allow existing function calls.
Thanks
Gaspar
Aaron Digulla wrote on Tue, 25 September 2012 10:49The grammar in Xtext is static because ANTLR doesn't support dynamic grammars, so you need a trick. In your grammar, use something like this:
IntFunctionCall: name=ID '(' ')'
You can offer the user a list of possible function names by creating your own ProposalProvider which examines the classpath via reflection.
When you "execute" the DSL, you will have to do the same: When you encounter a "IntFunctionCall" node, find the function via reflection and invoke it.
[Updated on: Wed, 26 September 2012 08:50] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.02838 seconds