/interpreter/tags/at2-build060407/src/edu/vub/util/regexp/RETokenOneOf.java

http://ambienttalk.googlecode.com/ · Java · 233 lines · 145 code · 18 blank · 70 comment · 46 complexity · fe092442e97995f1c3a3c8f4e8444aeb MD5 · raw file

  1. /* gnu/regexp/RETokenOneOf.java
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package edu.vub.util.regexp;
  32. import java.util.Vector;
  33. import java.util.Stack;
  34. final class RETokenOneOf extends REToken {
  35. private Vector options;
  36. private boolean negative;
  37. private Vector addition;
  38. // This Vector addition is used to store nested character classes.
  39. // For example, if the original expression is
  40. // [2-7a-c[f-k][m-z]&&[^p-v][st]]
  41. // the basic part /2-7a-c/ is stored in the Vector options, and
  42. // the additional part /[f-k][m-z]&&[^p-v][st]/ is stored in the
  43. // Vector addition in the following order (Reverse Polish Notation):
  44. // -- The matching result of the basic part is assumed here.
  45. // [f-k] -- REToken
  46. // "|" -- or
  47. // [m-z] -- REToken
  48. // "|" -- or
  49. // false
  50. // [^p-v] -- REToken
  51. // "|" -- or
  52. // [st] -- REToken
  53. // "|" -- or
  54. // "&" -- and
  55. //
  56. // As it is clear from the explanation above, the Vector addition is
  57. // effective only when this REToken originates from a character class
  58. // expression.
  59. // This constructor is used for convenience when we know the set beforehand,
  60. // e.g. \d --> new RETokenOneOf("0123456789",false, ..)
  61. // \D --> new RETokenOneOf("0123456789",true, ..)
  62. RETokenOneOf(int subIndex, String optionsStr, boolean negative, boolean insens) {
  63. super(subIndex);
  64. options = new Vector();
  65. this.negative = negative;
  66. for (int i = 0; i < optionsStr.length(); i++)
  67. options.addElement(new RETokenChar(subIndex,optionsStr.charAt(i),insens));
  68. }
  69. RETokenOneOf(int subIndex, Vector options, boolean negative) {
  70. super(subIndex);
  71. this.options = options;
  72. this.negative = negative;
  73. }
  74. RETokenOneOf(int subIndex, Vector options, Vector addition, boolean negative) {
  75. super(subIndex);
  76. this.options = options;
  77. this.addition = addition;
  78. this.negative = negative;
  79. }
  80. int getMinimumLength() {
  81. // (negative || addition != null) occurs when this token originates from
  82. // character class expression.
  83. if (negative || addition != null) return 1;
  84. int min = Integer.MAX_VALUE;
  85. int x;
  86. for (int i=0; i < options.size(); i++) {
  87. if ((x = ((REToken) options.elementAt(i)).getMinimumLength()) < min)
  88. min = x;
  89. }
  90. return min;
  91. }
  92. int getMaximumLength() {
  93. // (negative || addition != null) occurs when this token originates from
  94. // character class expression.
  95. if (negative || addition != null) return 1;
  96. int max = 0;
  97. int x;
  98. for (int i=0; i < options.size(); i++) {
  99. if ((x = ((REToken) options.elementAt(i)).getMaximumLength()) > max)
  100. max = x;
  101. }
  102. return max;
  103. }
  104. boolean match(CharIndexed input, REMatch mymatch) {
  105. REMatch tryMatch;
  106. boolean tryOnly;
  107. if (addition == null) {
  108. tryMatch = mymatch;
  109. tryOnly = false;
  110. }
  111. else {
  112. tryMatch = (REMatch) mymatch.clone();
  113. tryOnly = true;
  114. }
  115. boolean b = negative ?
  116. matchN(input, tryMatch, tryOnly) :
  117. matchP(input, tryMatch, tryOnly);
  118. if (addition == null) return b;
  119. Stack stack = new Stack();
  120. stack.push(new Boolean(b));
  121. for (int i=0; i < addition.size(); i++) {
  122. Object obj = addition.elementAt(i);
  123. if (obj instanceof REToken) {
  124. b = ((REToken)obj).match(input, (REMatch)mymatch.clone());
  125. stack.push(new Boolean(b));
  126. }
  127. else if (obj instanceof Boolean) {
  128. stack.push(obj);
  129. }
  130. else if (obj.equals("|")) {
  131. b = ((Boolean)stack.pop()).booleanValue();
  132. b = ((Boolean)stack.pop()).booleanValue() || b;
  133. stack.push(new Boolean(b));
  134. }
  135. else if (obj.equals("&")) {
  136. b = ((Boolean)stack.pop()).booleanValue();
  137. b = ((Boolean)stack.pop()).booleanValue() && b;
  138. stack.push(new Boolean(b));
  139. }
  140. else {
  141. throw new RuntimeException("Invalid object found");
  142. }
  143. }
  144. b = ((Boolean)stack.pop()).booleanValue();
  145. if (b) {
  146. ++mymatch.index;
  147. return next(input, mymatch);
  148. }
  149. return false;
  150. }
  151. private boolean matchN(CharIndexed input, REMatch mymatch, boolean tryOnly) {
  152. if (input.charAt(mymatch.index) == CharIndexed.OUT_OF_BOUNDS)
  153. return false;
  154. REMatch newMatch = null;
  155. REMatch last = null;
  156. REToken tk;
  157. for (int i=0; i < options.size(); i++) {
  158. tk = (REToken) options.elementAt(i);
  159. REMatch tryMatch = (REMatch) mymatch.clone();
  160. if (tk.match(input, tryMatch)) { // match was successful
  161. return false;
  162. } // is a match
  163. } // try next option
  164. if (tryOnly) return true;
  165. ++mymatch.index;
  166. return next(input, mymatch);
  167. }
  168. private boolean matchP(CharIndexed input, REMatch mymatch, boolean tryOnly) {
  169. boolean stopMatchingIfSatisfied =
  170. (mymatch.matchFlags & REMatch.MF_FIND_ALL) == 0;
  171. REMatch.REMatchList newMatch = new REMatch.REMatchList();
  172. REToken tk;
  173. for (int i=0; i < options.size(); i++) {
  174. // In order that the backtracking can work,
  175. // each option must be chained to the next token.
  176. // But the chain method has some side effect, so
  177. // we use clones.
  178. tk = (REToken)((REToken) options.elementAt(i)).clone();
  179. if (! tryOnly) {
  180. tk.chain(this.next);
  181. tk.setUncle(this.uncle);
  182. tk.subIndex = this.subIndex;
  183. }
  184. REMatch tryMatch = (REMatch) mymatch.clone();
  185. if (tk.match(input, tryMatch)) { // match was successful
  186. if (tryOnly) return true;
  187. newMatch.addTail(tryMatch);
  188. if (stopMatchingIfSatisfied) break;
  189. } // is a match
  190. } // try next option
  191. if (tryOnly) return false;
  192. if (newMatch.head != null) {
  193. // set contents of mymatch equal to newMatch
  194. // try each one that matched
  195. mymatch.assignFrom(newMatch.head);
  196. return true;
  197. } else {
  198. return false;
  199. }
  200. }
  201. void dump(StringBuffer os) {
  202. os.append(negative ? "[^" : "(?:");
  203. for (int i = 0; i < options.size(); i++) {
  204. if (!negative && (i > 0)) os.append('|');
  205. ((REToken) options.elementAt(i)).dumpAll(os);
  206. }
  207. os.append(negative ? ']' : ')');
  208. }
  209. }