001: /*
002: * Copyright (C) 2000-2001 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=BTE
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017:
018: package com.Ostermiller.bte;
019:
020: /**
021: * A Token is a token that is returned by a lexer that is lexing a bte
022: * source file. It has several attributes describing the token:
023: * The type of token, the text of the token, the line number on which it
024: * occurred, the number of characters into the input at which it started, and
025: * similarly, the number of characters into the inut at which it ended. <br>
026: */
027: class Token {
028:
029: private int ID;
030: private String contents;
031: private int lineNumber;
032: private int charBegin;
033: private int charEnd;
034:
035: /**
036: * Create a new token.
037: * The constructor is typically called by the lexer
038: *
039: * @param ID the id number of the token
040: * @param contents A string representing the text of the token
041: * @param lineNumber the line number of the input on which this token started
042: * @param charBegin the offset into the input in characters at which this token started
043: * @param charEnd the offset into the input in characters at which this token ended
044: */
045: Token(int ID, String contents, int lineNumber, int charBegin,
046: int charEnd) {
047: this .ID = ID;
048: this .contents = new String(contents);
049: this .lineNumber = lineNumber;
050: this .charBegin = charBegin;
051: this .charEnd = charEnd;
052: }
053:
054: /**
055: * get the ID number of this token
056: *
057: * @return the id number of the token
058: */
059: int getID() {
060: return ID;
061: }
062:
063: /**
064: * get the contents of this token
065: *
066: * @return A string representing the text of the token
067: */
068: String getContents() {
069: return (new String(contents));
070: }
071:
072: /**
073: * get the line number of the input on which this token started
074: *
075: * @return the line number of the input on which this token started
076: */
077: int getLineNumber() {
078: return lineNumber;
079: }
080:
081: /**
082: * get the offset into the input in characters at which this token started
083: *
084: * @return the offset into the input in characters at which this token started
085: */
086: int getCharBegin() {
087: return charBegin;
088: }
089:
090: /**
091: * get the offset into the input in characters at which this token ended
092: *
093: * @return the offset into the input in characters at which this token ended
094: */
095: int getCharEnd() {
096: return charEnd;
097: }
098:
099: /**
100: * Checks this token to see if it is a comment.
101: *
102: * @return true if this token is a comment, false otherwise
103: */
104: boolean isComment() {
105: return (false);
106: }
107:
108: /**
109: * Checks this token to see if it is White Space.
110: * Usually tabs, line breaks, form feed, spaces, etc.
111: *
112: * @return true if this token is White Space, false otherwise
113: */
114: boolean isWhiteSpace() {
115: return (ID == sym.WHITESPACE);
116: }
117:
118: /**
119: * Checks this token to see if it is an Error.
120: * Unfinished comments, numbers that are too big, unclosed strings, etc.
121: *
122: * @return true if this token is an Error, false otherwise
123: */
124: boolean isError() {
125: return (false);
126: }
127:
128: /**
129: * get a String that explains the error, if this token is an error.
130: *
131: * @return a String that explains the error, if this token is an error, null otherwise.
132: */
133: String errorString() {
134: return "";
135: }
136:
137: /**
138: * get a representation of this token as a human readable string.
139: * The format of this string is subject to change and should only be used
140: * for debugging purposes.
141: *
142: * @return a string representation of this token
143: */
144: public String toString() {
145: return ("Token " + ID + " Line " + lineNumber + " from "
146: + charBegin + " to " + charEnd + " : " + contents);
147: }
148:
149: }
|