org.antlr.runtime.Lexer Class Reference

Inheritance diagram for org.antlr.runtime.Lexer:

Inheritance graph
[legend]
Collaboration diagram for org.antlr.runtime.Lexer:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 Lexer ()
 Lexer (CharStream input)
 Lexer (CharStream input, RecognizerSharedState state)
void reset ()
Token nextToken ()
void skip ()
abstract void mTokens () throws RecognitionException
void setCharStream (CharStream input)
CharStream getCharStream ()
String getSourceName ()
void emit (Token token)
Token emit ()
void match (String s) throws MismatchedTokenException
void matchAny ()
void match (int c) throws MismatchedTokenException
void matchRange (int a, int b) throws MismatchedRangeException
int getLine ()
int getCharPositionInLine ()
int getCharIndex ()
String getText ()
void setText (String text)
void reportError (RecognitionException e)
String getErrorMessage (RecognitionException e, String[] tokenNames)
String getCharErrorDisplay (int c)
void recover (RecognitionException re)
void traceIn (String ruleName, int ruleIndex)
void traceOut (String ruleName, int ruleIndex)

Protected Attributes

CharStream input


Detailed Description

A lexer is recognizer that draws input symbols from a character stream. lexer grammars result in a subclass of this object. A Lexer object uses simplified match() and error recovery mechanisms in the interest of speed.

Definition at line 35 of file Lexer.java.


Constructor & Destructor Documentation

org.antlr.runtime.Lexer.Lexer (  ) 

Definition at line 39 of file Lexer.java.

org.antlr.runtime.Lexer.Lexer ( CharStream  input  ) 

Definition at line 42 of file Lexer.java.

org.antlr.runtime.Lexer.Lexer ( CharStream  input,
RecognizerSharedState  state 
)

Definition at line 46 of file Lexer.java.


Member Function Documentation

void org.antlr.runtime.Lexer.reset (  ) 

reset the parser's state; subclasses must rewinds the input stream

Reimplemented from org.antlr.runtime.BaseRecognizer.

Definition at line 51 of file Lexer.java.

Token org.antlr.runtime.Lexer.nextToken (  ) 

Return a token from this source; i.e., match a token on the char stream.

Implements org.antlr.runtime.TokenSource.

Definition at line 72 of file Lexer.java.

void org.antlr.runtime.Lexer.skip (  ) 

Instruct the lexer to skip creating a token for current lexer rule and look for another token. nextToken() knows to keep looking when a lexer rule finishes with token set to SKIP_TOKEN. Recall that if token==null at end of any token rule, it creates one for you and emits it.

Definition at line 110 of file Lexer.java.

abstract void org.antlr.runtime.Lexer.mTokens (  )  throws RecognitionException [pure virtual]

This is the lexer entry point that sets instance var 'token'

void org.antlr.runtime.Lexer.setCharStream ( CharStream  input  ) 

Set the char stream and reset the lexer

Definition at line 118 of file Lexer.java.

CharStream org.antlr.runtime.Lexer.getCharStream (  ) 

Definition at line 124 of file Lexer.java.

String org.antlr.runtime.Lexer.getSourceName (  )  [virtual]

Implements org.antlr.runtime.BaseRecognizer.

Definition at line 128 of file Lexer.java.

void org.antlr.runtime.Lexer.emit ( Token  token  ) 

Currently does not support multiple emits per nextToken invocation for efficiency reasons. Subclass and override this method and nextToken (to push tokens into a list and pull from that list rather than a single variable as this implementation does).

Definition at line 137 of file Lexer.java.

Token org.antlr.runtime.Lexer.emit (  ) 

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.

If you are building trees, then you should also override Parser or TreeParser.getMissingSymbol().

Definition at line 150 of file Lexer.java.

void org.antlr.runtime.Lexer.match ( String  s  )  throws MismatchedTokenException

Definition at line 159 of file Lexer.java.

void org.antlr.runtime.Lexer.matchAny (  ) 

Definition at line 178 of file Lexer.java.

void org.antlr.runtime.Lexer.match ( int  c  )  throws MismatchedTokenException

Definition at line 182 of file Lexer.java.

void org.antlr.runtime.Lexer.matchRange ( int  a,
int  b 
) throws MismatchedRangeException

Definition at line 197 of file Lexer.java.

int org.antlr.runtime.Lexer.getLine (  ) 

Definition at line 214 of file Lexer.java.

int org.antlr.runtime.Lexer.getCharPositionInLine (  ) 

Definition at line 218 of file Lexer.java.

int org.antlr.runtime.Lexer.getCharIndex (  ) 

What is the index of the current character of lookahead?

Definition at line 223 of file Lexer.java.

String org.antlr.runtime.Lexer.getText (  ) 

Return the text matched so far for the current token or any text override.

Definition at line 230 of file Lexer.java.

void org.antlr.runtime.Lexer.setText ( String  text  ) 

Set the complete text of this token; it wipes any previous changes to the text.

Definition at line 240 of file Lexer.java.

void org.antlr.runtime.Lexer.reportError ( RecognitionException  e  ) 

Report a recognition problem.

This method sets errorRecovery to indicate the parser is recovering not parsing. Once in recovery mode, no errors are generated. To get out of recovery mode, the parser must successfully match a token (after a resync). So it will go:

1. error occurs 2. enter recovery mode, report error 3. consume until token found in resynch set 4. try to resume parsing 5. next match() will reset errorRecovery mode

If you override, make sure to update syntaxErrors if you care about that.

Reimplemented from org.antlr.runtime.BaseRecognizer.

Definition at line 244 of file Lexer.java.

String org.antlr.runtime.Lexer.getErrorMessage ( RecognitionException  e,
String[]  tokenNames 
)

What error message should be generated for the various exception types?

Not very object-oriented code, but I like having all error message generation within one method rather than spread among all of the exception classes. This also makes it much easier for the exception handling because the exception classes do not have to have pointers back to this object to access utility routines and so on. Also, changing the message for an exception type would be difficult because you would have to subclassing exception, but then somehow get ANTLR to make those kinds of exception objects instead of the default. This looks weird, but trust me--it makes the most sense in terms of flexibility.

For grammar debugging, you will want to override this to add more information such as the stack frame with getRuleInvocationStack(e, this.getClass().getName()) and, for no viable alts, the decision description and state etc...

Override this to change the message generated for one or more exception types.

Reimplemented from org.antlr.runtime.BaseRecognizer.

Definition at line 259 of file Lexer.java.

String org.antlr.runtime.Lexer.getCharErrorDisplay ( int  c  ) 

Definition at line 296 of file Lexer.java.

void org.antlr.runtime.Lexer.recover ( RecognitionException  re  ) 

Lexers can normally match any char in it's vocabulary after matching a token, so do the easy thing and just kill a character and hope it all works out. You can instead use the rule invocation stack to do sophisticated error recovery if you are in a fragment rule.

Definition at line 320 of file Lexer.java.

void org.antlr.runtime.Lexer.traceIn ( String  ruleName,
int  ruleIndex 
)

Definition at line 326 of file Lexer.java.

void org.antlr.runtime.Lexer.traceOut ( String  ruleName,
int  ruleIndex 
)

Definition at line 331 of file Lexer.java.


Member Data Documentation

Where is the lexer drawing characters from?

Definition at line 37 of file Lexer.java.


The documentation for this class was generated from the following file:

Generated on Wed Oct 1 14:13:40 2008 for ANTLR API by  doxygen 1.5.5