Skip to content

Commit

Permalink
Make generate_buffer_validator goto into kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed Mar 9, 2023
1 parent 56cbb5f commit 079e24b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,21 @@ end
const DefaultCodeGenContext = CodeGenContext()

"""
generate_buffer_validator(name::Symbol, regexp::RE, goto=false)
generate_buffer_validator(name::Symbol, regexp::RE; goto=true; docstring=true)
Generate code that, when evaluated, defines a function named `name`, which takes a
single argument `data`, interpreted as a sequence of bytes.
The function returns `nothing` if `data` matches `Machine`, else the index of the first
invalid byte. If the machine reached unexpected EOF, returns `0`.
If `goto`, the function uses the faster but more complicated `:goto` code.
If `docstring`, automatically create a docstring for the generated function.
"""
function generate_buffer_validator(name::Symbol, regex::RegExp.RE, goto::Bool=false; docstring::Bool=true)
function generate_buffer_validator(
name::Symbol,
regex::RegExp.RE;
goto::Bool=true,
docstring::Bool=true
)
ctx = goto ? CodeGenContext(generator=:goto) : DefaultCodeGenContext
machine = compile(RegExp.strip_actions(regex))
code = quote
Expand Down
2 changes: 1 addition & 1 deletion test/simd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using Automa

context = CodeGenContext(generator=:goto)

eval(generate_buffer_validator(:is_valid_fasta, regex, true))
eval(generate_buffer_validator(:is_valid_fasta, regex; goto=true))

s1 = ">seq\nTAGGCTA\n>hello\nAJKGMP"
s2 = ">seq1\nTAGGC"
Expand Down
2 changes: 1 addition & 1 deletion test/test13.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using Test
(!re"A[BC]D?E", ["ABCDE", "ABCE"], ["ABDE", "ACE", "ABE"])
]
for goto in (false, true)
@eval $(Automa.generate_buffer_validator(:validate, regex, goto; docstring=false))
@eval $(Automa.generate_buffer_validator(:validate, regex; goto=goto, docstring=false))
for string in good_strings
@test validate(string) === nothing
end
Expand Down
2 changes: 1 addition & 1 deletion test/test18.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Test
@testset "Test18" begin
regex = re"\0\a\b\t\n\v\r\x00\xff\xFF[\\][^\\]"
for goto in (false, true)
@eval $(Automa.generate_buffer_validator(:validate, regex, goto; docstring=false))
@eval $(Automa.generate_buffer_validator(:validate, regex; goto=goto, docstring=false))

# Bad input types
@test_throws Exception validate(18)
Expand Down
4 changes: 2 additions & 2 deletions test/validator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ using Test

@testset "Validator" begin
regex = re"a(bc)*|(def)|x+" | re"def" | re"x+"
eval(Automa.generate_buffer_validator(:foobar, regex, false))
eval(Automa.generate_buffer_validator(:barfoo, regex, true))
eval(Automa.generate_buffer_validator(:foobar, regex; goto=false))
eval(Automa.generate_buffer_validator(:barfoo, regex; goto=true))

eval(Automa.generate_io_validator(:io_bar, regex; goto=false))
eval(Automa.generate_io_validator(:io_foo, regex; goto=true))
Expand Down

0 comments on commit 079e24b

Please sign in to comment.