Skip to content

Commit

Permalink
Rename Prism::Node to Prism::ProgramNodeContainer (#411)
Browse files Browse the repository at this point in the history
1. Makes it clear that it represents the structure that holds the root
   node of Prism's AST. It's not a node itself.
2. Makes it easier to distinguish from Sorbet parser's Node class
  • Loading branch information
st0012 authored Feb 20, 2025
1 parent 95f75f5 commit 8bc62fc
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion main/pipeline/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ unique_ptr<parser::Node> runPrismParser(core::GlobalState &gs, core::FileRef fil
core::UnfreezeNameTable nameTableAccess(gs);

Prism::Parser parser{source};
Prism::Node root = parser.parse_root();
Prism::ProgramNodeContainer root = parser.parse_root();

if (stopAfterParser) {
return std::unique_ptr<parser::Node>();
Expand Down
4 changes: 2 additions & 2 deletions parser/prism/Parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pm_parser_t *Parser::getRawParserPointer() {
return &storage->parser;
}

Node Parser::parse_root() {
ProgramNodeContainer Parser::parse_root() {
pm_node_t *root = pm_parse(getRawParserPointer());
return Node{*this, root};
return ProgramNodeContainer{*this, root};
};

core::LocOffsets Parser::translateLocation(pm_location_t location) {
Expand Down
16 changes: 9 additions & 7 deletions parser/prism/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern "C" {

namespace sorbet::parser::Prism {

class Node;
class ProgramNodeContainer;

class ParseError {
public:
Expand Down Expand Up @@ -51,7 +51,7 @@ struct ParserStorage {
};

class Parser final {
friend class Node;
friend class ProgramNodeContainer;
friend struct NodeDeleter;

std::shared_ptr<ParserStorage> storage;
Expand All @@ -64,7 +64,7 @@ class Parser final {
Parser(const Parser &) = default;
Parser &operator=(const Parser &) = default;

Node parse_root();
ProgramNodeContainer parse_root();
core::LocOffsets translateLocation(pm_location_t location);
std::string_view resolveConstant(pm_constant_id_t constant_id);
std::string_view extractString(pm_string_t *string);
Expand All @@ -75,7 +75,9 @@ class Parser final {
pm_parser_t *getRawParserPointer();
};

class Node final {
// This class holds a pointer to the parser and a pointer to the root node of Prism's AST
// It's main purpose is to provide a way to clean up the AST nodes
class ProgramNodeContainer final {
struct NodeDeleter {
Parser parser;

Expand All @@ -90,10 +92,10 @@ class Node final {
Parser parser;
std::unique_ptr<pm_node_t, NodeDeleter> node;

Node(Parser parser, pm_node_t *node) : parser{parser}, node{node, NodeDeleter{parser}} {}
ProgramNodeContainer(Parser parser, pm_node_t *node) : parser{parser}, node{node, NodeDeleter{parser}} {}

Node(const Node &) = delete; // Copy constructor
Node &operator=(const Node &) = delete; // Copy assignment
ProgramNodeContainer(const ProgramNodeContainer &) = delete; // Copy constructor
ProgramNodeContainer &operator=(const ProgramNodeContainer &) = delete; // Copy assignment

pm_node_t *getRawNodePointer() const {
return node.get();
Expand Down
4 changes: 2 additions & 2 deletions parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1350,8 +1350,8 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
}
}

unique_ptr<parser::Node> Translator::translate(const Node &node) {
return translate(node.getRawNodePointer());
unique_ptr<parser::Node> Translator::translate(const ProgramNodeContainer &container) {
return translate(container.getRawNodePointer());
}

core::LocOffsets Translator::translateLoc(pm_location_t loc) {
Expand Down
2 changes: 1 addition & 1 deletion parser/prism/Translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Translator final {

// Translates the given AST from Prism's node types into the equivalent AST in Sorbet's legacy parser node types.
std::unique_ptr<parser::Node> translate(pm_node_t *node);
std::unique_ptr<parser::Node> translate(const Node &node);
std::unique_ptr<parser::Node> translate(const ProgramNodeContainer &container);

private:
// Private constructor used only for creating child translators
Expand Down

0 comments on commit 8bc62fc

Please sign in to comment.