parser_impl.h 5.3 KB

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