Created
October 1, 2015 19:30
-
-
Save ghadishayban/04d9dc8a57c6267e5eca to your computer and use it in GitHub Desktop.
Pex JSON grammar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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