1 21 22 package net.percederberg.grammatica.parser.re; 23 24 import java.io.IOException ; 25 import java.io.Reader ; 26 import java.io.StringReader ; 27 28 import net.percederberg.grammatica.parser.LookAheadReader; 29 30 40 public class Matcher { 41 42 45 private Element element; 46 47 50 private LookAheadReader input; 51 52 55 private boolean ignoreCase; 56 57 60 private int start; 61 62 65 private int length; 66 67 71 private boolean endOfString; 72 73 80 Matcher(Element e, LookAheadReader input, boolean ignoreCase) { 81 this.element = e; 82 this.input = input; 83 this.ignoreCase = ignoreCase; 84 this.start = 0; 85 reset(); 86 } 87 88 96 public boolean isCaseInsensitive() { 97 return ignoreCase; 98 } 99 100 105 public void reset() { 106 length = -1; 107 endOfString = false; 108 } 109 110 118 public void reset(String str) { 119 reset(new StringReader (str)); 120 } 121 122 130 public void reset(StringBuffer str) { 131 reset(new StringReader (str.toString())); 132 } 133 134 143 public void reset(Reader input) { 144 if (input instanceof LookAheadReader) { 145 reset((LookAheadReader) input); 146 } else { 147 reset(new LookAheadReader(input)); 148 } 149 } 150 151 160 private void reset(LookAheadReader input) { 161 this.input = input; 162 reset(); 163 } 164 165 171 public int start() { 172 return start; 173 } 174 175 183 public int end() { 184 if (length > 0) { 185 return start + length; 186 } else { 187 return start; 188 } 189 } 190 191 197 public int length() { 198 return length; 199 } 200 201 209 public boolean hasReadEndOfString() { 210 return endOfString; 211 } 212 213 217 void setReadEndOfString() { 218 endOfString = true; 219 } 220 221 231 public boolean matchFromBeginning() throws IOException { 232 return matchFrom(0); 233 } 234 235 247 public boolean matchFrom(int pos) throws IOException { 248 reset(); 249 start = pos; 250 length = element.match(this, input, start, 0); 251 return length >= 0; 252 } 253 254 260 public String toString() { 261 if (length <= 0) { 262 return ""; 263 } else { 264 try { 265 return input.peekString(start, length); 266 } catch (IOException ignore) { 267 return ""; 268 } 269 } 270 } 271 } 272 | Popular Tags |