@@ -452,6 +452,53 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
452452 return results ;
453453}
454454
455+ VALUE rbsparser_parse_signatures (VALUE self , VALUE files ) {
456+ Check_Type (files , T_HASH );
457+
458+ VALUE result_hash = rb_hash_new ();
459+ VALUE keys = rb_funcall (files , rb_intern ("keys" ), 0 );
460+
461+ for (long i = 0 ; i < RARRAY_LEN (keys ); i ++ ) {
462+ VALUE file_path = rb_ary_entry (keys , i );
463+ VALUE buffer = rb_hash_aref (files , file_path );
464+
465+ VALUE string = rb_funcall (buffer , rb_intern ("content" ), 0 );
466+ StringValue (string );
467+ rb_encoding * encoding = rb_enc_get (string );
468+
469+ // Prepare parser
470+ rbs_parser_t * parser = alloc_parser_from_buffer (buffer , 0 , RSTRING_LEN (string ));
471+ struct parse_signature_arg arg = {
472+ .buffer = buffer ,
473+ .encoding = encoding ,
474+ .parser = parser ,
475+ .require_eof = false
476+ };
477+
478+ VALUE entry = rb_hash_new ();
479+
480+ int state = 0 ;
481+ VALUE ast = rb_protect (parse_signature_try , (VALUE ) & arg , & state );
482+
483+ if (state == 0 ) {
484+ // Success: store AST
485+ rb_hash_aset (entry , ID2SYM (rb_intern ("ast" )), ast );
486+ } else {
487+ // Error: store exception
488+ VALUE err = rb_errinfo ();
489+ rb_set_errinfo (Qnil ); // Clear the error info
490+ rb_hash_aset (entry , ID2SYM (rb_intern ("error" )), err );
491+ }
492+
493+ rb_hash_aset (result_hash , file_path , entry );
494+
495+ ensure_free_parser ((VALUE ) parser );
496+ RB_GC_GUARD (string );
497+ }
498+
499+ return result_hash ;
500+ }
501+
455502void rbs__init_parser (void ) {
456503 RBS_Parser = rb_define_class_under (RBS , "Parser" , rb_cObject );
457504 rb_gc_register_mark_object (RBS_Parser );
@@ -471,6 +518,7 @@ void rbs__init_parser(void) {
471518 rb_define_singleton_method (RBS_Parser , "_parse_inline_leading_annotation" , rbsparser_parse_inline_leading_annotation , 4 );
472519 rb_define_singleton_method (RBS_Parser , "_parse_inline_trailing_annotation" , rbsparser_parse_inline_trailing_annotation , 4 );
473520 rb_define_singleton_method (RBS_Parser , "_lex" , rbsparser_lex , 2 );
521+ rb_define_singleton_method (RBS_Parser , "_parse_signatures" , rbsparser_parse_signatures , 1 );
474522}
475523
476524static void Deinit_rbs_extension (ruby_vm_t * _ ) {
0 commit comments