From 53b1e54bba02aa8b4aaa3279c657dd6cb7f6da21 Mon Sep 17 00:00:00 2001 From: Gregor Kappler Date: Tue, 31 Aug 2021 15:48:02 +0200 Subject: [PATCH] disambiguate iterate_state --- src/CombinedParsers.jl | 5 ++--- src/assertions.jl | 5 ++++- src/re.jl | 12 ++---------- src/reverse.jl | 5 ++++- src/textparse.jl | 21 +++++++++------------ 5 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/CombinedParsers.jl b/src/CombinedParsers.jl index ef759cb..ec5f93c 100644 --- a/src/CombinedParsers.jl +++ b/src/CombinedParsers.jl @@ -113,7 +113,7 @@ print_constructor(io::IO,x) = export iterate_state """ - iterate_state(parser, sequence, till::Int, posi::Int[, next_i=posi[, state=nothing]]) + iterate_state(parser, sequence[, till::Int=lastindex(sequence)[, posi::Int=firstindex(sequence)[, next_i=posi[, state=nothing]]]]) Return position `after` next match of `parser` in `sequence` at `posi`. The next match is following current match `state` (first match iif `state==nothing`). @@ -133,9 +133,8 @@ If no next match is found, return `nothing`. - `Tuple{Int64,state_type(parser)}` with next position, match state if a match is found. """ -@inline iterate_state(parser::CombinedParser, sequence, till::Int, posi::Int) = +@inline iterate_state(parser::CombinedParser, sequence, till=lastindex(sequence), posi=firstindex(sequence)) = iterate_state(parser, sequence,till,posi,posi,nothing) -@deprecate iterate_state(parser::CombinedParser, sequence, till::Int, posi::Int, ::Nothing) iterate_state(parser, sequence,till,posi,posi,nothing) """ diff --git a/src/assertions.jl b/src/assertions.jl index 1480d87..f507264 100644 --- a/src/assertions.jl +++ b/src/assertions.jl @@ -6,7 +6,6 @@ Parsers that do not consume any input can inherit `Assertion{S,T}`. abstract type Assertion{S,T} <: CombinedParser{S,T} end @inline _leftof(str,i,parser::Assertion,x...) = i @inline _rightof(str,i,parser::Assertion,x...) = i -@inline iterate_state(t::Assertion{MatchState}, str, till, posi, next_i, state::MatchState) = nothing """ Base.get(parser::Assertion{MatchState, <:Assertion}, sequence, till, after, i, state) @@ -104,6 +103,7 @@ iterate_state(parser::Always, str, till, posi, next_i, s::Nothing) = Base.show(io::IO, x::Union{AtStart,AtEnd,Never,Always}) = print(io,"re\"",regex_string(x),"\"") +@inline iterate_state(t::Union{AtStart,AtEnd,Never,Always}, str, till, posi, next_i, state::MatchState) = nothing """ An assertion with an inner parser, like WrappedParser interface. @@ -146,6 +146,7 @@ julia> parse(la*AnyChar(),"peek") end end regex_prefix(x::PositiveLookahead) = "(?="*regex_prefix(x.parser) + function iterate_state(t::PositiveLookahead, str, till, posi, next_i, state) r = iterate_state(t.parser, str, till, posi, tuple_pos(state,posi), tuple_state(state)) if r === nothing @@ -194,6 +195,8 @@ function iterate_state(t::NegativeLookahead, str, till, posi, next_i, state::Not end end +@inline iterate_state(t::NegativeLookahead, str, till, posi, next_i, state::MatchState) = nothing + export Lookahead """ diff --git a/src/re.jl b/src/re.jl index cec7c30..4464cd7 100644 --- a/src/re.jl +++ b/src/re.jl @@ -248,9 +248,7 @@ function capture_substring(p::Backreference, sequence::SequenceWithCaptures) SubString(sequence.x, sequence.captures[index][end]) end -@inline function iterate_state(p::Union{Backreference,ParserOptions{<:Backreference}}, - sequence::SequenceWithCaptures, till, - posi, next_i, state::Nothing) +@inline function iterate_state(p::Union{Backreference,ParserOptions{<:Backreference}}, sequence::SequenceWithCaptures, till, posi, next_i, state::Nothing) r = iterate_state_constant( ConstantParser(capture_substring(p, sequence)), sequence, till, posi, next_i, state) @@ -258,14 +256,8 @@ end tuple_pos(r), tuple_pos(r)-next_i end -@inline function iterate_state(p::Union{Backreference,ParserOptions{<:Backreference}}, - sequence::SequenceWithCaptures, till, - posi, next_i, state) +@inline iterate_state(p::Union{Backreference,ParserOptions{<:Backreference}}, sequence::SequenceWithCaptures, till, posi, next_i, state::Int) = return nothing -end - - - iterate_state_condition(p::Backreference, sequence, till, posi, next_i, state) = resolve_index(p, sequence)>0 diff --git a/src/reverse.jl b/src/reverse.jl index 8fef071..bb0eede 100644 --- a/src/reverse.jl +++ b/src/reverse.jl @@ -82,8 +82,11 @@ function iterate_state(t::NegativeLookbehind, str, till, posi, next_i, state::No end end +@inline iterate_state(t::NegativeLookbehind, str, till, posi, next_i, state::MatchState) = + nothing + -iterate_state(t::PositiveLookbehind, str, till, posi, next_i, state::MatchState) = +@inline iterate_state(t::PositiveLookbehind, str, till, posi, next_i, state::MatchState) = nothing function iterate_state(t::PositiveLookbehind, str, till, posi, next_i, state) diff --git a/src/textparse.jl b/src/textparse.jl index c68c767..a66bf40 100644 --- a/src/textparse.jl +++ b/src/textparse.jl @@ -59,19 +59,16 @@ print_constructor(io::IO, x::AbstractTokenParser) = print_constructor(io::IO, x::AbstractTokenParser{<:TextParse.DateTimeToken}) = print(io, x.parser.format) -iterate_state(parser::AbstractTokenParser, sequence, till, before_i, next_i, state) = - iterate_state_token(parser.parser, sequence, till, before_i, next_i, state) - - -iterate_state_token(parser::AbstractToken, sequence, till, before_i, next_i, state) = +iterate_state(parser::AbstractTokenParser, sequence, till, before_i, next_i, state::NCodeunitsState) = nothing -function iterate_state_token(parser::AbstractToken, sequence, till, before_i, next_i, state::Nothing, opts=TextParse.default_opts) - r,next_i_ = tryparsenext(parser, sequence, next_i, till,opts) - if isnull(r) - nothing - else - NCodeunitsState(next_i,next_i_,get(r)) - end + +function iterate_state(parser::AbstractTokenParser, sequence, till, before_i, next_i, state::Nothing, opts=TextParse.default_opts) + r,next_i_ = tryparsenext(parser.parser, sequence, next_i, till,opts) + if isnull(r) + nothing + else + NCodeunitsState(next_i,next_i_,get(r)) + end end """