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

ruby-changes:50579

From: nobu <ko1@a...>
Date: Sun, 11 Mar 2018 21:19:15 +0900 (JST)
Subject: [ruby-changes:50579] nobu:r62724 (trunk): parse.y: fix interpolated string literal dedent

nobu	2018-03-11 21:19:08 +0900 (Sun, 11 Mar 2018)

  New Revision: 62724

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

  Log:
    parse.y: fix interpolated string literal dedent
    
    * parse.y (heredoc_dedent): fix interpolated string literal dedent,
      remove indentations from only nodes with the newline flag.
      [ruby-core:85983] [Bug #14584]
    
    * parse.y (here_document): set the newline flag on literal string
      nodes starting at the beginning of line.

  Modified files:
    trunk/parse.y
    trunk/test/ruby/test_syntax.rb
Index: parse.y
===================================================================
--- parse.y	(revision 62723)
+++ parse.y	(revision 62724)
@@ -6017,7 +6017,6 @@ static NODE * https://github.com/ruby/ruby/blob/trunk/parse.y#L6017
 heredoc_dedent(struct parser_params *p, NODE *root)
 {
     NODE *node, *str_node, *prev_node;
-    int bol = TRUE;
     int indent = p->heredoc_indent;
     VALUE prev_lit = 0;
 
@@ -6030,8 +6029,9 @@ heredoc_dedent(struct parser_params *p, https://github.com/ruby/ruby/blob/trunk/parse.y#L6029
 
     while (str_node) {
 	VALUE lit = str_node->nd_lit;
-	if (bol) dedent_string(lit, indent);
-	bol = TRUE;
+	if (str_node->flags & NODE_FL_NEWLINE) {
+	    dedent_string(lit, indent);
+	}
 	if (!prev_lit) {
 	    prev_lit = lit;
 	}
@@ -6055,7 +6055,6 @@ heredoc_dedent(struct parser_params *p, https://github.com/ruby/ruby/blob/trunk/parse.y#L6055
 	    if ((str_node = node->nd_head) != 0) {
 		enum node_type type = nd_type(str_node);
 		if (type == NODE_STR || type == NODE_DSTR) break;
-		bol = FALSE;
 		prev_lit = 0;
 		str_node = 0;
 	    }
@@ -6204,6 +6203,7 @@ here_document(struct parser_params *p, r https://github.com/ruby/ruby/blob/trunk/parse.y#L6203
     long len;
     VALUE str = 0;
     rb_encoding *enc = p->enc;
+    int bol;
 
     eos = RSTRING_PTR(here->term);
     len = RSTRING_LEN(here->term) - 2; /* here->term includes term_len and func */
@@ -6242,7 +6242,8 @@ here_document(struct parser_params *p, r https://github.com/ruby/ruby/blob/trunk/parse.y#L6242
 	p->lex.strterm = 0;
 	return 0;
     }
-    if (was_bol(p) && whole_match_p(p, eos, len, indent)) {
+    bol = was_bol(p);
+    if (bol && whole_match_p(p, eos, len, indent)) {
 	dispatch_heredoc_end(p);
 	heredoc_restore(p, &p->lex.strterm->u.heredoc);
 	p->lex.strterm = 0;
@@ -6317,6 +6318,9 @@ here_document(struct parser_params *p, r https://github.com/ruby/ruby/blob/trunk/parse.y#L6318
 	      flush_str:
 		set_yylval_str(str);
 		add_mark_object(p, str);
+#ifndef RIPPER
+		if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
+#endif
 		flush_string_content(p, enc);
 		return tSTRING_CONTENT;
 	    }
@@ -6339,6 +6343,9 @@ here_document(struct parser_params *p, r https://github.com/ruby/ruby/blob/trunk/parse.y#L6343
     p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
     set_yylval_str(str);
     add_mark_object(p, str);
+#ifndef RIPPER
+    if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
+#endif
     return tSTRING_CONTENT;
 }
 
Index: test/ruby/test_syntax.rb
===================================================================
--- test/ruby/test_syntax.rb	(revision 62723)
+++ test/ruby/test_syntax.rb	(revision 62724)
@@ -667,6 +667,12 @@ e" https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L667
     assert_dedented_heredoc(expected, result)
   end
 
+  def test_dedented_heredoc_expr_string
+    result = '  one#{"  two  "}'"\n"
+    expected = 'one#{"  two  "}'"\n"
+    assert_dedented_heredoc(expected, result)
+  end
+
   def test_lineno_after_heredoc
     bug7559 = '[ruby-dev:46737]'
     expected, _, actual = __LINE__, <<eom, __LINE__

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

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