parse.cpp 2.6 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 "executable_semantics/common/error.h"
  7. #include "executable_semantics/syntax/lexer.h"
  8. #include "executable_semantics/syntax/parse_and_lex_context.h"
  9. #include "executable_semantics/syntax/parser.h"
  10. namespace Carbon {
  11. auto ParseImpl(yyscan_t scanner, Nonnull<Arena*> arena,
  12. std::string_view input_file_name, bool trace)
  13. -> std::variant<AST, SyntaxErrorCode> {
  14. // Prepare other parser arguments.
  15. std::optional<AST> ast = std::nullopt;
  16. ParseAndLexContext context(arena->New<std::string>(input_file_name), trace);
  17. // Do the parse.
  18. auto parser = Parser(arena, scanner, context, &ast);
  19. if (trace) {
  20. parser.set_debug_level(1);
  21. }
  22. auto syntax_error_code = parser();
  23. // Return an error if appropriate.
  24. if (syntax_error_code != 0) {
  25. return syntax_error_code;
  26. }
  27. // Return parse results.
  28. CHECK(ast != std::nullopt)
  29. << "parser validated syntax yet didn't produce an AST.";
  30. return *ast;
  31. }
  32. auto Parse(Nonnull<Arena*> arena, std::string_view input_file_name, bool trace)
  33. -> std::variant<AST, SyntaxErrorCode> {
  34. FILE* input_file = fopen(std::string(input_file_name).c_str(), "r");
  35. if (input_file == nullptr) {
  36. FATAL_PROGRAM_ERROR_NO_LINE() << "Error opening '" << input_file_name
  37. << "': " << std::strerror(errno);
  38. }
  39. // Prepare the lexer.
  40. yyscan_t scanner;
  41. yylex_init(&scanner);
  42. auto buffer = yy_create_buffer(input_file, YY_BUF_SIZE, scanner);
  43. yy_switch_to_buffer(buffer, scanner);
  44. std::variant<AST, SyntaxErrorCode> result =
  45. ParseImpl(scanner, arena, input_file_name, trace);
  46. // Clean up the lexer.
  47. yy_delete_buffer(buffer, scanner);
  48. yylex_destroy(scanner);
  49. fclose(input_file);
  50. return result;
  51. }
  52. auto ParseFromString(Nonnull<Arena*> arena, std::string_view input_file_name,
  53. std::string_view file_contents, bool trace)
  54. -> std::variant<Carbon::AST, SyntaxErrorCode> {
  55. // Prepare the lexer.
  56. yyscan_t scanner;
  57. yylex_init(&scanner);
  58. auto buffer =
  59. yy_scan_bytes(file_contents.data(), file_contents.size(), scanner);
  60. yy_switch_to_buffer(buffer, scanner);
  61. std::variant<AST, SyntaxErrorCode> result =
  62. 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