Skip to content

Commit bca7d2c

Browse files
committed
Convert some of Parser#parse to Ruby
This commit just converts some of the parse method to Ruby
1 parent 251289b commit bca7d2c

2 files changed

Lines changed: 15 additions & 20 deletions

File tree

ext/psych/psych_parser.c

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

244-
/*
245-
* call-seq:
246-
* parser.parse(yaml)
247-
*
248-
* Parse the YAML document contained in +yaml+. Events will be called on
249-
* the handler set on the parser instance.
250-
*
251-
* See Psych::Parser and Psych::Parser#handler
252-
*/
253-
static VALUE parse(int argc, VALUE *argv, VALUE self)
244+
static VALUE parse(VALUE self, VALUE handler, VALUE yaml, VALUE path)
254245
{
255-
VALUE yaml, path;
256246
yaml_parser_t * parser;
257247
yaml_event_t event;
258248
int done = 0;
259249
int state = 0;
260250
int parser_encoding = YAML_ANY_ENCODING;
261251
int encoding = rb_utf8_encindex();
262252
rb_encoding * internal_enc = rb_default_internal_encoding();
263-
VALUE handler = rb_iv_get(self, "@handler");
264-
265-
if (rb_scan_args(argc, argv, "11", &yaml, &path) == 1) {
266-
if(rb_respond_to(yaml, id_path))
267-
path = rb_funcall(yaml, id_path, 0);
268-
else
269-
path = rb_str_new2("<unknown>");
270-
}
271253

272254
TypedData_Get_Struct(self, yaml_parser_t, &psych_parser_type, parser);
273255

@@ -558,7 +540,7 @@ void Init_psych_parser(void)
558540

559541
rb_require("psych/syntax_error");
560542

561-
rb_define_method(cPsychParser, "parse", parse, -1);
543+
rb_define_private_method(cPsychParser, "_native_parse", parse, 3);
562544
rb_define_method(cPsychParser, "mark", mark, 0);
563545

564546
id_read = rb_intern("read");

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

0 commit comments

Comments
 (0)