precedence.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 CARBON_TOOLCHAIN_PARSE_PRECEDENCE_H_
  5. #define CARBON_TOOLCHAIN_PARSE_PRECEDENCE_H_
  6. #include <optional>
  7. #include "toolchain/lex/token_kind.h"
  8. namespace Carbon::Parse {
  9. // Given two operators `$` and `@`, and an expression `a $ b @ c`, how should
  10. // the expression be parsed?
  11. enum class OperatorPriority : int8_t {
  12. // The left operator has higher precedence: `(a $ b) @ c`.
  13. LeftFirst = -1,
  14. // The expression is ambiguous.
  15. Ambiguous = 0,
  16. // The right operator has higher precedence: `a $ (b @ c)`.
  17. RightFirst = 1,
  18. };
  19. enum class Associativity : int8_t {
  20. LeftToRight = -1,
  21. None = 0,
  22. RightToLeft = 1
  23. };
  24. // A precedence group associated with an operator or expression.
  25. class PrecedenceGroup {
  26. public:
  27. struct Trailing;
  28. // Objects of this type should only be constructed using the static factory
  29. // functions below.
  30. PrecedenceGroup() = delete;
  31. // Get the sentinel precedence level for a postfix expression. All operators
  32. // have lower precedence than this.
  33. static auto ForPostfixExpr() -> PrecedenceGroup;
  34. // Get the precedence level for a top-level or parenthesized expression. All
  35. // expression operators have higher precedence than this.
  36. static auto ForTopLevelExpr() -> PrecedenceGroup;
  37. // Get the sentinel precedence level for a statement context. All operators,
  38. // including statement operators like `=` and `++`, have higher precedence
  39. // than this.
  40. static auto ForExprStatement() -> PrecedenceGroup;
  41. // Get the precedence level at which to parse a type expression. All type
  42. // operators have higher precedence than this.
  43. static auto ForType() -> PrecedenceGroup;
  44. // Get the precedence level at which to parse the type expression between
  45. // `impl` and `as`.
  46. static auto ForImplAs() -> PrecedenceGroup;
  47. // Get the precedence level at which to parse the type expression between
  48. // `require` and `impls`.
  49. static auto ForRequireImpls() -> PrecedenceGroup;
  50. // Get the precedence level at which to parse expressions in requirements
  51. // after `where`, `require`, or `observe`.
  52. static auto ForRequirements() -> PrecedenceGroup;
  53. // Get the precedence level for a pattern that is not nested within another
  54. // (eg, tuple or struct) pattern.
  55. static auto ForTopLevelPattern() -> PrecedenceGroup;
  56. // Look up the operator information of the given prefix operator token, or
  57. // return std::nullopt if the given token is not a prefix operator.
  58. static auto ForLeading(Lex::TokenKind kind) -> std::optional<PrecedenceGroup>;
  59. // Look up the operator information of the given infix or postfix operator
  60. // token, or return std::nullopt if the given token is not an infix or postfix
  61. // operator. `infix` indicates whether this is a valid infix operator, but is
  62. // only considered if the same operator symbol is available as both infix and
  63. // postfix.
  64. static auto ForTrailing(Lex::TokenKind kind, bool infix)
  65. -> std::optional<Trailing>;
  66. friend auto operator==(PrecedenceGroup lhs, PrecedenceGroup rhs) -> bool {
  67. return lhs.level_ == rhs.level_;
  68. }
  69. // Compare the precedence levels for two adjacent operators.
  70. static auto GetPriority(PrecedenceGroup left, PrecedenceGroup right)
  71. -> OperatorPriority;
  72. // Get the associativity of this precedence group.
  73. auto GetAssociativity() const -> Associativity {
  74. return static_cast<Associativity>(GetPriority(*this, *this));
  75. }
  76. private:
  77. enum PrecedenceLevel : int8_t {
  78. // Sentinel representing the absence of any operator.
  79. Highest,
  80. // Terms.
  81. TermPrefix,
  82. // Numeric.
  83. IncrementDecrement,
  84. NumericPrefix,
  85. Modulo,
  86. Multiplicative,
  87. Additive,
  88. // Bitwise.
  89. BitwisePrefix,
  90. BitwiseAnd,
  91. BitwiseOr,
  92. BitwiseXor,
  93. BitShift,
  94. // Type formation.
  95. TypePrefix,
  96. TypePostfix,
  97. // `where` keyword.
  98. Where,
  99. // Casts.
  100. As,
  101. // Logical.
  102. LogicalPrefix,
  103. Relational,
  104. LogicalAnd,
  105. LogicalOr,
  106. // Conditional.
  107. If,
  108. // `ref`
  109. Ref,
  110. // Assignment.
  111. Assignment,
  112. // Sentinel representing a context in which any operator can appear.
  113. Lowest,
  114. };
  115. struct OperatorPriorityTable;
  116. static const int8_t NumPrecedenceLevels;
  117. // We rely on implicit conversions via `int8_t` for enumerators defined in the
  118. // implementation.
  119. explicit(false) PrecedenceGroup(int8_t level) : level_(level) {}
  120. // The precedence level.
  121. int8_t level_;
  122. };
  123. // Precedence information for a trailing operator.
  124. struct PrecedenceGroup::Trailing {
  125. // The precedence level.
  126. PrecedenceGroup level;
  127. // `true` if this is an infix binary operator, `false` if this is a postfix
  128. // unary operator.
  129. bool is_binary;
  130. };
  131. ////////////////////////////////////////////////////////////////////////////////
  132. //
  133. // Only implementation details below this point.
  134. //
  135. ////////////////////////////////////////////////////////////////////////////////
  136. inline auto PrecedenceGroup::ForPostfixExpr() -> PrecedenceGroup {
  137. return PrecedenceGroup(Highest);
  138. }
  139. inline auto PrecedenceGroup::ForTopLevelExpr() -> PrecedenceGroup {
  140. return PrecedenceGroup(If);
  141. }
  142. inline auto PrecedenceGroup::ForExprStatement() -> PrecedenceGroup {
  143. return PrecedenceGroup(Lowest);
  144. }
  145. inline auto PrecedenceGroup::ForType() -> PrecedenceGroup {
  146. return ForTopLevelExpr();
  147. }
  148. inline auto PrecedenceGroup::ForImplAs() -> PrecedenceGroup {
  149. return PrecedenceGroup(As);
  150. }
  151. inline auto PrecedenceGroup::ForRequirements() -> PrecedenceGroup {
  152. return PrecedenceGroup(Where);
  153. }
  154. inline auto PrecedenceGroup::ForTopLevelPattern() -> PrecedenceGroup {
  155. return PrecedenceGroup(Relational);
  156. }
  157. } // namespace Carbon::Parse
  158. #endif // CARBON_TOOLCHAIN_PARSE_PRECEDENCE_H_