Skip to content

Commit fd0791b

Browse files
committed
JS: Parse types from original source string
1 parent a3c7b63 commit fd0791b

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

javascript/extractor/src/com/semmle/js/parser/JSDocParser.java

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ private enum Token {
221221
};
222222

223223
private class TypeExpressionParser {
224-
String source;
225-
int length;
224+
int startIndex;
225+
int endIndex;
226226
int previous, index;
227227
Token token;
228228
Object value;
@@ -256,13 +256,15 @@ private SourceLocation loc() {
256256
}
257257

258258
private Position pos() {
259-
return new Position(1, index + 1, index);
259+
// TEMPORARY: Produce relative positions as the parser originally did
260+
return new Position(1, index + 1 - startIndex, index - startIndex);
260261
}
261262

262263
private <T extends JSDocTypeExpression> T finishNode(T node) {
263264
SourceLocation loc = node.getLoc();
264265
Position end = pos();
265-
loc.setSource(inputSubstring(loc.getStart().getOffset(), end.getOffset()));
266+
// TEMPORARY: Assume relative positions as the parser originally did
267+
loc.setSource(inputSubstring(loc.getStart().getOffset() + startIndex, end.getOffset() + startIndex));
266268
loc.setEnd(end);
267269
return node;
268270
}
@@ -283,7 +285,7 @@ private String scanHexEscape(char prefix) {
283285

284286
len = (prefix == 'u') ? 4 : 2;
285287
for (i = 0; i < len; ++i) {
286-
if (index < length && isHexDigit(source.charAt(index))) {
288+
if (index < endIndex && isHexDigit(source.charAt(index))) {
287289
ch = advance();
288290
code = code * 16 + "0123456789abcdef".indexOf(Character.toLowerCase(ch));
289291
} else {
@@ -300,7 +302,7 @@ private Token scanString() throws ParseError {
300302
quote = source.charAt(index);
301303
++index;
302304

303-
while (index < length) {
305+
while (index < endIndex) {
304306
ch = advance();
305307

306308
if (ch == quote) {
@@ -350,14 +352,14 @@ private Token scanString() throws ParseError {
350352
// octal = true;
351353
// }
352354

353-
if (index < length && isOctalDigit(source.charAt(index))) {
355+
if (index < endIndex && isOctalDigit(source.charAt(index))) {
354356
// TODO Review Removal octal = true;
355357
code = code * 8 + "01234567".indexOf(advance());
356358

357359
// 3 digits are only allowed when string starts
358360
// with 0, 1, 2, 3
359361
if ("0123".indexOf(ch) >= 0
360-
&& index < length
362+
&& index < endIndex
361363
&& isOctalDigit(source.charAt(index))) {
362364
code = code * 8 + "01234567".indexOf(advance());
363365
}
@@ -369,7 +371,7 @@ && isOctalDigit(source.charAt(index))) {
369371
break;
370372
}
371373
} else {
372-
if (ch == '\r' && index < length && source.charAt(index) == '\n') {
374+
if (ch == '\r' && index < endIndex && source.charAt(index) == '\n') {
373375
++index;
374376
}
375377
}
@@ -396,12 +398,12 @@ private Token scanNumber() throws ParseError {
396398
if (ch != '.') {
397399
int next = advance();
398400
number.append((char) next);
399-
ch = index < length ? source.charAt(index) : '\0';
401+
ch = index < endIndex ? source.charAt(index) : '\0';
400402

401403
if (next == '0') {
402404
if (ch == 'x' || ch == 'X') {
403405
number.append((char) advance());
404-
while (index < length) {
406+
while (index < endIndex) {
405407
ch = source.charAt(index);
406408
if (!isHexDigit(ch)) {
407409
break;
@@ -414,7 +416,7 @@ private Token scanNumber() throws ParseError {
414416
throwError("unexpected token");
415417
}
416418

417-
if (index < length) {
419+
if (index < endIndex) {
418420
ch = source.charAt(index);
419421
if (isIdentifierStart(ch)) {
420422
throwError("unexpected token");
@@ -431,15 +433,15 @@ private Token scanNumber() throws ParseError {
431433

432434
if (isOctalDigit(ch)) {
433435
number.append((char) advance());
434-
while (index < length) {
436+
while (index < endIndex) {
435437
ch = source.charAt(index);
436438
if (!isOctalDigit(ch)) {
437439
break;
438440
}
439441
number.append((char) advance());
440442
}
441443

442-
if (index < length) {
444+
if (index < endIndex) {
443445
ch = source.charAt(index);
444446
if (isIdentifierStart(ch) || isDecimalDigit(ch)) {
445447
throwError("unexpected token");
@@ -459,7 +461,7 @@ private Token scanNumber() throws ParseError {
459461
}
460462
}
461463

462-
while (index < length) {
464+
while (index < endIndex) {
463465
ch = source.charAt(index);
464466
if (!isDecimalDigit(ch)) {
465467
break;
@@ -471,7 +473,7 @@ private Token scanNumber() throws ParseError {
471473
if (ch == '.') {
472474
isFloat = true;
473475
number.append((char) advance());
474-
while (index < length) {
476+
while (index < endIndex) {
475477
ch = source.charAt(index);
476478
if (!isDecimalDigit(ch)) {
477479
break;
@@ -484,15 +486,15 @@ private Token scanNumber() throws ParseError {
484486
isFloat = true;
485487
number.append((char) advance());
486488

487-
ch = index < length ? source.charAt(index) : '\0';
489+
ch = index < endIndex ? source.charAt(index) : '\0';
488490
if (ch == '+' || ch == '-') {
489491
number.append((char) advance());
490492
}
491493

492-
ch = index < length ? source.charAt(index) : '\0';
494+
ch = index < endIndex ? source.charAt(index) : '\0';
493495
if (isDecimalDigit(ch)) {
494496
number.append((char) advance());
495-
while (index < length) {
497+
while (index < endIndex) {
496498
ch = source.charAt(index);
497499
if (!isDecimalDigit(ch)) {
498500
break;
@@ -504,7 +506,7 @@ private Token scanNumber() throws ParseError {
504506
}
505507
}
506508

507-
if (index < length) {
509+
if (index < endIndex) {
508510
ch = source.charAt(index);
509511
if (isIdentifierStart(ch)) {
510512
throwError("unexpected token");
@@ -526,10 +528,10 @@ private Token scanTypeName() {
526528
char ch, ch2;
527529

528530
value = new String(Character.toChars(advance()));
529-
while (index < length && isTypeName(source.charAt(index))) {
531+
while (index < endIndex && isTypeName(source.charAt(index))) {
530532
ch = source.charAt(index);
531533
if (ch == '.') {
532-
if ((index + 1) < length) {
534+
if ((index + 1) < endIndex) {
533535
ch2 = source.charAt(index + 1);
534536
if (ch2 == '<') {
535537
break;
@@ -546,10 +548,10 @@ private Token next() throws ParseError {
546548

547549
previous = index;
548550

549-
while (index < length && isWhiteSpace(source.charAt(index))) {
551+
while (index < endIndex && isWhiteSpace(source.charAt(index))) {
550552
advance();
551553
}
552-
if (index >= length) {
554+
if (index >= endIndex) {
553555
token = Token.EOF;
554556
return token;
555557
}
@@ -602,15 +604,15 @@ private Token next() throws ParseError {
602604

603605
case '.':
604606
advance();
605-
if (index < length) {
607+
if (index < endIndex) {
606608
ch = source.charAt(index);
607609
if (ch == '<') {
608610
advance();
609611
token = Token.DOT_LT;
610612
return token;
611613
}
612614

613-
if (ch == '.' && index + 1 < length && source.charAt(index + 1) == '.') {
615+
if (ch == '.' && index + 1 < endIndex && source.charAt(index + 1) == '.') {
614616
advance();
615617
advance();
616618
token = Token.REST;
@@ -1143,13 +1145,13 @@ private JSDocTypeExpression parseTopParamType() throws ParseError {
11431145
return expr;
11441146
}
11451147

1146-
private JSDocTypeExpression parseType(String src) throws ParseError {
1148+
private JSDocTypeExpression parseType(int startIndex, int endIndex) throws ParseError {
11471149
JSDocTypeExpression expr;
11481150

1149-
source = src;
1150-
length = source.length();
1151-
index = 0;
1152-
previous = 0;
1151+
this.startIndex = startIndex;
1152+
this.endIndex = endIndex;
1153+
index = startIndex;
1154+
previous = startIndex;
11531155

11541156
next();
11551157
expr = parseTop();
@@ -1161,13 +1163,13 @@ private JSDocTypeExpression parseType(String src) throws ParseError {
11611163
return expr;
11621164
}
11631165

1164-
private JSDocTypeExpression parseParamType(String src) throws ParseError {
1166+
private JSDocTypeExpression parseParamType(int startIndex, int endIndex) throws ParseError {
11651167
JSDocTypeExpression expr;
11661168

1167-
source = src;
1168-
length = source.length();
1169-
index = 0;
1170-
previous = 0;
1169+
this.startIndex = startIndex;
1170+
this.endIndex = endIndex;
1171+
index = startIndex;
1172+
previous = startIndex;
11711173

11721174
next();
11731175
expr = parseTopParamType();
@@ -1299,14 +1301,11 @@ private JSDocTypeExpression parseType(String title, int last) throws ParseError
12991301
return throwError("Braces are not balanced");
13001302
}
13011303

1302-
// Get the type as a string, ignoring the last '}'
1303-
String type = source.substring(startIndex, index - 1);
1304-
13051304
try {
13061305
if (isParamTitle(title)) {
1307-
return typed.parseParamType(type);
1306+
return typed.parseParamType(startIndex, index - 1);
13081307
}
1309-
return typed.parseType(type);
1308+
return typed.parseType(startIndex, index - 1);
13101309
} catch (ParseError e) {
13111310
// parse failed
13121311
return null;

0 commit comments

Comments
 (0)