001 package com.saelist.stx.parser;
002
003 import com.saelist.util.Strings;
004 import org.apache.log4j.*;
005
006 /** Tokens for free form structured text.
007 */
008 public class Token {
009
010 public static Logger logger = Logger.getLogger("com.saelist.stx.parser.Token");
011
012 private int line;
013 private int column;
014 private int type;
015 private String value;
016
017 public static final int TEXT = 1;
018 public static final int SPACES = 2;
019 public static final int SPACE = ' ';
020 public static final int NEWLINE = '\n';
021 public static final int CR = '\r';
022 public static final int TAB = '\t';
023 public static final int LBR = '[';
024 public static final int RBR = ']';
025 public static final int SQ = '\'';
026 public static final int DQ = '"';
027
028
029 public Token(int line, int column, int type, String value) {
030 this.line = line;
031 this.column = column;
032 this.type = type;
033 this.value = value;
034 // logger.info(toString());
035 }
036
037 public int getLine() {
038 return line;
039 }
040
041 public int getColumn() {
042 return column;
043 }
044
045 public int getType() {
046 return type;
047 }
048
049 public String getValue() {
050 return value;
051 }
052
053 public String toString() {
054 return Strings.format(
055 "line[ % ] column[ % ] type[ % ] value[ '%' ]", "" + line, "" + column, getTypeName(), value);
056 }
057
058 public String getTypeName() {
059 switch(type) {
060 case TEXT : return "text";
061 case SPACES : return "spaces";
062 case SPACE : return "space";
063 case NEWLINE : return "newline";
064 case CR : return "cr";
065 case TAB : return "tab";
066 case LBR : return "lbr";
067 case RBR : return "rbr";
068 case SQ : return"sq";
069 case DQ : return "dq";
070 }
071 throw new RuntimeException("illegal token type " + type);
072 }
073
074 public boolean equals(Object object) {
075 if( ! (object instanceof Token))
076 return false;
077 Token token = (Token) object;
078 return
079 line == token.getLine() &&
080 column == token.getColumn() &&
081 type == token.getType() &&
082 value.equals(token.getValue());
083 }
084
085 }