parse_and_lex_context.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef CARBON_EXPLORER_SYNTAX_PARSE_AND_LEX_CONTEXT_H_
  5. #define CARBON_EXPLORER_SYNTAX_PARSE_AND_LEX_CONTEXT_H_
  6. #include <variant>
  7. #include "explorer/ast/ast.h"
  8. #include "explorer/syntax/parser.h" // from parser.ypp
  9. namespace Carbon {
  10. // The state and functionality that is threaded "globally" through the
  11. // lexing/parsing process.
  12. class ParseAndLexContext {
  13. public:
  14. // Creates an instance analyzing the given input file.
  15. ParseAndLexContext(Nonnull<const std::string*> input_file_name,
  16. bool parser_debug)
  17. : input_file_name_(input_file_name), parser_debug_(parser_debug) {}
  18. // Formats ands records a lexing oor parsing error. Returns an error token as
  19. // a convenience.
  20. auto RecordSyntaxError(Error error) -> Parser::symbol_type;
  21. auto RecordSyntaxError(const std::string& message) -> Parser::symbol_type;
  22. auto source_loc() const -> SourceLocation {
  23. return SourceLocation(input_file_name_,
  24. static_cast<int>(current_token_position.begin.line));
  25. }
  26. auto parser_debug() const -> bool { return parser_debug_; }
  27. // The source range of the token being (or just) lex'd.
  28. location current_token_position;
  29. auto take_errors() -> std::vector<Error> {
  30. std::vector<Error> errors = std::move(errors_);
  31. errors_.clear();
  32. return errors;
  33. }
  34. private:
  35. // A path to the file processed, relative to the current working directory
  36. // when *this is called.
  37. Nonnull<const std::string*> input_file_name_;
  38. bool parser_debug_;
  39. std::vector<Error> errors_;
  40. };
  41. } // namespace Carbon
  42. // Gives flex the yylex prototype we want.
  43. #define YY_DECL \
  44. auto yylex(Carbon::Nonnull<Carbon::Arena*> /*arena*/, yyscan_t yyscanner, \
  45. Carbon::ParseAndLexContext& context) \
  46. ->Carbon::Parser::symbol_type
  47. // Declares yylex for the parser's sake.
  48. YY_DECL;
  49. #endif // CARBON_EXPLORER_SYNTAX_PARSE_AND_LEX_CONTEXT_H_