Skip to content

Commit b84a39c

Browse files
committed
Add public semantic error reporting
Closes open-source-parsers#57
1 parent 7bd75b0 commit b84a39c

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

include/json/reader.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ class JSON_API Reader {
130130
*/
131131
std::vector<StructuredError> getStructuredErrors() const;
132132

133+
/** \brief Add a semantic error message.
134+
* \param value JSON Value location associated with the error
135+
* \param message The error message.
136+
* \return \c true if the error was successfully added, \c false if the
137+
* Value offset exceeds the document size.
138+
*/
139+
bool pushError(const Value& value, const std::string& message);
140+
141+
/** \brief Add a semantic error message with extra context.
142+
* \param value JSON Value location associated with the error
143+
* \param message The error message.
144+
* \param extra Additional JSON Value location to contextualize the error
145+
* \return \c true if the error was successfully added, \c false if either
146+
* Value offset exceeds the document size.
147+
*/
148+
bool pushError(const Value& value, const std::string& message, const Value& extra);
149+
150+
/** \brief Return whether there are any errors.
151+
* \return \c true if there are no errors to report \c false if
152+
* errors have occurred.
153+
*/
154+
bool good() const;
155+
133156
private:
134157
enum TokenType {
135158
tokenEndOfStream = 0,

src/lib_json/json_reader.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,43 @@ std::vector<Reader::StructuredError> Reader::getStructuredErrors() const {
830830
return allErrors;
831831
}
832832

833+
bool Reader::pushError(const Value& value, const std::string& message) {
834+
if(value.getOffsetStart() > end_ - begin_
835+
|| value.getOffsetLimit() > end_ - begin_)
836+
return false;
837+
Token token;
838+
token.type_ = tokenError;
839+
token.start_ = begin_ + value.getOffsetStart();
840+
token.end_ = end_ + value.getOffsetLimit();
841+
ErrorInfo info;
842+
info.token_ = token;
843+
info.message_ = message;
844+
info.extra_ = 0;
845+
errors_.push_back(info);
846+
return true;
847+
}
848+
849+
bool Reader::pushError(const Value& value, const std::string& message, const Value& extra) {
850+
if(value.getOffsetStart() > end_ - begin_
851+
|| value.getOffsetLimit() > end_ - begin_
852+
|| extra.getOffsetLimit() > end_ - begin_)
853+
return false;
854+
Token token;
855+
token.type_ = tokenError;
856+
token.start_ = begin_ + value.getOffsetStart();
857+
token.end_ = begin_ + value.getOffsetLimit();
858+
ErrorInfo info;
859+
info.token_ = token;
860+
info.message_ = message;
861+
info.extra_ = begin_ + extra.getOffsetStart();
862+
errors_.push_back(info);
863+
return true;
864+
}
865+
866+
bool Reader::good() const {
867+
return !errors_.size();
868+
}
869+
833870
std::istream& operator>>(std::istream& sin, Value& root) {
834871
Json::Reader reader;
835872
bool ok = reader.parse(sin, root, true);

0 commit comments

Comments
 (0)