|
|
|
|
|
|
|
|
Re: Nested comments [message #666775 is a reply to message #666771] |
Fri, 22 April 2011 12:45 |
costa Messages: 10 Registered: April 2011 |
Junior Member |
|
|
Henrik Lindberg wrote on Fri, 22 April 2011 08:22 | I would override the lexer, and lex the ML_COMMENT manually. No need for a
full external lexer, just a simple derived class that looks for ml comments
in front of the regular lexer. Don't use datatypes, then you loose all the
default processing for ml comments.
- henrik
| Thank you. I should change a file InternalApLexer.java or can change some file in the src folder?
[Updated on: Fri, 22 April 2011 12:46] Report message to a moderator
|
|
|
Re: Nested comments [message #666786 is a reply to message #666775] |
Fri, 22 April 2011 14:42 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
Create your own class derived from the generated lexer, and then bind
your lexer instead of the default. Then override the appropriate
method(s). Do *not* change the generated lexer (changes will be
overwritten next time you generate).
A suggestion is to create a very simple MyDSL grammar consisting of a
single statement and support for comments. It is easier to get a grip on
the inner workings of the lexer that way.
What you do is override the "mTokens" method.
Depending on the grammar and terminals, the lexer looks a bit different.
In one of my grammars, it starts like this:
public void mTokens() throws RecognitionException {
int alt25=164;
alt25 = dfa25.predict(input);
switch (alt25) {
case 1 :
{
mT21();
}
break;
// etc. etc. etc.
case 158 :
{
mRULE_ML_COMMENT();
}
Now, the mRULE_ML_COMMENT() is final, so it can not be overridden (that
is what we would have wanted to override), so instead we must do
something like:
public void mTokens() throws RecognitionException {
if(dfa.25.predict(input) == 158)
MY_mRULE_ML_COMMENT();
else
super.mTokens();
}
And in MY_mRULE_ML_COMMENT() you mimic the behavior or the original
rule, but modify it to count opening and closing nested ML comments.
This is a bit of a hack and you have to change the code if you make any
changes to any of the terminals (it may be a different number). I don't
recommend this approach for any extensive lexing work - you are then far
better of by writing a separate external lexer using Antlr (there is
support for this in Xtext).
Hope that helps.
Regards
- henrik
On 4/22/11 2:45 PM, costa wrote:
> Henrik Lindberg wrote on Fri, 22 April 2011 08:22
>> I would override the lexer, and lex the ML_COMMENT manually. No need
>> for a
>> full external lexer, just a simple derived class that looks for ml
>> comments
>> in front of the regular lexer. Don't use datatypes, then you loose all
>> the
>> default processing for ml comments.
>>
>> - henrik
> Thank you. I should change a file InternalApLexer.java or can change
> some file in src folder?
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06265 seconds