This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hie.hs
502 lines (419 loc) · 18.3 KB
/
hie.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
{-# LANGUAGE TemplateHaskell, NoMonomorphismRestriction #-}
-- | I, Chris Monsanto <[email protected]>, wrote this code. It's under GPLv3.
module Main where
import qualified Hie.Language.Haskell.Exts.Annotated as L
import qualified Hie.Language.Haskell.Exts.Pretty as Pretty
import qualified Language.Preprocessor.Cpphs as CPP
import qualified Data.Map as Map
import Prelude hiding ((.), id)
import Control.Category
import Data.Maybe
import Data.List
import Data.Label
import Text.Printf
import Data.Ord
import System.Environment
import System.IO
import System.Directory
import Control.Monad
import System.Exit
import Text.Regex
-- Tags ------------------------------------------------------------------------
data InstanceData = InstanceDecl
| ClassMember String
| NotInstance
deriving (Show, Eq, Ord)
data Ident = Ident {
_identStr :: String, -- ^What is the name of the object in question?
_identInstanceOf :: InstanceData -- ^Is this an instance? Of what?
} deriving (Show, Eq, Ord)
data Tag = Tag {
_tagPos :: (Int, Int), -- ^Where is this object? (line, column)
_tagParent :: Maybe Ident, -- ^This is only for export definitions.
_tagSignature :: Maybe String, -- ^Tell me about this object.
_tagQuickHelp :: Maybe String -- ^... in English, please...
} deriving Show
newIdent s = Ident s NotInstance
newTag loc = Tag loc Nothing Nothing Nothing
mergeTag :: Tag -> Tag -> Tag
mergeTag (Tag _ _ s _) (Tag p l s' h) = Tag p l (maybe s Just s') h
-- Database --------------------------------------------------------------------
-- | Here is our database of identifiers.
type Database = Map.Map Ident Tag
data Export = ExpString String | ExpAll String | ExpModule String
data Import = Import {
_impModule :: String,
_impList :: [Export],
_impAlias :: String,
_impQualified :: Bool,
_impHiding :: Bool
}
$(mkLabels [''Ident, ''Tag, ''Import])
type Result = (String, [Import], [Export], Database)
-- | Combine two databases.
merge :: Database -> Database -> Database
merge = Map.unionWith mergeTag
mergeWithParent :: Database -> Database -> Database
mergeWithParent m m' = Map.unionWith mergeTag m (Map.map (set tagParent (Just $ head $ Map.keys m)) m')
merges :: (a -> Database) -> [a] -> Database
merges f = Map.unionsWith mergeTag . map f
createTags :: FilePath -> String -> Either Result String
createTags file s =
case L.parseFileContentsWithComments mode s of
L.ParseOk (m, comments) ->
case extractModule m of
((line, _), (mod, imps, exps, db)) ->
let db' = commentsFixUp db $
dropWhile (\((line', _), _) -> line > line') $
map extractComment comments
in
Left (mod, imps, exps, aliasModules mod db')
L.ParseFailed (L.SrcLoc _ l c) s -> Right (printf "%i, %i: %s" l c s)
where
exts = fromMaybe [] (L.readExtensions s)
mode = L.ParseMode {
L.parseFilename = "", -- we unlit before, so don't do it again.
L.extensions = [
L.PackageImports,
L.TypeFamilies, -- uses family keyword
L.GADTs,
L.StandaloneDeriving,
L.TemplateHaskell,
L.FunctionalDependencies,
L.EmptyDataDecls,
L.ExistentialQuantification,
L.KindSignatures,
L.BangPatterns, -- uses ! as a keyword
L.ScopedTypeVariables,
L.ViewPatterns,
L.NamedFieldPuns,
L.RecordWildCards,
L.ImplicitParams, -- uses ? as a keyword, %keyword
L.UnboxedTuples, -- uses (#, #) as a keyword
L.MagicHash, -- uses # as a keyword
L.QuasiQuotes,
L.ExplicitForall,
--L.Arrows, -- -< and -<<
L.UnicodeSyntax,
L.RecursiveDo, -- mdo
L.ForeignFunctionInterface, -- foreign,
L.PatternGuards,
L.MultiParamTypeClasses,
L.TypeOperators,
L.TupleSections,
L.FlexibleContexts] ++ exts,
L.ignoreLanguagePragmas = False,
L.ignoreLinePragmas = False,
L.fixities = Nothing
}
-- | Set the signature of the database.
setSig :: (Pretty.Pretty a) => a -> Database -> Database
setSig k = Map.map (set tagSignature . Just $ pretty k)
aliasModules :: String -> Database -> Database
aliasModules "" db = db
aliasModules s db = Map.union db (Map.mapKeys f db)
where
f (Ident s' NotInstance) = (Ident (s ++ "." ++ s') NotInstance)
f i = i
-- | Add a context to the signature.
mapSigCtxt :: (Pretty.Pretty a) => a -> Database -> Database
mapSigCtxt k = Map.map (modify tagSignature (fmap (printf ctxtPretty (pretty k) ++)))
-- Comments
extractComment :: L.Comment -> ((Int, Int), String)
extractComment (L.Comment _ span s) = (L.srcSpanStart span, s)
commentsFixUp :: Database -> [((Int, Int), String)] -> Database
commentsFixUp db comments = Map.fromList $ commentsFixUp' objs comments
where
objs = sortBy (comparing (get tagPos . snd)) $ Map.toList db
commentsFixUp' objs [] = objs
commentsFixUp' [] _ = []
commentsFixUp' ((ident, tag) : objs) comments =
let
(line, column) = get tagPos tag
(aboveComments, rest) = span (\((line', column'), str) -> line > line') comments
sideComments = filter (\((line', column'), str) -> line == line' && column' > column) rest
in
(ident, set tagQuickHelp (Just $ extractHaddock aboveComments sideComments) tag) : commentsFixUp' objs rest
killIndent :: String -> String
killIndent s = let l = reverse $ dropWhile (== "") $ reverse $ dropWhile (== "") $ lines s
minLength = foldl min 9999 (map (length . takeWhile (== ' ')) $ filter (/= "") l)
in
unlines $ map (drop minLength) l
extractHaddock :: [((Int, Int), String)] -> [((Int, Int), String)] -> String
extractHaddock acoms scoms =
case (acom, scom) of
(_, Just s) -> s
(Just s, _) -> s
(_, _) -> ""
where
acom = case dropWhile (not . (isPrefixOf " |")) $ map snd acoms of
[] -> Nothing
(x : xs) -> Just (killIndent $ unlines ((fromJust $ stripPrefix " |" x) : xs))
scom = case scoms of
[(_, s)] -> case stripPrefix " ^" s of
Just x -> Just $ dropWhile (== ' ') x
Nothing -> Nothing
_ -> Nothing
-- Prettying -------------------------------------------------------------------
-- | What is the prefix for a context?
ctxtPretty = "[%s] "
-- | Display an object very prettily.
pretty = Pretty.prettyPrintStyleMode
Pretty.style
Pretty.defaultMode { Pretty.classIndent = 4,
Pretty.spacing = False}
-- Extraction ------------------------------------------------------------------
type Span = L.SrcSpanInfo
noPrelude = any (== "NoImplicitPrelude") . concat . mapMaybe (\x -> case x of
L.LanguagePragma _ ns -> Just $ map fromName ns
_ -> Nothing)
addPrelude :: [Import] -> [Import]
addPrelude l | not $ any ((== "Prelude") . get impModule) l = Import "Prelude" [] "Prelude" False True : l
| otherwise = l
-- | Re-exports, exported database, all database.
extractModule :: L.Module Span -> ((Int, Int), Result)
extractModule (L.Module _ mh prags importdecls decls) =
case mh of
-- note we grab the end line and column, not the normal beginning and end.
Just k@(L.ModuleHead (L.SrcSpanInfo (L.SrcSpan _ _ _ line column) _) (L.ModuleName _ s) _ exportspec) ->
((line, column),
(s, imports',
case concatMap extractExportSpec (maybe [] (\(L.ExportSpecList _ l) -> l) exportspec) of
[] -> [ExpModule s]
xs -> xs,
decls'))
Nothing -> ((0, 0), ("", imports', [], decls'))
where
imports = mapMaybe extractImport importdecls
imports' | noPrelude prags = imports
| otherwise = addPrelude imports
decls' = merges extractDecl decls
extractModule _ = error "no"
-- TODO don't forget to make a concat
fromName :: L.Name Span -> String
fromName (L.Ident _ s) = s
fromName (L.Symbol _ s) = s
fromQName :: L.QName Span -> (String, String)
fromQName (L.Qual _ (L.ModuleName _ s) n) = (s, fromName n)
fromQName (L.UnQual _ n) = ("", fromName n)
fromQName (L.Special {}) = ("", "")
qualit ("", y) = y
qualit (x, y) = x ++ "." ++ y
fromCName :: L.CName Span -> String
fromCName (L.VarName _ n) = fromName n
fromCName (L.ConName _ n) = fromName n
extractExportSpec :: L.ExportSpec Span -> [Export]
extractExportSpec (L.EVar _ q) = [ExpString (qualit $ fromQName q)]
extractExportSpec (L.EAbs _ q) = [ExpString (qualit $ fromQName q)]
extractExportSpec (L.EThingAll _ q) = [ExpAll (qualit $ fromQName q)]
extractExportSpec (L.EThingWith _ q cs) = ExpString (qualit (x, y)) :
map (\y' -> ExpString $ qualit (x, fromCName y')) cs
where
(x, y) = fromQName q
extractExportSpec (L.EModuleContents _ (L.ModuleName _ s)) = [ExpModule s]
extractImportSpec :: L.ImportSpec Span -> [Export]
extractImportSpec (L.IVar _ n) = [ExpString (fromName n)]
extractImportSpec (L.IAbs _ n) = [ExpString (fromName n)]
extractImportSpec (L.IThingAll _ n) = [ExpAll (fromName n)]
extractImportSpec (L.IThingWith _ n cs) = ExpString (fromName n) :
map (ExpString . fromCName) cs
extractImport :: L.ImportDecl Span -> Maybe Import
extractImport (L.ImportDecl _ (L.ModuleName _ s) qual issrc _ alias importlist)
| not issrc = Just $ case importlist of
Just (L.ImportSpecList _ hidden impspecs) ->
Import s (concatMap extractImportSpec impspecs) alias' qual hidden
Nothing -> Import s [] alias' qual True
| otherwise = Nothing
where
alias' = maybe s (\(L.ModuleName _ s) -> s) alias
extractDecl :: L.Decl Span -> Database
extractDecl k@(L.TypeDecl _ head type_) =
setSig k (extractDeclHead head)
extractDecl k@(L.TypeFamDecl _ head kind) =
setSig k (extractDeclHead head)
extractDecl k@(L.DataDecl _ _ ctxt head decls _) =
setSig k (extractDeclHead head) `mergeWithParent` mapSigCtxt head (merges extractQualConDecl decls)
extractDecl k@(L.GDataDecl _ _ ctxt head kind decls _) =
setSig k (extractDeclHead head) `mergeWithParent` mapSigCtxt head (merges extractGadtDecl decls)
extractDecl k@(L.DataFamDecl _ ctxt head kind) =
setSig k (extractDeclHead head)
extractDecl k@(L.ClassDecl src ctxt head deps clsdecls) =
setSig (L.ClassDecl src ctxt head deps (Just $ decls)) (extractDeclHead head)
`mergeWithParent`
mapSigCtxt head (merges extractClassDecl decls)
where
decls = mapMaybe doctorClassDecl $ fromMaybe [] clsdecls
extractDecl k@(L.TypeSig _ names type_) =
setSig k $ merges extractName names
extractDecl k@(L.FunBind _ (L.Match _ name _ _ _ : _)) =
extractName name
extractDecl k@(L.FunBind _ (L.InfixMatch _ _ name _ _ _ : _)) =
extractName name
extractDecl (L.PatBind _ pat _ _ _) =
extractPat pat
extractDecl (L.InstDecl loc ctxt head instdecls) =
Map.mapKeys (set identInstanceOf $ InstanceDecl)
(setSig (L.InstDecl loc ctxt head Nothing) (Map.singleton (newIdent (pretty head)) (newTag (extractLoc loc))))
`merge`
Map.mapKeys (set identInstanceOf $ ClassMember (pretty head)) (merges extractInstDecl (fromMaybe [] instdecls))
extractDecl k@(L.ForImp _ _ _ _ name type_) =
setSig k (extractName name)
extractDecl _ = Map.empty
extractInstDecl :: L.InstDecl Span -> Database
extractInstDecl (L.InsDecl _ decl) = extractDecl decl
extractInstDecl _ = Map.empty
extractDeclHead :: L.DeclHead Span -> Database
extractDeclHead k@(L.DHead _ name vars) =
setSig k $ extractName name
extractDeclHead k@(L.DHInfix _ var1 name var2) =
setSig k $ extractName name
extractDeclHead (L.DHParen _ head') =
extractDeclHead head'
extractQualConDecl :: L.QualConDecl Span -> Database
extractQualConDecl k@(L.QualConDecl _ _ ctxt (L.ConDecl _ name btypes)) =
setSig k (extractName name)
extractQualConDecl k@(L.QualConDecl _ _ ctxt (L.InfixConDecl _ btype1 name btype2)) =
setSig k (extractName name)
extractQualConDecl k@(L.QualConDecl _ _ ctxt (L.RecDecl _ name fields)) =
setSig k (extractName name) `merge` merges extractFieldDecl fields
extractFieldDecl :: L.FieldDecl Span -> Database
extractFieldDecl k@(L.FieldDecl _ names btype) =
setSig k (merges extractName names)
extractGadtDecl :: L.GadtDecl Span -> Database
extractGadtDecl k@(L.GadtDecl _ name type_) =
setSig k (extractName name)
extractClassDecl :: L.ClassDecl Span -> Database
extractClassDecl k@(L.ClsDecl _ decl) = extractDecl decl
extractClassDecl k@(L.ClsDataFam _ ctxt head kind) =
setSig k (extractDeclHead head)
extractClassDecl k@(L.ClsTyFam _ head kind) =
setSig k (extractDeclHead head)
extractClassDecl _ = Map.empty
extractPat :: L.Pat Span -> Database
extractPat (L.PVar _ name) = extractName name
extractPat (L.PApp _ _ pats) = merges extractPat pats
extractPat (L.PTuple _ _ pats) = merges extractPat pats
extractPat (L.PList _ pats) = merges extractPat pats
extractPat (L.PParen _ pat) = extractPat pat
extractPat (L.PAsPat _ name pat) = extractName name `merge` extractPat pat
extractPat (L.PIrrPat _ pat) = extractPat pat
extractPat (L.PatTypeSig _ pat _) = extractPat pat
extractPat (L.PBangPat _ pat) = extractPat pat
extractPat _ = Map.empty
extractName :: L.Name Span -> Database
extractName (L.Ident loc name) = Map.singleton (newIdent name) (newTag (extractLoc loc))
extractName (L.Symbol loc name) = Map.singleton (newIdent name) (newTag (extractLoc loc))
extractLoc :: Span -> (Int, Int)
extractLoc (L.SrcSpanInfo (L.SrcSpan _ line column _ _) _) = (line, column)
-- Doctoring -------------------------------------------------------------------
doctorClassDecl :: L.ClassDecl Span -> Maybe (L.ClassDecl Span)
doctorClassDecl k@(L.ClsDecl src (L.TypeSig src' names types)) = Just k
doctorClassDecl k@(L.ClsDataFam {}) = Just k
doctorClassDecl k@(L.ClsTyFam {}) = Just k
doctorClassDecl _ = Nothing
-- Export ----------------------------------------------------------------------
quote :: String -> String
quote s = "\"" ++ quote' s ++ "\""
where
quote' [] = []
quote' ('"' : xs) = "\\\"" ++ quote' xs
quote' ('\\' : xs) = "\\\\" ++ quote' xs
quote' (x : xs) = x : quote' xs
tplList :: [String] -> String
tplList l = "(list " ++ intercalate "\n" l ++ ")"
tplMaybe :: Maybe String -> String
tplMaybe = fromMaybe "nil"
tplBool :: Bool -> String
tplBool True = "t"
tplBool False = "nil"
tplExport :: Export -> String
tplExport (ExpString s) = printf "(list 'id %s)" (quote s)
tplExport (ExpAll s) = printf "(list 'all %s)" (quote s)
tplExport (ExpModule s) = printf "(list 'mod %s)" (quote s)
tplImport :: Import -> String
tplImport i = printf
(unlines ["(make-hieimport",
":name %s",
":list %s",
":alias %s",
":is-qualified %s",
":is-hidden %s)"])
(quote $ get impModule i)
(tplList (map tplExport $ get impList i))
(quote $ get impAlias i)
(tplBool $ get impQualified i)
(tplBool $ get impHiding i)
tplMakeName :: String -> InstanceData -> String
tplMakeName s (ClassMember c) = s ++ " / " ++ c
tplMakeName s _ = s
tplInstanceOf :: InstanceData -> String
tplInstanceOf NotInstance = "nil"
tplInstanceOf _ = "t"
tplLocalDef :: FilePath -> (Ident, Tag) -> String
tplLocalDef file (Ident name inst, Tag (line, column) parent sig qh) = printf
(unlines ["(make-hiedef",
":name %s",
":is-instance %s",
":parent %s",
":file %s",
":line %i",
":column %i",
":signature %s",
":help %s)"])
(quote (tplMakeName name inst))
(tplInstanceOf inst)
(tplMaybe (fmap (quote . get identStr) parent))
(quote file)
line
column
(tplMaybe (fmap quote sig))
(tplMaybe (fmap quote qh))
elisp :: FilePath -> Result -> String
elisp file (mod, imps, exps, db) = printf
(unlines ["(setq *hie-load* (make-hiemod :name %s",
":defs %s",
":imports %s",
":exports %s))"])
(if (null mod) then "nil" else (quote mod))
(tplList $ map (tplLocalDef file) $ Map.toList db)
(tplList $ map tplImport imps)
(tplList $ map tplExport exps)
-- Driver ----------------------------------------------------------------------
usage = putStrLn "Usage: hie file-to-jump-to (reads file from stdin; writes to stdout)"
runCPP :: Bool -> String -> IO String
runCPP unlit contents = CPP.runCpphs cppOpts "" contents
where
cppOpts = CPP.defaultCpphsOptions {
CPP.defines = [("__GLASGOW_HASKELL__", "0"),
("SIZEOF_HSWORD", "4"),
("FLT_RADIX", "2"),
("WORD_SIZE_IN_BITS", "32"),
("HAVE_KQUEUE", "1"),
("CALLCONV", "ccall")],
CPP.boolopts = CPP.defaultBoolOptions {
CPP.hashline = False,
CPP.warnings = False,
CPP.literate = unlit
}
}
sub regex repl s = subRegex (mkRegex regex) s repl
fixBugsHack :: String -> String
fixBugsHack = --sub "\\(# " "(" . -- stupid bug in haskell-src-exts
-- sub " #\\)" ")" . -- stupid bug in haskell-src-exts
-- sub "0x[A-Fa-f0-9]+#" "0" . -- stupid bug in haskell-src-exts
-- sub "SPECIALISE \\[[0-9]\\]" "SPECIALISE" . -- stupid bug in haskell-src-exts
-- sub "'\\\\x[A-Fa-f0-9]+'" "'k'" . -- stupid bug in haskell-src-exts
sub "^[A-Z0-9_]+\\(.*\\)[ \t]*$" "" -- try to guess cpp macros. this isn't even valid haskell syntax
main :: IO ()
main = do
files <- getArgs
case files of
[path] -> do
contents <- getContents >>= runCPP (".lhs" `isSuffixOf` path) >>= (return . fixBugsHack)
--let contents' = fixBugsHack contents
--putStrLn contents'
case createTags path contents of
Left res -> putStrLn (elisp path res) >>
exitSuccess
Right s -> hPutStrLn stderr s >> exitFailure
_ -> usage