lp.parse
Class LpLookaheadLexer

java.lang.Object
  extended by lp.parse.LpLexer
      extended by lp.parse.LpLookaheadLexer
All Implemented Interfaces:
Closeable

public class LpLookaheadLexer
extends LpLexer

This class provides the same functionality as LpLexer and it offers the possibility to take a look at future tokens and not loose the current position in the input.

Version:
1.0.0
Author:
Martin Slota
See Also:
LpLexer

Field Summary
private  LinkedList<LpToken> buffer
          A LpToken buffer used to implement the lookahead.
private static String NEGATIVE_STEPS_MESSAGE
          A string with an exception message used multiple times in the implementation.
 
Constructor Summary
LpLookaheadLexer()
           
 
Method Summary
 void close()
          Closes the underlying reader.
 String getLexem()
          Returns the lexem corresponding to the last token read.
 String getLexem(int n)
          Returns the lexem corresponding the token n steps further in the input.
 int getLineNumber()
          Returns the number of line of input on which the last token occured.
 int getLineNumber(int n)
          Returns the number of line of input on which the token n steps further in the input occurs.
 int getPosition()
          Returns the position of the last token's beginning within the line of input it's on.
 int getPosition(int n)
          Returns the position of the token n steps further in the input.
 LpToken getToken()
          Returns a LpToken instance containing information about the last token read.
 LpToken getToken(int n)
          Returns a LpToken instance containing information about the token n steps further in the input.
 LpTokenType getTokenType()
          Returns the type of the last token read.
 LpTokenType getTokenType(int n)
          Returns the type of the token n steps further in the input.
protected  void initialize()
          Reinitializes members and reads the first lookahead character.
private  void load(int count)
          Reads count tokens and stores them in the buffer.
 void nextToken()
          Reads the next token occuring on the input.
 
Methods inherited from class lp.parse.LpLexer
setInput, setInput, setInput
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NEGATIVE_STEPS_MESSAGE

private static final String NEGATIVE_STEPS_MESSAGE
A string with an exception message used multiple times in the implementation.

See Also:
Constant Field Values

buffer

private final LinkedList<LpToken> buffer
A LpToken buffer used to implement the lookahead.

Constructor Detail

LpLookaheadLexer

public LpLookaheadLexer()
Method Detail

close

public void close()
Closes the underlying reader. If LpLexer.setInput(CharSequence) or LpLexer.setInput(File) was used to set the current character source, this method should be called when no more tokens are required from the source. In other cases it is up to the programmer whether she will close the Reader given to LpLexer.setInput(Reader) herself or call this method.

Specified by:
close in interface Closeable
Overrides:
close in class LpLexer

initialize

protected void initialize()
Reinitializes members and reads the first lookahead character.

Overrides:
initialize in class LpLexer

nextToken

public void nextToken()
Reads the next token occuring on the input. More information about tokens can be found in the class description.

Overrides:
nextToken in class LpLexer

getTokenType

public LpTokenType getTokenType()
Returns the type of the last token read. This method is not meant to be called before LpLexer.nextToken() is called at least once after the last setInput() call. But if such a situation occurs, null is returned. Similarily, if LpLexer.close() has already been called, null is returned.

Overrides:
getTokenType in class LpLexer
Returns:
type of the last token read

getLexem

public String getLexem()
Returns the lexem corresponding to the last token read. In case it is a LpTokenType.EOF token, empty string is returned. This method is not meant to be called before LpLexer.nextToken() is called at least once after the last setInput() call. But if such a situation occurs, null is returned. Similarily, if LpLexer.close() has already been called, null is returned.

Overrides:
getLexem in class LpLexer
Returns:
lexem corresponding to the last token read

getLineNumber

public int getLineNumber()
Returns the number of line of input on which the last token occured. Lines are numbered from 1 (see the example in class description). A newline starts when either a '\n' or a '\r' character is detected. There is one exception: a '\n' character occuring right after a '\r' character is ignored, i.e. not considered to be another line delimiter. This method is not meant to be called before LpLexer.nextToken() is called at least once after the last setInput() call. But if such a situation occurs, -1 is returned. Similarily, if LpLexer.close() has already been called, -1 is returned.

Overrides:
getLineNumber in class LpLexer
Returns:
the number of line of input on which the last token occured

getPosition

public int getPosition()
Returns the position of the last token's beginning within the line of input it's on. The characters on the line are numbered from 1 (see the example in class description). Tabs also count as 1 character. This method is not meant to be called before LpLexer.nextToken() is called at least once after the last setInput() call. But if such a situation occurs, -1 is returned. Similarily, if LpLexer.close() has already been called, -1 is returned.

Overrides:
getPosition in class LpLexer
Returns:
position of the last token's beginning within a line of input

getToken

public LpToken getToken()
Returns a LpToken instance containing information about the last token read. The information is read using the LpLexer.getTokenType(), LpLexer.getLexem(), LpLexer.getPosition() and LpLexer.getLineNumber() methods.

Overrides:
getToken in class LpLexer
Returns:
a LpToken instance containing information about the last token read

getTokenType

public LpTokenType getTokenType(int n)
Returns the type of the token n steps further in the input. The same result will be returned if getTokenType() is called after n times calling nextToken(). The difference is that the current token doesn't change by calling this method—new tokens are read but the old ones are stored in a buffer. This method is not meant to be called before nextToken() is called at least once. But if such a situation occurs, null is returned. Similarily, if close() has already been called, null is returned.

Parameters:
n - number of steps further in the input to look at
Returns:
type of token n steps further in the input
Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while reading the input
IllegalArgumentException - if n is negative

getLexem

public String getLexem(int n)
Returns the lexem corresponding the token n steps further in the input. In case it is a LpTokenType.EOF token, empty string is returned. The same result will be returned if getLexem() is called after n times calling nextToken(). The difference is that the current token doesn't change by calling this method—new tokens are read but the old ones are stored in a buffer. This method is not meant to be called before nextToken() is called at least once. But if such a situation occurs, null is returned. Similarily, if close() has already been called, null is returned.

Parameters:
n - number of steps further in the input to look at
Returns:
lexem corresponding to the last tokenn n steps further in the input
Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while reading the input
IllegalArgumentException - if n is negative

getLineNumber

public int getLineNumber(int n)
Returns the number of line of input on which the token n steps further in the input occurs. Lines are numbered from 1. A newline starts when either a '\n' or a '\r' character is detected. There is one exception: a '\n' character occuring right after a '\r' character is ignored, i.e. not considered to be another line delimiter. The same result will be returned if getLineNumber() is called after n times calling nextToken(). The difference is that the current token doesn't change by calling this method—new tokens are read but the old ones are stored in a buffer. This method is not meant to be called before nextToken() is called at least once. But if such a situation occurs, -1 is returned. Similarily, if close() has already been called, -1 is returned.

Parameters:
n - number of steps further in the input to look at
Returns:
number of line on which the token n steps further in the input occurs
Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while reading the input
IllegalArgumentException - if n is negative

getPosition

public int getPosition(int n)
Returns the position of the token n steps further in the input. The characters on the line are numbered from 1. The same result will be returned if getPosition() is called after n times calling nextToken(). The difference is that the current token doesn't change by calling this method—new tokens are read but the old ones are stored in a buffer. This method is not meant to be called before nextToken() is called at least once. But if such a situation occurs, -1 is returned. Similarily, if close() has already been called, -1 is returned.

Parameters:
n - number of steps further in the input to look at
Returns:
position of the token's beginning within a line of input
Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while reading the input
IllegalArgumentException - if n is negative

getToken

public LpToken getToken(int n)
Returns a LpToken instance containing information about the token n steps further in the input. The same result will be returned if getToken() is called after n times calling nextToken(). The difference is that the current token doesn't change by calling this method—new tokens are read but the old ones are stored in a buffer. This method is not meant to be called before nextToken() is called at least once. But if such a situation occurs, null is returned. Similarily, if close() has already been called, null is returned.

Parameters:
n - number of steps further in the input to look at
Returns:
a LpToken instance containing information about the token n steps further in the input
Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while reading the input
IllegalArgumentException - if n is negative

load

private void load(int count)
Reads count tokens and stores them in the buffer.

Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while reading the input