Created
April 5, 2014 06:32
-
-
Save ykst/9988144 to your computer and use it in GitHub Desktop.
Parsecでアノテーション風にテキストの一部をファイルで置換する ref: http://qiita.com/ykst/items/77aa62ba539eb23bb1d6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ghc --make gen.hs | |
./gen sample.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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