Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Crocker <dcrocker@eschertech.com>2019-12-23 22:37:31 +0300
committerDavid Crocker <dcrocker@eschertech.com>2019-12-23 22:37:31 +0300
commitecbc545743695c73791ed8420bb4ad97266bec28 (patch)
tree4c33bb0c9efcd92e5bf8d219c447f6cfde894ae8 /src/ObjectModel
parent7cf6080c42246a0bfea1e807325d312a457e13d3 (diff)
Handle command parsing errors using exceptions
Diffstat (limited to 'src/ObjectModel')
-rw-r--r--src/ObjectModel/ObjectModel.cpp33
-rw-r--r--src/ObjectModel/ObjectModel.h21
2 files changed, 31 insertions, 23 deletions
diff --git a/src/ObjectModel/ObjectModel.cpp b/src/ObjectModel/ObjectModel.cpp
index c1d487ec..39620eff 100644
--- a/src/ObjectModel/ObjectModel.cpp
+++ b/src/ObjectModel/ObjectModel.cpp
@@ -10,6 +10,7 @@
#if SUPPORT_OBJECT_MODEL
#include "OutputMemory.h"
+#include <GCodes/GCodeBuffer/StringParser.h>
#include <cstring>
#include <General/SafeStrtod.h>
@@ -256,33 +257,35 @@ int ObjectModelTableEntry::IdCompare(const char *id) const
}
// Get the value of an object when we don't know what its type is
-TypeCode ObjectModel::GetObjectValue(ExpressionValue& val, const char *idString)
+ExpressionValue ObjectModel::GetObjectValue(const StringParser& sp, const char *idString)
{
+ ExpressionValue val;
+
const ObjectModelTableEntry *e = FindObjectModelTableEntry(idString);
if (e == nullptr)
{
- return NoType;
+ throw sp.ConstructParseException("unknown variable %s", idString);
}
idString = GetNextElement(idString);
void * param = e->param(this);
- TypeCode tc = e->type;
- if ((tc & IsArray) != 0)
+ val.type = e->type;
+ if ((val.type & IsArray) != 0)
{
if (*idString != '[')
{
- return NoType; // no array index is provided, and we don't currently allow an entire array to be returned
+ throw sp.ConstructParseException("can't use whole array");
}
const char *endptr;
- const unsigned long val = SafeStrtoul(idString + 1, &endptr);
+ const unsigned long index = SafeStrtoul(idString + 1, &endptr);
if (endptr == idString + 1 || *endptr != ']')
{
- return NoType; // invalid syntax
+ throw sp.ConstructParseException("expected ']'");
}
const ObjectModelArrayDescriptor *arr = (const ObjectModelArrayDescriptor*)param;
- if (val >= arr->GetNumElements(this))
+ if (index >= arr->GetNumElements(this))
{
- return NoType; // array index out of range
+ throw sp.ConstructParseException("array index out of bounds");
}
idString = endptr + 1; // skip past the ']'
@@ -290,14 +293,14 @@ TypeCode ObjectModel::GetObjectValue(ExpressionValue& val, const char *idString)
{
++idString; // skip any '.' after it because it could be an array of objects
}
- tc &= ~IsArray; // clear the array flag
- param = arr->GetElement(this, val); // fetch the pointer to the array element
+ val.type &= ~IsArray; // clear the array flag
+ param = arr->GetElement(this, index); // fetch the pointer to the array element
}
- switch (tc)
+ switch (val.type)
{
case TYPE_OF(ObjectModel):
- return ((ObjectModel*)param)->GetObjectValue(val, idString);
+ return ((ObjectModel*)param)->GetObjectValue(sp, idString);
case TYPE_OF(float):
case TYPE_OF(Float2):
@@ -328,9 +331,9 @@ TypeCode ObjectModel::GetObjectValue(ExpressionValue& val, const char *idString)
break;
default:
- return NoType;
+ throw sp.ConstructParseException("unknown type");
}
- return tc;
+ return val;
}
// Template specialisations
diff --git a/src/ObjectModel/ObjectModel.h b/src/ObjectModel/ObjectModel.h
index e1226dd7..5c5e7ea7 100644
--- a/src/ObjectModel/ObjectModel.h
+++ b/src/ObjectModel/ObjectModel.h
@@ -23,15 +23,20 @@ constexpr TypeCode NoType = 0; // code for an invalid or unknown type
// Forward declarations
class ObjectModelTableEntry;
class ObjectModel;
+class StringParser;
-union ExpressionValue
+struct ExpressionValue
{
- bool bVal;
- float fVal;
- int32_t iVal;
- uint32_t uVal;
- const char *sVal;
- const ObjectModel *omVal;
+ TypeCode type;
+ union
+ {
+ bool bVal;
+ float fVal;
+ int32_t iVal;
+ uint32_t uVal;
+ const char *sVal;
+ const ObjectModel *omVal;
+ };
};
// Dummy types, used to define type codes
@@ -58,7 +63,7 @@ public:
TypeCode GetObjectType(const char *idString);
// Get the value of an object when we don't know what its type is
- TypeCode GetObjectValue(ExpressionValue& val, const char *idString);
+ ExpressionValue GetObjectValue(const StringParser& sp, const char *idString);
// Specialisation of above for float, allowing conversion from integer to float
bool GetObjectValue(float& val, const char *idString);