Skip to content

Commit

Permalink
Export regex struct instead of module
Browse files Browse the repository at this point in the history
Users should not have access to the module directly. Instead, export the RE
struct, and also allow users to construct regex with `RE(str)`.
  • Loading branch information
jakobnissen committed Mar 7, 2023
1 parent 8f8a2b7 commit 6883b0f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/Automa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ if !isdefined(Base, :get_extension)
include("../ext/AutomaStream.jl")
end

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

# This list of exports lists the API
export RE,
Expand Down
5 changes: 4 additions & 1 deletion src/re.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function RE(head::Symbol, args::Vector)
return RE(head, args, nothing, nothing)
end

RE(s::AbstractString) = parse(s)

function actions!(re::RE)
if isnothing(re.actions)
re.actions = Dict{Symbol, Vector{Symbol}}()
Expand Down Expand Up @@ -129,7 +131,8 @@ end
const METACHAR = raw".*+?()[]\|-^"

# Parse a regular expression string using the shunting-yard algorithm.
function parse(str::String)
function parse(str_::AbstractString)
str = String(str_)
# stacks
operands = RE[]
operators = Symbol[]
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ end
@test Automa.generate_exec_code(ctx, machine, :debug) isa Any
end

@testset "Invalid RE.actions keys" begin
@testset "Invalid actions keys" begin
@test_throws Exception let
a = re"abc"
Automa.RegExp.actions!(a)[:badkey] = [:foo]
Expand Down

0 comments on commit 6883b0f

Please sign in to comment.