tribble.io
Class LexerAdapter

java.lang.Object
  extended by tribble.io.LexerAdapter
All Implemented Interfaces:
DiagnosticWriterI, LexerI

public abstract class LexerAdapter
extends java.lang.Object
implements LexerI, DiagnosticWriterI

Generic lexical analyzer (lexer) adapter.

This class is used as an abtract base class upon which to implement any kind of lexical analyzer (lexer), which reads characters from an input source stream (implementing the java.io.Reader class) and converting it into a stream of tokens (which implement the TokenI interface). Such a lexer stream can be used to read the underlying source text for a parser, such as those implementing the ParserI interface.

Since:
2001-07-17
Version:
$Revision: 1.5 $ $Date: 2003/02/26 04:41:16 $
Author:
David R. Tribble, david@tribble.com.
Copyright 2001-2003 by David R. Tribble all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.
See Also:
ParserI, TokenI, CharReader

Field Summary
protected  int m_colNo
          Input source column number.
protected  java.lang.String m_fName
          Input source filename.
protected  java.io.Reader m_in
          Lexer input stream.
protected  int m_lineNo
          Input source line number.
protected  DiagnosticOutputI m_out
          Diagnostic message output stream.
(package private) static java.lang.String REV
          Revision information.
 
Constructor Summary
protected LexerAdapter()
          Default constructor.
  LexerAdapter(java.io.Reader in)
          Constructor.
 
Method Summary
 void close()
          Close and disassociate the underlying input stream from this lexer.
protected  void finalize()
          Finalization.
 TokenI getToken()
          Read the next token from the input stream.
abstract  boolean getToken(TokenI tok)
          Read the next token from the lexer input stream.
 void setInput(java.io.Reader in)
          Establish the input stream for this lexer from which to read tokens.
 void setOutput(DiagnosticOutputI out)
          Establish the output stream to which diagnostic messages (warning and error messages) are written during parsing.
 void setSourceName(java.lang.String fname)
          Establish the input source filename.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REV

static final java.lang.String REV
Revision information.

See Also:
Constant Field Values

m_in

protected java.io.Reader m_in
Lexer input stream.


m_out

protected DiagnosticOutputI m_out
Diagnostic message output stream.


m_fName

protected java.lang.String m_fName
Input source filename.


m_lineNo

protected int m_lineNo
Input source line number.


m_colNo

protected int m_colNo
Input source column number.

Constructor Detail

LexerAdapter

public LexerAdapter(java.io.Reader in)
Constructor.

Parameters:
in - A character input stream from which to read tokens.
Since:
1.5, 2002-02-25

LexerAdapter

protected LexerAdapter()
Default constructor.

Since:
1.1, 2001-07-17
Method Detail

setOutput

public void setOutput(DiagnosticOutputI out)
Establish the output stream to which diagnostic messages (warning and error messages) are written during parsing.

Specified by:
setOutput in interface DiagnosticWriterI
Parameters:
out - The error output stream.
Since:
1.1, 2001-07-17

setInput

public void setInput(java.io.Reader in)
Establish the input stream for this lexer from which to read tokens.

Specified by:
setInput in interface LexerI
Parameters:
in - A character input stream from which to read tokens.
Since:
1.5, 2002-02-25

setSourceName

public void setSourceName(java.lang.String fname)
Establish the input source filename.

Specified by:
setSourceName in interface LexerI
Parameters:
fname - The source filename.
Since:
1.1, 2001-07-17

close

public void close()
Close and disassociate the underlying input stream from this lexer.

Specified by:
close in interface LexerI
Since:
1.1, 2001-07-17

getToken

public TokenI getToken()
                throws java.io.IOException
Read the next token from the input stream.

Either this method or getToken(TokenI) may be used interchangeably.

Specified by:
getToken in interface LexerI
Returns:
The next token read from the input stream, or null if there are no more tokens (i.e., the end of the token input stream was reached).
Throws:
java.io.IOException - Thrown if an I/O error or parsing error occurs.
Since:
1.1, 2001-07-17

getToken

public abstract boolean getToken(TokenI tok)
                          throws java.io.IOException
Read the next token from the lexer input stream.

Either this method or getToken() may be used interchangeably. This method might be more efficient, since a new token object does not need to be allocated on each call.

Specified by:
getToken in interface LexerI
Parameters:
tok - The token object which is filled in with the next token read from the input stream.
Returns:
True if a token was successfully read from the input stream, otherwise false (i.e., the end of the token input stream was reached).
Throws:
java.io.IOException - Thrown if an I/O error or parsing error occurs.
Since:
1.1, 2001-07-17

finalize

protected void finalize()
                 throws java.lang.Throwable
Finalization. Close and disassociate the input stream (m_in) from this lexer. Also disassociates the diagnostic output stream (m_out) from this lexer.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable
Since:
1.1, 2001-07-17