/interpreter/tags/at2-build190607/src/edu/vub/util/regexp/REMatch.java

http://ambienttalk.googlecode.com/ · Java · 319 lines · 139 code · 26 blank · 154 comment · 32 complexity · 9a9767241205c6df74f4daef0c63ea92 MD5 · raw file

  1. /* gnu/regexp/REMatch.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.io.Serializable;
  33. /**
  34. * An instance of this class represents a match
  35. * completed by a edu.vub.util.regexp matching function. It can be used
  36. * to obtain relevant information about the location of a match
  37. * or submatch.
  38. *
  39. * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
  40. */
  41. public final class REMatch implements Serializable, Cloneable {
  42. private String matchedText;
  43. // These variables are package scope for fast access within the engine
  44. int eflags; // execution flags this match was made using
  45. // Offset in source text where match was tried. This is zero-based;
  46. // the actual position in the source text is given by (offset + anchor).
  47. int offset;
  48. // Anchor position refers to the index into the source input
  49. // at which the matching operation began.
  50. // This is also useful for the ANCHORINDEX option.
  51. int anchor;
  52. // Package scope; used by RE.
  53. int index; // used while matching to mark current match position in input
  54. int[] start; // start positions (relative to offset) for each (sub)exp.
  55. int[] end; // end positions for the same
  56. REMatch next; // other possibility (to avoid having to use arrays)
  57. boolean empty; // empty string matched. This flag is used only within
  58. // RETokenRepeated.
  59. int matchFlags; // flags passed to match methods
  60. static final int MF_FIND_ALL = 0x01;
  61. public Object clone() {
  62. try {
  63. REMatch copy = (REMatch) super.clone();
  64. copy.next = null;
  65. copy.start = (int[]) start.clone();
  66. copy.end = (int[]) end.clone();
  67. return copy;
  68. } catch (CloneNotSupportedException e) {
  69. throw new Error(); // doesn't happen
  70. }
  71. }
  72. void assignFrom(REMatch other) {
  73. start = other.start;
  74. end = other.end;
  75. index = other.index;
  76. // need to deep clone?
  77. next = other.next;
  78. }
  79. REMatch(int subs, int anchor, int eflags) {
  80. start = new int[subs+1];
  81. end = new int[subs+1];
  82. this.anchor = anchor;
  83. this.eflags = eflags;
  84. clear(anchor);
  85. }
  86. void finish(CharIndexed text) {
  87. start[0] = 0;
  88. StringBuffer sb = new StringBuffer();
  89. int i;
  90. for (i = 0; i < end[0]; i++)
  91. sb.append(text.charAt(i));
  92. matchedText = sb.toString();
  93. for (i = 0; i < start.length; i++) {
  94. // If any subexpressions didn't terminate, they don't count
  95. // TODO check if this code ever gets hit
  96. if ((start[i] == -1) ^ (end[i] == -1)) {
  97. start[i] = -1;
  98. end[i] = -1;
  99. }
  100. }
  101. next = null; // cut off alternates
  102. }
  103. /** Clears the current match and moves the offset to the new index. */
  104. void clear(int index) {
  105. offset = index;
  106. this.index = 0;
  107. for (int i = 0; i < start.length; i++) {
  108. start[i] = end[i] = -1;
  109. }
  110. next = null; // cut off alternates
  111. }
  112. /**
  113. * Returns the string matching the pattern. This makes it convenient
  114. * to write code like the following:
  115. * <P>
  116. * <code>
  117. * REMatch myMatch = myExpression.getMatch(myString);<br>
  118. * if (myMatch != null) System.out.println("Regexp found: "+myMatch);
  119. * </code>
  120. */
  121. public String toString() {
  122. return matchedText;
  123. }
  124. /**
  125. * Returns the index within the input text where the match in its entirety
  126. * began.
  127. */
  128. public int getStartIndex() {
  129. return offset + start[0];
  130. }
  131. /**
  132. * Returns the index within the input string where the match in
  133. * its entirety ends. The return value is the next position after
  134. * the end of the string; therefore, a match created by the
  135. * following call:
  136. *
  137. * <P>
  138. * <code>REMatch myMatch = myExpression.getMatch(myString);</code>
  139. * <P>
  140. * can be viewed (given that myMatch is not null) by creating
  141. * <P>
  142. * <code>String theMatch = myString.substring(myMatch.getStartIndex(),
  143. * myMatch.getEndIndex());</code>
  144. * <P>
  145. * But you can save yourself that work, since the <code>toString()</code>
  146. * method (above) does exactly that for you.
  147. */
  148. public int getEndIndex() {
  149. return offset + end[0];
  150. }
  151. /**
  152. * Returns the string matching the given subexpression. The subexpressions
  153. * are indexed starting with one, not zero. That is, the subexpression
  154. * identified by the first set of parentheses in a regular expression
  155. * could be retrieved from an REMatch by calling match.toString(1).
  156. *
  157. * @param sub Index of the subexpression.
  158. */
  159. public String toString(int sub) {
  160. if ((sub >= start.length) || sub < 0)
  161. throw new IndexOutOfBoundsException("No group " + sub);
  162. if (start[sub] == -1) return null;
  163. return (matchedText.substring(start[sub],end[sub]));
  164. }
  165. /**
  166. * Returns the index within the input string used to generate this match
  167. * where subexpression number <i>sub</i> begins, or <code>-1</code> if
  168. * the subexpression does not exist. The initial position is zero.
  169. *
  170. * @param sub Subexpression index
  171. * @deprecated Use getStartIndex(int) instead.
  172. */
  173. public int getSubStartIndex(int sub) {
  174. if (sub >= start.length) return -1;
  175. int x = start[sub];
  176. return (x == -1) ? x : offset + x;
  177. }
  178. /**
  179. * Returns the index within the input string used to generate this match
  180. * where subexpression number <i>sub</i> begins, or <code>-1</code> if
  181. * the subexpression does not exist. The initial position is zero.
  182. *
  183. * @param sub Subexpression index
  184. * @since edu.vub.util.regexp 1.1.0
  185. */
  186. public int getStartIndex(int sub) {
  187. if (sub >= start.length) return -1;
  188. int x = start[sub];
  189. return (x == -1) ? x : offset + x;
  190. }
  191. /**
  192. * Returns the index within the input string used to generate this match
  193. * where subexpression number <i>sub</i> ends, or <code>-1</code> if
  194. * the subexpression does not exist. The initial position is zero.
  195. *
  196. * @param sub Subexpression index
  197. * @deprecated Use getEndIndex(int) instead
  198. */
  199. public int getSubEndIndex(int sub) {
  200. if (sub >= start.length) return -1;
  201. int x = end[sub];
  202. return (x == -1) ? x : offset + x;
  203. }
  204. /**
  205. * Returns the index within the input string used to generate this match
  206. * where subexpression number <i>sub</i> ends, or <code>-1</code> if
  207. * the subexpression does not exist. The initial position is zero.
  208. *
  209. * @param sub Subexpression index
  210. */
  211. public int getEndIndex(int sub) {
  212. if (sub >= start.length) return -1;
  213. int x = end[sub];
  214. return (x == -1) ? x : offset + x;
  215. }
  216. /**
  217. * Substitute the results of this match to create a new string.
  218. * This is patterned after PERL, so the tokens to watch out for are
  219. * <code>$0</code> through <code>$9</code>. <code>$0</code> matches
  220. * the full substring matched; <code>$<i>n</i></code> matches
  221. * subexpression number <i>n</i>.
  222. * <code>$10, $11, ...</code> may match the 10th, 11th, ... subexpressions
  223. * if such subexpressions exist.
  224. *
  225. * @param input A string consisting of literals and <code>$<i>n</i></code> tokens.
  226. */
  227. public String substituteInto(String input) {
  228. // a la Perl, $0 is whole thing, $1 - $9 are subexpressions
  229. StringBuffer output = new StringBuffer();
  230. int pos;
  231. for (pos = 0; pos < input.length()-1; pos++) {
  232. if ((input.charAt(pos) == '$') && (Character.isDigit(input.charAt(pos+1)))) {
  233. int val = Character.digit(input.charAt(++pos),10);
  234. int pos1 = pos + 1;
  235. while (pos1 < input.length() &&
  236. Character.isDigit(input.charAt(pos1))) {
  237. int val1 = val*10 + Character.digit(input.charAt(pos1),10);
  238. if (val1 >= start.length) break;
  239. pos1++;
  240. val = val1;
  241. }
  242. pos = pos1 - 1;
  243. if (val < start.length) {
  244. output.append(toString(val));
  245. }
  246. } else output.append(input.charAt(pos));
  247. }
  248. if (pos < input.length()) output.append(input.charAt(pos));
  249. return output.toString();
  250. }
  251. static class REMatchList {
  252. REMatch head;
  253. REMatch tail;
  254. REMatchList() {
  255. head = tail = null;
  256. }
  257. /* Not used now. But we may need this some day?
  258. void addHead(REMatch newone) {
  259. if (head == null) {
  260. head = newone;
  261. tail = newone;
  262. while (tail.next != null) {
  263. tail = tail.next;
  264. }
  265. }
  266. else {
  267. REMatch tmp = newone;
  268. while (tmp.next != null) tmp = tmp.next;
  269. tmp.next = head;
  270. head = newone;
  271. }
  272. }
  273. */
  274. void addTail(REMatch newone) {
  275. if (head == null) {
  276. head = newone;
  277. tail = newone;
  278. }
  279. else {
  280. tail.next = newone;
  281. }
  282. while (tail.next != null) {
  283. tail = tail.next;
  284. }
  285. }
  286. }
  287. }