expect macro for reader macros

This commit is contained in:
2022-12-11 23:24:43 +00:00
parent efd486c32d
commit 2a7d8e927d
3 changed files with 12 additions and 2 deletions

View File

@@ -641,9 +641,17 @@ class Kiss {
}; };
}; };
copy.macros["expect"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(3, null, "(expect <stream> <description> <stream method> <args...>)");
var b = wholeExp.expBuilder();
var streamSymbol = exps.shift();
b.callField("expect", streamSymbol, [exps.shift(), b.callSymbol("lambda", [b.list([]), b.callField(Prelude.symbolNameValue(exps.shift()), streamSymbol, exps)])]);
};
// TODO should this also be in forHScript()? // TODO should this also be in forHScript()?
// In macro evaluation, // In macro evaluation,
copy.macros.remove("eval"); copy.macros.remove("eval");
// BECAUSE it is provided as a function instead.
return copy; return copy;
} }

View File

@@ -39,6 +39,7 @@ abstract List<T>(Array<T>) from Array<T> to Array<T> {
return v; return v;
} }
// TODO deleting these should be fine, because the haxe Array functions already allow negative arguments
public function insert(idx:Int, v:T) { public function insert(idx:Int, v:T) {
this.insert(realIndex(idx), v); this.insert(realIndex(idx), v);
} }

View File

@@ -294,7 +294,7 @@ class Reader {
} }
} }
public static function readExpArray(stream:Stream, end:String, k:KissState, startingPos=null):Array<ReaderExp> { public static function readExpArray(stream:Stream, end:String, k:KissState, allowEof=false, startingPos=null):Array<ReaderExp> {
var array = []; var array = [];
if (startingPos == null) if (startingPos == null)
startingPos = stream.position(); startingPos = stream.position();
@@ -306,7 +306,8 @@ class Reader {
case Some(exp): case Some(exp):
array.push(exp); array.push(exp);
case None: case None:
throw new StreamError(startingPos, 'Ran out of expressions before $end was found.'); if (allowEof) return array;
else throw new StreamError(startingPos, 'Ran out of expressions before $end was found.');
} }
} catch (s:UnmatchedBracketSignal) { } catch (s:UnmatchedBracketSignal) {
if (s.type == end) if (s.type == end)