Skip to content

Commit

Permalink
Add error context in parser error messages (onnx#3752)
Browse files Browse the repository at this point in the history
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Mark <mhamilton723@gmail.com>
  • Loading branch information
gramalingam authored and mhamilton723 committed Oct 22, 2021
1 parent af993d9 commit 2a9487c
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions onnx/defs/parser.h
Expand Up @@ -184,9 +184,29 @@ class ParserBase {
return ONNX_NAMESPACE::MakeString("(line: ", line, " column: ", col, ")");
}

// Return a suitable suffix of what has been parsed to provide error message context:
// return the line containing the last non-space character preceding the error (if it exists).
std::string GetErrorContext() {
// Special cases: empty input string, and parse-error at first character.
const char* p = next_ < end_ ? next_ : next_ - 1;
while ((p > start_) && isspace(*p))
--p;
while ((p > start_) && (*p != '\n'))
--p;
// Start at character after '\n' unless we are at start of input
const char* context_start = (p > start_) ? (p + 1) : start_;
for (p = context_start; (p < end_) && (*p != '\n'); ++p)
;
return std::string(context_start, p-context_start);
}

template <typename... Args>
Status ParseError(const Args&... args) {
return Status(NONE, FAIL, ONNX_NAMESPACE::MakeString("[ParseError at position ", GetCurrentPos(), "]", args...));
return Status(
NONE,
FAIL,
ONNX_NAMESPACE::MakeString(
"[ParseError at position ", GetCurrentPos(), "]\n", "Error context: ", GetErrorContext(), "\n", args...));
}

void SkipWhiteSpace() {
Expand Down Expand Up @@ -219,7 +239,7 @@ class ParserBase {

Status Match(char ch, bool skipspace = true) {
if (!Matches(ch, skipspace))
return ParseError("Expected character ", ch, " not found", ch);
return ParseError("Expected character ", ch, " not found.");
return Status::OK();
}

Expand Down

0 comments on commit 2a9487c

Please sign in to comment.