Skip to content

Instantly share code, notes, and snippets.

@dbushenko
Created March 9, 2012 21:07
Show Gist options
  • Save dbushenko/2008707 to your computer and use it in GitHub Desktop.
Save dbushenko/2008707 to your computer and use it in GitHub Desktop.
(ns ptest.core
(:require [the.parsatron :as p]))
(p/defparser op []
(p/choice (p/char \+)
(p/char \-)))
(p/defparser number []
(p/let->> [d (p/many (p/digit))]
(p/always (reduce str d))))
(declare e2)
(p/defparser e []
(p/>> (number) (e2)))
(p/defparser e2 []
(p/choice (p/>> (op) (e))
(p/always nil)))
(declare expr)
(declare expr2)
(p/defparser expr []
(p/choice
(expr2)
(p/let->> [x (number)]
(p/choice (p/let->> [o (op),
y (expr)]
(p/always (list x o y)))
(p/always x)))))
(p/defparser expr2 []
(p/let->> [c1 (p/char \(),
e (expr),
c2 (p/char \))]
(p/always (list "[" e "]"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment