parse_and_lex_context.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 EXECUTABLE_SEMANTICS_SYNTAX_DRIVER_H_
  5. #define EXECUTABLE_SEMANTICS_SYNTAX_DRIVER_H_
  6. #include <variant>
  7. #include "executable_semantics/ast/ast.h"
  8. #include "executable_semantics/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, bool trace)
  16. : input_file_name_(input_file_name), trace_(trace) {}
  17. // Formats ands records a lexer error. Returns an error token as a
  18. // convenience.
  19. auto RecordSyntaxError(const std::string& message,
  20. bool prefix_with_newline = false)
  21. -> 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 trace() const -> bool { return trace_; }
  27. // The source range of the token being (or just) lex'd.
  28. location current_token_position;
  29. auto error_messages() const -> const std::vector<std::string> {
  30. return error_messages_;
  31. }
  32. private:
  33. // A path to the file processed, relative to the current working directory
  34. // when *this is called.
  35. Nonnull<const std::string*> input_file_name_;
  36. bool trace_;
  37. std::vector<std::string> error_messages_;
  38. };
  39. } // namespace Carbon
  40. // Gives flex the yylex prototype we want.
  41. #define YY_DECL \
  42. auto yylex(Carbon::Nonnull<Carbon::Arena*> arena, yyscan_t yyscanner, \
  43. Carbon::ParseAndLexContext& context) \
  44. ->Carbon::Parser::symbol_type
  45. // Declares yylex for the parser's sake.
  46. YY_DECL;
  47. #endif // EXECUTABLE_SYNTAX_DRIVER_H_