Commits

Lucian Brănescu-Mihăilă committed 4b9513b

Map literal and tests.

  • Participants
  • Parent commits 94d144b

Comments (0)

Files changed (2)

         return '[' + ' '.join(map(str, self)) + ']'
 
 
+def pairs(lst):
+    it = iter(lst)
+    while True:
+        yield next(it), next(it)
+
+
+class Map(dict, Collection):
+    start = '{'
+    stop = '}'
+
+    def __init__(self, init=None):
+        if init is None:
+            init = []
+
+        super(Map, self).__init__(pairs(init))
+
+
 class Symbol(str):
     def __repr__(self):
         return self
     else:
         quoting = None
 
-    # Lists
+    # Lists and Vectors
     if tok == '(':
         return parse_coll(List, tokens, quoting)
     elif tok == '[':
         return parse_coll(Vector, tokens, quoting)
+    elif tok == '{':
+        return parse_coll(Map, tokens, quoting)
 
     for i in [List.stop, Vector.stop]:
         if tok == i:
         r"\)": " ) ",
         r"\[": " [ ",
         r"\]": " ] ",
+        r"\{": " { ",
+        r"\}": " } ",
         r"'": " ' ",
         r"`": " ` ",
         # match only if next char isn't @, lookahead so it isn't consumed
     assert parse(['[', '1', '2', ']']) == [1, 2]
 
 
+def test_parse_map():
+    assert parse(['{', '1', '2', '}']) == {1: 2}
+    assert parse(['{', '1', '2', '3', '4', '}']) == {1: 2, 3: 4}
+    assert parse(['{', '1', '2', '3', '}']) == {1: 2}
+
+
 def test_parse_unmatched():
     with raises(SyntaxError):
         assert parse(['('])