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

ruby-changes:47079

From: nobu <ko1@a...>
Date: Wed, 28 Jun 2017 14:38:33 +0900 (JST)
Subject: [ruby-changes:47079] nobu:r59194 (trunk): parse.y: add ellipsis

nobu	2017-06-28 14:38:27 +0900 (Wed, 28 Jun 2017)

  New Revision: 59194

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

  Log:
    parse.y: add ellipsis
    
    * parse.y (parser_yyerror): add ellipsis properly when error line
      is truncated.

  Modified files:
    trunk/parse.y
    trunk/test/ruby/test_parse.rb
Index: test/ruby/test_parse.rb
===================================================================
--- test/ruby/test_parse.rb	(revision 59193)
+++ test/ruby/test_parse.rb	(revision 59194)
@@ -1013,6 +1013,15 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L1013
     end
   end
 
+  def test_truncated_source_line
+    e = assert_raise_with_message(SyntaxError, /unexpected tIDENTIFIER/) do
+      eval("'0123456789012345678901234567890123456789' abcdefghijklmnopqrstuvwxyz0123456789 0123456789012345678901234567890123456789")
+    end
+    line = e.message.lines[1]
+    assert_operator(line, :start_with?, "...")
+    assert_operator(line, :end_with?, "...\n")
+  end
+
 =begin
   def test_past_scope_variable
     assert_warning(/past scope/) {catch {|tag| eval("BEGIN{throw tag}; tap {a = 1}; a")}}
Index: parse.y
===================================================================
--- parse.y	(revision 59193)
+++ parse.y	(revision 59194)
@@ -5036,42 +5036,38 @@ parser_yyerror(struct parser_params *par https://github.com/ruby/ruby/blob/trunk/parse.y#L5036
 #ifndef RIPPER
     const int max_line_margin = 30;
     const char *p, *pe;
-    const char *pre = "", *post = "";
+    const char *pre = "", *post = "", *pend;
     const char *code = "", *caret = "", *newline = "";
     const char *lim;
     char *buf;
     long len;
     int i;
 
-    p = parser->tokp;
+    p = lex_p;
     lim = p - lex_pbeg > max_line_margin ? p - max_line_margin : lex_pbeg;
-    while (lim < p) {
-	if (*(p-1) == '\n') break;
-	p--;
-    }
+    while ((lim < p) && (*(p-1) != '\n')) p--;
 
-    pe = lex_p;
-    lim = lex_pend - pe > max_line_margin ? pe + max_line_margin : lex_pend;
-    while (pe < lim) {
-	if (*pe == '\n') break;
-	pe++;
+    pend = lex_pend;
+    if (pend > lex_pbeg && pend[-1] == '\n') {
+	if (--pend > lex_pbeg && pend[-1] == '\r') --pend;
     }
+    pe = lex_p;
+    lim = pend - pe > max_line_margin ? pe + max_line_margin : pend;
+    while ((pe < lim) && (*pe != '\n')) pe++;
 
     len = pe - p;
     if (len > 4) {
 	char *p2;
 
-	if (len > max_line_margin * 2 + 10) {
-	    if (lex_p - p > max_line_margin) {
-		p = rb_enc_prev_char(p, lex_p - max_line_margin, pe, rb_enc_get(lex_lastline));
-		pre = "...";
-	    }
-	    if (pe - lex_p > max_line_margin) {
-		pe = rb_enc_prev_char(lex_p, lex_p + max_line_margin, pe, rb_enc_get(lex_lastline));
-		post = "...";
-	    }
-	    len = pe - p;
+	if (p > lex_pbeg) {
+	    p = rb_enc_prev_char(lex_pbeg, p, lex_p, rb_enc_get(lex_lastline));
+	    if (p > lex_pbeg) pre = "...";
+	}
+	if (pe < pend) {
+	    pe = rb_enc_prev_char(lex_p, pe, pend, rb_enc_get(lex_lastline));
+	    if (pe < pend) post = "...";
 	}
+	len = pe - p;
 	i = (int)(lex_p - p);
 	buf = ALLOCA_N(char, i+2);
 	code = p;

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

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