awwx.ws

Creating a parser combinator library to parse JSON

Prev: manyContentsNext: JSON numbers, implemented

many1, implemented

Now I get to try out my implementation of many1:

(def many1 (parser)
  (seq parser
       (many parser)))
arc> (show-parse (many1 json-number-char) "123abc")
returning: (#\1 (#\2 #\3)) remaining: abc
nil

The matching part is working perfectly, but I’m getting (r (r ...)) for my return value instead of the (r ...) that I want. That’s because seq is returning a list of the return values of its parsers: the first match and the many match. I’ll need to modify the return value to cons the two parts together:

(def many1 (parser)
  (fn (p)
    (iflet (p2 (r1 rs)) ((seq parser
                              (many parser))
                         p)
      (return p2 (cons r1 rs)))))

Now I get the return value I’m looking for:

arc> (show-parse (many1 json-number-char) "123abc")
returning: (#\1 #\2 #\3) remaining: abc
nil

Prev: manyContentsNext: JSON numbers, implemented


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