string_literal.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_LEX_STRING_LITERAL_H_
  5. #define CARBON_TOOLCHAIN_LEX_STRING_LITERAL_H_
  6. #include <optional>
  7. #include "llvm/ADT/StringRef.h"
  8. #include "llvm/Support/Allocator.h"
  9. #include "toolchain/diagnostics/diagnostic_emitter.h"
  10. namespace Carbon::Lex {
  11. class StringLiteral {
  12. public:
  13. // Extract a string literal token from the given text, if it has a suitable
  14. // form. Returning std::nullopt indicates no string literal was found;
  15. // returning an invalid literal indicates a string prefix was found, but it's
  16. // malformed and is returning a partial string literal to assist error
  17. // construction.
  18. static auto Lex(llvm::StringRef source_text) -> std::optional<StringLiteral>;
  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. //
  22. // When content_needs_validation_ is false and the string has no indent to
  23. // deal with, this can return the content directly. Otherwise, the allocator
  24. // will be used for the StringRef.
  25. auto ComputeValue(llvm::BumpPtrAllocator& allocator,
  26. DiagnosticEmitter<const char*>& emitter) const
  27. -> llvm::StringRef;
  28. // Get the text corresponding to this literal.
  29. auto text() const -> llvm::StringRef { return text_; }
  30. // Determine whether this is a multi-line string literal.
  31. auto is_multi_line() const -> bool { return multi_line_; }
  32. // Returns true if the string has a valid terminator.
  33. auto is_terminated() const -> bool { return is_terminated_; }
  34. private:
  35. enum MultiLineKind : int8_t {
  36. NotMultiLine,
  37. MultiLine,
  38. MultiLineWithDoubleQuotes
  39. };
  40. struct Introducer;
  41. explicit StringLiteral(llvm::StringRef text, llvm::StringRef content,
  42. bool content_needs_validation, int hash_level,
  43. MultiLineKind multi_line, bool is_terminated)
  44. : text_(text),
  45. content_(content),
  46. content_needs_validation_(content_needs_validation),
  47. hash_level_(hash_level),
  48. multi_line_(multi_line),
  49. is_terminated_(is_terminated) {}
  50. // The complete text of the string literal.
  51. llvm::StringRef text_;
  52. // The content of the literal. For a multi-line literal, this begins
  53. // immediately after the newline following the file type indicator, and ends
  54. // at the start of the closing `"""`. Leading whitespace is not removed from
  55. // either end.
  56. llvm::StringRef content_;
  57. // Whether content needs validation, in particular due to either an escape
  58. // (which needs modifications) or a tab character (which may cause a warning).
  59. bool content_needs_validation_;
  60. // The number of `#`s preceding the opening `"` or `"""`.
  61. int hash_level_;
  62. // Whether this was a multi-line string literal.
  63. MultiLineKind multi_line_;
  64. // Whether the literal is valid, or should only be used for errors.
  65. bool is_terminated_;
  66. };
  67. } // namespace Carbon::Lex
  68. #endif // CARBON_TOOLCHAIN_LEX_STRING_LITERAL_H_