Skip to content

Instantly share code, notes, and snippets.

@borkdude
Created May 6, 2021 20:56
Show Gist options
  • Save borkdude/bbc78b34e77ef91510ace75862d23aeb to your computer and use it in GitHub Desktop.
Save borkdude/bbc78b34e77ef91510ace75862d23aeb to your computer and use it in GitHub Desktop.
Kern parser combinator with bb
#!/usr/bin/env bb
(require '[babashka.deps :as deps])
(deps/add-deps '{:deps {org.blancas/kern {:git/url "https://github.com/borkdude/kern"
:sha "34a8c2f0c1ec5ab3e0e9aac56e6278abc3ed5a0b"}}})
(use 'blancas.kern.core
'blancas.kern.lexer.basic)
(declare json)
(def pair (bind [f string-lit _ colon v json]
(return [f v])))
(def array (brackets (comma-sep (fwd json))))
(def object (braces
(bind [fields (comma-sep pair)]
(return (apply hash-map (reduce concat [] fields))))))
(def json (<|> string-lit dec-lit float-lit object array bool-lit nil-lit))
(run json "{\"fst\": \"Joe\", \"lst\": \"Hacks\",\"id\":1122}")
;;=> {"fst" "Joe", "lst" "Hacks", "id" 1122}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment