[前][次][番号順一覧][スレッド一覧]

ruby-changes:44949

From: nobu <ko1@a...>
Date: Thu, 8 Dec 2016 09:45:18 +0900 (JST)
Subject: [ruby-changes:44949] nobu:r57022 (trunk): parse.y: ripper generic input

nobu	2016-12-08 09:45:13 +0900 (Thu, 08 Dec 2016)

  New Revision: 57022

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57022

  Log:
    parse.y: ripper generic input
    
    * parse.y (ripper_initialize): allow generic input as source, if
      it has #gets method.

  Modified files:
    trunk/parse.y
    trunk/test/ripper/test_ripper.rb
Index: parse.y
===================================================================
--- parse.y	(revision 57021)
+++ parse.y	(revision 57022)
@@ -776,7 +776,7 @@ static VALUE parser_heredoc_dedent(struc https://github.com/ruby/ruby/blob/trunk/parse.y#L776
 # define rb_warning3L(l,fmt,a,b,c)   WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
 # define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
 #ifdef RIPPER
-static ID id_warn, id_warning;
+static ID id_warn, id_warning, id_gets;
 # define WARN_S_L(s,l) STR_NEW(s,l)
 # define WARN_S(s) STR_NEW2(s)
 # define WARN_I(i) INT2NUM(i)
@@ -11318,6 +11318,18 @@ ripper_compile_error(struct parser_param https://github.com/ruby/ruby/blob/trunk/parse.y#L11318
 static VALUE
 ripper_lex_get_generic(struct parser_params *parser, VALUE src)
 {
+    VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
+    if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
+	rb_raise(rb_eTypeError,
+		 "gets returned %"PRIsVALUE" (expected String or nil)",
+		 rb_obj_class(line));
+    }
+    return line;
+}
+
+static VALUE
+ripper_lex_io_get(struct parser_params *parser, VALUE src)
+{
     return rb_io_gets(src);
 }
 
@@ -11352,6 +11364,9 @@ ripper_initialize(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/parse.y#L11364
     TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
     rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
     if (RB_TYPE_P(src, T_FILE)) {
+        lex_gets = ripper_lex_io_get;
+    }
+    else if (rb_respond_to(src, id_gets)) {
         lex_gets = ripper_lex_get_generic;
     }
     else {
@@ -11519,6 +11534,7 @@ Init_ripper(void) https://github.com/ruby/ruby/blob/trunk/parse.y#L11534
     ripper_init_eventids2();
     id_warn = rb_intern_const("warn");
     id_warning = rb_intern_const("warning");
+    id_gets = rb_intern_const("gets");
 
     InitVM(ripper);
 }
Index: test/ripper/test_ripper.rb
===================================================================
--- test/ripper/test_ripper.rb	(revision 57021)
+++ test/ripper/test_ripper.rb	(revision 57022)
@@ -117,4 +117,22 @@ end https://github.com/ruby/ruby/blob/trunk/test/ripper/test_ripper.rb#L117
 
     assert_nothing_raised { Ripper.lex src }
   end
+
+  class TestInput < self
+    Input = Struct.new(:lines) do
+      def gets
+        lines.shift
+      end
+    end
+
+    def setup
+      @ripper = Ripper.new(Input.new(["1 + 1"]))
+    end
+
+    def test_invalid_gets
+      ripper = assert_nothing_raised {Ripper.new(Input.new([0]))}
+      assert_raise(TypeError) {ripper.parse}
+    end
+  end
+
 end if ripper_test

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]