[antlr-interest] Replacing the token type

Terence Parr parrt at cs.usfca.edu
Thu May 31 11:09:04 PDT 2007


Hi Han! Nice to see you're stilling using ANTLR. :)  Try overriding  
Lexer method:

	/** The standard method called to automatically emit a token at the
	 *  outermost lexical rule.  The token object should point into the
	 *  char buffer start..stop.  If there is a text override in 'text',
	 *  use that to set the token's text.  Override this method to emit
	 *  custom Token objects.
	 */
	public Token emit() {
		Token t = new CommonToken(input, type, channel,  
tokenStartCharIndex, getCharIndex()-1);
		t.setLine(tokenStartLine);
		t.setText(text);
		t.setCharPositionInLine(tokenStartCharPositionInLine);
		emit(t);
		return t;
	}


On May 30, 2007, at 5:35 PM, Gokhan Caglar wrote:

> Hi, I was wondering what would be the simplest way of changing the  
> instantiated token class?  I guess I have to derive from  
> CommonToken, and and CommonTokenCreator, but how can I hook that up  
> to the lexer?
> Thanks,
> Gokhan



More information about the antlr-interest mailing list