-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpre.hs
275 lines (240 loc) · 8.81 KB
/
hpre.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
{-# LANGUAGE CPP, LambdaCase #-}
import Control.Monad
import Control.Monad.State
import Data.Char
import Data.Functor (($>))
import Data.List
import Data.Maybe
import Data.Version
import Debug.Trace (trace)
import System.Environment (getArgs)
import Text.Read.Lex (isSymbolChar)
#ifdef BUILDING
import Paths_hpre (version)
#else
version = Version [] ["n/a"]
#endif
-- YMMV
tabWidth = 3 :: Int
importMarker = "--+" :: String
{-# INLINE __ #-}
__ = True -- second-best
warn :: String -> a -> a
warn = trace . ("Warning: " ++)
abort :: String -> a
abort = errorWithoutStackTrace
-- Yank up to end of quote.
-- TODO multiline quote support
skipQuote :: String -> (String, String)
skipQuote = go "" where
go q [] = (q, [])
go q "\\" = (q++['\\'], [])
go q ('"':s) = (q++"\"", s)
go q ('\\':'\\':s) = go (q++"\\\\") s
go q ('\\':'"':s) = go (q++"\\\"") s
go q (c:s) = go (q++[c]) s
untab :: String -> String
untab = go (0::Int) where
go _ [] = []
go m ('\t':l) = replicate (tabWidth-(m`mod`tabWidth)) ' ' ++ go 0 l
go m (c:l) = c : go (m+1) l
isNameChar :: Char -> Bool
isNameChar c = isAlphaNum c || c=='\'' || c=='_'
-- Allow "ticks" or "underscores" as digit separators.
-- That is, not settling on one proposal yet.
-- With NumericUnderscores an accepted extension, this is deprecated.
-- It should be removed eventually, as it can interfere with alignment
-- (and that isn't worth fixing since deprecated).
tickedNumLine :: String -> String
tickedNumLine l@(x:m)
| isDigit x =
let (p1, p2) = span (\y -> isDigit y || isTick y) l
p1' = filter (not.isTick) p1
++ reverse (takeWhile isTick $ reverse p1) -- prob not needed
in (if p1/=p1' && '\'' `elem` p1 then warn "Ticked numbers are deprecated." else id)
$ p1' ++ tickedNumLine p2
| isNameChar x =
let (nm, rest) = span isNameChar l
in nm ++ tickedNumLine rest
| x=='\\' || x=='\'', mh:mt <- m
= x : mh : tickedNumLine mt
| x=='"' = let (a, b) = skipQuote m in '"' : a ++ tickedNumLine b
| __ = x : tickedNumLine m
where
isTick c = c == '\'' || c == '_'
tickedNumLine _ = []
tickedNums :: [String] -> [String]
tickedNums ls = if "--+" `elem` ls then ls else map tickedNumLine ls
elseWord :: String
elseWord = "True"
-- Empty guards are filled in with True.
-- This was formerly monadic and could perhaps be simplified.
emptyGuard :: String -> String
emptyGuard (q:c:s) | q=='\\' || q=='\'' =
[q, c] ++ emptyGuard s
emptyGuard ('"':s) =
let (a, b) = skipQuote s in '"' : a ++ emptyGuard b
emptyGuard (c1:'|':c2:s) | isSpace c1 && isSpace c2 =
c1 : '|' : pf ++ emptyGuard p2 where
(p1, p2) = span isSpace s
p2' = takeWhile isSymbolChar p2
pf | p2' `elem` ["=", "→", "->"] =
case length p1 of
x | x<size -> elseWord
| __ -> c2 : elseWord ++ drop size p1
where size = length elseWord
| __ = c2 : p1
emptyGuard (c:s) = c : emptyGuard s
emptyGuard _ = []
columnPragma :: Int -> String
columnPragma col = "{-#COLUMN " ++ show col ++ "#-}"
type DittoHist = [(Int, String)]
histAtIndent :: Int -> DittoHist -> (Maybe String, DittoHist)
histAtIndent ind hist =
case dropWhile ((> ind) . fst) hist of
(ind', s) : hist' | ind == ind' -> (Just s, hist')
hist' -> (Nothing, hist')
-- This recomputes some stuff for better separation.
expandDitto :: String -> String -> String
expandDitto val line =
pre ++ val ++ columnPragma (length (pre ++ ditto ++ sps) + 1) ++ rest
where
(pre, l1) = span isSpace line
(ditto, l2) = break isSpace l1
(sps, rest) = span isSpace l2
letFinderS :: String -> [(Int, String)] -> [(Int, String)]
letFinderS line = flip loop chunks where
chunks = unfoldr chunk (0, line)
chunk (_, "") = Nothing
chunk (col, s) = Just ((s1, s2, col'), (col' + length s2, s3)) where
(s1, s1') = break isNameChar s
(s2, s3) = span isNameChar s1'
col' = col + length s1
loop ac ((_, "let", _) : (sps, nm, ind) : rest)
| all isSpace sps = loop ((ind, nm) : ac) rest
loop ac (_ : rest) = loop ac rest
loop ac _ = ac
-- "Ditto marks" for repeated names in definitions.
-- TODO? allow mid-line names to be ditto'ed
dittoM :: String -> State DittoHist String
dittoM line = do
let (sp1, rest) = span isSpace line
indent = length sp1
(cur, hist) <- gets $ histAtIndent indent
let put' = put . letFinderS line
let doDitto = case cur of
Just val -> do
put' $ (indent, val) : hist
pure $ expandDitto val line
_ -> abort $ "Orphaned ditto mark: " ++ line
case rest of
'\'':'\'':x:_ | isSpace x -> doDitto
c:x:_ | c `elem` "”〃", isSpace x -> doDitto
c:_ | isLower c -> do
put' $ (indent, takeWhile isNameChar rest) : hist
pure line
[] -> pure line
'-':'-':_ -> pure line
'{':'-':_ -> pure line
'-':'}':_ -> pure line
_ -> put' hist $> line
dittoMarks :: [String] -> [String]
dittoMarks inp = flip evalState [] $ traverse dittoM inp
-- Strips final comments and also spaces while it's at it.
-- TODO can get messed up by quotes
decomment :: String -> String
decomment s
| null s' || take 2 s' == "--"
= []
| isSymbolChar c = let (a, b) = span isSymbolChar s in a ++ decomment b
| __ = c : decomment rest
where
s' = dropWhile isSpace s
c:rest = s
stripSpace :: String -> String
stripSpace = reverse . dropWhile isSpace . reverse . dropWhile isSpace
-- If c is the first non-space character, replace with space.
spaceOut :: Char -> String -> Maybe String
spaceOut c s =
case span isSpace s of
(sp, c' : s') | c == c' -> Just $ sp ++ ' ' : s'
_ -> Nothing
spanCommentLines :: [String] -> ([String], [String])
spanCommentLines = span (null . stripSpace . decomment)
commasR :: [String] -> [String]
commasR [] = []
commasR (l:ls) = (: commasR ls) $ fromMaybe l $ do
',' : l' <- pure $ reverse $ decomment l
nxt : _ <- pure $ snd $ spanCommentLines ls
c : _ <- pure $ dropWhile isSpace nxt
guard $ c `elem` "])}"
pure $ reverse l'
commasL :: [String] -> [String]
commasL [] = []
commasL (l:ls) = fromMaybe (l : commasL ls) $ do
c : _ <- pure $ reverse $ decomment l
guard $ c `elem` "{(["
let (comms, rest) = spanCommentLines ls
nxt : ls' <- pure rest
nxt' <- spaceOut ',' nxt
pure $ l : comms ++ nxt' : commasL ls'
dataBarsL :: [String] -> [String]
dataBarsL [] = []
dataBarsL (l:ls) = fromMaybe (l : dataBarsL ls) $ do
guard $ "data " `isPrefixOf` l
'=' : _ <- pure $ reverse $ decomment l
let (comms, rest) = spanCommentLines ls
nxt : ls' <- pure rest
nxt' <- spaceOut '|' nxt
pure $ l : comms ++ nxt' : dataBarsL ls'
imports :: [String] -> [String]
imports xs = let (a, b) = break (== importMarker) xs in a ++ loop b where
loop (x:l)
| x == importMarker = "" : loop l
| "import " `isPrefixOf` x, x' <- dropWhile isSpace $ drop 6 x
= if "qualified" `isPrefixOf` x'
then warn "Use of qualified in multiplex import." skip
else
let (a, b) = span (\case c:_ -> isSpace c; _ -> True) l
in doImport (intercalate "\n" $ map decomment $ x' : a) : loop b
| __ = skip
where skip = x : loop l
loop _ = []
doImport :: String -> String
doImport blk = go 0 "" rest where
(name, rest) =
case break (\c -> c==',' || isSpace c) blk of
(a, b) | '"':_ <- dropWhile isSpace a
, (b1, b2) <- break (==' ') $ dropWhile isSpace b
-> (a ++ " " ++ b1, b2)
pair -> pair
go :: Int -> String -> String -> String
go p acc nx =
let (nx1, nx2) = break (`elem` ",()") nx
acc' = acc ++ nx1
in case nx2 of
'(':b -> go (p+1) (acc' ++ "(") b
')':b | p > 0 -> go (p-1) (acc' ++ ")") b
',':b | p == 0 ->
render acc' ++ (if all isSpace b then "" else ";" ++ go 0 "" b)
c:b -> go p (acc' ++ [c]) b
_ -> render acc'
render acc =
"import "
++ (if "as" `isPrefixOf` dropWhile isSpace acc
then "qualified " else "")
++ name ++ " " ++ acc
process :: String -> String
process =
unlines . imports . dataBarsL . commasL . commasR . dittoMarks
. map (emptyGuard . untab) . tickedNums . lines
main = do
args <- getArgs
case args of
["--version"] -> putStrLn $ "hpre v" ++ showVersion version
[nm, inf, outf] -> do
file <- readFile inf
writeFile outf
$ "{-# LINE 1 \"" ++ nm ++ "\" #-}\n" ++ process file
[] -> interact process
_ -> abort "Usage: hpre [name infile outfile]"