PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/gnu/regexp/RETokenOneOf.java

#
Java | 112 lines | 74 code | 13 blank | 25 comment | 23 complexity | 4def0c64def72d1b929b86a0a7a9b127 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * gnu/regexp/RETokenOneOf.java
  3. * Copyright (C) 1998-2001 Wes Biggs
  4. *
  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.
  9. *
  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.
  14. *
  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.
  18. */
  19. package gnu.regexp;
  20. import java.util.Vector;
  21. final class RETokenOneOf extends REToken {
  22. private Vector options;
  23. private boolean negative;
  24. // This constructor is used for convenience when we know the set beforehand,
  25. // e.g. \d --> new RETokenOneOf("0123456789",false, ..)
  26. // \D --> new RETokenOneOf("0123456789",true, ..)
  27. RETokenOneOf(int subIndex, String optionsStr, boolean negative, boolean insens) {
  28. super(subIndex);
  29. options = new Vector();
  30. this.negative = negative;
  31. for (int i = 0; i < optionsStr.length(); i++)
  32. options.addElement(new RETokenChar(subIndex,optionsStr.charAt(i),insens));
  33. }
  34. RETokenOneOf(int subIndex, Vector options, boolean negative) {
  35. super(subIndex);
  36. this.options = options;
  37. this.negative = negative;
  38. }
  39. int getMinimumLength() {
  40. int min = Integer.MAX_VALUE;
  41. int x;
  42. for (int i=0; i < options.size(); i++) {
  43. if ((x = ((REToken) options.elementAt(i)).getMinimumLength()) < min)
  44. min = x;
  45. }
  46. return min;
  47. }
  48. boolean match(CharIndexed input, REMatch mymatch) {
  49. if (negative && (input.charAt(mymatch.index) == CharIndexed.OUT_OF_BOUNDS))
  50. return false;
  51. REMatch newMatch = null;
  52. REMatch last = null;
  53. REToken tk;
  54. boolean isMatch;
  55. for (int i=0; i < options.size(); i++) {
  56. tk = (REToken) options.elementAt(i);
  57. REMatch tryMatch = (REMatch) mymatch.clone();
  58. if (tk.match(input, tryMatch)) { // match was successful
  59. if (negative) return false;
  60. if (next(input, tryMatch)) {
  61. // Add tryMatch to list of possibilities.
  62. if (last == null) {
  63. newMatch = tryMatch;
  64. last = tryMatch;
  65. } else {
  66. last.next = tryMatch;
  67. last = tryMatch;
  68. }
  69. } // next succeeds
  70. } // is a match
  71. } // try next option
  72. if (newMatch != null) {
  73. if (negative) {
  74. return false;
  75. } else {
  76. // set contents of mymatch equal to newMatch
  77. // try each one that matched
  78. mymatch.assignFrom(newMatch);
  79. return true;
  80. }
  81. } else {
  82. if (negative) {
  83. ++mymatch.index;
  84. return next(input, mymatch);
  85. } else {
  86. return false;
  87. }
  88. }
  89. // index+1 works for [^abc] lists, not for generic lookahead (--> index)
  90. }
  91. void dump(StringBuffer os) {
  92. os.append(negative ? "[^" : "(?:");
  93. for (int i = 0; i < options.size(); i++) {
  94. if (!negative && (i > 0)) os.append('|');
  95. ((REToken) options.elementAt(i)).dumpAll(os);
  96. }
  97. os.append(negative ? ']' : ')');
  98. }
  99. }