Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More default parsers? #21

Open
sjl opened this issue Nov 4, 2012 · 1 comment
Open

More default parsers? #21

sjl opened this issue Nov 4, 2012 · 1 comment

Comments

@sjl
Copy link
Contributor

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)
              []
              result))))

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

@sjl
Copy link
Contributor Author

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant