Skip to content

Commit 368e02f

Browse files
authored
Merge branch 'master' into iteration
2 parents e4a1ab3 + 3f35981 commit 368e02f

File tree

9 files changed

+138
-85
lines changed

9 files changed

+138
-85
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "YAML"
22
uuid = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
3-
version = "0.4.10"
3+
version = "0.4.11"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

src/YAML.jl

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1-
__precompile__(true)
1+
"""
2+
YAML
23
4+
A package to treat YAML.
5+
https://github.com/JuliaData/YAML.jl
6+
7+
Reading:
8+
9+
* `YAML.load` parses the first YAML document of a YAML file as a Julia object.
10+
* `YAML.load_all` parses the all YAML documents of a YAML file.
11+
* `YAML.load_file` is same with `YAML.load` except it reads from a file.
12+
* `YAML.load_all_file` is same with `YAML.load_all` except it reads from a file.
13+
14+
Writing:
15+
16+
* `YAML.write` prints a Julia object as a YAML file.
17+
* `YAML.write_file` is same with `YAML.write` except it writes to a file.
18+
* `YAML.yaml` converts a given Julia object to a YAML-formatted string.
19+
"""
320
module YAML
421

522
import Base: isempty, length, show, peek
@@ -10,8 +27,14 @@ using Dates
1027
using Printf
1128
using StringEncodings
1229

30+
include("queue.jl")
31+
include("buffered_input.jl")
32+
include("tokens.jl")
1333
include("scanner.jl")
34+
include("events.jl")
1435
include("parser.jl")
36+
include("nodes.jl")
37+
include("resolver.jl")
1538
include("composer.jl")
1639
include("constructor.jl")
1740
include("writer.jl") # write Julia dictionaries to YAML files
@@ -21,7 +44,7 @@ const _dicttype = Union{Type,Function}
2144

2245
# add a dicttype-aware version of construct_mapping to the constructors
2346
function _patch_constructors(more_constructors::_constructor, dicttype::_dicttype)
24-
if more_constructors == nothing
47+
if more_constructors === nothing
2548
more_constructors = Dict{String,Function}()
2649
else
2750
more_constructors = copy(more_constructors) # do not change the outside world
@@ -35,6 +58,12 @@ function _patch_constructors(more_constructors::_constructor, dicttype::_dicttyp
3558
end
3659

3760

61+
"""
62+
load(x::Union{AbstractString, IO})
63+
64+
Parse the string or stream `x` as a YAML file, and return the first YAML document as a
65+
Julia object.
66+
"""
3867
load(ts::TokenStream, constructor::Constructor) =
3968
construct_document(constructor, compose(EventStream(ts)))
4069

@@ -47,6 +76,12 @@ load(ts::TokenStream, more_constructors::_constructor = nothing, multi_construct
4776
load(input::IO, more_constructors::_constructor = nothing, multi_constructors::Dict = Dict(); kwargs...) =
4877
load(TokenStream(input), more_constructors, multi_constructors ; kwargs...)
4978

79+
"""
80+
YAMLDocIterator
81+
82+
An iterator type to represent multiple YAML documents. You can retrieve each YAML document
83+
as a Julia object by iterating.
84+
"""
5085
mutable struct YAMLDocIterator
5186
input::IO
5287
ts::TokenStream
@@ -85,6 +120,11 @@ iterate(it::YAMLDocIterator, s) = done(it, s) ? nothing : next(it, s)
85120
Base.IteratorSize(::Type{YAMLDocIterator}) = Base.SizeUnknown()
86121
Base.IteratorEltype(::Type{YAMLDocIterator}) = Base.EltypeUnknown()
87122

123+
"""
124+
load_all(x::Union{AbstractString, IO}) -> YAMLDocIterator
125+
126+
Parse the string or stream `x` as a YAML file, and return corresponding YAML documents.
127+
"""
88128
load_all(input::IO, args...; kwargs...) =
89129
YAMLDocIterator(input, args...; kwargs...)
90130

@@ -94,11 +134,21 @@ load(input::AbstractString, args...; kwargs...) =
94134
load_all(input::AbstractString, args...; kwargs...) =
95135
load_all(IOBuffer(input), args...; kwargs...)
96136

137+
"""
138+
load_file(filename::AbstractString)
139+
140+
Parse the YAML file `filename`, and return the first YAML document as a Julia object.
141+
"""
97142
load_file(filename::AbstractString, args...; kwargs...) =
98143
open(filename, "r") do input
99144
load(input, args...; kwargs...)
100145
end
101146

147+
"""
148+
load_all_file(filename::AbstractString) -> YAMLDocIterator
149+
150+
Parse the YAML file `filename`, and return corresponding YAML documents.
151+
"""
102152
load_all_file(filename::AbstractString, args...; kwargs...) =
103153
load_all(open(filename, "r"), args...; kwargs...)
104154
end # module

src/composer.jl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11

2-
include("nodes.jl")
3-
include("resolver.jl")
4-
5-
62
struct ComposerError
73
context::Union{String, Nothing}
84
context_mark::Union{Mark, Nothing}
@@ -18,7 +14,7 @@ struct ComposerError
1814
end
1915

2016
function show(io::IO, error::ComposerError)
21-
if error.context != nothing
17+
if error.context !== nothing
2218
print(io, error.context, " at ", error.context_mark, ": ")
2319
end
2420
print(io, error.problem, " at ", error.problem_mark)
@@ -34,21 +30,21 @@ end
3430

3531
function compose(events)
3632
composer = Composer(events, Dict{String, Node}(), Resolver())
37-
@assert typeof(forward!(composer.input)) == StreamStartEvent
33+
@assert forward!(composer.input) isa StreamStartEvent
3834
node = compose_document(composer)
39-
if typeof(peek(composer.input)) == StreamEndEvent
35+
if peek(composer.input) isa StreamEndEvent
4036
forward!(composer.input)
4137
else
42-
@assert typeof(peek(composer.input)) == DocumentStartEvent
38+
@assert peek(composer.input) isa DocumentStartEvent
4339
end
4440
node
4541
end
4642

4743

4844
function compose_document(composer::Composer)
49-
@assert typeof(forward!(composer.input)) == DocumentStartEvent
45+
@assert forward!(composer.input) isa DocumentStartEvent
5046
node = compose_node(composer)
51-
@assert typeof(forward!(composer.input)) == DocumentEndEvent
47+
@assert forward!(composer.input) isa DocumentEndEvent
5248
empty!(composer.anchors)
5349
node
5450
end
@@ -134,7 +130,9 @@ function _compose_sequence_node(start_event::SequenceStartEvent, composer, ancho
134130
composer.anchors[anchor] = node
135131
end
136132

137-
while (event = peek(composer.input)) !== nothing
133+
while true
134+
event = peek(composer.input)
135+
event === nothing && break
138136
__compose_sequence_node(event, composer, node) || break
139137
end
140138

src/constructor.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct ConstructorError
1515
end
1616

1717
function show(io::IO, error::ConstructorError)
18-
if error.context != nothing
18+
if error.context !== nothing
1919
print(io, error.context, " at ", error.context_mark, ": ")
2020
end
2121
print(io, error.problem, " at ", error.problem_mark)
@@ -147,7 +147,7 @@ function flatten_mapping(node::MappingNode)
147147
elseif value_node isa SequenceNode
148148
submerge = []
149149
for subnode in value_node.value
150-
if typeof(subnode) != MappingNode
150+
if !(subnode isa MappingNode)
151151
throw(ConstructorError("while constructing a mapping",
152152
node.start_mark,
153153
"expected a mapping node, but found $(typeof(subnode))",

0 commit comments

Comments
 (0)