Skip to content

Instantly share code, notes, and snippets.

@cgrand
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgrand/257de60e11458ee2749a to your computer and use it in GitHub Desktop.
Save cgrand/257de60e11458ee2749a to your computer and use it in GitHub Desktop.
; helper functions are missing, be it for char ranges or for lookaheads
; however this snippet demoes:
; * error-correction (the change in correction between the 3rd and 4th case shows that parsnip looks for a minimal deletion)
; * lookahead
=> (let [alpha (fn [c] (<= (int \a) (int c) (int \z)))
p (parser :map
{:map ["{" (* :sym (? :ws) :sym) "}"]
:ws (+ " ")
:sym [(+ (->InlineAsm [:PRED alpha]))
(->InlineAsm [:PEEK (complement alpha)])]})]
(doseq [input ["{aaaa}" "{aa aa}" "{a aa a}" "{a aa a}"]]
(prn input '-> (p input))))
"{aaaa}" -> [[:map "{" [:skip "aaaa"] "}"]]
"{aa aa}" -> [[:map "{" [:sym "aa"] [:ws " "] [:sym "aa"] "}"]]
"{a aa a}" -> [[:map "{" [:sym "a" [:skip " "] "aa"] [:ws " "] [:sym "a"] "}"]]
"{a aa a}" -> [[:map "{" [:sym "a"] [:ws " "] [:sym "aa" [:skip " "] "a"] "}"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment