001: /*
002: * This file is part of a syntax highlighting package
003: * Copyright (C) 1999, 2000 Stephen Ostermiller
004: * http://ostermiller.org/contact.pl?regarding=Syntax+Highlighting
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * See COPYING.TXT for details.
017: */
018: package workbench.sql.formatter;
019:
020: /**
021: * A generic token class.
022: */
023: public abstract class Token {
024:
025: /**
026: * A unique ID for this type of token.
027: * Typically, ID numbers for each type will
028: * be static variables of the Token class.
029: *
030: * @return an ID for this type of token.
031: */
032: public abstract int getID();
033:
034: /**
035: * A description of this token. The description should
036: * be appropriate for syntax highlighting. For example
037: * "comment" might be returned for a comment. This should
038: * make it easy to do html syntax highlighting. Just use
039: * style sheets to define classes with the same name as
040: * the description and write the token in the html file
041: * with that css class name.
042: *
043: * @return a description of this token.
044: */
045: public abstract String getDescription();
046:
047: /**
048: * The actual meat of the token.
049: *
050: * @return a string representing the text of the token.
051: */
052: public abstract String getContents();
053:
054: /**
055: * Determine if this token is a comment. Sometimes comments should be
056: * ignored (compiling code) other times they should be used
057: * (syntax highlighting). This provides a method to check
058: * in case you feel you should ignore comments.
059: *
060: * @return true if this token represents a comment.
061: */
062: public abstract boolean isComment();
063:
064: /**
065: * Determine if this token is whitespace. Sometimes whitespace should be
066: * ignored (compiling code) other times they should be used
067: * (code beautification). This provides a method to check
068: * in case you feel you should ignore whitespace.
069: *
070: * @return true if this token represents whitespace.
071: */
072: public abstract boolean isWhiteSpace();
073:
074: /**
075: * Determine if this token is an error. Lets face it, not all code
076: * conforms to spec. The lexer might know about an error
077: * if a string literal is not closed, for example.
078: *
079: * @return true if this token is an error.
080: */
081: public abstract boolean isError();
082:
083: /**
084: * get the line number of the input on which this token started
085: *
086: * @return the line number of the input on which this token started
087: */
088: public abstract int getLineNumber();
089:
090: /**
091: * get the offset into the input in characters at which this token started
092: *
093: * @return the offset into the input in characters at which this token started
094: */
095: public abstract int getCharBegin();
096:
097: /**
098: * get the offset into the input in characters at which this token ended
099: *
100: * @return the offset into the input in characters at which this token ended
101: */
102: public abstract int getCharEnd();
103:
104: /**
105: * get a String that explains the error, if this token is an error.
106: *
107: * @return a String that explains the error, if this token is an error, null otherwise.
108: */
109: public abstract String errorString();
110:
111: /**
112: * The state of the tokenizer is undefined.
113: */
114: public static final int UNDEFINED_STATE = -1;
115:
116: /**
117: * The initial state of the tokenizer.
118: * Anytime the tokenizer returns to this state,
119: * the tokenizer could be restarted from that point
120: * with side effects.
121: */
122: public static final int INITIAL_STATE = 0;
123:
124: /**
125: * Get an integer representing the state the tokenizer is in after
126: * returning this token.
127: * Those who are interested in incremental tokenizing for performance
128: * reasons will want to use this method to figure out where the tokenizer
129: * may be restarted. The tokenizer starts in Token.INITIAL_STATE, so
130: * any time that it reports that it has returned to this state, the
131: * tokenizer may be restarted from there.
132: */
133: public abstract int getState();
134: }
|