precedence.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_PARSER_PRECEDENCE_H_
  5. #define CARBON_TOOLCHAIN_PARSER_PRECEDENCE_H_
  6. #include <optional>
  7. #include "toolchain/lexer/token_kind.h"
  8. namespace Carbon {
  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. // should have lower precedence than this.
  33. static auto ForPostfixExpression() -> PrecedenceGroup;
  34. // Get the sentinel precedence level for a top-level expression context. All
  35. // operators should have higher precedence than this.
  36. static auto ForTopLevelExpression() -> PrecedenceGroup;
  37. // Get the precedence level at which to parse a type expression. All type
  38. // operators should have higher precedence than this.
  39. static auto ForType() -> PrecedenceGroup;
  40. // Look up the operator information of the given prefix operator token, or
  41. // return std::nullopt if the given token is not a prefix operator.
  42. static auto ForLeading(TokenKind kind) -> std::optional<PrecedenceGroup>;
  43. // Look up the operator information of the given infix or postfix operator
  44. // token, or return std::nullopt if the given token is not an infix or postfix
  45. // operator. `infix` indicates whether this is a valid infix operator, but is
  46. // only considered if the same operator symbol is available as both infix and
  47. // postfix.
  48. static auto ForTrailing(TokenKind kind, bool infix)
  49. -> std::optional<Trailing>;
  50. friend auto operator==(PrecedenceGroup lhs, PrecedenceGroup rhs) -> bool {
  51. return lhs.level_ == rhs.level_;
  52. }
  53. friend auto operator!=(PrecedenceGroup lhs, PrecedenceGroup rhs) -> bool {
  54. return lhs.level_ != rhs.level_;
  55. }
  56. // Compare the precedence levels for two adjacent operators.
  57. static auto GetPriority(PrecedenceGroup left, PrecedenceGroup right)
  58. -> OperatorPriority;
  59. // Get the associativity of this precedence group.
  60. [[nodiscard]] auto GetAssociativity() const -> Associativity {
  61. return static_cast<Associativity>(GetPriority(*this, *this));
  62. }
  63. private:
  64. // We rely on implicit conversions via `int8_t` for enumerators defined in the
  65. // implementation.
  66. // NOLINTNEXTLINE(google-explicit-constructor)
  67. PrecedenceGroup(int8_t level) : level_(level) {}
  68. // The precedence level.
  69. int8_t level_;
  70. };
  71. // Precedence information for a trailing operator.
  72. struct PrecedenceGroup::Trailing {
  73. // The precedence level.
  74. PrecedenceGroup level;
  75. // `true` if this is an infix binary operator, `false` if this is a postfix
  76. // unary operator.
  77. bool is_binary;
  78. };
  79. } // namespace Carbon
  80. #endif // CARBON_TOOLCHAIN_PARSER_PRECEDENCE_H_