Arc Forumnew | comments | leaders | submitlogin
Parser combinators (first try)
10 points by sjs 6136 days ago | 4 comments
I've started writing a parser combinator library that I hope will be useful for writing a proper implementation of markdown (and more). I haven't used it for anything except mucking around in the repl yet so there may be glaring bugs. If you're familiar with the Parsec library[1] for Haskell you should feel rather at home.

[1] http://legacy.cs.uu.nl/daan/parsec.html

Eventually I'll clean it up more and commit it to the git anarki, but for now you can get the code here: http://samhuri.net/parsecomb.arc

Primitives are: any-char, (char "x"), (not-char "y"), (one-of "abc"), (none-of "123"), digit, space, spaces.

Combinators are: alt, seq, many, many1, and try.

Comments, suggestions, and criticism are all appreciated.



4 points by sjs 6136 days ago | link

I fixed a few bugs and have pushed this to the git anarki: http://git.nex-3.com/arc-wiki.git

There is now a maybe combinator (like ? in a regex) and n-times, which is like {N} or {N,M} in a regex.

e.g.

    ((maybe "x") "hi") => (t "" "hi")
    ((maybe "x") "xhi") => (t "x" "hi")
    ((n-times any-char 3) "ab") => (nil nil "ab")
    ((n-times any-char 3) "abc") => (t "abc" "")
    ((n-times any-char 3) "abcd") => (t "abc" "d")
    ((n-times any-char 3 6) "abc") => (t "abc" "")
    ((n-times any-char 3 6) "abcd") => (t "abcd" "")
    ((n-times any-char 3 6) "abcdefg") => (t "abcdef" "g")
I'm starting to work on a simple markdown grammar, but so far it's all on paper. The library is not ready for real use yet but it's getting there.

-----

4 points by dfranke 6136 days ago | link

You might want a different name for try. That's likely to conflict with the standard library once Arc has more than vestigial exception handling.

-----

1 point by binx 6127 days ago | link

Does it optimize LL(1) parsing as parsec do? You might need some techniques of laziness to achieve this. Without this optimization, there would be space-leak and time-wasting.

-----

1 point by tel 6136 days ago | link

Totally glad to see a parser modeled after Parsec!

-----