diff --git a/toolchain/parse/precedence.cpp b/toolchain/parse/precedence.cpp index bc68a3500c8c..f62c118d40a8 100644 --- a/toolchain/parse/precedence.cpp +++ b/toolchain/parse/precedence.cpp @@ -8,48 +8,11 @@ namespace Carbon::Parse { -namespace { -enum PrecedenceLevel : int8_t { - // Sentinel representing the absence of any operator. - Highest, - // Terms. - TermPrefix, - // Numeric. - IncrementDecrement, - NumericPrefix, - Modulo, - Multiplicative, - Additive, - // Bitwise. - BitwisePrefix, - BitwiseAnd, - BitwiseOr, - BitwiseXor, - BitShift, - // Type formation. - TypePrefix, - TypePostfix, - // `where` keyword. - Where, - // Casts. - As, - // Logical. - LogicalPrefix, - Relational, - LogicalAnd, - LogicalOr, - // Conditional. - If, - // Assignment. - Assignment, - // Sentinel representing a context in which any operator can appear. - Lowest, -}; -constexpr int8_t NumPrecedenceLevels = Lowest + 1; +constexpr int8_t PrecedenceGroup::NumPrecedenceLevels = Lowest + 1; // A precomputed lookup table determining the relative precedence of two // precedence groups. -struct OperatorPriorityTable { +struct PrecedenceGroup::OperatorPriorityTable { constexpr OperatorPriorityTable() : table() { // Start with a list of , // relationships. @@ -176,29 +139,6 @@ struct OperatorPriorityTable { OperatorPriority table[NumPrecedenceLevels][NumPrecedenceLevels]; }; -} // namespace - -auto PrecedenceGroup::ForPostfixExpr() -> PrecedenceGroup { - return PrecedenceGroup(Highest); -} - -auto PrecedenceGroup::ForTopLevelExpr() -> PrecedenceGroup { - return PrecedenceGroup(If); -} - -auto PrecedenceGroup::ForExprStatement() -> PrecedenceGroup { - return PrecedenceGroup(Lowest); -} - -auto PrecedenceGroup::ForType() -> PrecedenceGroup { return ForTopLevelExpr(); } - -auto PrecedenceGroup::ForImplAs() -> PrecedenceGroup { - return PrecedenceGroup(As); -} - -auto PrecedenceGroup::ForRequirements() -> PrecedenceGroup { - return PrecedenceGroup(Where); -} auto PrecedenceGroup::ForLeading(Lex::TokenKind kind) -> std::optional { diff --git a/toolchain/parse/precedence.h b/toolchain/parse/precedence.h index d418a0fad898..5cd290efb9ab 100644 --- a/toolchain/parse/precedence.h +++ b/toolchain/parse/precedence.h @@ -88,6 +88,11 @@ class PrecedenceGroup { } private: + enum PrecedenceLevel : int8_t; + struct OperatorPriorityTable; + + static const int8_t NumPrecedenceLevels; + // We rely on implicit conversions via `int8_t` for enumerators defined in the // implementation. // NOLINTNEXTLINE(google-explicit-constructor) @@ -106,6 +111,73 @@ struct PrecedenceGroup::Trailing { bool is_binary; }; +//////////////////////////////////////////////////////////////////////////////// +// +// Only implementation details below this point. +// +//////////////////////////////////////////////////////////////////////////////// + +enum PrecedenceGroup::PrecedenceLevel : int8_t { + // Sentinel representing the absence of any operator. + Highest, + // Terms. + TermPrefix, + // Numeric. + IncrementDecrement, + NumericPrefix, + Modulo, + Multiplicative, + Additive, + // Bitwise. + BitwisePrefix, + BitwiseAnd, + BitwiseOr, + BitwiseXor, + BitShift, + // Type formation. + TypePrefix, + TypePostfix, + // `where` keyword. + Where, + // Casts. + As, + // Logical. + LogicalPrefix, + Relational, + LogicalAnd, + LogicalOr, + // Conditional. + If, + // Assignment. + Assignment, + // Sentinel representing a context in which any operator can appear. + Lowest, +}; + +inline auto PrecedenceGroup::ForPostfixExpr() -> PrecedenceGroup { + return PrecedenceGroup(Highest); +} + +inline auto PrecedenceGroup::ForTopLevelExpr() -> PrecedenceGroup { + return PrecedenceGroup(If); +} + +inline auto PrecedenceGroup::ForExprStatement() -> PrecedenceGroup { + return PrecedenceGroup(Lowest); +} + +inline auto PrecedenceGroup::ForType() -> PrecedenceGroup { + return ForTopLevelExpr(); +} + +inline auto PrecedenceGroup::ForImplAs() -> PrecedenceGroup { + return PrecedenceGroup(As); +} + +inline auto PrecedenceGroup::ForRequirements() -> PrecedenceGroup { + return PrecedenceGroup(Where); +} + } // namespace Carbon::Parse #endif // CARBON_TOOLCHAIN_PARSE_PRECEDENCE_H_