parse_and_lex_context.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 EXPLORER_SYNTAX_PARSE_AND_LEX_CONTEXT_H_
  5. #define 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 lexer error. Returns an error token as a
  19. // convenience.
  20. auto RecordSyntaxError(const std::string& message,
  21. bool prefix_with_newline = false)
  22. -> Parser::symbol_type;
  23. auto source_loc() const -> SourceLocation {
  24. return SourceLocation(input_file_name_,
  25. static_cast<int>(current_token_position.begin.line));
  26. }
  27. auto parser_debug() const -> bool { return parser_debug_; }
  28. // The source range of the token being (or just) lex'd.
  29. location current_token_position;
  30. auto error_messages() const -> const std::vector<std::string> {
  31. return error_messages_;
  32. }
  33. private:
  34. // A path to the file processed, relative to the current working directory
  35. // when *this is called.
  36. Nonnull<const std::string*> input_file_name_;
  37. bool parser_debug_;
  38. std::vector<std::string> error_messages_;
  39. };
  40. } // namespace Carbon
  41. // Gives flex the yylex prototype we want.
  42. #define YY_DECL \
  43. auto yylex(Carbon::Nonnull<Carbon::Arena*> arena, yyscan_t yyscanner, \
  44. Carbon::ParseAndLexContext& context) \
  45. ->Carbon::Parser::symbol_type
  46. // Declares yylex for the parser's sake.
  47. YY_DECL;
  48. #endif // EXPLORER_SYNTAX_PARSE_AND_LEX_CONTEXT_H_