Apr 21 2009

Regular expressions are the evil twin

Published by Dougal at 8:19 pm under Programming

One of the biggest boons of getting a new laptop has been that I now have access to a powerful enough machine to do programming on. Until very recently I would remember that I had personal projects about once a month, and have to fire up the desktop machine that’s sitting on a desk in the hallway. Neither comfortable nor accessible.

But enough waffle. You sat down this evening thinking, what has Dougal been programming? Didn’t you?

I’ve been playing with the parser combinator library called Parsec. I’m using it to parse a script (as for a stage or radio play) which I’ll be using later.

One of the nicer facilities Parsec has is somewhere to store state as we go along. So you can parse something and store it for later parses. For example, the file I’m parsing has sections that start

Scene 1. <path/to/file>

It’s perfectly legal for subsequent paths to be omitted, in which case we just assume it’s the same as Scene 1:

Scene 2.

Every time we parse a file name we store it for later reuse if the filename isn’t specified at the next section:

-- If no filename is given we fill in the most recently
-- used one. Then we store the current filename for later.
sceneheader = do try scenekw ; spaces
                 num <- many1 digit ; char '.' ; many space
                 oldurl <- getState
                 url <- option oldurl filename
                 setState url
                 newlines
                 return $ Start (read num) url

This really simplifies the sanity checking later on. I like it.

Comments Off

Comments are closed at this time.