Skip to content

Commit

Permalink
disambiguate iterate_state
Browse files Browse the repository at this point in the history
  • Loading branch information
gkappler committed Sep 1, 2021
1 parent c250148 commit 53b1e54
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
5 changes: 2 additions & 3 deletions src/CombinedParsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand All @@ -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)


"""
Expand Down
5 changes: 4 additions & 1 deletion src/assertions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

"""
Expand Down
12 changes: 2 additions & 10 deletions src/re.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,24 +248,16 @@ 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)
r === nothing && return nothing
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
Expand Down
5 changes: 4 additions & 1 deletion src/reverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 9 additions & 12 deletions src/textparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
Expand Down

2 comments on commit 53b1e54

@gkappler
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/43948

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.1 -m "<description of version>" 53b1e54bba02aa8b4aaa3279c657dd6cb7f6da21
git push origin v0.2.1

Please sign in to comment.