@@ -24,7 +24,9 @@ module ts {
24
24
reScanSlashToken ( ) : SyntaxKind ;
25
25
reScanTemplateToken ( ) : SyntaxKind ;
26
26
scan ( ) : SyntaxKind ;
27
- setText ( text : string ) : void ;
27
+ // Sets the text for the scanner to scan. An optional subrange starting point and length
28
+ // can be provided to have the scanner only scan a portion of the text.
29
+ setText ( text : string , start ?: number , length ?: number ) : void ;
28
30
setOnError ( onError : ErrorCallback ) : void ;
29
31
setScriptTarget ( scriptTarget : ScriptTarget ) : void ;
30
32
setTextPos ( textPos : number ) : void ;
@@ -597,11 +599,11 @@ module ts {
597
599
ch > CharacterCodes . maxAsciiCharacter && isUnicodeIdentifierPart ( ch , languageVersion ) ;
598
600
}
599
601
600
- /* @internal */
601
- export function createScanner ( languageVersion : ScriptTarget , skipTrivia : boolean , text ?: string , onError ?: ErrorCallback ) : Scanner {
602
+ // Creates a scanner over a (possibly unspecified) range of a piece of text.
603
+ /* @internal */
604
+ export function createScanner ( languageVersion : ScriptTarget , skipTrivia : boolean , text ?: string , onError ?: ErrorCallback , start ?: number , length ?: number ) : Scanner {
602
605
let pos : number ; // Current position (end position of text of current token)
603
- let len : number ; // Length of text
604
-
606
+ let end : number ; // end of text
605
607
let startPos : number ; // Start position of whitespace before current token
606
608
let tokenPos : number ; // Start position of text of current token
607
609
let token : SyntaxKind ;
@@ -610,7 +612,7 @@ module ts {
610
612
let hasExtendedUnicodeEscape : boolean ;
611
613
let tokenIsUnterminated : boolean ;
612
614
613
- setText ( text ) ;
615
+ setText ( text , start , length ) ;
614
616
615
617
return {
616
618
getStartPos : ( ) => startPos ,
@@ -732,7 +734,7 @@ module ts {
732
734
let result = "" ;
733
735
let start = pos ;
734
736
while ( true ) {
735
- if ( pos >= len ) {
737
+ if ( pos >= end ) {
736
738
result += text . substring ( start , pos ) ;
737
739
tokenIsUnterminated = true ;
738
740
error ( Diagnostics . Unterminated_string_literal ) ;
@@ -774,7 +776,7 @@ module ts {
774
776
let resultingToken : SyntaxKind ;
775
777
776
778
while ( true ) {
777
- if ( pos >= len ) {
779
+ if ( pos >= end ) {
778
780
contents += text . substring ( start , pos ) ;
779
781
tokenIsUnterminated = true ;
780
782
error ( Diagnostics . Unterminated_template_literal ) ;
@@ -793,7 +795,7 @@ module ts {
793
795
}
794
796
795
797
// '${'
796
- if ( currChar === CharacterCodes . $ && pos + 1 < len && text . charCodeAt ( pos + 1 ) === CharacterCodes . openBrace ) {
798
+ if ( currChar === CharacterCodes . $ && pos + 1 < end && text . charCodeAt ( pos + 1 ) === CharacterCodes . openBrace ) {
797
799
contents += text . substring ( start , pos ) ;
798
800
pos += 2 ;
799
801
resultingToken = startedWithBacktick ? SyntaxKind . TemplateHead : SyntaxKind . TemplateMiddle ;
@@ -814,7 +816,7 @@ module ts {
814
816
contents += text . substring ( start , pos ) ;
815
817
pos ++ ;
816
818
817
- if ( pos < len && text . charCodeAt ( pos ) === CharacterCodes . lineFeed ) {
819
+ if ( pos < end && text . charCodeAt ( pos ) === CharacterCodes . lineFeed ) {
818
820
pos ++ ;
819
821
}
820
822
@@ -834,7 +836,7 @@ module ts {
834
836
835
837
function scanEscapeSequence ( ) : string {
836
838
pos ++ ;
837
- if ( pos >= len ) {
839
+ if ( pos >= end ) {
838
840
error ( Diagnostics . Unexpected_end_of_text ) ;
839
841
return "" ;
840
842
}
@@ -860,7 +862,7 @@ module ts {
860
862
return "\"" ;
861
863
case CharacterCodes . u :
862
864
// '\u{DDDDDDDD}'
863
- if ( pos < len && text . charCodeAt ( pos ) === CharacterCodes . openBrace ) {
865
+ if ( pos < end && text . charCodeAt ( pos ) === CharacterCodes . openBrace ) {
864
866
hasExtendedUnicodeEscape = true ;
865
867
pos ++ ;
866
868
return scanExtendedUnicodeEscape ( ) ;
@@ -876,7 +878,7 @@ module ts {
876
878
// when encountering a LineContinuation (i.e. a backslash and a line terminator sequence),
877
879
// the line terminator is interpreted to be "the empty code unit sequence".
878
880
case CharacterCodes . carriageReturn :
879
- if ( pos < len && text . charCodeAt ( pos ) === CharacterCodes . lineFeed ) {
881
+ if ( pos < end && text . charCodeAt ( pos ) === CharacterCodes . lineFeed ) {
880
882
pos ++ ;
881
883
}
882
884
// fall through
@@ -915,7 +917,7 @@ module ts {
915
917
isInvalidExtendedEscape = true ;
916
918
}
917
919
918
- if ( pos >= len ) {
920
+ if ( pos >= end ) {
919
921
error ( Diagnostics . Unexpected_end_of_text ) ;
920
922
isInvalidExtendedEscape = true ;
921
923
}
@@ -952,7 +954,7 @@ module ts {
952
954
// Current character is known to be a backslash. Check for Unicode escape of the form '\uXXXX'
953
955
// and return code point value if valid Unicode escape is found. Otherwise return -1.
954
956
function peekUnicodeEscape ( ) : number {
955
- if ( pos + 5 < len && text . charCodeAt ( pos + 1 ) === CharacterCodes . u ) {
957
+ if ( pos + 5 < end && text . charCodeAt ( pos + 1 ) === CharacterCodes . u ) {
956
958
let start = pos ;
957
959
pos += 2 ;
958
960
let value = scanExactNumberOfHexDigits ( 4 ) ;
@@ -965,7 +967,7 @@ module ts {
965
967
function scanIdentifierParts ( ) : string {
966
968
let result = "" ;
967
969
let start = pos ;
968
- while ( pos < len ) {
970
+ while ( pos < end ) {
969
971
let ch = text . charCodeAt ( pos ) ;
970
972
if ( isIdentifierPart ( ch ) ) {
971
973
pos ++ ;
@@ -1032,7 +1034,7 @@ module ts {
1032
1034
tokenIsUnterminated = false ;
1033
1035
while ( true ) {
1034
1036
tokenPos = pos ;
1035
- if ( pos >= len ) {
1037
+ if ( pos >= end ) {
1036
1038
return token = SyntaxKind . EndOfFileToken ;
1037
1039
}
1038
1040
let ch = text . charCodeAt ( pos ) ;
@@ -1045,7 +1047,7 @@ module ts {
1045
1047
continue ;
1046
1048
}
1047
1049
else {
1048
- if ( ch === CharacterCodes . carriageReturn && pos + 1 < len && text . charCodeAt ( pos + 1 ) === CharacterCodes . lineFeed ) {
1050
+ if ( ch === CharacterCodes . carriageReturn && pos + 1 < end && text . charCodeAt ( pos + 1 ) === CharacterCodes . lineFeed ) {
1049
1051
// consume both CR and LF
1050
1052
pos += 2 ;
1051
1053
}
@@ -1063,7 +1065,7 @@ module ts {
1063
1065
continue ;
1064
1066
}
1065
1067
else {
1066
- while ( pos < len && isWhiteSpace ( text . charCodeAt ( pos ) ) ) {
1068
+ while ( pos < end && isWhiteSpace ( text . charCodeAt ( pos ) ) ) {
1067
1069
pos ++ ;
1068
1070
}
1069
1071
return token = SyntaxKind . WhitespaceTrivia ;
@@ -1136,7 +1138,7 @@ module ts {
1136
1138
if ( text . charCodeAt ( pos + 1 ) === CharacterCodes . slash ) {
1137
1139
pos += 2 ;
1138
1140
1139
- while ( pos < len ) {
1141
+ while ( pos < end ) {
1140
1142
if ( isLineBreak ( text . charCodeAt ( pos ) ) ) {
1141
1143
break ;
1142
1144
}
@@ -1156,7 +1158,7 @@ module ts {
1156
1158
pos += 2 ;
1157
1159
1158
1160
let commentClosed = false ;
1159
- while ( pos < len ) {
1161
+ while ( pos < end ) {
1160
1162
let ch = text . charCodeAt ( pos ) ;
1161
1163
1162
1164
if ( ch === CharacterCodes . asterisk && text . charCodeAt ( pos + 1 ) === CharacterCodes . slash ) {
@@ -1191,7 +1193,7 @@ module ts {
1191
1193
return pos ++ , token = SyntaxKind . SlashToken ;
1192
1194
1193
1195
case CharacterCodes . _0 :
1194
- if ( pos + 2 < len && ( text . charCodeAt ( pos + 1 ) === CharacterCodes . X || text . charCodeAt ( pos + 1 ) === CharacterCodes . x ) ) {
1196
+ if ( pos + 2 < end && ( text . charCodeAt ( pos + 1 ) === CharacterCodes . X || text . charCodeAt ( pos + 1 ) === CharacterCodes . x ) ) {
1195
1197
pos += 2 ;
1196
1198
let value = scanMinimumNumberOfHexDigits ( 1 ) ;
1197
1199
if ( value < 0 ) {
@@ -1201,7 +1203,7 @@ module ts {
1201
1203
tokenValue = "" + value ;
1202
1204
return token = SyntaxKind . NumericLiteral ;
1203
1205
}
1204
- else if ( pos + 2 < len && ( text . charCodeAt ( pos + 1 ) === CharacterCodes . B || text . charCodeAt ( pos + 1 ) === CharacterCodes . b ) ) {
1206
+ else if ( pos + 2 < end && ( text . charCodeAt ( pos + 1 ) === CharacterCodes . B || text . charCodeAt ( pos + 1 ) === CharacterCodes . b ) ) {
1205
1207
pos += 2 ;
1206
1208
let value = scanBinaryOrOctalDigits ( /* base */ 2 ) ;
1207
1209
if ( value < 0 ) {
@@ -1211,7 +1213,7 @@ module ts {
1211
1213
tokenValue = "" + value ;
1212
1214
return token = SyntaxKind . NumericLiteral ;
1213
1215
}
1214
- else if ( pos + 2 < len && ( text . charCodeAt ( pos + 1 ) === CharacterCodes . O || text . charCodeAt ( pos + 1 ) === CharacterCodes . o ) ) {
1216
+ else if ( pos + 2 < end && ( text . charCodeAt ( pos + 1 ) === CharacterCodes . O || text . charCodeAt ( pos + 1 ) === CharacterCodes . o ) ) {
1215
1217
pos += 2 ;
1216
1218
let value = scanBinaryOrOctalDigits ( /* base */ 8 ) ;
1217
1219
if ( value < 0 ) {
@@ -1222,7 +1224,7 @@ module ts {
1222
1224
return token = SyntaxKind . NumericLiteral ;
1223
1225
}
1224
1226
// Try to parse as an octal
1225
- if ( pos + 1 < len && isOctalDigit ( text . charCodeAt ( pos + 1 ) ) ) {
1227
+ if ( pos + 1 < end && isOctalDigit ( text . charCodeAt ( pos + 1 ) ) ) {
1226
1228
tokenValue = "" + scanOctalDigits ( ) ;
1227
1229
return token = SyntaxKind . NumericLiteral ;
1228
1230
}
@@ -1337,7 +1339,7 @@ module ts {
1337
1339
default :
1338
1340
if ( isIdentifierStart ( ch ) ) {
1339
1341
pos ++ ;
1340
- while ( pos < len && isIdentifierPart ( ch = text . charCodeAt ( pos ) ) ) pos ++ ;
1342
+ while ( pos < end && isIdentifierPart ( ch = text . charCodeAt ( pos ) ) ) pos ++ ;
1341
1343
tokenValue = text . substring ( tokenPos , pos ) ;
1342
1344
if ( ch === CharacterCodes . backslash ) {
1343
1345
tokenValue += scanIdentifierParts ( ) ;
@@ -1388,7 +1390,7 @@ module ts {
1388
1390
while ( true ) {
1389
1391
// If we reach the end of a file, or hit a newline, then this is an unterminated
1390
1392
// regex. Report error and return what we have so far.
1391
- if ( p >= len ) {
1393
+ if ( p >= end ) {
1392
1394
tokenIsUnterminated = true ;
1393
1395
error ( Diagnostics . Unterminated_regular_expression_literal )
1394
1396
break ;
@@ -1424,7 +1426,7 @@ module ts {
1424
1426
p ++ ;
1425
1427
}
1426
1428
1427
- while ( p < len && isIdentifierPart ( text . charCodeAt ( p ) ) ) {
1429
+ while ( p < end && isIdentifierPart ( text . charCodeAt ( p ) ) ) {
1428
1430
p ++ ;
1429
1431
}
1430
1432
pos = p ;
@@ -1473,10 +1475,10 @@ module ts {
1473
1475
return speculationHelper ( callback , /*isLookahead:*/ false ) ;
1474
1476
}
1475
1477
1476
- function setText ( newText : string ) {
1478
+ function setText ( newText : string , start : number , length : number ) {
1477
1479
text = newText || "" ;
1478
- len = text . length ;
1479
- setTextPos ( 0 ) ;
1480
+ end = length === undefined ? text . length : start + length ;
1481
+ setTextPos ( start || 0 ) ;
1480
1482
}
1481
1483
1482
1484
function setOnError ( errorCallback : ErrorCallback ) {
@@ -1488,6 +1490,7 @@ module ts {
1488
1490
}
1489
1491
1490
1492
function setTextPos ( textPos : number ) {
1493
+ Debug . assert ( textPos >= 0 ) ;
1491
1494
pos = textPos ;
1492
1495
startPos = textPos ;
1493
1496
tokenPos = textPos ;
0 commit comments