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,38 +419,7 @@ class Reader {
assertNoPriorState(stream); assertNoPriorState(stream);
} }
var startOfFileMacro = chooseReadFunction(stream, k.startOfFileReadTable); function finish() {
if (startOfFileMacro != null) {
var pos = stream.position();
var v = startOfFileMacro(stream, k);
if (v != null)
process(v.withPos(pos));
}
while (true) {
stream.dropWhitespace();
var endOfFileMacro = chooseReadFunction(stream, k.endOfFileReadTable, true);
if (endOfFileMacro != null) {
var pos = stream.position();
var v = endOfFileMacro(stream, k);
if (v != null)
process(v.withPos(pos));
}
if (stream.isEmpty())
break;
var position = stream.position();
var nextExp = Reader._read(stream, k);
// The last expression might be a comment, in which case None will be returned
switch (nextExp) {
case Some(nextExp):
process(nextExp);
case None:
stream.dropWhitespace(); // If there was a comment, drop whitespace that comes after
}
}
if (readExpArrayStartPositions.length != 0) { if (readExpArrayStartPositions.length != 0) {
throw new StreamError(stream.position(), "readExpArray() state is remaining in Reader after readAndProcess()"); throw new StreamError(stream.position(), "readExpArray() state is remaining in Reader after readAndProcess()");
} }
@@ -460,6 +429,53 @@ class Reader {
} }
} }
function afterStartOfFile():Void {
stream.dropWhitespace();
var endOfFileMacro = chooseReadFunction(stream, k.endOfFileReadTable, true);
if (endOfFileMacro != null) {
var pos = stream.position();
var v = endOfFileMacro(stream, k);
if (v != null){
process(v.withPos(pos), finish);
return;
}
}
if (stream.isEmpty()){
finish();
return;
}
var position = stream.position();
var nextExp = Reader._read(stream, k);
// The last expression might be a comment, in which case None will be returned
switch (nextExp) {
case Some(nextExp):
process(nextExp, afterStartOfFile);
case None:
stream.dropWhitespace(); // If there was a comment, drop whitespace that comes after
}
}
var startOfFileMacro = chooseReadFunction(stream, k.startOfFileReadTable);
if (startOfFileMacro != null) {
var pos = stream.position();
var v = startOfFileMacro(stream, k);
if (v != null)
process(v.withPos(pos), afterStartOfFile);
} else {
afterStartOfFile();
}
}
public static function readAndProcess(stream:Stream, k:HasReadTables, process:(ReaderExp) -> Void, nested = false) {
readAndProcessCC(stream, k, (exp, cc) -> {
process(exp);
cc();
});
}
public static function withPos(def:ReaderExpDef, pos:Position) { public static function withPos(def:ReaderExpDef, pos:Position) {
return { return {
pos: pos, pos: pos,