string_literal.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_TOOLCHAIN_LEXER_STRING_LITERAL_H_
  5. #define CARBON_TOOLCHAIN_LEXER_STRING_LITERAL_H_
  6. #include <string>
  7. #include "llvm/ADT/Optional.h"
  8. #include "llvm/ADT/StringRef.h"
  9. #include "toolchain/diagnostics/diagnostic_emitter.h"
  10. namespace Carbon {
  11. class LexedStringLiteral {
  12. public:
  13. // Extract a string literal token from the given text, if it has a suitable
  14. // form. Returning llvm::None indicates no string literal was found; returning
  15. // an invalid literal indicates a string prefix was found, but it's malformed
  16. // and is returning a partial string literal to assist error construction.
  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. // Get the text corresponding to this literal.
  24. [[nodiscard]] auto text() const -> llvm::StringRef { return text_; }
  25. // Determine whether this is a multi-line string literal.
  26. [[nodiscard]] auto is_multi_line() const -> bool { return multi_line_; }
  27. // Returns true if the string has a valid terminator.
  28. [[nodiscard]] auto is_terminated() const -> bool { return is_terminated_; }
  29. private:
  30. LexedStringLiteral(llvm::StringRef text, llvm::StringRef content,
  31. int hash_level, bool multi_line, bool is_terminated)
  32. : text_(text),
  33. content_(content),
  34. hash_level_(hash_level),
  35. multi_line_(multi_line),
  36. is_terminated_(is_terminated) {}
  37. // The complete text of the string literal.
  38. llvm::StringRef text_;
  39. // The content of the literal. For a multi-line literal, this begins
  40. // immediately after the newline following the file type indicator, and ends
  41. // at the start of the closing `"""`. Leading whitespace is not removed from
  42. // either end.
  43. llvm::StringRef content_;
  44. // The number of `#`s preceding the opening `"` or `"""`.
  45. int hash_level_;
  46. // Whether this was a multi-line string literal.
  47. bool multi_line_;
  48. // Whether the literal is valid, or should only be used for errors.
  49. bool is_terminated_;
  50. };
  51. } // namespace Carbon
  52. #endif // CARBON_TOOLCHAIN_LEXER_STRING_LITERAL_H_