beginning and end of file readerMacros

This commit is contained in:
2021-12-21 17:05:37 -07:00
parent 5582ecde10
commit 8a4414273b
3 changed files with 38 additions and 0 deletions

View File

@@ -27,6 +27,8 @@ typedef KissState = {
file:String,
readTable:ReadTable,
startOfLineReadTable:ReadTable,
startOfFileReadTable:ReadTable,
endOfFileReadTable:ReadTable,
fieldForms:Map<String, FieldFormFunction>,
specialForms:Map<String, SpecialFormFunction>,
macros:Map<String, MacroFunction>,
@@ -55,6 +57,8 @@ class Kiss {
file: "",
readTable: Reader.builtins(),
startOfLineReadTable: new ReadTable(),
startOfFileReadTable: new ReadTable(),
endOfFileReadTable: new ReadTable(),
fieldForms: FieldForms.builtins(),
specialForms: SpecialForms.builtins(),
macros: Macros.builtins(),

View File

@@ -527,6 +527,12 @@ class Macros {
case MetaExp("start", stringsExp):
table = k.startOfLineReadTable;
stringsThatMatch(stringsExp, "defReaderMacro");
case MetaExp("bof", stringsExp):
table = k.startOfFileReadTable;
stringsThatMatch(stringsExp, "defReaderMacro");
case MetaExp("eof", stringsExp):
table = k.endOfFileReadTable;
stringsThatMatch(stringsExp, "defReaderMacro");
default:
stringsThatMatch(exps[0], "defReaderMacro");
};
@@ -591,6 +597,12 @@ class Macros {
case MetaExp("start", stringsExp):
table = k.startOfLineReadTable;
stringsThatMatch(stringsExp, "undefReaderMacro");
case MetaExp("bof", stringsExp):
table = k.startOfFileReadTable;
stringsThatMatch(stringsExp, "undefReaderMacro");
case MetaExp("eof", stringsExp):
table = k.endOfFileReadTable;
stringsThatMatch(stringsExp, "undefReaderMacro");
default:
stringsThatMatch(exps[0], "undefReaderMacro");
};

View File

@@ -287,8 +287,30 @@ class Reader {
They can't be read all at once because some expressions change the Readtable state
**/
public static function readAndProcess(stream:Stream, k:KissState, process:(ReaderExp) -> Void) {
for (key => func in k.startOfFileReadTable) {
if (stream.startsWith(key)) {
var pos = stream.position();
stream.dropString(key);
var v = func(stream, k);
if (v != null)
process(v.withPos(pos));
break;
}
}
while (true) {
stream.dropWhitespace();
for (key => func in k.endOfFileReadTable) {
if (stream.content == key) {
var pos = stream.position();
stream.dropString(key);
var v = func(stream, k);
if (v != null)
process(v.withPos(pos));
break;
}
}
if (stream.isEmpty())
break;
var position = stream.position();