//============================================================================== // tribble/io/LexerAdapter.java //------------------------------------------------------------------------------ package tribble.io; // System imports import java.io.IOException; import java.io.Reader; import java.lang.Exception; import java.lang.String; import java.lang.Throwable; // Local imports import tribble.io.DiagnosticOutputI; import tribble.io.DiagnosticWriterI; import tribble.io.LexerI; import tribble.io.TokenAdapter; import tribble.io.TokenI; /******************************************************************************* * 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 {@link TokenI} interface). * Such a lexer stream can be used to read the underlying source text for a * parser, such as those implementing the {@link ParserI} interface. * * @version $Revision: 1.5 $ $Date: 2003/02/26 04:41:16 $ * @since 2001-07-17 * @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 ParserI * @see TokenI * @see CharReader */ public abstract class LexerAdapter implements LexerI, DiagnosticWriterI { // Identification /** Revision information. */ static final String REV = "@(#)tribble/io/LexerAdapter.java $Revision: 1.5 $ $Date: 2003/02/26 04:41:16 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Constants // (None) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Static methods // (None) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected variables /** Lexer input stream. */ protected Reader m_in; /** Diagnostic message output stream. */ protected DiagnosticOutputI m_out; /** Input source filename. */ protected String m_fName; /** Input source line number. */ protected int m_lineNo = 1; /** Input source column number. */ protected int m_colNo = 1; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public constructors /*************************************************************************** * Constructor. * * @param in * A character input stream from which to read tokens. * * @since 1.5, 2002-02-25 */ public LexerAdapter(Reader in) { // Establish the input stream for this lexer setInput(in); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public methods /*************************************************************************** * Establish the output stream to which diagnostic messages (warning and * error messages) are written during parsing. * * @param out * The error output stream. * * @since 1.1, 2001-07-17 */ public synchronized void setOutput(DiagnosticOutputI out) //implements tribble.io.DiagnosticWriterI { // Establish the diagnostic output stream m_out = out; } /*************************************************************************** * Establish the input stream for this lexer from which to read tokens. * * @param in * A character input stream from which to read tokens. * * @since 1.5, 2002-02-25 */ public synchronized void setInput(Reader in) //implements tribble.io.LexerI { // Establish the input stream for this lexer m_in = in; } /*************************************************************************** * Establish the input source filename. * * @param fname * The source filename. * * @since 1.1, 2001-07-17 */ public synchronized void setSourceName(String fname) //implements tribble.io.LexerI { // Establish the input source filename m_fName = fname; } /*************************************************************************** * Close and disassociate the underlying input stream from this lexer. * * @since 1.1, 2001-07-17 */ public synchronized void close() //implements tribble.io.LexerI { try { // Close the input stream if (m_in != null) { synchronized (m_in) { m_in.close(); } } } catch (IOException ex) { // Ignored } // Disassociate the underlying input stream from this lexer m_in = null; } /*************************************************************************** * Read the next token from the input stream. * *

* Either this method or {@link #getToken(TokenI)} may be used * interchangeably. * * @return * 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 IOException * Thrown if an I/O error or parsing error occurs. * * @since 1.1, 2001-07-17 */ public TokenI getToken() throws IOException //implements tribble.io.LexerI { TokenAdapter tok; // Create and fill a new token tok = new TokenAdapter(); if (getToken(tok)) return (tok); else return (null); } /*************************************************************************** * Read the next token from the lexer input stream. * *

* Either this method or {@link #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. * * @param tok * The token object which is filled in with the next token read from the * input stream. * * @return * 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 IOException * Thrown if an I/O error or parsing error occurs. * * @since 1.1, 2001-07-17 */ public abstract boolean getToken(TokenI tok) throws IOException; //implements tribble.io.LexerI // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected constructors /*************************************************************************** * Default constructor. * * @since 1.1, 2001-07-17 */ protected LexerAdapter() { // Do nothing } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected methods /*************************************************************************** * Finalization. * Close and disassociate the input stream ({@link #m_in}) from this lexer. * Also disassociates the diagnostic output stream ({@link #m_out}) from this * lexer. * * @since 1.1, 2001-07-17 */ protected synchronized void finalize() throws Throwable { // Close the input stream close(); // Note: Do not close the diagnostic output stream 'm_out' // Disassociate the diagnostic output stream from this lexer m_out = null; // Cascade super.finalize(); } } // End LexerAdapter.java