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