Skip to content

Commit

Permalink
Merge pull request #27 from CocoaPods/segiddins/comment-after-trailin…
Browse files Browse the repository at this point in the history
…g-comma

Fix parsing arrays that contain a comment after a trailing comma
  • Loading branch information
segiddins committed Mar 29, 2018
2 parents f9e2d77 + 5cb56c5 commit 2612cd9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

##### Enhancements

* None.
* Fix parsing arrays that contain a comment after a trailing comma.
[Samuel Giddins](https://github.com/segiddins)
[#26](https://github.com/CocoaPods/Nanaimo/issues/26)

##### Bug Fixes

Expand Down
14 changes: 6 additions & 8 deletions lib/nanaimo/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def read_string_encoding
# TODO
end

def parse_object
_comment = skip_to_non_space_matching_annotations
def parse_object(already_parsed_comment: false)
_comment = skip_to_non_space_matching_annotations unless already_parsed_comment
start_pos = @scanner.pos
raise_parser_error ParseError, 'Unexpected end of string while parsing' if @scanner.eos?
if @scanner.skip(/\{/)
Expand Down Expand Up @@ -163,10 +163,10 @@ def parse_quotedstring(quote)
def parse_array
objects = []
until @scanner.eos?
eat_whitespace!
_comment = skip_to_non_space_matching_annotations
break if @scanner.skip(/\)/)

objects << parse_object
objects << parse_object(already_parsed_comment: true)

eat_whitespace!
break if @scanner.skip(/\)/)
Expand All @@ -181,10 +181,10 @@ def parse_array
def parse_dictionary
objects = {}
until @scanner.eos?
skip_to_non_space_matching_annotations
_comment = skip_to_non_space_matching_annotations
break if @scanner.skip(/}/)

key = parse_object
key = parse_object(already_parsed_comment: true)
eat_whitespace!
unless @scanner.skip(/=/)
raise_parser_error ParseError, "Dictionary missing value for key #{key.as_ruby.inspect}, expected '=' and found #{current_character.inspect}"
Expand Down Expand Up @@ -263,8 +263,6 @@ def skip_to_non_space_matching_annotations
next
end

eat_whitespace!

break
end
annotation
Expand Down
63 changes: 63 additions & 0 deletions spec/nanaimo/reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ module Nanaimo
it 'should maintain ordering' do
expect(subject.root_object.value.map(&:value)).to eql %w(IDENTIFIER ANOTHER_IDENTIFIER)
end

describe 'with a trailing comma' do
let(:string) { <<-PLIST }
{
singleline = ( A, B, );
singleline_no_spaces = (A,B,);
singleline_trailing_comment = ( A , B, /* comment!*/ );
multiline = (
A,
B,
);
multiline_last_line_comment = (
A,
B,
// comment! C,
)
}
PLIST

it 'should parse' do
expect(subject).to be_a Plist
expect(subject.as_ruby).to eq(
'singleline' => %w[A B],
'singleline_no_spaces' => %w[A B],
'singleline_trailing_comment' => %w[A B],
'multiline' => %w[A B],
'multiline_last_line_comment' => %w[A B]
)
end
end
end
end

Expand All @@ -57,6 +87,19 @@ module Nanaimo
], '')
}, '')
end

describe 'when there are multiple comments after the object' do
let(:string) { "{a /*annotation1*/ /*annotation2*/ = ( b //annotation3\n//annotation4\n )}" }

it 'should parse the annotations' do
expect(subject).to eq Nanaimo::Dictionary.new({
Nanaimo::String.new('a', 'annotation2') =>
Nanaimo::Array.new([
Nanaimo::String.new('b', "annotation4\n")
], '')
}, '')
end
end
end

describe 'reading root level dictionaries' do
Expand Down Expand Up @@ -91,6 +134,26 @@ module Nanaimo
end
end
end

context 'when the dictionarly contains comments' do
let(:string) { <<-PLIST }
{
comment_before_key = { /* comment */ k = v; };
comment_before_value = { k = /* comment */ v; };
comment_after_value_annotation = { k = v /* annotation */ /* comment */ ; };
comment_after_last_semicolon = { k = v; /* comment */ };
}
PLIST

it 'parses correctly' do
expect(subject.as_ruby).to eq(
'comment_after_value_annotation' => { 'k' => 'v' },
'comment_before_key' => { 'k' => 'v' },
'comment_before_value' => { 'k' => 'v' },
'comment_after_last_semicolon' => { 'k' => 'v' }
)
end
end
end

describe 'unquoted strings' do
Expand Down

0 comments on commit 2612cd9

Please sign in to comment.