string_literal.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include <string>
  5. #include "llvm/ADT/Optional.h"
  6. #include "llvm/ADT/StringRef.h"
  7. #include "toolchain/diagnostics/diagnostic_emitter.h"
  8. namespace Carbon {
  9. class LexedStringLiteral {
  10. public:
  11. // Get the text corresponding to this literal.
  12. [[nodiscard]] auto Text() const -> llvm::StringRef { return text; }
  13. // Determine whether this is a multi-line string literal.
  14. [[nodiscard]] auto IsMultiLine() const -> bool { return multi_line; }
  15. // Extract a string literal token from the given text, if it has a suitable
  16. // form.
  17. static auto Lex(llvm::StringRef source_text)
  18. -> llvm::Optional<LexedStringLiteral>;
  19. // Expand any escape sequences in the given string literal and compute the
  20. // resulting value. This handles error recovery internally and cannot fail.
  21. auto ComputeValue(DiagnosticEmitter<const char*>& emitter) const
  22. -> std::string;
  23. private:
  24. LexedStringLiteral(llvm::StringRef text, llvm::StringRef content,
  25. int hash_level, bool multi_line)
  26. : text(text),
  27. content(content),
  28. hash_level(hash_level),
  29. multi_line(multi_line) {}
  30. // The complete text of the string literal.
  31. llvm::StringRef text;
  32. // The content of the literal. For a multi-line literal, this begins
  33. // immediately after the newline following the file type indicator, and ends
  34. // at the start of the closing `"""`. Leading whitespace is not removed from
  35. // either end.
  36. llvm::StringRef content;
  37. // The number of `#`s preceding the opening `"` or `"""`.
  38. int hash_level;
  39. // Whether this was a multi-line string literal.
  40. bool multi_line;
  41. };
  42. } // namespace Carbon