A simple scripting language in C++
Ferenc Szontágh
2025-04-14 c34b2c57219aa496a202c2be1e12332b4eeea440
src/ScriptException.hpp
@@ -37,6 +37,15 @@
    const Token & token() const { return token_; }
    static ScriptException makeUnexpectedEndOfFileError(const Token & token, const std::string & file = "",
                                                        int line = 0) {
        std::string msg = "unexpected end of file";
        if (!token.lexeme.empty()) {
            msg += " near '" + token.lexeme + "'";
        }
        return ScriptException(ScriptErrorType::UnexpectedToken, msg, file, line, token);
    }
    static ScriptException makeUnexpectedTokenError(const Token & token, const std::string & expected = "",
                                                    const std::string & file = "", int line = 0) {
        std::string msg = "unexpected token: '" + token.lexeme + "'";
@@ -46,7 +55,7 @@
#endif
        if (!expected.empty()) {
            msg += ", expected: '" + expected + "'";
            msg += ", expected " + expected;
        }
        return ScriptException(ScriptErrorType::UnexpectedToken, msg, file, line, token);
    }
@@ -61,7 +70,7 @@
                                                      const std::string & file = "", int line = 0) {
        std::string msg = "undefined function: '" + name + "'";
#if DEBUG_BUILD == 1
        msg.append(", type: " + tokenTypeNames.at(token.type));
        msg.append(", type: " + getTokenTypeAsString(token.type));
#endif
        return ScriptException(ScriptErrorType::UndefinedFunction, msg, file, line, token);
    }
@@ -99,6 +108,21 @@
        return ScriptException(ScriptErrorType::Custom, msg, file, line, token);
    }
    static ScriptException makeFunctionArgumentCountMismatchError(const std::string & functionName,
                                                                  const size_t & expected, size_t actual,
                                                                  const Token & token, const std::string & file = "",
                                                                  int line = 0) {
        std::string msg = "invalid argument count for function '" + functionName + "', expected " +
                          std::to_string(expected) + ", got " + std::to_string(actual);
        return ScriptException(ScriptErrorType::Custom, msg, file, line, token);
    }
    static ScriptException makeFunctionBodyEmptyError(const std::string & functionName, const Token & token,
                                                      const std::string & file = "", int line = 0) {
        std::string msg = "function '" + functionName + "' has no body";
        return ScriptException(ScriptErrorType::Custom, msg, file, line, token);
    }
  private:
    ScriptErrorType type_;
    std::string     file_;