string_literal.h 1.8 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. #include <string>
  5. #include "diagnostics/diagnostic_emitter.h"
  6. #include "llvm/ADT/Optional.h"
  7. #include "llvm/ADT/StringRef.h"
  8. namespace Carbon {
  9. class StringLiteralToken {
  10. public:
  11. // Get the text corresponding to this literal.
  12. auto Text() const -> llvm::StringRef { return text; }
  13. // Determine whether this is a multi-line string literal.
  14. 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<StringLiteralToken>;
  19. // The result of expanding escape sequences in a string literal.
  20. struct ExpandedValue {
  21. std::string result;
  22. bool has_errors;
  23. };
  24. // Expand any escape sequences in the given string literal and compute the
  25. // resulting value.
  26. auto ComputeValue(DiagnosticEmitter& emitter) const -> ExpandedValue;
  27. private:
  28. StringLiteralToken(llvm::StringRef text, llvm::StringRef content,
  29. int hash_level, bool multi_line)
  30. : text(text),
  31. content(content),
  32. hash_level(hash_level),
  33. multi_line(multi_line) {}
  34. // The complete text of the string literal.
  35. llvm::StringRef text;
  36. // The content of the literal. For a multi-line literal, this begins
  37. // immediately after the newline following the file type indicator, and ends
  38. // at the start of the closing `"""`. Leading whitespace is not removed from
  39. // either end.
  40. llvm::StringRef content;
  41. // The number of `#`s preceding the opening `"` or `"""`.
  42. int hash_level;
  43. // Whether this was a multi-line string literal.
  44. bool multi_line;
  45. };
  46. } // namespace Carbon