-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathComments.hs
More file actions
189 lines (162 loc) · 6.03 KB
/
Comments.hs
File metadata and controls
189 lines (162 loc) · 6.03 KB
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
{-# LANGUAGE DeriveDataTypeable, Safe #-}
-----------------------------------------------------------------------------
-- |
-- Module : Language.Haskell.Exts.Comments
-- Copyright : (c) JP Moresmau 2015
-- License : BSD-style (see the file LICENSE.txt)
--
-- Maintainer : Niklas Broberg, d00nibro@chalmers.se
-- Stability : experimental
-- Portability : portable
--
-- This module processes comments along with an annotated AST,
-- to be able to associate Haddock comments with the actual item
-- they refer to.
--
-- Example:
--
-- @
-- let
-- parse1Result :: ParseResult (Module SrcSpanInfo,[Comment])
-- parse1Result =
-- parseFileContentsWithComments
-- (defaultParseMode { parseFilename = file })
-- contents
-- withC :: ParseResult (Module (SrcSpanInfo,[Comment]))
-- withC = case parse1Result of
-- ParseOk res -> ParseOk $ associateHaddock res
-- ParseFailed sloc msg -> ParseFailed sloc msg
-- @
--
-- In this code sample, parse1Result is what you get when you parse a file:
-- a 'Module' annotated wth 'SrcSpanInfo', and a list of comments
-- After passing the result to 'associateHaddock', you get a 'Module'
-- annotated with both a 'SrcSpanInfo' and the list of 'Comment' related to the
-- specific AST node.
--
-----------------------------------------------------------------------------
module Language.Haskell.Exts.Comments
( associateHaddock
, Comment(..), UnknownPragma(..)
) where
import Language.Haskell.Exts.Syntax
import Language.Haskell.Exts.SrcLoc
import Data.Char (isSpace)
import Data.Traversable
import Data.Data
-- | A Haskell comment. The 'Bool' is 'True' if the comment is multi-line, i.e. @{- -}@.
data Comment = Comment Bool SrcSpan String
deriving (Eq,Show,Typeable,Data)
-- | An unknown pragma.
data UnknownPragma = UnknownPragma SrcSpan String
deriving (Eq,Show,Typeable,Data)
-- | Associates an AST with Source Span Information
-- with relevant Haddock comments
associateHaddock
::(Annotated ast,Traversable ast)
=> (ast SrcSpanInfo,[Comment])
-> ast (SrcSpanInfo,[Comment])
associateHaddock (ast,[]) = fmap (\src->(src,[])) ast
associateHaddock (ast,comments) =
let
(ca,assocs1) = mapAccumL associate1 (newAccumulator comments) ast
in snd $ mapAccumL merge (lastPost ca) assocs1
-- | Merge existing association with post comment associations
merge
:: [(SrcSpanInfo,[Comment])]
-> (SrcSpanInfo,[Comment])
-> ([(SrcSpanInfo,[Comment])], (SrcSpanInfo,[Comment]))
merge [] ret = ([],ret)
merge (x:xs) (src,cmts) =
if fst x == src
then (xs,(src,cmts ++ snd x))
else (x:xs,(src,cmts))
-- | Ensure that if file ends with comment we process it
lastPost :: CommentAccumulator -> [(SrcSpanInfo, [Comment])]
lastPost (CommentAccumulator (Post cmt : rest) past assocs) =
let (toMerge, _) = span isNone rest
psrc = matchPreviousSrc past
in (assocs ++ [(psrc, cmt : map hcComment toMerge)])
lastPost (CommentAccumulator _ _ assocs) = assocs
-- | Accumulate comments mappings, either directly with the source
-- or in another association list for later processing
associate1
:: CommentAccumulator
-> SrcSpanInfo
-> (CommentAccumulator,(SrcSpanInfo,[Comment]))
associate1 ca@(CommentAccumulator [] _ _) src = (ca,(src,[]))
associate1 (CommentAccumulator (hc@(Pre cmt):rest) _ assocs) src =
if isBefore hc src
then
let (toMerge,next) = getToMerge src rest
newAssoc = (src,cmt : map hcComment toMerge)
in (CommentAccumulator next [] assocs,newAssoc)
else (CommentAccumulator (hc:rest) [] assocs,(src,[]))
associate1 (CommentAccumulator (hc@(Post cmt):rest) past assocs) src =
if isBefore hc src
then
let (toMerge,next) = getToMerge src rest
newAssocs =
if null past
then assocs
else assocs++[(matchPreviousSrc past,cmt : map hcComment toMerge)]
in associate1 (CommentAccumulator next [] newAssocs) src
else (CommentAccumulator (hc:rest) (src:past) assocs,(src,[]))
associate1 (CommentAccumulator (_:rest) past assocs) src =
(CommentAccumulator rest (src:past) assocs,(src,[]))
-- | The comment accumulator
data CommentAccumulator = CommentAccumulator
[HaddockComment] -- The Haddock comments to process
[SrcSpanInfo] -- The past src infos to resolve post comments
[(SrcSpanInfo,[Comment])] -- The additional associations between src and comments
-- | Create a new accumulator
newAccumulator :: [Comment] -> CommentAccumulator
newAccumulator comments = CommentAccumulator (commentsToHaddock comments) [] []
-- | Get comments to merge
getToMerge
:: SrcSpanInfo -- ^ Stop before src
-> [HaddockComment] -- ^ All remaining comments
-> ([HaddockComment],[HaddockComment]) -- ^ Comments to merge, left overs
getToMerge src = span (\hc-> isNone hc && isBefore hc src)
-- | Get the biggest src that ends where the first one does
matchPreviousSrc :: [SrcSpanInfo] -> SrcSpanInfo
matchPreviousSrc [] =
error "Language.Haskell.Exts.Annotated.Comments.matchPreviousSrc: empty list"
matchPreviousSrc srcs =
let end = srcSpanEnd $ srcInfoSpan $ head srcs
in last $ filter ((end ==) . srcSpanEnd . srcInfoSpan) srcs
-- | Is a Haddock comment before a given location
isBefore :: HaddockComment -> SrcSpanInfo -> Bool
isBefore hc src=
let
(Comment _ csrc _) = hcComment hc
in csrc < srcInfoSpan src
-- | Represents a Haddock Comment
data HaddockComment =
-- | Comment before declaration
Pre
{
hcComment::Comment
}
-- | Comment after declaration
| Post {
hcComment::Comment
}
-- | Non Haddock comment
| None {
hcComment::Comment
}
-- | Is a comment not haddock?
isNone :: HaddockComment -> Bool
isNone (None _) = True
isNone _ = False
-- | Comments to Haddock Comments
commentsToHaddock :: [Comment] -> [HaddockComment]
commentsToHaddock = map commentToHaddock
-- | Comment to Haddock Comment
commentToHaddock :: Comment -> HaddockComment
commentToHaddock c@(Comment _ _ txt) =
case dropWhile isSpace txt of
('|':_) -> Pre c
('^':_) -> Post c
_ -> None c