Commits

Moritz Heidkamp  committed 8c99a98

Add register-edn-parser!

  • Participants
  • Parent commits df9a5f6

Comments (0)

Files changed (3)

File edn-impl.scm

 (define ((collection-result-parser tag) items)
   (cons tag items))
 
+(define (default-edn-parsers)
+  `((true    . ,(constantly #t))
+    (false   . ,(constantly #f))
+    (nil     . ,(constantly 'nil))
+    (string  . ,identity)
+    (char    . ,identity)
+    (symbol  . ,(symbol-result-parser string->symbol))
+    (keyword . ,(symbol-result-parser string->keyword))
+    (integer . ,number-result-parser)
+    (float   . ,number-result-parser)
+    (list    . ,(collection-result-parser 'list))
+    (vector  . ,(collection-result-parser 'vector))
+    (map     . ,(collection-result-parser 'map))
+    (set     . ,(collection-result-parser 'set))))
+
 (define edn-parsers
   (make-parameter
-   `((true    . ,(constantly #t))
-     (false   . ,(constantly #f))
-     (nil     . ,(constantly 'nil))
-     (string  . ,identity)
-     (char    . ,identity)
-     (symbol  . ,(symbol-result-parser string->symbol))
-     (keyword . ,(symbol-result-parser string->keyword))
-     (integer . ,number-result-parser)
-     (float   . ,number-result-parser)
-     (list    . ,(collection-result-parser 'list))
-     (vector  . ,(collection-result-parser 'vector))
-     (map     . ,(collection-result-parser 'map))
-     (set     . ,(collection-result-parser 'set)))))
+   (default-edn-parsers)))
+
+(define (register-edn-parser! name parser)
+  (edn-parsers (alist-update! name parser (edn-parsers))))
 
 (define (parser-ref name)
   (or (alist-ref name (edn-parsers))
 
 (read-edn
  edn-parsers
+ default-edn-parsers
+ register-edn-parser!
  register-edn-tag-parser!
  parse-unknown-edn-tags?
  write-edn

File tests/read.scm

    'nice 'string (lambda (x) (string-append x " is nice!")))
   (test-read "#nice/string \"edn\"" "edn is nice!"))
 
+(test-group "custom parsers"
+  (register-edn-parser! 'vector list->vector)
+  (test-read "[1 2 3]" (vector 1 2 3)))
+
 (test-end)