Debian Codegraph @

src/com/google/javascript/rhino/TokenStream.java

raw symbols SCIP
1 /*
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Rhino code, released
17 * May 6, 1999.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1997-1999
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * Roger Lawrence
26 * Mike McCabe
27 * Igor Bukanov
28 * Ethan Hugg
29 * Bob Jervis
30 * Terry Lucas
31 * Milen Nankov
32 * Pascal-Louis Perez
33 *
34 * Alternatively, the contents of this file may be used under the terms of
35 * the GNU General Public License Version 2 or later (the "GPL"), in which
36 * case the provisions of the GPL are applicable instead of those above. If
37 * you wish to allow use of your version of this file only under the terms of
38 * the GPL and not to allow others to use your version of this file under the
39 * MPL, indicate your decision by deleting the provisions above and replacing
40 * them with the notice and other provisions required by the GPL. If you do
41 * not delete the provisions above, a recipient may use your version of this
42 * file under either the MPL or the GPL.
43 *
44 * ***** END LICENSE BLOCK ***** */
45
46 package com.google.javascript.rhino;
47
48 /**
49 * This class implements the JavaScript scanner.
50 *
51 * It is based on the C source files jsscan.c and jsscan.h
52 * in the jsref package.
53 *
54 */
55
56 public class TokenStream {
57 public static boolean isKeyword(String name) {
58 boolean id = false;
59 String s = name;
60 complete: {
61 String X = null;
62 int c;
63 partial: switch (s.length()) {
64 case 2: c=s.charAt(1);
65 if (c=='f') {
66 if (s.charAt(0)=='i') {id=true; break complete;}
67 } else if (c=='n') {
68 if (s.charAt(0)=='i') {id=true; break complete;}
69 } else if (c=='o') {
70 if (s.charAt(0)=='d') {id=true; break complete;}
71 }
72 break partial;
73 case 3: switch (s.charAt(0)) {
74 case 'f':
75 if (s.charAt(2)=='r' && s.charAt(1)=='o') {
76 id=true; break complete;
77 } break partial;
78 case 'i':
79 if (s.charAt(2)=='t' && s.charAt(1)=='n') {
80 id=true; break complete;
81 } break partial;
82 case 'n':
83 if (s.charAt(2)=='w' && s.charAt(1)=='e') {
84 id=true; break complete;
85 } break partial;
86 case 't':
87 if (s.charAt(2)=='y' && s.charAt(1)=='r') {
88 id=true; break complete;
89 } break partial;
90 case 'v':
91 if (s.charAt(2)=='r' && s.charAt(1)=='a') {
92 id=true; break complete;
93 } break partial;
94 } break partial;
95 case 4: switch (s.charAt(0)) {
96 case 'b': X="byte";id=true; break partial;
97 case 'c': c=s.charAt(3);
98 if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='a') {
99 id=true; break complete;} }
100 else if (c=='r') {
101 if (s.charAt(2)=='a' && s.charAt(1)=='h') {
102 id=true; break complete;
103 }
104 }
105 break partial;
106 case 'e': c=s.charAt(3);
107 if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='l') {
108 id=true; break complete;} }
109 else if (c=='m') {
110 if (s.charAt(2)=='u' && s.charAt(1)=='n') {
111 id=true; break complete;} }
112 break partial;
113 case 'g': X="goto";id=true; break partial;
114 case 'l': X="long";id=true; break partial;
115 case 'n': X="null";id=true; break partial;
116 case 't': c=s.charAt(3);
117 if (c=='e') { if (s.charAt(2)=='u' && s.charAt(1)=='r') {
118 id=true; break complete;} }
119 else if (c=='s') {
120 if (s.charAt(2)=='i' && s.charAt(1)=='h') {
121 id=true; break complete;} }
122 break partial;
123 case 'v': X="void";id=true; break partial;
124 case 'w': X="with";id=true; break partial;
125 } break partial;
126 case 5: switch (s.charAt(2)) {
127 case 'a': X="class";id=true; break partial;
128 case 'e': X="break";id=true; break partial;
129 case 'i': X="while";id=true; break partial;
130 case 'l': X="false";id=true; break partial;
131 case 'n': c=s.charAt(0);
132 if (c=='c') { X="const";id=true; }
133 else if (c=='f') { X="final";id=true; }
134 break partial;
135 case 'o': c=s.charAt(0);
136 if (c=='f') { X="float";id=true; }
137 else if (c=='s') { X="short";id=true; }
138 break partial;
139 case 'p': X="super";id=true; break partial;
140 case 'r': X="throw";id=true; break partial;
141 case 't': X="catch";id=true; break partial;
142 } break partial;
143 case 6: switch (s.charAt(1)) {
144 case 'a': X="native";id=true; break partial;
145 case 'e': c=s.charAt(0);
146 if (c=='d') { X="delete";id=true; }
147 else if (c=='r') { X="return";id=true; }
148 break partial;
149 case 'h': X="throws";id=true; break partial;
150 case 'm': X="import";id=true; break partial;
151 case 'o': X="double";id=true; break partial;
152 case 't': X="static";id=true; break partial;
153 case 'u': X="public";id=true; break partial;
154 case 'w': X="switch";id=true; break partial;
155 case 'x': X="export";id=true; break partial;
156 case 'y': X="typeof";id=true; break partial;
157 } break partial;
158 case 7: switch (s.charAt(1)) {
159 case 'a': X="package";id=true; break partial;
160 case 'e': X="default";id=true; break partial;
161 case 'i': X="finally";id=true; break partial;
162 case 'o': X="boolean";id=true; break partial;
163 case 'r': X="private";id=true; break partial;
164 case 'x': X="extends";id=true; break partial;
165 } break partial;
166 case 8: switch (s.charAt(0)) {
167 case 'a': X="abstract";id=true; break partial;
168 case 'c': X="continue";id=true; break partial;
169 case 'd': X="debugger";id=true; break partial;
170 case 'f': X="function";id=true; break partial;
171 case 'v': X="volatile";id=true; break partial;
172 } break partial;
173 case 9: c=s.charAt(0);
174 if (c=='i') { X="interface";id=true; }
175 else if (c=='p') { X="protected";id=true; }
176 else if (c=='t') { X="transient";id=true; }
177 break partial;
178 case 10: c=s.charAt(1);
179 if (c=='m') { X="implements";id=true; }
180 else if (c=='n') { X="instanceof";id=true; }
181 break partial;
182 case 12: X="synchronized";id=true; break partial;
183 }
184 // partial match validate the entire string the one possibility
185 if (X!=null && X!=s && !X.equals(s)) return false;
186 }
187 return id;
188 }
189
190 public static boolean isJSIdentifier(String s) {
191 int length = s.length();
192
193 if (length == 0 ||
194 Character.isIdentifierIgnorable(s.charAt(0)) ||
195 !Character.isJavaIdentifierStart(s.charAt(0))) {
196 return false;
197 }
198
199 for (int i = 1; i < length; i++) {
200 if (Character.isIdentifierIgnorable(s.charAt(i)) ||
201 !Character.isJavaIdentifierPart(s.charAt(i))) {
202 return false;
203 }
204 }
205
206 return true;
207 }
208 }