Created
July 29, 2012 22:07
-
-
Save Idorobots/3202109 to your computer and use it in GitHub Desktop.
A set of grammar reader macros to parse JSON in ASM programming language
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
# ;; JSON anybody? | |
(grammar ((JObject <- (: "\\{") (? JPair (* (:",") JPair)) (:"\\}")) | |
`($(car JObject) | |
$(cdr JObject))) | |
((JPair <- JString (: ":") JValue) | |
`($(car JPair) | |
$(cdr JPair))) | |
((JString <- (:"\"") "[^\"]*" (:"\""))) | |
((JValue <- (/ JString JNumber JObject JArray JTrue JFalse JNull))) | |
((JNumber <- (/ "0" "[1-9][0-9]*(\\.[0-9]*)?")) | |
`($(car JNumber) | |
($(str->num (caadr JNumber))))) | |
((JArray <- (:"\\[") (? JValue (* (:",") JValue)) (:"\\]")) | |
`($(car JArray) | |
($(vectorof (cadr JArray))))) | |
((JTrue <- "true")) | |
((JFalse <- "false")) | |
((JNull <- "null"))) | |
# ;; Let's test it: | |
(write (JObject "{ | |
\"Number\": 42, | |
\"Decimal\": 123.456, | |
\"String\": \"abc\", | |
\"Empty\" : {}, | |
\"Array\" : [0,1,2], | |
\"Array2\": [0, [0,1,2], \"abc\"], | |
\"Obj\" : { \"Member\":0, \"Member\":[0,1,2] }, | |
\"True\" : true, | |
\"False\" : false, | |
\"Null\" : null | |
}")) | |
# ;; Parses to: | |
(("Number" 42) | |
("Decimal" 123.456) | |
("String" "abc") | |
("Empty" ()) | |
("Array" [0 1 2]) | |
("Array2" [0 [0 1 2] "abc"]) | |
("Obj" (("Member" 0) | |
("Member" [0 1 2]))) | |
("True" "true") | |
("False" "false") | |
("Null" "null")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment