paren_contents.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 EXECUTABLE_SEMANTICS_AST_PAREN_CONTENTS_H_
  5. #define EXECUTABLE_SEMANTICS_AST_PAREN_CONTENTS_H_
  6. #include <optional>
  7. #include <string>
  8. #include <vector>
  9. #include "executable_semantics/ast/source_location.h"
  10. #include "executable_semantics/common/error.h"
  11. namespace Carbon {
  12. // Represents the syntactic contents of an expression or pattern delimited by
  13. // parentheses. In those syntaxes, parentheses can be used either for grouping
  14. // or for forming a tuple, depending on their context and the syntax of their
  15. // contents; this class helps calling code resolve that ambiguity. Since that
  16. // ambiguity is purely syntactic, this class should only be needed during
  17. // parsing.
  18. //
  19. // `Term` is the type of the syntactic grouping being built, and the type of
  20. // the individual syntactic units it's built from; typically it should be
  21. // either `Expression` or `Pattern`.
  22. template <typename Term>
  23. struct ParenContents {
  24. struct Element {
  25. std::optional<std::string> name;
  26. Ptr<const Term> term;
  27. };
  28. // If this object represents a single term, with no name and no trailing
  29. // comma, this method returns that term. This typically means the parentheses
  30. // can be interpreted as grouping.
  31. auto SingleTerm() const -> std::optional<Ptr<const Term>>;
  32. // Converts `elements` to std::vector<TupleElement>. TupleElement must
  33. // have a constructor that takes a std::string and a Ptr<const Term>.
  34. //
  35. // TODO: Find a way to deduce TupleElement from Term.
  36. template <typename TupleElement>
  37. auto TupleElements(SourceLocation loc) const -> std::vector<TupleElement>;
  38. std::vector<Element> elements;
  39. bool has_trailing_comma;
  40. };
  41. // Implementation details only below here.
  42. template <typename Term>
  43. auto ParenContents<Term>::SingleTerm() const -> std::optional<Ptr<const Term>> {
  44. if (elements.size() == 1 && !elements.front().name.has_value() &&
  45. !has_trailing_comma) {
  46. return elements.front().term;
  47. } else {
  48. return std::nullopt;
  49. }
  50. }
  51. template <typename Term>
  52. template <typename TupleElement>
  53. auto ParenContents<Term>::TupleElements(SourceLocation loc) const
  54. -> std::vector<TupleElement> {
  55. std::vector<TupleElement> result;
  56. int i = 0;
  57. bool seen_named_member = false;
  58. for (auto element : elements) {
  59. if (element.name.has_value()) {
  60. seen_named_member = true;
  61. result.push_back(TupleElement(*element.name, element.term));
  62. } else {
  63. if (seen_named_member) {
  64. FATAL_PROGRAM_ERROR(loc)
  65. << "positional members must come before named members";
  66. }
  67. result.push_back(TupleElement(std::to_string(i), element.term));
  68. }
  69. ++i;
  70. }
  71. return result;
  72. }
  73. } // namespace Carbon
  74. #endif // EXECUTABLE_SEMANTICS_AST_PAREN_CONTENTS_H_