precedence.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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` or `require`.
  52. static auto ForRequirements() -> PrecedenceGroup;
  53. // Look up the operator information of the given prefix operator token, or
  54. // return std::nullopt if the given token is not a prefix operator.
  55. static auto ForLeading(Lex::TokenKind kind) -> std::optional<PrecedenceGroup>;
  56. // Look up the operator information of the given infix or postfix operator
  57. // token, or return std::nullopt if the given token is not an infix or postfix
  58. // operator. `infix` indicates whether this is a valid infix operator, but is
  59. // only considered if the same operator symbol is available as both infix and
  60. // postfix.
  61. static auto ForTrailing(Lex::TokenKind kind, bool infix)
  62. -> std::optional<Trailing>;
  63. friend auto operator==(PrecedenceGroup lhs, PrecedenceGroup rhs) -> bool {
  64. return lhs.level_ == rhs.level_;
  65. }
  66. // Compare the precedence levels for two adjacent operators.
  67. static auto GetPriority(PrecedenceGroup left, PrecedenceGroup right)
  68. -> OperatorPriority;
  69. // Get the associativity of this precedence group.
  70. auto GetAssociativity() const -> Associativity {
  71. return static_cast<Associativity>(GetPriority(*this, *this));
  72. }
  73. private:
  74. enum PrecedenceLevel : int8_t {
  75. // Sentinel representing the absence of any operator.
  76. Highest,
  77. // Terms.
  78. TermPrefix,
  79. // Numeric.
  80. IncrementDecrement,
  81. NumericPrefix,
  82. Modulo,
  83. Multiplicative,
  84. Additive,
  85. // Bitwise.
  86. BitwisePrefix,
  87. BitwiseAnd,
  88. BitwiseOr,
  89. BitwiseXor,
  90. BitShift,
  91. // Type formation.
  92. TypePrefix,
  93. TypePostfix,
  94. // `where` keyword.
  95. Where,
  96. // Casts.
  97. As,
  98. // Logical.
  99. LogicalPrefix,
  100. Relational,
  101. LogicalAnd,
  102. LogicalOr,
  103. // Conditional.
  104. If,
  105. // `ref`
  106. Ref,
  107. // Assignment.
  108. Assignment,
  109. // Sentinel representing a context in which any operator can appear.
  110. Lowest,
  111. };
  112. struct OperatorPriorityTable;
  113. static const int8_t NumPrecedenceLevels;
  114. // We rely on implicit conversions via `int8_t` for enumerators defined in the
  115. // implementation.
  116. explicit(false) PrecedenceGroup(int8_t level) : level_(level) {}
  117. // The precedence level.
  118. int8_t level_;
  119. };
  120. // Precedence information for a trailing operator.
  121. struct PrecedenceGroup::Trailing {
  122. // The precedence level.
  123. PrecedenceGroup level;
  124. // `true` if this is an infix binary operator, `false` if this is a postfix
  125. // unary operator.
  126. bool is_binary;
  127. };
  128. ////////////////////////////////////////////////////////////////////////////////
  129. //
  130. // Only implementation details below this point.
  131. //
  132. ////////////////////////////////////////////////////////////////////////////////
  133. inline auto PrecedenceGroup::ForPostfixExpr() -> PrecedenceGroup {
  134. return PrecedenceGroup(Highest);
  135. }
  136. inline auto PrecedenceGroup::ForTopLevelExpr() -> PrecedenceGroup {
  137. return PrecedenceGroup(If);
  138. }
  139. inline auto PrecedenceGroup::ForExprStatement() -> PrecedenceGroup {
  140. return PrecedenceGroup(Lowest);
  141. }
  142. inline auto PrecedenceGroup::ForType() -> PrecedenceGroup {
  143. return ForTopLevelExpr();
  144. }
  145. inline auto PrecedenceGroup::ForImplAs() -> PrecedenceGroup {
  146. return PrecedenceGroup(As);
  147. }
  148. inline auto PrecedenceGroup::ForRequirements() -> PrecedenceGroup {
  149. return PrecedenceGroup(Where);
  150. }
  151. } // namespace Carbon::Parse
  152. #endif // CARBON_TOOLCHAIN_PARSE_PRECEDENCE_H_