Skip to content

Instantly share code, notes, and snippets.

@ykst
Created April 5, 2014 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ykst/9988144 to your computer and use it in GitHub Desktop.
Save ykst/9988144 to your computer and use it in GitHub Desktop.
Parsecでアノテーション風にテキストの一部をファイルで置換する ref: http://qiita.com/ykst/items/77aa62ba539eb23bb1d6
ghc --make gen.hs
./gen sample.js
module Main where
import System.Environment (getArgs)
import Text.Parsec
import Text.Parsec.String
import Control.Monad.Trans (liftIO)
import Control.Applicative hiding ((<|>), many, optional)
annotateP :: ParsecT String () IO String
annotateP = (try (wrapP includeP)) <|> (many anyChar)
where
wrapP innerP =
(++) <$> many (satisfy ('@' /=)) <*> ((++) <$> innerP <*> many anyChar)
includeP =
(string "@include" <* spaces) >>
(between (char '(' <* spaces) (char ')') (many (noneOf ") \t") <* spaces)) >>=
(liftIO . readFile)
main = getArgs >>= \args -> case args of
[f] -> readFile f >>= convert . lines
_ -> putStrLn "specify one file"
where
convert [] = pure ()
convert (l:ls) =
runParserT annotateP () "" l >>= either (error . show) putStrLn >> convert ls
(function(global) {
(function() {
@include(a.js)
@include(b.js)
@include(c.js)
}).apply(global);
// do something
}({}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment