Skip to content

Commit

Permalink
Fix trailing commas in flow sequences (#116)
Browse files Browse the repository at this point in the history
Fixes #114
  • Loading branch information
hexane360 authored May 28, 2021
1 parent de8fa78 commit fa25484
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/composer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ function _compose_sequence_node(start_event::SequenceStartEvent, composer, ancho
composer.anchors[anchor] = node
end

while true
event = peek(composer.input)
while (event = peek(composer.input)) !== nothing
__compose_sequence_node(event, composer, node) || break
end

Expand Down
14 changes: 8 additions & 6 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -491,20 +491,22 @@ function _parse_flow_sequence_entry(token::Any, stream::EventStream, first_entry
forward!(stream.input)
else
throw(ParserError("while parsing a flow sequence",
stream.mark[end],
stream.marks[end],
"expected ',' or ']', but got $(typeof(token))",
token.span.start_mark))
end
end

token = peek(stream.input)
if typeof(token) == KeyToken
if isa(token, KeyToken)
stream.state = parse_flow_sequence_entry_mapping_key
return MappingStartEvent(token.span.start_mark, token.span.end_mark,
nothing, nothing, true, true)
elseif typeof(token) != FlowSequenceEndToken
MappingStartEvent(token.span.start_mark, token.span.end_mark,
nothing, nothing, true, true)
elseif isa(token, FlowSequenceEndToken)
nothing
else
push!(stream.states, parse_flow_sequence_entry)
return parse_flow_node(stream)
parse_flow_node(stream)
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,13 @@ order_two = OrderedDict(dict_content[[2,1]]...) # reverse order
@test YAML.load(YAML.yaml(Dict("a" => Dict()))) == Dict("a" => Dict())
@test YAML.load(YAML.yaml(Dict("a" => []))) == Dict("a" => [])

# issue 114 - gracefully handle extra commas in flow collections
@testset "issue114" begin
@test YAML.load("[3,4,]") == [3,4]
@test YAML.load("{a:4,b:5,}") == Dict("a" => 4, "b" => 5)
@test YAML.load("[?a:4, ?b:5]") == [Dict("a" => 4), Dict("b" => 5)]
@test_throws YAML.ParserError YAML.load("[3,,4]")
@test_throws YAML.ParserError YAML.load("{a: 3,,b:3}")
end

end # module

0 comments on commit fa25484

Please sign in to comment.