在HIDDEN频道中迭代令牌

时间:2015-01-30 21:37:00

标签: java antlr4

我目前正致力于为自定义,类似lua的脚本语言MobTalkerScript(MTS)创建一个IDE,它为我提供了一个ANTLR4 lexer。由于MTS语言文件中的规范将注释放入HIDDEN_CHANNEL频道,因此我需要告诉词法分析者实际从HIDDEN_CHANNEL频道读取。这就是我试图这样做的方式。

Mts3Lexer lexer = new Mts3Lexer(new ANTLRInputStream("<replace this with the input>"));
lexer.setTokenFactory(new CommonTokenFactory(false));
lexer.setChannel(Token.HIDDEN_CHANNEL);

Token token = lexer.emit();
int type = token.getType();

do {
    switch(type) {
        case Mts3Lexer.LINE_COMMENT:
        case Mts3Lexer.COMMENT:
            System.out.println("token "+token.getText()+" is a comment");
        default:
            System.out.println("token "+token.getText()+" is not a comment");
    }
} while((token = lexer.nextToken()) != null && (type = token.getType()) != Token.EOF);

现在,如果我在以下输入上使用此代码,则除token ... is not a comment之外的任何内容都会打印到控制台。

function foo()
    -- this should be a single-line comment
    something = "blah"
    --[[ this should
         be a multi-line
         comment ]]--
end

但是包含评论的标记永远不会出现。所以我搜索了这个问题的根源,并在ANTLR4 Lexer类中找到了以下方法:

/** Return a token from this source; i.e., match a token on the char
 *  stream.
 */
@Override
public Token nextToken() {
    if (_input == null) {
        throw new IllegalStateException("nextToken requires a non-null input stream.");
    }

    // Mark start location in char stream so unbuffered streams are
    // guaranteed at least have text of current token
    int tokenStartMarker = _input.mark();
    try{
        outer:
        while (true) {
            if (_hitEOF) {
                emitEOF();
                return _token;
            }

            _token = null;
            _channel = Token.DEFAULT_CHANNEL;
            _tokenStartCharIndex = _input.index();
            _tokenStartCharPositionInLine = getInterpreter().getCharPositionInLine();
            _tokenStartLine = getInterpreter().getLine();
            _text = null;
            do {
                _type = Token.INVALID_TYPE;
                // System.out.println("nextToken line "+tokenStartLine+" at "+((char)input.LA(1))+
                // " in mode "+mode+
                // " at index "+input.index());
                int ttype;
                try {
                    ttype = getInterpreter().match(_input, _mode);
                }
                catch (LexerNoViableAltException e) {
                    notifyListeners(e);     // report error
                    recover(e);
                    ttype = SKIP;
                }
                if ( _input.LA(1)==IntStream.EOF ) {
                    _hitEOF = true;
                }
                if ( _type == Token.INVALID_TYPE ) _type = ttype;
                if ( _type ==SKIP ) {
                    continue outer;
                }
            } while ( _type ==MORE );
            if ( _token == null ) emit();
            return _token;
        }
    }
    finally {
        // make sure we release marker after match or
        // unbuffered char stream will keep buffering
        _input.release(tokenStartMarker);
    }
}

引起我注意的界线如下:

_channel = Token.DEFAULT_CHANNEL;

我对ANTLR了解不多,但显然这条线将词法分析器保留在DEFAULT_CHANNEL频道。

我尝试从HIDDEN_CHANNEL频道正确读取的方式是否正确,或者我是否无法将nextToken()与隐藏频道一起使用?

2 个答案:

答案 0 :(得分:3)

我发现为什么lexer没有给我任何包含评论的令牌 - 我似乎错过了语法文件跳过评论而不是将它们放入隐藏的频道。与作者联系,更改了语法文件,现在它可以正常工作。

请注意自己:更多地关注你所读到的内容。

答案 1 :(得分:0)

对于Go(golang),这个代码段对我有用:

import (
    "github.com/antlr/antlr4/runtime/Go/antlr"
)

type antlrparser interface {
    GetParser() antlr.Parser
}

func fullText(prc antlr.ParserRuleContext) string {
    p := prc.(antlrparser).GetParser()
    ts := p.GetTokenStream()
    tx := ts.GetTextFromTokens(prc.GetStart(), prc.GetStop())
    return tx
}

ctx.GetSomething()传递给fullText。当然,如上所示,空格必须转到*.g4文件中的隐藏通道:

WS: [ \t\r\n] -> channel(HIDDEN);
相关问题