diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 404482812..19260e77b 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -760,7 +760,8 @@ struct IDLOptions { // This encapsulates where the parser is in the current source file. struct ParserState { ParserState() - : cursor_(nullptr), + : prev_cursor_(nullptr), + cursor_(nullptr), line_start_(nullptr), line_(0), token_(-1), @@ -768,6 +769,7 @@ struct ParserState { protected: void ResetState(const char *source) { + prev_cursor_ = source; cursor_ = source; line_ = 0; MarkNewLine(); @@ -782,7 +784,8 @@ struct ParserState { FLATBUFFERS_ASSERT(cursor_ && line_start_ && cursor_ >= line_start_); return static_cast(cursor_ - line_start_); } - + + const char *prev_cursor_; const char *cursor_; const char *line_start_; int line_; // the current line being parsed diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index bd9780f37..b16c3bec8 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -483,6 +483,7 @@ CheckedError Parser::SkipByteOrderMark() { CheckedError Parser::Next() { doc_comment_.clear(); + prev_cursor_ = cursor_; bool seen_newline = cursor_ == source_; attribute_.clear(); attr_is_trivial_ascii_string_ = true; @@ -3312,7 +3313,7 @@ bool Parser::ParseJson(const char *json, const char *json_filename) { } std::ptrdiff_t Parser::BytesConsumed() const { - return std::distance(source_, cursor_); + return std::distance(source_, prev_cursor_); } CheckedError Parser::StartParseFile(const char *source, diff --git a/tests/test.cpp b/tests/test.cpp index db2916276..15d0d4fc8 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1434,14 +1434,14 @@ void DoNotRequireEofTest(const std::string &tests_data_path) { ok = parser.Parse(schemafile.c_str(), include_directories); TEST_EQ(ok, true); - const char *str = R"(This string contains two monsters, the first one is { + const char *str = R"(Some text at the beginning. { "name": "Blob", "hp": 5 - } - and the second one is { + }{ "name": "Imp", "hp": 10 } + Some extra text at the end too. )"; const char *tableStart = std::strchr(str, '{'); ok = parser.ParseJson(tableStart); @@ -1453,7 +1453,6 @@ void DoNotRequireEofTest(const std::string &tests_data_path) { tableStart += parser.BytesConsumed(); - tableStart = std::strchr(tableStart + 1, '{'); ok = parser.ParseJson(tableStart); TEST_EQ(ok, true);