001: /*
002: * gnu/regexp/RETokenPOSIX.java
003: * Copyright (C) 1998 Wes Biggs
004: *
005: * This library is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU Library General Public License as published
007: * by the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Library General Public License for more details.
014: *
015: * You should have received a copy of the GNU Library General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018: */
019:
020: package gnu.regexp;
021:
022: import java.util.Hashtable;
023:
024: class RETokenPOSIX extends REToken {
025: int m_type;
026: boolean m_insens;
027: boolean m_negated;
028:
029: static final int ALNUM = 0;
030: static final int ALPHA = 1;
031: static final int BLANK = 2;
032: static final int CNTRL = 3;
033: static final int DIGIT = 4;
034: static final int GRAPH = 5;
035: static final int LOWER = 6;
036: static final int PRINT = 7;
037: static final int PUNCT = 8;
038: static final int SPACE = 9;
039: static final int UPPER = 10;
040: static final int XDIGIT = 11;
041:
042: // Array indices correspond to constants defined above.
043: static final String[] s_nameTable = { "alnum", "alpha", "blank",
044: "cntrl", "digit", "graph", "lower", "print", "punct",
045: "space", "upper", "xdigit" };
046:
047: // The RE constructor uses this to look up the constant for a string
048: static int intValue(String key) {
049: for (int i = 0; i < s_nameTable.length; i++) {
050: if (s_nameTable[i].equals(key))
051: return i;
052: }
053: return -1;
054: }
055:
056: RETokenPOSIX(int f_subIndex, int f_type, boolean f_insens,
057: boolean f_negated) {
058: super (f_subIndex);
059: m_type = f_type;
060: m_insens = f_insens;
061: m_negated = f_negated;
062: }
063:
064: int getMinimumLength() {
065: return 1;
066: }
067:
068: int[] match(CharIndexed input, int index, int eflags,
069: REMatch mymatch) {
070: char ch = input.charAt(index);
071: if (ch == CharIndexed.OUT_OF_BOUNDS)
072: return null;
073:
074: boolean retval = false;
075: switch (m_type) {
076: case ALNUM:
077: retval = Character.isLetterOrDigit(ch);
078: break;
079: case ALPHA:
080: retval = Character.isLetter(ch);
081: break;
082: case BLANK:
083: retval = ((ch == ' ') || (ch == '\t'));
084: break;
085: case CNTRL:
086: retval = Character.isISOControl(ch);
087: break;
088: case DIGIT:
089: retval = Character.isDigit(ch);
090: break;
091: case GRAPH:
092: retval = (!(Character.isWhitespace(ch) || Character
093: .isISOControl(ch)));
094: break;
095: case LOWER:
096: retval = ((m_insens && Character.isLetter(ch)) || Character
097: .isLowerCase(ch));
098: break;
099: case PRINT:
100: retval = Character.isLetterOrDigit(ch);
101: break;
102: case PUNCT:
103: retval = ("`~!@#$%^&*()-_=+[]{}\\|;:'\"/?,.<>".indexOf(ch) != -1);
104: break;
105: case SPACE:
106: retval = Character.isWhitespace(ch);
107: break;
108: case UPPER:
109: retval = ((m_insens && Character.isLetter(ch)) || Character
110: .isUpperCase(ch));
111: break;
112: case XDIGIT:
113: retval = (Character.isDigit(ch) || ("abcdefABCDEF"
114: .indexOf(ch) != -1));
115: break;
116: }
117:
118: if (m_negated)
119: retval = !retval;
120: if (retval)
121: return next(input, index + 1, eflags, mymatch);
122: else
123: return null;
124: }
125:
126: void dump(StringBuffer os) {
127: if (m_negated)
128: os.append('^');
129: os.append("[:" + s_nameTable[m_type] + ":]");
130: }
131: }
|