Fix Reader errors
This commit is contained in:
@@ -68,6 +68,10 @@ class Kiss {
|
|||||||
static function _try<T>(operation:() -> T):Null<T> {
|
static function _try<T>(operation:() -> T):Null<T> {
|
||||||
try {
|
try {
|
||||||
return operation();
|
return operation();
|
||||||
|
} catch (err:StreamError) {
|
||||||
|
Sys.stderr().writeString(err + "\n");
|
||||||
|
Sys.exit(1);
|
||||||
|
return null;
|
||||||
} catch (err:CompileError) {
|
} catch (err:CompileError) {
|
||||||
Sys.stderr().writeString(err + "\n");
|
Sys.stderr().writeString(err + "\n");
|
||||||
Sys.exit(1);
|
Sys.exit(1);
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ class Reader {
|
|||||||
public static function nextToken(stream:Stream, expect:String) {
|
public static function nextToken(stream:Stream, expect:String) {
|
||||||
var tok = stream.expect(expect, () -> stream.takeUntilOneOf(terminators));
|
var tok = stream.expect(expect, () -> stream.takeUntilOneOf(terminators));
|
||||||
if (tok.length == 0) {
|
if (tok.length == 0) {
|
||||||
error(stream, 'Expected $expect');
|
stream.error('Expected $expect');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return tok;
|
return tok;
|
||||||
@@ -117,7 +117,7 @@ class Reader {
|
|||||||
case Some(exp):
|
case Some(exp):
|
||||||
exp;
|
exp;
|
||||||
case None:
|
case None:
|
||||||
error(stream, 'Ran out of Kiss expressions');
|
stream.error('Ran out of Kiss expressions');
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -264,7 +264,7 @@ class Reader {
|
|||||||
case '$':
|
case '$':
|
||||||
currentStringPart += '$';
|
currentStringPart += '$';
|
||||||
default:
|
default:
|
||||||
error(stream, 'unsupported escape sequence \\$escapeSequence');
|
stream.error('unsupported escape sequence \\$escapeSequence');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case '"':
|
case '"':
|
||||||
@@ -290,19 +290,13 @@ class Reader {
|
|||||||
case '"':
|
case '"':
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
error(stream, 'Invalid syntax for raw string. Delete $next');
|
stream.error('Invalid syntax for raw string. Delete $next');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} while (true);
|
} while (true);
|
||||||
return StrExp(stream.expect('closing $terminator', () -> stream.takeUntilAndDrop(terminator)));
|
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) {
|
public static function toString(exp:ReaderExpDef) {
|
||||||
return switch (exp) {
|
return switch (exp) {
|
||||||
case CallExp(func, args):
|
case CallExp(func, args):
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ package kiss;
|
|||||||
|
|
||||||
import sys.io.File;
|
import sys.io.File;
|
||||||
import haxe.ds.Option;
|
import haxe.ds.Option;
|
||||||
import kiss.Reader;
|
|
||||||
|
|
||||||
using StringTools;
|
using StringTools;
|
||||||
using Lambda;
|
using Lambda;
|
||||||
|
using kiss.Stream;
|
||||||
|
|
||||||
typedef Position = {
|
typedef Position = {
|
||||||
file:String,
|
file:String,
|
||||||
@@ -14,6 +14,22 @@ typedef Position = {
|
|||||||
absoluteChar:Int
|
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 {
|
class Stream {
|
||||||
public var content(default, null):String;
|
public var content(default, null):String;
|
||||||
|
|
||||||
@@ -125,7 +141,7 @@ class Stream {
|
|||||||
public function dropString(s:String) {
|
public function dropString(s:String) {
|
||||||
var toDrop = content.substr(0, s.length);
|
var toDrop = content.substr(0, s.length);
|
||||||
if (toDrop != s) {
|
if (toDrop != s) {
|
||||||
Reader.error(this, 'Expected $s');
|
error(this, 'Expected $s');
|
||||||
}
|
}
|
||||||
dropChars(s.length);
|
dropChars(s.length);
|
||||||
}
|
}
|
||||||
@@ -168,8 +184,12 @@ class Stream {
|
|||||||
case Some(s):
|
case Some(s):
|
||||||
return s;
|
return s;
|
||||||
default:
|
default:
|
||||||
Reader.error(this, 'Expected $whatToExpect');
|
error(this, 'Expected $whatToExpect');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function error(stream:Stream, message:String) {
|
||||||
|
throw new StreamError(stream.position(), message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user