precedence.cpp 10 KB

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