lex_scan_helper.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_LEX_SCAN_HELPER_H_
  5. #define CARBON_EXPLORER_SYNTAX_LEX_SCAN_HELPER_H_
  6. #include <string>
  7. #include "explorer/syntax/parse_and_lex_context.h"
  8. #include "explorer/syntax/parser.h"
  9. // Exposes yyinput; defined in lexer.lpp.
  10. extern auto YyinputWrapper(yyscan_t yyscanner) -> int;
  11. namespace Carbon {
  12. class StringLexHelper {
  13. public:
  14. StringLexHelper(const char* text, yyscan_t yyscanner,
  15. Carbon::ParseAndLexContext& context)
  16. : str_(text), yyscanner_(yyscanner), context_(context), is_eof_(false) {}
  17. // Advances yyscanner by one char. Sets is_eof to true and returns false on
  18. // EOF.
  19. auto Advance() -> bool;
  20. // Returns the last scanned char.
  21. auto last_char() -> char { return str_.back(); }
  22. // Returns the scanned string.
  23. auto str() -> const std::string& { return str_; }
  24. auto is_eof() -> bool { return is_eof_; }
  25. private:
  26. std::string str_;
  27. yyscan_t yyscanner_;
  28. Carbon::ParseAndLexContext& context_;
  29. // Skips reading next char.
  30. bool is_eof_;
  31. };
  32. // Tries to Read `hashtag_num` hashtags. Returns true on success.
  33. // Reads `hashtag_num` characters on success, and number of consecutive hashtags
  34. // (< `hashtag_num`) + 1 characters on failure.
  35. auto ReadHashTags(Carbon::StringLexHelper& scan_helper, size_t hashtag_num)
  36. -> bool;
  37. // Removes quotes and escapes a single line string. Reports an error on
  38. // invalid escaping.
  39. auto ProcessSingleLineString(llvm::StringRef str,
  40. Carbon::ParseAndLexContext& context,
  41. size_t hashtag_num) -> Carbon::Parser::symbol_type;
  42. auto ProcessMultiLineString(llvm::StringRef str,
  43. Carbon::ParseAndLexContext& context,
  44. size_t hashtag_num) -> Carbon::Parser::symbol_type;
  45. } // namespace Carbon
  46. #endif // CARBON_EXPLORER_SYNTAX_LEX_SCAN_HELPER_H_