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 #78

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 14 additions & 9 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@ version = "1.4.1"
[deps]
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"

[extras]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[weakdeps]
ArrowTypes = "31f734f8-188a-4ce0-8406-c8a06bd891cd"
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"

[compat]
Parsers = "2"
julia = "1.6"

[weakdeps]
ArrowTypes = "31f734f8-188a-4ce0-8406-c8a06bd891cd"

[extensions]
ArrowTypesExt = "ArrowTypes"
ParsersExt = "Parsers"
Copy link
Member

Choose a reason for hiding this comment

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

Question for my own edification, not really related to the PR: If two packages (say InlineStrings and SaxophoneHorse) have extensions for Parsers, both call their extension module ParsersExt, and you load InlineStrings, SaxophoneHorse, and Parsers, what happens? That is, does the convention of naming an extension module <main package><weakdep>Ext provide functional disambiguation or is it more for the benefit of the human?

Copy link
Member

Choose a reason for hiding this comment

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

Never mind, mostly answered my own question. Extension modules having the same name doesn't cause any issues, so I suppose the naming convention is more for the human's benefit.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, they have different UUID so the loading system can distinguish them.


[extras]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"


ararslan marked this conversation as resolved.
Show resolved Hide resolved

[targets]
test = ["Arrow", "Test", "Random", "Serialization"]
test = ["Arrow", "Test", "Parsers", "Random", "Serialization"]
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 @@ -878,69 +876,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 @@ -1138,4 +1073,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
Loading