@@ -260,7 +260,7 @@ class MonarchClassicTokensCollector implements IMonarchTokensCollector {
260260 }
261261 this . _lastTokenType = type ;
262262 this . _lastTokenLanguage = this . _language ;
263- this . _tokens . push ( new Token ( startOffset , type , this . _language ) ) ;
263+ this . _tokens . push ( new Token ( startOffset , type , this . _language ! ) ) ;
264264 }
265265
266266 public nestedModeTokenize ( embeddedModeLine : string , embeddedModeData : EmbeddedModeData , offsetDelta : number ) : modes . IState {
@@ -306,7 +306,7 @@ class MonarchModernTokensCollector implements IMonarchTokensCollector {
306306 }
307307
308308 public enterMode ( startOffset : number , modeId : string ) : void {
309- this . _currentLanguageId = this . _modeService . getLanguageIdentifier ( modeId ) . id ;
309+ this . _currentLanguageId = this . _modeService . getLanguageIdentifier ( modeId ) ! . id ;
310310 }
311311
312312 public emit ( startOffset : number , type : string ) : void {
@@ -417,7 +417,7 @@ class MonarchTokenizer implements modes.ITokenizationSupport {
417417 }
418418
419419 public getInitialState ( ) : modes . IState {
420- let rootState = MonarchStackElementFactory . create ( null , this . _lexer . start ) ;
420+ let rootState = MonarchStackElementFactory . create ( null , this . _lexer . start ! ) ;
421421 return MonarchLineStateFactory . create ( rootState , null ) ;
422422 }
423423
@@ -506,6 +506,13 @@ class MonarchTokenizer implements modes.ITokenizationSupport {
506506 return this . _myTokenize ( restOfTheLine , lineState , offsetDelta + popOffset , tokensCollector ) ;
507507 }
508508
509+ private _safeRuleName ( rule : monarchCommon . IRule | null ) : string {
510+ if ( rule ) {
511+ return rule . name ;
512+ }
513+ return '(unknown)' ;
514+ }
515+
509516 private _myTokenize ( line : string , lineState : MonarchLineState , offsetDelta : number , tokensCollector : IMonarchTokensCollector ) : MonarchLineState {
510517 tokensCollector . enterMode ( offsetDelta , this . _modeId ) ;
511518
@@ -519,7 +526,7 @@ class MonarchTokenizer implements modes.ITokenizationSupport {
519526 // these never need cloning or equality since they are only used within a line match
520527 interface GroupMatching {
521528 matches : string [ ] ;
522- rule : monarchCommon . IRule ;
529+ rule : monarchCommon . IRule | null ;
523530 groups : { action : monarchCommon . FuzzyAction ; matched : string ; } [ ] ;
524531 }
525532 let groupMatching : GroupMatching | null = null ;
@@ -599,6 +606,11 @@ class MonarchTokenizer implements modes.ITokenizationSupport {
599606 action = this . _lexer . defaultToken ;
600607 }
601608
609+ if ( ! matched ) {
610+ // should never happen, needed for strict null checking
611+ break ;
612+ }
613+
602614 // advance stream
603615 pos += matched . length ;
604616
@@ -647,7 +659,7 @@ class MonarchTokenizer implements modes.ITokenizationSupport {
647659 nextState = nextState . substr ( 1 ) ; // peel off starting '@'
648660 }
649661 if ( ! monarchCommon . findRules ( this . _lexer , nextState ) ) {
650- throw monarchCommon . createError ( this . _lexer , 'trying to switch to a state \'' + nextState + '\' that is undefined in rule: ' + rule . name ) ;
662+ throw monarchCommon . createError ( this . _lexer , 'trying to switch to a state \'' + nextState + '\' that is undefined in rule: ' + this . _safeRuleName ( rule ) ) ;
651663 } else {
652664 stack = stack . switchTo ( nextState ) ;
653665 }
@@ -657,15 +669,15 @@ class MonarchTokenizer implements modes.ITokenizationSupport {
657669 if ( action . next === '@push' ) {
658670 if ( stack . depth >= this . _lexer . maxStack ) {
659671 throw monarchCommon . createError ( this . _lexer , 'maximum tokenizer stack size reached: [' +
660- stack . state + ',' + stack . parent . state + ',...]' ) ;
672+ stack . state + ',' + stack . parent ! . state + ',...]' ) ;
661673 } else {
662674 stack = stack . push ( state ) ;
663675 }
664676 } else if ( action . next === '@pop' ) {
665677 if ( stack . depth <= 1 ) {
666- throw monarchCommon . createError ( this . _lexer , 'trying to pop an empty stack in rule: ' + rule . name ) ;
678+ throw monarchCommon . createError ( this . _lexer , 'trying to pop an empty stack in rule: ' + this . _safeRuleName ( rule ) ) ;
667679 } else {
668- stack = stack . pop ( ) ;
680+ stack = stack . pop ( ) ! ;
669681 }
670682 } else if ( action . next === '@popall' ) {
671683 stack = stack . popall ( ) ;
@@ -676,7 +688,7 @@ class MonarchTokenizer implements modes.ITokenizationSupport {
676688 }
677689
678690 if ( ! monarchCommon . findRules ( this . _lexer , nextState ) ) {
679- throw monarchCommon . createError ( this . _lexer , 'trying to set a next state \'' + nextState + '\' that is undefined in rule: ' + rule . name ) ;
691+ throw monarchCommon . createError ( this . _lexer , 'trying to set a next state \'' + nextState + '\' that is undefined in rule: ' + this . _safeRuleName ( rule ) ) ;
680692 } else {
681693 stack = stack . push ( nextState ) ;
682694 }
@@ -690,23 +702,23 @@ class MonarchTokenizer implements modes.ITokenizationSupport {
690702
691703 // check result
692704 if ( result === null ) {
693- throw monarchCommon . createError ( this . _lexer , 'lexer rule has no well-defined action in rule: ' + rule . name ) ;
705+ throw monarchCommon . createError ( this . _lexer , 'lexer rule has no well-defined action in rule: ' + this . _safeRuleName ( rule ) ) ;
694706 }
695707
696708 // is the result a group match?
697709 if ( Array . isArray ( result ) ) {
698710 if ( groupMatching && groupMatching . groups . length > 0 ) {
699- throw monarchCommon . createError ( this . _lexer , 'groups cannot be nested: ' + rule . name ) ;
711+ throw monarchCommon . createError ( this . _lexer , 'groups cannot be nested: ' + this . _safeRuleName ( rule ) ) ;
700712 }
701713 if ( matches . length !== result . length + 1 ) {
702- throw monarchCommon . createError ( this . _lexer , 'matched number of groups does not match the number of actions in rule: ' + rule . name ) ;
714+ throw monarchCommon . createError ( this . _lexer , 'matched number of groups does not match the number of actions in rule: ' + this . _safeRuleName ( rule ) ) ;
703715 }
704716 let totalLen = 0 ;
705717 for ( let i = 1 ; i < matches . length ; i ++ ) {
706718 totalLen += matches [ i ] . length ;
707719 }
708720 if ( totalLen !== matched . length ) {
709- throw monarchCommon . createError ( this . _lexer , 'with groups, all characters should be matched in consecutive groups in rule: ' + rule . name ) ;
721+ throw monarchCommon . createError ( this . _lexer , 'with groups, all characters should be matched in consecutive groups in rule: ' + this . _safeRuleName ( rule ) ) ;
710722 }
711723
712724 groupMatching = {
@@ -740,7 +752,7 @@ class MonarchTokenizer implements modes.ITokenizationSupport {
740752 if ( stackLen0 !== stack . depth || state !== stack . state || ( ! groupMatching ? 0 : groupMatching . groups . length ) !== groupLen0 ) {
741753 continue ;
742754 } else {
743- throw monarchCommon . createError ( this . _lexer , 'no progress in tokenizer in rule: ' + rule . name ) ;
755+ throw monarchCommon . createError ( this . _lexer , 'no progress in tokenizer in rule: ' + this . _safeRuleName ( rule ) ) ;
744756 pos = lineLength ; // must make progress or editor loops
745757 }
746758 }
0 commit comments