precedence.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // Look up the operator information of the given prefix operator token, or
  45. // return std::nullopt if the given token is not a prefix operator.
  46. static auto ForLeading(Lex::TokenKind kind) -> std::optional<PrecedenceGroup>;
  47. // Look up the operator information of the given infix or postfix operator
  48. // token, or return std::nullopt if the given token is not an infix or postfix
  49. // operator. `infix` indicates whether this is a valid infix operator, but is
  50. // only considered if the same operator symbol is available as both infix and
  51. // postfix.
  52. static auto ForTrailing(Lex::TokenKind kind, bool infix)
  53. -> std::optional<Trailing>;
  54. friend auto operator==(PrecedenceGroup lhs, PrecedenceGroup rhs) -> bool {
  55. return lhs.level_ == rhs.level_;
  56. }
  57. friend auto operator!=(PrecedenceGroup lhs, PrecedenceGroup rhs) -> bool {
  58. return lhs.level_ != rhs.level_;
  59. }
  60. // Compare the precedence levels for two adjacent operators.
  61. static auto GetPriority(PrecedenceGroup left, PrecedenceGroup right)
  62. -> OperatorPriority;
  63. // Get the associativity of this precedence group.
  64. [[nodiscard]] auto GetAssociativity() const -> Associativity {
  65. return static_cast<Associativity>(GetPriority(*this, *this));
  66. }
  67. private:
  68. // We rely on implicit conversions via `int8_t` for enumerators defined in the
  69. // implementation.
  70. // NOLINTNEXTLINE(google-explicit-constructor)
  71. PrecedenceGroup(int8_t level) : level_(level) {}
  72. // The precedence level.
  73. int8_t level_;
  74. };
  75. // Precedence information for a trailing operator.
  76. struct PrecedenceGroup::Trailing {
  77. // The precedence level.
  78. PrecedenceGroup level;
  79. // `true` if this is an infix binary operator, `false` if this is a postfix
  80. // unary operator.
  81. bool is_binary;
  82. };
  83. } // namespace Carbon::Parse
  84. #endif // CARBON_TOOLCHAIN_PARSE_PRECEDENCE_H_