[Haskell-cafe] how to #include files within parsec ... without unsafePerformIO?

Leonard Siebeneicher l-siebeneicher at versanet.de
Thu Jun 18 07:58:53 EDT 2009

Dear reader,

I wonder whether there is a 'general' working solution to include files
within a parsec parser. Without the need of unsafePerformIO.

Appending an example program, using unsafePerformIO.

Thanx for reading.

Leonard Siebeneicher

--- Begin: experiment.hs ---
import Text.ParserCombinators.Parsec
import System.IO.Unsafe

my_str :: Parser String
my_str = many1 anyToken

wrap_input :: Parser String -> Parser String
wrap_input p = 
      i <- getInput
      setInput readI
      a <- my_str
      setInput i
      b <- my_str
      return $ a ++ " //\n\n " ++ b
      {- Aaaah ... any solution without unsafePerformIO? -}
      readI = unsafePerformIO (readFile "experiment.hs")

main = 
    case parse (wrap_input my_str) "" "eintest" of
      Left err -> putStrLn "Error raised"
      Right ostr -> putStrLn ostr
--- End: experiment.hs ---

Thinking about a special type like

data MyInclude = PlainText String
               | IncludeFile String

the parser could generate [MyInclude] data, but it does not work

More information about the Haskell-Cafe mailing list