parsatron
parsatron copied to clipboard
More default parsers?
Hey, I've been using the Parsatron in another project lately and came up with a few pretty basic parsers that might be useful to other people. If you want them I can send a pull request that adds them, some tests, and some documentation for them:
(defparser optional [p]
(either (attempt p)
(always nil)))
(defparser separated1 [p separatorp]
(let->> [fst p
rst (many (>> separatorp p))]
(always (concat [fst] rst))))
(defparser separated [p separatorp]
(let->> [result (optional (separated1 p separatorp))]
(always (if (nil? result)
[]
result))))
It's possible there's an easier way to do these and I just missed it, in which case let me know!
Oh, and to give an example of why they're useful:
(run (separated (digit) (char \,)) "1,2,3")
[\1 \2 \3]