@@ -9,8 +9,8 @@ module TOML
9
9
10
10
using Base: IdSet
11
11
12
- # In case we do not have the Dates stdlib available
13
12
# we parse DateTime into these internal structs,
13
+ # unless a different DateTime library is passed to the Parser constructor
14
14
# note that these do not do any argument checking
15
15
struct Date
16
16
year:: Int
@@ -85,12 +85,10 @@ mutable struct Parser
85
85
# Filled in in case we are parsing a file to improve error messages
86
86
filepath:: Union{String, Nothing}
87
87
88
- # Gets populated with the Dates stdlib if it exists
88
+ # Optionally populate with the Dates stdlib to change the type of Date types returned
89
89
Dates:: Union{Module, Nothing}
90
90
end
91
91
92
- const DATES_PKGID = Base. PkgId (Base. UUID (" ade2ca70-3891-5945-98fb-dc099432e06a" ), " Dates" )
93
-
94
92
function Parser (str:: String ; filepath= nothing )
95
93
root = TOMLDict ()
96
94
l = Parser (
@@ -109,7 +107,7 @@ function Parser(str::String; filepath=nothing)
109
107
IdSet {TOMLDict} (), # defined_tables
110
108
root,
111
109
filepath,
112
- isdefined (Base, :maybe_root_module ) ? Base . maybe_root_module (DATES_PKGID) : nothing ,
110
+ nothing
113
111
)
114
112
startup (l)
115
113
return l
151
149
# Errors #
152
150
# #########
153
151
154
- throw_internal_error (msg) = error (" internal TOML parser error: $msg " )
155
-
156
152
# Many functions return a ParserError. We want this to bubble up
157
153
# all the way and have this error be returned to the user
158
154
# if the parse is called with `raise=false`. This macro
900
896
function parse_float (l:: Parser , contains_underscore):: Err{Float64}
901
897
s = take_string_or_substring (l, contains_underscore)
902
898
v = Base. tryparse (Float64, s)
903
- v === nothing && return ( ParserError (ErrGenericValueError) )
899
+ v === nothing && return ParserError (ErrGenericValueError)
904
900
return v
905
901
end
906
902
@@ -909,8 +905,8 @@ function parse_int(l::Parser, contains_underscore, base=nothing)::Err{Int64}
909
905
v = try
910
906
Base. parse (Int64, s; base= base)
911
907
catch e
912
- e isa Base. OverflowError && return ( ParserError (ErrOverflowError) )
913
- error ( " internal parser error: did not correctly discredit $( repr (s)) as an int " )
908
+ e isa Base. OverflowError && return ParserError (ErrOverflowError)
909
+ rethrow ( )
914
910
end
915
911
return v
916
912
end
@@ -932,8 +928,8 @@ for (name, T1, T2, n1, n2) in (("integer", Int64, Int128, 17, 33),
932
928
Base. parse (BigInt, s; base)
933
929
end
934
930
catch e
935
- e isa Base. OverflowError && return ( ParserError (ErrOverflowError) )
936
- error ( " internal parser error: did not correctly discredit $( repr (s)) as an int " )
931
+ e isa Base. OverflowError && return ParserError (ErrOverflowError)
932
+ rethrow ( )
937
933
end
938
934
return v
939
935
end
@@ -1030,8 +1026,9 @@ function try_return_datetime(p, year, month, day, h, m, s, ms)
1030
1026
if Dates != = nothing
1031
1027
try
1032
1028
return Dates. DateTime (year, month, day, h, m, s, ms)
1033
- catch
1034
- return ParserError (ErrParsingDateTime)
1029
+ catch ex
1030
+ ex isa ArgumentError && return ParserError (ErrParsingDateTime)
1031
+ rethrow ()
1035
1032
end
1036
1033
else
1037
1034
return DateTime (year, month, day, h, m, s, ms)
@@ -1043,8 +1040,9 @@ function try_return_date(p, year, month, day)
1043
1040
if Dates != = nothing
1044
1041
try
1045
1042
return Dates. Date (year, month, day)
1046
- catch
1047
- return ParserError (ErrParsingDateTime)
1043
+ catch ex
1044
+ ex isa ArgumentError && return ParserError (ErrParsingDateTime)
1045
+ rethrow ()
1048
1046
end
1049
1047
else
1050
1048
return Date (year, month, day)
@@ -1065,8 +1063,9 @@ function try_return_time(p, h, m, s, ms)
1065
1063
if Dates != = nothing
1066
1064
try
1067
1065
return Dates. Time (h, m, s, ms)
1068
- catch
1069
- return ParserError (ErrParsingDateTime)
1066
+ catch ex
1067
+ ex isa ArgumentError && return ParserError (ErrParsingDateTime)
1068
+ rethrow ()
1070
1069
end
1071
1070
else
1072
1071
return Time (h, m, s, ms)
0 commit comments