Skip to content

Commit

Permalink
Merge pull request #209 from Shopify/Alex/translate-class
Browse files Browse the repository at this point in the history
Implement Prism -> Sorbet translation for class declarations (again)
  • Loading branch information
amomchilov authored Aug 21, 2024
2 parents ca98459 + 7e372e6 commit 4ccaedc
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
39 changes: 36 additions & 3 deletions parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
return sendNode;
}
}
case PM_CLASS_NODE: { // Class declarations, not including singleton class declarations (`class <<`)
auto classNode = reinterpret_cast<pm_class_node *>(node);
pm_location_t *loc = &classNode->base.location;
pm_location_t *declLoc = &classNode->class_keyword_loc;

auto name = translate(reinterpret_cast<pm_node *>(classNode->constant_path));
std::unique_ptr<parser::Node> superclass;

if (classNode->superclass != nullptr) {
superclass = translate(reinterpret_cast<pm_node *>(classNode->superclass));
}

return make_unique<parser::Class>(parser.translateLocation(loc), parser.translateLocation(declLoc),
std::move(name), std::move(superclass), nullptr);
}
case PM_CONSTANT_PATH_NODE: {
// Part of a constant path, like the `A` in `A::B`. `B` is a `PM_CONSTANT_READ_NODE`
auto constantPathNode = reinterpret_cast<pm_constant_path_node *>(node);
Expand Down Expand Up @@ -404,6 +419,27 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {

return make_unique<parser::Return>(parser.translateLocation(loc), std::move(returnValues));
}
case PM_SELF_NODE: {
auto selfNode = reinterpret_cast<pm_singleton_class_node *>(node);
pm_location_t *loc = &selfNode->base.location;

return make_unique<parser::Self>(parser.translateLocation(loc));
}
case PM_SINGLETON_CLASS_NODE: {
auto classNode = reinterpret_cast<pm_singleton_class_node *>(node);
pm_location_t *loc = &classNode->base.location;
pm_location_t *declLoc = &classNode->class_keyword_loc;

auto expr = translate(classNode->expression);
unique_ptr<parser::Node> body;

if (classNode->body != nullptr) {
body = translate(classNode->body);
}

return make_unique<parser::SClass>(parser.translateLocation(loc), parser.translateLocation(declLoc),
std::move(expr), std::move(body));
}
case PM_STATEMENTS_NODE: {
auto inlineIfSingle = true;
return translateStatements(reinterpret_cast<pm_statements_node *>(node), inlineIfSingle);
Expand Down Expand Up @@ -491,7 +527,6 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
case PM_CAPTURE_PATTERN_NODE:
case PM_CASE_MATCH_NODE:
case PM_CASE_NODE:
case PM_CLASS_NODE:
case PM_CLASS_VARIABLE_AND_WRITE_NODE:
case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
case PM_CLASS_VARIABLE_OR_WRITE_NODE:
Expand Down Expand Up @@ -570,9 +605,7 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
case PM_RESCUE_MODIFIER_NODE:
case PM_RESCUE_NODE:
case PM_RETRY_NODE:
case PM_SELF_NODE:
case PM_SHAREABLE_CONSTANT_NODE:
case PM_SINGLETON_CLASS_NODE:
case PM_SOURCE_ENCODING_NODE:
case PM_SOURCE_FILE_NODE:
case PM_SOURCE_LINE_NODE:
Expand Down
35 changes: 35 additions & 0 deletions test/prism_regression/class.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Begin {
stmts = [
Class {
name = Const {
scope = NULL
name = <C <U Parent>>
}
superclass = NULL
body = NULL
}
Class {
name = Const {
scope = NULL
name = <C <U Child>>
}
superclass = Const {
scope = NULL
name = <C <U Parent>>
}
body = NULL
}
SClass {
expr = Self {
}
body = NULL
}
SClass {
expr = Const {
scope = NULL
name = <C <U Parent>>
}
body = NULL
}
]
}
9 changes: 9 additions & 0 deletions test/prism_regression/class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# typed: strict

class Parent; end
class Child < Parent; end

class << self; end

class << Parent; end
# ^^^^^^ error: `class << EXPRESSION` is only supported for `class << self`
2 changes: 2 additions & 0 deletions test/prism_regression/self.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Self {
}
3 changes: 3 additions & 0 deletions test/prism_regression/self.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# typed: strict

self

0 comments on commit 4ccaedc

Please sign in to comment.