readAndProcessCC

This commit is contained in:
2024-12-10 20:51:19 -06:00
parent 1ae2eaa3c5
commit e09993ea78

View File

@@ -411,7 +411,7 @@ class Reader {
Read all the expressions in the given stream, processing them one by one while reading. Read all the expressions in the given stream, processing them one by one while reading.
They can't be read all at once because some expressions change the Readtable state They can't be read all at once because some expressions change the Readtable state
**/ **/
public static function readAndProcess(stream:Stream, k:HasReadTables, process:(ReaderExp) -> Void, nested = false) { public static function readAndProcessCC(stream:Stream, k:HasReadTables, process:(ReaderExp, cc:Void->Void) -> Void, nested = false) {
if (nested) { if (nested) {
nestedReadExpArrayStartPositions.push(readExpArrayStartPositions); nestedReadExpArrayStartPositions.push(readExpArrayStartPositions);
readExpArrayStartPositions = []; readExpArrayStartPositions = [];
@@ -419,45 +419,61 @@ class Reader {
assertNoPriorState(stream); assertNoPriorState(stream);
} }
var startOfFileMacro = chooseReadFunction(stream, k.startOfFileReadTable); function finish() {
if (startOfFileMacro != null) { if (readExpArrayStartPositions.length != 0) {
var pos = stream.position(); throw new StreamError(stream.position(), "readExpArray() state is remaining in Reader after readAndProcess()");
var v = startOfFileMacro(stream, k); }
if (v != null)
process(v.withPos(pos)); if (nested) {
readExpArrayStartPositions = nestedReadExpArrayStartPositions.pop();
}
} }
while (true) { function afterStartOfFile():Void {
stream.dropWhitespace(); stream.dropWhitespace();
var endOfFileMacro = chooseReadFunction(stream, k.endOfFileReadTable, true); var endOfFileMacro = chooseReadFunction(stream, k.endOfFileReadTable, true);
if (endOfFileMacro != null) { if (endOfFileMacro != null) {
var pos = stream.position(); var pos = stream.position();
var v = endOfFileMacro(stream, k); var v = endOfFileMacro(stream, k);
if (v != null) if (v != null){
process(v.withPos(pos)); process(v.withPos(pos), finish);
return;
}
}
if (stream.isEmpty()){
finish();
return;
} }
if (stream.isEmpty())
break;
var position = stream.position(); var position = stream.position();
var nextExp = Reader._read(stream, k); var nextExp = Reader._read(stream, k);
// The last expression might be a comment, in which case None will be returned // The last expression might be a comment, in which case None will be returned
switch (nextExp) { switch (nextExp) {
case Some(nextExp): case Some(nextExp):
process(nextExp); process(nextExp, afterStartOfFile);
case None: case None:
stream.dropWhitespace(); // If there was a comment, drop whitespace that comes after stream.dropWhitespace(); // If there was a comment, drop whitespace that comes after
} }
} }
if (readExpArrayStartPositions.length != 0) { var startOfFileMacro = chooseReadFunction(stream, k.startOfFileReadTable);
throw new StreamError(stream.position(), "readExpArray() state is remaining in Reader after readAndProcess()"); if (startOfFileMacro != null) {
var pos = stream.position();
var v = startOfFileMacro(stream, k);
if (v != null)
process(v.withPos(pos), afterStartOfFile);
} else {
afterStartOfFile();
} }
}
if (nested) { public static function readAndProcess(stream:Stream, k:HasReadTables, process:(ReaderExp) -> Void, nested = false) {
readExpArrayStartPositions = nestedReadExpArrayStartPositions.pop(); readAndProcessCC(stream, k, (exp, cc) -> {
} process(exp);
cc();
});
} }
public static function withPos(def:ReaderExpDef, pos:Position) { public static function withPos(def:ReaderExpDef, pos:Position) {