Initial support for expression parsing. (#455)

This commit is contained in:
Richard Smith
2021-04-14 16:02:20 -07:00
committed by GitHub
parent 7a6c46dc1a
commit 82d5a25caa
4 changed files with 412 additions and 74 deletions
+53 -19
View File
@@ -58,9 +58,8 @@ class ParseTree::Parser {
// Start parsing one (or more) subtrees of nodes.
//
// This returns a marker representing start position. It will also enforce
// that at least *some* node is added using this starting position. Multiple
// nodes can be added if they share a start position though.
// This returns a marker representing start position. Multiple nodes can be
// added if they share a start position.
auto StartSubtree() -> SubtreeStart;
// Add a node to the parse tree that potentially has a subtree larger than
@@ -69,7 +68,7 @@ class ParseTree::Parser {
// Requires a start marker be passed to compute the size of the subtree rooted
// at this node.
auto AddNode(ParseNodeKind n_kind, TokenizedBuffer::Token t,
SubtreeStart& start, bool has_error = false) -> Node;
SubtreeStart start, bool has_error = false) -> Node;
// If the current token is an opening symbol for a matched group, skips
// forward to one past the matched closing symbol and returns true. Otherwise,
@@ -79,28 +78,33 @@ class ParseTree::Parser {
// Skip forward to the token immediately after the given token.
auto SkipTo(TokenizedBuffer::Token t) -> void;
// Skips forward to move past the likely end of a declaration.
// Find the next token of the given kind at the current bracketing level.
auto FindNext(TokenKind t) -> llvm::Optional<TokenizedBuffer::Token>;
// Callback used if we find a semicolon when skipping to the end of a
// declaration or statement.
using SemiHandler = llvm::function_ref<
auto(TokenizedBuffer::Token semi)->llvm::Optional<Node>>;
// Skips forward to move past the likely end of a declaration or statement.
//
// Looks forward, skipping over any matched symbol groups, to find the next
// position that is likely past the end of a declaration. This is a heuristic
// and should only be called when skipping past parse errors.
// position that is likely past the end of a declaration or statement. This
// is a heuristic and should only be called when skipping past parse errors.
//
// The strategy for recognizing when we have likely passed the end of a
// declaration:
// - If we get to close curly brace, we likely ended the entire context of
// declarations.
// - If we get to a semicolon, that should have ended the declaration.
// declaration or statement:
// - If we get to close curly brace, we likely ended the entire context.
// - If we get to a semicolon, that should have ended the declaration or
// statement.
// - If we get to a new line from the `SkipRoot` token, but with the same or
// less indentation, there is likely a missing semicolon. Continued
// declarations across multiple lines should be indented.
// declarations or statements across multiple lines should be indented.
//
// If we find a semicolon based on this skipping, we try to build a parse node
// to represent it and will return that node. Otherwise we will return an
// empty optional. If `IsInsideDeclaration` is true (the default) we build a
// node that marks the end of the declaration we are inside. Otherwise we
// build an empty declaration node.
auto SkipPastLikelyDeclarationEnd(TokenizedBuffer::Token skip_root,
bool is_inside_declaration = true)
// If we find a semicolon based on this skipping, we call `on_semi_` to try
// to build a parse node to represent it, and will return that node.
// Otherwise we will return an empty optional.
auto SkipPastLikelyEnd(TokenizedBuffer::Token skip_root, SemiHandler on_semi)
-> llvm::Optional<Node>;
// Parses the signature of the function, consisting of a parameter list and an
@@ -126,6 +130,36 @@ class ParseTree::Parser {
// even when a node is returned.
auto ParseDeclaration() -> llvm::Optional<Node>;
// Parses a parenthesized expression.
auto ParseParenExpression() -> llvm::Optional<Node>;
// Parses a primary expression, which is either a terminal portion of an
// expression tree, such as an identifier or literal, or a parenthesized
// expression.
auto ParsePrimaryExpression() -> llvm::Optional<Node>;
// Parses a designator expression suffix starting with `.`.
auto ParseDesignatorExpression(SubtreeStart start, bool has_errors)
-> llvm::Optional<Node>;
// Parses a call expression suffix starting with `(`.
auto ParseCallExpression(SubtreeStart start, bool has_errors)
-> llvm::Optional<Node>;
// Parses a postfix expression, which is a primary expression followed by
// zero or more of the following:
//
// - function applications
// - array indexes (TODO)
// - designators
auto ParsePostfixExpression() -> llvm::Optional<Node>;
// Parses an expression.
auto ParseExpression() -> llvm::Optional<Node>;
// Parses an expression statement: an expression followed by a semicolon.
auto ParseExpressionStatement() -> llvm::Optional<Node>;
ParseTree& tree;
TokenizedBuffer& tokens;
TokenDiagnosticEmitter& emitter;