Skip to content

Commit b0df446

Browse files
committed
Expose parse_type_params to Ruby
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
1 parent a19d0dd commit b0df446

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

ext/rbs_extension/main.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,58 @@ static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos
272272
return result;
273273
}
274274

275+
276+
struct parse_type_params_arg {
277+
VALUE buffer;
278+
rb_encoding *encoding;
279+
rbs_parser_t *parser;
280+
VALUE module_type_params;
281+
};
282+
283+
static VALUE parse_type_params_try(VALUE a) {
284+
struct parse_type_params_arg *arg = (struct parse_type_params_arg *)a;
285+
rbs_parser_t *parser = arg->parser;
286+
287+
if (parser->next_token.type == pEOF) {
288+
return Qnil;
289+
}
290+
291+
rbs_node_list_t *params = NULL;
292+
rbs_parse_type_params(parser, arg->module_type_params, &params);
293+
294+
raise_error_if_any(parser, arg->buffer);
295+
296+
rbs_translation_context_t ctx = rbs_translation_context_create(
297+
&parser->constant_pool,
298+
arg->buffer,
299+
arg->encoding
300+
);
301+
302+
return rbs_node_list_to_ruby_array(ctx, params);
303+
}
304+
305+
306+
static VALUE rbsparser_parse_type_params(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE module_type_params) {
307+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
308+
StringValue(string);
309+
rb_encoding *encoding = rb_enc_get(string);
310+
311+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
312+
struct parse_type_params_arg arg = {
313+
.buffer = buffer,
314+
.encoding = encoding,
315+
.parser = parser,
316+
.module_type_params = module_type_params
317+
};
318+
319+
VALUE result = rb_ensure(parse_type_params_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
320+
321+
RB_GC_GUARD(string);
322+
323+
return result;
324+
}
325+
326+
275327
static VALUE parse_inline_leading_annotation_try(VALUE a) {
276328
struct parse_type_arg *arg = (struct parse_type_arg *) a;
277329
rbs_parser_t *parser = arg->parser;
@@ -391,6 +443,7 @@ void rbs__init_parser(void) {
391443
rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
392444
rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
393445
rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
446+
rb_define_singleton_method(RBS_Parser, "_parse_type_params", rbsparser_parse_type_params, 4);
394447
rb_define_singleton_method(RBS_Parser, "_parse_inline_leading_annotation", rbsparser_parse_inline_leading_annotation, 4);
395448
rb_define_singleton_method(RBS_Parser, "_parse_inline_trailing_annotation", rbsparser_parse_inline_trailing_annotation, 4);
396449
rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);

lib/rbs/parser_aux.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def self.parse_signature(source)
3535
[buf, dirs, decls]
3636
end
3737

38+
def self.parse_type_params(source, module_type_params: true)
39+
buf = buffer(source)
40+
_parse_type_params(buf, 0, buf.last_position, module_type_params)
41+
end
42+
3843
def self.magic_comment(buf)
3944
start_pos = 0
4045

0 commit comments

Comments
 (0)