//============================================================================== // tribble/io/TokenAdapter.java //------------------------------------------------------------------------------ package tribble.io; // System imports import java.lang.Exception; import java.lang.String; import java.io.PrintWriter; // Local imports import tribble.io.TokenI; import tribble.io.TraceableI; /******************************************************************************* * Default generic lexical token implementation. * *

* This is a simple implementation for any kind of lexical token object, which * is constructed by some form of lexical analyzer (lexer) such as any that * implements the {@link LexerI} interface. A token object represents a single * lexical entity from an input source text stream. A lexer provides a stream of * tokens read from such an input stream, which can then be used by a syntactical * parser (such as any that implements the {@link ParserI} interface) for parsing * sequences of source tokens into a symbol table or parse tree. * *

* A token object has the following attributes: * *

*

*

text *
* The textual contents of the token, which is typically the characters it is * composed of as it occurs in the source text stream. * *

*

type *
* A code used to indicate what kind of token it is. This is typically a * particular value from a predefined set of token code constants. * *

*

line number *
* The source line number, corresponding to the physical text line that the * token was read from. * *

*

column number *
* The source column number, corresponding to the physical text column that the * token was read from. This can be either the column number at which the * token starts or ends. * *

*

filename *
* The source filename, corresponding to the name of the physical file or * stream that the token was read from. *
* * @version $Revision: 1.3 $ $Date: 2003/02/23 03:06:13 $ * @since 2003-02-22 * @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. */ public class TokenAdapter implements TokenI, TraceableI { // Identification /** Revision information. */ static final String REV = "@(#)tribble/io/TokenAdapter.java $Revision: 1.3 $ $Date: 2003/02/23 03:06:13 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected variables /** Token code. */ protected int m_tok; /** Token text. */ protected String m_text; /** Source line number. */ protected int m_lineNo; /** Source column number. */ protected int m_colNo; /** Source filename. */ protected String m_fileName; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public constructors /*************************************************************************** * Default constructor. * * @since 1.1, 2001-05-19 */ public TokenAdapter() { // Do nothing } /*************************************************************************** * Constructor. * * @param text * The string containing the textual contents for this token. * * @since 1.1, 2001-05-19 */ public TokenAdapter(String text) { setText(text); } /*************************************************************************** * Constructor. * * @param type * The token type code for this token. * * @param text * The string containing the textual contents for this token. * * @param ln * The source line number for this token. * * @since 1.1, 2001-05-19 */ public TokenAdapter(int type, String text, int ln) { setType(type); setText(text); setLineNumber(ln); } /*************************************************************************** * Constructor. * * @param type * The token type code for this token. * * @param text * The string containing the textual contents for this token. * * @param ln * The source line number for this token. * * @param col * The source column number for this token. * * @since 1.3, 2003-02-22 */ public TokenAdapter(int type, String text, int ln, int col) { setType(type); setText(text); setLineNumber(ln); setColumnNumber(col); } /*************************************************************************** * Constructor. * * @param type * The token type code for this token. * * @param text * The string containing the textual contents for this token. * * @param fname * The source filename for this token. * * @param ln * The source line number for this token. * * @param col * The source column number for this token. * * @since 1.3, 2003-02-22 */ public TokenAdapter(int type, String text, String fname, int ln, int col) { setType(type); setText(text); setFileName(fname); setLineNumber(ln); setColumnNumber(col); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public methods /*************************************************************************** * Establish the textual contents of this token. * * @param text * The string containing the textual contents for this token. * * @since 1.1, 2001-05-19 */ public void setText(String text) //implements tribble.io.TokenI { m_text = text; } /*************************************************************************** * Retrieve the textual contents of this token. * * @return * A string containing the textual contents for this token. * * @since 1.1, 2001-05-19 */ public String getText() //implements tribble.io.TokenI { return (m_text); } /*************************************************************************** * Establish the token type code of this token. * * @param type * The token type code for this token. * * @since 1.1, 2001-05-19 */ public void setType(int type) //implements tribble.io.TokenI { m_tok = type; } /*************************************************************************** * Retrieve the type code of this token. * * @return * The type code for this token. * * @since 1.1, 2001-05-19 */ public int getType() //implements tribble.io.TokenI { return (m_tok); } /*************************************************************************** * Establish the source line number of this token. * * @param ln * The source line number for this token. * If this number is negative, the line number setting of this token is not * modified. * * @since 1.3, 2003-02-22 */ public void setLineNumber(int ln) //implements tribble.io.TokenI { if (ln >= 0) m_lineNo = ln; } /*************************************************************************** * Retrieve the source line number of this token. * * @return * The source line number for this token. * * @since 1.3, 2003-02-22 */ public int getLineNumber() //implements tribble.io.TokenI { return (m_lineNo); } /*************************************************************************** * Establish the source column number of this token. * * @param col * The source column number for this token. * If this number is negative, the column number setting of this token is not * modified. * * @since 1.3, 2003-02-22 */ public void setColumnNumber(int col) //implements tribble.io.TokenI { if (col >= 0) m_colNo = col; } /*************************************************************************** * Retrieve the source column number of this token. * * @return * The source column number for this token. * * @since 1.3, 2003-02-22 */ public int getColumnNumber() //implements tribble.io.TokenI { return (m_colNo); } /*************************************************************************** * Establish the source filename of this token. * * @param fname * The source filename for this token. * * @since 1.1, 2001-05-19 */ public void setFileName(String fname) //implements tribble.io.TokenI { m_fileName = fname; } /*************************************************************************** * Retrieve the source filename of this token. * * @return * The source filename for this token. * * @since 1.1, 2001-05-19 */ public String getFileName() //implements tribble.io.TokenI { return (m_fileName); } /*************************************************************************** * Dump the contents of this token to a (debugging) output stream. * * @param out * A (debugging) output stream. * * @since 1.1, 2001-05-19 */ public void dump(PrintWriter out) //implements tribble.io.TraceableI { // Check for an open output stream if (out == null) return; // Print the contents of this object out.println("TokenAdapter @" + hashCode() + ":"); out.println(".m_tok ............ " + m_tok); out.println(".m_text ........... \"" + m_text + "\""); out.println(".m_lineNo ......... " + m_lineNo); out.println(".m_colNo .......... " + m_colNo); out.println(".m_fileName ....... \"" + m_fileName + "\""); } /*************************************************************************** * Copy the contents of another token into this token. * * @param tok * Another token to copy from. * * @since 1.1, 2001-07-20 */ public void copyFrom(TokenAdapter tok) { // Copy the contents of another token into this token this.m_tok = tok.m_tok; this.m_text = tok.m_text; this.m_lineNo = tok.m_lineNo; this.m_colNo = tok.m_colNo; this.m_fileName = tok.m_fileName; } } // End TokenAdapter.java