I am trying to parse some files using the parsec combinator library
and am stuck in some kind of monad mismatch or general blindness or
so. For the sake of this argument, the documents to be parsed are just
a list of things:

document :: Parser [Thing]

Parsing the things with

thing :: Parser Thing

is easy. The problematic part is that they may recursively call
include files that are also lists of things. My naive first try was:

document = do
             whiteSpace
             xs <- many includes
             x  <- many thing
             return $ foldr (++) x xs

This should probably work, if I could beat includes into

includes :: Parser [Thing]

and make it use the document parser to parse the files specified in
the include directives. Parsec has a function parseFromFile that almost
does what I want, but

includes = do
             symbol "#include"
             filename <- stringLiteral
             ret <- parseFromFile document filename
             case (ret) of 
                 ...

obviously can't work, since 
getFromFile :: Parser a -> String -> IO (Either ParseError a)
and I can't get the resulting [Thing] out of the IO monad. 

Parsec also supplies a "setInput" function which allegedly can be used
to deal with include files, but that is about the whole documentation
of setInput.

Since compilers are one of the areas where everyone agrees that FPLs
are the right tool for the job, there should be a standard pattern to
deal with include files. Am I missing something essential?

Yours, Florian.

Reply via email to