Skip to content

Commit bbdde9c

Browse files
keywords can start with numbers or true, false, nil
1 parent 9543c07 commit bbdde9c

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

pixie/vm/reader.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pixie.vm.keyword import keyword, Keyword
1111
import pixie.vm.rt as rt
1212
from pixie.vm.persistent_vector import EMPTY as EMPTY_VECTOR
13-
from pixie.vm.libs.libedit import _readline
1413
from pixie.vm.string import Character
1514
from pixie.vm.code import wrap_fn
1615
from pixie.vm.persistent_hash_map import EMPTY as EMPTY_MAP
@@ -306,6 +305,21 @@ def invoke(self, rdr, ch):
306305
return cons(symbol(u"quote"), cons(itm))
307306

308307
class KeywordReader(ReaderHandler):
308+
def read(self, rdr):
309+
acc = []
310+
311+
try:
312+
while True:
313+
ch = rdr.read()
314+
if is_whitespace(ch) or is_terminating_macro(ch):
315+
rdr.unread()
316+
break
317+
acc.append(ch)
318+
except EOFError:
319+
pass
320+
sym_str = u"".join(acc)
321+
return symbol(sym_str)
322+
309323
def fqd(self, itm):
310324
ns_alias = rt.namespace(itm)
311325
current_nms = rt.ns.deref()
@@ -319,11 +333,12 @@ def fqd(self, itm):
319333
def invoke(self, rdr, ch):
320334
ch = rdr.read()
321335
if ch == u":":
322-
itm = read_inner(rdr, True)
336+
itm = self.read(rdr)
323337
return self.fqd(itm)
324338
else:
325339
rdr.unread()
326-
itm = read_inner(rdr, True)
340+
itm = self.read(rdr)
341+
327342
return keyword(rt.name(itm), rt.namespace(itm))
328343

329344
class LiteralStringReader(ReaderHandler):

tests/pixie/tests/test-keywords.pxi

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
(t/assert= (:a m) 1)
77
(t/assert= (:b m) 2)
88
(t/assert= (:c m) 3)
9-
109
(t/assert= (:d m) nil)
1110
(t/assert= (:d m :foo) :foo)))
1211

@@ -29,9 +28,40 @@
2928
(t/assert= (not= :foo/bar :cat/bar) true)
3029
(t/assert= (not= :foo/cat :foo/dog) true))
3130

31+
(t/deftest keyword-reader
32+
(t/assert= (read-string ":1") :1)
33+
(t/assert= (read-string ":1") :1)
34+
(t/assert= (read-string ":1.0") :1.0)
35+
(t/assert= (read-string ":foo") :foo)
36+
(t/assert= (read-string ":1foo") :1foo)
37+
(t/assert= (read-string ":foo/bar") :foo/bar)
38+
(t/assert= (read-string ":1foo/1bar") :1foo/1bar)
39+
(t/assert= (read-string ":nil") :nil)
40+
(t/assert= (read-string ":true") :true)
41+
(t/assert= (read-string ":false") :false)
42+
43+
;; We are reading at runtime so the namespace isn't
44+
;; going to be pixie.test.test-keywords. Its probably
45+
;; 'user but lets explicitly set it.
46+
;; The refer-ns is to initialize a new space
47+
(refer-ns 'my.other.ns 'my.fake.core 'fake)
48+
(binding [*ns* (the-ns 'my.other.ns)]
49+
(t/assert= (read-string "::1") :my.other.ns/1)
50+
(t/assert= (read-string "::1.0") :my.other.ns/1.0)
51+
(t/assert= (read-string "::foo") :my.other.ns/foo)
52+
(t/assert= (read-string "::true") :my.other.ns/true)))
53+
3254
(t/deftest string-to-keyword
55+
(t/assert= (keyword "1") :1)
56+
(t/assert= (keyword "1") :1)
57+
(t/assert= (keyword "1.0") :1.0)
3358
(t/assert= (keyword "foo") :foo)
59+
(t/assert= (keyword "1foo") :1foo)
3460
(t/assert= (keyword "foo/bar") :foo/bar)
61+
(t/assert= (keyword "1foo/1bar") :1foo/1bar)
62+
(t/assert= (keyword "nil") :nil)
63+
(t/assert= (keyword "true") :true)
64+
(t/assert= (keyword "false") :false)
3565
(t/assert-throws? (keyword 1))
3666
(t/assert-throws? (keyword :a))
3767
(t/assert-throws? (keyword 'a))

0 commit comments

Comments
 (0)