paren_contents.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. Nonnull<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<Nonnull<const Term*>>;
  32. // Converts `elements` to std::vector<TupleElement>. TupleElement must
  33. // have a constructor that takes a std::string and a Nonnull<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
  44. -> std::optional<Nonnull<const Term*>> {
  45. if (elements.size() == 1 && !elements.front().name.has_value() &&
  46. !has_trailing_comma) {
  47. return elements.front().term;
  48. } else {
  49. return std::nullopt;
  50. }
  51. }
  52. template <typename Term>
  53. template <typename TupleElement>
  54. auto ParenContents<Term>::TupleElements(SourceLocation loc) const
  55. -> std::vector<TupleElement> {
  56. std::vector<TupleElement> result;
  57. int i = 0;
  58. bool seen_named_member = false;
  59. for (auto element : elements) {
  60. if (element.name.has_value()) {
  61. seen_named_member = true;
  62. result.push_back(TupleElement(*element.name, element.term));
  63. } else {
  64. if (seen_named_member) {
  65. FATAL_PROGRAM_ERROR(loc)
  66. << "positional members must come before named members";
  67. }
  68. result.push_back(TupleElement(std::to_string(i), element.term));
  69. }
  70. ++i;
  71. }
  72. return result;
  73. }
  74. } // namespace Carbon
  75. #endif // EXECUTABLE_SEMANTICS_AST_PAREN_CONTENTS_H_