parse.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "explorer/syntax/parse.h"
  5. #include "common/check.h"
  6. #include "common/error.h"
  7. #include "explorer/common/error_builders.h"
  8. #include "explorer/syntax/lexer.h"
  9. #include "explorer/syntax/parse_and_lex_context.h"
  10. #include "explorer/syntax/parser.h"
  11. #include "llvm/Support/Error.h"
  12. namespace Carbon {
  13. static auto ParseImpl(yyscan_t scanner, Nonnull<Arena*> arena,
  14. std::string_view input_file_name, bool parser_debug)
  15. -> ErrorOr<AST> {
  16. // Prepare other parser arguments.
  17. std::optional<AST> ast = std::nullopt;
  18. ParseAndLexContext context(arena->New<std::string>(input_file_name),
  19. parser_debug);
  20. // Do the parse.
  21. auto parser = Parser(arena, scanner, context, &ast);
  22. if (parser_debug) {
  23. parser.set_debug_level(1);
  24. }
  25. if (auto syntax_error_code = parser(); syntax_error_code != 0) {
  26. auto errors = context.take_errors();
  27. if (errors.empty()) {
  28. return Error("Unknown parser erroor");
  29. }
  30. return std::move(errors.front());
  31. }
  32. // Return parse results.
  33. CARBON_CHECK(ast != std::nullopt)
  34. << "parser validated syntax yet didn't produce an AST.";
  35. return *ast;
  36. }
  37. auto Parse(Nonnull<Arena*> arena, std::string_view input_file_name,
  38. bool parser_debug) -> ErrorOr<AST> {
  39. std::string name_str(input_file_name);
  40. FILE* input_file = fopen(name_str.c_str(), "r");
  41. if (input_file == nullptr) {
  42. return ProgramError(SourceLocation(name_str.c_str(), 0))
  43. << "Error opening file: " << std::strerror(errno);
  44. }
  45. // Prepare the lexer.
  46. yyscan_t scanner;
  47. yylex_init(&scanner);
  48. auto buffer = yy_create_buffer(input_file, YY_BUF_SIZE, scanner);
  49. yy_switch_to_buffer(buffer, scanner);
  50. ErrorOr<AST> result =
  51. ParseImpl(scanner, arena, input_file_name, parser_debug);
  52. // Clean up the lexer.
  53. yy_delete_buffer(buffer, scanner);
  54. yylex_destroy(scanner);
  55. fclose(input_file);
  56. return result;
  57. }
  58. auto ParseFromString(Nonnull<Arena*> arena, std::string_view input_file_name,
  59. std::string_view file_contents, bool parser_debug)
  60. -> ErrorOr<AST> {
  61. // Prepare the lexer.
  62. yyscan_t scanner;
  63. yylex_init(&scanner);
  64. auto buffer =
  65. yy_scan_bytes(file_contents.data(), file_contents.size(), scanner);
  66. yy_switch_to_buffer(buffer, scanner);
  67. ErrorOr<AST> result =
  68. ParseImpl(scanner, arena, input_file_name, parser_debug);
  69. // Clean up the lexer.
  70. yy_delete_buffer(buffer, scanner);
  71. yylex_destroy(scanner);
  72. return result;
  73. }
  74. } // namespace Carbon