@@ -24,7 +24,9 @@ module ts {
2424 reScanSlashToken ( ) : SyntaxKind ;
2525 reScanTemplateToken ( ) : SyntaxKind ;
2626 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 ;
2830 setOnError ( onError : ErrorCallback ) : void ;
2931 setScriptTarget ( scriptTarget : ScriptTarget ) : void ;
3032 setTextPos ( textPos : number ) : void ;
@@ -597,11 +599,11 @@ module ts {
597599 ch > CharacterCodes . maxAsciiCharacter && isUnicodeIdentifierPart ( ch , languageVersion ) ;
598600 }
599601
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 {
602605 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
605607 let startPos : number ; // Start position of whitespace before current token
606608 let tokenPos : number ; // Start position of text of current token
607609 let token : SyntaxKind ;
@@ -610,7 +612,7 @@ module ts {
610612 let hasExtendedUnicodeEscape : boolean ;
611613 let tokenIsUnterminated : boolean ;
612614
613- setText ( text ) ;
615+ setText ( text , start , length ) ;
614616
615617 return {
616618 getStartPos : ( ) => startPos ,
@@ -732,7 +734,7 @@ module ts {
732734 let result = "" ;
733735 let start = pos ;
734736 while ( true ) {
735- if ( pos >= len ) {
737+ if ( pos >= end ) {
736738 result += text . substring ( start , pos ) ;
737739 tokenIsUnterminated = true ;
738740 error ( Diagnostics . Unterminated_string_literal ) ;
@@ -774,7 +776,7 @@ module ts {
774776 let resultingToken : SyntaxKind ;
775777
776778 while ( true ) {
777- if ( pos >= len ) {
779+ if ( pos >= end ) {
778780 contents += text . substring ( start , pos ) ;
779781 tokenIsUnterminated = true ;
780782 error ( Diagnostics . Unterminated_template_literal ) ;
@@ -793,7 +795,7 @@ module ts {
793795 }
794796
795797 // '${'
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 ) {
797799 contents += text . substring ( start , pos ) ;
798800 pos += 2 ;
799801 resultingToken = startedWithBacktick ? SyntaxKind . TemplateHead : SyntaxKind . TemplateMiddle ;
@@ -814,7 +816,7 @@ module ts {
814816 contents += text . substring ( start , pos ) ;
815817 pos ++ ;
816818
817- if ( pos < len && text . charCodeAt ( pos ) === CharacterCodes . lineFeed ) {
819+ if ( pos < end && text . charCodeAt ( pos ) === CharacterCodes . lineFeed ) {
818820 pos ++ ;
819821 }
820822
@@ -834,7 +836,7 @@ module ts {
834836
835837 function scanEscapeSequence ( ) : string {
836838 pos ++ ;
837- if ( pos >= len ) {
839+ if ( pos >= end ) {
838840 error ( Diagnostics . Unexpected_end_of_text ) ;
839841 return "" ;
840842 }
@@ -860,7 +862,7 @@ module ts {
860862 return "\"" ;
861863 case CharacterCodes . u :
862864 // '\u{DDDDDDDD}'
863- if ( pos < len && text . charCodeAt ( pos ) === CharacterCodes . openBrace ) {
865+ if ( pos < end && text . charCodeAt ( pos ) === CharacterCodes . openBrace ) {
864866 hasExtendedUnicodeEscape = true ;
865867 pos ++ ;
866868 return scanExtendedUnicodeEscape ( ) ;
@@ -876,7 +878,7 @@ module ts {
876878 // when encountering a LineContinuation (i.e. a backslash and a line terminator sequence),
877879 // the line terminator is interpreted to be "the empty code unit sequence".
878880 case CharacterCodes . carriageReturn :
879- if ( pos < len && text . charCodeAt ( pos ) === CharacterCodes . lineFeed ) {
881+ if ( pos < end && text . charCodeAt ( pos ) === CharacterCodes . lineFeed ) {
880882 pos ++ ;
881883 }
882884 // fall through
@@ -915,7 +917,7 @@ module ts {
915917 isInvalidExtendedEscape = true ;
916918 }
917919
918- if ( pos >= len ) {
920+ if ( pos >= end ) {
919921 error ( Diagnostics . Unexpected_end_of_text ) ;
920922 isInvalidExtendedEscape = true ;
921923 }
@@ -952,7 +954,7 @@ module ts {
952954 // Current character is known to be a backslash. Check for Unicode escape of the form '\uXXXX'
953955 // and return code point value if valid Unicode escape is found. Otherwise return -1.
954956 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 ) {
956958 let start = pos ;
957959 pos += 2 ;
958960 let value = scanExactNumberOfHexDigits ( 4 ) ;
@@ -965,7 +967,7 @@ module ts {
965967 function scanIdentifierParts ( ) : string {
966968 let result = "" ;
967969 let start = pos ;
968- while ( pos < len ) {
970+ while ( pos < end ) {
969971 let ch = text . charCodeAt ( pos ) ;
970972 if ( isIdentifierPart ( ch ) ) {
971973 pos ++ ;
@@ -1032,7 +1034,7 @@ module ts {
10321034 tokenIsUnterminated = false ;
10331035 while ( true ) {
10341036 tokenPos = pos ;
1035- if ( pos >= len ) {
1037+ if ( pos >= end ) {
10361038 return token = SyntaxKind . EndOfFileToken ;
10371039 }
10381040 let ch = text . charCodeAt ( pos ) ;
@@ -1045,7 +1047,7 @@ module ts {
10451047 continue ;
10461048 }
10471049 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 ) {
10491051 // consume both CR and LF
10501052 pos += 2 ;
10511053 }
@@ -1063,7 +1065,7 @@ module ts {
10631065 continue ;
10641066 }
10651067 else {
1066- while ( pos < len && isWhiteSpace ( text . charCodeAt ( pos ) ) ) {
1068+ while ( pos < end && isWhiteSpace ( text . charCodeAt ( pos ) ) ) {
10671069 pos ++ ;
10681070 }
10691071 return token = SyntaxKind . WhitespaceTrivia ;
@@ -1136,7 +1138,7 @@ module ts {
11361138 if ( text . charCodeAt ( pos + 1 ) === CharacterCodes . slash ) {
11371139 pos += 2 ;
11381140
1139- while ( pos < len ) {
1141+ while ( pos < end ) {
11401142 if ( isLineBreak ( text . charCodeAt ( pos ) ) ) {
11411143 break ;
11421144 }
@@ -1156,7 +1158,7 @@ module ts {
11561158 pos += 2 ;
11571159
11581160 let commentClosed = false ;
1159- while ( pos < len ) {
1161+ while ( pos < end ) {
11601162 let ch = text . charCodeAt ( pos ) ;
11611163
11621164 if ( ch === CharacterCodes . asterisk && text . charCodeAt ( pos + 1 ) === CharacterCodes . slash ) {
@@ -1191,7 +1193,7 @@ module ts {
11911193 return pos ++ , token = SyntaxKind . SlashToken ;
11921194
11931195 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 ) ) {
11951197 pos += 2 ;
11961198 let value = scanMinimumNumberOfHexDigits ( 1 ) ;
11971199 if ( value < 0 ) {
@@ -1201,7 +1203,7 @@ module ts {
12011203 tokenValue = "" + value ;
12021204 return token = SyntaxKind . NumericLiteral ;
12031205 }
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 ) ) {
12051207 pos += 2 ;
12061208 let value = scanBinaryOrOctalDigits ( /* base */ 2 ) ;
12071209 if ( value < 0 ) {
@@ -1211,7 +1213,7 @@ module ts {
12111213 tokenValue = "" + value ;
12121214 return token = SyntaxKind . NumericLiteral ;
12131215 }
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 ) ) {
12151217 pos += 2 ;
12161218 let value = scanBinaryOrOctalDigits ( /* base */ 8 ) ;
12171219 if ( value < 0 ) {
@@ -1222,7 +1224,7 @@ module ts {
12221224 return token = SyntaxKind . NumericLiteral ;
12231225 }
12241226 // 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 ) ) ) {
12261228 tokenValue = "" + scanOctalDigits ( ) ;
12271229 return token = SyntaxKind . NumericLiteral ;
12281230 }
@@ -1337,7 +1339,7 @@ module ts {
13371339 default :
13381340 if ( isIdentifierStart ( ch ) ) {
13391341 pos ++ ;
1340- while ( pos < len && isIdentifierPart ( ch = text . charCodeAt ( pos ) ) ) pos ++ ;
1342+ while ( pos < end && isIdentifierPart ( ch = text . charCodeAt ( pos ) ) ) pos ++ ;
13411343 tokenValue = text . substring ( tokenPos , pos ) ;
13421344 if ( ch === CharacterCodes . backslash ) {
13431345 tokenValue += scanIdentifierParts ( ) ;
@@ -1388,7 +1390,7 @@ module ts {
13881390 while ( true ) {
13891391 // If we reach the end of a file, or hit a newline, then this is an unterminated
13901392 // regex. Report error and return what we have so far.
1391- if ( p >= len ) {
1393+ if ( p >= end ) {
13921394 tokenIsUnterminated = true ;
13931395 error ( Diagnostics . Unterminated_regular_expression_literal )
13941396 break ;
@@ -1424,7 +1426,7 @@ module ts {
14241426 p ++ ;
14251427 }
14261428
1427- while ( p < len && isIdentifierPart ( text . charCodeAt ( p ) ) ) {
1429+ while ( p < end && isIdentifierPart ( text . charCodeAt ( p ) ) ) {
14281430 p ++ ;
14291431 }
14301432 pos = p ;
@@ -1473,10 +1475,10 @@ module ts {
14731475 return speculationHelper ( callback , /*isLookahead:*/ false ) ;
14741476 }
14751477
1476- function setText ( newText : string ) {
1478+ function setText ( newText : string , start : number , length : number ) {
14771479 text = newText || "" ;
1478- len = text . length ;
1479- setTextPos ( 0 ) ;
1480+ end = length === undefined ? text . length : start + length ;
1481+ setTextPos ( start || 0 ) ;
14801482 }
14811483
14821484 function setOnError ( errorCallback : ErrorCallback ) {
@@ -1488,6 +1490,7 @@ module ts {
14881490 }
14891491
14901492 function setTextPos ( textPos : number ) {
1493+ Debug . assert ( textPos >= 0 ) ;
14911494 pos = textPos ;
14921495 startPos = textPos ;
14931496 tokenPos = textPos ;
0 commit comments