Skip to content

Commit 29fbcba

Browse files
committed
Fix deprecations (mapIt, base methods)
1 parent 2323fd9 commit 29fbcba

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

src/docopt.nim

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# Licensed under terms of MIT license (see LICENSE)
44

55

6-
import nre, options, sequtils, os, tables
6+
import nre, options, os, tables
7+
from sequtils import deduplicate, delete, filter_it
78
import private/util
89

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

21-
2222
gen_class:
2323
type
2424
Pattern = ref object of RootObj
@@ -88,25 +88,25 @@ type
8888

8989
{.warning[LockLevel]: off.}
9090

91-
method str(self: Pattern): string =
91+
method str(self: Pattern): string {.base.} =
9292
assert false
9393

94-
method name(self: Pattern): string =
94+
method name(self: Pattern): string {.base.} =
9595
self.m_name
96-
method `name=`(self: Pattern, name: string) =
96+
method `name=`(self: Pattern, name: string) {.base.} =
9797
self.m_name = name
9898

99-
method `==`(self, other: Pattern): bool =
99+
method `==`(self, other: Pattern): bool {.base.} =
100100
self.str == other.str
101101

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

105105
method match(self: Pattern, left: seq[Pattern],
106-
collected: seq[Pattern] = @[]): MatchResult =
106+
collected: seq[Pattern] = @[]): MatchResult {.base.} =
107107
assert false
108108

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

120-
method fix_identities(self: Pattern) =
120+
method fix_identities(self: Pattern) {.base.} =
121121
self.fix_identities(self.flat().deduplicate())
122122

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

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

172-
method fix(self: Pattern) =
172+
method fix(self: Pattern) {.base.} =
173173
self.fix_identities()
174174
self.fix_repeating_arguments()
175175

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

183183
method single_match(self: ChildPattern,
184-
left: seq[Pattern]): SingleMatchResult =
184+
left: seq[Pattern]): SingleMatchResult {.base.} =
185185
assert false
186186

187187
method match(self: ChildPattern, left: seq[Pattern],
@@ -258,7 +258,6 @@ proc option_parse[T](
258258
else:
259259
argcount = 1
260260
if argcount > 0:
261-
var matched = @[""]
262261
var m = description.find(re"(?i)\[default:\ (.*)\]")
263262
if m.is_some:
264263
value = val(m.get.captures[0])

src/private/util.nim

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22
# Licensed under terms of MIT license (see LICENSE)
33

44

5-
import sequtils, strutils, macros
5+
import strutils, macros
66

77

88
template any_it*(lst, pred: expr): expr =
9-
## Does `pred` return true for any of the items of an iterable?
10-
var result = false
9+
## Does `pred` return true for any of the `it`s of `lst`?
10+
var result {.gensym.} = false
1111
for it {.inject.} in lst:
1212
if pred:
1313
result = true
1414
break
1515
result
1616

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

1825
proc count*[T](s: openarray[T], it: T): int =
1926
## How many times this item appears in an array
@@ -44,7 +51,9 @@ macro gen_class*(body: stmt): stmt {.immediate.} =
4451
## When applied to a type block, this will generate methods
4552
## that return each type's name as a string.
4653
for typ in body[0].children:
47-
body.add(parse_stmt(
48-
"""method class(self: $1): string = "$1"""".format(typ[0])
49-
))
54+
var meth = "method class(self: $1): string"
55+
if $typ[2][0][1][0] == "RootObj":
56+
meth &= "{.base.}"
57+
meth &= "= \"$1\""
58+
body.add(parse_stmt(meth.format(typ[0])))
5059
body

src/private/value.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
# Licensed under terms of MIT license (see LICENSE)
33

44

5-
import strutils, sequtils
5+
import strutils
6+
import private/util
67

78

89
type

0 commit comments

Comments
 (0)