1 33 34 package edu.rice.cs.drjava.model.definitions.reducedmodel; 35 36 43 class Brace extends ReducedToken implements ReducedModelStates { 44 52 public static final String [] braces = { 53 "{", "}", "(", ")", "[", "]", "/*", "*/", "//", "\n", "/", "*", "\"", "\"", 54 "'", "'", "\\\\", "\\", "\\'", "\\\"", "" 55 }; 56 public static final String BLK_CMT_BEG = "/*"; 57 public static final String BLK_CMT_END = "*/"; 58 public static final String EOLN = "\n"; 59 public static final String LINE_CMT = "//"; 60 public static final String SINGLE_QUOTE = "'"; 61 public static final String DOUBLE_QUOTE = "\""; 62 public static final String STAR = "*"; 63 public static final String SLASH = "/"; 64 65 66 protected int _type; 67 68 75 public static Brace MakeBrace(String type, ReducedModelState state) { 76 int index = findBrace(type); 77 if (index == braces.length) { 78 throw new BraceException("Invalid brace type \"" + type + "\""); 79 } 80 else { 81 return new Brace(index, state); 82 } 83 } 84 85 90 private Brace(int type, ReducedModelState state) { 91 super(state); 92 _type = type; 93 } 94 95 99 public String getType() { 100 return (_type == braces.length) ? "!" : braces[_type]; 101 } 102 103 106 public int getSize() { 107 return getType().length(); 108 } 109 110 115 public String toString() { 116 final StringBuilder val = new StringBuilder (); 118 int i; 119 for (i = 0; i < getSize(); i++) { 120 val.append(' '); 121 val.append(getType().charAt(i)); 122 } 123 return val.toString(); 124 } 125 126 129 public void flip() { 130 if (isOpen()) _type += 1; 131 else if (_type < braces.length - 1) _type -= 1; 132 } 133 134 138 public boolean isOpen() { 139 return (((_type%2) == 0) && (_type < braces.length - 1)); 140 } 141 142 145 public boolean isOpenBrace() { 146 return ((_type == 0) || (_type == 2) || (_type == 4)); 147 } 148 149 152 public boolean isClosedBrace() { 153 return ((_type == 1) || (_type == 3) || (_type == 5)); 154 } 155 156 160 public boolean isClosed() { 161 return !isOpen(); 162 } 163 164 168 public void setType(String type) { 169 int index = findBrace(type); 170 if (index == braces.length) { 171 throw new BraceException("Invalid brace type \"" + type + "\""); 172 } 173 else { 174 _type = index; 175 } 176 } 177 178 185 protected static int findBrace(String type) { 186 int i; 187 for (i = 0; i < braces.length; i++) { 188 if (type.equals(braces[i])) 189 break; 190 } 191 return i; 192 } 193 194 199 public boolean isMatch(ReducedToken other) { 200 if (this.getType().equals("")) { 201 return false; 202 } 203 int off = (this.isOpen()) ? 1 : -1; 204 return (braces[_type + off].equals(other.getType())); 205 } 206 207 210 public boolean isDoubleQuote() { 211 return this.getType().equals(DOUBLE_QUOTE); 212 } 213 214 public boolean isSingleQuote() { 215 return this.getType().equals(SINGLE_QUOTE); 216 } 217 218 219 222 public boolean isLineComment() { 223 return this.getType().equals(LINE_CMT); 224 } 225 226 229 public boolean isBlockCommentStart() { 230 return this.getType().equals(BLK_CMT_BEG); 231 } 232 233 236 public boolean isBlockCommentEnd() { 237 return this.getType().equals(BLK_CMT_END); 238 } 239 240 242 public boolean isNewline() { 243 return this.getType().equals(EOLN); 244 } 245 246 248 public boolean isMultipleCharBrace() { 249 return isLineComment() || isBlockCommentStart() || 250 isBlockCommentEnd() || isDoubleEscapeSequence(); 251 } 252 253 256 public boolean isDoubleEscapeSequence() { 257 return isDoubleEscape() || isEscapedDoubleQuote() || 258 isEscapedSingleQuote(); 259 } 260 261 264 public boolean isDoubleEscape() { 265 return this.getType().equals("\\\\"); 266 } 267 268 271 public boolean isEscapedDoubleQuote() { 272 return this.getType().equals("\\\""); 273 } 274 275 278 public boolean isEscapedSingleQuote() { 279 return this.getType().equals("\\'"); 280 } 281 282 287 public boolean isGap() { 288 return false; 289 } 290 291 294 public boolean isSlash() { 295 return this.getType().equals(SLASH); 296 } 297 298 301 public boolean isStar() { 302 return this.getType().equals(STAR); 303 } 304 305 309 public void grow(int delta) { 310 throw new RuntimeException ("Braces can't grow."); 311 } 312 313 317 public void shrink(int delta) { 318 throw new RuntimeException ("Braces can't shrink."); 319 } 320 321 } 322 323 324 327 class BraceException extends RuntimeException { 328 329 333 public BraceException(String s) { 334 super(s); 335 } 336 } 337 338 339 340 | Popular Tags |