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

http://ambienttalk.googlecode.com/ · Java · 148 lines · 95 code · 13 blank · 40 comment · 23 complexity · 3afafbf50275becd95af7b812b9964c0 MD5 · raw file

  1. /* gnu/regexp/RETokenPOSIX.java
  2. Copyright (C) 1998-2001, 2004 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. final class RETokenPOSIX extends REToken {
  33. int type;
  34. boolean insens;
  35. boolean negated;
  36. static final int ALNUM = 0;
  37. static final int ALPHA = 1;
  38. static final int BLANK = 2;
  39. static final int CNTRL = 3;
  40. static final int DIGIT = 4;
  41. static final int GRAPH = 5;
  42. static final int LOWER = 6;
  43. static final int PRINT = 7;
  44. static final int PUNCT = 8;
  45. static final int SPACE = 9;
  46. static final int UPPER = 10;
  47. static final int XDIGIT = 11;
  48. // Array indices correspond to constants defined above.
  49. static final String[] s_nameTable = {
  50. "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower",
  51. "print", "punct", "space", "upper", "xdigit"
  52. };
  53. // The RE constructor uses this to look up the constant for a string
  54. static int intValue(String key) {
  55. for (int i = 0; i < s_nameTable.length; i++) {
  56. if (s_nameTable[i].equals(key)) return i;
  57. }
  58. return -1;
  59. }
  60. RETokenPOSIX(int subIndex, int type, boolean insens, boolean negated) {
  61. super(subIndex);
  62. this.type = type;
  63. this.insens = insens;
  64. this.negated = negated;
  65. }
  66. int getMinimumLength() {
  67. return 1;
  68. }
  69. int getMaximumLength() {
  70. return 1;
  71. }
  72. boolean match(CharIndexed input, REMatch mymatch) {
  73. char ch = input.charAt(mymatch.index);
  74. if (ch == CharIndexed.OUT_OF_BOUNDS)
  75. return false;
  76. boolean retval = false;
  77. switch (type) {
  78. case ALNUM:
  79. // Note that there is some debate over whether '_' should be included
  80. retval = Character.isLetterOrDigit(ch) || (ch == '_');
  81. break;
  82. case ALPHA:
  83. retval = Character.isLetter(ch);
  84. break;
  85. case BLANK:
  86. retval = ((ch == ' ') || (ch == '\t'));
  87. break;
  88. case CNTRL:
  89. retval = Character.isISOControl(ch);
  90. break;
  91. case DIGIT:
  92. retval = Character.isDigit(ch);
  93. break;
  94. case GRAPH:
  95. retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch)));
  96. break;
  97. case LOWER:
  98. retval = ((insens && Character.isLetter(ch)) || Character.isLowerCase(ch));
  99. break;
  100. case PRINT:
  101. retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch)))
  102. || (ch == ' ');
  103. break;
  104. case PUNCT:
  105. // This feels sloppy, especially for non-U.S. locales.
  106. retval = ("`~!@#$%^&*()-_=+[]{}\\|;:'\"/?,.<>".indexOf(ch)!=-1);
  107. break;
  108. case SPACE:
  109. retval = Character.isWhitespace(ch);
  110. break;
  111. case UPPER:
  112. retval = ((insens && Character.isLetter(ch)) || Character.isUpperCase(ch));
  113. break;
  114. case XDIGIT:
  115. retval = (Character.isDigit(ch) || ("abcdefABCDEF".indexOf(ch)!=-1));
  116. break;
  117. }
  118. if (negated) retval = !retval;
  119. if (retval) {
  120. ++mymatch.index;
  121. return next(input, mymatch);
  122. }
  123. else return false;
  124. }
  125. void dump(StringBuffer os) {
  126. if (negated) os.append('^');
  127. os.append("[:" + s_nameTable[type] + ":]");
  128. }
  129. }