paren_contents.h 2.2 KB

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