Skip to content

Commit eb02c81

Browse files
committed
TOML: Make Dates a type parameter
This allows the `Dates` calls to be statically inferred
1 parent 1193997 commit eb02c81

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

base/loading.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ const LOADING_CACHE = Ref{Union{LoadingCache, Nothing}}(nothing)
264264
LoadingCache() = LoadingCache(load_path(), Dict(), Dict(), Dict(), Set(), Dict(), Dict(), Dict())
265265

266266

267-
struct TOMLCache
268-
p::TOML.Parser
267+
struct TOMLCache{Dates}
268+
p::TOML.Parser{Dates}
269269
d::Dict{String, CachedTOMLDict}
270270
end
271-
const TOML_CACHE = TOMLCache(TOML.Parser(), Dict{String, Dict{String, Any}}())
271+
const TOML_CACHE = TOMLCache(TOML.Parser(), Dict{String, CachedTOMLDict}())
272272

273273
parsed_toml(project_file::AbstractString) = parsed_toml(project_file, TOML_CACHE, require_lock)
274274
function parsed_toml(project_file::AbstractString, toml_cache::TOMLCache, toml_lock::ReentrantLock)

base/toml_parser.jl

+14-16
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const TOMLDict = Dict{String, Any}
3838
# Parser #
3939
##########
4040

41-
mutable struct Parser
41+
mutable struct Parser{Dates}
4242
str::String
4343
# 1 character look ahead
4444
current_char::Char
@@ -84,14 +84,11 @@ mutable struct Parser
8484

8585
# Filled in in case we are parsing a file to improve error messages
8686
filepath::Union{String, Nothing}
87-
88-
# Optionally populate with the Dates stdlib to change the type of Date types returned
89-
Dates::Union{Module, Nothing}
9087
end
9188

92-
function Parser(str::String; filepath=nothing)
89+
function Parser{Dates}(str::String; filepath=nothing) where {Dates}
9390
root = TOMLDict()
94-
l = Parser(
91+
l = Parser{Dates}(
9592
str, # str
9693
EOF_CHAR, # current_char
9794
firstindex(str), # pos
@@ -106,12 +103,12 @@ function Parser(str::String; filepath=nothing)
106103
IdSet{Any}(), # static_arrays
107104
IdSet{TOMLDict}(), # defined_tables
108105
root,
109-
filepath,
110-
nothing
106+
filepath
111107
)
112108
startup(l)
113109
return l
114110
end
111+
115112
function startup(l::Parser)
116113
# Populate our one character look-ahead
117114
c = eat_char(l)
@@ -122,8 +119,12 @@ function startup(l::Parser)
122119
end
123120
end
124121

125-
Parser() = Parser("")
126-
Parser(io::IO) = Parser(read(io, String))
122+
Parser{Dates}() where {Dates} = Parser{Dates}("")
123+
Parser{Dates}(io::IO) where {Dates} = Parser{Dates}(read(io, String))
124+
125+
Parser() = Parser{nothing}()
126+
Parser(io::IO) = Parser{nothing}(io)
127+
Parser(str::String; filepath=nothing) = Parser{nothing}(str; filepath)
127128

128129
function reinit!(p::Parser, str::String; filepath::Union{Nothing, String}=nothing)
129130
p.str = str
@@ -1021,8 +1022,7 @@ function parse_datetime(l)
10211022
return try_return_datetime(l, year, month, day, h, m, s, ms)
10221023
end
10231024

1024-
function try_return_datetime(p, year, month, day, h, m, s, ms)
1025-
Dates = p.Dates
1025+
function try_return_datetime(p::Parser{Dates}, year, month, day, h, m, s, ms) where Dates
10261026
if Dates !== nothing
10271027
try
10281028
return Dates.DateTime(year, month, day, h, m, s, ms)
@@ -1035,8 +1035,7 @@ function try_return_datetime(p, year, month, day, h, m, s, ms)
10351035
end
10361036
end
10371037

1038-
function try_return_date(p, year, month, day)
1039-
Dates = p.Dates
1038+
function try_return_date(p::Parser{Dates}, year, month, day) where Dates
10401039
if Dates !== nothing
10411040
try
10421041
return Dates.Date(year, month, day)
@@ -1058,8 +1057,7 @@ function parse_local_time(l::Parser)
10581057
return try_return_time(l, h, m, s, ms)
10591058
end
10601059

1061-
function try_return_time(p, h, m, s, ms)
1062-
Dates = p.Dates
1060+
function try_return_time(p::Parser{Dates}, h, m, s, ms) where Dates
10631061
if Dates !== nothing
10641062
try
10651063
return Dates.Time(h, m, s, ms)

stdlib/TOML/src/TOML.jl

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ const Parser = Internals.Parser
4444
Constructor for a TOML `Parser` which returns date and time objects from Dates.
4545
"""
4646
function DTParser(args...; kwargs...)
47-
parser = Parser(args...; kwargs...)
48-
parser.Dates = Dates
47+
parser = Parser{Dates}(args...; kwargs...)
4948
return parser
5049
end
5150

0 commit comments

Comments
 (0)