context.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_PARSE_CONTEXT_H_
  6. #include <optional>
  7. #include "common/check.h"
  8. #include "common/vlog.h"
  9. #include "toolchain/lex/token_kind.h"
  10. #include "toolchain/lex/tokenized_buffer.h"
  11. #include "toolchain/parse/node_kind.h"
  12. #include "toolchain/parse/precedence.h"
  13. #include "toolchain/parse/state.h"
  14. #include "toolchain/parse/tree.h"
  15. namespace Carbon::Parse {
  16. // An amount by which to look ahead of the current token. Lookahead should be
  17. // used sparingly, and unbounded lookahead should be avoided.
  18. //
  19. // TODO: Decide whether we want to avoid lookahead altogether.
  20. enum class Lookahead : int32_t {
  21. CurrentToken = 0,
  22. NextToken = 1,
  23. };
  24. // Context and shared functionality for parser handlers. See state.def for state
  25. // documentation.
  26. class Context {
  27. public:
  28. // Possible operator fixities for errors.
  29. enum class OperatorFixity : int8_t { Prefix, Infix, Postfix };
  30. // Possible return values for FindListToken.
  31. enum class ListTokenKind : int8_t { Comma, Close, CommaClose };
  32. // Supported kinds for HandlePattern.
  33. enum class PatternKind : int8_t { ImplicitParam, Param, Variable, Let };
  34. // Supported return values for GetDeclContext.
  35. enum class DeclContext : int8_t {
  36. File, // Top-level context.
  37. Class,
  38. Interface,
  39. NamedConstraint,
  40. };
  41. // Used for restricting ordering of `package` and `import` directives.
  42. enum class PackagingState : int8_t {
  43. FileStart,
  44. InImports,
  45. AfterNonPackagingDecl,
  46. // A warning about `import` placement has been issued so we don't keep
  47. // issuing more (when `import` is repeated) until more non-`import`
  48. // declarations come up.
  49. InImportsAfterNonPackagingDecl,
  50. };
  51. // Used to track state on state_stack_.
  52. struct StateStackEntry : public Printable<StateStackEntry> {
  53. explicit StateStackEntry(State state, PrecedenceGroup ambient_precedence,
  54. PrecedenceGroup lhs_precedence,
  55. Lex::TokenIndex token, int32_t subtree_start)
  56. : state(state),
  57. ambient_precedence(ambient_precedence),
  58. lhs_precedence(lhs_precedence),
  59. token(token),
  60. subtree_start(subtree_start) {}
  61. // Prints state information for verbose output.
  62. auto Print(llvm::raw_ostream& output) const -> void {
  63. output << state << " @" << token << " subtree_start=" << subtree_start
  64. << " has_error=" << has_error;
  65. };
  66. // The state.
  67. State state;
  68. // Set to true to indicate that an error was found, and that contextual
  69. // error recovery may be needed.
  70. bool has_error = false;
  71. // Precedence information used by expression states in order to determine
  72. // operator precedence. The ambient_precedence deals with how the expression
  73. // should interact with outside context, while the lhs_precedence is
  74. // specific to the lhs of an operator expression.
  75. PrecedenceGroup ambient_precedence;
  76. PrecedenceGroup lhs_precedence;
  77. // A token providing context based on the subtree. This will typically be
  78. // the first token in the subtree, but may sometimes be a token within. It
  79. // will typically be used for the subtree's root node.
  80. Lex::TokenIndex token;
  81. // The offset within the Tree of the subtree start.
  82. int32_t subtree_start;
  83. };
  84. // We expect StateStackEntry to fit into 12 bytes:
  85. // state = 1 byte
  86. // has_error = 1 byte
  87. // ambient_precedence = 1 byte
  88. // lhs_precedence = 1 byte
  89. // token = 4 bytes
  90. // subtree_start = 4 bytes
  91. // If it becomes bigger, it'd be worth examining better packing; it should be
  92. // feasible to pack the 1-byte entries more tightly.
  93. static_assert(sizeof(StateStackEntry) == 12,
  94. "StateStackEntry has unexpected size!");
  95. explicit Context(Tree& tree, Lex::TokenizedBuffer& tokens,
  96. Lex::TokenDiagnosticEmitter& emitter,
  97. llvm::raw_ostream* vlog_stream);
  98. // Adds a node to the parse tree that has no children (a leaf).
  99. auto AddLeafNode(NodeKind kind, Lex::TokenIndex token, bool has_error = false)
  100. -> void;
  101. // Adds a node to the parse tree that has children.
  102. auto AddNode(NodeKind kind, Lex::TokenIndex token, int subtree_start,
  103. bool has_error) -> void;
  104. // Returns the current position and moves past it.
  105. auto Consume() -> Lex::TokenIndex { return *(position_++); }
  106. // Consumes the current token. Does not return it.
  107. auto ConsumeAndDiscard() -> void { ++position_; }
  108. // Parses an open paren token, possibly diagnosing if necessary. Creates a
  109. // leaf parse node of the specified start kind. The default_token is used when
  110. // there's no open paren. Returns the open paren token if it was found.
  111. auto ConsumeAndAddOpenParen(Lex::TokenIndex default_token,
  112. NodeKind start_kind)
  113. -> std::optional<Lex::TokenIndex>;
  114. // Parses a closing symbol corresponding to the opening symbol
  115. // `expected_open`, possibly skipping forward and diagnosing if necessary.
  116. // Creates a parse node of the specified close kind. If `expected_open` is not
  117. // an opening symbol, the parse node will be associated with `state.token`,
  118. // no input will be consumed, and no diagnostic will be emitted.
  119. auto ConsumeAndAddCloseSymbol(Lex::TokenIndex expected_open,
  120. StateStackEntry state, NodeKind close_kind)
  121. -> void;
  122. // Composes `ConsumeIf` and `AddLeafNode`, returning false when ConsumeIf
  123. // fails.
  124. auto ConsumeAndAddLeafNodeIf(Lex::TokenKind token_kind, NodeKind node_kind)
  125. -> bool;
  126. // Returns the current position and moves past it. Requires the token is the
  127. // expected kind.
  128. auto ConsumeChecked(Lex::TokenKind kind) -> Lex::TokenIndex;
  129. // If the current position's token matches this `Kind`, returns it and
  130. // advances to the next position. Otherwise returns an empty optional.
  131. auto ConsumeIf(Lex::TokenKind kind) -> std::optional<Lex::TokenIndex>;
  132. // Find the next token of any of the given kinds at the current bracketing
  133. // level.
  134. auto FindNextOf(std::initializer_list<Lex::TokenKind> desired_kinds)
  135. -> std::optional<Lex::TokenIndex>;
  136. // If the token is an opening symbol for a matched group, skips to the matched
  137. // closing symbol and returns true. Otherwise, returns false.
  138. auto SkipMatchingGroup() -> bool;
  139. // Skips forward to move past the likely end of a declaration or statement.
  140. //
  141. // Looks forward, skipping over any matched symbol groups, to find the next
  142. // position that is likely past the end of a declaration or statement. This
  143. // is a heuristic and should only be called when skipping past parse errors.
  144. //
  145. // The strategy for recognizing when we have likely passed the end of a
  146. // declaration or statement:
  147. // - If we get to a close curly brace, we likely ended the entire context.
  148. // - If we get to a semicolon, that should have ended the declaration or
  149. // statement.
  150. // - If we get to a new line from the `SkipRoot` token, but with the same or
  151. // less indentation, there is likely a missing semicolon. Continued
  152. // declarations or statements across multiple lines should be indented.
  153. //
  154. // Returns a semicolon token if one is the likely end.
  155. auto SkipPastLikelyEnd(Lex::TokenIndex skip_root)
  156. -> std::optional<Lex::TokenIndex>;
  157. // Skip forward to the given token. Verifies that it is actually forward.
  158. auto SkipTo(Lex::TokenIndex t) -> void;
  159. // Returns true if the current token satisfies the lexical validity rules
  160. // for an infix operator.
  161. auto IsLexicallyValidInfixOperator() -> bool;
  162. // Determines whether the current trailing operator should be treated as
  163. // infix.
  164. auto IsTrailingOperatorInfix() -> bool;
  165. // Diagnoses whether the current token is not written properly for the given
  166. // fixity. For example, because mandatory whitespace is missing. Regardless of
  167. // whether there's an error, it's expected that parsing continues.
  168. auto DiagnoseOperatorFixity(OperatorFixity fixity) -> void;
  169. // If the current position is a `,`, consumes it, adds the provided token, and
  170. // returns `Comma`. Returns `Close` if the current position is close_token
  171. // (for example, `)`). `CommaClose` indicates it found both (for example,
  172. // `,)`). Handles cases where invalid tokens are present by advancing the
  173. // position, and may emit errors. Pass already_has_error in order to suppress
  174. // duplicate errors.
  175. auto ConsumeListToken(NodeKind comma_kind, Lex::TokenKind close_kind,
  176. bool already_has_error) -> ListTokenKind;
  177. // Gets the kind of the next token to be consumed. If `lookahead` is
  178. // provided, it specifies which token to inspect.
  179. auto PositionKind(Lookahead lookahead = Lookahead::CurrentToken) const
  180. -> Lex::TokenKind {
  181. return tokens_->GetKind(position_[static_cast<int32_t>(lookahead)]);
  182. }
  183. // Tests whether the next token to be consumed is of the specified kind. If
  184. // `lookahead` is provided, it specifies which token to inspect.
  185. auto PositionIs(Lex::TokenKind kind,
  186. Lookahead lookahead = Lookahead::CurrentToken) const -> bool {
  187. return PositionKind(lookahead) == kind;
  188. }
  189. // Pops the state and keeps the value for inspection.
  190. auto PopState() -> StateStackEntry {
  191. auto back = state_stack_.pop_back_val();
  192. CARBON_VLOG() << "Pop " << state_stack_.size() << ": " << back << "\n";
  193. return back;
  194. }
  195. // Pops the state and discards it.
  196. auto PopAndDiscardState() -> void {
  197. CARBON_VLOG() << "PopAndDiscard " << state_stack_.size() - 1 << ": "
  198. << state_stack_.back() << "\n";
  199. state_stack_.pop_back();
  200. }
  201. // Pushes a new state with the current position for context.
  202. auto PushState(State state) -> void {
  203. PushState(StateStackEntry(state, PrecedenceGroup::ForTopLevelExpr(),
  204. PrecedenceGroup::ForTopLevelExpr(), *position_,
  205. tree_->size()));
  206. }
  207. // Pushes a new state with a specific token for context. Used when forming a
  208. // new subtree with a token that isn't the start of the subtree.
  209. auto PushState(State state, Lex::TokenIndex token) -> void {
  210. PushState(StateStackEntry(state, PrecedenceGroup::ForTopLevelExpr(),
  211. PrecedenceGroup::ForTopLevelExpr(), token,
  212. tree_->size()));
  213. }
  214. // Pushes a new expression state with specific precedence.
  215. auto PushStateForExpr(PrecedenceGroup ambient_precedence) -> void {
  216. PushState(StateStackEntry(State::Expr, ambient_precedence,
  217. PrecedenceGroup::ForTopLevelExpr(), *position_,
  218. tree_->size()));
  219. }
  220. // Pushes a new state with detailed precedence for expression resume states.
  221. auto PushStateForExprLoop(State state, PrecedenceGroup ambient_precedence,
  222. PrecedenceGroup lhs_precedence) -> void {
  223. PushState(StateStackEntry(state, ambient_precedence, lhs_precedence,
  224. *position_, tree_->size()));
  225. }
  226. // Pushes a constructed state onto the stack.
  227. auto PushState(StateStackEntry state) -> void {
  228. CARBON_VLOG() << "Push " << state_stack_.size() << ": " << state << "\n";
  229. state_stack_.push_back(state);
  230. CARBON_CHECK(state_stack_.size() < (1 << 20))
  231. << "Excessive stack size: likely infinite loop";
  232. }
  233. // Returns the current declaration context according to state_stack_.
  234. // This is expected to be called in cases which are close to a context.
  235. // Although it looks like it could be O(n) for state_stack_'s depth, valid
  236. // parses should only need to look down a couple steps.
  237. //
  238. // This currently assumes it's being called from within the declaration's
  239. // DeclScopeLoop.
  240. auto GetDeclContext() -> DeclContext;
  241. // Propagates an error up the state stack, to the parent state.
  242. auto ReturnErrorOnState() -> void { state_stack_.back().has_error = true; }
  243. // For HandlePattern, tries to consume a wrapping keyword.
  244. auto ConsumeIfPatternKeyword(Lex::TokenKind keyword_token,
  245. State keyword_state, int subtree_start) -> void;
  246. // Emits a diagnostic for a declaration missing a semi.
  247. auto EmitExpectedDeclSemi(Lex::TokenKind expected_kind) -> void;
  248. // Emits a diagnostic for a declaration missing a semi or definition.
  249. auto EmitExpectedDeclSemiOrDefinition(Lex::TokenKind expected_kind) -> void;
  250. // Handles error recovery in a declaration, particularly before any possible
  251. // definition has started (although one could be present). Recover to a
  252. // semicolon when it makes sense as a possible end, otherwise use the
  253. // introducer token for the error.
  254. auto RecoverFromDeclError(StateStackEntry state, NodeKind parse_node_kind,
  255. bool skip_past_likely_end) -> void;
  256. // Sets the package directive information. Called at most once.
  257. auto set_packaging_directive(Tree::PackagingNames packaging_names,
  258. Tree::ApiOrImpl api_or_impl) -> void {
  259. CARBON_CHECK(!tree_->packaging_directive_);
  260. tree_->packaging_directive_ = {.names = packaging_names,
  261. .api_or_impl = api_or_impl};
  262. }
  263. // Adds an import.
  264. auto AddImport(Tree::PackagingNames package) -> void {
  265. tree_->imports_.push_back(package);
  266. }
  267. // Prints information for a stack dump.
  268. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  269. auto tree() const -> const Tree& { return *tree_; }
  270. auto tokens() const -> const Lex::TokenizedBuffer& { return *tokens_; }
  271. auto emitter() -> Lex::TokenDiagnosticEmitter& { return *emitter_; }
  272. auto position() -> Lex::TokenIterator& { return position_; }
  273. auto position() const -> Lex::TokenIterator { return position_; }
  274. auto state_stack() -> llvm::SmallVector<StateStackEntry>& {
  275. return state_stack_;
  276. }
  277. auto state_stack() const -> const llvm::SmallVector<StateStackEntry>& {
  278. return state_stack_;
  279. }
  280. auto packaging_state() const -> PackagingState { return packaging_state_; }
  281. auto set_packaging_state(PackagingState packaging_state) -> void {
  282. packaging_state_ = packaging_state;
  283. }
  284. auto first_non_packaging_token() const -> Lex::TokenIndex {
  285. return first_non_packaging_token_;
  286. }
  287. auto set_first_non_packaging_token(Lex::TokenIndex token) -> void {
  288. CARBON_CHECK(!first_non_packaging_token_.is_valid());
  289. first_non_packaging_token_ = token;
  290. }
  291. private:
  292. // Prints a single token for a stack dump. Used by PrintForStackDump.
  293. auto PrintTokenForStackDump(llvm::raw_ostream& output,
  294. Lex::TokenIndex token) const -> void;
  295. Tree* tree_;
  296. Lex::TokenizedBuffer* tokens_;
  297. Lex::TokenDiagnosticEmitter* emitter_;
  298. // Whether to print verbose output.
  299. llvm::raw_ostream* vlog_stream_;
  300. // The current position within the token buffer.
  301. Lex::TokenIterator position_;
  302. // The FileEnd token.
  303. Lex::TokenIterator end_;
  304. llvm::SmallVector<StateStackEntry> state_stack_;
  305. // The current packaging state, whether `import`/`package` are allowed.
  306. PackagingState packaging_state_ = PackagingState::FileStart;
  307. // The first non-packaging token, starting as invalid. Used for packaging
  308. // state warnings.
  309. Lex::TokenIndex first_non_packaging_token_ = Lex::TokenIndex::Invalid;
  310. };
  311. // `clang-format` has a bug with spacing around `->` returns in macros. See
  312. // https://bugs.llvm.org/show_bug.cgi?id=48320 for details.
  313. #define CARBON_PARSE_STATE(Name) auto Handle##Name(Context& context)->void;
  314. #include "toolchain/parse/state.def"
  315. } // namespace Carbon::Parse
  316. #endif // CARBON_TOOLCHAIN_PARSE_CONTEXT_H_