parse_and_lex_context.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. // Writes a syntax error diagnostic containing message to standard error.
  18. auto PrintDiagnostic(const std::string& message) -> void;
  19. auto source_loc() -> SourceLocation {
  20. return SourceLocation(input_file_name_,
  21. static_cast<int>(current_token_position.begin.line));
  22. }
  23. auto trace() -> bool { return trace_; }
  24. // The source range of the token being (or just) lex'd.
  25. location current_token_position;
  26. private:
  27. // A path to the file processed, relative to the current working directory
  28. // when *this is called.
  29. Nonnull<const std::string*> input_file_name_;
  30. bool trace_;
  31. };
  32. } // namespace Carbon
  33. // Gives flex the yylex prototype we want.
  34. #define YY_DECL \
  35. auto yylex(Carbon::Nonnull<Carbon::Arena*> arena, yyscan_t yyscanner, \
  36. Carbon::ParseAndLexContext& context) \
  37. ->Carbon::Parser::symbol_type
  38. // Declares yylex for the parser's sake.
  39. YY_DECL;
  40. #endif // EXECUTABLE_SYNTAX_DRIVER_H_