Skip to content

Commit

Permalink
Merge branch 'master' into iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
contradict authored Jun 12, 2024
2 parents e4a1ab3 + 3f35981 commit 368e02f
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "YAML"
uuid = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
version = "0.4.10"
version = "0.4.11"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
54 changes: 52 additions & 2 deletions src/YAML.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
__precompile__(true)
"""
YAML
A package to treat YAML.
https://github.com/JuliaData/YAML.jl
Reading:
* `YAML.load` parses the first YAML document of a YAML file as a Julia object.
* `YAML.load_all` parses the all YAML documents of a YAML file.
* `YAML.load_file` is same with `YAML.load` except it reads from a file.
* `YAML.load_all_file` is same with `YAML.load_all` except it reads from a file.
Writing:
* `YAML.write` prints a Julia object as a YAML file.
* `YAML.write_file` is same with `YAML.write` except it writes to a file.
* `YAML.yaml` converts a given Julia object to a YAML-formatted string.
"""
module YAML

import Base: isempty, length, show, peek
Expand All @@ -10,8 +27,14 @@ using Dates
using Printf
using StringEncodings

include("queue.jl")
include("buffered_input.jl")
include("tokens.jl")
include("scanner.jl")
include("events.jl")
include("parser.jl")
include("nodes.jl")
include("resolver.jl")
include("composer.jl")
include("constructor.jl")
include("writer.jl") # write Julia dictionaries to YAML files
Expand All @@ -21,7 +44,7 @@ const _dicttype = Union{Type,Function}

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


"""
load(x::Union{AbstractString, IO})
Parse the string or stream `x` as a YAML file, and return the first YAML document as a
Julia object.
"""
load(ts::TokenStream, constructor::Constructor) =
construct_document(constructor, compose(EventStream(ts)))

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

"""
YAMLDocIterator
An iterator type to represent multiple YAML documents. You can retrieve each YAML document
as a Julia object by iterating.
"""
mutable struct YAMLDocIterator
input::IO
ts::TokenStream
Expand Down Expand Up @@ -85,6 +120,11 @@ iterate(it::YAMLDocIterator, s) = done(it, s) ? nothing : next(it, s)
Base.IteratorSize(::Type{YAMLDocIterator}) = Base.SizeUnknown()
Base.IteratorEltype(::Type{YAMLDocIterator}) = Base.EltypeUnknown()

"""
load_all(x::Union{AbstractString, IO}) -> YAMLDocIterator
Parse the string or stream `x` as a YAML file, and return corresponding YAML documents.
"""
load_all(input::IO, args...; kwargs...) =
YAMLDocIterator(input, args...; kwargs...)

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

"""
load_file(filename::AbstractString)
Parse the YAML file `filename`, and return the first YAML document as a Julia object.
"""
load_file(filename::AbstractString, args...; kwargs...) =
open(filename, "r") do input
load(input, args...; kwargs...)
end

"""
load_all_file(filename::AbstractString) -> YAMLDocIterator
Parse the YAML file `filename`, and return corresponding YAML documents.
"""
load_all_file(filename::AbstractString, args...; kwargs...) =
load_all(open(filename, "r"), args...; kwargs...)
end # module
20 changes: 9 additions & 11 deletions src/composer.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

include("nodes.jl")
include("resolver.jl")


struct ComposerError
context::Union{String, Nothing}
context_mark::Union{Mark, Nothing}
Expand All @@ -18,7 +14,7 @@ struct ComposerError
end

function show(io::IO, error::ComposerError)
if error.context != nothing
if error.context !== nothing
print(io, error.context, " at ", error.context_mark, ": ")
end
print(io, error.problem, " at ", error.problem_mark)
Expand All @@ -34,21 +30,21 @@ end

function compose(events)
composer = Composer(events, Dict{String, Node}(), Resolver())
@assert typeof(forward!(composer.input)) == StreamStartEvent
@assert forward!(composer.input) isa StreamStartEvent
node = compose_document(composer)
if typeof(peek(composer.input)) == StreamEndEvent
if peek(composer.input) isa StreamEndEvent
forward!(composer.input)
else
@assert typeof(peek(composer.input)) == DocumentStartEvent
@assert peek(composer.input) isa DocumentStartEvent
end
node
end


function compose_document(composer::Composer)
@assert typeof(forward!(composer.input)) == DocumentStartEvent
@assert forward!(composer.input) isa DocumentStartEvent
node = compose_node(composer)
@assert typeof(forward!(composer.input)) == DocumentEndEvent
@assert forward!(composer.input) isa DocumentEndEvent
empty!(composer.anchors)
node
end
Expand Down Expand Up @@ -134,7 +130,9 @@ function _compose_sequence_node(start_event::SequenceStartEvent, composer, ancho
composer.anchors[anchor] = node
end

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

Expand Down
4 changes: 2 additions & 2 deletions src/constructor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct ConstructorError
end

function show(io::IO, error::ConstructorError)
if error.context != nothing
if error.context !== nothing
print(io, error.context, " at ", error.context_mark, ": ")
end
print(io, error.problem, " at ", error.problem_mark)
Expand Down Expand Up @@ -147,7 +147,7 @@ function flatten_mapping(node::MappingNode)
elseif value_node isa SequenceNode
submerge = []
for subnode in value_node.value
if typeof(subnode) != MappingNode
if !(subnode isa MappingNode)
throw(ConstructorError("while constructing a mapping",
node.start_mark,
"expected a mapping node, but found $(typeof(subnode))",
Expand Down
Loading

0 comments on commit 368e02f

Please sign in to comment.