Custom language plugin: How to use more than one lexer (at different times)?

Answered

In my custom language plugin, my language has two variants that treat significant whitespace differently.  This is most easily captured by two different flex files, which only differ in X in this rule

<STATE> {X} { return TokenType.WHITE_SPACE; }

I now instantiate one of the two resulting lexers in MyLexerAdapter depending on some CodeStyleSettings:

public class MyLexerAdapter extends FlexAdapter {
public MyLexerAdapter() {
super(MyCodeStyleSettings.getInstance(null).STYLE_X ?
new MyOtherLexer(null) :
new MyLexer(null));
}
}

The problem is that once a lexer is instantiated, it won't be replaced when changing the setting STYLE_X.

Is there a way I can force a new lexer being used, i.e., MyLexerAdapter being reinstantiated?

3 comments
Comment actions Permalink

On second thought, I think I can get away with just one lexer, but then use two different states, and check the CodeStyleSettings in the action, e.g.,

{ if (CodeStyleSettings.getInstance(prj).STYLE_X == true) 
yybegin(STATE1); else yybegin(STATE2); }

Is there a way to get the project from the lexer, or can I pass null to getInstance?

0
Comment actions Permalink

No, I cannot pass null into CodeStyleSettings.getInstance.  So where do I get the project from in the lexer?  Should I use static attributes in CodeStyleSettings?

0
Comment actions Permalink

Never mind.  After discussing on Slack, I'll move some of my rules, including the settings check, from the lexer to the parser.

0

Please sign in to leave a comment.