Fix Reader errors

This commit is contained in:
2021-01-25 20:02:36 -07:00
parent 5fa7c1bc39
commit aa0f6d29c1
3 changed files with 31 additions and 13 deletions

View File

@@ -68,6 +68,10 @@ class Kiss {
static function _try<T>(operation:() -> T):Null<T> {
try {
return operation();
} catch (err:StreamError) {
Sys.stderr().writeString(err + "\n");
Sys.exit(1);
return null;
} catch (err:CompileError) {
Sys.stderr().writeString(err + "\n");
Sys.exit(1);

View File

@@ -105,7 +105,7 @@ class Reader {
public static function nextToken(stream:Stream, expect:String) {
var tok = stream.expect(expect, () -> stream.takeUntilOneOf(terminators));
if (tok.length == 0) {
error(stream, 'Expected $expect');
stream.error('Expected $expect');
return null;
}
return tok;
@@ -117,7 +117,7 @@ class Reader {
case Some(exp):
exp;
case None:
error(stream, 'Ran out of Kiss expressions');
stream.error('Ran out of Kiss expressions');
return null;
};
}
@@ -264,7 +264,7 @@ class Reader {
case '$':
currentStringPart += '$';
default:
error(stream, 'unsupported escape sequence \\$escapeSequence');
stream.error('unsupported escape sequence \\$escapeSequence');
return null;
}
case '"':
@@ -290,19 +290,13 @@ class Reader {
case '"':
break;
default:
error(stream, 'Invalid syntax for raw string. Delete $next');
stream.error('Invalid syntax for raw string. Delete $next');
return null;
}
} while (true);
return StrExp(stream.expect('closing $terminator', () -> stream.takeUntilAndDrop(terminator)));
}
public static function error(stream:Stream, message:String) {
Sys.stderr().writeString('Kiss reader error!\n');
Sys.stderr().writeString(stream.position().toPrint() + ': $message\n');
Sys.exit(1);
}
public static function toString(exp:ReaderExpDef) {
return switch (exp) {
case CallExp(func, args):

View File

@@ -2,10 +2,10 @@ package kiss;
import sys.io.File;
import haxe.ds.Option;
import kiss.Reader;
using StringTools;
using Lambda;
using kiss.Stream;
typedef Position = {
file:String,
@@ -14,6 +14,22 @@ typedef Position = {
absoluteChar:Int
};
class StreamError {
var position:Position;
var message:String;
public function new(position:Position, message:String) {
this.position = position;
this.message = message;
}
public function toString() {
return '\nKiss reader error!\n'
+ position.toPrint()
+ ': $message\n';
}
}
class Stream {
public var content(default, null):String;
@@ -125,7 +141,7 @@ class Stream {
public function dropString(s:String) {
var toDrop = content.substr(0, s.length);
if (toDrop != s) {
Reader.error(this, 'Expected $s');
error(this, 'Expected $s');
}
dropChars(s.length);
}
@@ -168,8 +184,12 @@ class Stream {
case Some(s):
return s;
default:
Reader.error(this, 'Expected $whatToExpect');
error(this, 'Expected $whatToExpect');
return null;
}
}
public static function error(stream:Stream, message:String) {
throw new StreamError(stream.position(), message);
}
}