mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
committed by
Wouter van Oortmerssen
parent
dd85c3b721
commit
dd6daa709b
@@ -135,21 +135,21 @@ template<typename F> CheckedError Parser::Recurse(F f) {
|
||||
return ce;
|
||||
}
|
||||
|
||||
CheckedError Parser::InvalidNumber(const char *number, const std::string &msg) {
|
||||
return Error("invalid number: \"" + std::string(number) + "\"" + msg);
|
||||
template<typename T> std::string TypeToIntervalString() {
|
||||
return "[" + NumToString((flatbuffers::numeric_limits<T>::lowest)()) + "; " +
|
||||
NumToString((flatbuffers::numeric_limits<T>::max)()) + "]";
|
||||
}
|
||||
// atot: templated version of atoi/atof: convert a string to an instance of T.
|
||||
|
||||
// atot: template version of atoi/atof: convert a string to an instance of T.
|
||||
template<typename T>
|
||||
inline CheckedError atot(const char *s, Parser &parser, T *val) {
|
||||
auto done = StringToNumber(s, val);
|
||||
if (done) return NoError();
|
||||
|
||||
return parser.InvalidNumber(
|
||||
s, (0 == *val)
|
||||
? ""
|
||||
: (", constant does not fit [" +
|
||||
NumToString(flatbuffers::numeric_limits<T>::lowest()) + "; " +
|
||||
NumToString(flatbuffers::numeric_limits<T>::max()) + "]"));
|
||||
if (0 == *val)
|
||||
return parser.Error("invalid number: \"" + std::string(s) + "\"");
|
||||
else
|
||||
return parser.Error("invalid number: \"" + std::string(s) + "\"" +
|
||||
", constant does not fit " + TypeToIntervalString<T>());
|
||||
}
|
||||
template<>
|
||||
inline CheckedError atot<Offset<void>>(const char *s, Parser &parser,
|
||||
@@ -1254,7 +1254,7 @@ CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field,
|
||||
nested_parser.enums_.vec.clear();
|
||||
|
||||
if (!ok) {
|
||||
ECHECK(Error(nested_parser.error_));
|
||||
ECHECK(Error(nested_parser.error_));
|
||||
}
|
||||
// Force alignment for nested flatbuffer
|
||||
builder_.ForceVectorAlignment(nested_parser.builder_.GetSize(), sizeof(uint8_t),
|
||||
@@ -1334,8 +1334,9 @@ CheckedError Parser::TryTypedValue(const std::string *name, int dtoken,
|
||||
return NoError();
|
||||
}
|
||||
|
||||
CheckedError Parser::ParseEnumFromString(Type &type, int64_t *result) {
|
||||
*result = 0;
|
||||
CheckedError Parser::ParseEnumFromString(const Type &type,
|
||||
std::string *result) {
|
||||
int64_t i64 = 0;
|
||||
// Parse one or more enum identifiers, separated by spaces.
|
||||
const char *next = attribute_.c_str();
|
||||
do {
|
||||
@@ -1353,7 +1354,7 @@ CheckedError Parser::ParseEnumFromString(Type &type, int64_t *result) {
|
||||
if (!enum_val)
|
||||
return Error("unknown enum value: " + word +
|
||||
", for enum: " + type.enum_def->name);
|
||||
*result |= enum_val->value;
|
||||
i64 |= enum_val->value;
|
||||
} else { // No enum type, probably integral field.
|
||||
if (!IsInteger(type.base_type))
|
||||
return Error("not a valid value for this field: " + word);
|
||||
@@ -1367,9 +1368,10 @@ CheckedError Parser::ParseEnumFromString(Type &type, int64_t *result) {
|
||||
if (!enum_def) return Error("unknown enum: " + enum_def_str);
|
||||
auto enum_val = enum_def->vals.Lookup(enum_val_str);
|
||||
if (!enum_val) return Error("unknown enum value: " + enum_val_str);
|
||||
*result |= enum_val->value;
|
||||
i64 |= enum_val->value;
|
||||
}
|
||||
} while (*next);
|
||||
*result = NumToString(i64);
|
||||
return NoError();
|
||||
}
|
||||
|
||||
@@ -1503,9 +1505,7 @@ CheckedError Parser::ParseSingleValue(const std::string *name, Value &e,
|
||||
// Enum can have only true integer base type.
|
||||
if (!match && IsInteger(e.type.base_type) && !IsBool(e.type.base_type) &&
|
||||
IsIdentifierStart(*attribute_.c_str())) {
|
||||
int64_t val;
|
||||
ECHECK(ParseEnumFromString(e.type, &val));
|
||||
e.constant = NumToString(val);
|
||||
ECHECK(ParseEnumFromString(e.type, &e.constant));
|
||||
NEXT();
|
||||
match = true;
|
||||
}
|
||||
@@ -2482,8 +2482,8 @@ CheckedError Parser::ParseRoot(const char *source, const char **include_paths,
|
||||
for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
|
||||
auto &enum_def = **it;
|
||||
if (enum_def.is_union) {
|
||||
for (auto val_it = enum_def.vals.vec.begin();
|
||||
val_it != enum_def.vals.vec.end(); ++val_it) {
|
||||
for (auto val_it = enum_def.Vals().begin();
|
||||
val_it != enum_def.Vals().end(); ++val_it) {
|
||||
auto &val = **val_it;
|
||||
if (!SupportsAdvancedUnionFeatures() && val.union_type.struct_def &&
|
||||
val.union_type.struct_def->fixed)
|
||||
@@ -3192,7 +3192,7 @@ std::string Parser::ConformTo(const Parser &base) {
|
||||
enum_def.defined_namespace->GetFullyQualifiedName(enum_def.name);
|
||||
auto enum_def_base = base.enums_.Lookup(qualified_name);
|
||||
if (!enum_def_base) continue;
|
||||
for (auto evit = enum_def.vals.vec.begin(); evit != enum_def.vals.vec.end();
|
||||
for (auto evit = enum_def.Vals().begin(); evit != enum_def.Vals().end();
|
||||
++evit) {
|
||||
auto &enum_val = **evit;
|
||||
auto enum_val_base = enum_def_base->vals.Lookup(enum_val.name);
|
||||
|
||||
Reference in New Issue
Block a user