Skip to content

Commit

Permalink
WIP Implement translation for it keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Sep 3, 2024
1 parent 82f377a commit 4282cc6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
21 changes: 18 additions & 3 deletions parser/prism/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,35 @@ namespace sorbet::parser::Prism {
class Node;

class Parser final {
// The version of Ruby syntax that we're parsing with Prism. This determines what syntax is supported or not.
static constexpr std::string_view ParsedRubyVersion = "3.3.0";

friend class Node;
friend struct NodeDeleter;

std::shared_ptr<pm_parser_t> parser;
// This makes the `Parser` bigger (and thus the `Node`s that reference it).
// Perhaps we need to rework this into a `ParserHandle` (just 1 `shared_ptr`), and another class which stores the
// the `pm_parser_t` and `pm_options_t` (at that point, those can just be stored in-line).
// std::unique_ptr<pm_options_t, decltype(&pm_options_free)> options;

public:
Parser(std::string_view source_code)
: parser(new pm_parser_t, [](auto p) {
pm_parser_free(p);
delete (p);
}) {
const pm_options_t *options = nullptr;
})/*, options(new pm_options_t, [](auto options) {
pm_options_free(options);
delete options;
})*/ {
// `pm_parser_init()` does an immutable borrow of `options`.

// FIXME: This is a memory leak. This is just a temporary workaround until we figure out our shit on line 24-27.
pm_options_t *temp_options = new pm_options_t{};
pm_options_version_set(temp_options, ParsedRubyVersion.data(), ParsedRubyVersion.size());

pm_parser_init(parser.get(), reinterpret_cast<const uint8_t *>(source_code.data()), source_code.size(),
options);
temp_options);
}

Parser(const Parser &) = default;
Expand Down
10 changes: 8 additions & 2 deletions parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,14 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
case PM_INTERPOLATED_SYMBOL_NODE:
case PM_INTERPOLATED_X_STRING_NODE:
case PM_IT_LOCAL_VARIABLE_READ_NODE:
case PM_IT_PARAMETERS_NODE:
case PM_IT_LOCAL_VARIABLE_READ_NODE: {
// See Prism::Parser::ParsedRubyVersion
unreachable("The `it` keyword was introduced in Ruby 3.4, which isn't supported by Sorbet yet.");
}
case PM_IT_PARAMETERS_NODE: {
// See Prism::Parser::ParsedRubyVersion
unreachable("The `it` keyword was introduced in Ruby 3.4, which isn't supported by Sorbet yet.");
}
case PM_LAMBDA_NODE:
case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
Expand Down
49 changes: 49 additions & 0 deletions test/prism_regression/keyword_it.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Begin {
stmts = [
Block {
send = Send {
receiver = Const {
scope = NULL
name = <C <U Proc>>
}
method = <U new>
args = [
]
}
args = NULL
body = Begin {
stmts = [
Assign {
lhs = LVarLhs {
name = <U it>
}
rhs = Integer {
val = "123"
}
}
LVar {
name = <U it>
}
]
}
}
Block {
send = Send {
receiver = Const {
scope = NULL
name = <C <U Proc>>
}
method = <U new>
args = [
]
}
args = NULL
body = Send {
receiver = NULL
method = <U it>
args = [
]
}
}
]
}
14 changes: 14 additions & 0 deletions test/prism_regression/keyword_it.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# typed: false

# Introduced in Ruby 3.4, which isn't supported by Sorbet yet.
# https://bugs.ruby-lang.org/issues/18980

# For now, this is just treated like a local variable read or method call.
Proc.new do
it = 123 # Prior to Ruby 3.4, this would be a local variable write.
it # In Ruby 3.4, this is a local variable write.
end

Proc.new do
it # Prior to Ruby 3.4, this would be a method call
end

0 comments on commit 4282cc6

Please sign in to comment.