Show (short) list of possible enum values when an invalid value is encountered (resolves #1318)

All enum errors have (hopefully) been changed to a luax_enumerror, which has a
fixed error message. If additionally a list of valid options is passed, it
lists that in the error message. For every enum error with few options I've
implemented this using a getConstants call.

This solution has been designed specifically to reduce the number of template
instantiations (as I've been told that was a concern). All new template code
happens in places where there already was an instantiation of the relevant
StringMap. Of course there is still std::vector<std::string>...

--HG--
branch : minor
This commit is contained in:
Bart van Strien
2017-10-02 17:11:53 +02:00
parent ac84589c57
commit 9e4374935d
72 changed files with 366 additions and 101 deletions
+15
View File
@@ -23,6 +23,9 @@
#include "Exception.h"
#include <string>
#include <vector>
namespace love
{
@@ -145,6 +148,18 @@ public:
return hash;
}
std::vector<std::string> getNames() const
{
std::vector<std::string> names;
names.reserve(SIZE);
for (unsigned int i = 0; i < SIZE; ++i)
if (reverse[i] != nullptr)
names.emplace_back(reverse[i]);
return names;
}
private:
struct Record
+20
View File
@@ -31,6 +31,7 @@
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <sstream>
namespace love
{
@@ -832,6 +833,25 @@ extern "C" int luax_typerror(lua_State *L, int narg, const char *tname)
return luaL_argerror(L, narg, msg);
}
int luax_enumerror(lua_State *L, const char *enumName, const char *value)
{
return luaL_error(L, "Invalid %s: %s", enumName, value);
}
int luax_enumerror(lua_State *L, const char *enumName, const std::vector<std::string> &values, const char *value)
{
std::stringstream valueStream;
bool first = true;
for (auto value : values)
{
valueStream << (first ? "'" : ", '") << value << "'";
first = false;
}
std::string valueString = valueStream.str();
return luaL_error(L, "Invalid %s '%s', expected one of: %s", enumName, value, valueString.c_str());
}
size_t luax_objlen(lua_State *L, int ndx)
{
#if LUA_VERSION_NUM == 501
+3
View File
@@ -459,6 +459,9 @@ extern "C" { // Also called from luasocket
int luax_typerror(lua_State *L, int narg, const char *tname);
}
int luax_enumerror(lua_State *L, const char *enumName, const char *value);
int luax_enumerror(lua_State *L, const char *enumName, const std::vector<std::string> &values, const char *value);
/**
* Calls luax_objlen/lua_rawlen depending on version
**/