string_literal.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // Extract a string literal token from the given text, if it has a suitable
  12. // form. Returning llvm::None indicates no string literal was found; returning
  13. // an invalid literal indicates a string prefix was found, but it's malformed
  14. // and is returning a partial string literal to assist error construction.
  15. static auto Lex(llvm::StringRef source_text)
  16. -> llvm::Optional<LexedStringLiteral>;
  17. // Expand any escape sequences in the given string literal and compute the
  18. // resulting value. This handles error recovery internally and cannot fail.
  19. auto ComputeValue(DiagnosticEmitter<const char*>& emitter) const
  20. -> std::string;
  21. // Get the text corresponding to this literal.
  22. [[nodiscard]] auto text() const -> llvm::StringRef { return text_; }
  23. // Determine whether this is a multi-line string literal.
  24. [[nodiscard]] auto is_multi_line() const -> bool { return multi_line_; }
  25. // Returns true if the string has a valid terminator.
  26. [[nodiscard]] auto is_terminated() const -> bool { return is_terminated_; }
  27. private:
  28. LexedStringLiteral(llvm::StringRef text, llvm::StringRef content,
  29. int hash_level, bool multi_line, bool is_terminated)
  30. : text_(text),
  31. content_(content),
  32. hash_level_(hash_level),
  33. multi_line_(multi_line),
  34. is_terminated_(is_terminated) {}
  35. // The complete text of the string literal.
  36. llvm::StringRef text_;
  37. // The content of the literal. For a multi-line literal, this begins
  38. // immediately after the newline following the file type indicator, and ends
  39. // at the start of the closing `"""`. Leading whitespace is not removed from
  40. // either end.
  41. llvm::StringRef content_;
  42. // The number of `#`s preceding the opening `"` or `"""`.
  43. int hash_level_;
  44. // Whether this was a multi-line string literal.
  45. bool multi_line_;
  46. // Whether the literal is valid, or should only be used for errors.
  47. bool is_terminated_;
  48. };
  49. } // namespace Carbon