Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
grammar T;

@lexer::members {
	public Token nextToken() {
		while (true) {
			state.token = null;
			state.channel = Token.DEFAULT_CHANNEL;
			state.tokenStartCharIndex = input.index();
			state.tokenStartCharPositionInLine = input.getCharPositionInLine();
			state.tokenStartLine = input.getLine();
			state.text = null;
			if ( input.LA(1)==CharStream.EOF ) {
				return Token.EOF_TOKEN;
			}
			try {
				mTokens();
				if ( state.token==null ) {
					emit();
				}
				else if ( state.token==Token.SKIP_TOKEN ) {
					continue;
				}
				return state.token;
			}
			catch (RecognitionException re) {
				reportError(re);
				if ( re instanceof NoViableAltException ) { recover(re); }
                                // create token that holds mismatched char
				Token t = new CommonToken(input, Token.INVALID_TOKEN_TYPE,
				                          Token.DEFAULT_CHANNEL,
				                          state.tokenStartCharIndex,
				                          getCharIndex()-1);
				t.setLine(state.tokenStartLine);
				t.setCharPositionInLine(state.tokenStartCharPositionInLine);
				emit(t);
				if ( re instanceof NoViableAltException ) { recover(re); }
				return state.token;
			}
		}
	}
}

a
@init {
    for (Object t : ((CommonTokenStream)input).getTokens()) { // dump token stream from lexer
        System.out.println(t);
    }
}
  : 'hi' INT {System.out.println("not printed upon lexical error");}
  ;

INT : '0'..'9'+ ;
WS : (' '|'\n')+ {skip();} ;

...