Skip to content

Commit e09993e

Browse files
committed
readAndProcessCC
1 parent 1ae2eaa commit e09993e

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/kiss/Reader.hx

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -411,53 +411,69 @@ class Reader {
411411
Read all the expressions in the given stream, processing them one by one while reading.
412412
They can't be read all at once because some expressions change the Readtable state
413413
**/
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) {
415415
if (nested) {
416416
nestedReadExpArrayStartPositions.push(readExpArrayStartPositions);
417417
readExpArrayStartPositions = [];
418418
} else {
419419
assertNoPriorState(stream);
420420
}
421421

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+
}
428430
}
429431

430-
while (true) {
432+
function afterStartOfFile():Void {
431433
stream.dropWhitespace();
432434

433435
var endOfFileMacro = chooseReadFunction(stream, k.endOfFileReadTable, true);
434436
if (endOfFileMacro != null) {
435437
var pos = stream.position();
436438
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;
439448
}
440449

441-
if (stream.isEmpty())
442-
break;
443450
var position = stream.position();
444451
var nextExp = Reader._read(stream, k);
445452
// The last expression might be a comment, in which case None will be returned
446453
switch (nextExp) {
447454
case Some(nextExp):
448-
process(nextExp);
455+
process(nextExp, afterStartOfFile);
449456
case None:
450457
stream.dropWhitespace(); // If there was a comment, drop whitespace that comes after
451458
}
452459
}
453460

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();
456469
}
470+
}
457471

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+
});
461477
}
462478

463479
public static function withPos(def:ReaderExpDef, pos:Position) {

0 commit comments

Comments
 (0)