Skip to content

Commit 2613531

Browse files
tenderlovematzbot
authored andcommitted
[ruby/psych] Convert some of Parser#parse to Ruby
This commit just converts some of the parse method to Ruby ruby/psych@bca7d2c549
1 parent 12889fa commit 2613531

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

ext/psych/lib/psych/parser.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,18 @@ def initialize handler = Handler.new
4848
@handler = handler
4949
@external_encoding = ANY
5050
end
51+
52+
###
53+
# call-seq:
54+
# parser.parse(yaml)
55+
#
56+
# Parse the YAML document contained in +yaml+. Events will be called on
57+
# the handler set on the parser instance.
58+
#
59+
# See Psych::Parser and Psych::Parser#handler
60+
61+
def parse yaml, path = yaml.respond_to?(:path) ? yaml.path : "<unknown>"
62+
_native_parse @handler, yaml, path
63+
end
5164
end
5265
end

ext/psych/psych_parser.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -245,33 +245,15 @@ static VALUE protected_event_location(VALUE pointer)
245245
return rb_funcall3(args[0], id_event_location, 4, args + 1);
246246
}
247247

248-
/*
249-
* call-seq:
250-
* parser.parse(yaml)
251-
*
252-
* Parse the YAML document contained in +yaml+. Events will be called on
253-
* the handler set on the parser instance.
254-
*
255-
* See Psych::Parser and Psych::Parser#handler
256-
*/
257-
static VALUE parse(int argc, VALUE *argv, VALUE self)
248+
static VALUE parse(VALUE self, VALUE handler, VALUE yaml, VALUE path)
258249
{
259-
VALUE yaml, path;
260250
yaml_parser_t * parser;
261251
yaml_event_t event;
262252
int done = 0;
263253
int state = 0;
264254
int parser_encoding = YAML_ANY_ENCODING;
265255
int encoding = rb_utf8_encindex();
266256
rb_encoding * internal_enc = rb_default_internal_encoding();
267-
VALUE handler = rb_iv_get(self, "@handler");
268-
269-
if (rb_scan_args(argc, argv, "11", &yaml, &path) == 1) {
270-
if(rb_respond_to(yaml, id_path))
271-
path = rb_funcall(yaml, id_path, 0);
272-
else
273-
path = rb_str_new2("<unknown>");
274-
}
275257

276258
TypedData_Get_Struct(self, yaml_parser_t, &psych_parser_type, parser);
277259

@@ -562,7 +544,7 @@ void Init_psych_parser(void)
562544

563545
rb_require("psych/syntax_error");
564546

565-
rb_define_method(cPsychParser, "parse", parse, -1);
547+
rb_define_private_method(cPsychParser, "_native_parse", parse, 3);
566548
rb_define_method(cPsychParser, "mark", mark, 0);
567549

568550
id_read = rb_intern("read");

0 commit comments

Comments
 (0)