Skip to content

Instantly share code, notes, and snippets.

@Idorobots
Created July 29, 2012 22:07
Show Gist options
  • Save Idorobots/3202109 to your computer and use it in GitHub Desktop.
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
# ;; 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