1 11 package com.ibm.icu.impl.data; 12 13 import java.io.IOException ; 14 15 import com.ibm.icu.impl.UCharacterProperty; 16 import com.ibm.icu.impl.Utility; 17 import com.ibm.icu.text.UTF16; 18 19 26 public class TokenIterator { 27 28 private ResourceReader reader; 29 private String line; 30 private StringBuffer buf; 31 private boolean done; 32 private int pos; 33 private int lastpos; 34 35 41 public TokenIterator(ResourceReader r) { 42 reader = r; 43 line = null; 44 done = false; 45 buf = new StringBuffer (); 46 pos = lastpos = -1; 47 } 48 49 53 public String next() throws IOException { 54 if (done) { 55 return null; 56 } 57 for (;;) { 58 if (line == null) { 59 line = reader.readLineSkippingComments(); 60 if (line == null) { 61 done = true; 62 return null; 63 } 64 pos = 0; 65 } 66 buf.setLength(0); 67 lastpos = pos; 68 pos = nextToken(pos); 69 if (pos < 0) { 70 line = null; 71 continue; 72 } 73 return buf.toString(); 74 } 75 } 76 77 83 public int getLineNumber() { 84 return reader.getLineNumber(); 85 } 86 87 91 public String describePosition() { 92 return reader.describePosition() + ':' + (lastpos+1); 93 } 94 95 107 private int nextToken(int pos) { 108 pos = Utility.skipWhitespace(line, pos); 109 if (pos == line.length()) { 110 return -1; 111 } 112 int startpos = pos; 113 char c = line.charAt(pos++); 114 char quote = 0; 115 switch (c) { 116 case '"': 117 case '\'': 118 quote = c; 119 break; 120 case '#': 121 return -1; 122 default: 123 buf.append(c); 124 break; 125 } 126 int[] posref = null; 127 while (pos < line.length()) { 128 c = line.charAt(pos); if (c == '\\') { 130 if (posref == null) { 131 posref = new int[1]; 132 } 133 posref[0] = pos+1; 134 int c32 = Utility.unescapeAt(line, posref); 135 if (c32 < 0) { 136 throw new RuntimeException ("Invalid escape at " + 137 reader.describePosition() + ':' + 138 pos); 139 } 140 UTF16.append(buf, c32); 141 pos = posref[0]; 142 } else if ((quote != 0 && c == quote) || 143 (quote == 0 && UCharacterProperty.isRuleWhiteSpace(c))) { 144 return ++pos; 145 } else if (quote == 0 && c == '#') { 146 return pos; } else { 148 buf.append(c); 149 ++pos; 150 } 151 } 152 if (quote != 0) { 153 throw new RuntimeException ("Unterminated quote at " + 154 reader.describePosition() + ':' + 155 startpos); 156 } 157 return pos; 158 } 159 } 160 | Popular Tags |