Skip to content

Commit 98d11d1

Browse files
committed
TOML: Avoid type-pirating Base.TOML.Parser
Since stdlibs can be duplicated but Base never is, `require_stdlib` makes type piracy even more complicated than it normally would be. To adapt, this changes `TOML.Parser` to be type defined by the TOML stdlib, so that we can define methods on it without committing type-piracy and avoid problems like Pkg.jl#4017
1 parent e4b29f7 commit 98d11d1

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

stdlib/TOML/src/TOML.jl

+16-14
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ explicitly create a `Parser` but instead one directly use use
3636
will however reuse some internal data structures which can be beneficial for
3737
performance if a larger number of small files are parsed.
3838
"""
39-
const Parser = Internals.Parser
39+
struct Parser
40+
_p::Internals.Parser{Dates}
41+
end
4042

4143
# Dates-enabled constructors
42-
Parser() = Parser{Dates}()
43-
Parser(io::IO) = Parser{Dates}(io)
44-
Parser(str::String; filepath=nothing) = Parser{Dates}(str; filepath)
44+
Parser() = Parser(Internals.Parser{Dates}())
45+
Parser(io::IO) = Parser(Internals.Parser{Dates}(io))
46+
Parser(str::String; filepath=nothing) = Parser(Internals.Parser{Dates}(str; filepath))
4547

4648
"""
4749
parsefile(f::AbstractString)
@@ -53,9 +55,9 @@ Parse file `f` and return the resulting table (dictionary). Throw a
5355
See also [`TOML.tryparsefile`](@ref).
5456
"""
5557
parsefile(f::AbstractString) =
56-
Internals.parse(Parser(readstring(f); filepath=abspath(f)))
58+
Internals.parse(Internals.Parser{Dates}(readstring(f); filepath=abspath(f)))
5759
parsefile(p::Parser, f::AbstractString) =
58-
Internals.parse(Internals.reinit!(p, readstring(f); filepath=abspath(f)))
60+
Internals.parse(Internals.reinit!(p._p, readstring(f); filepath=abspath(f)))
5961

6062
"""
6163
tryparsefile(f::AbstractString)
@@ -67,9 +69,9 @@ Parse file `f` and return the resulting table (dictionary). Return a
6769
See also [`TOML.parsefile`](@ref).
6870
"""
6971
tryparsefile(f::AbstractString) =
70-
Internals.tryparse(Parser(readstring(f); filepath=abspath(f)))
72+
Internals.tryparse(Internals.Parser{Dates}(readstring(f); filepath=abspath(f)))
7173
tryparsefile(p::Parser, f::AbstractString) =
72-
Internals.tryparse(Internals.reinit!(p, readstring(f); filepath=abspath(f)))
74+
Internals.tryparse(Internals.reinit!(p._p, readstring(f); filepath=abspath(f)))
7375

7476
"""
7577
parse(x::Union{AbstractString, IO})
@@ -81,11 +83,11 @@ Throw a [`ParserError`](@ref) upon failure.
8183
See also [`TOML.tryparse`](@ref).
8284
"""
8385
parse(str::AbstractString) =
84-
Internals.parse(Parser(String(str)))
86+
Internals.parse(Internals.Parser{Dates}(String(str)))
8587
parse(p::Parser, str::AbstractString) =
86-
Internals.parse(Internals.reinit!(p, String(str)))
88+
Internals.parse(Internals.reinit!(p._p, String(str)))
8789
parse(io::IO) = parse(read(io, String))
88-
parse(p::Parser, io::IO) = parse(p, read(io, String))
90+
parse(p::Parser, io::IO) = parse(p._p, read(io, String))
8991

9092
"""
9193
tryparse(x::Union{AbstractString, IO})
@@ -97,11 +99,11 @@ Return a [`ParserError`](@ref) upon failure.
9799
See also [`TOML.parse`](@ref).
98100
"""
99101
tryparse(str::AbstractString) =
100-
Internals.tryparse(Parser(String(str)))
102+
Internals.tryparse(Internals.Parser{Dates}(String(str)))
101103
tryparse(p::Parser, str::AbstractString) =
102-
Internals.tryparse(Internals.reinit!(p, String(str)))
104+
Internals.tryparse(Internals.reinit!(p._p, String(str)))
103105
tryparse(io::IO) = tryparse(read(io, String))
104-
tryparse(p::Parser, io::IO) = tryparse(p, read(io, String))
106+
tryparse(p::Parser, io::IO) = tryparse(p._p, read(io, String))
105107

106108
"""
107109
ParserError

0 commit comments

Comments
 (0)