[update] Unified the exceptions thrown by sjscript.

This commit is contained in:
falsycat 2019-10-10 00:00:00 +00:00
parent b4690f9603
commit 109ae328b8
6 changed files with 43 additions and 37 deletions

View File

@ -0,0 +1,22 @@
/// License: MIT
module sjscript.ScriptException;
import dast.tokenize;
///
class ScriptException : Exception {
public:
///
this(string msg, TokenPos pos, string file = __FILE__, size_t line = __LINE__) {
super(msg, file, line);
this.pos = pos;
}
///
this(
string msg, size_t stline, size_t stchar,
string file = __FILE__, size_t line = __LINE__) {
super(msg, file, line);
pos = TokenPos(stline, stline, stchar, stchar+1);
}
const TokenPos pos;
}

View File

@ -1,27 +0,0 @@
/// License: MIT
module sjscript.exception;
import sjscript.Token;
///
class ScriptException : Exception {
public:
///
this(string msg, Token token, string file = __FILE__, size_t line = __LINE__) {
super(msg, file, line);
this.token = token;
}
const Token token;
}
///
class PreprocessException : ScriptException {
public:
mixin ExceptionConstructor;
}
private mixin template ExceptionConstructor() {
public:
this(string msg, Token token, string file = __FILE__, size_t line = __LINE__) {
super(msg, token, file, line);
}
}

View File

@ -38,6 +38,6 @@ float CallFunction(string name, float[] args) {
case "step": EnforceArgs!2; return args[1] > args[0]? 1: 0;
case "clamp": EnforceArgs!3; return clamp(args[0], args[1], args[2]);
default: throw new Exception("unknown exception");
default: throw new Exception("unknown function");
}
}

View File

@ -11,10 +11,9 @@ import sjscript.Token,
sjscript.preprocess;
public {
import sjscript.Expression,
sjscript.ParametersBlock,
sjscript.calculate,
sjscript.exception;
import sjscript.ParametersBlock,
sjscript.ScriptException,
sjscript.calculate;
}
///

View File

@ -8,6 +8,7 @@ import dast.parse;
import sjscript.Expression,
sjscript.ParametersBlock,
sjscript.ScriptException,
sjscript.Token,
sjscript.calculate;
@ -33,7 +34,18 @@ EOS";
///
ParametersBlock[] Parse(R)(R tokens)
if (isInputRange!R && is(ElementType!R == Token)) {
import dast.tokenize : TokenizeException;
alias Token = ElementType!R;
try {
return dast.parse.Parse!Whole(tokens, cast(RuleSet) null).blocks;
} catch (TokenizeException e) {
throw new ScriptException(e.msg, e.srcline, e.srcchar);
} catch (ParseException!Token e) {
throw new ScriptException(e.msg, e.token.pos);
}
}
private class RuleSet {

View File

@ -9,8 +9,8 @@ import std.algorithm,
std.range.primitives,
std.typecons;
import sjscript.Token,
sjscript.exception;
import sjscript.ScriptException,
sjscript.Token;
///
unittest {
@ -46,7 +46,7 @@ EOS";
enum src = q"EOS
$unknown_template
EOS";
assertThrown!PreprocessException(Tokenize(src).array);
assertThrown!ScriptException(Tokenize(src).array);
}
}
@ -243,5 +243,5 @@ private struct Preprocessor(R)
private void enforce(T)(T val, string msg, lazy Token token,
string file = __FILE__, size_t line = __LINE__) {
if (!val) throw new PreprocessException(msg, token, file, line);
if (!val) throw new ScriptException(msg, token.pos, file, line);
}