how to #include files within parsec ... without unsafePerformIO?
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. Greetings, 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 = do i <- getInput setInput readI a <- my_str setInput i b <- my_str return $ a ++ " //\n\n " ++ b where {- 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 generally.
Leonard Siebeneicher wrote:
Dear reader,
I wonder whether there is a 'general' working solution to include files within a parsec parser. Without the need of unsafePerformIO.
At least in parsec 2, I don't think so. Our solution was to read in the main file, tokenise it (using Alex), preprocess it (using some regex-like pattern matching on the token stream) in the IO monad and include the new files then (also tokenising and preprocessing them). Then after preprocessing we feed the entire resulting token stream to Parsec. Whether a two-phase approach (preprocess then parse) works depends on whether your include syntax is simple enough that you can spot the includes without parsing. Thanks, Neil.
Hi, ParsecT with m=IO? Your 'do' block would become: do i <- getInput included <- liftIO readI -- import Control.Monad.Trans for liftIO setInput included a <- my_str setInput i b <- my_str return $ a ++ " //\n\n " ++ b where readI = readFile "experiment.hs" Maybe I'm misunderstanding the problem. Greetings, Daniel On Thursday 18 June 2009 13:58:53 Leonard Siebeneicher wrote:
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.
Greetings, 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 = do i <- getInput setInput readI a <- my_str setInput i b <- my_str return $ a ++ " //\n\n " ++ b where {- 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 generally.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Daniel Schüssler -
Leonard Siebeneicher -
Neil Brown