2

私はjflexを学んでいて、単一の文字を作る最も単純なjflexコードを書きました#:

import com.intellij.lexer.FlexLexer;
import com.intellij.psi.tree.IElementType;

%%

%class PubspecLexer
%implements FlexLexer
%unicode
%type IElementType
%function advance
%debug

Comment = "#"

%%

{Comment} { System.out.println("Found comment!"); return PubTokenTypes.Comment; }
.                                        { return PubTokenTypes.BadCharacter; }

次に、PubspecLexerクラスを生成して試します。

public static void main(String[] args) throws IOException {
    PubspecLexer lexer = new PubspecLexer(new StringReader("#!!!!!"));
    for (int i = 0; i < 3; i++) {
        IElementType token = lexer.advance();
        System.out.println(token);
    }
}

しかし、それは3null秒を出力します:

null
null
null

なぜ返さCommentないのBadCharacterですか?

4

1 に答える 1

4

これは jflex の問題ではありません。実際には、idea-flex が元の使用方法を変更するためです。

jflex を使用して intellij-idea プラグインを作成する場合、パッチを適用した「JFlex.jar」と「idea-flex.skeleton」を使用し、後でzzRefillメソッドを次のように定義します。

private boolean zzRefill() throws java.io.IOException {
    return true;
}

オリジナルの代わりに:

private boolean zzRefill() throws java.io.IOException {

  // ... ignore some code 

  /* finally: fill the buffer with new input */
  int numRead = zzReader.read(zzBuffer, zzEndRead,
                                          zzBuffer.length-zzEndRead);
  // ... ignore some code

  // numRead < 0
  return true;
}

zzReaderコードに があり、渡した文字列を保持していることに注意してください#!!!!!。しかし、idea-flex バージョンでは、これは使用されません。

idea-flex バージョンを使用するには、次のように使用する必要があります。

public class MyLexer extends FlexAdapter {
    public MyLexer() {
        super(new PubspecLexer((Reader) null));
    }
}

それで:

public static void main(String[] args) {
    String input = "#!!!!!";
    MyLexer lexer = new MyLexer();
    lexer.start(input);
    for (int i = 0; i < 3; i++) {
        System.out.println(lexer.getTokenType());
        lexer.advance();
    }
}

どちらが印刷されますか:

match: --#--
action [19] { System.out.println("Found comment!"); return PubTokenTypes.Comment(); }
Found comment!
Pub:Comment
match: --!--
action [20] { return PubTokenTypes.BadCharacter(); }
Pub:BadCharacter
match: --!--
action [20] { return PubTokenTypes.BadCharacter(); }
Pub:BadCharacter
于 2014-04-13T09:29:59.507 に答える