Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Not Urgent, Stylistic] Format Julia source files by JuliaFormatter #157

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Formatting options for the default style supported in JuliaFormatter v1.0.56.
# https://domluna.github.io/JuliaFormatter.jl/stable/

# style = "default"
# indent = 4
# margin = 92
always_for_in = "nothing" # default false
# for_in_replacement = "in"
whitespace_typedefs = true # default false
# whitespace_ops_in_indices = false
remove_extra_newlines = true # default false
# import_to_using = false
# pipe_to_function_call = false
# short_to_long_function_def = false
# long_to_short_function_def = false
# always_use_return = false
whitespace_in_kwargs = false # default true
annotate_untyped_fields_with_any = false # default true
# format_docstrings = false
# align_struct_field = false
# align_assignment = false
# align_conditional = false
align_pair_arrow = true # default false
# conditional_to_if = false
# normalize_line_endings = "auto"
# align_matrix = false
# join_lines_based_on_source = false
trailing_comma = true # default false
# trailing_zero = true
# indent_submodule = false
# separate_kwargs_with_semicolon = false
surround_whereop_typeparameters = false # default true
# variable_call_indent = []
# short_circuit_to_if = false
51 changes: 34 additions & 17 deletions src/YAML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,45 @@ include("constructor.jl")
include("writer.jl") # write Julia dictionaries to YAML files

const _constructor = Union{Nothing, Dict}
const _dicttype = Union{Type,Function}
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
more_constructors = Dict{String,Function}()
more_constructors = Dict{String, Function}()
else
more_constructors = copy(more_constructors) # do not change the outside world
end
if !haskey(more_constructors, "tag:yaml.org,2002:map")
more_constructors["tag:yaml.org,2002:map"] = custom_mapping(dicttype) # map to the custom type
elseif dicttype != Dict{Any,Any} # only warn if another type has explicitly been set
elseif dicttype != Dict{Any, Any} # only warn if another type has explicitly been set
@warn "dicttype=$dicttype has no effect because more_constructors has the key \"tag:yaml.org,2002:map\""
end
return more_constructors
end


load(ts::TokenStream, constructor::Constructor) =
construct_document(constructor, compose(EventStream(ts)))

load(input::IO, constructor::Constructor) =
load(TokenStream(input), constructor)

load(ts::TokenStream, more_constructors::_constructor = nothing, multi_constructors::Dict = Dict(); dicttype::_dicttype = Dict{Any, Any}, constructorType::Function = SafeConstructor) =
load(ts, constructorType(_patch_constructors(more_constructors, dicttype), multi_constructors))

load(input::IO, more_constructors::_constructor = nothing, multi_constructors::Dict = Dict(); kwargs...) =
load(TokenStream(input), more_constructors, multi_constructors ; kwargs...)
load(input::IO, constructor::Constructor) = load(TokenStream(input), constructor)

load(
ts::TokenStream,
more_constructors::_constructor=nothing,
multi_constructors::Dict=Dict();
dicttype::_dicttype=Dict{Any, Any},
constructorType::Function=SafeConstructor,
) = load(
ts,
constructorType(_patch_constructors(more_constructors, dicttype), multi_constructors),
Comment on lines +40 to +48
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this formatting

Copy link
Contributor

@GunnarFarneback GunnarFarneback Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. This is the kind of JuliaFormatter thing that makes me not want to touch a code base.

This particular definition shouldn't have been in short form at all though; there's way too many arguments to make that look good, regardless of formatting. Never mind, Juliaformatter makes long form definitions equally horrible.

)

load(
input::IO,
more_constructors::_constructor=nothing,
multi_constructors::Dict=Dict();
kwargs...,
) = load(TokenStream(input), more_constructors, multi_constructors; kwargs...)

mutable struct YAMLDocIterator
input::IO
Expand All @@ -58,7 +68,16 @@ mutable struct YAMLDocIterator
end
end

YAMLDocIterator(input::IO, more_constructors::_constructor=nothing, multi_constructors::Dict = Dict(); dicttype::_dicttype=Dict{Any, Any}, constructorType::Function = SafeConstructor) = YAMLDocIterator(input, constructorType(_patch_constructors(more_constructors, dicttype), multi_constructors))
YAMLDocIterator(
input::IO,
more_constructors::_constructor=nothing,
multi_constructors::Dict=Dict();
dicttype::_dicttype=Dict{Any, Any},
constructorType::Function=SafeConstructor,
) = YAMLDocIterator(
input,
constructorType(_patch_constructors(more_constructors, dicttype), multi_constructors),
)

# Old iteration protocol:
start(it::YAMLDocIterator) = nothing
Expand All @@ -80,11 +99,9 @@ done(it::YAMLDocIterator, state) = it.next_doc === nothing
iterate(it::YAMLDocIterator) = next(it, start(it))
iterate(it::YAMLDocIterator, s) = done(it, s) ? nothing : next(it, s)

load_all(input::IO, args...; kwargs...) =
YAMLDocIterator(input, args...; kwargs...)
load_all(input::IO, args...; kwargs...) = YAMLDocIterator(input, args...; kwargs...)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This I agree with


load(input::AbstractString, args...; kwargs...) =
load(IOBuffer(input), args...; kwargs...)
load(input::AbstractString, args...; kwargs...) = load(IOBuffer(input), args...; kwargs...)

load_all(input::AbstractString, args...; kwargs...) =
load_all(IOBuffer(input), args...; kwargs...)
Expand Down
11 changes: 3 additions & 8 deletions src/buffered_input.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@


# Simple buffered input that allows peeking an arbitrary number of characters
# ahead by maintaining a typically quite small buffer of a few characters.


mutable struct BufferedInput
input::IO
buffer::Vector{Char}
Expand All @@ -15,13 +13,12 @@ mutable struct BufferedInput
end
end


# Read and buffer n more characters
function __fill(bi::BufferedInput, bi_input::IO, n::Integer)
for i in 1:n
c = eof(bi_input) ? '\0' : read(bi_input, Char)
if bi.offset + bi.avail + 1 <= length(bi.buffer)
bi.buffer[bi.offset + bi.avail + 1] = c
bi.buffer[bi.offset+bi.avail+1] = c
else
push!(bi.buffer, c)
end
Expand All @@ -37,20 +34,18 @@ function peek(bi::BufferedInput, i::Integer=0)
if bi.avail < i + 1
_fill(bi, i + 1 - bi.avail)
end
return bi.buffer[bi.offset + i + 1]
return bi.buffer[bi.offset+i+1]
end


# Return the string formed from the first n characters from the current position
# of the stream.
function prefix(bi::BufferedInput, n::Integer=1)
if bi.avail < n + 1
_fill(bi, n + 1 - bi.avail)
end
return string(bi.buffer[(bi.offset + 1):(bi.offset + n)]...)
return string(bi.buffer[(bi.offset+1):(bi.offset+n)]...)
end


# NOPE: This is wrong. What if n > bi.avail

# Advance the stream by n characters.
Expand Down
73 changes: 41 additions & 32 deletions src/composer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
include("nodes.jl")
include("resolver.jl")


struct ComposerError
context::Union{String, Nothing}
context_mark::Union{Mark, Nothing}
problem::Union{String, Nothing}
problem_mark::Union{Mark, Nothing}
note::Union{String, Nothing}

function ComposerError(context=nothing, context_mark=nothing,
problem=nothing, problem_mark=nothing,
note=nothing)
function ComposerError(
context=nothing,
context_mark=nothing,
problem=nothing,
problem_mark=nothing,
note=nothing,
)
new(context, context_mark, problem, problem_mark, note)
end
end
Expand All @@ -24,14 +27,12 @@ function show(io::IO, error::ComposerError)
print(io, error.problem, " at ", error.problem_mark)
end


mutable struct Composer
input::EventStream
anchors::Dict{String, Node}
resolver::Resolver
end


function compose(events)
composer = Composer(events, Dict{String, Node}(), Resolver())
@assert typeof(forward!(composer.input)) == StreamStartEvent
Expand All @@ -44,7 +45,6 @@ function compose(events)
node
end


function compose_document(composer::Composer)
@assert typeof(forward!(composer.input)) == DocumentStartEvent
node = compose_node(composer)
Expand All @@ -53,20 +53,31 @@ function compose_document(composer::Composer)
node
end


function handle_event(event::AliasEvent, composer)
anchor = event.anchor
forward!(composer.input)
haskey(composer.anchors, anchor) || throw(ComposerError(
nothing, nothing, "found undefined alias '$(anchor)'", event.start_mark))
haskey(composer.anchors, anchor) || throw(
ComposerError(
nothing,
nothing,
"found undefined alias '$(anchor)'",
event.start_mark,
),
)
return composer.anchors[anchor]
end

handle_error(event, composer, anchor) =
anchor !== nothing && haskey(composer.anchors, anchor) && throw(ComposerError(
"found duplicate anchor '$(anchor)'; first occurance",
composer.anchors[anchor].start_mark, "second occurence",
event.start_mark))
anchor !== nothing &&
haskey(composer.anchors, anchor) &&
throw(
ComposerError(
"found duplicate anchor '$(anchor)'; first occurance",
composer.anchors[anchor].start_mark,
"second occurence",
event.start_mark,
),
)

function handle_event(event::ScalarEvent, composer)
anchor = event.anchor
Expand All @@ -88,22 +99,22 @@ end

handle_event(event, composer) = nothing


function compose_node(composer::Composer)
event = peek(composer.input)
handle_event(event, composer)
end


function _compose_scalar_node(event::ScalarEvent, composer::Composer, anchor::Union{String, Nothing})
function _compose_scalar_node(
event::ScalarEvent,
composer::Composer,
anchor::Union{String, Nothing},
)
tag = event.tag
if tag === nothing || tag == "!"
tag = resolve(composer.resolver, ScalarNode,
event.value, event.implicit)
tag = resolve(composer.resolver, ScalarNode, event.value, event.implicit)
end

node = ScalarNode(tag, event.value, event.start_mark, event.end_mark,
event.style)
node = ScalarNode(tag, event.value, event.start_mark, event.end_mark, event.style)
if anchor !== nothing
composer.anchors[anchor] = node
end
Expand All @@ -114,7 +125,6 @@ end
compose_scalar_node(composer::Composer, anchor::Union{String, Nothing}) =
_compose_scalar_node(forward!(composer.input), composer, anchor)


__compose_sequence_node(event::SequenceEndEvent, composer, node) = false
function __compose_sequence_node(event::Event, composer, node)
push!(node.value, compose_node(composer))
Expand All @@ -124,12 +134,10 @@ end
function _compose_sequence_node(start_event::SequenceStartEvent, composer, anchor)
tag = start_event.tag
if tag === nothing || tag == "!"
tag = resolve(composer.resolver, SequenceNode,
nothing, start_event.implicit)
tag = resolve(composer.resolver, SequenceNode, nothing, start_event.implicit)
end

node = SequenceNode(tag, Any[], start_event.start_mark, nothing,
start_event.flow_style)
node = SequenceNode(tag, Any[], start_event.start_mark, nothing, start_event.flow_style)
if anchor !== nothing
composer.anchors[anchor] = node
end
Expand All @@ -147,7 +155,6 @@ end
compose_sequence_node(composer::Composer, anchor::Union{String, Nothing}) =
_compose_sequence_node(forward!(composer.input), composer, anchor)


__compose_mapping_node(event::MappingEndEvent, composer, node) = false
function __compose_mapping_node(event::Event, composer, node)
item_key = compose_node(composer)
Expand All @@ -156,15 +163,17 @@ function __compose_mapping_node(event::Event, composer, node)
true
end

function _compose_mapping_node(start_event::MappingStartEvent, composer::Composer, anchor::Union{String, Nothing})
function _compose_mapping_node(
start_event::MappingStartEvent,
composer::Composer,
anchor::Union{String, Nothing},
)
tag = start_event.tag
if tag === nothing || tag == "!"
tag = resolve(composer.resolver, MappingNode,
nothing, start_event.implicit)
tag = resolve(composer.resolver, MappingNode, nothing, start_event.implicit)
end

node = MappingNode(tag, Any[], start_event.start_mark, nothing,
start_event.flow_style)
node = MappingNode(tag, Any[], start_event.start_mark, nothing, start_event.flow_style)
if anchor !== nothing
composer.anchors[anchor] = node
end
Expand Down
Loading
Loading