mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-01 16:35:53 +01:00
Working on toolchain semantics: - SemanticsIR is set up as a container for the semantic tree. - SemanticsIRFactory builds the tree, with separate transformations for each ParseNodeKind. - ParseSubtreeConsumer is a helper for transforming a ParseTree::Node's children, managing size/nodes to prevent errors. - The nodes subdirectory contains SemanticIR nodes. - MetaNode is used to represent nodes which have "sub-classes": Statements, Declarations, and Expressions. - MetaNodeBlock is used to represent nodes which exist together in a block with name lookup: Statements and Declarations (not Expressions). This is traversing children first in order to address the RPO format of ParseTree. This means that when lists are formed, they're reversed to be in code-order (`FixReverseOrdering`). This is still very much incomplete -- the main intent at present is to demonstrate structure.
68 lines
2.5 KiB
C++
68 lines
2.5 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef CARBON_TOOLCHAIN_SEMANTICS_PARSE_SUBTREE_CONSUMER_H_
|
|
#define CARBON_TOOLCHAIN_SEMANTICS_PARSE_SUBTREE_CONSUMER_H_
|
|
|
|
#include "toolchain/parser/parse_tree.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Consumes a subtree from the parser, returning only its direct children.
|
|
//
|
|
// This traverses in reverse postorder because the parent of a subtree needs to
|
|
// be seen before its children.
|
|
class ParseSubtreeConsumer {
|
|
public:
|
|
using ParseTreeIterator = std::reverse_iterator<ParseTree::PostorderIterator>;
|
|
|
|
// Returns a subtree consumer for a particular node in the tree.
|
|
static auto ForParent(const ParseTree& parse_tree,
|
|
ParseTree::Node parent_node) -> ParseSubtreeConsumer;
|
|
|
|
// Returns a subtree consumer for the root of the tree.
|
|
static auto ForTree(const ParseTree& parse_tree) -> ParseSubtreeConsumer;
|
|
|
|
// Prevent copies because we require completion of parsing in the destructor.
|
|
ParseSubtreeConsumer(const ParseSubtreeConsumer&) = delete;
|
|
auto operator=(const ParseSubtreeConsumer&) -> ParseSubtreeConsumer& = delete;
|
|
|
|
~ParseSubtreeConsumer();
|
|
|
|
// Returns the next node.
|
|
// CHECK-fails on unexpected states.
|
|
[[nodiscard]] auto RequireConsume() -> ParseTree::Node;
|
|
|
|
// Requires the next node be of the given kind, and returns it.
|
|
// CHECK-fails on unexpected states.
|
|
[[nodiscard]] auto RequireConsume(ParseNodeKind node_kind) -> ParseTree::Node;
|
|
|
|
// Returns the next node if one exists.
|
|
[[nodiscard]] auto TryConsume() -> llvm::Optional<ParseTree::Node>;
|
|
|
|
// Returns the next node if it's of the given kind.
|
|
[[nodiscard]] auto TryConsume(ParseNodeKind node_kind)
|
|
-> llvm::Optional<ParseTree::Node>;
|
|
|
|
// Returns true if there are no more nodes to consume.
|
|
auto is_done() -> bool { return cursor_ == subtree_end_; }
|
|
|
|
private:
|
|
// Constructs for a subtree.
|
|
ParseSubtreeConsumer(const ParseTree& parse_tree, ParseTreeIterator cursor,
|
|
ParseTreeIterator subtree_end)
|
|
: parse_tree_(&parse_tree), cursor_(cursor), subtree_end_(subtree_end) {}
|
|
|
|
// Advances to the next sibling, returning the current node.
|
|
auto GetNodeAndAdvance() -> ParseTree::Node;
|
|
|
|
const ParseTree* parse_tree_;
|
|
ParseTreeIterator cursor_;
|
|
ParseTreeIterator subtree_end_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_TOOLCHAIN_SEMANTICS_PARSE_SUBTREE_CONSUMER_H_
|