1 package fri.patterns.interpreter.parsergenerator; 2 3 16 17 public class Token 18 { 19 20 public static final String EPSILON = "\"EoI\""; 21 22 23 public static final char STRING_QUOTE = '"'; 24 25 26 public static final char CHAR_QUOTE = '\''; 27 28 29 public static final char COMMAND_QUOTE = '`'; 30 31 32 public static final String UPTO = ".."; 33 34 35 public static final String BUTNOT = "-"; 36 37 38 public static final String TOKEN = "token"; 39 40 41 public static final String IGNORED = "ignored"; 42 43 44 public static final String ARTIFICIAL_NONTERMINAL_START_CHARACTER = "_"; 45 46 47 public final String symbol; 48 49 public final Object text; 50 51 public final Range range; 52 53 54 55 public static class Address implements Comparable 56 { 57 58 public final int line; 59 60 public final int column; 61 62 public final int offset; 63 64 public Address() { 65 this(1, 0, 0); 66 } 67 public Address(int line, int column, int offset) { 68 this.line = line; 69 this.column = column; 70 this.offset = offset; 71 } 72 public String toString() { 73 return line+"/"+column; 74 } 75 public boolean equals(Object o) { 76 return offset == ((Address) o).offset; 77 } 78 public int hashCode() { 79 return offset; 80 } 81 public int compareTo(Object o) { 82 return offset - ((Address) o).offset; 83 } 84 } 85 86 87 public static class Range implements Comparable 88 { 89 90 public final Address start; 91 92 public final Address end; 93 94 public Range(Address start, Address end) { 95 this.start = start != null ? start : new Address(); 96 this.end = end != null ? end : new Address(); 97 } 98 public boolean equals(Object o) { 99 return start.equals(((Range) o).start) && end.equals(((Range) o).end); 100 } 101 public int hashCode() { 102 return start.hashCode() + end.hashCode(); 103 } 104 public String toString() { 105 return start+"-"+end; 106 } 107 public int compareTo(Object o) { 108 return start.compareTo(((Range) o).start) + end.compareTo(((Range) o).end); 109 } 110 } 111 112 113 public Token(String symbol, Object text, Range range) { 114 this.symbol = symbol; 115 this.text = text; 116 this.range = range; 117 } 118 119 123 public static boolean isEpsilon(Token token) { 124 return isEpsilon(token.symbol); 125 } 126 127 131 public static boolean isEpsilon(String symbol) { 132 return symbol != null && symbol == EPSILON; 133 } 134 135 139 public static boolean isTerminal(String symbol) { 140 char c = symbol.charAt(0); 141 return 142 c == STRING_QUOTE || 143 c == CHAR_QUOTE || 144 c == COMMAND_QUOTE || 145 Character.isDigit(c) || 146 Token.isEpsilon(symbol); 147 } 148 149 } | Popular Tags |