88
99 Object * = ((object or tuple ) and not KdlSome )
1010 List * = (array or seq )
11- Value * = (SomeNumber or string or bool )
11+ Value * = (SomeNumber or string or bool or range )
1212 KdlSome * = (KdlDoc or KdlNode or KdlVal )
1313 SomeTable * [K, V] = (Table [K, V] or OrderedTable [K, V])
1414
15- template fail * (msg: string ) =
15+ template fail * (msg: string ) =
1616 raise newException (KdlError , msg)
1717
18- template check * (cond: untyped , msg = " " ) =
18+ template check * (cond: untyped , msg = " " ) =
1919 if not cond:
2020 let txt = msg
2121 fail astToStr (cond) & " failed" & (if txt.len > 0 : " : " & txt else : " " )
@@ -50,12 +50,12 @@ proc eqIdent*(v, a: openarray[char], ignoreChars = {'_', '-'}): bool = cmpIgnore
5050
5151# ----- Streams -----
5252
53- proc peekRune * (s: Stream ): Rune =
53+ proc peekRune * (s: Stream ): Rune =
5454 let str = s.peekStr (4 )
5555 if str.len > 0 :
5656 result = str.runeAt (0 )
5757
58- proc peekLineFromStart * (s: Stream ): string =
58+ proc peekLineFromStart * (s: Stream ): string =
5959 let before = s.getPosition ()
6060 while s.getPosition () > 0 :
6161 s.setPosition (s.getPosition () - 1 )
@@ -69,7 +69,7 @@ proc peekLineFromStart*(s: Stream): string =
6969 result = s.peekLine ()
7070 s.setPosition before
7171
72- proc peekLineFromStart * (s: string , at: int ): string =
72+ proc peekLineFromStart * (s: string , at: int ): string =
7373 if at >= s.len:
7474 return
7575
@@ -112,7 +112,7 @@ proc getCoord*(s: string, at: int): Coord =
112112
113113 inc result .idx
114114
115- proc errorAt * (s: Stream , coord: Coord ): string =
115+ proc errorAt * (s: Stream , coord: Coord ): string =
116116 let before = s.getPosition ()
117117 s.setPosition coord.idx
118118 let line = s.peekLineFromStart ()
@@ -122,7 +122,7 @@ proc errorAt*(s: Stream, coord: Coord): string =
122122 result .add (& " { lineNum} { line} \n " )
123123 result .add (& " { repeat (' ' , lineNum.len + coord.col)} ^" )
124124
125- proc errorAt * (s: string , coord: Coord ): string =
125+ proc errorAt * (s: string , coord: Coord ): string =
126126 let line = s.peekLineFromStart (coord.idx)
127127
128128 let lineNum = & " { coord.line + 1 } | "
@@ -131,16 +131,19 @@ proc errorAt*(s: string, coord: Coord): string =
131131
132132# ----- Object variants -----
133133
134- macro isObjVariant * (a: typedesc ): bool =
134+ macro isObjVariant * (a: typedesc ): bool =
135135 var a = a.getTypeImpl
136- doAssert a.kind == nnkBracketExpr
136+ if a.kind != nnkBracketExpr:
137+ return ident (" false" )
138+
137139 let sym = a[1 ]
138140 let t = sym.getTypeImpl
139141 if t.kind != nnkObjectTy:
140142 return ident (" false" )
141143
142144 let t2 = t[2 ]
143- doAssert t2.kind == nnkRecList
145+ if t2.kind != nnkRecList:
146+ return ident (" false" )
144147
145148 result = ident (" false" )
146149
@@ -155,24 +158,30 @@ macro getDiscriminants*(a: typedesc): seq[string] =
155158 # # return the discriminant keys
156159 # candidate for std/typetraits
157160 var a = a.getTypeImpl
158- doAssert a.kind == nnkBracketExpr
161+ if a.kind != nnkBracketExpr:
162+ return quote do :
163+ newSeq [string ]()
164+
159165 let sym = a[1 ]
160166 let t = sym.getTypeImpl
161167 if t.kind != nnkObjectTy:
162168 return quote do :
163169 newSeq [string ]()
164170
165171 let t2 = t[2 ]
166- doAssert t2.kind == nnkRecList
172+ if t2.kind != nnkRecList:
173+ return quote do :
174+ newSeq [string ]()
175+
167176 result = newTree (nnkBracket)
168177
169178 for ti in t2:
170179 if ti.kind == nnkRecCase:
171180 let key = ti[0 ][0 ]
172181 let typ = ti[0 ][1 ]
173182 result .add newLit key.strVal
174-
175- result =
183+
184+ result =
176185 if result .len > 0 :
177186 quote do :
178187 @ `result`
@@ -214,3 +223,4 @@ macro initCaseObject*(T: typedesc, discriminatorSetter): untyped =
214223 result .add newTree (nnkExprColonExpr, key, val)
215224
216225template typeofdesc * [T](b: typedesc [T]): untyped = T
226+
0 commit comments