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

View File

@@ -527,6 +527,12 @@ class Macros {
case MetaExp("start", stringsExp): case MetaExp("start", stringsExp):
table = k.startOfLineReadTable; table = k.startOfLineReadTable;
stringsThatMatch(stringsExp, "defReaderMacro"); stringsThatMatch(stringsExp, "defReaderMacro");
case MetaExp("bof", stringsExp):
table = k.startOfFileReadTable;
stringsThatMatch(stringsExp, "defReaderMacro");
case MetaExp("eof", stringsExp):
table = k.endOfFileReadTable;
stringsThatMatch(stringsExp, "defReaderMacro");
default: default:
stringsThatMatch(exps[0], "defReaderMacro"); stringsThatMatch(exps[0], "defReaderMacro");
}; };
@@ -591,6 +597,12 @@ class Macros {
case MetaExp("start", stringsExp): case MetaExp("start", stringsExp):
table = k.startOfLineReadTable; table = k.startOfLineReadTable;
stringsThatMatch(stringsExp, "undefReaderMacro"); stringsThatMatch(stringsExp, "undefReaderMacro");
case MetaExp("bof", stringsExp):
table = k.startOfFileReadTable;
stringsThatMatch(stringsExp, "undefReaderMacro");
case MetaExp("eof", stringsExp):
table = k.endOfFileReadTable;
stringsThatMatch(stringsExp, "undefReaderMacro");
default: default:
stringsThatMatch(exps[0], "undefReaderMacro"); 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 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) { 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) { while (true) {
stream.dropWhitespace(); 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()) if (stream.isEmpty())
break; break;
var position = stream.position(); var position = stream.position();