From 73ec0296d9444f0a6f5c7d3f107cc3a9e2c0e6ae Mon Sep 17 00:00:00 2001 From: Gregor Kappler Date: Wed, 1 Sep 2021 13:41:47 +0200 Subject: [PATCH] tryparse sentinel argument (#23) --- src/match.jl | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/match.jl b/src/match.jl index 8f96fdc..bcf84dd 100644 --- a/src/match.jl +++ b/src/match.jl @@ -278,20 +278,20 @@ end import Base: tryparse, parse export tryparse_pos function Base.parse(p::AbstractToken, s, pos...; kw...) - i = tryparse_pos(p, s, pos...; kw...) - i === nothing && throw(ArgumentError("no successfull parsing.")) + i = tryparse_pos(p, s, pos...; sentinel = NoMatch(), kw...) + i === NoMatch() && throw(ArgumentError("no successfull parsing.")) i[1] end -function Base.tryparse(p::AbstractToken, s, pos...; kw...) - i = tryparse_pos(p, s, pos...; kw...) - i === nothing && return nothing +function Base.tryparse(p::AbstractToken, s, pos...; sentinel=nothing, kw...) + i = tryparse_pos(p, s, pos...; sentinel=sentinel, kw...) + i === sentinel && return sentinel i[1] end -function tryparse_pos(p,s, idx=firstindex(s), till=lastindex(s); kw...) +function tryparse_pos(p,s, idx=firstindex(s), till=lastindex(s); sentinel=nothing, kw...) i = iterate_state(wrap(p; kw...),s,till,idx,idx,nothing) - i === nothing && return nothing + i === nothing && return sentinel get(p,s,till,tuple_pos(i),1,tuple_state(i)), tuple_pos(i) end @@ -301,12 +301,15 @@ end Parse `sequence` with `parser` at start and produce an instance of `result_type(parser)`. If `log!==nothing`, parser is transformed with [`log_names`](@ref)`(p, log)` before matching. - tryparse(parser::CombinedParser, sequence[, idx=firstindex(sequence)[, till=lastindex(sequence)]]; log=nothing) +Throws an `ArgumentError` if parsing is not successful (like `Base.parse`). + + tryparse(parser::CombinedParser, sequence[, idx=firstindex(sequence)[, till=lastindex(sequence)]]; sentinel=nothing, log=nothing) -returns either a result value or `nothing` if sequence does not start with with a match. +returns either a result value or `sentinel` if sequence does not start with with a match. +(Consider `sentinel=CombinedParsers.NoMatch()` for parsers what might result in `nothing` after success.) - tryparse_pos(parser::CombinedParser, str::AbstractString[, idx=firstindex(sequence)[, till=lastindex(s)]]; log=nothing) -returns either a tuple of result value and the position after the match, or `nothing` if sequence does not start with with a match. + tryparse_pos(parser::CombinedParser, str::AbstractString[, idx=firstindex(sequence)[, till=lastindex(s)]]; sentinel=nothing, log=nothing) +returns either a tuple of result value and the position after the match, or `sentinel` if sequence does not start with with a match. # Example