001: /*
002: * Token.java - Syntax token
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1998, 1999, 2000, 2001, 2002 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package org.gjt.sp.jedit.syntax;
023:
024: import java.lang.reflect.Field;
025:
026: /**
027: * A linked list of syntax tokens.
028: *
029: * @author Slava Pestov
030: * @version $Id: Token.java 5354 2006-03-03 16:18:06Z ezust $
031: */
032: public class Token {
033: //{{{ stringToToken() method
034: /**
035: * Converts a token type string to a token type constant.
036: * @param value The token type
037: * @since jEdit 4.1pre1
038: */
039: public static byte stringToToken(String value) {
040: try {
041: Field f = Token.class.getField(value);
042: return f.getByte(null);
043: } catch (Exception e) {
044: return -1;
045: }
046: } //}}}
047:
048: //{{{ tokenToString() method
049: /**
050: * Converts a token type constant to a token type string.
051: * @since jEdit 4.2pre1
052: */
053: public static String tokenToString(byte token) {
054: return TOKEN_TYPES[token];
055: } //}}}
056:
057: //{{{ Token types
058: public static final String[] TOKEN_TYPES = new String[] { "NULL",
059: "COMMENT1", "COMMENT2", "COMMENT3", "COMMENT4", "DIGIT",
060: "FUNCTION", "INVALID", "KEYWORD1", "KEYWORD2", "KEYWORD3",
061: "KEYWORD4", "LABEL", "LITERAL1", "LITERAL2", "LITERAL3",
062: "LITERAL4", "MARKUP", "OPERATOR" };
063:
064: public static final byte NULL = 0;
065:
066: public static final byte COMMENT1 = 1;
067: public static final byte COMMENT2 = 2;
068: public static final byte COMMENT3 = 3;
069: public static final byte COMMENT4 = 4;
070: public static final byte DIGIT = 5;
071: public static final byte FUNCTION = 6;
072: public static final byte INVALID = 7;
073: public static final byte KEYWORD1 = 8;
074: public static final byte KEYWORD2 = 9;
075: public static final byte KEYWORD3 = 10;
076: public static final byte KEYWORD4 = 11;
077: public static final byte LABEL = 12;
078: public static final byte LITERAL1 = 13;
079: public static final byte LITERAL2 = 14;
080: public static final byte LITERAL3 = 15;
081: public static final byte LITERAL4 = 16;
082: public static final byte MARKUP = 17;
083: public static final byte OPERATOR = 18;
084: //}}}
085:
086: public static final byte ID_COUNT = 19;
087:
088: // Special:
089: public static final byte END = 127;
090:
091: //{{{ Instance variables
092: /**
093: * The id of this token.
094: */
095: public byte id;
096:
097: /**
098: * The start offset of this token.
099: */
100: public int offset;
101:
102: /**
103: * The length of this token.
104: */
105: public int length;
106:
107: /**
108: * The rule set of this token.
109: */
110: public ParserRuleSet rules;
111:
112: /**
113: * The next token in the linked list.
114: */
115: public Token next;
116:
117: //}}}
118:
119: //{{{ Token constructor
120: /**
121: * Creates a new token.
122: * @param id The id of the token
123: * @param offset The start offset of the token
124: * @param length The length of the token
125: * @param rules The parser rule set that generated this token
126: */
127: public Token(byte id, int offset, int length, ParserRuleSet rules) {
128: this .id = id;
129: this .offset = offset;
130: this .length = length;
131: this .rules = rules;
132: } //}}}
133:
134: //{{{ toString() method
135: /**
136: * Returns a string representation of this token.
137: */
138: public String toString() {
139: return "[id=" + id + ",offset=" + offset + ",length=" + length
140: + "]";
141: } //}}}
142: }
|