Skip to content

Commit

Permalink
comment out works only after /[ \t]+#/ in nonquoted string
Browse files Browse the repository at this point in the history
  • Loading branch information
sonots committed Oct 4, 2014
1 parent 8787252 commit 4f17c1b
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 29 deletions.
12 changes: 12 additions & 0 deletions example/v1_literal_example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@
key9 "#{test}" # replaced by eval('test')
key10 "\\[(?<time>[^\\]]*)\\] (?<message>.*)" # \[(?<time>[^\]]*\] (?<message>.*)
</section2>
<section3>
key1 text # text
key2 \ # \ (1 char)
key3 \\ # \\ (2 char)
key4 \t # \t (2 char)
key5 \[ # \[ (2 char)
key6 \\[ # \\[ (3 char)
key7 #t # #t (2 char)
key8 \#{test} # \#{test} (8 char)
key9 #{test} # #{test} (7 char)
key10 \[(?<time>[^\]]*)\] (?<message>.*) # \[(?<time>[^\]]*\] (?<message>.*)
</section3>
5 changes: 5 additions & 0 deletions lib/fluent/config/basic_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def initialize(strscan)

LINE_END = /(?:[ \t]*(?:\#.*)?(?:\z|[\r\n]))+/
SPACING = /(?:[ \t\r\n]|\z|\#.*?(?:\z|[\r\n]))+/
SPACING_WITHOUT_COMMENT = /(?:[ \t\r\n]|\z)+/

module ClassMethods
def symbol(string)
Expand Down Expand Up @@ -78,6 +79,10 @@ def spacing
skip(SPACING)
end

def spacing_without_comment
skip(SPACING_WITHOUT_COMMENT)
end

def parse_error!(message)
raise ConfigParseError, "#{message} at #{error_sample}"
end
Expand Down
6 changes: 1 addition & 5 deletions lib/fluent/config/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,7 @@ def to_s(nest = 0)
out << "#{indent}<#{@name} #{@arg}>\n"
end
each_pair { |k, v|
if @v1_config
out << "#{nindent}#{k} #{Element.unescape_parameter(v)}\n"
else
out << "#{nindent}#{k} #{v}\n"
end
out << "#{nindent}#{k} #{v}\n"
}
@elements.each { |e|
out << e.to_s(nest + 1)
Expand Down
6 changes: 4 additions & 2 deletions lib/fluent/config/literal_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def initialize(strscan, eval_context)
end

def parse_literal(string_boundary_charset = LINE_END)
spacing
spacing_without_comment

value = if skip(/\[/)
scan_json(true)
Expand Down Expand Up @@ -113,7 +113,9 @@ def scan_nonquoted_string(boundary_charset = LINE_END)

string = []
while true
if s = scan(charset)
if s = scan(/\#/)
string << '#'
elsif s = scan(charset)
string << s
else
break
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/config/v1_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def parse_element(root_element, elem_name, attrs = {}, elems = [])

else
k = scan_string(SPACING)
spacing
spacing_without_comment
if prev_match.include?("\n") # support 'tag_mapped' like "without value" configuration
attrs[k] = ""
else
Expand Down
33 changes: 28 additions & 5 deletions spec/config/config_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,31 @@ def e(name, arg='', attrs={}, elements=[])
expect("k1 a b c").to be_parsed_as(e('ROOT', '', {"k1" => "a b c"}))
end

it "ignores comments after value" do
expect(" k1 a#comment").to be_parsed_as(e('ROOT', '', {"k1" => "a"}))
context 'non-quoted string' do
it "remains text starting with '#'" do
expect(" k1 #not_comment").to be_parsed_as(e('ROOT', '', {"k1" => "#not_comment"}))
end

it "remains text just after '#'" do
expect(" k1 a#not_comment").to be_parsed_as(e('ROOT', '', {"k1" => "a#not_comment"}))
end

it "remove text after ` #` (comment)" do
expect(" k1 a #comment").to be_parsed_as(e('ROOT', '', {"k1" => "a"}))
end

it "does not require escaping backslash" do
expect(" k1 \\\\").to be_parsed_as(e('ROOT', '', {"k1" => "\\\\"}))
expect(" k1 \\").to be_parsed_as(e('ROOT', '', {"k1" => "\\"}))
end

it "remains backslash in front of a normal character" do
expect(" k1 \\[").to be_parsed_as(e('ROOT', '', {"k1" => '\['}))
end

it "does not accept escape characters" do
expect(" k1 \\n").to be_parsed_as(e('ROOT', '', {"k1" => '\n'}))
end
end

context 'double quoted string' do
Expand Down Expand Up @@ -348,10 +371,10 @@ def prepare_config
# TODO: Add uri based include spec
end

describe 'unescape parameter' do
describe '#to_s' do
it 'parses dumpped configuration' do
original = %q!a\\\\\n\r\f\b\\'\\"z!
expected = %!a\\\n\r\f\b'"z!
original = %q!a\\\n\r\f\b'"z!
expected = %q!a\\\n\r\f\b'"z!

conf = parse_text(%[k1 #{original}])
expect(conf['k1']).to eq(expected) # escape check
Expand Down
42 changes: 27 additions & 15 deletions spec/config/literal_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,34 @@ def parse_text(text)
end

describe 'nonquoted string parsing' do
# empty
it { expect('').to be_parsed_as(nil) }

it { expect('\\"').to be_parsed_as("\\\"") }
it { expect('\\t').to be_parsed_as("\\t") }
it { expect('\\n').to be_parsed_as("\\n") }
it { expect('\\r\\n').to be_parsed_as("\\r\\n") }
it { expect('\\f\\b').to be_parsed_as("\\f\\b") }
it { expect('\\.t').to be_parsed_as("\\.t") }
it { expect('\\$t').to be_parsed_as("\\$t") }
it { expect('\\#t').to be_parsed_as("\\") } # comment out
it { expect('\\z').to be_parsed_as("\\z") }
it { expect('\\0').to be_parsed_as("\\0") }
it { expect('\\1').to be_parsed_as("\\1") }
it { expect('\\#{test}').to be_parsed_as("\\") } # comment out
it { expect('\[').to be_parsed_as("\\[") }
it { expect('text').to be_parsed_as('text') }
it { expect('\"').to be_parsed_as('\"') }
it { expect('\t').to be_parsed_as('\t') }
it { expect('\n').to be_parsed_as('\n') }
it { expect('\r\n').to be_parsed_as('\r\n') }
it { expect('\f\b').to be_parsed_as('\f\b') }
it { expect('\.t').to be_parsed_as('\.t') }
it { expect('\$t').to be_parsed_as('\$t') }
it { expect('\#t').to be_parsed_as('\#t') }
it { expect('\z').to be_parsed_as('\z') }
it { expect('\0').to be_parsed_as('\0') }
it { expect('\1').to be_parsed_as('\1') }
it { expect('.').to be_parsed_as('.') }
it { expect('*').to be_parsed_as('*') }
it { expect('@').to be_parsed_as('@') }
it { expect('#{test}').to be_parsed_as('#{test}') }
it { expect('$').to be_parsed_as('$') }
it { expect('$t').to be_parsed_as('$t') }
it { expect('$}').to be_parsed_as('$}') }
it { expect('\\\\').to be_parsed_as('\\\\') }
it { expect('\[').to be_parsed_as('\[') }
it { expect('#foo').to be_parsed_as('#foo') } # not comment out
it { expect('foo#bar').to be_parsed_as('foo#bar') } # not comment out
it { expect(' text').to be_parsed_as('text') } # remove starting spaces
it { expect(' #foo').to be_parsed_as('#foo') } # remove starting spaces
it { expect('foo #bar').to be_parsed_as('foo') } # comment out
it { expect("foo\t#bar").to be_parsed_as('foo') } # comment out

it { expect('t').to be_parsed_as('t') }
it { expect('T').to be_parsed_as('T') }
Expand Down
2 changes: 1 addition & 1 deletion test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_multi_msgpack

def test_with_regexp
d = create_driver(CONFIG + %[
format /^(?<field_1>\\\\d+):(?<field_2>\\\\w+)$/
format /^(?<field_1>\\d+):(?<field_2>\\w+)$/
types field_1:integer
])

Expand Down

0 comments on commit 4f17c1b

Please sign in to comment.