Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Dynamically extend parts of the grammar(Inject new entities to the grammar)
Dynamically extend parts of the grammar [message #921587] Mon, 24 September 2012 08:45 Go to next message
Gaspar DinFriend
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:
int mult(int a, int b)

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 #921922 is a reply to message #921587] Mon, 24 September 2012 14:55 Go to previous messageGo to next message
Gaspar DinFriend
Messages: 21
Registered: September 2012
Junior Member
Is there any provider class for doing s.th. like this?

Any ideas?
Re: Dynamically extend parts of the grammar [message #923031 is a reply to message #921922] Tue, 25 September 2012 14:49 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
The 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.
Re: Dynamically extend parts of the grammar [message #923854 is a reply to message #923031] Wed, 26 September 2012 08:49 Go to previous message
Gaspar DinFriend
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:49
The 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

Previous Topic:How to refer to elements declared afterwards
Next Topic:Exclude keywords in certain context
Goto Forum:
  


Current Time: Fri Mar 29 05:24:16 GMT 2024

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

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

Back to the top