Moritz Heidkamp avatar Moritz Heidkamp committed 8c99a98

Add register-edn-parser!

Comments (0)

Files changed (3)

 (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
    '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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.