@@ -411,53 +411,69 @@ class Reader {
411
411
Read all the expressions in the given stream, processing them one by one while reading.
412
412
They can't be read all at once because some expressions change the Readtable state
413
413
**/
414
- public static function readAndProcess (stream : Stream , k : HasReadTables , process : (ReaderExp ) -> Void , nested = false ) {
414
+ public static function readAndProcessCC (stream : Stream , k : HasReadTables , process : (ReaderExp , cc : Void -> Void ) -> Void , nested = false ) {
415
415
if (nested ) {
416
416
nestedReadExpArrayStartPositions .push (readExpArrayStartPositions );
417
417
readExpArrayStartPositions = [];
418
418
} else {
419
419
assertNoPriorState (stream );
420
420
}
421
421
422
- var startOfFileMacro = chooseReadFunction (stream , k .startOfFileReadTable );
423
- if (startOfFileMacro != null ) {
424
- var pos = stream .position ();
425
- var v = startOfFileMacro (stream , k );
426
- if (v != null )
427
- process (v .withPos (pos ));
422
+ function finish () {
423
+ if (readExpArrayStartPositions .length != 0 ) {
424
+ throw new StreamError (stream .position (), " readExpArray() state is remaining in Reader after readAndProcess()" );
425
+ }
426
+
427
+ if (nested ) {
428
+ readExpArrayStartPositions = nestedReadExpArrayStartPositions .pop ();
429
+ }
428
430
}
429
431
430
- while ( true ) {
432
+ function afterStartOfFile () : Void {
431
433
stream .dropWhitespace ();
432
434
433
435
var endOfFileMacro = chooseReadFunction (stream , k .endOfFileReadTable , true );
434
436
if (endOfFileMacro != null ) {
435
437
var pos = stream .position ();
436
438
var v = endOfFileMacro (stream , k );
437
- if (v != null )
438
- process (v .withPos (pos ));
439
+ if (v != null ){
440
+ process (v .withPos (pos ), finish );
441
+ return ;
442
+ }
443
+ }
444
+
445
+ if (stream .isEmpty ()){
446
+ finish ();
447
+ return ;
439
448
}
440
449
441
- if (stream .isEmpty ())
442
- break ;
443
450
var position = stream .position ();
444
451
var nextExp = Reader ._read (stream , k );
445
452
// The last expression might be a comment, in which case None will be returned
446
453
switch (nextExp ) {
447
454
case Some (nextExp ):
448
- process (nextExp );
455
+ process (nextExp , afterStartOfFile );
449
456
case None :
450
457
stream .dropWhitespace (); // If there was a comment, drop whitespace that comes after
451
458
}
452
459
}
453
460
454
- if (readExpArrayStartPositions .length != 0 ) {
455
- throw new StreamError (stream .position (), " readExpArray() state is remaining in Reader after readAndProcess()" );
461
+ var startOfFileMacro = chooseReadFunction (stream , k .startOfFileReadTable );
462
+ if (startOfFileMacro != null ) {
463
+ var pos = stream .position ();
464
+ var v = startOfFileMacro (stream , k );
465
+ if (v != null )
466
+ process (v .withPos (pos ), afterStartOfFile );
467
+ } else {
468
+ afterStartOfFile ();
456
469
}
470
+ }
457
471
458
- if (nested ) {
459
- readExpArrayStartPositions = nestedReadExpArrayStartPositions .pop ();
460
- }
472
+ public static function readAndProcess (stream : Stream , k : HasReadTables , process : (ReaderExp ) -> Void , nested = false ) {
473
+ readAndProcessCC (stream , k , (exp , cc ) -> {
474
+ process (exp );
475
+ cc ();
476
+ });
461
477
}
462
478
463
479
public static function withPos (def : ReaderExpDef , pos : Position ) {
0 commit comments