Debian Codegraph @

src/com/google/javascript/jscomp/parsing/JsDocTokenStream.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp.parsing;
18
19 import com.google.common.base.Preconditions;
20 import com.google.javascript.rhino.head.ScriptRuntime;
21
22 /**
23 * This class implements the scanner for JsDoc strings.
24 *
25 * It is heavily based on Rhino's TokenStream.
26 *
27 */
28 class JsDocTokenStream {
29 /*
30 * For chars - because we need something out-of-range
31 * to check. (And checking EOF by exception is annoying.)
32 * Note distinction from EOF token type!
33 */
34 private final static int
35 EOF_CHAR = -1;
36
37 JsDocTokenStream(String sourceString) {
38 this(sourceString, 0);
39 }
40
41 JsDocTokenStream(String sourceString, int lineno) {
42 this(sourceString, lineno, 0);
43 }
44
45 JsDocTokenStream(String sourceString, int lineno, int initCharno) {
46 Preconditions.checkNotNull(sourceString);
47 this.lineno = lineno;
48 this.sourceString = sourceString;
49 this.sourceEnd = sourceString.length();
50 this.sourceCursor = this.cursor = 0;
51 this.initLineno = lineno;
52 this.initCharno = initCharno;
53 }
54
55 /**
56 * Tokenizes JSDoc comments.
57 */
58 @SuppressWarnings("fallthrough")
59 final JsDocToken getJsDocToken() {
60 int c;
61 stringBufferTop = 0;
62 for (;;) {
63 // eat white spaces
64 for (;;) {
65 charno = -1;
66 c = getChar();
67 if (c == EOF_CHAR) {
68 return JsDocToken.EOF;
69 } else if (c == '\n') {
70 return JsDocToken.EOL;
71 } else if (!isJSSpace(c)) {
72 break;
73 }
74 }
75
76 switch (c) {
77 // annotation, e.g. @type or @constructor
78 case '@':
79 do {
80 c = getChar();
81 if (isAlpha(c)) {
82 addToString(c);
83 } else {
84 ungetChar(c);
85 this.string = getStringFromBuffer();
86 stringBufferTop = 0;
87 return JsDocToken.ANNOTATION;
88 }
89 } while (true);
90
91 case '*':
92 if (matchChar('/')) {
93 return JsDocToken.EOC;
94 } else {
95 return JsDocToken.STAR;
96 }
97
98 case ',':
99 return JsDocToken.COMMA;
100
101 case '>':
102 return JsDocToken.GT;
103
104 case '(':
105 return JsDocToken.LP;
106
107 case ')':
108 return JsDocToken.RP;
109
110 case '{':
111 return JsDocToken.LC;
112
113 case '}':
114 return JsDocToken.RC;
115
116 case '[':
117 return JsDocToken.LB;
118
119 case ']':
120 return JsDocToken.RB;
121
122 case '?':
123 return JsDocToken.QMARK;
124
125 case '!':
126 return JsDocToken.BANG;
127
128 case ':':
129 return JsDocToken.COLON;
130
131 case '=':
132 return JsDocToken.EQUALS;
133
134 case '|':
135 matchChar('|');
136 return JsDocToken.PIPE;
137
138 case '.':
139 c = getChar();
140 if (c == '<') {
141 return JsDocToken.LT;
142 } else {
143 if (c == '.') {
144 c = getChar();
145 if (c == '.') {
146 return JsDocToken.ELLIPSIS;
147 } else {
148 addToString('.');
149 }
150 }
151 // we may backtrack across line boundary
152 ungetBuffer[ungetCursor++] = c;
153 c = '.';
154 }
155 // fall through
156
157 default: {
158 // recognize a JsDoc string but discard last . if it is followed by
159 // a non-JsDoc comment char, e.g. Array.<
160 int c1 = c;
161 addToString(c);
162 int c2 = getChar();
163 if (!isJSDocString(c2)) {
164 ungetChar(c2);
165 this.string = getStringFromBuffer();
166 stringBufferTop = 0;
167 return JsDocToken.STRING;
168 } else {
169 do {
170 c1 = c2;
171 c2 = getChar();
172 if (c1 == '.' && c2 == '<') {
173 ungetChar(c2);
174 ungetChar(c1);
175 this.string = getStringFromBuffer();
176 stringBufferTop = 0;
177 return JsDocToken.STRING;
178 } else {
179 if (isJSDocString(c2)) {
180 addToString(c1);
181 } else {
182 ungetChar(c2);
183 addToString(c1);
184 this.string = getStringFromBuffer();
185 stringBufferTop = 0;
186 return JsDocToken.STRING;
187 }
188 }
189 } while (true);
190 }
191 }
192 }
193 }
194 }
195
196 /**
197 * Gets the remaining JSDoc line without the {@link JsDocToken#EOL},
198 * {@link JsDocToken#EOF} or {@link JsDocToken#EOC}.
199 */
200 @SuppressWarnings("fallthrough")
201 String getRemainingJSDocLine() {
202 int c;
203 for (;;) {
204 c = getChar();
205 switch (c) {
206 case '*':
207 if (peekChar() != '/') {
208 addToString(c);
209 break;
210 }
211 // fall through
212 case EOF_CHAR:
213 case '\n':
214 ungetChar(c);
215 this.string = getStringFromBuffer();
216 stringBufferTop = 0;
217 return this.string;
218
219 default:
220 addToString(c);
221 break;
222 }
223 }
224 }
225
226 final int getLineno() { return lineno; }
227
228 final int getCharno() {
229 return lineno == initLineno? initCharno + charno : charno;
230 }
231
232 final String getString() { return string; }
233
234 final boolean eof() { return hitEOF; }
235
236 private String getStringFromBuffer() {
237 tokenEnd = cursor;
238 return new String(stringBuffer, 0, stringBufferTop);
239 }
240
241 private void addToString(int c) {
242 int N = stringBufferTop;
243 if (N == stringBuffer.length) {
244 char[] tmp = new char[stringBuffer.length * 2];
245 System.arraycopy(stringBuffer, 0, tmp, 0, N);
246 stringBuffer = tmp;
247 }
248 stringBuffer[N] = (char)c;
249 stringBufferTop = N + 1;
250 }
251
252 void ungetChar(int c) {
253 // can not unread past across line boundary
254 assert(!(ungetCursor != 0 && ungetBuffer[ungetCursor - 1] == '\n'));
255 ungetBuffer[ungetCursor++] = c;
256 cursor--;
257 }
258
259 private boolean matchChar(int test) {
260 int c = getCharIgnoreLineEnd();
261 if (c == test) {
262 tokenEnd = cursor;
263 return true;
264 } else {
265 ungetCharIgnoreLineEnd(c);
266 return false;
267 }
268 }
269
270 private static boolean isAlpha(int c) {
271 // Use 'Z' < 'a'
272 if (c <= 'Z') {
273 return 'A' <= c;
274 } else {
275 return 'a' <= c && c <= 'z';
276 }
277 }
278
279 private boolean isJSDocString(int c) {
280 switch (c) {
281 case '@':
282 case '*':
283 case ',':
284 case '>':
285 case ':':
286 case '(':
287 case ')':
288 case '{':
289 case '}':
290 case '[':
291 case ']':
292 case '?':
293 case '!':
294 case '|':
295 case '=':
296 case EOF_CHAR:
297 case '\n':
298 return false;
299
300 default:
301 return !isJSSpace(c);
302 }
303 }
304
305 /* As defined in ECMA. jsscan.c uses C isspace() (which allows
306 * \v, I think.) note that code in getChar() implicitly accepts
307 * '\r' == \u000D as well.
308 */
309 static boolean isJSSpace(int c) {
310 if (c <= 127) {
311 return c == 0x20 || c == 0x9 || c == 0xC || c == 0xB;
312 } else {
313 return c == 0xA0
314 || Character.getType((char)c) == Character.SPACE_SEPARATOR;
315 }
316 }
317
318 private static boolean isJSFormatChar(int c) {
319 return c > 127 && Character.getType((char)c) == Character.FORMAT;
320 }
321
322 /**
323 * Allows the JSDocParser to update the character offset
324 * so that getCharno() returns a valid character position.
325 */
326 void update() {
327 charno = getOffset();
328 }
329
330 private int peekChar() {
331 int c = getChar();
332 ungetChar(c);
333 return c;
334 }
335
336 protected int getChar() {
337 if (ungetCursor != 0) {
338 cursor++;
339 --ungetCursor;
340 if (charno == -1) {
341 charno = getOffset();
342 }
343 return ungetBuffer[ungetCursor];
344 }
345
346 for(;;) {
347 int c;
348 if (sourceCursor == sourceEnd) {
349 hitEOF = true;
350 if (charno == -1) {
351 charno = getOffset();
352 }
353 return EOF_CHAR;
354 }
355 cursor++;
356 c = sourceString.charAt(sourceCursor++);
357
358
359 if (lineEndChar >= 0) {
360 if (lineEndChar == '\r' && c == '\n') {
361 lineEndChar = '\n';
362 continue;
363 }
364 lineEndChar = -1;
365 lineStart = sourceCursor - 1;
366 lineno++;
367 }
368
369 if (c <= 127) {
370 if (c == '\n' || c == '\r') {
371 lineEndChar = c;
372 c = '\n';
373 }
374 } else {
375 if (isJSFormatChar(c)) {
376 continue;
377 }
378 if (ScriptRuntime.isJSLineTerminator(c)) {
379 lineEndChar = c;
380 c = '\n';
381 }
382 }
383
384 if (charno == -1) {
385 charno = getOffset();
386 }
387
388 return c;
389 }
390 }
391
392 private int getCharIgnoreLineEnd() {
393 if (ungetCursor != 0) {
394 cursor++;
395 --ungetCursor;
396 if (charno == -1) {
397 charno = getOffset();
398 }
399 return ungetBuffer[ungetCursor];
400 }
401
402 for(;;) {
403 int c;
404 if (sourceCursor == sourceEnd) {
405 hitEOF = true;
406 if (charno == -1) {
407 charno = getOffset();
408 }
409 return EOF_CHAR;
410 }
411 cursor++;
412 c = sourceString.charAt(sourceCursor++);
413
414
415 if (c <= 127) {
416 if (c == '\n' || c == '\r') {
417 lineEndChar = c;
418 c = '\n';
419 }
420 } else {
421 if (isJSFormatChar(c)) {
422 continue;
423 }
424 if (ScriptRuntime.isJSLineTerminator(c)) {
425 lineEndChar = c;
426 c = '\n';
427 }
428 }
429
430 if (charno == -1) {
431 charno = getOffset();
432 }
433
434 return c;
435 }
436 }
437
438 private void ungetCharIgnoreLineEnd(int c) {
439 ungetBuffer[ungetCursor++] = c;
440 cursor--;
441 }
442
443 /**
444 * Returns the offset into the current line.
445 */
446 final int getOffset() {
447 return sourceCursor - lineStart - ungetCursor - 1;
448 }
449
450 // Set this to an initial non-null value so that the Parser has
451 // something to retrieve even if an error has occurred and no
452 // string is found. Fosters one class of error, but saves lots of
453 // code.
454 private String string = "";
455
456 private char[] stringBuffer = new char[128];
457 private int stringBufferTop;
458
459 // Room to backtrace from to < on failed match of the last - in <!--
460 private final int[] ungetBuffer = new int[3];
461 private int ungetCursor;
462
463 private boolean hitEOF = false;
464
465 private int lineStart = 0;
466 private int lineEndChar = -1;
467 int lineno;
468 private int charno = -1;
469 private int initCharno;
470 private int initLineno;
471
472 private String sourceString;
473 private int sourceEnd;
474
475 // sourceCursor is an index into a small buffer that keeps a
476 // sliding window of the source stream.
477 int sourceCursor;
478
479 // cursor is a monotonically increasing index into the original
480 // source stream, tracking exactly how far scanning has progressed.
481 // Its value is the index of the next character to be scanned.
482 int cursor;
483
484 // Record start and end positions of last scanned token.
485 int tokenBeg;
486 int tokenEnd;
487 }