awwx.ws

Creating a parser combinator library to parse JSON

Prev: Matching JSON literalsContentsNext: fromjson

alt

I come to my first parser combinator. I want to be able to try a series of alternatives, until I find one that matches:

(= json-value
  (alt json-true
       json-false
       json-null))

I need to go through a list of parsers, and call each one on the parse position until I get one that doesn’t return nil, and return what it returns. Arc’s some function makes this easy:

(def alt parsers
  (fn (p)
    (some [_ p] parsers)))

Now I have a json-value parser that will match any of the JSON literals:

arc> (show-parse json-value "true,1,2")
returning: t remaining: ,1,2
nil
arc> (show-parse json-value "false,1,2")
returning: nil remaining: ,1,2
nil

Prev: Matching JSON literalsContentsNext: fromjson


Questions? Comments? Email me andrew.wilcox [at] gmail.com