Make Reader not rely on KissState specifically

This commit is contained in:
2024-12-10 19:54:27 -06:00
parent abdbc976e1
commit 4a457f57a7
3 changed files with 23 additions and 21 deletions

View File

@@ -41,13 +41,10 @@ typedef FormDoc = {
}; };
typedef KissState = { typedef KissState = {
> HasReadTables,
className:String, className:String,
pack:Array<String>, pack:Array<String>,
file:String, file:String,
readTable:ReadTable,
startOfLineReadTable:ReadTable,
startOfFileReadTable:ReadTable,
endOfFileReadTable:ReadTable,
fieldForms:Map<String, FieldFormFunction>, fieldForms:Map<String, FieldFormFunction>,
specialForms:Map<String, SpecialFormFunction>, specialForms:Map<String, SpecialFormFunction>,
specialFormMacroExpanders:Map<String, MacroFunction>, specialFormMacroExpanders:Map<String, MacroFunction>,
@@ -57,7 +54,6 @@ typedef KissState = {
wrapListExps:Bool, wrapListExps:Bool,
loadedFiles:Map<String, Null<ReaderExp>>, loadedFiles:Map<String, Null<ReaderExp>>,
callAliases:Map<String, ReaderExpDef>, callAliases:Map<String, ReaderExpDef>,
identAliases:Map<String, ReaderExpDef>,
typeAliases:Map<String, String>, typeAliases:Map<String, String>,
fieldList:Array<Field>, fieldList:Array<Field>,
// TODO This map was originally created to track whether the programmer wrote their own main function, but could also // TODO This map was originally created to track whether the programmer wrote their own main function, but could also

View File

@@ -610,7 +610,7 @@ class Macros {
if (streamArgName == null) throw messageForBadArgs; if (streamArgName == null) throw messageForBadArgs;
for (s in strings) { for (s in strings) {
table[s] = (stream, k) -> { table[s] = (stream, k:Dynamic) -> {
if (strings.length > 1) { if (strings.length > 1) {
stream.putBackString(s); stream.putBackString(s);
} }

View File

@@ -2,12 +2,11 @@ package kiss;
import haxe.ds.Option; import haxe.ds.Option;
import kiss.Stream; import kiss.Stream;
import kiss.Kiss;
import kiss.ReaderExp; import kiss.ReaderExp;
using kiss.Reader; using kiss.Reader;
using kiss.ExpBuilder;
using kiss.Stream; using kiss.Stream;
using kiss.Helpers;
using StringTools; using StringTools;
class UnmatchedBracketSignal { class UnmatchedBracketSignal {
@@ -22,8 +21,15 @@ class UnmatchedBracketSignal {
} }
} }
typedef ReadFunction = (Stream, KissState) -> Null<ReaderExpDef>; typedef ReadFunction = (Stream, HasReadTables) -> Null<ReaderExpDef>;
typedef ReadTable = Map<String, ReadFunction>; typedef ReadTable = Map<String, ReadFunction>;
typedef HasReadTables = {
readTable:ReadTable,
startOfLineReadTable:ReadTable,
startOfFileReadTable:ReadTable,
endOfFileReadTable:ReadTable,
identAliases:Map<String,ReaderExpDef>
};
@:allow(kiss.Helpers) @:allow(kiss.Helpers)
class Reader { class Reader {
@@ -163,7 +169,7 @@ class Reader {
// -+>countVar {body} // -+>countVar {body}
// -+>countVar (body) // -+>countVar (body)
// or any of those with the first expression after -> or -+> prefixed by :Void // or any of those with the first expression after -> or -+> prefixed by :Void
function arrowSyntax(countingLambda:Bool, stream:Stream, k:KissState) { function arrowSyntax(countingLambda:Bool, stream:Stream, k:HasReadTables) {
var countVar = if (countingLambda) { var countVar = if (countingLambda) {
_assertRead(stream, k); _assertRead(stream, k);
} else { } else {
@@ -209,7 +215,7 @@ class Reader {
readTable["-+>"] = arrowSyntax.bind(true); readTable["-+>"] = arrowSyntax.bind(true);
// Because macro keys are sorted by length and peekChars(0) returns "", this will be used as the default reader macro: // Because macro keys are sorted by length and peekChars(0) returns "", this will be used as the default reader macro:
readTable[""] = (stream:Stream, k:KissState) -> { readTable[""] = (stream:Stream, k:HasReadTables) -> {
var position = stream.position(); var position = stream.position();
var token = nextToken(stream, "a symbol name"); var token = nextToken(stream, "a symbol name");
// Process dot-access on alias identifiers // Process dot-access on alias identifiers
@@ -263,11 +269,11 @@ class Reader {
} }
} }
public static function assertRead(stream:Stream, k:KissState):ReaderExp { public static function assertRead(stream:Stream, k:HasReadTables):ReaderExp {
return _assertRead(stream, k); return _assertRead(stream, k);
} }
static function _assertRead(stream:Stream, k:KissState):ReaderExp { static function _assertRead(stream:Stream, k:HasReadTables):ReaderExp {
var position = stream.position(); var position = stream.position();
return switch (_read(stream, k)) { return switch (_read(stream, k)) {
case Some(exp): case Some(exp):
@@ -294,12 +300,12 @@ class Reader {
return null; return null;
} }
public static function read(stream:Stream, k:KissState):Option<ReaderExp> { public static function read(stream:Stream, k:HasReadTables):Option<ReaderExp> {
assertNoPriorState(stream); assertNoPriorState(stream);
return _read(stream, k); return _read(stream, k);
} }
static function _read(stream:Stream, k:KissState):Option<ReaderExp> { static function _read(stream:Stream, k:HasReadTables):Option<ReaderExp> {
var readTable = k.readTable; var readTable = k.readTable;
stream.dropWhitespace(); stream.dropWhitespace();
@@ -327,7 +333,7 @@ class Reader {
} }
} }
static function _readOr(stream:Stream, k:KissState, defaultExp:ReaderExp):ReaderExp { static function _readOr(stream:Stream, k:HasReadTables, defaultExp:ReaderExp):ReaderExp {
return switch (_read(stream, k)) { return switch (_read(stream, k)) {
case Some(exp): case Some(exp):
exp; exp;
@@ -356,12 +362,12 @@ class Reader {
} }
} }
public static function readExpArray(stream:Stream, end:String, k:KissState, allowEof=false, startingPos=null):Array<ReaderExp> { public static function readExpArray(stream:Stream, end:String, k:HasReadTables, allowEof=false, startingPos=null):Array<ReaderExp> {
assertNoPriorState(stream); assertNoPriorState(stream);
return _readExpArray(stream, end, k, allowEof, startingPos); return _readExpArray(stream, end, k, allowEof, startingPos);
} }
static function _readExpArray(stream:Stream, end:String, k:KissState, allowEof=false, startingPos=null):Array<ReaderExp> { static function _readExpArray(stream:Stream, end:String, k:HasReadTables, allowEof=false, startingPos=null):Array<ReaderExp> {
var array = []; var array = [];
if (startingPos == null) if (startingPos == null)
startingPos = stream.position(); startingPos = stream.position();
@@ -405,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:KissState, process:(ReaderExp) -> Void, nested = false) { public static function readAndProcess(stream:Stream, k:HasReadTables, process:(ReaderExp) -> Void, nested = false) {
if (nested) { if (nested) {
nestedReadExpArrayStartPositions.push(readExpArrayStartPositions); nestedReadExpArrayStartPositions.push(readExpArrayStartPositions);
readExpArrayStartPositions = []; readExpArrayStartPositions = [];
@@ -469,7 +475,7 @@ class Reader {
} }
// Read a string literal OR a shell section which supports interpolation // Read a string literal OR a shell section which supports interpolation
static function readString(stream:Stream, k:KissState, shell = false) { static function readString(stream:Stream, k:HasReadTables, shell = false) {
var pos = stream.position(); var pos = stream.position();
var stringParts:Array<ReaderExp> = []; var stringParts:Array<ReaderExp> = [];
var currentStringPart = ""; var currentStringPart = "";
@@ -554,7 +560,7 @@ class Reader {
} }
// Read a raw string literal // Read a raw string literal
static function readRawString(stream:Stream, k:KissState) { static function readRawString(stream:Stream, k:HasReadTables) {
var terminator = '"#'; var terminator = '"#';
do { do {
var next = stream.expect('# or "', () -> stream.takeChars(1)); var next = stream.expect('# or "', () -> stream.takeChars(1));