Skip to content

Commit

Permalink
Add SnoopPrecompile
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed Mar 8, 2023
1 parent 87d1f83 commit e5b22a5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
12 changes: 7 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
name = "Automa"
uuid = "67c07d97-cdcb-5c2c-af73-a7f9c32a568b"
authors = ["Kenta Sato <[email protected]>", "Jakob Nybo Nissen <[email protected]"]
version = "0.8.2"

[weakdeps]
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
version = "1.0.0"

[deps]
ScanByte = "7b38b023-a4d7-4c5e-8d43-3f3097f304eb"
SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

[weakdeps]
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

[extensions]
AutomaStream = "TranscodingStreams"

[compat]
ScanByte = "0.3.3"
SnoopPrecompile = "1"
TranscodingStreams = "0.9"
julia = "1.5"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
2 changes: 2 additions & 0 deletions src/Automa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ end

using .RegExp: RE, @re_str, opt, rep, rep1, onenter!, onexit!, onall!, onfinal!, precond!

include("workload.jl")

# This list of exports lists the API
export RE,
@re_str,
Expand Down
4 changes: 2 additions & 2 deletions src/tokenizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ end

# By default, the counter C is 1
Tokenizer{E}(data) where E = Tokenizer{E, typeof(data), 1}(data)
Base.IteratorSize(::Type{<:Tokenizer}) = Base.SizeUnknown()
Base.eltype(::Type{<:Tokenizer{E}}) where E = Tuple{Int, Int32, E}
Base.IteratorSize(::Type{Tokenizer{E, D, C}}) where {E, D, C} = Base.SizeUnknown()
Base.eltype(::Type{Tokenizer{E, D, C}}) where {E, D, C} = Tuple{Int, Int32, E}

"""
tokenize(::Type{E}, data, version=1)
Expand Down
44 changes: 44 additions & 0 deletions src/workload.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using SnoopPrecompile: @precompile_all_calls

@precompile_all_calls begin
let
# Create a buffer validator
regex = let
name = onexit!(onenter!(re"[^\t\r\n]+", :mark), :name)
field = onexit!(onenter!(re"[^\t\r\n]+", :mark), :field)
nameline = name * rep('\t' * name)
record = onexit!(field * rep('\t' * field), :record)
nameline * re"\r?\n" * record * rep(re"\r?\n" * record) * rep(re"\r?\n")
end
generate_buffer_validator(:foo, regex)

# Create an ordinary parser
machine = compile(regex)
actions = Dict(
:mark => :(pos = p),
:name => :(push!(headers, String(data[pos:p-1]))),
:field => quote
n_fields += 1
push!(fields, String(data[pos:p-1]))
end,
:record => quote
n_fields == length(headers) || error("Malformed TSV")
n_fields = 0
end
)

generate_code(machine, actions)

# Create a tokenizer
tokens = [
re"[A-Za-z_][0-9A-Za-z_!]*!",
re"\(",
re"\)",
re",",
re"abc",
re"\"",
re"[\t\f ]+",
];
make_tokenizer(tokens)
end
end

0 comments on commit e5b22a5

Please sign in to comment.