This repository has been archived by the owner on Nov 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.hs
94 lines (78 loc) · 3.2 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
-- | The Interpol pre-processor.
module Main (
main
) where
import Data.Generics ( mkT, everywhere, mkQ, gcount, GenericQ )
import Data.List ( isPrefixOf, tails )
import Language.Haskell.Exts ( prettyPrint
, ParseResult(..), parseFileWithExts
, Module(..), Exp(..), QOp(..)
, QName(..), Name(..), Literal(..)
, ImportDecl(..), ModuleName(..), SrcLoc(..) )
import System.Environment ( getArgs )
import Text.Printf ( printf )
import Text.Regex.Posix ( (=~) )
main :: IO ()
main = do
[_, fin, fout] <- getArgs
res <- parseFileWithExts [] fin
case res of
ParseFailed loc reason -> do
printf "%s:%s: Parse Failed: %s" fin (show loc) reason
writeFile fout =<< readFile fin
ParseOk m -> do
let magicLine = "{-# LINE 1 \"" ++ fin ++ "\" #-}\n"
writeFile fout $ magicLine ++ prettyPrint (transform m)
transform :: Module -> Module
transform = addNecessaryImports . transformed
where
-- Replace strings by interpol operator
transformed = everywhere (mkT trans)
-- How many interpol operates are present in the module
countInterpolUses :: GenericQ Int
countInterpolUses = gcount (mkQ False usesInterpol)
addNecessaryImports m@(Module _ _ _ _ _ importDecls _)
| usesInterpol && not alreadyImported = addDecl m
| otherwise = m
where
usesInterpol = countInterpolUses m > 0
alreadyImported = any isInterpolImport importDecls
isInterpolImport ImportDecl { importModule = ModuleName name
, importQualified = isQualified } =
isQualified && name == "Text.Interpol"
qualifiedInterpolImport = ImportDecl {
importLoc = SrcLoc { srcFilename = ""
, srcLine = 0
, srcColumn = 0 }
, importModule = ModuleName "Text.Interpol"
, importQualified = True
, importSrc = False
, importPkg = Nothing
, importAs = Nothing
, importSpecs = Nothing }
addDecl :: Module -> Module
addDecl (Module sl mn mps mwt mes ids ds) =
let ids' = qualifiedInterpolImport : ids
in Module sl mn mps mwt mes ids' ds
trans :: Exp -> Exp
trans (Lit (String s)) = interpol s
trans e = e
identRE = "\\{[A-z_][A-z0-9_]*}"
interpolOperator = Symbol "Text.Interpol.^-^"
usesInterpol :: Name -> Bool
usesInterpol n = n == interpolOperator
interpol :: String -> Exp
interpol s
| s =~ identRE = Paren $ go s
| otherwise = Lit (String s)
go :: String -> Exp
go s = let (before, ident, after) = s =~ identRE
e = InfixApp (Lit (String before))
appendOp
(Var (UnQual (Ident $ ti ident)))
in case ident of
"" -> Lit (String before)
_ -> InfixApp e appendOp $ go after
appendOp :: QOp
appendOp = QVarOp (UnQual interpolOperator)
ti = tail . init