parse.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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. const std::string error_message = context.error_messages().empty()
  27. ? "Unknown parser error"
  28. : context.error_messages()[0];
  29. return Error(error_message);
  30. }
  31. // Return parse results.
  32. CHECK(ast != std::nullopt)
  33. << "parser validated syntax yet didn't produce an AST.";
  34. return *ast;
  35. }
  36. auto Parse(Nonnull<Arena*> arena, std::string_view input_file_name,
  37. bool parser_debug) -> ErrorOr<AST> {
  38. std::string name_str(input_file_name);
  39. FILE* input_file = fopen(name_str.c_str(), "r");
  40. if (input_file == nullptr) {
  41. return ProgramError(SourceLocation(name_str.c_str(), 0))
  42. << "Error opening file: " << std::strerror(errno);
  43. }
  44. // Prepare the lexer.
  45. yyscan_t scanner;
  46. yylex_init(&scanner);
  47. auto buffer = yy_create_buffer(input_file, YY_BUF_SIZE, scanner);
  48. yy_switch_to_buffer(buffer, scanner);
  49. ErrorOr<AST> result =
  50. ParseImpl(scanner, arena, input_file_name, parser_debug);
  51. // Clean up the lexer.
  52. yy_delete_buffer(buffer, scanner);
  53. yylex_destroy(scanner);
  54. fclose(input_file);
  55. return result;
  56. }
  57. auto ParseFromString(Nonnull<Arena*> arena, std::string_view input_file_name,
  58. std::string_view file_contents, bool parser_debug)
  59. -> ErrorOr<AST> {
  60. // Prepare the lexer.
  61. yyscan_t scanner;
  62. yylex_init(&scanner);
  63. auto buffer =
  64. yy_scan_bytes(file_contents.data(), file_contents.size(), scanner);
  65. yy_switch_to_buffer(buffer, scanner);
  66. ErrorOr<AST> result =
  67. ParseImpl(scanner, arena, input_file_name, parser_debug);
  68. // Clean up the lexer.
  69. yy_delete_buffer(buffer, scanner);
  70. yylex_destroy(scanner);
  71. return result;
  72. }
  73. } // namespace Carbon