diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 0ef9bbef387..0f500243d15 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -424,13 +424,12 @@ struct IDLOptions { // This encapsulates where the parser is in the current source file. struct ParserState { - ParserState() : cursor_(nullptr), line_(1), token_(-1), is_bool_(false) {} + ParserState() : cursor_(nullptr), line_(1), token_(-1) {} protected: const char *cursor_; int line_; // the current line being parsed int token_; - bool is_bool_; std::string attribute_; std::vector doc_comment_; diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 4ee9bfe7521..12d721cfcdf 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -184,7 +184,8 @@ std::string Namespace::GetFullyQualifiedName(const std::string &name, TD(Attribute, 270, "attribute") \ TD(Null, 271, "null") \ TD(Service, 272, "rpc_service") \ - TD(NativeInclude, 273, "native_include") + TD(NativeInclude, 273, "native_include") \ + TD(BooleanConstant, 274, "boolean constant") #ifdef __GNUC__ __extension__ // Stop GCC complaining about trailing comma with -Wpendantic. #endif @@ -251,7 +252,6 @@ CheckedError Parser::Next() { doc_comment_.clear(); bool seen_newline = false; attribute_.clear(); - is_bool_ = false; for (;;) { char c = *cursor_++; token_ = c; @@ -391,8 +391,7 @@ CheckedError Parser::Next() { // which simplifies our logic downstream. if (attribute_ == "true" || attribute_ == "false") { attribute_ = NumToString(attribute_ == "true"); - token_ = kTokenIntegerConstant; - is_bool_ = true; + token_ = kTokenBooleanConstant; return NoError(); } // Check for declaration keywords: @@ -1301,6 +1300,11 @@ CheckedError Parser::ParseSingleValue(Value &e) { e, BASE_TYPE_INT, &match)); + ECHECK(TryTypedValue(kTokenBooleanConstant, + IsScalar(e.type.base_type), + e, + BASE_TYPE_BOOL, + &match)); ECHECK(TryTypedValue(kTokenFloatConstant, IsFloat(e.type.base_type), e, @@ -1970,6 +1974,9 @@ CheckedError Parser::SkipAnyJsonValue() { case kTokenFloatConstant: EXPECT(kTokenFloatConstant); break; + case kTokenBooleanConstant: + EXPECT(kTokenBooleanConstant); + break; default: return TokenError(); } @@ -2024,13 +2031,13 @@ CheckedError Parser::ParseFlexBufferValue(flexbuffers::Builder *builder) { EXPECT(kTokenStringConstant); break; case kTokenIntegerConstant: - if (is_bool_) { - builder->Bool(StringToInt(attribute_.c_str()) ? true : false); - } else { - builder->Int(StringToInt(attribute_.c_str())); - } + builder->Int(StringToInt(attribute_.c_str())); EXPECT(kTokenIntegerConstant); break; + case kTokenBooleanConstant: + builder->Bool(StringToInt(attribute_.c_str()) != 0); + EXPECT(kTokenBooleanConstant); + break; case kTokenFloatConstant: builder->Double(strtod(attribute_.c_str(), nullptr)); EXPECT(kTokenFloatConstant);