precedence.h 5.3 KB

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