KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > jjtree > TokenUtils


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.jjtree;
24
25 import org.javacc.parser.JavaCCErrors;
26
27 public class TokenUtils
28 {
29   static void print(Token t, IO io, String JavaDoc in, String JavaDoc out) {
30     Token tt = t.specialToken;
31     if (tt != null) {
32       while (tt.specialToken != null) tt = tt.specialToken;
33       while (tt != null) {
34     io.print(addUnicodeEscapes(tt.image));
35     tt = tt.next;
36       }
37     }
38     String JavaDoc i = t.image;
39     if (in != null && i.equals(in)) {
40       i = out;
41     }
42     io.print(addUnicodeEscapes(i));
43   }
44
45   static void print(Token t, IO io) {
46     print(t, io, null, null);
47   }
48
49   static String JavaDoc addUnicodeEscapes(String JavaDoc str) {
50     String JavaDoc retval = "";
51     char ch;
52     for (int i = 0; i < str.length(); i++) {
53       ch = str.charAt(i);
54       if ((ch < 0x20 || ch > 0x7e) && ch != '\t' && ch != '\n' && ch != '\r' && ch != '\f') {
55     String JavaDoc s = "0000" + Integer.toString(ch, 16);
56     retval += "\\u" + s.substring(s.length() - 4, s.length());
57       } else {
58     retval += ch;
59       }
60     }
61     return retval;
62   }
63
64
65   static boolean hasTokens(SimpleNode n)
66   {
67     if (n.getLastToken().next == n.getFirstToken()) {
68       return false;
69     } else {
70       return true;
71     }
72   }
73
74   static String JavaDoc remove_escapes_and_quotes(Token t, String JavaDoc str) {
75     String JavaDoc retval = "";
76     int index = 1;
77     char ch, ch1;
78     int ordinal;
79     while (index < str.length()-1) {
80       if (str.charAt(index) != '\\') {
81         retval += str.charAt(index); index++;
82         continue;
83       }
84       index++;
85       ch = str.charAt(index);
86       if (ch == 'b') {
87         retval += '\b'; index++;
88         continue;
89       }
90       if (ch == 't') {
91         retval += '\t'; index++;
92         continue;
93       }
94       if (ch == 'n') {
95         retval += '\n'; index++;
96         continue;
97       }
98       if (ch == 'f') {
99         retval += '\f'; index++;
100         continue;
101       }
102       if (ch == 'r') {
103         retval += '\r'; index++;
104         continue;
105       }
106       if (ch == '"') {
107         retval += '\"'; index++;
108         continue;
109       }
110       if (ch == '\'') {
111         retval += '\''; index++;
112         continue;
113       }
114       if (ch == '\\') {
115         retval += '\\'; index++;
116         continue;
117       }
118       if (ch >= '0' && ch <= '7') {
119         ordinal = ((int)ch) - ((int)'0'); index++;
120         ch1 = str.charAt(index);
121         if (ch1 >= '0' && ch1 <= '7') {
122           ordinal = ordinal*8 + ((int)ch1) - ((int)'0'); index++;
123           ch1 = str.charAt(index);
124           if (ch <= '3' && ch1 >= '0' && ch1 <= '7') {
125             ordinal = ordinal*8 + ((int)ch1) - ((int)'0'); index++;
126           }
127         }
128         retval += (char)ordinal;
129         continue;
130       }
131       if (ch == 'u') {
132         index++; ch = str.charAt(index);
133         if (hexchar(ch)) {
134           ordinal = hexval(ch);
135           index++; ch = str.charAt(index);
136           if (hexchar(ch)) {
137             ordinal = ordinal*16 + hexval(ch);
138             index++; ch = str.charAt(index);
139             if (hexchar(ch)) {
140               ordinal = ordinal*16 + hexval(ch);
141               index++; ch = str.charAt(index);
142               if (hexchar(ch)) {
143                 ordinal = ordinal*16 + hexval(ch);
144                 index++;
145                 continue;
146               }
147             }
148           }
149         }
150         JavaCCErrors.parse_error(t, "Encountered non-hex character '" + ch + "' at position " + index + " of string - Unicode escape must have 4 hex digits after it.");
151         return retval;
152       }
153       JavaCCErrors.parse_error(t, "Illegal escape sequence '\\" + ch + "' at position " + index + " of string.");
154       return retval;
155     }
156     return retval;
157   }
158
159   private static boolean hexchar(char ch) {
160     if (ch >= '0' && ch <= '9') return true;
161     if (ch >= 'A' && ch <= 'F') return true;
162     if (ch >= 'a' && ch <= 'f') return true;
163     return false;
164   }
165
166   private static int hexval(char ch) {
167     if (ch >= '0' && ch <= '9') return ((int)ch) - ((int)'0');
168     if (ch >= 'A' && ch <= 'F') return ((int)ch) - ((int)'A') + 10;
169     return ((int)ch) - ((int)'a') + 10;
170   }
171
172 }
173
174 /*end*/
175
Popular Tags