@@ -195,11 +195,7 @@ module TypeScript.Parser {
195
195
//
196
196
// Getting this all correct is tricky and requires careful reading of the grammar to
197
197
// understand when these values should be changed versus when they should be inherited.
198
- var strictModeContext : boolean = false ;
199
- var disallowInContext : boolean = false ;
200
- var yieldContext : boolean = false ;
201
- var generatorParameterContext : boolean = false ;
202
- var contextFlags : number = 0 ;
198
+ var contextFlags : ParserContextFlags = 0 ;
203
199
204
200
// Current state of the parser. If we need to rewind we will store and reset these values as
205
201
// appropriate.
@@ -223,7 +219,7 @@ module TypeScript.Parser {
223
219
224
220
// Now, clear out our state so that our singleton parser doesn't keep things alive.
225
221
diagnostics = [ ] ;
226
- contextFlags = SyntaxNodeConstants . None ;
222
+ contextFlags = 0 ;
227
223
fileName = undefined ;
228
224
source . release ( ) ;
229
225
source = undefined ;
@@ -284,10 +280,7 @@ module TypeScript.Parser {
284
280
// are unaffected by strict mode. It's just the parser will decide what to do with it
285
281
// differently depending on what mode it is in.
286
282
if ( node &&
287
- parsedInStrictModeContext ( node ) === strictModeContext &&
288
- parsedInDisallowInContext ( node ) === disallowInContext &&
289
- parsedInYieldContext ( node ) === yieldContext &&
290
- parsedInGeneratorParameterContext ( node ) === generatorParameterContext ) {
283
+ parserContextFlags ( node ) === contextFlags ) {
291
284
292
285
return node ;
293
286
}
@@ -421,7 +414,7 @@ module TypeScript.Parser {
421
414
422
415
// If we have a 'yield' keyword, and we're in the [yield] context, then 'yield' is
423
416
// considered a keyword and is not an identifier.
424
- if ( tokenKind === SyntaxKind . YieldKeyword && yieldContext ) {
417
+ if ( tokenKind === SyntaxKind . YieldKeyword && inYieldContext ( ) ) {
425
418
return false ;
426
419
}
427
420
@@ -431,7 +424,7 @@ module TypeScript.Parser {
431
424
if ( tokenKind <= SyntaxKind . LastFutureReservedStrictKeyword ) {
432
425
// Could be a keyword or identifier. It's an identifier if we're not in strict
433
426
// mode.
434
- return ! strictModeContext ;
427
+ return ! inStrictModeContext ( ) ;
435
428
}
436
429
437
430
// If it's typescript keyword, then it's actually a javascript identifier.
@@ -633,39 +626,52 @@ module TypeScript.Parser {
633
626
throw Errors . invalidOperation ( ) ;
634
627
}
635
628
636
- function updateContextFlags ( ) {
637
- contextFlags =
638
- ( strictModeContext ? SyntaxNodeConstants . ParsedInStrictModeContext : 0 ) |
639
- ( disallowInContext ? SyntaxNodeConstants . ParsedInDisallowInContext : 0 ) |
640
- ( yieldContext ? SyntaxNodeConstants . ParsedInYieldContext : 0 ) |
641
- ( generatorParameterContext ? SyntaxNodeConstants . ParsedInGeneratorParameterContext : 0 ) ;
629
+ function setContextFlag ( val : boolean , flag : ParserContextFlags ) {
630
+ if ( val ) {
631
+ contextFlags |= flag ;
632
+ }
633
+ else {
634
+ contextFlags &= ~ flag ;
635
+ }
642
636
}
643
637
644
638
function setStrictModeContext ( val : boolean ) {
645
- strictModeContext = val ;
646
- updateContextFlags ( ) ;
639
+ setContextFlag ( val , ParserContextFlags . StrictMode ) ;
647
640
}
648
641
649
642
function setDisallowInContext ( val : boolean ) {
650
- disallowInContext = val ;
651
- updateContextFlags ( ) ;
643
+ setContextFlag ( val , ParserContextFlags . DisallowIn ) ;
652
644
}
653
645
654
646
function setYieldContext ( val : boolean ) {
655
- yieldContext = val ;
656
- updateContextFlags ( ) ;
647
+ setContextFlag ( val , ParserContextFlags . Yield ) ;
657
648
}
658
649
659
650
function setGeneratorParameterContext ( val : boolean ) {
660
- generatorParameterContext = val ;
661
- updateContextFlags ( ) ;
651
+ setContextFlag ( val , ParserContextFlags . GeneratorParameter ) ;
652
+ }
653
+
654
+ function inStrictModeContext ( ) {
655
+ return ( contextFlags & ParserContextFlags . StrictMode ) !== 0 ;
656
+ }
657
+
658
+ function inDisallowInContext ( ) {
659
+ return ( contextFlags & ParserContextFlags . DisallowIn ) !== 0 ;
660
+ }
661
+
662
+ function inYieldContext ( ) {
663
+ return ( contextFlags & ParserContextFlags . Yield ) !== 0 ;
664
+ }
665
+
666
+ function inGeneratorParameterContext ( ) {
667
+ return ( contextFlags & ParserContextFlags . GeneratorParameter ) !== 0 ;
662
668
}
663
669
664
670
function parseSourceUnit ( ) : SourceUnitSyntax {
665
671
// Note: saving and restoring the 'isInStrictMode' state is not really necessary here
666
672
// (as it will never be read afterwards). However, for symmetry with the rest of the
667
673
// parsing code, we do the same here.
668
- var savedIsInStrictMode = strictModeContext
674
+ var savedIsInStrictMode = inStrictModeContext ( )
669
675
670
676
// Note: any skipped tokens produced after the end of all the module elements will be
671
677
// added as skipped trivia to the start of the EOF token.
@@ -692,7 +698,7 @@ module TypeScript.Parser {
692
698
}
693
699
694
700
function updateStrictModeState ( items : any [ ] ) : void {
695
- if ( ! strictModeContext ) {
701
+ if ( ! inStrictModeContext ( ) ) {
696
702
// Check if all the items are directive prologue elements.
697
703
for ( var i = 0 , n = items . length ; i < n ; i ++ ) {
698
704
if ( ! isDirectivePrologueElement ( items [ i ] ) ) {
@@ -972,7 +978,7 @@ module TypeScript.Parser {
972
978
}
973
979
974
980
function allowInAnd < T > ( func : ( ) => T ) : T {
975
- if ( disallowInContext ) {
981
+ if ( inDisallowInContext ( ) ) {
976
982
setDisallowInContext ( false ) ;
977
983
var result = func ( ) ;
978
984
setDisallowInContext ( true ) ;
@@ -984,7 +990,7 @@ module TypeScript.Parser {
984
990
}
985
991
986
992
function disallowInAnd < T > ( func : ( ) => T ) : T {
987
- if ( disallowInContext ) {
993
+ if ( inDisallowInContext ( ) ) {
988
994
// no need to do anything special if 'in' is already disallowed.
989
995
return func ( ) ;
990
996
}
@@ -996,7 +1002,7 @@ module TypeScript.Parser {
996
1002
}
997
1003
998
1004
function enterYieldContextAnd < T > ( func : ( ) => T ) : T {
999
- if ( yieldContext ) {
1005
+ if ( inYieldContext ( ) ) {
1000
1006
// no need to do anything special if we're already in the [Yield] context.
1001
1007
return func ( ) ;
1002
1008
}
@@ -1008,7 +1014,7 @@ module TypeScript.Parser {
1008
1014
}
1009
1015
1010
1016
function exitYieldContextAnd < T > ( func : ( ) => T ) : T {
1011
- if ( yieldContext ) {
1017
+ if ( inYieldContext ( ) ) {
1012
1018
setYieldContext ( false ) ;
1013
1019
var result = func ( ) ;
1014
1020
setYieldContext ( true ) ;
@@ -1108,7 +1114,7 @@ module TypeScript.Parser {
1108
1114
// [+GeneratorParameter] ClassHeritageopt { ClassBodyopt }
1109
1115
1110
1116
if ( isHeritageClause ( ) ) {
1111
- return isClassHeritageClause && generatorParameterContext
1117
+ return isClassHeritageClause && inGeneratorParameterContext ( )
1112
1118
? exitYieldContextAnd ( parseHeritageClausesWorker )
1113
1119
: parseHeritageClausesWorker ( ) ;
1114
1120
}
@@ -1143,7 +1149,7 @@ module TypeScript.Parser {
1143
1149
// [+GeneratorParameter] ClassHeritageopt { ClassBodyopt }
1144
1150
1145
1151
if ( openBraceToken . fullWidth ( ) > 0 ) {
1146
- return generatorParameterContext
1152
+ return inGeneratorParameterContext ( )
1147
1153
? exitYieldContextAnd ( parseClassElements )
1148
1154
: parseClassElements ( ) ;
1149
1155
}
@@ -2446,11 +2452,11 @@ module TypeScript.Parser {
2446
2452
if ( _currentToken . kind === SyntaxKind . YieldKeyword ) {
2447
2453
// If we have a 'yield' keyword, and htis is a context where yield expressions are
2448
2454
// allowed, then definitely parse out a yield expression.
2449
- if ( yieldContext ) {
2455
+ if ( inYieldContext ( ) ) {
2450
2456
return true ;
2451
2457
}
2452
2458
2453
- if ( strictModeContext ) {
2459
+ if ( inStrictModeContext ( ) ) {
2454
2460
// If we're in strict mode, then 'yield' is a keyword, could only ever start
2455
2461
// a yield expression.
2456
2462
return true ;
@@ -2583,7 +2589,7 @@ module TypeScript.Parser {
2583
2589
}
2584
2590
2585
2591
// also, if it's the 'in' operator, only allow if our caller allows it.
2586
- if ( tokenKind === SyntaxKind . InKeyword && disallowInContext ) {
2592
+ if ( tokenKind === SyntaxKind . InKeyword && inDisallowInContext ( ) ) {
2587
2593
break ;
2588
2594
}
2589
2595
@@ -3140,7 +3146,7 @@ module TypeScript.Parser {
3140
3146
//
3141
3147
// [Yield], on the other hand, is available, and is passed through.
3142
3148
3143
- var callSignature = parseCallSignature ( /*requireCompleteTypeParameterList:*/ true , /*yield:*/ yieldContext , /*generatorParameter:*/ false ) ;
3149
+ var callSignature = parseCallSignature ( /*requireCompleteTypeParameterList:*/ true , /*yield:*/ inYieldContext ( ) , /*generatorParameter:*/ false ) ;
3144
3150
3145
3151
if ( requireArrow && currentToken ( ) . kind !== SyntaxKind . EqualsGreaterThanToken ) {
3146
3152
return undefined ;
@@ -3502,7 +3508,7 @@ module TypeScript.Parser {
3502
3508
3503
3509
var _currentToken = currentToken ( ) ;
3504
3510
if ( _currentToken . kind === SyntaxKind . OpenBracketToken ) {
3505
- return generatorParameterContext
3511
+ return inGeneratorParameterContext ( )
3506
3512
? exitYieldContextAnd ( parseComputedPropertyName )
3507
3513
: parseComputedPropertyName ( ) ;
3508
3514
}
@@ -3585,17 +3591,17 @@ module TypeScript.Parser {
3585
3591
}
3586
3592
3587
3593
function parseFunctionBlockStatements ( ) {
3588
- var savedIsInStrictMode = strictModeContext ;
3594
+ var savedIsInStrictMode = inStrictModeContext ( ) ;
3589
3595
var statements = parseSyntaxList < IStatementSyntax > ( ListParsingState . Block_Statements , updateStrictModeState ) ;
3590
3596
setStrictModeContext ( savedIsInStrictMode ) ;
3591
3597
3592
3598
return statements ;
3593
3599
}
3594
3600
3595
- function parseCallSignature ( requireCompleteTypeParameterList : boolean , _yieldContext : boolean , _generatorParameterContext : boolean ) : CallSignatureSyntax {
3601
+ function parseCallSignature ( requireCompleteTypeParameterList : boolean , yieldContext : boolean , generatorParameterContext : boolean ) : CallSignatureSyntax {
3596
3602
return new CallSignatureSyntax ( contextFlags ,
3597
3603
tryParseTypeParameterList ( requireCompleteTypeParameterList ) ,
3598
- parseParameterList ( _yieldContext , _generatorParameterContext ) ,
3604
+ parseParameterList ( yieldContext , generatorParameterContext ) ,
3599
3605
parseOptionalTypeAnnotation ( /*allowStringLiteral:*/ false ) ) ;
3600
3606
}
3601
3607
@@ -3645,7 +3651,7 @@ module TypeScript.Parser {
3645
3651
return new ConstraintSyntax ( contextFlags , eatToken ( SyntaxKind . ExtendsKeyword ) , parseTypeOrExpression ( ) ) ;
3646
3652
}
3647
3653
3648
- function parseParameterList ( _yieldContext : boolean , _generatorParameterContext : boolean ) : ParameterListSyntax {
3654
+ function parseParameterList ( yieldContext : boolean , generatorParameterContext : boolean ) : ParameterListSyntax {
3649
3655
// FormalParameters[Yield,GeneratorParameter] :
3650
3656
// ...
3651
3657
//
@@ -3661,11 +3667,11 @@ module TypeScript.Parser {
3661
3667
// [+GeneratorParameter]BindingIdentifier[Yield]Initializer[In]opt
3662
3668
// [~GeneratorParameter]BindingIdentifier[?Yield]Initializer[In, ?Yield]opt
3663
3669
3664
- var savedYieldContext = yieldContext ;
3665
- var savedGeneratorParameterContext = generatorParameterContext ;
3670
+ var savedYieldContext = inYieldContext ( ) ;
3671
+ var savedGeneratorParameterContext = inGeneratorParameterContext ( ) ;
3666
3672
3667
- setYieldContext ( _yieldContext ) ;
3668
- setGeneratorParameterContext ( _generatorParameterContext ) ;
3673
+ setYieldContext ( yieldContext ) ;
3674
+ setGeneratorParameterContext ( generatorParameterContext ) ;
3669
3675
3670
3676
var openParenToken : ISyntaxToken ;
3671
3677
var result = new ParameterListSyntax ( contextFlags ,
@@ -3747,8 +3753,8 @@ module TypeScript.Parser {
3747
3753
function tryParseType ( ) : ITypeSyntax {
3748
3754
// The rules about 'yield' only apply to actual code/expression contexts. They don't
3749
3755
// apply to 'type' contexts. So we disable these parameters here before moving on.
3750
- var savedYieldContext = yieldContext ;
3751
- var savedGeneratorParameterContext = generatorParameterContext ;
3756
+ var savedYieldContext = inYieldContext ( ) ;
3757
+ var savedGeneratorParameterContext = inGeneratorParameterContext ( ) ;
3752
3758
3753
3759
setYieldContext ( false ) ;
3754
3760
setGeneratorParameterContext ( false ) ;
@@ -3997,7 +4003,7 @@ module TypeScript.Parser {
3997
4003
// [+GeneratorParameter]BindingIdentifier[Yield]Initializer[In]opt
3998
4004
// [~GeneratorParameter]BindingIdentifier[?Yield]Initializer[In, ?Yield]opt
3999
4005
4000
- var identifier = generatorParameterContext
4006
+ var identifier = inGeneratorParameterContext ( )
4001
4007
? enterYieldContextAnd ( eatIdentifierToken )
4002
4008
: eatIdentifierToken ( ) ;
4003
4009
@@ -4006,7 +4012,7 @@ module TypeScript.Parser {
4006
4012
4007
4013
var equalsValueClause : EqualsValueClauseSyntax = undefined ;
4008
4014
if ( isEqualsValueClause ( /*inParameter*/ true ) ) {
4009
- equalsValueClause = generatorParameterContext
4015
+ equalsValueClause = inGeneratorParameterContext ( )
4010
4016
? exitYieldContextAnd ( parseEqualsValueClause )
4011
4017
: parseEqualsValueClause ( ) ;
4012
4018
}
@@ -4388,7 +4394,7 @@ module TypeScript.Parser {
4388
4394
}
4389
4395
4390
4396
function isExpectedVariableDeclaration_VariableDeclaratorsTerminator ( ) : boolean {
4391
- if ( disallowInContext ) {
4397
+ if ( inDisallowInContext ( ) ) {
4392
4398
// This is the case when we're parsing variable declarations in a for/for-in statement.
4393
4399
var tokenKind = currentToken ( ) . kind ;
4394
4400
0 commit comments