mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-30 22:02:41 +01:00
Replaced std::exit() with return Carbon::ErrorOr for expected errors like invalid syntax (#1120)
* Replaced std::exit() with return llvm::Expected/llvm::Error<T> for expected errors like invalid syntax. * Use llvm::formatv() for formatting lexer error messages. x * Addresed merge errors. * Fixed impl scope. * Made ErrorBuilder::operator<< nodiscard, to catch code forgetting 'return' in 'return FATAL_COMPILATION_ERROR()'. * FatalComplationError() -> ParseAndLexContext::RecordLexerError(). Other usages of ERROR_TOKEN in lexer.lpp were actually supposed to be END_OF_FILE. * Update executable_semantics/syntax/parse_and_lex_context.h Co-authored-by: Jon Meow <jperkins@google.com> * Code review fixes. * Update executable_semantics/syntax/parser.ypp Co-authored-by: Jon Meow <jperkins@google.com> * More code review fixes. * Update executable_semantics/interpreter/type_checker.h Co-authored-by: Jon Meow <jperkins@google.com> * Yet more code review fixes... * Update executable_semantics/syntax/lexer.lpp Co-authored-by: Jon Meow <jperkins@google.com> * code review comments * Update executable_semantics/interpreter/interpreter.cpp Co-authored-by: Geoff Romer <gromer@google.com> * Apply suggestions from code review Co-authored-by: Jon Meow <jperkins@google.com> * Update executable_semantics/syntax/lexer.lpp Co-authored-by: Jon Meow <jperkins@google.com> * code review * code review * Apply suggestions from code review Co-authored-by: Jon Meow <jperkins@google.com> * formatted code * review comments * Switched to the new ErrorOr<V> error implementation * code review comments * fixed comment * restored ostream.h as #976 makes the change unnecesary * review comments Co-authored-by: Jon Meow <jperkins@google.com> Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Jon Meow
Geoff Romer
parent
e5a87af6fe
commit
aa8a5f174d
@@ -10,6 +10,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "common/error.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/nonnull.h"
|
||||
#include "executable_semantics/interpreter/exec_program.h"
|
||||
@@ -21,18 +22,26 @@
|
||||
static void AddPrelude(
|
||||
std::string_view prelude_file_name, Carbon::Nonnull<Carbon::Arena*> arena,
|
||||
std::vector<Carbon::Nonnull<Carbon::Declaration*>>* declarations) {
|
||||
std::variant<Carbon::AST, Carbon::SyntaxErrorCode> parse_result =
|
||||
Carbon::ErrorOr<Carbon::AST> parse_result =
|
||||
Carbon::Parse(arena, prelude_file_name, false);
|
||||
if (std::holds_alternative<Carbon::SyntaxErrorCode>(parse_result)) {
|
||||
if (!parse_result.ok()) {
|
||||
// Try again with tracing, to help diagnose the problem.
|
||||
Carbon::Parse(arena, prelude_file_name, true);
|
||||
FATAL() << "Failed to parse prelude.";
|
||||
Carbon::ErrorOr<Carbon::AST> trace_parse_result =
|
||||
Carbon::Parse(arena, prelude_file_name, true);
|
||||
FATAL() << "Failed to parse prelude: "
|
||||
<< trace_parse_result.error().message();
|
||||
}
|
||||
const auto& prelude = std::get<Carbon::AST>(parse_result);
|
||||
const auto& prelude = *parse_result;
|
||||
declarations->insert(declarations->begin(), prelude.declarations.begin(),
|
||||
prelude.declarations.end());
|
||||
}
|
||||
|
||||
// Prints an error message and returns error code value.
|
||||
auto PrintError(const Carbon::Error& error) -> int {
|
||||
llvm::errs() << error.message() << "\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto main(int argc, char* argv[]) -> int {
|
||||
llvm::setBugReportMsg(
|
||||
"Please report issues to "
|
||||
@@ -56,18 +65,16 @@ auto main(int argc, char* argv[]) -> int {
|
||||
llvm::cl::ParseCommandLineOptions(argc, argv);
|
||||
|
||||
Carbon::Arena arena;
|
||||
std::variant<Carbon::AST, Carbon::SyntaxErrorCode> ast_or_error =
|
||||
Carbon::ErrorOr<Carbon::AST> ast =
|
||||
Carbon::Parse(&arena, input_file_name, trace_option);
|
||||
|
||||
if (auto* error = std::get_if<Carbon::SyntaxErrorCode>(&ast_or_error)) {
|
||||
// Diagnostic already reported to std::cerr; this is just a return code.
|
||||
return *error;
|
||||
if (!ast.ok()) {
|
||||
return PrintError(ast.error());
|
||||
}
|
||||
auto& ast = std::get<Carbon::AST>(ast_or_error);
|
||||
|
||||
AddPrelude(prelude_file_name, &arena, &ast.declarations);
|
||||
AddPrelude(prelude_file_name, &arena, &ast->declarations);
|
||||
|
||||
// Typecheck and run the parsed program.
|
||||
Carbon::ExecProgram(&arena, std::get<Carbon::AST>(ast_or_error),
|
||||
trace_option);
|
||||
Carbon::ErrorOr<int> result = Carbon::ExecProgram(&arena, *ast, trace_option);
|
||||
if (!result.ok()) {
|
||||
return PrintError(result.error());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user