dynamically accessing the list of authorized keywords [message #533457] |
Thu, 13 May 2010 22:58  |
Eclipse User |
|
|
|
Hi,
I'm currently building a DSL with Xtext for a simulation platform
( http://code.google.com/p/gama-platform/source/browse/#svn/br anches )
The problem, in the language used in the platform, is that the lists of keywords (for example control commands or operators) are dynamically defined (basically, and without going into further details, the keywords available depend on the loading of specific libraries by the user).
Is there a way, like in a regular ANTLR grammar, to define the grammar without hard-coding the list of keywords ? (for instance, within the grammar, to know if a keyword belongs to the set of authorized operators, I'd like to call a function in Java). And without having to regenerate the whole XText project every time a new keyword is added ?
Thank you in advance !
Cheers
|
|
|
|
Re: dynamically accessing the list of authorized keywords [message #533543 is a reply to message #533514] |
Fri, 14 May 2010 09:52   |
Eclipse User |
|
|
|
I have a similar issue in b3, and I did exactly what Alex suggests.
In my case I can get by with a fairly simple ("dynamic") grammar of:
ID '{' (ID '=' Expression)* '}'
This is all my extensions are allowed to specify, and this is easy to
handle in terms of validation, coloring, and code completion.
However, it gets complicated when there is a requirement to dynamically
add support for complex grammar fragments - i.e. could be as complex as
implementing support for the original grammar!
It would be really neat if it was possible to inject grammar/parser
fragments dynamically.
I started toying with the idea to do something like:
CustomRule : key=ID '{' extended += (ExtRules | ANY)* '}'
ExtKeywords : ID | STRING | INT | "keyword1" | "keyword2" | ... ;
And use a second parser on the result of the "extended", but I never
tried since my grammar is quite large, and my list of keywords would be
quite large, and for my (current) simple needs this was total overkill.
I realized that this was a poor man's "modal lexer", so with a bit of
googling...
I found that ANTLR has "lexer states" support by using a multiplexing
token stream
http://sds.sourceforge.net/src/antlr/doc/streams.html#lexers tates which
looks really interesting for this sort of thing. It requires being able
to switch lexer streams from within the Xtext grammar though. I did not
explore how that could be done, but I know it is possible to customize
the lexer used for an XText grammar (search earlier posts in this
newsgroup).
I hope that helps with some starting points for figuring out how to
handle this
Regards
- henrik
On 5/14/10 1:21 PM, Alexander Nittka wrote:
> Hi,
>
> depending on what the rest of the grammar looks like, you can solve the
> problem, but not on the grammar level.
>
> Rule: keyword=ID...;
>
> Then do a semantic check whether the keyword entered is valid. Code
> completion and highlighting will have to be customised as well, of course.
>
> Alex
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03834 seconds