mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Factor out a Pattern sum type from Expression (#685)
`Pattern` is intended to pilot some changes I would like to apply to all our sum types: - The alternatives are expressed as derived classes rather than members of a `std::variant`. - The alternatives are classes in the [style guide sense](https://google.github.io/styleguide/cppguide.html#Structs_vs._Classes), meaning they can have invariants, but can't have public data members. - Creating an object is expressed using a constructor rather than a factory function. - Accessing an alternative is expressed as a cast (using LLVM's RTTI system) rather than `std::get` or a `Get` method. Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
co-authored by
Jon Meow
parent
b08f6bb0f1
commit
6ac3adfa53
@@ -5,47 +5,82 @@
|
||||
#ifndef EXECUTABLE_SEMANTICS_SYNTAX_PAREN_CONTENTS_H_
|
||||
#define EXECUTABLE_SEMANTICS_SYNTAX_PAREN_CONTENTS_H_
|
||||
|
||||
#include <list>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/common/error.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// Represents the syntactic contents of an expression delimited by
|
||||
// parentheses. Such expressions can be interpreted as either tuples or
|
||||
// arbitrary expressions, depending on their context and the syntax of their
|
||||
// Represents the syntactic contents of an expression or pattern delimited by
|
||||
// parentheses. In those syntaxes, parentheses can be used either for grouping
|
||||
// or for forming a tuple, depending on their context and the syntax of their
|
||||
// contents; this class helps calling code resolve that ambiguity. Since that
|
||||
// ambiguity is purely syntactic, this class should only be needed during
|
||||
// parsing.
|
||||
class ParenContents {
|
||||
public:
|
||||
// Indicates whether the paren expression's contents end with a comma.
|
||||
enum class HasTrailingComma { Yes, No };
|
||||
//
|
||||
// `Term` is the type of the syntactic grouping being built, and the type of
|
||||
// the individual syntactic units it's built from; typically it should be
|
||||
// either `Expression` or `Pattern`.
|
||||
template <typename Term>
|
||||
struct ParenContents {
|
||||
struct Element {
|
||||
std::optional<std::string> name;
|
||||
const Term* term;
|
||||
};
|
||||
|
||||
// Constructs a ParenContents representing the contents of "()".
|
||||
ParenContents() : fields_({}), has_trailing_comma_(HasTrailingComma::No) {}
|
||||
// If this object represents a single term, with no name and no trailing
|
||||
// comma, this method returns that term. This typically means the parentheses
|
||||
// can be interpreted as grouping.
|
||||
auto SingleTerm() const -> std::optional<const Term*>;
|
||||
|
||||
// Constructs a ParenContents representing the given list of fields,
|
||||
// with or without a trailing comma.
|
||||
ParenContents(std::vector<FieldInitializer> fields,
|
||||
HasTrailingComma has_trailing_comma)
|
||||
: fields_(fields), has_trailing_comma_(has_trailing_comma) {}
|
||||
// Converts `elements` to std::vector<TupleElement>. TupleElement must
|
||||
// have a constructor that takes a std::string and a const Term*.
|
||||
//
|
||||
// TODO: Find a way to deduce TupleElement from Term.
|
||||
template <typename TupleElement>
|
||||
auto TupleElements(int line_num) const -> std::vector<TupleElement>;
|
||||
|
||||
ParenContents(const ParenContents&) = default;
|
||||
ParenContents& operator=(const ParenContents&) = default;
|
||||
|
||||
// Returns the paren expression, interpreted as a tuple.
|
||||
const Expression* AsTuple(int line_number) const;
|
||||
|
||||
// Returns the paren expression, with no external constraints on what kind
|
||||
// of expression it represents.
|
||||
const Expression* AsExpression(int line_number) const;
|
||||
|
||||
private:
|
||||
std::vector<FieldInitializer> fields_;
|
||||
HasTrailingComma has_trailing_comma_;
|
||||
std::vector<Element> elements;
|
||||
bool has_trailing_comma;
|
||||
};
|
||||
|
||||
// Implementation details only below here.
|
||||
|
||||
template <typename Term>
|
||||
auto ParenContents<Term>::SingleTerm() const -> std::optional<const Term*> {
|
||||
if (elements.size() == 1 && !elements.front().name.has_value() &&
|
||||
!has_trailing_comma) {
|
||||
return elements.front().term;
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Term>
|
||||
template <typename TupleElement>
|
||||
auto ParenContents<Term>::TupleElements(int line_num) const
|
||||
-> std::vector<TupleElement> {
|
||||
std::vector<TupleElement> result;
|
||||
int i = 0;
|
||||
bool seen_named_member = false;
|
||||
for (auto element : elements) {
|
||||
if (element.name.has_value()) {
|
||||
seen_named_member = true;
|
||||
result.push_back(TupleElement(*element.name, element.term));
|
||||
} else {
|
||||
if (seen_named_member) {
|
||||
FATAL_USER_ERROR(line_num)
|
||||
<< "positional members must come before named members";
|
||||
}
|
||||
result.push_back(TupleElement(std::to_string(i), element.term));
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_SYNTAX_PAREN_CONTENTS_H_
|
||||
|
||||
Reference in New Issue
Block a user