Error: track message and prefix/location separately. (#1529)

This allows us to combine multiple Errors together without repeating the prefix
information. Also fixes several cases where two "COMPILATION ERROR" prefixes
would be prepended to the same message when errors with prefixes and locations
were produced by the lexer and parser.
This commit is contained in:
Richard Smith
2022-07-26 15:08:19 -07:00
committed by GitHub
parent 856a5c4536
commit 7a67715ac5
16 changed files with 177 additions and 52 deletions
+9 -12
View File
@@ -4,23 +4,20 @@
#include "explorer/syntax/parse_and_lex_context.h"
#include "explorer/common/error_builders.h"
namespace Carbon {
auto ParseAndLexContext::RecordSyntaxError(const std::string& message,
bool prefix_with_newline)
-> Parser::symbol_type {
// Optionally adds a newline in trace mode because trace prints an incomplete
// line "Reading a token: " which can prevent LIT from finding expected
// patterns.
// TODO: support formatting of `SourceLocation` instances with formatv().
std::string full_message;
llvm::raw_string_ostream(full_message)
<< (prefix_with_newline && parser_debug() ? "\n" : "")
<< "COMPILATION ERROR: " << source_loc() << ": " << message;
error_messages_.push_back(full_message);
auto ParseAndLexContext::RecordSyntaxError(Error error) -> Parser::symbol_type {
errors_.push_back(std::move(error));
// TODO: use `YYerror` token once bison is upgraded to at least 3.5.
return Parser::make_END_OF_FILE(current_token_position);
}
auto ParseAndLexContext::RecordSyntaxError(const std::string& message)
-> Parser::symbol_type {
return RecordSyntaxError(CompilationError(source_loc()) << message);
}
} // namespace Carbon