Skip to content

Commit

Permalink
Fix escaping - and ^ inside character classes. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcjones authored and bicycle1885 committed Jul 9, 2018
1 parent 7a0791c commit 3c98910
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/re.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ macro re_str(s::String)
return parse(unescape_string(escape_re_string(s)))
end

const METACHAR = ".*+?()[]\\|"
const METACHAR = ".*+?()[]\\|-^"

function escape_re_string(str::String)
buf = IOBuffer()
Expand Down Expand Up @@ -237,7 +237,7 @@ function prec(op::Symbol)
end

function parse_class(str, s)
chars = []
chars = Tuple{Bool, Char}[]
while !done(str, s)
c, s = next(str, s)
if c == ']'
Expand All @@ -247,12 +247,12 @@ function parse_class(str, s)
error("missing ]")
end
c, s = next(str, s)
push!(chars, c)
push!(chars, (true, c))
else
push!(chars, c)
push!(chars, (false, c))
end
end
if !isempty(chars) && first(chars) == '^'
if !isempty(chars) && !first(chars)[1] && first(chars)[2] == '^'
head = :cclass
popfirst!(chars)
else
Expand All @@ -264,9 +264,9 @@ function parse_class(str, s)

args = []
while !isempty(chars)
c = popfirst!(chars)
if !isempty(chars) && first(chars) == '-' && length(chars) 2
push!(args, UInt8(c):UInt8(chars[2]))
c = popfirst!(chars)[2]
if !isempty(chars) && !first(chars)[1] && first(chars)[2] == '-' && length(chars) 2
push!(args, UInt8(c):UInt8(chars[2][2]))
popfirst!(chars)
popfirst!(chars)
else
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ include("test13.jl")
include("test14.jl")
include("test15.jl")
include("test16.jl")
include("test17.jl")

module TestFASTA

Expand Down
58 changes: 58 additions & 0 deletions test/test17.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Test17

if VERSION >= v"0.7-"
using Test
else
using Base.Test
end
import Automa
import Automa.RegExp: @re_str
import Compat: lastindex, occursin

@testset "Test17" begin
re1 = re"[a\-c]"
re1.actions[:enter] = [:enter]
re1.actions[:exit] = [:exit]
machine1 = Automa.compile(re1)
@test occursin(r"^Automa.Machine\(<.*>\)$", repr(machine1))

for generator in (:table, :inline, :goto), checkbounds in (true, false), clean in (true, false)
ctx = Automa.CodeGenContext(generator=generator, checkbounds=checkbounds, clean=clean)
init_code = Automa.generate_init_code(ctx, machine1)
exec_code = Automa.generate_exec_code(ctx, machine1, :debug)
validate = @eval function (data)
logger = Symbol[]
$(init_code)
p_end = p_eof = lastindex(data)
$(exec_code)
return cs == 0, logger
end

@test validate(b"-") == (true, [:enter, :exit])
@test validate(b"b") == (false, Symbol[])
end

re2 = re"[a-c]"
re2.actions[:enter] = [:enter]
re2.actions[:exit] = [:exit]
machine2 = Automa.compile(re2)
@test occursin(r"^Automa.Machine\(<.*>\)$", repr(machine2))

for generator in (:table, :inline, :goto), checkbounds in (true, false), clean in (true, false)
ctx = Automa.CodeGenContext(generator=generator, checkbounds=checkbounds, clean=clean)
init_code = Automa.generate_init_code(ctx, machine2)
exec_code = Automa.generate_exec_code(ctx, machine2, :debug)
validate = @eval function (data)
logger = Symbol[]
$(init_code)
p_end = p_eof = lastindex(data)
$(exec_code)
return cs == 0, logger
end

@test validate(b"-") == (false, [])
@test validate(b"b") == (true, Symbol[:enter, :exit])
end
end

end

0 comments on commit 3c98910

Please sign in to comment.