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 it keyword #228

Merged
merged 3 commits into from
Sep 16, 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
10 changes: 5 additions & 5 deletions parser/prism/Parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
namespace sorbet::parser::Prism {

pm_parser_t *Parser::get_raw_parser_pointer() {
return parser.get();
return &storage->parser;
}

Node Parser::parse_root() {
pm_node_t *root = pm_parse(parser.get());
pm_node_t *root = pm_parse(get_raw_parser_pointer());
return Node{*this, root};
};

core::LocOffsets Parser::translateLocation(pm_location_t *location) {
uint32_t start = static_cast<uint32_t>(location->start - parser->start);
uint32_t end = static_cast<uint32_t>(location->end - parser->start);
uint32_t start = static_cast<uint32_t>(location->start - storage->parser.start);
uint32_t end = static_cast<uint32_t>(location->end - storage->parser.start);

return core::LocOffsets{start, end};
}

std::string_view Parser::resolveConstant(pm_constant_id_t constant_id) {
pm_constant_t *constant = pm_constant_pool_id_to_constant(&parser->constant_pool, constant_id);
pm_constant_t *constant = pm_constant_pool_id_to_constant(&storage->parser.constant_pool, constant_id);

return std::string_view(reinterpret_cast<const char *>(constant->start), constant->length);
}
Expand Down
36 changes: 26 additions & 10 deletions parser/prism/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,38 @@ namespace sorbet::parser::Prism {

class Node;

// A backing implemenation detail of `Parser`, which stores a Prism parser and its options in a single allocation.
struct ParserStorage {
// 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";
pm_parser_t parser;
pm_options_t options;

ParserStorage(std::string_view source_code) : parser{}, options{} {
pm_options_version_set(&options, ParsedRubyVersion.data(), ParsedRubyVersion.size());

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

~ParserStorage() {
pm_parser_free(&parser);
pm_options_free(&options);
}

ParserStorage(const ParserStorage &) = delete;
ParserStorage &operator=(const ParserStorage &) = delete;
ParserStorage(ParserStorage &&) = delete;
ParserStorage &operator=(ParserStorage &&) = delete;
};

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

std::shared_ptr<pm_parser_t> parser;
std::shared_ptr<ParserStorage> storage;

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;
pm_parser_init(parser.get(), reinterpret_cast<const uint8_t *>(source_code.data()), source_code.size(),
options);
}
Parser(std::string_view source_code) : storage(std::make_shared<ParserStorage>(source_code)) {}

Parser(const Parser &) = default;
Parser &operator=(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 @@ -862,8 +862,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::ParserStorage::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::ParserStorage::ParsedRubyVersion
unreachable("The `it` keyword was introduced in Ruby 3.4, which isn't supported by Sorbet yet.");
}
case PM_LAMBDA_NODE:
case PM_MATCH_LAST_LINE_NODE:
case PM_MATCH_PREDICATE_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 = Send {
receiver = NULL
method = <U it>
args = [
]
}
}
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>
}
]
}
}
]
}
15 changes: 15 additions & 0 deletions test/prism_regression/keyword_it.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# typed: false

# The `it` keyword was introduced in Ruby 3.4, which isn't supported by Sorbet yet.
# https://bugs.ruby-lang.org/issues/18980
#
# For now, we'll just treat it like a local variable read or method call.

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

Proc.new do
it = 123 # Prior to Ruby 3.4, this would just be a local variable write
it # ... and this is a local variable read.
end