Skip to content

Commit

Permalink
improve inferrabilities within TOML module (#38831)
Browse files Browse the repository at this point in the history
* improve inferrability within TOML module

* simplify with `@try` macro

* apply suggestion, use `Int64`
  • Loading branch information
aviatesk authored Dec 12, 2020
1 parent bc14e28 commit b3eaa34
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions base/toml_parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -925,21 +925,21 @@ ok_end_value(c::Char) = iswhitespace(c) || c == '#' || c == EOF_CHAR || c == ']'
accept_two(l, f::F) where {F} = accept_n(l, 2, f) || return(ParserError(ErrParsingDateTime))
function parse_datetime(l)
# Year has already been eaten when we reach here
year = parse_int(l, false)::Int64
year = @try parse_int(l, false)
year in 0:9999 || return ParserError(ErrParsingDateTime)

# Month
accept(l, '-') || return ParserError(ErrParsingDateTime)
set_marker!(l)
@try accept_two(l, isdigit)
month = parse_int(l, false)
month = @try parse_int(l, false)
month in 1:12 || return ParserError(ErrParsingDateTime)
accept(l, '-') || return ParserError(ErrParsingDateTime)

# Day
set_marker!(l)
@try accept_two(l, isdigit)
day = parse_int(l, false)
day = @try parse_int(l, false)
# Verify the real range in the constructor below
day in 1:31 || return ParserError(ErrParsingDateTime)

Expand Down Expand Up @@ -976,9 +976,10 @@ function parse_datetime(l)
end

function try_return_datetime(p, year, month, day, h, m, s, ms)
if p.Dates !== nothing
Dates = p.Dates
if Dates !== nothing
try
return p.Dates.DateTime(year, month, day, h, m, s, ms)
return Dates.DateTime(year, month, day, h, m, s, ms)
catch
return ParserError(ErrParsingDateTime)
end
Expand All @@ -988,9 +989,10 @@ function try_return_datetime(p, year, month, day, h, m, s, ms)
end

function try_return_date(p, year, month, day)
if p.Dates !== nothing
Dates = p.Dates
if Dates !== nothing
try
return p.Dates.Date(year, month, day)
return Dates.Date(year, month, day)
catch
return ParserError(ErrParsingDateTime)
end
Expand All @@ -1000,7 +1002,7 @@ function try_return_date(p, year, month, day)
end

function parse_local_time(l::Parser)
h = parse_int(l, false)
h = @try parse_int(l, false)
h in 0:23 || return ParserError(ErrParsingDateTime)
_, m, s, ms = @try _parse_local_time(l, true)
# TODO: Could potentially parse greater accuracy for the
Expand All @@ -1009,9 +1011,10 @@ function parse_local_time(l::Parser)
end

function try_return_time(p, h, m, s, ms)
if p.Dates !== nothing
Dates = p.Dates
if Dates !== nothing
try
return p.Dates.Time(h, m, s, ms)
return Dates.Time(h, m, s, ms)
catch
return ParserError(ErrParsingDateTime)
end
Expand Down Expand Up @@ -1133,7 +1136,7 @@ function parse_string_continue(l::Parser, multiline::Bool, quoted::Bool)::Err{St
if !accept_n(l, n, isvalid_hex)
return ParserError(ErrInvalidUnicodeScalar)
end
codepoint = parse_int(l, false, 16)
codepoint = parse_int(l, false, 16)::Int64
#=
Unicode Scalar Value
---------------------
Expand Down

0 comments on commit b3eaa34

Please sign in to comment.