[toolchain] Parse variables and parameters as 'name: Type'. (#574)

Factor out code for pattern parsing, that only recognizes this form for
now, and uniformly form a 'PatternBinding' parse node for this, for
both variables and patterns.
This commit is contained in:
Richard Smith
2021-06-14 14:28:45 -07:00
committed by GitHub
parent 47ee66feef
commit 61b30b1243
6 changed files with 103 additions and 58 deletions
+16
View File
@@ -32,6 +32,8 @@ enum PrecedenceLevel : int8_t {
// Assignment.
SimpleAssignment,
CompoundAssignment,
// Sentinel representing a type context.
Type,
// Sentinel representing a context in which any operator can appear.
Lowest,
};
@@ -58,6 +60,16 @@ struct OperatorPriorityTable {
{SimpleAssignment, CompoundAssignment, LogicalAnd, LogicalOr},
{Lowest});
// FIXME: Decide upon a precedence level to use for types. It's important
// that this is no higher than simple assignment, otherwise
// var x: T = y;
// would be parsed as
// var x: (T = y);
// For now, we have no type operators and no operator overloading, so we
// only parse primary expressions in types.
MarkHigherThan({Highest}, {Type});
MarkHigherThan({Type}, {Lowest});
// Compute the transitive closure of the above relationships: if we parse
// `a $ b @ c` as `(a $ b) @ c` and parse `b @ c % d` as `(b @ c) % d`,
// then we will parse `a $ b @ c % d` as `((a $ b) @ c) % d` and should
@@ -181,6 +193,10 @@ auto PrecedenceGroup::ForTopLevelExpression() -> PrecedenceGroup {
return PrecedenceGroup(Lowest);
}
auto PrecedenceGroup::ForType() -> PrecedenceGroup {
return PrecedenceGroup(Type);
}
auto PrecedenceGroup::ForLeading(TokenKind kind)
-> llvm::Optional<PrecedenceGroup> {
switch (kind) {