diff --git a/parser/prism/Translator.cc b/parser/prism/Translator.cc index 2f8dacfa8c3..f57b2d29fc6 100644 --- a/parser/prism/Translator.cc +++ b/parser/prism/Translator.cc @@ -130,14 +130,12 @@ std::unique_ptr Translator::translate(pm_node_t *node) { pm_location_t *declLoc = &classNode->class_keyword_loc; auto name = translate(reinterpret_cast(classNode->constant_path)); - std::unique_ptr superclass; + auto superclass = translate(classNode->superclass); - if (classNode->superclass != nullptr) { - superclass = translate(reinterpret_cast(classNode->superclass)); - } + auto body = translate(classNode->body); return make_unique(parser.translateLocation(loc), parser.translateLocation(declLoc), - std::move(name), std::move(superclass), nullptr); + std::move(name), std::move(superclass), std::move(body)); } case PM_CONSTANT_PATH_NODE: { // Part of a constant path, like the `A` in `A::B`. `B` is a `PM_CONSTANT_READ_NODE` @@ -150,7 +148,7 @@ std::unique_ptr Translator::translate(pm_node_t *node) { if (constantPathNode->parent) { // This constant reference is chained onto another constant reference. // E.g. if `node` is pointing to `B`, then then `A` is the `parent` in `A::B::C`. - parent = translate(reinterpret_cast(constantPathNode->parent)); + parent = translate(constantPathNode->parent); } else { // This is a fully qualified constant reference, like `::A`. pm_location_t *delimiterLoc = &constantPathNode->delimiter_loc; // The location of the `::` parent = make_unique(parser.translateLocation(delimiterLoc)); @@ -284,6 +282,17 @@ std::unique_ptr Translator::translate(pm_node_t *node) { return make_unique(parser.translateLocation(loc), gs.enterNameUTF8(name)); } + case PM_MODULE_NODE: { // Modules declarations, like `module A::B::C; ...; end` + auto moduleNode = reinterpret_cast(node); + pm_location_t *loc = &moduleNode->base.location; + pm_location_t *declLoc = &moduleNode->module_keyword_loc; + + auto name = translate(moduleNode->constant_path); + auto body = translate(moduleNode->body); + + return make_unique(parser.translateLocation(loc), parser.translateLocation(declLoc), + std::move(name), std::move(body)); + } case PM_NIL_NODE: { auto nilNode = reinterpret_cast(node); pm_location_t *loc = &nilNode->base.location; @@ -619,7 +628,6 @@ std::unique_ptr Translator::translate(pm_node_t *node) { case PM_MATCH_REQUIRED_NODE: case PM_MATCH_WRITE_NODE: case PM_MISSING_NODE: - case PM_MODULE_NODE: case PM_MULTI_TARGET_NODE: case PM_MULTI_WRITE_NODE: case PM_NEXT_NODE: diff --git a/test/prism_regression/class.parse-tree.exp b/test/prism_regression/class.parse-tree.exp index c68f855ba46..cf1f1c20bf3 100644 --- a/test/prism_regression/class.parse-tree.exp +++ b/test/prism_regression/class.parse-tree.exp @@ -6,7 +6,19 @@ Begin { name = > } superclass = NULL - body = NULL + body = Begin { + stmts = [ + Integer { + val = "1" + } + Integer { + val = "2" + } + Integer { + val = "3" + } + ] + } } Class { name = Const { @@ -22,7 +34,19 @@ Begin { SClass { expr = Self { } - body = NULL + body = Begin { + stmts = [ + Integer { + val = "4" + } + Integer { + val = "5" + } + Integer { + val = "6" + } + ] + } } SClass { expr = Const { diff --git a/test/prism_regression/class.rb b/test/prism_regression/class.rb index 11543340f63..7298967828e 100644 --- a/test/prism_regression/class.rb +++ b/test/prism_regression/class.rb @@ -1,9 +1,18 @@ # typed: strict -class Parent; end +class Parent + 1 + 2 + 3 +end + class Child < Parent; end -class << self; end +class << self + 4 + 5 + 6 +end class << Parent; end # ^^^^^^ error: `class << EXPRESSION` is only supported for `class << self` diff --git a/test/prism_regression/module.parse-tree.exp b/test/prism_regression/module.parse-tree.exp new file mode 100644 index 00000000000..15fdb646dba --- /dev/null +++ b/test/prism_regression/module.parse-tree.exp @@ -0,0 +1,19 @@ +Module { + name = Const { + scope = NULL + name = > + } + body = Begin { + stmts = [ + Integer { + val = "1" + } + Integer { + val = "2" + } + Integer { + val = "3" + } + ] + } +} diff --git a/test/prism_regression/module.rb b/test/prism_regression/module.rb new file mode 100644 index 00000000000..691c35c6d8e --- /dev/null +++ b/test/prism_regression/module.rb @@ -0,0 +1,7 @@ +# typed: strict + +module M + 1 + 2 + 3 +end