KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > parser > ScannerHelper


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.parser;
12
13 import java.io.DataInputStream JavaDoc;
14 import java.io.FileNotFoundException JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 import org.eclipse.jdt.core.compiler.InvalidInputException;
18 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
19
20 public class ScannerHelper {
21     
22     public final static long[] Bits = {
23         ASTNode.Bit1, ASTNode.Bit2, ASTNode.Bit3, ASTNode.Bit4, ASTNode.Bit5, ASTNode.Bit6,
24         ASTNode.Bit7, ASTNode.Bit8, ASTNode.Bit9, ASTNode.Bit10, ASTNode.Bit11, ASTNode.Bit12,
25         ASTNode.Bit13, ASTNode.Bit14, ASTNode.Bit15, ASTNode.Bit16, ASTNode.Bit17, ASTNode.Bit18,
26         ASTNode.Bit19, ASTNode.Bit20, ASTNode.Bit21, ASTNode.Bit22, ASTNode.Bit23, ASTNode.Bit24,
27         ASTNode.Bit25, ASTNode.Bit26, ASTNode.Bit27, ASTNode.Bit28, ASTNode.Bit29, ASTNode.Bit30,
28         ASTNode.Bit31, ASTNode.Bit32, ASTNode.Bit33L, ASTNode.Bit34L, ASTNode.Bit35L, ASTNode.Bit36L,
29         ASTNode.Bit37L, ASTNode.Bit38L, ASTNode.Bit39L, ASTNode.Bit40L, ASTNode.Bit41L, ASTNode.Bit42L,
30         ASTNode.Bit43L, ASTNode.Bit44L, ASTNode.Bit45L, ASTNode.Bit46L, ASTNode.Bit47L, ASTNode.Bit48L,
31         ASTNode.Bit49L, ASTNode.Bit50L, ASTNode.Bit51L, ASTNode.Bit52L, ASTNode.Bit53L, ASTNode.Bit54L,
32         ASTNode.Bit55L, ASTNode.Bit56L, ASTNode.Bit57L, ASTNode.Bit58L, ASTNode.Bit59L, ASTNode.Bit60L,
33         ASTNode.Bit61L, ASTNode.Bit62L, ASTNode.Bit63L, ASTNode.Bit64L,
34     };
35
36     private static final int START_INDEX = 0;
37     private static final int PART_INDEX = 1;
38
39     private static long[][][] Tables;
40
41     public final static int MAX_OBVIOUS = 128;
42     public final static int[] OBVIOUS_IDENT_CHAR_NATURES = new int[MAX_OBVIOUS];
43
44     public final static int C_JLS_SPACE = ASTNode.Bit9;
45     public final static int C_SPECIAL = ASTNode.Bit8;
46     public final static int C_IDENT_START = ASTNode.Bit7;
47     public final static int C_UPPER_LETTER = ASTNode.Bit6;
48     public final static int C_LOWER_LETTER = ASTNode.Bit5;
49     public final static int C_IDENT_PART = ASTNode.Bit4;
50     public final static int C_DIGIT = ASTNode.Bit3;
51     public final static int C_SEPARATOR = ASTNode.Bit2;
52     public final static int C_SPACE = ASTNode.Bit1;
53
54     static {
55         OBVIOUS_IDENT_CHAR_NATURES[0] = C_IDENT_PART;
56         OBVIOUS_IDENT_CHAR_NATURES[1] = C_IDENT_PART;
57         OBVIOUS_IDENT_CHAR_NATURES[2] = C_IDENT_PART;
58         OBVIOUS_IDENT_CHAR_NATURES[3] = C_IDENT_PART;
59         OBVIOUS_IDENT_CHAR_NATURES[4] = C_IDENT_PART;
60         OBVIOUS_IDENT_CHAR_NATURES[5] = C_IDENT_PART;
61         OBVIOUS_IDENT_CHAR_NATURES[6] = C_IDENT_PART;
62         OBVIOUS_IDENT_CHAR_NATURES[7] = C_IDENT_PART;
63         OBVIOUS_IDENT_CHAR_NATURES[8] = C_IDENT_PART;
64         OBVIOUS_IDENT_CHAR_NATURES[14] = C_IDENT_PART;
65         OBVIOUS_IDENT_CHAR_NATURES[15] = C_IDENT_PART;
66         OBVIOUS_IDENT_CHAR_NATURES[16] = C_IDENT_PART;
67         OBVIOUS_IDENT_CHAR_NATURES[17] = C_IDENT_PART;
68         OBVIOUS_IDENT_CHAR_NATURES[18] = C_IDENT_PART;
69         OBVIOUS_IDENT_CHAR_NATURES[19] = C_IDENT_PART;
70         OBVIOUS_IDENT_CHAR_NATURES[20] = C_IDENT_PART;
71         OBVIOUS_IDENT_CHAR_NATURES[21] = C_IDENT_PART;
72         OBVIOUS_IDENT_CHAR_NATURES[22] = C_IDENT_PART;
73         OBVIOUS_IDENT_CHAR_NATURES[23] = C_IDENT_PART;
74         OBVIOUS_IDENT_CHAR_NATURES[24] = C_IDENT_PART;
75         OBVIOUS_IDENT_CHAR_NATURES[25] = C_IDENT_PART;
76         OBVIOUS_IDENT_CHAR_NATURES[26] = C_IDENT_PART;
77         OBVIOUS_IDENT_CHAR_NATURES[27] = C_IDENT_PART;
78         OBVIOUS_IDENT_CHAR_NATURES[127] = C_IDENT_PART;
79         
80         for (int i = '0'; i <= '9'; i++)
81             OBVIOUS_IDENT_CHAR_NATURES[i] = C_DIGIT | C_IDENT_PART;
82         
83         for (int i = 'a'; i <= 'z'; i++)
84             OBVIOUS_IDENT_CHAR_NATURES[i] = C_LOWER_LETTER | C_IDENT_PART | C_IDENT_START;
85         for (int i = 'A'; i <= 'Z'; i++)
86             OBVIOUS_IDENT_CHAR_NATURES[i] = C_UPPER_LETTER | C_IDENT_PART | C_IDENT_START;
87
88         OBVIOUS_IDENT_CHAR_NATURES['_'] = C_SPECIAL | C_IDENT_PART | C_IDENT_START;
89         OBVIOUS_IDENT_CHAR_NATURES['$'] = C_SPECIAL | C_IDENT_PART | C_IDENT_START;
90         
91         OBVIOUS_IDENT_CHAR_NATURES[9] = C_SPACE | C_JLS_SPACE; // \ u0009: HORIZONTAL TABULATION
92
OBVIOUS_IDENT_CHAR_NATURES[10] = C_SPACE | C_JLS_SPACE; // \ u000a: LINE FEED
93
OBVIOUS_IDENT_CHAR_NATURES[11] = C_SPACE;
94         OBVIOUS_IDENT_CHAR_NATURES[12] = C_SPACE | C_JLS_SPACE; // \ u000c: FORM FEED
95
OBVIOUS_IDENT_CHAR_NATURES[13] = C_SPACE | C_JLS_SPACE; // \ u000d: CARRIAGE RETURN
96
OBVIOUS_IDENT_CHAR_NATURES[28] = C_SPACE;
97         OBVIOUS_IDENT_CHAR_NATURES[29] = C_SPACE;
98         OBVIOUS_IDENT_CHAR_NATURES[30] = C_SPACE;
99         OBVIOUS_IDENT_CHAR_NATURES[31] = C_SPACE;
100         OBVIOUS_IDENT_CHAR_NATURES[32] = C_SPACE | C_JLS_SPACE; // \ u0020: SPACE
101

102         OBVIOUS_IDENT_CHAR_NATURES['.'] = C_SEPARATOR;
103         OBVIOUS_IDENT_CHAR_NATURES[':'] = C_SEPARATOR;
104         OBVIOUS_IDENT_CHAR_NATURES[';'] = C_SEPARATOR;
105         OBVIOUS_IDENT_CHAR_NATURES[','] = C_SEPARATOR;
106         OBVIOUS_IDENT_CHAR_NATURES['['] = C_SEPARATOR;
107         OBVIOUS_IDENT_CHAR_NATURES[']'] = C_SEPARATOR;
108         OBVIOUS_IDENT_CHAR_NATURES['('] = C_SEPARATOR;
109         OBVIOUS_IDENT_CHAR_NATURES[')'] = C_SEPARATOR;
110         OBVIOUS_IDENT_CHAR_NATURES['{'] = C_SEPARATOR;
111         OBVIOUS_IDENT_CHAR_NATURES['}'] = C_SEPARATOR;
112         OBVIOUS_IDENT_CHAR_NATURES['+'] = C_SEPARATOR;
113         OBVIOUS_IDENT_CHAR_NATURES['-'] = C_SEPARATOR;
114         OBVIOUS_IDENT_CHAR_NATURES['*'] = C_SEPARATOR;
115         OBVIOUS_IDENT_CHAR_NATURES['/'] = C_SEPARATOR;
116         OBVIOUS_IDENT_CHAR_NATURES['='] = C_SEPARATOR;
117         OBVIOUS_IDENT_CHAR_NATURES['&'] = C_SEPARATOR;
118         OBVIOUS_IDENT_CHAR_NATURES['|'] = C_SEPARATOR;
119         OBVIOUS_IDENT_CHAR_NATURES['?'] = C_SEPARATOR;
120         OBVIOUS_IDENT_CHAR_NATURES['<'] = C_SEPARATOR;
121         OBVIOUS_IDENT_CHAR_NATURES['>'] = C_SEPARATOR;
122         OBVIOUS_IDENT_CHAR_NATURES['!'] = C_SEPARATOR;
123         OBVIOUS_IDENT_CHAR_NATURES['%'] = C_SEPARATOR;
124         OBVIOUS_IDENT_CHAR_NATURES['^'] = C_SEPARATOR;
125         OBVIOUS_IDENT_CHAR_NATURES['~'] = C_SEPARATOR;
126         OBVIOUS_IDENT_CHAR_NATURES['"'] = C_SEPARATOR;
127         OBVIOUS_IDENT_CHAR_NATURES['\''] = C_SEPARATOR;
128     }
129     
130 static {
131     Tables = new long[2][][];
132     Tables[START_INDEX] = new long[2][];
133     Tables[PART_INDEX] = new long[3][];
134     try {
135         DataInputStream JavaDoc inputStream = new DataInputStream JavaDoc(ScannerHelper.class.getResourceAsStream("start1.rsc")); //$NON-NLS-1$
136
long[] readValues = new long[1024];
137         for (int i = 0; i < 1024; i++) {
138             readValues[i] = inputStream.readLong();
139         }
140         inputStream.close();
141         Tables[START_INDEX][0] = readValues;
142     } catch (FileNotFoundException JavaDoc e) {
143         e.printStackTrace();
144     } catch (IOException JavaDoc e) {
145         e.printStackTrace();
146     }
147     try {
148         DataInputStream JavaDoc inputStream = new DataInputStream JavaDoc(ScannerHelper.class.getResourceAsStream("start2.rsc")); //$NON-NLS-1$
149
long[] readValues = new long[1024];
150         for (int i = 0; i < 1024; i++) {
151             readValues[i] = inputStream.readLong();
152         }
153         inputStream.close();
154         Tables[START_INDEX][1] = readValues;
155     } catch (FileNotFoundException JavaDoc e) {
156         e.printStackTrace();
157     } catch (IOException JavaDoc e) {
158         e.printStackTrace();
159     }
160     try {
161         DataInputStream JavaDoc inputStream = new DataInputStream JavaDoc(ScannerHelper.class.getResourceAsStream("part1.rsc")); //$NON-NLS-1$
162
long[] readValues = new long[1024];
163         for (int i = 0; i < 1024; i++) {
164             readValues[i] = inputStream.readLong();
165         }
166         inputStream.close();
167         Tables[PART_INDEX][0] = readValues;
168     } catch (FileNotFoundException JavaDoc e) {
169         e.printStackTrace();
170     } catch (IOException JavaDoc e) {
171         e.printStackTrace();
172     }
173     try {
174         DataInputStream JavaDoc inputStream = new DataInputStream JavaDoc(ScannerHelper.class.getResourceAsStream("part2.rsc")); //$NON-NLS-1$
175
long[] readValues = new long[1024];
176         for (int i = 0; i < 1024; i++) {
177             readValues[i] = inputStream.readLong();
178         }
179         inputStream.close();
180         Tables[PART_INDEX][1] = readValues;
181     } catch (FileNotFoundException JavaDoc e) {
182         e.printStackTrace();
183     } catch (IOException JavaDoc e) {
184         e.printStackTrace();
185     }
186     try {
187         DataInputStream JavaDoc inputStream = new DataInputStream JavaDoc(ScannerHelper.class.getResourceAsStream("part14.rsc")); //$NON-NLS-1$
188
long[] readValues = new long[1024];
189         for (int i = 0; i < 1024; i++) {
190             readValues[i] = inputStream.readLong();
191         }
192         inputStream.close();
193         Tables[PART_INDEX][2] = readValues;
194     } catch (FileNotFoundException JavaDoc e) {
195         e.printStackTrace();
196     } catch (IOException JavaDoc e) {
197         e.printStackTrace();
198     }
199 }
200
201 private final static boolean isBitSet(long[] values, int i) {
202     try {
203         return (values[i / 64] & Bits[i % 64]) != 0;
204     } catch (NullPointerException JavaDoc e) {
205         return false;
206     }
207 }
208 public static boolean isJavaIdentifierPart(char c) {
209     if (c < MAX_OBVIOUS) {
210         return (ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_IDENT_PART) != 0;
211     }
212     return Character.isJavaIdentifierPart(c);
213 }
214 public static boolean isJavaIdentifierPart(char high, char low) {
215     int codePoint = toCodePoint(high, low);
216     switch((codePoint & 0x1F0000) >> 16) {
217         case 0 :
218             return Character.isJavaIdentifierPart((char) codePoint);
219         case 1 :
220             return isBitSet(Tables[PART_INDEX][0], codePoint & 0xFFFF);
221         case 2 :
222             return isBitSet(Tables[PART_INDEX][1], codePoint & 0xFFFF);
223         case 14 :
224             return isBitSet(Tables[PART_INDEX][2], codePoint & 0xFFFF);
225     }
226     return false;
227 }
228 public static boolean isJavaIdentifierStart(char c) {
229     if (c < MAX_OBVIOUS) {
230         return (ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_IDENT_START) != 0;
231     }
232     return Character.isJavaIdentifierStart(c);
233 }
234 public static boolean isJavaIdentifierStart(char high, char low) {
235     int codePoint = toCodePoint(high, low);
236     switch((codePoint & 0x1F0000) >> 16) {
237         case 0 :
238             return Character.isJavaIdentifierStart((char) codePoint);
239         case 1 :
240             return isBitSet(Tables[START_INDEX][0], codePoint & 0xFFFF);
241         case 2 :
242             return isBitSet(Tables[START_INDEX][1], codePoint & 0xFFFF);
243     }
244     return false;
245 }
246
247 private static int toCodePoint(char high, char low) {
248     return (high - Scanner.HIGH_SURROGATE_MIN_VALUE) * 0x400 + (low - Scanner.LOW_SURROGATE_MIN_VALUE) + 0x10000;
249 }
250 public static boolean isDigit(char c) throws InvalidInputException {
251     if(c < ScannerHelper.MAX_OBVIOUS) {
252         return (ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_DIGIT) != 0;
253     }
254     if (Character.isDigit(c)) {
255         throw new InvalidInputException(Scanner.INVALID_DIGIT);
256     }
257     return false;
258 }
259 public static int digit(char c, int radix) {
260     if (c < ScannerHelper.MAX_OBVIOUS) {
261         switch(radix) {
262             case 8 :
263                 if (c >= 48 && c <= 55) {
264                     return c - 48;
265                 }
266                 return -1;
267             case 10 :
268                 if (c >= 48 && c <= 57) {
269                     return c - 48;
270                 }
271                 return -1;
272             case 16 :
273                 if (c >= 48 && c <= 57) {
274                     return c - 48;
275                 }
276                 if (c >= 65 && c <= 70) {
277                     return c - 65 + 10;
278                 }
279                 if (c >= 97 && c <= 102) {
280                     return c - 97 + 10;
281                 }
282                 return -1;
283         }
284     }
285     return Character.digit(c, radix);
286 }
287 public static int getNumericValue(char c) {
288     if (c < ScannerHelper.MAX_OBVIOUS) {
289         switch(ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c]) {
290             case C_DIGIT :
291                 return c - '0';
292             case C_LOWER_LETTER :
293                 return 10 + c - 'a';
294             case C_UPPER_LETTER :
295                 return 10 + c - 'A';
296         }
297     }
298     return Character.getNumericValue(c);
299 }
300 public static char toUpperCase(char c) {
301     if (c < MAX_OBVIOUS) {
302         if ((ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_UPPER_LETTER) != 0) {
303             return c;
304         } else if ((ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_LOWER_LETTER) != 0) {
305             return (char) (c - 32);
306         }
307     }
308     return Character.toLowerCase(c);
309 }
310 public static char toLowerCase(char c) {
311     if (c < MAX_OBVIOUS) {
312         if ((ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_LOWER_LETTER) != 0) {
313             return c;
314         } else if ((ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_UPPER_LETTER) != 0) {
315             return (char) (32 + c);
316         }
317     }
318     return Character.toLowerCase(c);
319 }
320 public static boolean isLowerCase(char c) {
321     if (c < MAX_OBVIOUS) {
322         return (ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_LOWER_LETTER) != 0;
323     }
324     return Character.isLowerCase(c);
325 }
326 public static boolean isUpperCase(char c) {
327     if (c < MAX_OBVIOUS) {
328         return (ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_UPPER_LETTER) != 0;
329     }
330     return Character.isUpperCase(c);
331 }
332 /**
333  * Include also non JLS whitespaces.
334  *
335  * return true if Character.isWhitespace(c) would return true
336  */

337 public static boolean isWhitespace(char c) {
338     if (c < MAX_OBVIOUS) {
339         return (ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_SPACE) != 0;
340     }
341     return Character.isWhitespace(c);
342 }
343 public static boolean isLetter(char c) {
344     if (c < MAX_OBVIOUS) {
345         return (ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & (ScannerHelper.C_UPPER_LETTER | ScannerHelper.C_LOWER_LETTER)) != 0;
346     }
347     return Character.isLetter(c);
348 }
349 public static boolean isLetterOrDigit(char c) {
350     if (c < MAX_OBVIOUS) {
351         return (ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & (ScannerHelper.C_UPPER_LETTER | ScannerHelper.C_LOWER_LETTER | ScannerHelper.C_DIGIT)) != 0;
352     }
353     return Character.isLetterOrDigit(c);
354 }
355 }
356
Popular Tags