mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 19:01:07 +01:00
Support parsing Carbon files from strings. (#954)
Also, pass file names as string_view for greater convenience.
This commit is contained in:
@@ -12,6 +12,16 @@ cc_library(
|
||||
deps = ["//common:check"],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "parse_test",
|
||||
srcs = ["parse_test.cpp"],
|
||||
deps = [
|
||||
":syntax",
|
||||
"//executable_semantics/common:arena",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "syntax",
|
||||
srcs = [
|
||||
|
||||
@@ -12,19 +12,9 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto Parse(Nonnull<Arena*> arena, const std::string& input_file_name,
|
||||
bool trace) -> 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);
|
||||
|
||||
auto ParseImpl(yyscan_t scanner, Nonnull<Arena*> arena,
|
||||
std::string_view input_file_name, bool trace)
|
||||
-> std::variant<AST, SyntaxErrorCode> {
|
||||
// Prepare other parser arguments.
|
||||
std::optional<AST> ast = std::nullopt;
|
||||
ParseAndLexContext context(arena->New<std::string>(input_file_name), trace);
|
||||
@@ -36,10 +26,6 @@ auto Parse(Nonnull<Arena*> arena, const std::string& input_file_name,
|
||||
}
|
||||
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;
|
||||
@@ -51,4 +37,48 @@ auto Parse(Nonnull<Arena*> arena, const std::string& input_file_name,
|
||||
return *ast;
|
||||
}
|
||||
|
||||
auto Parse(Nonnull<Arena*> arena, std::string_view input_file_name, bool trace)
|
||||
-> std::variant<AST, SyntaxErrorCode> {
|
||||
FILE* input_file = fopen(std::string(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);
|
||||
auto buffer = yy_create_buffer(input_file, YY_BUF_SIZE, scanner);
|
||||
yy_switch_to_buffer(buffer, scanner);
|
||||
|
||||
std::variant<AST, SyntaxErrorCode> result =
|
||||
ParseImpl(scanner, arena, input_file_name, trace);
|
||||
|
||||
// Clean up the lexer.
|
||||
yy_delete_buffer(buffer, scanner);
|
||||
yylex_destroy(scanner);
|
||||
fclose(input_file);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
auto ParseFromString(Nonnull<Arena*> arena, std::string_view input_file_name,
|
||||
std::string_view file_contents, bool trace)
|
||||
-> std::variant<Carbon::AST, SyntaxErrorCode> {
|
||||
// Prepare the lexer.
|
||||
yyscan_t scanner;
|
||||
yylex_init(&scanner);
|
||||
auto buffer =
|
||||
yy_scan_bytes(file_contents.data(), file_contents.size(), scanner);
|
||||
yy_switch_to_buffer(buffer, scanner);
|
||||
|
||||
std::variant<AST, SyntaxErrorCode> result =
|
||||
ParseImpl(scanner, arena, input_file_name, trace);
|
||||
|
||||
// Clean up the lexer.
|
||||
yy_delete_buffer(buffer, scanner);
|
||||
yylex_destroy(scanner);
|
||||
|
||||
return result;
|
||||
}
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -18,8 +18,15 @@ using SyntaxErrorCode = int;
|
||||
|
||||
// Returns the AST representing the contents of the named file, or an error code
|
||||
// if parsing fails. Allocations go into the provided arena.
|
||||
auto Parse(Nonnull<Arena*> arena, const std::string& input_file_name,
|
||||
bool trace) -> std::variant<Carbon::AST, SyntaxErrorCode>;
|
||||
auto Parse(Nonnull<Arena*> arena, std::string_view input_file_name, bool trace)
|
||||
-> std::variant<Carbon::AST, SyntaxErrorCode>;
|
||||
|
||||
// Equivalent to `Parse`, but parses the contents of `file_contents`.
|
||||
// `input_file_name` is used only for reporting source locations, and does
|
||||
// not need to name a real file.
|
||||
auto ParseFromString(Nonnull<Arena*> arena, std::string_view input_file_name,
|
||||
std::string_view file_contents, bool trace)
|
||||
-> std::variant<Carbon::AST, SyntaxErrorCode>;
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// 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 <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "executable_semantics/common/arena.h"
|
||||
|
||||
namespace Carbon {
|
||||
namespace {
|
||||
|
||||
static constexpr std::string_view FileContents = R"(
|
||||
package ExecutableSemanticsTest api;
|
||||
|
||||
fn Foo() {}
|
||||
)";
|
||||
|
||||
TEST(ParseTest, ParseFromString) {
|
||||
Arena arena;
|
||||
std::variant<AST, SyntaxErrorCode> parse_result =
|
||||
ParseFromString(&arena, "file.carbon", FileContents, /*trace=*/false);
|
||||
ASSERT_TRUE(std::holds_alternative<AST>(parse_result));
|
||||
EXPECT_EQ(std::get<AST>(parse_result).declarations.size(), 1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace Carbon
|
||||
Reference in New Issue
Block a user