Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Parsers a weakdep #67

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ version = "1.4.0"
[deps]
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"

[weakdeps]
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"

[extensions]
ParsersExt = "Parsers"

[extras]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Expand All @@ -16,4 +22,4 @@ Parsers = "2"
julia = "1.6"

[targets]
test = ["Test", "Random", "Serialization"]
test = ["Test", "Random", "Serialization", "Parsers"]
67 changes: 67 additions & 0 deletions ext/ParsersExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module ParsersExt
using Parsers
using InlineStrings: InlineString, InlineString1, addcodeunit

Parsers.xparse(::Type{T}, buf::AbstractString, pos, len, options, ::Type{S}=T) where {T <: InlineString, S} =
Parsers.xparse(T, codeunits(buf), pos, len, options, S)

function Parsers.xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, options::Parsers.Options, ::Type{S}=T) where {T <: InlineString, S}
res = Parsers.xparse(String, source, pos, len, options, PosLen)
code = res.code
overflowed = false
poslen = res.val
if !Parsers.valueok(code) || Parsers.sentinel(code)
x = T()
else
poslen = res.val
if T === InlineString1
if poslen.len != 1
overflowed = true
x = T()
else
Parsers.fastseek!(source, poslen.pos)
x = InlineString1(Parsers.peekbyte(source, poslen.pos))
Parsers.fastseek!(source, pos + res.tlen - 1)
end
elseif Parsers.escapedstring(code) || !(source isa AbstractVector{UInt8})
if poslen.len > (sizeof(T) - 1)
overflowed = true
x = T()
else
# manually build up InlineString
i = poslen.pos
maxi = i + poslen.len
x = T()
Parsers.fastseek!(source, i - 1)
while i < maxi
b = Parsers.peekbyte(source, i)
if b == options.e
i += 1
Parsers.incr!(source)
b = Parsers.peekbyte(source, i)
end
x, overflowed = addcodeunit(x, b)
i += 1
Parsers.incr!(source)
end
Parsers.fastseek!(source, maxi)
end
else
vlen = poslen.len
if vlen > (sizeof(T) - 1)
# @show T, vlen, sizeof(T)
overflowed = true
x = T()
else
# @show poslen.pos, vlen
x = T(source, poslen.pos, vlen)
end
end
end
if overflowed
code |= Parsers.OVERFLOW
end
return Parsers.Result{S}(code, res.tlen, x)
end

end
69 changes: 4 additions & 65 deletions src/InlineStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module InlineStrings

import Base: ==

using Parsers

export InlineString, InlineStringType, inlinestrings
export @inline_str

Expand Down Expand Up @@ -861,69 +859,6 @@ end
end
end

# Parsers.xparse
Parsers.xparse(::Type{T}, buf::AbstractString, pos, len, options, ::Type{S}=T) where {T <: InlineString, S} =
Parsers.xparse(T, codeunits(buf), pos, len, options, S)

function Parsers.xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, options::Parsers.Options, ::Type{S}=T) where {T <: InlineString, S}
res = Parsers.xparse(String, source, pos, len, options, PosLen)
code = res.code
overflowed = false
poslen = res.val
if !Parsers.valueok(code) || Parsers.sentinel(code)
x = T()
else
poslen = res.val
if T === InlineString1
if poslen.len != 1
overflowed = true
x = T()
else
Parsers.fastseek!(source, poslen.pos)
x = InlineString1(Parsers.peekbyte(source, poslen.pos))
Parsers.fastseek!(source, pos + res.tlen - 1)
end
elseif Parsers.escapedstring(code) || !(source isa AbstractVector{UInt8})
if poslen.len > (sizeof(T) - 1)
overflowed = true
x = T()
else
# manually build up InlineString
i = poslen.pos
maxi = i + poslen.len
x = T()
Parsers.fastseek!(source, i - 1)
while i < maxi
b = Parsers.peekbyte(source, i)
if b == options.e
i += 1
Parsers.incr!(source)
b = Parsers.peekbyte(source, i)
end
x, overflowed = addcodeunit(x, b)
i += 1
Parsers.incr!(source)
end
Parsers.fastseek!(source, maxi)
end
else
vlen = poslen.len
if vlen > (sizeof(T) - 1)
# @show T, vlen, sizeof(T)
overflowed = true
x = T()
else
# @show poslen.pos, vlen
x = T(source, poslen.pos, vlen)
end
end
end
if overflowed
code |= Parsers.OVERFLOW
end
return Parsers.Result{S}(code, res.tlen, x)
end

## InlineString sorting
using Base.Sort, Base.Order

Expand Down Expand Up @@ -1121,4 +1056,8 @@ Base.Broadcast.broadcasted(::Type{InlineString}, A::AbstractArray) = inlinestrin
Base.map(::Type{InlineString}, A::AbstractArray) = inlinestrings(A)
Base.collect(::Type{InlineString}, A::AbstractArray) = inlinestrings(A)

if !isdefined(Base, :get_extension)
include("../ext/ParsersExt.jl")
end

end # module