Skip to content

Commit 4cda608

Browse files
committed
reading 1M etc literals
temporary solution, should probably become a new type
1 parent 871e235 commit 4cda608

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

edn.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.2.0"
3+
version = "0.2.1"
44
author = "Roland Sadowski"
55
description = "EDN and Clojure parser"
66
license = "EPL-2.0"

src/edn.nim

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ proc read_reader_conditional(p: var EdnParser): EdnNode =
863863

864864

865865
const META_CANNOT_APPLY_MSG =
866-
"Metadata can be applied only to symbols, lists, vectors and map. Got :"
866+
"Metadata can be applied only to symbols, lists, vectors and maps and sets. Got :"
867867

868868
proc add_meta*(node: EdnNode, meta: HMap): EdnNode =
869869
case node.kind
@@ -875,6 +875,8 @@ proc add_meta*(node: EdnNode, meta: HMap): EdnNode =
875875
node.map_meta = meta
876876
of EdnVector:
877877
node.vec_meta = meta
878+
of EdnSet:
879+
node.set_meta = meta
878880
else:
879881
raise new_exception(ParseError, META_CANNOT_APPLY_MSG & $node.kind)
880882
result = node
@@ -889,6 +891,8 @@ proc get_meta*(node: EdnNode): HMap =
889891
return node.map_meta
890892
of EdnVector:
891893
return node.vec_meta
894+
of EdnSet:
895+
return node.set_meta
892896
else:
893897
raise new_exception(ParseError, "Given type does not support metadata: " & $node.kind)
894898

@@ -920,6 +924,8 @@ proc read_metadata(p: var EdnParser): EdnNode =
920924
m[KeyTag] = meta
921925
of EdnMap:
922926
m = meta.map
927+
of EdnSet:
928+
m = meta.map
923929
else:
924930
p.options = old_opts
925931
raise new_exception(ParseError, META_INVALID_MSG)
@@ -1273,9 +1279,14 @@ proc read_num(p: var EdnParser): EdnNode =
12731279
result = new_edn_ratio(numerator.num, denom.num)
12741280
else:
12751281
raise new_exception(ParseError, "error reading a ratio: " & p.a)
1282+
elif p.buf[p.bufpos] == 'M': #TODO: for now...
1283+
inc(p.bufpos)
1284+
result = new_edn_int(p.a)
12761285
else:
12771286
result = new_edn_int(p.a)
12781287
of tkFloat:
1288+
if p.buf[p.bufpos] == 'M': #TODO: for now...
1289+
inc(p.bufpos)
12791290
result = new_edn_float(p.a)
12801291
of tkError:
12811292
raise new_exception(ParseError, "error reading a number: " & p.a)

tests/test.nim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ test "everything":
122122
check node.kind == EdnInt
123123
check node.num == -1
124124

125+
node = read("1M")
126+
check node.kind == EdnInt #TODO: for now...
127+
125128
node = read("()")
126129
check node.kind == EdnList
127130
check node.list.len == 0
@@ -176,6 +179,14 @@ test "everything":
176179
check node.kind == EdnMap
177180
check node.map.len == 2
178181

182+
node = read("{:x 1M :y 2}")
183+
check node.kind == EdnMap
184+
check node.map.len == 2
185+
186+
node = read("{:order_date #clj-time/date-time \"2019-12-01T00:00:00.000Z\", :quantity 125.3M, 1 1}")
187+
check node.kind == EdnMap
188+
check node.map.len == 3
189+
179190
try:
180191
node = read("moo/bar/baz")
181192
raise new_exception(Exception, "FAILURE")
@@ -209,6 +220,10 @@ test "everything":
209220
check node.list_meta.count == 1
210221
check node.list_meta[KeyTag].get() == new_edn_symbol("", "foo")
211222

223+
node = read("^{:x 1} #{1}")
224+
check node.kind == EdnSet
225+
check node.set_meta.count == 1
226+
212227
node = read("^\"foo\" Symbol")
213228
check node.kind == EdnSymbol
214229
check node.symbol == new_edn_symbol("", "Symbol").symbol

0 commit comments

Comments
 (0)