2 * gnu/regexp/RETokenOneOf.java
3 * Copyright (C) 1998-2001 Wes Biggs
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2.1 of the License, or
8 * (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 import java.util.Vector;
23 final class RETokenOneOf extends REToken {
24 private Vector options;
25 private boolean negative;
27 // This constructor is used for convenience when we know the set beforehand,
28 // e.g. \d --> new RETokenOneOf("0123456789",false, ..)
29 // \D --> new RETokenOneOf("0123456789",true, ..)
31 RETokenOneOf(int subIndex, String optionsStr, boolean negative, boolean insens) {
33 options = new Vector();
34 this.negative = negative;
35 for (int i = 0; i < optionsStr.length(); i++)
36 options.addElement(new RETokenChar(subIndex,optionsStr.charAt(i),insens));
39 RETokenOneOf(int subIndex, Vector options, boolean negative) {
41 this.options = options;
42 this.negative = negative;
45 int getMinimumLength() {
46 int min = Integer.MAX_VALUE;
48 for (int i=0; i < options.size(); i++) {
49 if ((x = ((REToken) options.elementAt(i)).getMinimumLength()) < min)
55 boolean match(CharIndexed input, REMatch mymatch) {
56 if (negative && (input.charAt(mymatch.index) == CharIndexed.OUT_OF_BOUNDS))
59 REMatch newMatch = null;
63 for (int i=0; i < options.size(); i++) {
64 tk = (REToken) options.elementAt(i);
65 REMatch tryMatch = (REMatch) mymatch.clone();
66 if (tk.match(input, tryMatch)) { // match was successful
67 if (negative) return false;
69 if (next(input, tryMatch)) {
70 // Add tryMatch to list of possibilities.
82 if (newMatch != null) {
86 // set contents of mymatch equal to newMatch
88 // try each one that matched
89 mymatch.assignFrom(newMatch);
95 return next(input, mymatch);
101 // index+1 works for [^abc] lists, not for generic lookahead (--> index)
104 void dump(StringBuffer os) {
105 os.append(negative ? "[^" : "(?:");
106 for (int i = 0; i < options.size(); i++) {
107 if (!negative && (i > 0)) os.append('|');
108 ((REToken) options.elementAt(i)).dumpAll(os);
110 os.append(negative ? ']' : ')');