Skip to content

Commit

Permalink
Fix deprecations (mapIt, base methods)
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Oct 15, 2015
1 parent 2323fd9 commit 29fbcba
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
29 changes: 14 additions & 15 deletions src/docopt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# Licensed under terms of MIT license (see LICENSE)


import nre, options, sequtils, os, tables
import nre, options, os, tables
from sequtils import deduplicate, delete, filter_it
import private/util

export tables
Expand All @@ -18,7 +19,6 @@ type
## Exit in case user invoked program with incorrect arguments.
usage*: string


gen_class:
type
Pattern = ref object of RootObj
Expand Down Expand Up @@ -88,25 +88,25 @@ type

{.warning[LockLevel]: off.}

method str(self: Pattern): string =
method str(self: Pattern): string {.base.} =
assert false

method name(self: Pattern): string =
method name(self: Pattern): string {.base.} =
self.m_name
method `name=`(self: Pattern, name: string) =
method `name=`(self: Pattern, name: string) {.base.} =
self.m_name = name

method `==`(self, other: Pattern): bool =
method `==`(self, other: Pattern): bool {.base.} =
self.str == other.str

method flat(self: Pattern, types: varargs[string]): seq[Pattern] =
method flat(self: Pattern, types: varargs[string]): seq[Pattern] {.base.} =
assert false

method match(self: Pattern, left: seq[Pattern],
collected: seq[Pattern] = @[]): MatchResult =
collected: seq[Pattern] = @[]): MatchResult {.base.} =
assert false

method fix_identities(self: Pattern, uniq: seq[Pattern]) =
method fix_identities(self: Pattern, uniq: seq[Pattern]) {.base.} =
## Make pattern-tree tips point to same object if they are equal.
if self.children.is_nil:
return
Expand All @@ -117,10 +117,10 @@ method fix_identities(self: Pattern, uniq: seq[Pattern]) =
else:
child.fix_identities(uniq)

method fix_identities(self: Pattern) =
method fix_identities(self: Pattern) {.base.} =
self.fix_identities(self.flat().deduplicate())

method either(self: Pattern): Either =
method either(self: Pattern): Either {.base.} =
## Transform pattern into an equivalent, with only top-level Either.
# Currently the pattern will not be equivalent, but more "narrow",
# although good enough to reason about list arguments.
Expand Down Expand Up @@ -150,7 +150,7 @@ method either(self: Pattern): Either =
ret.add children
either(ret.map_it(Pattern, required(it)))

method fix_repeating_arguments(self: Pattern) =
method fix_repeating_arguments(self: Pattern) {.base.} =
## Fix elements that should accumulate/increment values.
var either: seq[seq[Pattern]] = @[]
for child in self.either.children:
Expand All @@ -169,7 +169,7 @@ method fix_repeating_arguments(self: Pattern) =
e.class == "Option" and Option(e).argcount == 0:
e.value = val(0)

method fix(self: Pattern) =
method fix(self: Pattern) {.base.} =
self.fix_identities()
self.fix_repeating_arguments()

Expand All @@ -181,7 +181,7 @@ method flat(self: ChildPattern, types: varargs[string]): seq[Pattern] =
if types.len == 0 or self.class in types: @[Pattern(self)] else: @[]

method single_match(self: ChildPattern,
left: seq[Pattern]): SingleMatchResult =
left: seq[Pattern]): SingleMatchResult {.base.} =
assert false

method match(self: ChildPattern, left: seq[Pattern],
Expand Down Expand Up @@ -258,7 +258,6 @@ proc option_parse[T](
else:
argcount = 1
if argcount > 0:
var matched = @[""]
var m = description.find(re"(?i)\[default:\ (.*)\]")
if m.is_some:
value = val(m.get.captures[0])
Expand Down
21 changes: 15 additions & 6 deletions src/private/util.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@
# Licensed under terms of MIT license (see LICENSE)


import sequtils, strutils, macros
import strutils, macros


template any_it*(lst, pred: expr): expr =
## Does `pred` return true for any of the items of an iterable?
var result = false
## Does `pred` return true for any of the `it`s of `lst`?
var result {.gensym.} = false
for it {.inject.} in lst:
if pred:
result = true
break
result

template map_it*(lst, typ, op: expr): expr =
## Returns `seq[typ]` that contains `op` applied to each `it` of `lst`
var result {.gensym.}: seq[typ] = @[]
for it {.inject.} in items(lst):
result.add(op)
result


proc count*[T](s: openarray[T], it: T): int =
## How many times this item appears in an array
Expand Down Expand Up @@ -44,7 +51,9 @@ macro gen_class*(body: stmt): stmt {.immediate.} =
## When applied to a type block, this will generate methods
## that return each type's name as a string.
for typ in body[0].children:
body.add(parse_stmt(
"""method class(self: $1): string = "$1"""".format(typ[0])
))
var meth = "method class(self: $1): string"
if $typ[2][0][1][0] == "RootObj":
meth &= "{.base.}"
meth &= "= \"$1\""
body.add(parse_stmt(meth.format(typ[0])))
body
3 changes: 2 additions & 1 deletion src/private/value.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Licensed under terms of MIT license (see LICENSE)


import strutils, sequtils
import strutils
import private/util


type
Expand Down

0 comments on commit 29fbcba

Please sign in to comment.