summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-29 12:21:52 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-29 12:21:52 +0000
commitba02d4b03bf536b24786f5b5982a08b44cf9d427 (patch)
treecd5e67c0be07e282b14e76987f0fbc0633ca4396
parent2110125b43fca50e532c27a009362009f8522663 (diff)
merge revision(s) 53398: [Backport #11871]
* parse.y (parser_here_document): update indent for each line in indented here document with single-quotes. [ruby-core:72479] [Bug #11871] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_3@54398 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--parse.y53
-rw-r--r--test/ruby/test_syntax.rb116
-rw-r--r--version.h2
4 files changed, 100 insertions, 77 deletions
diff --git a/ChangeLog b/ChangeLog
index 638c2662b6..f16dfb0ed0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Tue Mar 29 21:18:09 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * parse.y (parser_here_document): update indent for each line in
+ indented here document with single-quotes.
+ [ruby-core:72479] [Bug #11871]
+
Tue Mar 29 21:03:40 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* symbol.h (is_attrset_id): ASET is an attrset ID. fix
diff --git a/parse.y b/parse.y
index 1d10f167c7..68b3219a9f 100644
--- a/parse.y
+++ b/parse.y
@@ -6210,6 +6210,32 @@ simple_re_meta(int c)
}
static int
+parser_update_heredoc_indent(struct parser_params *parser, int c)
+{
+ if (heredoc_line_indent == -1) {
+ if (c == '\n') heredoc_line_indent = 0;
+ }
+ else {
+ if (c == ' ') {
+ heredoc_line_indent++;
+ return TRUE;
+ }
+ else if (c == '\t') {
+ int w = (heredoc_line_indent / TAB_WIDTH) + 1;
+ heredoc_line_indent = w * TAB_WIDTH;
+ return TRUE;
+ }
+ else if (c != '\n') {
+ if (heredoc_indent > heredoc_line_indent) {
+ heredoc_indent = heredoc_line_indent;
+ }
+ heredoc_line_indent = -1;
+ }
+ }
+ return FALSE;
+}
+
+static int
parser_tokadd_string(struct parser_params *parser,
int func, int term, int paren, long *nest,
rb_encoding **encp)
@@ -6239,24 +6265,7 @@ parser_tokadd_string(struct parser_params *parser,
while ((c = nextc()) != -1) {
if (heredoc_indent > 0) {
- if (heredoc_line_indent == -1) {
- if (c == '\n') heredoc_line_indent = 0;
- }
- else {
- if (c == ' ') {
- heredoc_line_indent++;
- }
- else if (c == '\t') {
- int w = (heredoc_line_indent / TAB_WIDTH) + 1;
- heredoc_line_indent = w * TAB_WIDTH;
- }
- else if (c != '\n') {
- if (heredoc_indent > heredoc_line_indent) {
- heredoc_indent = heredoc_line_indent;
- }
- heredoc_line_indent = -1;
- }
- }
+ parser_update_heredoc_indent(parser, c);
}
if (paren && c == paren) {
@@ -6876,6 +6885,14 @@ parser_here_document(struct parser_params *parser, NODE *here)
--pend;
}
}
+
+ if (heredoc_indent > 0) {
+ long i = 0;
+ while (p + i < pend && parser_update_heredoc_indent(parser, p[i]))
+ i++;
+ heredoc_line_indent = 0;
+ }
+
if (str)
rb_str_cat(str, p, pend - p);
else
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index 63997f423f..1daf7b2d91 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -492,92 +492,92 @@ e"
assert_equal(expected, actual, "#{Bug7559}: ")
end
+ def assert_dedented_heredoc(expect, result, mesg = "")
+ %w[eos "eos" 'eos'].each do |eos|
+ assert_equal(eval("<<-#{eos}\n#{expect}eos\n"),
+ eval("<<~#{eos}\n#{result}eos\n"),
+ message(mesg) {"with #{eos}"})
+ end
+ end
+
def test_dedented_heredoc_without_indentation
- assert_equal(" y\nz\n", <<~eos)
- y
-z
- eos
+ result = " y\n" \
+ "z\n"
+ expect = result
+ assert_dedented_heredoc(expect, result)
end
def test_dedented_heredoc_with_indentation
- assert_equal(" a\nb\n", <<~eos)
- a
- b
- eos
+ result = " a\n" \
+ " b\n"
+ expect = " a\n" \
+ "b\n"
+ assert_dedented_heredoc(expect, result)
end
def test_dedented_heredoc_with_blank_less_indented_line
# the blank line has two leading spaces
- result = eval("<<~eos\n" \
- " a\n" \
- " \n" \
- " b\n" \
- " eos\n")
- assert_equal("a\n\nb\n", result)
+ result = " a\n" \
+ " \n" \
+ " b\n"
+ expect = "a\n" \
+ "\n" \
+ "b\n"
+ assert_dedented_heredoc(expect, result)
end
def test_dedented_heredoc_with_blank_less_indented_line_escaped
- result = eval("<<~eos\n" \
- " a\n" \
- "\\ \\ \n" \
- " b\n" \
- " eos\n")
- assert_equal(" a\n \n b\n", result)
+ result = " a\n" \
+ "\\ \\ \n" \
+ " b\n"
+ expect = result
+ assert_dedented_heredoc(expect, result)
end
def test_dedented_heredoc_with_blank_more_indented_line
# the blank line has six leading spaces
- result = eval("<<~eos\n" \
- " a\n" \
- " \n" \
- " b\n" \
- " eos\n")
- assert_equal("a\n \nb\n", result)
+ result = " a\n" \
+ " \n" \
+ " b\n"
+ expect = "a\n" \
+ " \n" \
+ "b\n"
+ assert_dedented_heredoc(expect, result)
end
def test_dedented_heredoc_with_blank_more_indented_line_escaped
- result = eval("<<~eos\n" \
- " a\n" \
- "\\ \\ \\ \\ \\ \\ \n" \
- " b\n" \
- " eos\n")
- assert_equal(" a\n \n b\n", result)
+ result = " a\n" \
+ "\\ \\ \\ \\ \\ \\ \n" \
+ " b\n"
+ expect = result
+ assert_dedented_heredoc(expect, result)
end
def test_dedented_heredoc_with_empty_line
-result = eval("<<~eos\n" \
- " This would contain specially formatted text.\n" \
- "\n" \
- " That might span many lines\n" \
- " eos\n")
- assert_equal(<<-eos, result)
-This would contain specially formatted text.
-
-That might span many lines
- eos
+ result = " This would contain specially formatted text.\n" \
+ "\n" \
+ " That might span many lines\n"
+ expect = 'This would contain specially formatted text.'"\n" \
+ ''"\n" \
+ 'That might span many lines'"\n"
+ assert_dedented_heredoc(expect, result)
end
def test_dedented_heredoc_with_interpolated_expression
- result = eval(" <<~eos\n" \
- " #{1}a\n" \
- " zy\n" \
- " eos\n")
- assert_equal(<<-eos, result)
- #{1}a
-zy
- eos
+ result = ' #{1}a'"\n" \
+ " zy\n"
+ expect = ' #{1}a'"\n" \
+ "zy\n"
+ assert_dedented_heredoc(expect, result)
end
def test_dedented_heredoc_with_interpolated_string
w = ""
- result = eval("<<~eos\n" \
- " \#{w} a\n" \
- " zy\n" \
- " eos\n")
- assert_equal(<<-eos, result)
-#{w} a
- zy
- eos
+ result = " \#{mesg} a\n" \
+ " zy\n"
+ expect = '#{mesg} a'"\n" \
+ ' zy'"\n"
+ assert_dedented_heredoc(expect, result)
end
def test_lineno_after_heredoc
diff --git a/version.h b/version.h
index 4e4dfc17c0..5072e45be6 100644
--- a/version.h
+++ b/version.h
@@ -1,6 +1,6 @@
#define RUBY_VERSION "2.3.0"
#define RUBY_RELEASE_DATE "2016-03-29"
-#define RUBY_PATCHLEVEL 47
+#define RUBY_PATCHLEVEL 48
#define RUBY_RELEASE_YEAR 2016
#define RUBY_RELEASE_MONTH 3