| 1 |
769 |
jeremybenn |
/* gnu/regexp/REToken.java
|
| 2 |
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
| 7 |
|
|
it under the terms of the GNU General Public License as published by
|
| 8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
| 9 |
|
|
any later version.
|
| 10 |
|
|
|
| 11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
| 12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 14 |
|
|
General Public License for more details.
|
| 15 |
|
|
|
| 16 |
|
|
You should have received a copy of the GNU General Public License
|
| 17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
| 18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
| 19 |
|
|
02110-1301 USA.
|
| 20 |
|
|
|
| 21 |
|
|
Linking this library statically or dynamically with other modules is
|
| 22 |
|
|
making a combined work based on this library. Thus, the terms and
|
| 23 |
|
|
conditions of the GNU General Public License cover the whole
|
| 24 |
|
|
combination.
|
| 25 |
|
|
|
| 26 |
|
|
As a special exception, the copyright holders of this library give you
|
| 27 |
|
|
permission to link this library with independent modules to produce an
|
| 28 |
|
|
executable, regardless of the license terms of these independent
|
| 29 |
|
|
modules, and to copy and distribute the resulting executable under
|
| 30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
| 31 |
|
|
independent module, the terms and conditions of the license of that
|
| 32 |
|
|
module. An independent module is a module which is not derived from
|
| 33 |
|
|
or based on this library. If you modify this library, you may extend
|
| 34 |
|
|
this exception to your version of the library, but you are not
|
| 35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
| 36 |
|
|
exception statement from your version. */
|
| 37 |
|
|
|
| 38 |
|
|
package gnu.java.util.regex;
|
| 39 |
|
|
|
| 40 |
|
|
import gnu.java.lang.CPStringBuilder;
|
| 41 |
|
|
|
| 42 |
|
|
import java.io.Serializable;
|
| 43 |
|
|
|
| 44 |
|
|
abstract class REToken implements Serializable, Cloneable
|
| 45 |
|
|
{
|
| 46 |
|
|
|
| 47 |
|
|
protected REToken next = null;
|
| 48 |
|
|
protected REToken uncle = null;
|
| 49 |
|
|
protected int subIndex;
|
| 50 |
|
|
protected boolean unicodeAware = true;
|
| 51 |
|
|
|
| 52 |
|
|
public Object clone ()
|
| 53 |
|
|
{
|
| 54 |
|
|
try
|
| 55 |
|
|
{
|
| 56 |
|
|
REToken copy = (REToken) super.clone ();
|
| 57 |
|
|
return copy;
|
| 58 |
|
|
}
|
| 59 |
|
|
catch (CloneNotSupportedException e)
|
| 60 |
|
|
{
|
| 61 |
|
|
throw new Error (); // doesn't happen
|
| 62 |
|
|
}
|
| 63 |
|
|
}
|
| 64 |
|
|
|
| 65 |
|
|
protected REToken (int subIndex)
|
| 66 |
|
|
{
|
| 67 |
|
|
this.subIndex = subIndex;
|
| 68 |
|
|
}
|
| 69 |
|
|
|
| 70 |
|
|
int getMinimumLength ()
|
| 71 |
|
|
{
|
| 72 |
|
|
return 0;
|
| 73 |
|
|
}
|
| 74 |
|
|
|
| 75 |
|
|
int getMaximumLength ()
|
| 76 |
|
|
{
|
| 77 |
|
|
return Integer.MAX_VALUE;
|
| 78 |
|
|
}
|
| 79 |
|
|
|
| 80 |
|
|
void setUncle (REToken anUncle)
|
| 81 |
|
|
{
|
| 82 |
|
|
uncle = anUncle;
|
| 83 |
|
|
}
|
| 84 |
|
|
|
| 85 |
|
|
/** Returns true if the match succeeded, false if it failed. */
|
| 86 |
|
|
boolean match (CharIndexed input, REMatch mymatch)
|
| 87 |
|
|
{
|
| 88 |
|
|
return match (input, mymatch, false);
|
| 89 |
|
|
}
|
| 90 |
|
|
boolean matchFake (CharIndexed input, REMatch mymatch)
|
| 91 |
|
|
{
|
| 92 |
|
|
return match (input, mymatch, true);
|
| 93 |
|
|
}
|
| 94 |
|
|
|
| 95 |
|
|
private boolean match (CharIndexed input, REMatch mymatch, boolean fake)
|
| 96 |
|
|
{
|
| 97 |
|
|
if (!fake)
|
| 98 |
|
|
{
|
| 99 |
|
|
setHitEnd (input, mymatch);
|
| 100 |
|
|
}
|
| 101 |
|
|
REMatch m = matchThis (input, mymatch);
|
| 102 |
|
|
if (m == null)
|
| 103 |
|
|
return false;
|
| 104 |
|
|
if (next (input, m))
|
| 105 |
|
|
{
|
| 106 |
|
|
mymatch.assignFrom (m);
|
| 107 |
|
|
return true;
|
| 108 |
|
|
}
|
| 109 |
|
|
return false;
|
| 110 |
|
|
}
|
| 111 |
|
|
|
| 112 |
|
|
/** Sets whether the matching occurs at the end of input */
|
| 113 |
|
|
void setHitEnd (CharIndexed input, REMatch mymatch)
|
| 114 |
|
|
{
|
| 115 |
|
|
input.setHitEnd (mymatch);
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
/** Returns true if the match succeeded, false if it failed.
|
| 119 |
|
|
* The matching is done against this REToken only. Chained
|
| 120 |
|
|
* tokens are not checked.
|
| 121 |
|
|
* This method is used to define the default match method.
|
| 122 |
|
|
* Simple subclasses of REToken, for example, such that
|
| 123 |
|
|
* matches only one character, should implement this method.
|
| 124 |
|
|
* Then the default match method will work. But complicated
|
| 125 |
|
|
* subclasses of REToken, which needs a special match method,
|
| 126 |
|
|
* do not have to implement this method.
|
| 127 |
|
|
*/
|
| 128 |
|
|
REMatch matchThis (CharIndexed input, REMatch mymatch)
|
| 129 |
|
|
{
|
| 130 |
|
|
throw new
|
| 131 |
|
|
UnsupportedOperationException
|
| 132 |
|
|
("This REToken does not have a matchThis method");
|
| 133 |
|
|
}
|
| 134 |
|
|
|
| 135 |
|
|
/** Returns true if the rest of the tokens match, false if they fail. */
|
| 136 |
|
|
protected boolean next (CharIndexed input, REMatch mymatch)
|
| 137 |
|
|
{
|
| 138 |
|
|
REToken nextToken = getNext ();
|
| 139 |
|
|
if (nextToken == null)
|
| 140 |
|
|
return true;
|
| 141 |
|
|
return nextToken.match (input, mymatch);
|
| 142 |
|
|
}
|
| 143 |
|
|
|
| 144 |
|
|
/** Returns the next REToken chained to this REToken. */
|
| 145 |
|
|
REToken getNext ()
|
| 146 |
|
|
{
|
| 147 |
|
|
return (next != null ? next : uncle);
|
| 148 |
|
|
}
|
| 149 |
|
|
|
| 150 |
|
|
/** Finds a match at the position specified by the given REMatch.
|
| 151 |
|
|
* If necessary, adds a BacktrackStack.Backtrack object to backtrackStack
|
| 152 |
|
|
* of the REmatch found this time so that another possible match
|
| 153 |
|
|
* may be found when backtrack is called.
|
| 154 |
|
|
* By default, nothing is added to the backtrackStack.
|
| 155 |
|
|
* @param input Input character sequence.
|
| 156 |
|
|
* @param mymatch Position at which a match should be found
|
| 157 |
|
|
* @return REMatch object if a match was found, null otherwise.
|
| 158 |
|
|
*/
|
| 159 |
|
|
REMatch findMatch (CharIndexed input, REMatch mymatch)
|
| 160 |
|
|
{
|
| 161 |
|
|
boolean b = match (input, mymatch);
|
| 162 |
|
|
if (b)
|
| 163 |
|
|
return mymatch;
|
| 164 |
|
|
return null;
|
| 165 |
|
|
}
|
| 166 |
|
|
|
| 167 |
|
|
boolean returnsFixedLengthMatches ()
|
| 168 |
|
|
{
|
| 169 |
|
|
return false;
|
| 170 |
|
|
}
|
| 171 |
|
|
|
| 172 |
|
|
int findFixedLengthMatches (CharIndexed input, REMatch mymatch, int max)
|
| 173 |
|
|
{
|
| 174 |
|
|
throw new
|
| 175 |
|
|
UnsupportedOperationException
|
| 176 |
|
|
("This token does not support findFixedLengthMatches");
|
| 177 |
|
|
}
|
| 178 |
|
|
|
| 179 |
|
|
/**
|
| 180 |
|
|
* Backtrack to another possibility.
|
| 181 |
|
|
* Ordinary REToken cannot do anything if this method is called.
|
| 182 |
|
|
*/
|
| 183 |
|
|
REMatch backtrack (CharIndexed input, REMatch mymatch, Object param)
|
| 184 |
|
|
{
|
| 185 |
|
|
throw new IllegalStateException ("This token cannot be backtracked to");
|
| 186 |
|
|
}
|
| 187 |
|
|
|
| 188 |
|
|
boolean chain (REToken token)
|
| 189 |
|
|
{
|
| 190 |
|
|
next = token;
|
| 191 |
|
|
return true; // Token was accepted
|
| 192 |
|
|
}
|
| 193 |
|
|
|
| 194 |
|
|
abstract void dump (CPStringBuilder os);
|
| 195 |
|
|
|
| 196 |
|
|
void dumpAll (CPStringBuilder os)
|
| 197 |
|
|
{
|
| 198 |
|
|
dump (os);
|
| 199 |
|
|
if (next != null)
|
| 200 |
|
|
next.dumpAll (os);
|
| 201 |
|
|
}
|
| 202 |
|
|
|
| 203 |
|
|
public String toString ()
|
| 204 |
|
|
{
|
| 205 |
|
|
CPStringBuilder os = new CPStringBuilder ();
|
| 206 |
|
|
dump (os);
|
| 207 |
|
|
return os.toString ();
|
| 208 |
|
|
}
|
| 209 |
|
|
|
| 210 |
|
|
/**
|
| 211 |
|
|
* Converts the character argument to lowercase.
|
| 212 |
|
|
* @param ch the character to be converted.
|
| 213 |
|
|
* @param unicodeAware If true, use java.lang.Character#toLowerCase;
|
| 214 |
|
|
* otherwise, only US-ASCII charactes can be converted.
|
| 215 |
|
|
* @return the lowercase equivalent of the character, if any;
|
| 216 |
|
|
* otherwise, the character itself.
|
| 217 |
|
|
*/
|
| 218 |
|
|
public static char toLowerCase (char ch, boolean unicodeAware)
|
| 219 |
|
|
{
|
| 220 |
|
|
if (unicodeAware)
|
| 221 |
|
|
return Character.toLowerCase (ch);
|
| 222 |
|
|
if (ch >= 'A' && ch <= 'Z')
|
| 223 |
|
|
return (char) (ch + 'a' - 'A');
|
| 224 |
|
|
return ch;
|
| 225 |
|
|
}
|
| 226 |
|
|
|
| 227 |
|
|
/**
|
| 228 |
|
|
* Converts the character argument to uppercase.
|
| 229 |
|
|
* @param ch the character to be converted.
|
| 230 |
|
|
* @param unicodeAware If true, use java.lang.Character#toUpperCase;
|
| 231 |
|
|
* otherwise, only US-ASCII charactes can be converted.
|
| 232 |
|
|
* @return the uppercase equivalent of the character, if any;
|
| 233 |
|
|
* otherwise, the character itself.
|
| 234 |
|
|
*/
|
| 235 |
|
|
public static char toUpperCase (char ch, boolean unicodeAware)
|
| 236 |
|
|
{
|
| 237 |
|
|
if (unicodeAware)
|
| 238 |
|
|
return Character.toUpperCase (ch);
|
| 239 |
|
|
if (ch >= 'a' && ch <= 'z')
|
| 240 |
|
|
return (char) (ch + 'A' - 'a');
|
| 241 |
|
|
return ch;
|
| 242 |
|
|
}
|
| 243 |
|
|
|
| 244 |
|
|
}
|