1 11 package com.ibm.icu.impl; 12 13 import java.text.ParsePosition ; 14 import com.ibm.icu.text.SymbolTable; 15 import com.ibm.icu.text.UTF16; 16 17 24 public class RuleCharacterIterator { 25 26 34 37 private String text; 38 39 42 private ParsePosition pos; 43 44 47 private SymbolTable sym; 48 49 52 private char[] buf; 53 54 57 private int bufPos; 58 59 62 private boolean isEscaped; 63 64 67 public static final int DONE = -1; 68 69 74 public static final int PARSE_VARIABLES = 1; 75 76 81 public static final int PARSE_ESCAPES = 2; 82 83 89 public static final int SKIP_WHITESPACE = 4; 90 91 102 public RuleCharacterIterator(String text, SymbolTable sym, 103 ParsePosition pos) { 104 if (text == null || pos.getIndex() > text.length()) { 105 throw new IllegalArgumentException (); 106 } 107 this.text = text; 108 this.sym = sym; 109 this.pos = pos; 110 buf = null; 111 } 112 113 116 public boolean atEnd() { 117 return buf == null && pos.getIndex() == text.length(); 118 } 119 120 128 public int next(int options) { 129 int c = DONE; 130 isEscaped = false; 131 132 for (;;) { 133 c = _current(); 134 _advance(UTF16.getCharCount(c)); 135 136 if (c == SymbolTable.SYMBOL_REF && buf == null && 137 (options & PARSE_VARIABLES) != 0 && sym != null) { 138 String name = sym.parseReference(text, pos, text.length()); 139 if (name == null) { 142 break; 143 } 144 bufPos = 0; 145 buf = sym.lookup(name); 146 if (buf == null) { 147 throw new IllegalArgumentException ( 148 "Undefined variable: " + name); 149 } 150 if (buf.length == 0) { 152 buf = null; 153 } 154 continue; 155 } 156 157 if ((options & SKIP_WHITESPACE) != 0 && 158 UCharacterProperty.isRuleWhiteSpace(c)) { 159 continue; 160 } 161 162 if (c == '\\' && (options & PARSE_ESCAPES) != 0) { 163 int offset[] = new int[] { 0 }; 164 c = Utility.unescapeAt(lookahead(), offset); 165 jumpahead(offset[0]); 166 isEscaped = true; 167 if (c < 0) { 168 throw new IllegalArgumentException ("Invalid escape"); 169 } 170 } 171 172 break; 173 } 174 175 return c; 176 } 177 178 184 public boolean isEscaped() { 185 return isEscaped; 186 } 187 188 191 public boolean inVariable() { 192 return buf != null; 193 } 194 195 214 public Object getPos(Object p) { 215 if (p == null) { 216 return new Object [] {buf, new int[] {pos.getIndex(), bufPos}}; 217 } 218 Object [] a = (Object []) p; 219 a[0] = buf; 220 int[] v = (int[]) a[1]; 221 v[0] = pos.getIndex(); 222 v[1] = bufPos; 223 return p; 224 } 225 226 231 public void setPos(Object p) { 232 Object [] a = (Object []) p; 233 buf = (char[]) a[0]; 234 int[] v = (int[]) a[1]; 235 pos.setIndex(v[0]); 236 bufPos = v[1]; 237 } 238 239 247 public void skipIgnored(int options) { 248 if ((options & SKIP_WHITESPACE) != 0) { 249 for (;;) { 250 int a = _current(); 251 if (!UCharacterProperty.isRuleWhiteSpace(a)) break; 252 _advance(UTF16.getCharCount(a)); 253 } 254 } 255 } 256 257 269 public String lookahead() { 270 if (buf != null) { 271 return new String (buf, bufPos, buf.length - bufPos); 272 } else { 273 return text.substring(pos.getIndex()); 274 } 275 } 276 277 282 public void jumpahead(int count) { 283 if (count < 0) { 284 throw new IllegalArgumentException (); 285 } 286 if (buf != null) { 287 bufPos += count; 288 if (bufPos > buf.length) { 289 throw new IllegalArgumentException (); 290 } 291 if (bufPos == buf.length) { 292 buf = null; 293 } 294 } else { 295 int i = pos.getIndex() + count; 296 pos.setIndex(i); 297 if (i > text.length()) { 298 throw new IllegalArgumentException (); 299 } 300 } 301 } 302 303 309 public String toString() { 310 int b = pos.getIndex(); 311 return text.substring(0, b) + '|' + text.substring(b); 312 } 313 314 319 private int _current() { 320 if (buf != null) { 321 return UTF16.charAt(buf, 0, buf.length, bufPos); 322 } else { 323 int i = pos.getIndex(); 324 return (i < text.length()) ? UTF16.charAt(text, i) : DONE; 325 } 326 } 327 328 332 private void _advance(int count) { 333 if (buf != null) { 334 bufPos += count; 335 if (bufPos == buf.length) { 336 buf = null; 337 } 338 } else { 339 pos.setIndex(pos.getIndex() + count); 340 if (pos.getIndex() > text.length()) { 341 pos.setIndex(text.length()); 342 } 343 } 344 } 345 } | Popular Tags |