libjava/
[official-gcc.git] / libjava / classpath / gnu / java / util / regex / REToken.java
blobaddc62225ebdb5aaf205bc9e59fc4e435005f604
1 /* gnu/regexp/REToken.java
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package gnu.java.util.regex;
39 import java.io.Serializable;
41 abstract class REToken implements Serializable, Cloneable {
43 protected REToken next = null;
44 protected REToken uncle = null;
45 protected int subIndex;
46 protected boolean unicodeAware = true;
48 public Object clone() {
49 try {
50 REToken copy = (REToken) super.clone();
51 return copy;
52 } catch (CloneNotSupportedException e) {
53 throw new Error(); // doesn't happen
57 protected REToken(int subIndex) {
58 this.subIndex = subIndex;
61 int getMinimumLength() {
62 return 0;
65 int getMaximumLength() {
66 return Integer.MAX_VALUE;
69 void setUncle(REToken anUncle) {
70 uncle = anUncle;
73 /** Returns true if the match succeeded, false if it failed. */
74 boolean match(CharIndexed input, REMatch mymatch) {
75 return match(input, mymatch, false);
77 boolean matchFake(CharIndexed input, REMatch mymatch) {
78 return match(input, mymatch, true);
81 private boolean match(CharIndexed input, REMatch mymatch, boolean fake) {
82 if (!fake) {
83 setHitEnd(input, mymatch);
85 REMatch m = matchThis(input, mymatch);
86 if (m == null) return false;
87 if (next(input, m)) {
88 mymatch.assignFrom(m);
89 return true;
91 return false;
94 /** Sets whether the matching occurs at the end of input */
95 void setHitEnd(CharIndexed input, REMatch mymatch) {
96 input.setHitEnd(mymatch);
99 /** Returns true if the match succeeded, false if it failed.
100 * The matching is done against this REToken only. Chained
101 * tokens are not checked.
102 * This method is used to define the default match method.
103 * Simple subclasses of REToken, for example, such that
104 * matches only one character, should implement this method.
105 * Then the default match method will work. But complicated
106 * subclasses of REToken, which needs a special match method,
107 * do not have to implement this method.
109 REMatch matchThis(CharIndexed input, REMatch mymatch) {
110 throw new UnsupportedOperationException(
111 "This REToken does not have a matchThis method");
114 /** Returns true if the rest of the tokens match, false if they fail. */
115 protected boolean next(CharIndexed input, REMatch mymatch) {
116 REToken nextToken = getNext();
117 if (nextToken == null) return true;
118 return nextToken.match(input, mymatch);
121 /** Returns the next REToken chained to this REToken. */
122 REToken getNext() {
123 return (next != null ? next : uncle);
126 /** Finds a match at the position specified by the given REMatch.
127 * If necessary, adds a BacktrackStack.Backtrack object to backtrackStack
128 * of the REmatch found this time so that another possible match
129 * may be found when backtrack is called.
130 * By default, nothing is added to the backtrackStack.
131 * @param input Input character sequence.
132 * @param mymatch Position at which a match should be found
133 * @return REMatch object if a match was found, null otherwise.
135 REMatch findMatch(CharIndexed input, REMatch mymatch) {
136 boolean b = match(input, mymatch);
137 if (b) return mymatch;
138 return null;
141 boolean returnsFixedLengthMatches() {
142 return false;
145 int findFixedLengthMatches(CharIndexed input, REMatch mymatch, int max) {
146 throw new UnsupportedOperationException(
147 "This token does not support findFixedLengthMatches");
151 * Backtrack to another possibility.
152 * Ordinary REToken cannot do anything if this method is called.
154 REMatch backtrack(CharIndexed input, REMatch mymatch, Object param) {
155 throw new IllegalStateException("This token cannot be backtracked to");
158 boolean chain(REToken token) {
159 next = token;
160 return true; // Token was accepted
163 abstract void dump(StringBuffer os);
165 void dumpAll(StringBuffer os) {
166 dump(os);
167 if (next != null) next.dumpAll(os);
170 public String toString() {
171 StringBuffer os = new StringBuffer();
172 dump(os);
173 return os.toString();
177 * Converts the character argument to lowercase.
178 * @param ch the character to be converted.
179 * @param unicodeAware If true, use java.lang.Character#toLowerCase;
180 * otherwise, only US-ASCII charactes can be converted.
181 * @return the lowercase equivalent of the character, if any;
182 * otherwise, the character itself.
184 public static char toLowerCase(char ch, boolean unicodeAware) {
185 if (unicodeAware) return Character.toLowerCase(ch);
186 if (ch >= 'A' && ch <= 'Z') return (char)(ch + 'a' - 'A');
187 return ch;
191 * Converts the character argument to uppercase.
192 * @param ch the character to be converted.
193 * @param unicodeAware If true, use java.lang.Character#toUpperCase;
194 * otherwise, only US-ASCII charactes can be converted.
195 * @return the uppercase equivalent of the character, if any;
196 * otherwise, the character itself.
198 public static char toUpperCase(char ch, boolean unicodeAware) {
199 if (unicodeAware) return Character.toUpperCase(ch);
200 if (ch >= 'a' && ch <= 'z') return (char)(ch + 'A' - 'a');
201 return ch;