Implemented the keyword argument parser.

This commit is contained in:
2022-07-22 16:00:37 -04:00
parent 4735c1fd7d
commit 001add12bf
7 changed files with 116 additions and 113 deletions

View File

@@ -827,90 +827,61 @@ void PlaneMap::untrack_all() {
}
}
eng::string PlaneScan::configure(const LuaStack &LS0, LuaSlot config) {
void PlaneScan::configure(LuaKeywordParser &kp) {
lua_State *L = kp.state();
LuaVar val, vx, vy, vz;
LuaStack LS(LS0.state(), val, vx, vy, vz);
if (!LS.istable(config)) {
return "scan configuration is not a table";
}
LuaStack LS(L, val, vx, vy, vz);
bool have_plane = false;
bool have_center = false;
bool have_radius = false;
bool have_shape = false;
bool have_near = false;
int parameters = 0;
LS.rawget(val, config, "plane");
if (!LS.isnil(val)) {
if (!LS.isstring(val)) {
return "scan configuration: 'plane' must be a string";
}
if (kp.parse(val, "plane")) {
LS.checkstring(val, "plane");
plane_ = LS.ckstring(val);
have_plane = true;
parameters += 1;
}
LS.rawget(vx, config, "centerx");
LS.rawget(vy, config, "centery");
LS.rawget(vz, config, "centerz");
kp.parse(vx, "centerx");
kp.parse(vy, "centery");
kp.parse(vz, "centerz");
if ((!LS.isnil(vx)) || (!LS.isnil(vy)) || (!LS.isnil(vz))) {
if (!LS.isnumber(vx)) {
return "scan configuration: 'centerx' must be a number";
}
if (!LS.isnumber(vy)) {
return "scan configuration: 'centery' must be a number";
}
if (!LS.isnumber(vz)) {
return "scan configuration: 'centerz' must be a number";
}
LS.checknumber(vx, "centerx");
LS.checknumber(vy, "centery");
LS.checknumber(vz, "centerz");
center_.x = LS.cknumber(vx);
center_.y = LS.cknumber(vy);
center_.z = LS.cknumber(vz);
have_center = true;
parameters += 3;
}
LS.rawget(val, config, "radius");
if (!LS.isnil(val)) {
if (!LS.isnumber(val)) {
return "scan configuration: 'radius' must be a number";
}
if (kp.parse(val, "radius")) {
LS.checknumber(val, "radius");
radius_.x = LS.cknumber(val);
radius_.y = radius_.z = radius_.x;
have_radius = true;
parameters += 1;
}
LS.rawget(vx, config, "radiusx");
LS.rawget(vy, config, "radiusy");
LS.rawget(vz, config, "radiusz");
kp.parse(vx, "radiusx");
kp.parse(vy, "radiusy");
kp.parse(vz, "radiusz");
if ((!LS.isnil(vx)) || (!LS.isnil(vy)) || (!LS.isnil(vz))) {
if (!LS.isnumber(vx)) {
return "scan configuration: 'radiusx' must be a number";
}
if (!LS.isnumber(vy)) {
return "scan configuration: 'radiusy' must be a number";
}
if (!LS.isnumber(vz)) {
return "scan configuration: 'radiusz' must be a number";
}
LS.checknumber(vx, "radiusx");
LS.checknumber(vy, "radiusy");
LS.checknumber(vz, "radiusz");
if (have_radius) {
return "scan configuration: specified both 'radius' and 'radiusx'";
luaL_error(L, "scan configuration: specified both 'radius' and 'radiusx'");
}
radius_.x = LS.cknumber(vx);
radius_.y = LS.cknumber(vy);
radius_.z = LS.cknumber(vz);
have_radius = true;
parameters += 3;
}
LS.rawget(val, config, "shape");
if (!LS.isnil(val)) {
if (!LS.isstring(val)) {
return "scan configuration: 'shape' must be a string";
}
if (kp.parse(val, "shape")) {
LS.checkstring(val, "shape");
eng::string shape = LS.ckstring(val);
if (shape == "box") {
shape_ = BOX;
@@ -919,50 +890,40 @@ eng::string PlaneScan::configure(const LuaStack &LS0, LuaSlot config) {
} else if (shape == "cylinder") {
shape_ = CYLINDER;
} else {
return util::ss("scan configuration: unknown shape ", shape);
luaL_error(L, "scan configuration: unknown shape %s", shape.c_str());
}
have_shape = true;
parameters += 1;
}
LS.rawget(val, config, "near");
if (!LS.isnil(val)) {
if (kp.parse(val, "near")) {
int64_t id = LS.tanid(val);
if (id == 0) {
return "scan configuration: 'near' must be a tangible";
luaL_error(L, "scan configuration: 'near' must be a tangible");
}
if (have_center) {
return "scan configuration: specified both 'center' and 'near'";
luaL_error(L, "scan configuration: specified both 'center' and 'near'");
}
if (have_plane) {
return "scan configuration: specified both 'plane' and 'near'";
luaL_error(L, "scan configuration: specified both 'plane' and 'near'");
}
near_ = id;
have_center = true;
have_plane = true;
have_near = true;
parameters += 1;
}
LS.rawget(val, config, "include");
if (!LS.isnil(val)) {
if (!LS.isboolean(val)) {
return "scan configuration: 'include' must be a boolean";
}
if (kp.parse(val, "include")) {
LS.checkboolean(val, "include");
if (!have_near) {
return "scan configuration: 'include' specified without 'near'";
luaL_error(L, "scan configuration: 'include' specified without 'near'");
}
include_near_ = LS.ckboolean(val);
parameters += 1;
}
LS.rawget(val, config, "wholeplane");
if (!LS.isnil(val)) {
if (!LS.isstring(val)) {
return "scan configuration: 'wholeplane' must be a string";
}
if (kp.parse(val, "wholeplane")) {
LS.checkstring(val, "wholeplane");
if (have_plane || have_center || have_radius || have_shape) {
return "scan configuration: do not specify plane, center, shape, or radius with 'wholeplane'";
luaL_error(L, "scan configuration: do not specify plane, center, shape, or radius with 'wholeplane'");
}
plane_ = LS.ckstring(val);
set_whole_plane();
@@ -970,26 +931,20 @@ eng::string PlaneScan::configure(const LuaStack &LS0, LuaSlot config) {
have_center = true;
have_radius = true;
have_shape = true;
parameters += 1;
}
if (lua_nkeys(LS.state(), config.index()) != parameters) {
return "scan configuration: unrecognized parameters in table";
}
if (!have_plane) {
return "scan configuration: did not specify plane";
luaL_error(L, "scan configuration: did not specify plane");
}
if (!have_radius) {
return "scan configuration: did not specify radius";
luaL_error(L, "scan configuration: did not specify radius");
}
if (!have_center) {
return "scan configuration: did not specify center";
luaL_error(L, "scan configuration: did not specify center");
}
if (!have_shape) {
shape_ = SPHERE; // default value.
}
return "";
}
eng::string PlaneScan::debug_string() const {