parse.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_builders.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. std::string name_str(input_file_name);
  37. FILE* input_file = fopen(name_str.c_str(), "r");
  38. if (input_file == nullptr) {
  39. return ProgramError(SourceLocation(name_str.c_str(), 0))
  40. << "Error opening file: " << std::strerror(errno);
  41. }
  42. // Prepare the lexer.
  43. yyscan_t scanner;
  44. yylex_init(&scanner);
  45. auto buffer = yy_create_buffer(input_file, YY_BUF_SIZE, scanner);
  46. yy_switch_to_buffer(buffer, scanner);
  47. ErrorOr<AST> result = ParseImpl(scanner, arena, input_file_name, trace);
  48. // Clean up the lexer.
  49. yy_delete_buffer(buffer, scanner);
  50. yylex_destroy(scanner);
  51. fclose(input_file);
  52. return result;
  53. }
  54. auto ParseFromString(Nonnull<Arena*> arena, std::string_view input_file_name,
  55. std::string_view file_contents, bool trace)
  56. -> ErrorOr<AST> {
  57. // Prepare the lexer.
  58. yyscan_t scanner;
  59. yylex_init(&scanner);
  60. auto buffer =
  61. yy_scan_bytes(file_contents.data(), file_contents.size(), scanner);
  62. yy_switch_to_buffer(buffer, scanner);
  63. ErrorOr<AST> result = ParseImpl(scanner, arena, input_file_name, trace);
  64. // Clean up the lexer.
  65. yy_delete_buffer(buffer, scanner);
  66. yylex_destroy(scanner);
  67. return result;
  68. }
  69. } // namespace Carbon