<div dir="ltr"><div><div><div><div><div><div><div><div><div>Dear Haskellers<br><br></div>This is gonna be my first post on here : ) . <br><br>I am developing some tools to interact with Chemistry packages, and I basically have to write input files for those programs.<br>
<br></div>I would like to be able to write those input in a better way inside the hs code. For example this would be perfect ( {{{+ and +}}} are just labels to mark the "special syntax"):<br><br></div><div>--------------a.hs--------------------------------<br>
</div>main = do<br></div>            writeFile "inputOfChemPack" string<br><br></div>string = {{{+<br></div>This is the actual input file<br></div>Formatted like this<br></div>with some %variable<br>+}}}<br><br>
</div>variable = "foo"<br><div><div><div><div>--------------------------------------------------<br><br></div><div>I have written a parser that is reading the code and outputs a file like this (it can handle special characters and variables that are not strings, but let's keep it simple here):<br>
<br></div><div>----------------b.hs------------------------------<br></div><div>main = do<br>            writeFile "inputOfChemPack" string<br><br>string = "This is the actual input file\nFormatted like this\nwith some " ++ variable ++ " "<br>
<br>variable = "foo"<br>--------------------------------------------------<br><br></div><div>Now... this file can be compiled and it does exactly what I want: it prints a file like this:<br><br>--------inputOfChemPack-----<br>
This is the actual input file<br>Formatted like this<br>with some foo<br></div><div>--------------------------------------<br><br></div><div>Now the big question... right now I am doing this with some bash scripts (run the parser on the file and then compile its output)... but... is it possible to automatize this using my parser as a sort of preprocessor?<br>
<br></div><div>the "dream" would be to make an extension that just applies the parser automatically before compilation, such as:<br><br></div><div>-----------------a.hs-----------------------------<br></div><div>
{-# extensionName #-}<br></div><div><br>main = do<br>            writeFile "inputOfChemPack" string<br><br>string = {{{+<br>This is the actual input file<br>Formatted like this<br>with some %variable<br>+}}}<br>
<br>variable = "foo"<br>--------------------------------------------------<br><br></div><div>$ ghci a.hs<br>GHCi, version 7.6.3: <a href="http://www.haskell.org/ghc/">http://www.haskell.org/ghc/</a>  :? for help<br>
Loading package ghc-prim ... linking ... done.<br>Loading package integer-gmp ... linking ... done.<br>Loading package base ... linking ... done.<br>[1 of 1] Compiling Main             ( a.hs, interpreted )<br>Ok, modules loaded: Main.<br>
*Main> string<br>"This is the actual input file\nFormatted like this\nwith some foo"<br><br></div><div>I have been browsing stuffs about quasiquotation and preprocessors, but I have the feeling that is a quite simple task compared to those solutions. <br>
<br>Any ideas? How can I solve this problem?<br></div><div><br>Thank you<br><br></div><div>Alessio<br></div></div></div></div></div>