Arc Forumnew | comments | leaders | submitlogin
1 point by CatDancer 5681 days ago | link | parent

I think the question is what to do with the common operations such as moving the forward the pointer to what we're going to parse next. One way to do it is to keep state in a some surrounding lexical scope (similar to what lib/parser.arc in Anarki does):

  (def run-parser (j ...)
     (withs
        (next-char (fn () car.j))
        (bump-j (fn () (= j cdr.j))
        (parse-string (fn ()
                         if (is (next-char) #\")
                              (do (bump-j)
                                  ...
I might try keeping track of state implicitly using Scheme parameters and see if that makes things shorter.


1 point by shader 5681 days ago | link

I think that char-char is supposed to be next char ;)

Other than that, I'm not good enough yet at writing parsers in arc to help you much. Your original was already pretty short, though there were a few spots that you could have used the . operator more (car, cdr, etc.)

I am also working on a parser too, though, so whatever you discover will certainly be helpful ;)

-----

1 point by CatDancer 5681 days ago | link

char-char

ha, I just squeaked in under the one-hour edit window :-)

-----

1 point by shader 5681 days ago | link

You know, if you used a input-port instead of a string, then next-char and bump-j would just be peekc and readc. I don't know if that would make you're life much easier, but it's an idea.

-----

1 point by CatDancer 5681 days ago | link

Right, a port encapsulates some state (where you're reading from, what your position is in the input), and then Arc uses a Scheme parameter (stdin) so that the input port doesn't need to be passed to every function that calls some function that calls some function that calls read or readc. So like you say I could use an input-port if that did what I needed, or I could implement my own thing if that turned out to be necessary.

-----