Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Prism -> Sorbet translation for pinning patterns #276

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,9 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
case PM_ARRAY_PATTERN_NODE: // An array pattern such as the `[head, *tail]` in the `a in [head, *tail]`
case PM_FIND_PATTERN_NODE: // A find pattern such as the `[*, middle, *]` in the `a in [*, middle, *]`
case PM_HASH_PATTERN_NODE: // An hash pattern such as the `{ k: Integer }` in the `h in { k: Integer }`
case PM_IN_NODE: // An `in` pattern such as in a `case` statement, or as a standalone expression.
case PM_IN_NODE: // An `in` pattern such as in a `case` statement, or as a standalone expression.
case PM_PINNED_EXPRESSION_NODE: // A "pinned" expression, like `^(1 + 2)` in `in ^(1 + 2)`
case PM_PINNED_VARIABLE_NODE: // A "pinned" variable, like `^x` in `in ^x`
unreachable(
"These pattern-match related nodes are handled separately in `Translator::patternTranslate()`.");

Expand All @@ -1037,8 +1039,6 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
case PM_MULTI_TARGET_NODE:
case PM_NUMBERED_PARAMETERS_NODE:
case PM_NUMBERED_REFERENCE_READ_NODE:
case PM_PINNED_EXPRESSION_NODE:
case PM_PINNED_VARIABLE_NODE:
case PM_POST_EXECUTION_NODE:
case PM_PRE_EXECUTION_NODE:
case PM_RESCUE_NODE:
Expand Down Expand Up @@ -1211,6 +1211,25 @@ unique_ptr<parser::Node> Translator::patternTranslate(pm_node_t *node) {

return make_unique<MatchVar>(location, gs.enterNameUTF8(name));
}
case PM_PINNED_EXPRESSION_NODE: { // A "pinned" expression, like `^(1 + 2)` in `in ^(1 + 2)`
auto pinnedExprNode = reinterpret_cast<pm_pinned_expression_node *>(node);

auto expr = translate(pinnedExprNode->expression);

// Sorbet's parser always wraps the pinned expression in a `Begin` node.
NodeVec statements;
statements.emplace_back(move(expr));
auto beginNode = make_unique<parser::Begin>(translateLoc(pinnedExprNode->base.location), move(statements));

return make_unique<Pin>(location, move(beginNode));
}
case PM_PINNED_VARIABLE_NODE: { // A "pinned" variable, like `^x` in `in ^x`
auto pinnedVarNode = reinterpret_cast<pm_pinned_variable_node *>(node);

auto variable = translate(pinnedVarNode->variable);

return make_unique<Pin>(location, move(variable));
}
default: {
return translate(node);
}
Expand Down Expand Up @@ -1379,5 +1398,4 @@ unique_ptr<SorbetLHSNode> Translator::translateConst(PrismLhsNode *node) {
template <typename SorbetNode> unique_ptr<SorbetNode> Translator::translateSimpleKeyword(pm_node_t *node) {
return make_unique<SorbetNode>(translateLoc(node->location));
}

}; // namespace sorbet::parser::Prism
Loading
Loading