Refactor the read loop into Reader

This commit is contained in:
2020-12-08 18:58:38 -07:00
parent 419e72e280
commit b4bf0d1576
2 changed files with 36 additions and 28 deletions

View File

@@ -62,16 +62,7 @@ class Kiss {
if (k == null) if (k == null)
k = defaultKissState(); k = defaultKissState();
while (true) { Reader.readAndProcess(stream, k.readTable, (nextExp) -> {
stream.dropWhitespace();
if (stream.isEmpty())
break;
var position = stream.position();
var nextExp = Reader.read(stream, k.readTable);
// The last expression might be a comment, in which case None will be returned
switch (nextExp) {
case Some(nextExp):
#if test #if test
Sys.println(nextExp.def.toString()); Sys.println(nextExp.def.toString());
#end #end
@@ -87,10 +78,7 @@ class Kiss {
#end #end
classFields.push(field); classFields.push(field);
} }
case None: });
stream.dropWhitespace(); // If there was a comment, drop whitespace that comes after
}
}
return classFields; return classFields;
} catch (err:CompileError) { } catch (err:CompileError) {

View File

@@ -148,6 +148,26 @@ class Reader {
return array; return array;
} }
/**
Read all the expressions in the given stream, processing them one by one while reading.
**/
public static function readAndProcess(stream:Stream, readTable:Map<String, ReadFunction>, process:(ReaderExp) -> Void) {
while (true) {
stream.dropWhitespace();
if (stream.isEmpty())
break;
var position = stream.position();
var nextExp = Reader.read(stream, readTable);
// 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
}
}
}
public static function withPos(def:ReaderExpDef, pos:Position) { public static function withPos(def:ReaderExpDef, pos:Position) {
return { return {
pos: pos, pos: pos,