2013-04-13 20:00:36 +00:00
|
|
|
{- Expands template haskell splices
|
|
|
|
-
|
|
|
|
- First, the code must be built with a ghc that supports TH,
|
|
|
|
- and the splices dumped to a log. For example:
|
|
|
|
- cabal build --ghc-options=-ddump-splices 2>&1 | tee log
|
|
|
|
-
|
2013-04-14 17:25:06 +00:00
|
|
|
- Along with the log, a headers file may also be provided, containing
|
2013-04-13 20:00:36 +00:00
|
|
|
- additional imports needed by the template haskell code.
|
|
|
|
-
|
|
|
|
- This program will parse the log, and expand all splices therein,
|
2013-04-14 17:25:06 +00:00
|
|
|
- writing files to the specified destdir (which can be "." to modify
|
|
|
|
- the source tree directly). They can then be built a second
|
2013-04-13 20:00:36 +00:00
|
|
|
- time, with a ghc that does not support TH.
|
|
|
|
-
|
|
|
|
- Note that template haskell code may refer to symbols that are not
|
|
|
|
- exported by the library that defines the TH code. In this case,
|
|
|
|
- the library has to be modifed to export those symbols.
|
|
|
|
-
|
|
|
|
- There can also be other problems with the generated code; it may
|
|
|
|
- need modifications to compile.
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- Copyright 2013 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
import Text.Parsec
|
|
|
|
import Text.Parsec.String
|
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
import Data.Either
|
|
|
|
import Data.List
|
|
|
|
import Data.String.Utils
|
|
|
|
import Data.Char
|
2013-04-14 17:25:06 +00:00
|
|
|
import System.Environment
|
|
|
|
import System.FilePath
|
|
|
|
import System.Directory
|
2013-04-14 19:49:19 +00:00
|
|
|
import Control.Monad
|
2013-04-13 20:00:36 +00:00
|
|
|
|
|
|
|
import Utility.Monad
|
|
|
|
import Utility.Misc
|
|
|
|
import Utility.Exception
|
2013-04-14 17:25:06 +00:00
|
|
|
import Utility.Path
|
2013-04-13 20:00:36 +00:00
|
|
|
|
|
|
|
data Coord = Coord
|
|
|
|
{ coordLine :: Int
|
|
|
|
, coordColumn :: Int
|
|
|
|
}
|
|
|
|
deriving (Read, Show)
|
|
|
|
|
|
|
|
offsetCoord :: Coord -> Coord -> Coord
|
|
|
|
offsetCoord a b = Coord
|
|
|
|
(coordLine a - coordLine b)
|
|
|
|
(coordColumn a - coordColumn b)
|
|
|
|
|
2013-04-14 20:44:05 +00:00
|
|
|
data SpliceType = SpliceExpression | SpliceDeclaration
|
|
|
|
deriving (Read, Show, Eq)
|
|
|
|
|
2013-04-13 20:00:36 +00:00
|
|
|
data Splice = Splice
|
|
|
|
{ splicedFile :: FilePath
|
|
|
|
, spliceStart :: Coord
|
|
|
|
, spliceEnd :: Coord
|
|
|
|
, splicedExpression :: String
|
2013-04-13 21:38:11 +00:00
|
|
|
, splicedCode :: String
|
2013-04-14 20:44:05 +00:00
|
|
|
, spliceType :: SpliceType
|
2013-04-13 20:00:36 +00:00
|
|
|
}
|
|
|
|
deriving (Read, Show)
|
|
|
|
|
2013-04-14 20:44:05 +00:00
|
|
|
isExpressionSplice :: Splice -> Bool
|
|
|
|
isExpressionSplice s = spliceType s == SpliceExpression
|
|
|
|
|
2013-04-13 20:00:36 +00:00
|
|
|
number :: Parser Int
|
|
|
|
number = read <$> many1 digit
|
|
|
|
|
2013-04-13 21:38:11 +00:00
|
|
|
{- A pair of Coords is written in one of three ways:
|
|
|
|
- "95:21-73", "1:1", or "(92,25)-(94,2)"
|
2013-04-13 20:00:36 +00:00
|
|
|
-}
|
|
|
|
coordsParser :: Parser (Coord, Coord)
|
2013-04-13 21:38:11 +00:00
|
|
|
coordsParser = (try singleline <|> try weird <|> multiline) <?> "Coords"
|
2013-04-13 20:00:36 +00:00
|
|
|
where
|
|
|
|
singleline = do
|
|
|
|
line <- number
|
|
|
|
char ':'
|
|
|
|
startcol <- number
|
|
|
|
char '-'
|
|
|
|
endcol <- number
|
|
|
|
return $ (Coord line startcol, Coord line endcol)
|
|
|
|
|
2013-04-13 21:38:11 +00:00
|
|
|
weird = do
|
|
|
|
line <- number
|
|
|
|
char ':'
|
|
|
|
col <- number
|
|
|
|
return $ (Coord line col, Coord line col)
|
|
|
|
|
2013-04-13 20:00:36 +00:00
|
|
|
multiline = do
|
|
|
|
start <- fromparens
|
|
|
|
char '-'
|
|
|
|
end <- fromparens
|
|
|
|
return $ (start, end)
|
|
|
|
|
|
|
|
fromparens = between (char '(') (char ')') $ do
|
|
|
|
line <- number
|
|
|
|
char ','
|
|
|
|
col <- number
|
|
|
|
return $ Coord line col
|
|
|
|
|
|
|
|
indent :: Parser String
|
|
|
|
indent = many1 $ char ' '
|
|
|
|
|
|
|
|
restOfLine :: Parser String
|
|
|
|
restOfLine = newline `after` many (noneOf "\n")
|
|
|
|
|
|
|
|
indentedLine :: Parser String
|
|
|
|
indentedLine = indent >> restOfLine
|
|
|
|
|
|
|
|
spliceParser :: Parser Splice
|
|
|
|
spliceParser = do
|
|
|
|
file <- many1 (noneOf ":\n")
|
|
|
|
char ':'
|
|
|
|
(start, end) <- coordsParser
|
2013-04-13 21:38:11 +00:00
|
|
|
string ": Splicing "
|
2013-04-14 20:44:05 +00:00
|
|
|
splicetype <- tosplicetype
|
|
|
|
<$> (string "expression" <|> string "declarations")
|
2013-04-13 20:00:36 +00:00
|
|
|
newline
|
|
|
|
|
|
|
|
expression <- indentedLine
|
|
|
|
|
|
|
|
indent
|
|
|
|
string "======>"
|
|
|
|
newline
|
|
|
|
|
2013-04-13 21:38:11 +00:00
|
|
|
{- All lines of the splice code will start with the same
|
2013-04-13 20:00:36 +00:00
|
|
|
- indent, which is stripped. Any other indentation is preserved. -}
|
2013-04-13 21:38:11 +00:00
|
|
|
indent <- lookAhead indent
|
|
|
|
let getcodeline = do
|
|
|
|
string indent
|
|
|
|
restOfLine
|
|
|
|
|
2013-04-14 20:44:05 +00:00
|
|
|
{- When splicing declarations, GHC will output a splice
|
|
|
|
- at 1:1, and then inside the splice code block,
|
|
|
|
- the first line will give the actual coordinates of the
|
|
|
|
- line that was spliced.
|
|
|
|
-}
|
2013-04-13 21:38:11 +00:00
|
|
|
let getrealcoords = do
|
|
|
|
string indent
|
|
|
|
string file
|
|
|
|
char ':'
|
|
|
|
char '\n' `after` coordsParser
|
|
|
|
|
|
|
|
realcoords <- try (Right <$> getrealcoords) <|> (Left <$> getcodeline)
|
|
|
|
codelines <- many getcodeline
|
|
|
|
return $ case realcoords of
|
|
|
|
Left firstcodeline ->
|
|
|
|
Splice file start end expression
|
|
|
|
(unlines $ firstcodeline:codelines)
|
2013-04-14 20:44:05 +00:00
|
|
|
splicetype
|
2013-04-13 21:38:11 +00:00
|
|
|
Right (realstart, realend) ->
|
|
|
|
Splice file realstart realend expression
|
|
|
|
(unlines codelines)
|
2013-04-14 20:44:05 +00:00
|
|
|
splicetype
|
|
|
|
where
|
|
|
|
tosplicetype "declarations" = SpliceDeclaration
|
|
|
|
tosplicetype "expression" = SpliceExpression
|
|
|
|
tosplicetype s = error $ "unknown splice type: " ++ s
|
2013-04-13 20:00:36 +00:00
|
|
|
|
|
|
|
{- Extracts the splices, ignoring the rest of the compiler output. -}
|
|
|
|
splicesExtractor :: Parser [Splice]
|
|
|
|
splicesExtractor = rights <$> many extract
|
|
|
|
where
|
|
|
|
extract = try (Right <$> spliceParser) <|> (Left <$> compilerJunkLine)
|
|
|
|
compilerJunkLine = restOfLine
|
|
|
|
|
|
|
|
{- Modifies the source file, expanding the splices, which all must
|
2013-04-14 17:25:06 +00:00
|
|
|
- have the same splicedFile. Writes the new file to the destdir.
|
2013-04-13 20:00:36 +00:00
|
|
|
-
|
|
|
|
- Each splice's Coords refer to the original position in the file,
|
|
|
|
- and not to its position after any previous splices may have inserted
|
|
|
|
- or removed lines.
|
|
|
|
-
|
|
|
|
- To deal with this complication, the file is broken into logical lines
|
|
|
|
- (which can contain any String, including a multiline or empty string).
|
|
|
|
- Each splice is assumed to be on its own block of lines; two
|
|
|
|
- splices on the same line is not currently supported.
|
|
|
|
- This means that a splice can modify the logical lines within its block
|
|
|
|
- as it likes, without interfering with the Coords of other splices.
|
|
|
|
-
|
2013-04-14 20:44:05 +00:00
|
|
|
- When splicing in declarations, they are not placed on the line
|
|
|
|
- that defined them, because at least with Yesod, that line has another TH
|
|
|
|
- splice, and things would get mixed up. Since declarations are stand
|
|
|
|
- alone, they can go anywhere, and are added to the very end of the file.
|
|
|
|
-
|
2013-04-13 20:00:36 +00:00
|
|
|
- As well as expanding splices, this can add a block of imports to the
|
|
|
|
- file. These are put right before the first line in the file that
|
|
|
|
- starts with "import "
|
|
|
|
-}
|
2013-04-14 17:25:06 +00:00
|
|
|
applySplices :: FilePath -> Maybe String -> [Splice] -> IO ()
|
2013-04-14 20:44:05 +00:00
|
|
|
applySplices destdir imports splices@(first:_) = do
|
2013-04-13 20:00:36 +00:00
|
|
|
let f = splicedFile first
|
2013-04-14 17:25:06 +00:00
|
|
|
let dest = (destdir </> f)
|
2013-04-13 21:38:11 +00:00
|
|
|
putStrLn $ "splicing " ++ f
|
2013-04-13 20:00:36 +00:00
|
|
|
lls <- map (++ "\n") . lines <$> readFileStrict f
|
2013-04-14 17:25:06 +00:00
|
|
|
createDirectoryIfMissing True (parentDir dest)
|
2013-04-14 20:44:05 +00:00
|
|
|
let newcontent = concat $ addimports $
|
|
|
|
expanddeclarations declarationsplices $
|
|
|
|
expandexpressions lls expressionsplices
|
2013-04-14 20:11:12 +00:00
|
|
|
oldcontent <- catchMaybeIO $ readFileStrict dest
|
2013-04-14 19:49:19 +00:00
|
|
|
when (oldcontent /= Just newcontent) $
|
|
|
|
writeFile dest newcontent
|
2013-04-13 20:00:36 +00:00
|
|
|
where
|
2013-04-14 20:44:05 +00:00
|
|
|
(expressionsplices, declarationsplices) =
|
|
|
|
partition isExpressionSplice splices
|
|
|
|
|
|
|
|
expandexpressions lls [] = lls
|
|
|
|
expandexpressions lls (s:rest) =
|
|
|
|
expandexpressions (expandExpressionSplice s lls) rest
|
|
|
|
|
|
|
|
expanddeclarations [] lls = lls
|
|
|
|
expanddeclarations l lls = lls ++ map (mangleCode . splicedCode) l
|
2013-04-13 20:00:36 +00:00
|
|
|
|
|
|
|
addimports lls = case imports of
|
|
|
|
Nothing -> lls
|
|
|
|
Just v ->
|
|
|
|
let (start, end) = break ("import " `isPrefixOf`) lls
|
|
|
|
in if null end
|
|
|
|
then start
|
|
|
|
else concat
|
|
|
|
[ start
|
|
|
|
, [v]
|
|
|
|
, end
|
|
|
|
]
|
|
|
|
|
2013-04-14 20:44:05 +00:00
|
|
|
expandExpressionSplice :: Splice -> [String] -> [String]
|
|
|
|
expandExpressionSplice s lls = concat [before, new:splicerest, end]
|
2013-04-13 20:00:36 +00:00
|
|
|
where
|
|
|
|
cs = spliceStart s
|
|
|
|
ce = spliceEnd s
|
|
|
|
|
|
|
|
(before, rest) = splitAt (coordLine cs - 1) lls
|
|
|
|
(oldlines, end) = splitAt (1 + coordLine (offsetCoord ce cs)) rest
|
|
|
|
(splicestart, splicerest) = case oldlines of
|
|
|
|
l:r -> (expandtabs l, take (length r) (repeat []))
|
|
|
|
_ -> ([], [])
|
|
|
|
new = concat
|
2013-04-13 22:08:32 +00:00
|
|
|
[ joinsplice $ deqqstart $ take (coordColumn cs - 1) splicestart
|
2013-04-13 21:38:11 +00:00
|
|
|
, addindent (findindent splicestart) (mangleCode $ splicedCode s)
|
2013-04-13 20:00:36 +00:00
|
|
|
, deqqend $ drop (coordColumn ce) splicestart
|
|
|
|
]
|
|
|
|
|
|
|
|
{- coordinates assume tabs are expanded to 8 spaces -}
|
|
|
|
expandtabs = replace "\t" (take 8 $ repeat ' ')
|
|
|
|
|
|
|
|
{- splicing leaves $() quasiquote behind; remove it -}
|
|
|
|
deqqstart s = case reverse s of
|
|
|
|
('(':'$':rest) -> reverse rest
|
|
|
|
_ -> s
|
|
|
|
deqqend (')':s) = s
|
|
|
|
deqqend s = s
|
|
|
|
|
2013-04-13 22:08:32 +00:00
|
|
|
{- Prepare the code that comes just before the splice so
|
|
|
|
- the splice will combine with it appropriately. -}
|
|
|
|
joinsplice s
|
|
|
|
-- all indentation? Skip it, we'll use the splice's indentation
|
|
|
|
| all isSpace s = ""
|
|
|
|
-- function definition needs no preparation
|
|
|
|
-- ie: foo = $(splice)
|
|
|
|
| "=" `isSuffixOf` s' = s
|
|
|
|
-- already have a $ to set off the splice
|
|
|
|
-- ie: foo $ $(splice)
|
|
|
|
| "$" `isSuffixOf` s' = s
|
|
|
|
-- need to add a $ to set off the splice
|
|
|
|
-- ie: bar $(splice)
|
|
|
|
| otherwise = s ++ " $ "
|
|
|
|
where
|
|
|
|
s' = filter (not . isSpace) s
|
|
|
|
|
2013-04-13 20:00:36 +00:00
|
|
|
findindent = length . takeWhile isSpace
|
|
|
|
addindent n = unlines . map (i ++) . lines
|
|
|
|
where
|
|
|
|
i = take n $ repeat ' '
|
|
|
|
|
|
|
|
{- Tweaks code output by GHC in splices to actually build. Yipes. -}
|
|
|
|
mangleCode :: String -> String
|
2013-04-14 20:53:41 +00:00
|
|
|
mangleCode = nested_instances . fix_bad_escape . remove_package_version
|
2013-04-13 21:15:05 +00:00
|
|
|
where
|
|
|
|
{- GHC may incorrectly escape "}" within a multi-line string. -}
|
|
|
|
fix_bad_escape = replace " \\}" " }"
|
|
|
|
|
2013-04-14 20:53:41 +00:00
|
|
|
{- GHC may output this:
|
|
|
|
-
|
|
|
|
- instance RenderRoute WebApp where
|
|
|
|
- data instance Route WebApp
|
|
|
|
- ^^^^^^^^
|
|
|
|
- The marked word should not be there.
|
|
|
|
-
|
|
|
|
- FIXME: This is a yesod-specific hack, it should look for the
|
|
|
|
- outer instance.
|
|
|
|
-}
|
|
|
|
nested_instances = replace " data instance Route" " data Route"
|
|
|
|
|
2013-04-13 21:15:05 +00:00
|
|
|
{- GHC may add full package and version qualifications for
|
|
|
|
- symbols from unimported modules. We don't want these.
|
|
|
|
-
|
|
|
|
- Examples:
|
|
|
|
- "blaze-html-0.4.3.1:Text.Blaze.Internal.preEscapedText"
|
|
|
|
- "ghc-prim:GHC.Types.:"
|
|
|
|
-}
|
|
|
|
remove_package_version s = case parse findQualifiedSymbols "" s of
|
|
|
|
Left e -> s
|
|
|
|
Right symbols -> concat $
|
|
|
|
map (either (\c -> [c]) mangleSymbol) symbols
|
|
|
|
|
|
|
|
findQualifiedSymbols :: Parser [Either Char String]
|
|
|
|
findQualifiedSymbols = many $
|
|
|
|
try (Right <$> qualifiedSymbol) <|> (Left <$> anyChar)
|
|
|
|
|
|
|
|
qualifiedSymbol :: Parser String
|
|
|
|
qualifiedSymbol = do
|
|
|
|
token
|
|
|
|
char ':'
|
|
|
|
token
|
|
|
|
|
|
|
|
token :: Parser String
|
|
|
|
token = many1 $ satisfy isAlphaNum <|> oneOf "-.'"
|
|
|
|
|
|
|
|
mangleSymbol "GHC.Types." = ""
|
|
|
|
mangleSymbol s = s
|
2013-04-13 20:00:36 +00:00
|
|
|
|
2013-04-14 17:25:06 +00:00
|
|
|
main :: IO ()
|
|
|
|
main = go =<< getArgs
|
|
|
|
where
|
|
|
|
go (destdir:log:header:[]) = run destdir log (Just header)
|
|
|
|
go (destdir:log:[]) = run destdir log Nothing
|
|
|
|
go _ = error "usage: EvilSplicer destdir logfile [headerfile]"
|
|
|
|
|
|
|
|
run destdir log mheader = do
|
|
|
|
r <- parseFromFile splicesExtractor log
|
|
|
|
case r of
|
|
|
|
Left e -> error $ show e
|
|
|
|
Right splices -> do
|
|
|
|
let groups = groupBy (\a b -> splicedFile a == splicedFile b) splices
|
|
|
|
imports <- maybe (return Nothing) (catchMaybeIO . readFile) mheader
|
|
|
|
mapM_ (applySplices destdir imports) groups
|