Skip to content

Commit

Permalink
reading 1M etc literals
Browse files Browse the repository at this point in the history
temporary solution, should probably become a new type
  • Loading branch information
rosado committed Feb 22, 2020
1 parent 871e235 commit 4cda608
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion edn.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.2.0"
version = "0.2.1"
author = "Roland Sadowski"
description = "EDN and Clojure parser"
license = "EPL-2.0"
Expand Down
13 changes: 12 additions & 1 deletion src/edn.nim
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ proc read_reader_conditional(p: var EdnParser): EdnNode =


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

proc add_meta*(node: EdnNode, meta: HMap): EdnNode =
case node.kind
Expand All @@ -875,6 +875,8 @@ proc add_meta*(node: EdnNode, meta: HMap): EdnNode =
node.map_meta = meta
of EdnVector:
node.vec_meta = meta
of EdnSet:
node.set_meta = meta
else:
raise new_exception(ParseError, META_CANNOT_APPLY_MSG & $node.kind)
result = node
Expand All @@ -889,6 +891,8 @@ proc get_meta*(node: EdnNode): HMap =
return node.map_meta
of EdnVector:
return node.vec_meta
of EdnSet:
return node.set_meta
else:
raise new_exception(ParseError, "Given type does not support metadata: " & $node.kind)

Expand Down Expand Up @@ -920,6 +924,8 @@ proc read_metadata(p: var EdnParser): EdnNode =
m[KeyTag] = meta
of EdnMap:
m = meta.map
of EdnSet:
m = meta.map
else:
p.options = old_opts
raise new_exception(ParseError, META_INVALID_MSG)
Expand Down Expand Up @@ -1273,9 +1279,14 @@ proc read_num(p: var EdnParser): EdnNode =
result = new_edn_ratio(numerator.num, denom.num)
else:
raise new_exception(ParseError, "error reading a ratio: " & p.a)
elif p.buf[p.bufpos] == 'M': #TODO: for now...
inc(p.bufpos)
result = new_edn_int(p.a)
else:
result = new_edn_int(p.a)
of tkFloat:
if p.buf[p.bufpos] == 'M': #TODO: for now...
inc(p.bufpos)
result = new_edn_float(p.a)
of tkError:
raise new_exception(ParseError, "error reading a number: " & p.a)
Expand Down
15 changes: 15 additions & 0 deletions tests/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ test "everything":
check node.kind == EdnInt
check node.num == -1

node = read("1M")
check node.kind == EdnInt #TODO: for now...

node = read("()")
check node.kind == EdnList
check node.list.len == 0
Expand Down Expand Up @@ -176,6 +179,14 @@ test "everything":
check node.kind == EdnMap
check node.map.len == 2

node = read("{:x 1M :y 2}")
check node.kind == EdnMap
check node.map.len == 2

node = read("{:order_date #clj-time/date-time \"2019-12-01T00:00:00.000Z\", :quantity 125.3M, 1 1}")
check node.kind == EdnMap
check node.map.len == 3

try:
node = read("moo/bar/baz")
raise new_exception(Exception, "FAILURE")
Expand Down Expand Up @@ -209,6 +220,10 @@ test "everything":
check node.list_meta.count == 1
check node.list_meta[KeyTag].get() == new_edn_symbol("", "foo")

node = read("^{:x 1} #{1}")
check node.kind == EdnSet
check node.set_meta.count == 1

node = read("^\"foo\" Symbol")
check node.kind == EdnSymbol
check node.symbol == new_edn_symbol("", "Symbol").symbol
Expand Down

0 comments on commit 4cda608

Please sign in to comment.