|
3 | 3 | # Licensed under terms of MIT license (see LICENSE)
|
4 | 4 |
|
5 | 5 |
|
6 |
| -import re, sequtils, strutils, macros, os, tables |
| 6 | +import re, sequtils, os, tables |
7 | 7 | import private/util
|
8 | 8 |
|
9 | 9 | export tables
|
10 | 10 |
|
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 |
101 | 12 |
|
102 | 13 |
|
103 | 14 | type
|
|
108 | 19 | usage*: string
|
109 | 20 |
|
110 | 21 |
|
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 |
| - |
119 | 22 | gen_class:
|
120 | 23 | type
|
121 | 24 | Pattern = ref object of RootObj
|
|
0 commit comments