ruby-changes:18565
From: tenderlove <ko1@a...>
Date: Tue, 18 Jan 2011 04:47:55 +0900 (JST)
Subject: [ruby-changes:18565] Ruby:r30588 (trunk): * ext/psych/lib/psych/parser.rb (Mark): Adding a class to wrap
tenderlove 2011-01-18 04:44:31 +0900 (Tue, 18 Jan 2011) New Revision: 30588 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30588 Log: * ext/psych/lib/psych/parser.rb (Mark): Adding a class to wrap marker information * ext/psych/parser.c (mark): Add a method to return the mark object for the parser * test/psych/test_parser.rb: tests for the Mark class. Modified files: trunk/ChangeLog trunk/ext/psych/lib/psych/parser.rb trunk/ext/psych/parser.c trunk/test/psych/test_parser.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 30587) +++ ChangeLog (revision 30588) @@ -1,3 +1,13 @@ +Tue Jan 18 04:42:44 2011 Aaron Patterson <aaron@t...> + + * ext/psych/lib/psych/parser.rb (Mark): Adding a class to wrap + marker information + + * ext/psych/parser.c (mark): Add a method to return the mark object + for the parser + + * test/psych/test_parser.rb: tests for the Mark class. + Tue Jan 18 02:46:55 2011 Aaron Patterson <aaron@t...> * ext/psych/lib/psych/visitors/json_tree.rb (visit_String): JSON Index: ext/psych/lib/psych/parser.rb =================================================================== --- ext/psych/lib/psych/parser.rb (revision 30587) +++ ext/psych/lib/psych/parser.rb (revision 30588) @@ -30,6 +30,9 @@ # construct an AST of the parsed YAML document. class Parser + class Mark < Struct.new(:index, :line, :column) + end + # The handler on which events will be called attr_accessor :handler Index: ext/psych/parser.c =================================================================== --- ext/psych/parser.c (revision 30587) +++ ext/psych/parser.c (revision 30588) @@ -307,6 +307,28 @@ return encoding; } +/* + * call-seq: + * parser.mark # => #<Psych::Parser::Mark> + * + * Returns a Psych::Parser::Mark object that contains line, column, and index + * information. + */ +static VALUE mark(VALUE self) +{ + VALUE mark_klass; + VALUE args[3]; + yaml_parser_t * parser; + + Data_Get_Struct(self, yaml_parser_t, parser); + mark_klass = rb_const_get_at(cPsychParser, rb_intern("Mark")); + args[0] = INT2NUM(parser->mark.index); + args[1] = INT2NUM(parser->mark.line); + args[2] = INT2NUM(parser->mark.column); + + return rb_class_new_instance(3, args, mark_klass); +} + void Init_psych_parser() { #if 0 @@ -331,6 +353,7 @@ ePsychSyntaxError = rb_define_class_under(mPsych, "SyntaxError", rb_eSyntaxError); rb_define_method(cPsychParser, "parse", parse, 1); + rb_define_method(cPsychParser, "mark", mark, 0); rb_define_method(cPsychParser, "external_encoding=", set_external_encoding, 1); id_read = rb_intern("read"); Index: test/psych/test_parser.rb =================================================================== --- test/psych/test_parser.rb (revision 30587) +++ test/psych/test_parser.rb (revision 30588) @@ -5,9 +5,12 @@ module Psych class TestParser < TestCase class EventCatcher < Handler - attr_reader :calls + attr_accessor :parser + attr_reader :calls, :marks def initialize - @calls = [] + @parser = nil + @calls = [] + @marks = [] end (Handler.instance_methods(true) - @@ -15,6 +18,7 @@ class_eval %{ def #{m} *args super + @marks << @parser.mark if @parser @calls << [:#{m}, args] end } @@ -23,9 +27,59 @@ def setup super - @parser = Psych::Parser.new EventCatcher.new + @handler = EventCatcher.new + @parser = Psych::Parser.new @handler + @handler.parser = @parser end + def test_line_numbers + assert_equal 0, @parser.mark.line + @parser.parse "---\n- hello\n- world" + line_calls = @handler.marks.map(&:line).zip(@handler.calls.map(&:first)) + assert_equal [[0, :start_stream], + [0, :start_document], + [1, :start_sequence], + [2, :scalar], + [3, :scalar], + [3, :end_sequence], + [3, :end_document], + [3, :end_stream]], line_calls + + assert_equal 3, @parser.mark.line + end + + def test_column_numbers + assert_equal 0, @parser.mark.column + @parser.parse "---\n- hello\n- world" + col_calls = @handler.marks.map(&:column).zip(@handler.calls.map(&:first)) + assert_equal [[0, :start_stream], + [3, :start_document], + [1, :start_sequence], + [0, :scalar], + [0, :scalar], + [0, :end_sequence], + [0, :end_document], + [0, :end_stream]], col_calls + + assert_equal 0, @parser.mark.column + end + + def test_index_numbers + assert_equal 0, @parser.mark.index + @parser.parse "---\n- hello\n- world" + idx_calls = @handler.marks.map(&:index).zip(@handler.calls.map(&:first)) + assert_equal [[0, :start_stream], + [3, :start_document], + [5, :start_sequence], + [12, :scalar], + [19, :scalar], + [19, :end_sequence], + [19, :end_document], + [19, :end_stream]], idx_calls + + assert_equal 19, @parser.mark.index + end + def test_set_encoding_twice @parser.external_encoding = Psych::Parser::UTF16LE -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/