Skip to content

Commit 3b9b0ec

Browse files
committed
Move things to modules
1 parent 6d644da commit 3b9b0ec

File tree

4 files changed

+110
-101
lines changed

4 files changed

+110
-101
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Note that this is not how the values are actually stored, because a `Table` can
113113

114114
Note that you can use any kind of value in a boolean context and convert any value to `string`.
115115

116-
Look [in the source code](src/docopt.nim#L30) to find out more about these conversions.
116+
Look [in the source code](src/private/value.nim) to find out more about these conversions.
117117

118118

119119
Installation

src/docopt.nim

+2-99
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,12 @@
33
# Licensed under terms of MIT license (see LICENSE)
44

55

6-
import re, sequtils, strutils, macros, os, tables
6+
import re, sequtils, os, tables
77
import private/util
88

99
export tables
1010

11-
12-
type
13-
ValueKind* = enum
14-
vkNone, ## No value
15-
vkBool, ## A boolean
16-
vkInt, ## An integer
17-
vkStr, ## A string
18-
vkList ## A list of strings
19-
Value* = object ## docopt variant type
20-
case kind*: ValueKind
21-
of vkNone:
22-
nil
23-
of vkBool:
24-
bool_v: bool
25-
of vkInt:
26-
int_v: int
27-
of vkStr:
28-
str_v: string
29-
of vkList:
30-
list_v: seq[string]
31-
32-
33-
converter to_bool*(v: Value): bool =
34-
## Convert a Value to bool, depending on its kind:
35-
## - vkNone: false
36-
## - vkBool: boolean value itself
37-
## - vkInt: true if integer is not zero
38-
## - vkStr: true if string is not empty
39-
## - vkList: true if sequence is not empty
40-
case v.kind
41-
of vkNone: false
42-
of vkBool: v.bool_v
43-
of vkInt: v.int_v != 0
44-
of vkStr: v.str_v != nil and v.str_v.len > 0
45-
of vkList: not v.list_v.is_nil and v.list_v.len > 0
46-
47-
proc len*(v: Value): int =
48-
## Return the integer of a vkInt Value
49-
## or the length of the seq of a vkList value.
50-
## It is an error to use it on other kinds of Values.
51-
if v.kind == vkInt: v.int_v
52-
else: v.list_v.len
53-
54-
proc `@`*(v: Value): seq[string] =
55-
## Return the seq of a vkList Value.
56-
## It is an error to use it on other kinds of Values.
57-
v.list_v
58-
59-
proc `[]`*(v: Value, i: int): string =
60-
## Return the i-th item of the seq of a vkList Value.
61-
## It is an error to use it on other kinds of Values.
62-
v.list_v[i]
63-
64-
65-
proc val(): Value = Value(kind: vkNone)
66-
proc val(v: bool): Value = Value(kind: vkBool, bool_v: v)
67-
proc val(v: int): Value = Value(kind: vkInt, int_v: v)
68-
proc val(v: string): Value = Value(kind: vkStr, str_v: v)
69-
proc val(v: seq[string]): Value = Value(kind: vkList, list_v: v)
70-
71-
72-
proc str(s: string): string =
73-
if s.is_nil: "nil"
74-
else: "\"" & s.replace("\"", "\\\"") & "\""
75-
76-
proc str[T](s: seq[T]): string =
77-
if s.is_nil: "nil"
78-
else: "[" & s.map_it(string, it.str).join(", ") & "]"
79-
80-
proc str(v: Value): string =
81-
case v.kind
82-
of vkNone: "nil"
83-
of vkStr: v.str_v.str
84-
of vkInt: $v.int_v
85-
of vkBool: $v.bool_v
86-
of vkList: v.list_v.str
87-
88-
proc `$`*(v: Value): string =
89-
## Return the string of a vkStr Value,
90-
## or the item of a vkList Value, if there is exactly one,
91-
## or a string representation of any other kind of Value.
92-
if v.kind == vkStr:
93-
v.str_v
94-
elif v.kind == vkList and
95-
not v.list_v.is_nil and v.list_v.len == 1:
96-
v.list_v[0]
97-
else: v.str
98-
99-
proc `==`*(a, b: Value): bool =
100-
a.kind == b.kind and a.str == b.str
11+
include private/value
10112

10213

10314
type
@@ -108,14 +19,6 @@ type
10819
usage*: string
10920

11021

111-
macro gen_class(body: stmt): stmt {.immediate.} =
112-
for typ in body[0].children:
113-
body.add(parse_stmt(
114-
"""method class(self: $1): string = "$1"""".format(typ[0])
115-
))
116-
body
117-
118-
11922
gen_class:
12023
type
12124
Pattern = ref object of RootObj

src/private/util.nim

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under terms of MIT license (see LICENSE)
33

44

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

77

88
template any_it*(lst, pred: expr): expr =
@@ -62,3 +62,13 @@ proc sub*[T](s: seq[T], a, b: int): seq[T] =
6262
## Items from `a` to `b` non-inclusive
6363
if a < b: s[a .. <b]
6464
else: @[]
65+
66+
67+
macro gen_class*(body: stmt): stmt {.immediate.} =
68+
## When applied to a type block, this will generate methods
69+
## that return each type's name as a string.
70+
for typ in body[0].children:
71+
body.add(parse_stmt(
72+
"""method class(self: $1): string = "$1"""".format(typ[0])
73+
))
74+
body

src/private/value.nim

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright (C) 2015 Oleh Prypin <blaxpirit@gmail.com>
2+
# Licensed under terms of MIT license (see LICENSE)
3+
4+
5+
import strutils, sequtils
6+
7+
8+
type
9+
ValueKind* = enum
10+
vkNone, ## No value
11+
vkBool, ## A boolean
12+
vkInt, ## An integer
13+
vkStr, ## A string
14+
vkList ## A list of strings
15+
Value* = object ## docopt variant type
16+
case kind*: ValueKind
17+
of vkNone:
18+
nil
19+
of vkBool:
20+
bool_v: bool
21+
of vkInt:
22+
int_v: int
23+
of vkStr:
24+
str_v: string
25+
of vkList:
26+
list_v: seq[string]
27+
28+
29+
converter to_bool*(v: Value): bool =
30+
## Convert a Value to bool, depending on its kind:
31+
## - vkNone: false
32+
## - vkBool: boolean value itself
33+
## - vkInt: true if integer is not zero
34+
## - vkStr: true if string is not empty
35+
## - vkList: true if sequence is not empty
36+
case v.kind
37+
of vkNone: false
38+
of vkBool: v.bool_v
39+
of vkInt: v.int_v != 0
40+
of vkStr: v.str_v != nil and v.str_v.len > 0
41+
of vkList: not v.list_v.is_nil and v.list_v.len > 0
42+
43+
proc len*(v: Value): int =
44+
## Return the integer of a vkInt Value
45+
## or the length of the seq of a vkList value.
46+
## It is an error to use it on other kinds of Values.
47+
if v.kind == vkInt: v.int_v
48+
else: v.list_v.len
49+
50+
proc `@`*(v: Value): seq[string] =
51+
## Return the seq of a vkList Value.
52+
## It is an error to use it on other kinds of Values.
53+
v.list_v
54+
55+
proc `[]`*(v: Value, i: int): string =
56+
## Return the i-th item of the seq of a vkList Value.
57+
## It is an error to use it on other kinds of Values.
58+
v.list_v[i]
59+
60+
61+
proc str(s: string): string =
62+
if s.is_nil: "nil"
63+
else: "\"" & s.replace("\"", "\\\"") & "\""
64+
65+
proc str[T](s: seq[T]): string =
66+
if s.is_nil: "nil"
67+
else: "[" & s.map_it(string, it.str).join(", ") & "]"
68+
69+
proc str(v: Value): string =
70+
case v.kind
71+
of vkNone: "nil"
72+
of vkStr: v.str_v.str
73+
of vkInt: $v.int_v
74+
of vkBool: $v.bool_v
75+
of vkList: v.list_v.str
76+
77+
proc `$`*(v: Value): string =
78+
## Return the string of a vkStr Value,
79+
## or the item of a vkList Value, if there is exactly one,
80+
## or a string representation of any other kind of Value.
81+
if v.kind == vkStr:
82+
v.str_v
83+
elif v.kind == vkList and
84+
not v.list_v.is_nil and v.list_v.len == 1:
85+
v.list_v[0]
86+
else: v.str
87+
88+
proc `==`*(a, b: Value): bool =
89+
a.kind == b.kind and a.str == b.str
90+
91+
92+
proc val(): Value = Value(kind: vkNone)
93+
proc val(v: bool): Value = Value(kind: vkBool, bool_v: v)
94+
proc val(v: int): Value = Value(kind: vkInt, int_v: v)
95+
proc val(v: string): Value = Value(kind: vkStr, str_v: v)
96+
proc val(v: seq[string]): Value = Value(kind: vkList, list_v: v)

0 commit comments

Comments
 (0)