mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 16:20:11 +01:00
Note that ptr.h also includes an enable_if change, to help avoid https://bugs.llvm.org/show_bug.cgi?id=51881
56 lines
1.6 KiB
C++
56 lines
1.6 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "executable_semantics/syntax/parse.h"
|
|
|
|
#include "common/check.h"
|
|
#include "executable_semantics/common/error.h"
|
|
#include "executable_semantics/common/tracing_flag.h"
|
|
#include "executable_semantics/syntax/lexer.h"
|
|
#include "executable_semantics/syntax/parse_and_lex_context.h"
|
|
#include "executable_semantics/syntax/parser.h"
|
|
|
|
namespace Carbon {
|
|
|
|
auto Parse(Nonnull<Arena*> arena, const std::string& input_file_name)
|
|
-> std::variant<AST, SyntaxErrorCode> {
|
|
FILE* input_file = fopen(input_file_name.c_str(), "r");
|
|
if (input_file == nullptr) {
|
|
FATAL_PROGRAM_ERROR_NO_LINE() << "Error opening '" << input_file_name
|
|
<< "': " << std::strerror(errno);
|
|
}
|
|
|
|
// Prepare the lexer.
|
|
yyscan_t scanner;
|
|
yylex_init(&scanner);
|
|
yyset_in(input_file, scanner);
|
|
|
|
// Prepare other parser arguments.
|
|
std::optional<AST> ast = std::nullopt;
|
|
ParseAndLexContext context(arena->New<std::string>(input_file_name));
|
|
|
|
// Do the parse.
|
|
auto parser = Parser(arena, scanner, context, &ast);
|
|
if (tracing_output) {
|
|
parser.set_debug_level(1);
|
|
}
|
|
auto syntax_error_code = parser();
|
|
|
|
// Clean up the lexer.
|
|
fclose(input_file);
|
|
yylex_destroy(scanner);
|
|
|
|
// Return an error if appropriate.
|
|
if (syntax_error_code != 0) {
|
|
return syntax_error_code;
|
|
}
|
|
|
|
// Return parse results.
|
|
CHECK(ast != std::nullopt)
|
|
<< "parser validated syntax yet didn't produce an AST.";
|
|
return *ast;
|
|
}
|
|
|
|
} // namespace Carbon
|