Skip to content

More default parsers? #21

sjl opened this Issue Nov 4, 2012 · 1 comment

1 participant

sjl commented Nov 4, 2012

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)

It's possible there's an easier way to do these and I just missed it, in which case let me know!

sjl commented Nov 4, 2012

Oh, and to give an example of why they're useful:

(run (separated (digit) (char \,)) "1,2,3")
[\1 \2 \3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.