Skip to content

Commit

Permalink
Add proper support for Booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzier committed Jul 28, 2017
1 parent 8caa55b commit 8557909
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
3 changes: 1 addition & 2 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> doc_comment_;
Expand Down
25 changes: 16 additions & 9 deletions src/idl_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1970,6 +1974,9 @@ CheckedError Parser::SkipAnyJsonValue() {
case kTokenFloatConstant:
EXPECT(kTokenFloatConstant);
break;
case kTokenBooleanConstant:
EXPECT(kTokenBooleanConstant);
break;
default:
return TokenError();
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8557909

Please sign in to comment.