Skip to content

Instantly share code, notes, and snippets.

@ghadishayban
Created October 1, 2015 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghadishayban/04d9dc8a57c6267e5eca to your computer and use it in GitHub Desktop.
Save ghadishayban/04d9dc8a57c6267e5eca to your computer and use it in GitHub Desktop.
Pex JSON grammar
(def JSON '{json [whitespace value EOI]
value (/ string number object array jtrue jfalse jnull)
object [(:ws "{")
(:join [string (:ws ":") value] ",")
(:ws "}")
(action capture-object)]
array [(:ws "[") (:join value (:ws ",")) (:ws "]")]
string [\" (action clear-sb) characters (:ws \") (action push-sb)]
number (:ws (capture integer (? frac) (? exp)))
characters (/ (* normal-character)
[\\ escaped-character])
quote-backslash "\"\\"
normal-character [(not quote-backslash) any (action append-last)]
escaped-character (/ quote-backslash
"b"
"f"
"n"
"r"
"t"
unicode)
unicode ["u"
(capture (class hexdigit)
(class hexdigit)
(class hexdigit)
(class hexdigit))
(action append-hexdigit)]
integer [(? "-") (/ [(class digit19) digits]
(class digit))]
digits [(class digit) (* (class digit))]
frac ["." digits]
exp [(:ignore-case "e") (? (:anyof "+-")) digits]
jtrue ["true" whitespace]
jfalse ["false" whitespace]
jnull ["null" whitespace]
whitespace (* (:anyof " \n\r\t\f"))})
(def json-macros {:anyof (fn [str] (apply list '/ (seq str)))
:ws (fn [patt] [patt 'whitespace])
:join (fn [patt sep] [patt (list '* sep patt)])
:ignore-case identity})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment