KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > parser > JavaCCParserInternals


1 /*
2  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
4  * intellectual property rights relating to technology embodied in the product
5  * that is described in this document. In particular, and without limitation,
6  * these intellectual property rights may include one or more of the U.S.
7  * patents listed at http://www.sun.com/patents and one or more additional
8  * patents or pending patent applications in the U.S. and in other countries.
9  * U.S. Government Rights - Commercial software. Government users are subject
10  * to the Sun Microsystems, Inc. standard license agreement and applicable
11  * provisions of the FAR and its supplements. Use is subject to license terms.
12  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
13  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
14  * product is covered and controlled by U.S. Export Control laws and may be
15  * subject to the export or import laws in other countries. Nuclear, missile,
16  * chemical biological weapons or nuclear maritime end uses or end users,
17  * whether direct or indirect, are strictly prohibited. Export or reexport
18  * to countries subject to U.S. embargo or to entities identified on U.S.
19  * export exclusion lists, including, but not limited to, the denied persons
20  * and specially designated nationals lists is strictly prohibited.
21  */

22
23 package org.javacc.parser;
24
25 public abstract class JavaCCParserInternals extends JavaCCGlobals {
26
27   static protected void initialize() {
28     Integer JavaDoc i = new Integer JavaDoc(0);
29     lexstate_S2I.put("DEFAULT", i);
30     lexstate_I2S.put(i, "DEFAULT");
31     simple_tokens_table.put("DEFAULT", new java.util.Hashtable JavaDoc());
32   }
33
34   static protected void addcuname(String JavaDoc id) {
35     cu_name = id;
36   }
37
38   static protected void compare(Token t, String JavaDoc id1, String JavaDoc id2) {
39     if (!id2.equals(id1)) {
40       JavaCCErrors.parse_error(t, "Name " + id2 + " must be the same as that used at PARSER_BEGIN (" + id1 + ")");
41     }
42   }
43
44   static private java.util.Vector JavaDoc add_cu_token_here = cu_to_insertion_point_1;
45   static private Token first_cu_token;
46   static private boolean insertionpoint1set = false;
47   static private boolean insertionpoint2set = false;
48
49   static protected void setinsertionpoint(Token t, int no) {
50     do {
51       add_cu_token_here.addElement(first_cu_token);
52       first_cu_token = first_cu_token.next;
53     } while (first_cu_token != t);
54     if (no == 1) {
55       if (insertionpoint1set) {
56         JavaCCErrors.parse_error(t, "Multiple declaration of parser class.");
57       } else {
58         insertionpoint1set = true;
59         add_cu_token_here = cu_to_insertion_point_2;
60       }
61     } else {
62       add_cu_token_here = cu_from_insertion_point_2;
63       insertionpoint2set = true;
64     }
65     first_cu_token = t;
66   }
67
68   static protected void insertionpointerrors(Token t) {
69     while (first_cu_token != t) {
70       add_cu_token_here.addElement(first_cu_token);
71       first_cu_token = first_cu_token.next;
72     }
73     if (!insertionpoint1set || !insertionpoint2set) {
74       JavaCCErrors.parse_error(t, "Parser class has not been defined between PARSER_BEGIN and PARSER_END.");
75     }
76   }
77
78   static protected void set_initial_cu_token(Token t) {
79     first_cu_token = t;
80   }
81
82   static protected void addproduction(NormalProduction p) {
83     bnfproductions.addElement(p);
84   }
85
86   static protected void production_addexpansion(BNFProduction p, Expansion e) {
87     e.parent = p;
88     p.expansion = e;
89   }
90
91   static private int nextFreeLexState = 1;
92
93   static protected void addregexpr(TokenProduction p) {
94     Integer JavaDoc ii;
95     rexprlist.addElement(p);
96     if (Options.getUserTokenManager()) {
97       if (p.lexStates == null || p.lexStates.length != 1 || !p.lexStates[0].equals("DEFAULT")) {
98         JavaCCErrors.warning(p, "Ignoring lexical state specifications since option USER_TOKEN_MANAGER has been set to true.");
99       }
100     }
101     if (p.lexStates == null) {
102       return;
103     }
104     for (int i = 0; i < p.lexStates.length; i++) {
105       for (int j = 0; j < i; j++) {
106         if (p.lexStates[i].equals(p.lexStates[j])) {
107           JavaCCErrors.parse_error(p, "Multiple occurrence of \"" + p.lexStates[i] + "\" in lexical state list.");
108         }
109       }
110       if (lexstate_S2I.get(p.lexStates[i]) == null) {
111         ii = new Integer JavaDoc(nextFreeLexState++);
112         lexstate_S2I.put(p.lexStates[i], ii);
113         lexstate_I2S.put(ii, p.lexStates[i]);
114         simple_tokens_table.put(p.lexStates[i], new java.util.Hashtable JavaDoc());
115       }
116     }
117   }
118
119   static protected void add_token_manager_decls(Token t, java.util.Vector JavaDoc decls) {
120     if (token_mgr_decls != null) {
121       JavaCCErrors.parse_error(t, "Multiple occurrence of \"TOKEN_MGR_DECLS\".");
122     } else {
123       token_mgr_decls = decls;
124       if (Options.getUserTokenManager()) {
125         JavaCCErrors.warning(t, "Ignoring declarations in \"TOKEN_MGR_DECLS\" since option USER_TOKEN_MANAGER has been set to true.");
126       }
127     }
128   }
129
130   static protected void add_inline_regexpr(RegularExpression r) {
131     if (!(r instanceof REndOfFile)) {
132       TokenProduction p = new TokenProduction();
133       p.isExplicit = false;
134       p.lexStates = new String JavaDoc[1];
135       p.lexStates[0] = "DEFAULT";
136       p.kind = TokenProduction.TOKEN;
137       RegExprSpec res = new RegExprSpec();
138       res.rexp = r;
139       res.rexp.tpContext = p;
140       res.act = new Action();
141       res.nextState = null;
142       res.nsTok = null;
143       p.respecs.addElement(res);
144       rexprlist.addElement(p);
145     }
146   }
147
148   static protected boolean hexchar(char ch) {
149     if (ch >= '0' && ch <= '9') return true;
150     if (ch >= 'A' && ch <= 'F') return true;
151     if (ch >= 'a' && ch <= 'f') return true;
152     return false;
153   }
154
155   static protected int hexval(char ch) {
156     if (ch >= '0' && ch <= '9') return ((int)ch) - ((int)'0');
157     if (ch >= 'A' && ch <= 'F') return ((int)ch) - ((int)'A') + 10;
158     return ((int)ch) - ((int)'a') + 10;
159   }
160
161   static protected String JavaDoc remove_escapes_and_quotes(Token t, String JavaDoc str) {
162     String JavaDoc retval = "";
163     int index = 1;
164     char ch, ch1;
165     int ordinal;
166     while (index < str.length()-1) {
167       if (str.charAt(index) != '\\') {
168         retval += str.charAt(index); index++;
169         continue;
170       }
171       index++;
172       ch = str.charAt(index);
173       if (ch == 'b') {
174         retval += '\b'; index++;
175         continue;
176       }
177       if (ch == 't') {
178         retval += '\t'; index++;
179         continue;
180       }
181       if (ch == 'n') {
182         retval += '\n'; index++;
183         continue;
184       }
185       if (ch == 'f') {
186         retval += '\f'; index++;
187         continue;
188       }
189       if (ch == 'r') {
190         retval += '\r'; index++;
191         continue;
192       }
193       if (ch == '"') {
194         retval += '\"'; index++;
195         continue;
196       }
197       if (ch == '\'') {
198         retval += '\''; index++;
199         continue;
200       }
201       if (ch == '\\') {
202         retval += '\\'; index++;
203         continue;
204       }
205       if (ch >= '0' && ch <= '7') {
206         ordinal = ((int)ch) - ((int)'0'); index++;
207         ch1 = str.charAt(index);
208         if (ch1 >= '0' && ch1 <= '7') {
209           ordinal = ordinal*8 + ((int)ch1) - ((int)'0'); index++;
210           ch1 = str.charAt(index);
211           if (ch <= '3' && ch1 >= '0' && ch1 <= '7') {
212             ordinal = ordinal*8 + ((int)ch1) - ((int)'0'); index++;
213           }
214         }
215         retval += (char)ordinal;
216         continue;
217       }
218       if (ch == 'u') {
219         index++; ch = str.charAt(index);
220         if (hexchar(ch)) {
221           ordinal = hexval(ch);
222           index++; ch = str.charAt(index);
223           if (hexchar(ch)) {
224             ordinal = ordinal*16 + hexval(ch);
225             index++; ch = str.charAt(index);
226             if (hexchar(ch)) {
227               ordinal = ordinal*16 + hexval(ch);
228               index++; ch = str.charAt(index);
229               if (hexchar(ch)) {
230                 ordinal = ordinal*16 + hexval(ch);
231                 index++;
232                 continue;
233               }
234             }
235           }
236         }
237         JavaCCErrors.parse_error(t, "Encountered non-hex character '" + ch + "' at position " + index + " of string - Unicode escape must have 4 hex digits after it.");
238         return retval;
239       }
240       JavaCCErrors.parse_error(t, "Illegal escape sequence '\\" + ch + "' at position " + index + " of string.");
241       return retval;
242     }
243     return retval;
244   }
245
246   static protected char character_descriptor_assign(Token t, String JavaDoc s) {
247     if (s.length() != 1) {
248       JavaCCErrors.parse_error(t, "String in character list may contain only one character.");
249       return ' ';
250     } else {
251       return s.charAt(0);
252     }
253   }
254
255   static protected char character_descriptor_assign(Token t, String JavaDoc s, String JavaDoc left) {
256     if (s.length() != 1) {
257       JavaCCErrors.parse_error(t, "String in character list may contain only one character.");
258       return ' ';
259     } else if ((int)(left.charAt(0)) > (int)(s.charAt(0))) {
260       JavaCCErrors.parse_error(t, "Right end of character range \'" + s + "\' has a lower ordinal value than the left end of character range \'" + left + "\'.");
261       return left.charAt(0);
262     } else {
263       return s.charAt(0);
264     }
265   }
266
267   static protected void makeTryBlock(
268     Token tryLoc,
269     Container result,
270     Container nestedExp,
271     java.util.Vector JavaDoc types,
272     java.util.Vector JavaDoc ids,
273     java.util.Vector JavaDoc catchblks,
274     java.util.Vector JavaDoc finallyblk
275   )
276   {
277     if (catchblks.size() == 0 && finallyblk == null) {
278       JavaCCErrors.parse_error(tryLoc, "Try block must contain at least one catch or finally block.");
279       result = nestedExp;
280       return;
281     }
282     TryBlock tblk = new TryBlock();
283     tblk.line = tryLoc.beginLine;
284     tblk.column = tryLoc.beginColumn;
285     tblk.exp = (Expansion)(nestedExp.member);
286     tblk.exp.parent = tblk;
287     tblk.exp.ordinal = 0;
288     tblk.types = types;
289     tblk.ids = ids;
290     tblk.catchblks = catchblks;
291     tblk.finallyblk = finallyblk;
292     result.member = tblk;
293   }
294
295    public static void reInit()
296    {
297       add_cu_token_here = cu_to_insertion_point_1;
298       first_cu_token = null;
299       insertionpoint1set = false;
300       insertionpoint2set = false;
301       nextFreeLexState = 1;
302    }
303
304 }
305
Popular Tags