parse_and_lex_context.h 1.6 KB

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