Creating a parser combinator library to parse JSON
Prev: fromjson | Contents | Next: JSON numbers |
By happy happenstance, the JSON definition of whitespace is the same as Arc’s, so skipping whitespace is easy:
(def skipw (p) (mem nonwhite p))
arc> (show-parse json-true:skipw " true") returning: t remaining: nil
I’ll make that into a combinator. When passed a parser, it will return a parser that first skips whitespace, and then runs the passed parser:
(def skipwhite (parser) (fn (p) (parser (skipw p))))
arc> (show-parse (skipwhite json-true) " true") returning: t remaining: nil
Now I have a json-value
parser that will skip whitespace before any JSON value:
(= json-value (skipwhite:alt json-true json-false json-null))
arc> (show-parse json-value " false,1,2") returning: nil remaining: ,1,2 nilarc> (fromjson " true") t
Prev: fromjson | Contents | Next: JSON numbers |
Questions? Comments? Email me andrew.wilcox [at] gmail.com