precedence.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #include "toolchain/parser/precedence.h"
  5. #include <utility>
  6. namespace Carbon {
  7. namespace {
  8. enum PrecedenceLevel : int8_t {
  9. // Sentinel representing the absence of any operator.
  10. Highest,
  11. // Numeric.
  12. NumericPrefix,
  13. NumericPostfix,
  14. Modulo,
  15. Multiplicative,
  16. Additive,
  17. // Bitwise.
  18. BitwisePrefix,
  19. BitwiseAnd,
  20. BitwiseOr,
  21. BitwiseXor,
  22. BitShift,
  23. // Type formation.
  24. TypePostfix,
  25. // Sentinel representing a type context.
  26. Type,
  27. // Logical.
  28. LogicalPrefix,
  29. Relational,
  30. LogicalAnd,
  31. LogicalOr,
  32. // Assignment.
  33. SimpleAssignment,
  34. CompoundAssignment,
  35. // Sentinel representing a context in which any operator can appear.
  36. Lowest,
  37. };
  38. constexpr int8_t NumPrecedenceLevels = Lowest + 1;
  39. // A precomputed lookup table determining the relative precedence of two
  40. // precedence groups.
  41. struct OperatorPriorityTable {
  42. constexpr OperatorPriorityTable() : table() {
  43. // Start with a list of <higher precedence>, <lower precedence>
  44. // relationships.
  45. MarkHigherThan({Highest}, {NumericPrefix, BitwisePrefix, LogicalPrefix,
  46. NumericPostfix, TypePostfix});
  47. MarkHigherThan({NumericPrefix, NumericPostfix},
  48. {Modulo, Multiplicative, BitShift});
  49. MarkHigherThan({Multiplicative}, {Additive});
  50. MarkHigherThan({BitwisePrefix},
  51. {BitwiseAnd, BitwiseOr, BitwiseXor, BitShift});
  52. MarkHigherThan({TypePostfix}, {Type});
  53. MarkHigherThan(
  54. {Modulo, Additive, BitwiseAnd, BitwiseOr, BitwiseXor, BitShift, Type},
  55. {SimpleAssignment, CompoundAssignment, Relational});
  56. MarkHigherThan({Relational, LogicalPrefix}, {LogicalAnd, LogicalOr});
  57. MarkHigherThan(
  58. {SimpleAssignment, CompoundAssignment, LogicalAnd, LogicalOr},
  59. {Lowest});
  60. // Compute the transitive closure of the above relationships: if we parse
  61. // `a $ b @ c` as `(a $ b) @ c` and parse `b @ c % d` as `(b @ c) % d`,
  62. // then we will parse `a $ b @ c % d` as `((a $ b) @ c) % d` and should
  63. // also parse `a $ bc % d` as `(a $ bc) % d`.
  64. MakeTransitivelyClosed();
  65. // Make the relation symmetric. If we parse `a $ b @ c` as `(a $ b) @ c`
  66. // then we want to parse `a @ b $ c` as `a @ (b $ c)`.
  67. MakeSymmetric();
  68. // Fill in the diagonal, which represents operator associativity.
  69. AddAssociativityRules();
  70. ConsistencyCheck();
  71. }
  72. constexpr void MarkHigherThan(
  73. std::initializer_list<PrecedenceLevel> higher_group,
  74. std::initializer_list<PrecedenceLevel> lower_group) {
  75. for (auto higher : higher_group) {
  76. for (auto lower : lower_group) {
  77. table[higher][lower] = OperatorPriority::LeftFirst;
  78. }
  79. }
  80. }
  81. constexpr void MakeTransitivelyClosed() {
  82. // A naive algorithm compiles acceptably fast for now (~0.5s). This should
  83. // be revisited if we see compile time problems after adding precedence
  84. // groups; it's easy to do this faster.
  85. bool changed = false;
  86. do {
  87. changed = false;
  88. for (int8_t a = 0; a != NumPrecedenceLevels; ++a) {
  89. for (int8_t b = 0; b != NumPrecedenceLevels; ++b) {
  90. if (table[a][b] == OperatorPriority::LeftFirst) {
  91. for (int8_t c = 0; c != NumPrecedenceLevels; ++c) {
  92. if (table[b][c] == OperatorPriority::LeftFirst &&
  93. table[a][c] != OperatorPriority::LeftFirst) {
  94. table[a][c] = OperatorPriority::LeftFirst;
  95. changed = true;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. } while (changed);
  102. }
  103. constexpr void MakeSymmetric() {
  104. for (int8_t a = 0; a != NumPrecedenceLevels; ++a) {
  105. for (int8_t b = 0; b != NumPrecedenceLevels; ++b) {
  106. if (table[a][b] == OperatorPriority::LeftFirst) {
  107. if (table[b][a] == OperatorPriority::LeftFirst) {
  108. throw "inconsistent lookup table entries";
  109. }
  110. table[b][a] = OperatorPriority::RightFirst;
  111. }
  112. }
  113. }
  114. }
  115. constexpr void AddAssociativityRules() {
  116. // Associativity rules occupy the diagonal
  117. // For prefix operators, RightFirst would mean `@@x` is `@(@x)` and
  118. // Ambiguous would mean it's an error. LeftFirst is meaningless. For now we
  119. // allow all prefix operators to be repeated.
  120. for (PrecedenceLevel prefix :
  121. {NumericPrefix, BitwisePrefix, LogicalPrefix}) {
  122. table[prefix][prefix] = OperatorPriority::RightFirst;
  123. }
  124. // Postfix operators are symmetric with prefix operators.
  125. for (PrecedenceLevel postfix : {NumericPostfix, TypePostfix}) {
  126. table[postfix][postfix] = OperatorPriority::LeftFirst;
  127. }
  128. // Traditionally-associative operators are given left-to-right
  129. // associativity.
  130. for (PrecedenceLevel assoc :
  131. {Multiplicative, Additive, BitwiseAnd, BitwiseOr, BitwiseXor,
  132. LogicalAnd, LogicalOr}) {
  133. table[assoc][assoc] = OperatorPriority::LeftFirst;
  134. }
  135. // Assignment is given right-to-left associativity in order to support
  136. // chained assignment.
  137. table[SimpleAssignment][SimpleAssignment] = OperatorPriority::RightFirst;
  138. // For other operators, there isn't an obvious answer and we require
  139. // explicit parentheses.
  140. }
  141. constexpr void ConsistencyCheck() {
  142. for (int8_t level = 0; level != NumPrecedenceLevels; ++level) {
  143. if (level != Highest) {
  144. if (table[Highest][level] != OperatorPriority::LeftFirst ||
  145. table[level][Highest] != OperatorPriority::RightFirst) {
  146. throw "Highest is not highest priority";
  147. }
  148. }
  149. if (level != Lowest) {
  150. if (table[Lowest][level] != OperatorPriority::RightFirst ||
  151. table[level][Lowest] != OperatorPriority::LeftFirst) {
  152. throw "Lowest is not lowest priority";
  153. }
  154. }
  155. }
  156. }
  157. OperatorPriority table[NumPrecedenceLevels][NumPrecedenceLevels];
  158. };
  159. } // namespace
  160. auto PrecedenceGroup::ForPostfixExpression() -> PrecedenceGroup {
  161. return PrecedenceGroup(Highest);
  162. }
  163. auto PrecedenceGroup::ForTopLevelExpression() -> PrecedenceGroup {
  164. return PrecedenceGroup(Lowest);
  165. }
  166. auto PrecedenceGroup::ForType() -> PrecedenceGroup {
  167. return PrecedenceGroup(Type);
  168. }
  169. auto PrecedenceGroup::ForLeading(TokenKind kind)
  170. -> llvm::Optional<PrecedenceGroup> {
  171. switch (kind) {
  172. case TokenKind::NotKeyword():
  173. return PrecedenceGroup(LogicalPrefix);
  174. case TokenKind::Minus():
  175. case TokenKind::MinusMinus():
  176. case TokenKind::PlusPlus():
  177. return PrecedenceGroup(NumericPrefix);
  178. case TokenKind::Tilde():
  179. return PrecedenceGroup(BitwisePrefix);
  180. default:
  181. return llvm::None;
  182. }
  183. }
  184. auto PrecedenceGroup::ForTrailing(TokenKind kind, bool infix)
  185. -> llvm::Optional<Trailing> {
  186. switch (kind) {
  187. // Assignment operators.
  188. case TokenKind::Equal():
  189. return Trailing{.level = SimpleAssignment, .is_binary = true};
  190. case TokenKind::PlusEqual():
  191. case TokenKind::MinusEqual():
  192. case TokenKind::StarEqual():
  193. case TokenKind::SlashEqual():
  194. case TokenKind::PercentEqual():
  195. case TokenKind::AmpEqual():
  196. case TokenKind::PipeEqual():
  197. case TokenKind::GreaterGreaterEqual():
  198. case TokenKind::LessLessEqual():
  199. return Trailing{.level = CompoundAssignment, .is_binary = true};
  200. // Logical operators.
  201. case TokenKind::AndKeyword():
  202. return Trailing{.level = LogicalAnd, .is_binary = true};
  203. case TokenKind::OrKeyword():
  204. return Trailing{.level = LogicalOr, .is_binary = true};
  205. // Bitwise operators.
  206. case TokenKind::Amp():
  207. return Trailing{.level = BitwiseAnd, .is_binary = true};
  208. case TokenKind::Pipe():
  209. return Trailing{.level = BitwiseOr, .is_binary = true};
  210. case TokenKind::XorKeyword():
  211. return Trailing{.level = BitwiseXor, .is_binary = true};
  212. case TokenKind::GreaterGreater():
  213. case TokenKind::LessLess():
  214. return Trailing{.level = BitShift, .is_binary = true};
  215. // Relational operators.
  216. case TokenKind::EqualEqual():
  217. case TokenKind::ExclaimEqual():
  218. case TokenKind::Less():
  219. case TokenKind::LessEqual():
  220. case TokenKind::Greater():
  221. case TokenKind::GreaterEqual():
  222. case TokenKind::LessEqualGreater():
  223. return Trailing{.level = Relational, .is_binary = true};
  224. // Addative operators.
  225. case TokenKind::Plus():
  226. case TokenKind::Minus():
  227. return Trailing{.level = Additive, .is_binary = true};
  228. // Multiplicative operators.
  229. case TokenKind::Slash():
  230. return Trailing{.level = Multiplicative, .is_binary = true};
  231. case TokenKind::Percent():
  232. return Trailing{.level = Modulo, .is_binary = true};
  233. // `*` could be multiplication or pointer type formation.
  234. case TokenKind::Star():
  235. return infix ? Trailing{.level = Multiplicative, .is_binary = true}
  236. : Trailing{.level = TypePostfix, .is_binary = false};
  237. // Postfix operators.
  238. case TokenKind::MinusMinus():
  239. case TokenKind::PlusPlus():
  240. return Trailing{.level = NumericPostfix, .is_binary = false};
  241. // Prefix-only operators.
  242. case TokenKind::Tilde():
  243. case TokenKind::NotKeyword():
  244. break;
  245. // Symbolic tokens that might be operators eventually.
  246. case TokenKind::Backslash():
  247. case TokenKind::Caret():
  248. case TokenKind::CaretEqual():
  249. case TokenKind::Comma():
  250. case TokenKind::TildeEqual():
  251. case TokenKind::Exclaim():
  252. case TokenKind::LessGreater():
  253. case TokenKind::Question():
  254. case TokenKind::Colon():
  255. break;
  256. // Symbolic tokens that are intentionally not operators.
  257. case TokenKind::At():
  258. case TokenKind::LessMinus():
  259. case TokenKind::MinusGreater():
  260. case TokenKind::EqualGreater():
  261. case TokenKind::ColonEqual():
  262. case TokenKind::Period():
  263. case TokenKind::Semi():
  264. break;
  265. default:
  266. break;
  267. }
  268. return llvm::None;
  269. }
  270. auto PrecedenceGroup::GetPriority(PrecedenceGroup left, PrecedenceGroup right)
  271. -> OperatorPriority {
  272. static constexpr OperatorPriorityTable Lookup;
  273. return Lookup.table[left.level][right.level];
  274. }
  275. } // namespace Carbon