Skip to content

Commit

Permalink
Merge pull request #26 from yadongli/master
Browse files Browse the repository at this point in the history
fixed deprecation warnings for julia 0.7
  • Loading branch information
staticfloat authored Oct 28, 2018
2 parents de8573c + a28da07 commit f956e63
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: julia
os:
- linux
julia:
- 0.6
- 0.7
- 1.0
notifications:
email: false
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
julia 0.6
julia 0.7
40 changes: 21 additions & 19 deletions src/DocOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ module DocOpt

export docopt

using Printf, Dates

# port of str.partition in Python
function partition(s::AbstractString, delim::AbstractString)
range = search(s, delim)
if length(range) == 0
range = findfirst(delim, s)
if range == nothing
# no match
return s, "", ""
elseif length(range) == 1
Expand Down Expand Up @@ -59,8 +61,8 @@ mutable struct Option <: LeafPattern
function Option(option_description::AbstractString)
short, long, argcount, value = nothing, nothing, 0, false
options, _, description = partition(strip(option_description), " ")
options = replace(options, ',', ' ')
options = replace(options, '=', ' ')
options = replace(options, ',' => ' ')
options = replace(options, '=' => ' ')
for s in split(options)
if startswith(s, "--")
long = s
Expand Down Expand Up @@ -108,13 +110,13 @@ mutable struct Tokens
new(source, error)
end
function Tokens(source::AbstractString, error=DocOptLanguageError)
source = replace(source, r"([\[\]\(\)\|]|\.\.\.)", s -> " " * s * " ")
source = matchall(r"\S*<.*?>|\S+", source)
source = replace(source, r"([\[\]\(\)\|]|\.\.\.)" => s -> " " * s * " ")
source = collect((m.match for m in eachmatch(r"\S*<.*?>|\S+", source)))
new(source, error)
end
end

function Base.iteratorsize(::Type{Tokens})
function IteratorSize(::Type{Tokens})
return Base.SizeUnknown()
end

Expand Down Expand Up @@ -259,7 +261,7 @@ function flat(pattern::BranchPattern, types=[])
if typeof(pattern) in types
return [pattern]
else
return reduce(vcat, Pattern[], [flat(child, types) for child in pattern.children])
return reduce(vcat, [flat(child, types) for child in pattern.children], init=Pattern[])
end
end

Expand All @@ -275,7 +277,7 @@ function fix_identities(pattern::Pattern, uniq=nothing)
uniq = uniq === nothing ? unique(flat(pattern)) : uniq
for (i, child) in enumerate(pattern.children)
if !isa(child, BranchPattern)
pattern.children[i] = uniq[findfirst(uniq, child)]
pattern.children[i] = uniq[something(findfirst(isequal(child), uniq), 0)]
else
fix_identities(child, uniq)
end
Expand Down Expand Up @@ -305,11 +307,11 @@ function transform(pattern::Pattern)
result = Any[]
groups = Any[Pattern[pattern]]
while !isempty(groups)
children = shift!(groups)
children = popfirst!(groups)
parents = [Required, Optional, OptionsShortcut, Either, OneOrMore]
if any(map(t -> t in map(typeof, children), parents))
child = first(filter(c -> typeof(c) in parents, children))
splice!(children, findfirst(children, child))
splice!(children, something(findfirst(isequal(child), children), 0))
if isa(child, Either)
for c in child.children
push!(groups, vcat([c], children))
Expand All @@ -329,11 +331,11 @@ end
Base.hash(pattern::Pattern) = pattern |> string |> hash

Base.getindex(tokens::Tokens, i::Integer) = tokens.tokens[i]
Base.start(tokens::Tokens) = 1
Base.done(tokens::Tokens, i::Int) = i > endof(tokens.tokens)
Base.next(tokens::Tokens, i::Int) = tokens.tokens[i], i + 1
Base.iterate(tokens::Tokens) = isempty(tokens.tokens) ? nothing : (tokens.tokens[1], 2)
Base.iterate(tokens::Tokens, i::Int) = (i > lastindex(tokens.tokens)) ? nothing : (tokens.tokens[i], i+1)
Base.length(tokens::Tokens) = length(tokens.tokens)

move!(tokens::Tokens) = isempty(tokens.tokens) ? nothing : shift!(tokens.tokens)
move!(tokens::Tokens) = isempty(tokens.tokens) ? nothing : popfirst!(tokens.tokens)
current(tokens::Tokens) = isempty(tokens.tokens) ? nothing : tokens[1]

# parsers
Expand Down Expand Up @@ -471,7 +473,7 @@ function parse_atom(tokens, options)
return parse_long(tokens, options)
elseif startswith(token, '-') && !isdash(token)
return parse_shorts(tokens, options)
elseif startswith(token, '<') && endswith(token, '>') || all(isupper, token)
elseif startswith(token, '<') && endswith(token, '>') || all(isuppercase, token)
return [Argument(move!(tokens))]
else
return [Command(move!(tokens))]
Expand Down Expand Up @@ -504,7 +506,7 @@ end

function parse_section(name, source)
pattern = Regex("^([^\\n]*$name[^\\n]*\\n?(?:[ \\t].*?(?:\\n|\$))*)", "im")
map(strip, matchall(pattern, source))
map(strip, collect((m.match for m in eachmatch(pattern, source))))
end

function parse_defaults(doc)
Expand All @@ -523,7 +525,7 @@ end
function formal_usage(section)
_, _, section = partition(section, ':')
words = split(strip(section))
program = shift!(words)
program = popfirst!(words)
patterns = AbstractString[]
for w in words
if w == program
Expand Down Expand Up @@ -601,7 +603,7 @@ function docopt(doc::AbstractString,
return ret
end
if exit_on_error
@printf(STDERR, "%s\n", docoptexit.usage)
@printf(stderr, "%s\n", docoptexit.usage)
isinteractive() || exit(1)
else
throw(docoptexit)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Base.Test
using Test

using DocOpt # import docopt method
import DocOpt:
Expand Down

0 comments on commit f956e63

Please sign in to comment.