[antlr-interest] Filter lexers do not skip tokens

Zenzike zenzike at gmail.com
Thu Jan 22 03:57:48 PST 2009


It looks like a bug.

I suggest that in the file
/src/org/antlr/codegen/templates/Java/Java.stg modifies its definition
of filteringNextToken() to the following,
where a check for state.token==Token.SKIP_TOKEN occurs, and emit() is
only called if the state token is not previously set.

filteringNextToken() ::= <<
public Token nextToken() {
    while (true) {
        if ( input.LA(1)==CharStream.EOF ) {
            return Token.EOF_TOKEN;
        }
        state.token = null;
	state.channel = Token.DEFAULT_CHANNEL;
        state.tokenStartCharIndex = input.index();
        state.tokenStartCharPositionInLine = input.getCharPositionInLine();
        state.tokenStartLine = input.getLine();
	state.text = null;
        try {
            int m = input.mark();
            state.backtracking=1; <! means we won't throw slow exception !>
            state.failed=false;
            mTokens();
            state.backtracking=0;
            <! mTokens backtracks with synpred at backtracking==2
               and we set the synpredgate to allow actions at level 1. !>
            if ( state.failed ) {
                input.rewind(m);
                input.consume(); <! advance one char and try again !>
            }
            else {            <! emit a token only if it is not a SKIP_TOKEN !>
    				if ( state.token==null ) {
    					emit();
    				}
    				else if ( state.token==Token.SKIP_TOKEN ) {
    					continue;
    				}
    				return state.token;
            }
        }
        catch (RecognitionException re) {
            // shouldn't happen in backtracking mode, but...
            reportError(re);
            recover(re);
        }
    }
}


More information about the antlr-interest mailing list