Skip to content

Commit

Permalink
Merge pull request #929 from llby/support-multiline-string-in-quoted-…
Browse files Browse the repository at this point in the history
…string

[#772]  Support multiline string in " quoted string
  • Loading branch information
repeatedly committed May 10, 2016
2 parents 5bad4e0 + 9fd7e8d commit 0d05606
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/fluent/config/basic_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def initialize(strscan)
SPACING = /(?:[ \t\r\n]|\z|\#.*?(?:\z|[\r\n]))+/
ZERO_OR_MORE_SPACING = /(?:[ \t\r\n]|\z|\#.*?(?:\z|[\r\n]))*/
SPACING_WITHOUT_COMMENT = /(?:[ \t\r\n]|\z)+/
LINE_END_WITHOUT_SPACING_AND_COMMENT = /(?:\z|[\r\n])/

module ClassMethods
def symbol(string)
Expand Down Expand Up @@ -71,6 +72,10 @@ def prev_match
@ss[0]
end

def check(pattern)
@ss.check(pattern)
end

def line_end
skip(LINE_END)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/fluent/config/literal_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def scan_double_quoted_string
while true
if skip(/\"/)
return string.join
elsif check(/[^"]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/)
if s = check(/[^\\]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/)
string << s
end
skip(/[^"]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/)
elsif s = scan(/\\./)
string << eval_escape_char(s[1,1])
elsif skip(/\#\{/)
Expand Down
35 changes: 35 additions & 0 deletions test/config/test_config_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "fluent/config/basic_parser"
require "fluent/config/literal_parser"
require "fluent/config/v1_parser"
require 'fluent/config/parser'

module Fluent::Config
module V1TestHelper
Expand Down Expand Up @@ -129,6 +130,7 @@ def parse_text(text)
test "requires escaping double quote" do
assert_text_parsed_as(e('ROOT', '', {"k1" => '"'}), ' k1 "\\""')
assert_parse_error(' k1 """')
assert_parse_error(' k1 ""\'')
end

test "removes backslash in front of a normal character" do
Expand All @@ -138,6 +140,39 @@ def parse_text(text)
test "accepts escape characters" do
assert_text_parsed_as(e('ROOT', '', {"k1" => "\n"}), ' k1 "\\n"')
end

test "support multiline string" do
assert_text_parsed_as(e('ROOT', '',
{"k1" => %[line1
line2]
}),
%[k1 "line1
line2"]
)
assert_text_parsed_as(e('ROOT', '',
{"k1" => %[line1 line2]
}),
%[k1 "line1\\
line2"]
)
assert_text_parsed_as(e('ROOT', '',
{"k1" => %[line1
line2
line3]
}),
%[k1 "line1
line2
line3"]
)
assert_text_parsed_as(e('ROOT', '',
{"k1" => %[line1
line2 line3]
}),
%[k1 "line1
line2\\
line3"]
)
end
end

sub_test_case 'single quoted string' do
Expand Down
2 changes: 2 additions & 0 deletions test/config/test_literal_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def test_falseX
test('"\\0"') { assert_parse_error('"\\0"') } # unknown escaped character
test('"\\1"') { assert_parse_error('"\\1"') } # unknown escaped character
test('"t') { assert_parse_error('"t') } # non-terminated quoted character
test("\"t\nt\"") { assert_text_parsed_as("t\nt", "\"t\nt\"" ) } # multiline string
test("\"t\\\nt\"") { assert_text_parsed_as("tt", "\"t\\\nt\"" ) } # multiline string
test('t"') { assert_text_parsed_as('t"', 't"') }
test('"."') { assert_text_parsed_as('.', '"."') }
test('"*"') { assert_text_parsed_as('*', '"*"') }
Expand Down

0 comments on commit 0d05606

Please sign in to comment.