paren_contents.h 2.8 KB

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