parser_impl.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. namespace Carbon {
  13. class ParseTree::Parser {
  14. public:
  15. // Parses the tokens into a parse tree, emitting any errors encountered.
  16. //
  17. // This is the entry point to the parser implementation.
  18. static auto Parse(TokenizedBuffer& tokens, TokenDiagnosticEmitter& de)
  19. -> ParseTree;
  20. private:
  21. struct SubtreeStart;
  22. explicit Parser(ParseTree& tree_arg, TokenizedBuffer& tokens_arg);
  23. auto AtEndOfFile() -> bool {
  24. return tokens.GetKind(*position) == TokenKind::EndOfFile();
  25. }
  26. // Requires (and asserts) that the current position matches the provide
  27. // `Kind`. Returns the current token and advances to the next position.
  28. auto Consume(TokenKind kind) -> TokenizedBuffer::Token;
  29. // If the current position's token matches this `Kind`, returns it and
  30. // advances to the next position. Otherwise returns an empty optional.
  31. auto ConsumeIf(TokenKind kind) -> llvm::Optional<TokenizedBuffer::Token>;
  32. // Adds a node to the parse tree that is fully parsed, has no children
  33. // ("leaf"), and has a subsequent sibling.
  34. //
  35. // This sets up the next sibling of the node to be the next node in the parse
  36. // tree's preorder sequence.
  37. auto AddLeafNode(ParseNodeKind kind, TokenizedBuffer::Token token) -> Node;
  38. // Composes `consumeIf` and `addLeafNode`, propagating the failure case
  39. // through the optional.
  40. auto ConsumeAndAddLeafNodeIf(TokenKind t_kind, ParseNodeKind n_kind)
  41. -> llvm::Optional<Node>;
  42. // Marks the node `N` as having some parse error and that the tree contains
  43. // a node with a parse error.
  44. auto MarkNodeError(Node n) -> void;
  45. // Start parsing one (or more) subtrees of nodes.
  46. //
  47. // This returns a marker representing start position. It will also enforce
  48. // that at least *some* node is added using this starting position. Multiple
  49. // nodes can be added if they share a start position though.
  50. auto StartSubtree() -> SubtreeStart;
  51. // Add a node to the parse tree that potentially has a subtree larger than
  52. // itself.
  53. //
  54. // Requires a start marker be passed to compute the size of the subtree rooted
  55. // at this node.
  56. auto AddNode(ParseNodeKind n_kind, TokenizedBuffer::Token t,
  57. SubtreeStart& start, bool has_error = false) -> Node;
  58. // If the current token is an opening symbol for a matched group, skips
  59. // forward to one past the matched closing symbol and returns true. Otherwise,
  60. // returns false.
  61. auto SkipMatchingGroup() -> bool;
  62. // Skip forward to the token immediately after the given token.
  63. auto SkipTo(TokenizedBuffer::Token t) -> void;
  64. // Skips forward to move past the likely end of a declaration.
  65. //
  66. // Looks forward, skipping over any matched symbol groups, to find the next
  67. // position that is likely past the end of a declaration. This is a heuristic
  68. // and should only be called when skipping past parse errors.
  69. //
  70. // The strategy for recognizing when we have likely passed the end of a
  71. // declaration:
  72. // - If we get to close curly brace, we likely ended the entire context of
  73. // declarations.
  74. // - If we get to a semicolon, that should have ended the declaration.
  75. // - If we get to a new line from the `SkipRoot` token, but with the same or
  76. // less indentation, there is likely a missing semicolon. Continued
  77. // declarations across multiple lines should be indented.
  78. //
  79. // If we find a semicolon based on this skipping, we try to build a parse node
  80. // to represent it and will return that node. Otherwise we will return an
  81. // empty optional. If `IsInsideDeclaration` is true (the default) we build a
  82. // node that marks the end of the declaration we are inside. Otherwise we
  83. // build an empty declaration node.
  84. auto SkipPastLikelyDeclarationEnd(TokenizedBuffer::Token skip_root,
  85. bool is_inside_declaration = true)
  86. -> llvm::Optional<Node>;
  87. // Parses the signature of the function, consisting of a parameter list and an
  88. // optional return type. Returns the root node of the signature which must be
  89. // based on the open parenthesis of the parameter list.
  90. auto ParseFunctionSignature() -> Node;
  91. // Parses a block of code: `{ ... }`.
  92. //
  93. // These can form the definition for a function or be nested within a function
  94. // definition. These contain variable declarations and statements.
  95. auto ParseCodeBlock() -> Node;
  96. // Parses a function declaration with an optional definition. Returns the
  97. // function parse node which is based on the `fn` introducer keyword.
  98. auto ParseFunctionDeclaration() -> Node;
  99. // Parses and returns an empty declaration node from a single semicolon token.
  100. auto ParseEmptyDeclaration() -> Node;
  101. // Tries to parse a declaration. If a declaration, even an empty one after
  102. // skipping errors, can be parsed, it is returned. There may be parse errors
  103. // even when a node is returned.
  104. auto ParseDeclaration() -> llvm::Optional<Node>;
  105. ParseTree& tree;
  106. TokenizedBuffer& tokens;
  107. // The current position within the token buffer. Never equal to `end`.
  108. TokenizedBuffer::TokenIterator position;
  109. // The end position of the token buffer. There will always be an `EndOfFile`
  110. // token between `position` (inclusive) and `end` (exclusive).
  111. TokenizedBuffer::TokenIterator end;
  112. };
  113. } // namespace Carbon
  114. #endif // PARSER_PARSER_IMPL_H_