parser_impl.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 PARSER_PARSER_IMPL_H_
  5. #define PARSER_PARSER_IMPL_H_
  6. #include "diagnostics/diagnostic_emitter.h"
  7. #include "lexer/token_kind.h"
  8. #include "lexer/tokenized_buffer.h"
  9. #include "llvm/ADT/Optional.h"
  10. #include "parser/parse_node_kind.h"
  11. #include "parser/parse_tree.h"
  12. #include "parser/precedence.h"
  13. namespace Carbon {
  14. class ParseTree::Parser {
  15. public:
  16. // Parses the tokens into a parse tree, emitting any errors encountered.
  17. //
  18. // This is the entry point to the parser implementation.
  19. static auto Parse(TokenizedBuffer& tokens, TokenDiagnosticEmitter& de)
  20. -> ParseTree;
  21. private:
  22. struct SubtreeStart;
  23. explicit Parser(ParseTree& tree_arg, TokenizedBuffer& tokens_arg,
  24. TokenDiagnosticEmitter& emitter);
  25. auto AtEndOfFile() -> bool {
  26. return tokens.GetKind(*position) == TokenKind::EndOfFile();
  27. }
  28. // Requires (and asserts) that the current position matches the provide
  29. // `Kind`. Returns the current token and advances to the next position.
  30. auto Consume(TokenKind kind) -> TokenizedBuffer::Token;
  31. // If the current position's token matches this `Kind`, returns it and
  32. // advances to the next position. Otherwise returns an empty optional.
  33. auto ConsumeIf(TokenKind kind) -> llvm::Optional<TokenizedBuffer::Token>;
  34. // Adds a node to the parse tree that is fully parsed, has no children
  35. // ("leaf"), and has a subsequent sibling.
  36. //
  37. // This sets up the next sibling of the node to be the next node in the parse
  38. // tree's preorder sequence.
  39. auto AddLeafNode(ParseNodeKind kind, TokenizedBuffer::Token token) -> Node;
  40. // Composes `consumeIf` and `addLeafNode`, propagating the failure case
  41. // through the optional.
  42. auto ConsumeAndAddLeafNodeIf(TokenKind t_kind, ParseNodeKind n_kind)
  43. -> llvm::Optional<Node>;
  44. // Marks the node `N` as having some parse error and that the tree contains
  45. // a node with a parse error.
  46. auto MarkNodeError(Node n) -> void;
  47. // Start parsing one (or more) subtrees of nodes.
  48. //
  49. // This returns a marker representing start position. Multiple nodes can be
  50. // added if they share a start position.
  51. auto StartSubtree() -> SubtreeStart;
  52. // Add a node to the parse tree that potentially has a subtree larger than
  53. // itself.
  54. //
  55. // Requires a start marker be passed to compute the size of the subtree rooted
  56. // at this node.
  57. auto AddNode(ParseNodeKind n_kind, TokenizedBuffer::Token t,
  58. SubtreeStart start, bool has_error = false) -> Node;
  59. // If the current token is an opening symbol for a matched group, skips
  60. // forward to one past the matched closing symbol and returns true. Otherwise,
  61. // returns false.
  62. auto SkipMatchingGroup() -> bool;
  63. // Skip forward to the token immediately after the given token.
  64. auto SkipTo(TokenizedBuffer::Token t) -> void;
  65. // Find the next token of the given kind at the current bracketing level.
  66. auto FindNext(TokenKind t) -> llvm::Optional<TokenizedBuffer::Token>;
  67. // Callback used if we find a semicolon when skipping to the end of a
  68. // declaration or statement.
  69. using SemiHandler = llvm::function_ref<
  70. auto(TokenizedBuffer::Token semi)->llvm::Optional<Node>>;
  71. // Skips forward to move past the likely end of a declaration or statement.
  72. //
  73. // Looks forward, skipping over any matched symbol groups, to find the next
  74. // position that is likely past the end of a declaration or statement. This
  75. // is a heuristic and should only be called when skipping past parse errors.
  76. //
  77. // The strategy for recognizing when we have likely passed the end of a
  78. // declaration or statement:
  79. // - If we get to close curly brace, we likely ended the entire context.
  80. // - If we get to a semicolon, that should have ended the declaration or
  81. // statement.
  82. // - If we get to a new line from the `SkipRoot` token, but with the same or
  83. // less indentation, there is likely a missing semicolon. Continued
  84. // declarations or statements across multiple lines should be indented.
  85. //
  86. // If we find a semicolon based on this skipping, we call `on_semi_` to try
  87. // to build a parse node to represent it, and will return that node.
  88. // Otherwise we will return an empty optional.
  89. auto SkipPastLikelyEnd(TokenizedBuffer::Token skip_root, SemiHandler on_semi)
  90. -> llvm::Optional<Node>;
  91. // Parses the signature of the function, consisting of a parameter list and an
  92. // optional return type. Returns the root node of the signature which must be
  93. // based on the open parenthesis of the parameter list.
  94. auto ParseFunctionSignature() -> Node;
  95. // Parses a block of code: `{ ... }`.
  96. //
  97. // These can form the definition for a function or be nested within a function
  98. // definition. These contain variable declarations and statements.
  99. auto ParseCodeBlock() -> Node;
  100. // Parses a function declaration with an optional definition. Returns the
  101. // function parse node which is based on the `fn` introducer keyword.
  102. auto ParseFunctionDeclaration() -> Node;
  103. // Parses and returns an empty declaration node from a single semicolon token.
  104. auto ParseEmptyDeclaration() -> Node;
  105. // Tries to parse a declaration. If a declaration, even an empty one after
  106. // skipping errors, can be parsed, it is returned. There may be parse errors
  107. // even when a node is returned.
  108. auto ParseDeclaration() -> llvm::Optional<Node>;
  109. // Parses a parenthesized expression.
  110. auto ParseParenExpression() -> llvm::Optional<Node>;
  111. // Parses a primary expression, which is either a terminal portion of an
  112. // expression tree, such as an identifier or literal, or a parenthesized
  113. // expression.
  114. auto ParsePrimaryExpression() -> llvm::Optional<Node>;
  115. // Parses a designator expression suffix starting with `.`.
  116. auto ParseDesignatorExpression(SubtreeStart start, bool has_errors)
  117. -> llvm::Optional<Node>;
  118. // Parses a call expression suffix starting with `(`.
  119. auto ParseCallExpression(SubtreeStart start, bool has_errors)
  120. -> llvm::Optional<Node>;
  121. // Parses a postfix expression, which is a primary expression followed by
  122. // zero or more of the following:
  123. //
  124. // - function applications
  125. // - array indexes (TODO)
  126. // - designators
  127. auto ParsePostfixExpression() -> llvm::Optional<Node>;
  128. // Parses an expression involving operators, in a context with the given
  129. // precedence.
  130. auto ParseOperatorExpression(llvm::Optional<PrecedenceGroup> precedence)
  131. -> llvm::Optional<Node>;
  132. // Parses an expression.
  133. auto ParseExpression() -> llvm::Optional<Node>;
  134. // Parses an expression statement: an expression followed by a semicolon.
  135. auto ParseExpressionStatement() -> llvm::Optional<Node>;
  136. ParseTree& tree;
  137. TokenizedBuffer& tokens;
  138. TokenDiagnosticEmitter& emitter;
  139. // The current position within the token buffer. Never equal to `end`.
  140. TokenizedBuffer::TokenIterator position;
  141. // The end position of the token buffer. There will always be an `EndOfFile`
  142. // token between `position` (inclusive) and `end` (exclusive).
  143. TokenizedBuffer::TokenIterator end;
  144. };
  145. } // namespace Carbon
  146. #endif // PARSER_PARSER_IMPL_H_