@@ -551,30 +551,50 @@ private void Cancel(bool createNewTokenSource = true)
551
551
552
552
private void ParseAsyncInternal ( IVBComponent component , CancellationToken token , TokenStreamRewriter rewriter = null )
553
553
{
554
+ State . SetModuleState ( component , ParserState . Parsing ) ;
555
+
554
556
var preprocessor = _preprocessorFactory ( ) ;
555
557
var parser = new ComponentParseTask ( component , preprocessor , _attributeParser , rewriter ) ;
556
- parser . ParseFailure += ( sender , e ) =>
558
+
559
+ var finishedParseTask = FinishedComponentParseTask ( parser , token ) ; //This runs synchronously.
560
+
561
+ if ( finishedParseTask . IsFaulted )
557
562
{
558
- State . SetModuleState ( component , ParserState . Error , e . Cause as SyntaxErrorException ) ;
559
- } ;
560
- parser . ParseCompleted += ( sender , e ) =>
563
+ State . SetModuleState ( component , ParserState . Error , finishedParseTask . Exception . InnerException as SyntaxErrorException ) ;
564
+ }
565
+ else if ( finishedParseTask . IsCompleted )
561
566
{
567
+ var result = finishedParseTask . Result ;
562
568
lock ( State )
569
+ {
563
570
lock ( component )
564
571
{
565
- State . SetModuleAttributes ( component , e . Attributes ) ;
566
- State . AddParseTree ( component , e . ParseTree ) ;
567
- State . AddTokenStream ( component , e . Tokens ) ;
568
- State . SetModuleComments ( component , e . Comments ) ;
569
- State . SetModuleAnnotations ( component , e . Annotations ) ;
572
+ State . SetModuleAttributes ( component , result . Attributes ) ;
573
+ State . AddParseTree ( component , result . ParseTree ) ;
574
+ State . AddTokenStream ( component , result . Tokens ) ;
575
+ State . SetModuleComments ( component , result . Comments ) ;
576
+ State . SetModuleAnnotations ( component , result . Annotations ) ;
570
577
571
578
// This really needs to go last
572
579
State . SetModuleState ( component , ParserState . Parsed ) ;
573
580
}
581
+ }
582
+ }
583
+ }
584
+
585
+ private Task < ComponentParseTask . ParseCompletionArgs > FinishedComponentParseTask ( ComponentParseTask parser , CancellationToken token )
586
+ {
587
+ var tcs = new TaskCompletionSource < ComponentParseTask . ParseCompletionArgs > ( ) ;
588
+ parser . ParseFailure += ( sender , e ) =>
589
+ {
590
+ tcs . SetException ( e . Cause ) ;
591
+ } ;
592
+ parser . ParseCompleted += ( sender , e ) =>
593
+ {
594
+ tcs . SetResult ( e ) ;
574
595
} ;
575
- State . SetModuleState ( component , ParserState . Parsing ) ;
576
-
577
596
parser . Start ( token ) ;
597
+ return tcs . Task ;
578
598
}
579
599
580
600
private readonly ConcurrentDictionary < string , Declaration > _projectDeclarations = new ConcurrentDictionary < string , Declaration > ( ) ;
0 commit comments