diff --git a/sjscript/src/sjscript/ScriptException.d b/sjscript/src/sjscript/ScriptException.d new file mode 100644 index 0000000..19f036b --- /dev/null +++ b/sjscript/src/sjscript/ScriptException.d @@ -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; +} diff --git a/sjscript/src/sjscript/exception.d b/sjscript/src/sjscript/exception.d deleted file mode 100644 index 31cf1cb..0000000 --- a/sjscript/src/sjscript/exception.d +++ /dev/null @@ -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); - } -} diff --git a/sjscript/src/sjscript/func.d b/sjscript/src/sjscript/func.d index 4d1b2b8..f20f102 100644 --- a/sjscript/src/sjscript/func.d +++ b/sjscript/src/sjscript/func.d @@ -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"); } } diff --git a/sjscript/src/sjscript/package.d b/sjscript/src/sjscript/package.d index d594ae8..a30be80 100644 --- a/sjscript/src/sjscript/package.d +++ b/sjscript/src/sjscript/package.d @@ -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; } /// diff --git a/sjscript/src/sjscript/parse.d b/sjscript/src/sjscript/parse.d index 5900e18..a44ee05 100644 --- a/sjscript/src/sjscript/parse.d +++ b/sjscript/src/sjscript/parse.d @@ -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)) { - return dast.parse.Parse!Whole(tokens, cast(RuleSet) null).blocks; + 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 { diff --git a/sjscript/src/sjscript/preprocess.d b/sjscript/src/sjscript/preprocess.d index d73d2fd..fb079a1 100644 --- a/sjscript/src/sjscript/preprocess.d +++ b/sjscript/src/sjscript/preprocess.d @@ -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); }