paren_contents.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 <list>
  7. #include "executable_semantics/ast/expression.h"
  8. namespace Carbon {
  9. // Represents the syntactic contents of an expression delimited by
  10. // parentheses. Such expressions can be interpreted as either tuples or
  11. // arbitrary expressions, depending on their context and the syntax of their
  12. // contents; this class helps calling code resolve that ambiguity. Since that
  13. // ambiguity is purely syntactic, this class should only be needed during
  14. // parsing.
  15. class ParenContents {
  16. public:
  17. // Indicates whether the paren expression's contents end with a comma.
  18. enum class HasTrailingComma { Yes, No };
  19. // Constructs a ParenContents representing the contents of "()".
  20. ParenContents() : fields_({}), has_trailing_comma_(HasTrailingComma::No) {}
  21. // Constructs a ParenContents representing the given list of fields,
  22. // with or without a trailing comma.
  23. ParenContents(std::vector<FieldInitializer> fields,
  24. HasTrailingComma has_trailing_comma)
  25. : fields_(fields), has_trailing_comma_(has_trailing_comma) {}
  26. ParenContents(const ParenContents&) = default;
  27. ParenContents& operator=(const ParenContents&) = default;
  28. // Returns the paren expression, interpreted as a tuple.
  29. const Expression* AsTuple(int line_number) const;
  30. // Returns the paren expression, with no external constraints on what kind
  31. // of expression it represents.
  32. const Expression* AsExpression(int line_number) const;
  33. private:
  34. std::vector<FieldInitializer> fields_;
  35. HasTrailingComma has_trailing_comma_;
  36. };
  37. } // namespace Carbon
  38. #endif // EXECUTABLE_SEMANTICS_SYNTAX_PAREN_CONTENTS_H_