parse.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "executable_semantics/syntax/parse.h"
  5. #include "common/check.h"
  6. #include "common/error.h"
  7. #include "executable_semantics/common/error.h"
  8. #include "executable_semantics/syntax/lexer.h"
  9. #include "executable_semantics/syntax/parse_and_lex_context.h"
  10. #include "executable_semantics/syntax/parser.h"
  11. #include "llvm/Support/Error.h"
  12. namespace Carbon {
  13. auto ParseImpl(yyscan_t scanner, Nonnull<Arena*> arena,
  14. std::string_view input_file_name, bool trace) -> ErrorOr<AST> {
  15. // Prepare other parser arguments.
  16. std::optional<AST> ast = std::nullopt;
  17. ParseAndLexContext context(arena->New<std::string>(input_file_name), trace);
  18. // Do the parse.
  19. auto parser = Parser(arena, scanner, context, &ast);
  20. if (trace) {
  21. parser.set_debug_level(1);
  22. }
  23. if (auto syntax_error_code = parser(); syntax_error_code != 0) {
  24. const std::string error_message = context.error_messages().empty()
  25. ? "Unknown parser error"
  26. : context.error_messages()[0];
  27. return Error(error_message);
  28. }
  29. // Return parse results.
  30. CHECK(ast != std::nullopt)
  31. << "parser validated syntax yet didn't produce an AST.";
  32. return *ast;
  33. }
  34. auto Parse(Nonnull<Arena*> arena, std::string_view input_file_name, bool trace)
  35. -> ErrorOr<AST> {
  36. FILE* input_file = fopen(std::string(input_file_name).c_str(), "r");
  37. if (input_file == nullptr) {
  38. return FATAL_PROGRAM_ERROR_NO_LINE() << "Error opening '" << input_file_name
  39. << "': " << std::strerror(errno);
  40. }
  41. // Prepare the lexer.
  42. yyscan_t scanner;
  43. yylex_init(&scanner);
  44. auto buffer = yy_create_buffer(input_file, YY_BUF_SIZE, scanner);
  45. yy_switch_to_buffer(buffer, scanner);
  46. ErrorOr<AST> result = ParseImpl(scanner, arena, input_file_name, trace);
  47. // Clean up the lexer.
  48. yy_delete_buffer(buffer, scanner);
  49. yylex_destroy(scanner);
  50. fclose(input_file);
  51. return result;
  52. }
  53. auto ParseFromString(Nonnull<Arena*> arena, std::string_view input_file_name,
  54. std::string_view file_contents, bool trace)
  55. -> ErrorOr<AST> {
  56. // Prepare the lexer.
  57. yyscan_t scanner;
  58. yylex_init(&scanner);
  59. auto buffer =
  60. yy_scan_bytes(file_contents.data(), file_contents.size(), scanner);
  61. yy_switch_to_buffer(buffer, scanner);
  62. ErrorOr<AST> result = ParseImpl(scanner, arena, input_file_name, trace);
  63. // Clean up the lexer.
  64. yy_delete_buffer(buffer, scanner);
  65. yylex_destroy(scanner);
  66. return result;
  67. }
  68. } // namespace Carbon