PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  4This file is part of GNU Classpath.
  5
  6GNU Classpath is free software; you can redistribute it and/or modify
  7it under the terms of the GNU General Public License as published by
  8the Free Software Foundation; either version 2, or (at your option)
  9any later version.
 10
 11GNU Classpath is distributed in the hope that it will be useful, but
 12WITHOUT ANY WARRANTY; without even the implied warranty of
 13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14General Public License for more details.
 15
 16You should have received a copy of the GNU General Public License
 17along with GNU Classpath; see the file COPYING.  If not, write to the
 18Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 1902110-1301 USA.
 20
 21Linking this library statically or dynamically with other modules is
 22making a combined work based on this library.  Thus, the terms and
 23conditions of the GNU General Public License cover the whole
 24combination.
 25
 26As a special exception, the copyright holders of this library give you
 27permission to link this library with independent modules to produce an
 28executable, regardless of the license terms of these independent
 29modules, and to copy and distribute the resulting executable under
 30terms of your choice, provided that you also meet, for each linked
 31independent module, the terms and conditions of the license of that
 32module.  An independent module is a module which is not derived from
 33or based on this library.  If you modify this library, you may extend
 34this exception to your version of the library, but you are not
 35obligated to do so.  If you do not wish to do so, delete this
 36exception statement from your version. */
 37
 38
 39package edu.vub.util.regexp;
 40
 41final class RETokenPOSIX extends REToken {
 42  int type;
 43  boolean insens;
 44  boolean negated;
 45
 46  static final int  ALNUM = 0;
 47  static final int  ALPHA = 1;
 48  static final int  BLANK = 2;
 49  static final int  CNTRL = 3;
 50  static final int  DIGIT = 4;
 51  static final int  GRAPH = 5;
 52  static final int  LOWER = 6;
 53  static final int  PRINT = 7;
 54  static final int  PUNCT = 8;
 55  static final int  SPACE = 9;
 56  static final int  UPPER = 10;
 57  static final int XDIGIT = 11;
 58
 59  // Array indices correspond to constants defined above.
 60  static final String[] s_nameTable =  {
 61    "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower",
 62    "print", "punct", "space", "upper", "xdigit" 
 63  };
 64
 65  // The RE constructor uses this to look up the constant for a string
 66  static int intValue(String key) {
 67    for (int i = 0; i < s_nameTable.length; i++) {
 68      if (s_nameTable[i].equals(key)) return i;
 69    }
 70    return -1;
 71  }
 72
 73  RETokenPOSIX(int subIndex, int type, boolean insens, boolean negated) {
 74    super(subIndex);
 75    this.type = type;
 76    this.insens = insens;
 77    this.negated = negated;
 78  }
 79
 80    int getMinimumLength() {
 81	return 1;
 82    }
 83
 84    int getMaximumLength() {
 85	return 1;
 86    }
 87
 88    boolean match(CharIndexed input, REMatch mymatch) {
 89    char ch = input.charAt(mymatch.index);
 90    if (ch == CharIndexed.OUT_OF_BOUNDS)
 91      return false;
 92    
 93    boolean retval = false;
 94    switch (type) {
 95    case ALNUM:
 96	// Note that there is some debate over whether '_' should be included
 97	retval = Character.isLetterOrDigit(ch) || (ch == '_');
 98	break;
 99    case ALPHA:
100	retval = Character.isLetter(ch);
101	break;
102    case BLANK:
103	retval = ((ch == ' ') || (ch == '\t'));
104	break;
105    case CNTRL:
106	retval = Character.isISOControl(ch);
107	break;
108    case DIGIT:
109	retval = Character.isDigit(ch);
110	break;
111    case GRAPH:
112	retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch)));
113	break;
114    case LOWER:
115	retval = ((insens && Character.isLetter(ch)) || Character.isLowerCase(ch));
116	break;
117    case PRINT:
118	retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch)))
119	    || (ch == ' ');
120	break;
121    case PUNCT:
122	// This feels sloppy, especially for non-U.S. locales.
123	retval = ("`~!@#$%^&*()-_=+[]{}\\|;:'\"/?,.<>".indexOf(ch)!=-1);
124	break;
125    case SPACE:
126	retval = Character.isWhitespace(ch);
127	break;
128    case UPPER:
129	retval = ((insens && Character.isLetter(ch)) || Character.isUpperCase(ch));
130	break;
131    case XDIGIT:
132	retval = (Character.isDigit(ch) || ("abcdefABCDEF".indexOf(ch)!=-1));
133	break;
134    }
135
136    if (negated) retval = !retval;
137    if (retval) {
138	++mymatch.index;
139	return next(input, mymatch);
140    }
141    else return false;
142  }
143
144  void dump(StringBuffer os) {
145    if (negated) os.append('^');
146    os.append("[:" + s_nameTable[type] + ":]");
147  }
148}