2 % (c) The University of Glasgow, 1992-2006
9 mkSrcLoc, isGoodSrcLoc, mkGeneralSrcLoc,
10 noSrcLoc, -- "I'm sorry, I haven't a clue"
13 generatedSrcLoc, -- Code generated within the compiler
14 interactiveSrcLoc, -- Code from an interactive session
16 srcLocFile, -- return the file name part
17 srcLocLine, -- return the line part
18 srcLocCol, -- return the column part
23 wiredInSrcSpan, -- Something wired into the compiler
25 isGoodSrcSpan, isOneLineSpan,
26 mkSrcSpan, srcLocSpan,
28 srcSpanStart, srcSpanEnd,
31 -- These are dubious exports, because they crash on some inputs,
32 -- used only in Lexer.x where we are sure what the Span looks like
34 srcSpanStartLine, srcSpanEndLine,
35 srcSpanStartCol, srcSpanEndCol,
37 Located(..), getLoc, unLoc, noLoc, eqLocated, cmpLocated, combineLocs, addCLoc,
38 leftmost_smallest, leftmost_largest, rightmost, spans, isSubspanOf
44 import System.FilePath
47 %************************************************************************
49 \subsection[SrcLoc-SrcLocations]{Source-location information}
51 %************************************************************************
53 We keep information about the {\em definition} point for each entity;
54 this is the obvious stuff:
57 = SrcLoc FastString -- A precise location (file name)
58 {-# UNPACK #-} !Int -- line number, begins at 1
59 {-# UNPACK #-} !Int -- column number, begins at 0
60 -- Don't ask me why lines start at 1 and columns start at
61 -- zero. That's just the way it is, so there. --SDM
63 | UnhelpfulLoc FastString -- Just a general indication
66 %************************************************************************
68 \subsection[SrcLoc-access-fns]{Access functions for names}
70 %************************************************************************
74 mkSrcLoc :: FastString -> Int -> Int -> SrcLoc
75 mkSrcLoc x line col = SrcLoc x line col
77 noSrcLoc, generatedSrcLoc, interactiveSrcLoc :: SrcLoc
78 noSrcLoc = UnhelpfulLoc (fsLit "<no location info>")
79 generatedSrcLoc = UnhelpfulLoc (fsLit "<compiler-generated code>")
80 interactiveSrcLoc = UnhelpfulLoc (fsLit "<interactive session>")
82 mkGeneralSrcLoc :: FastString -> SrcLoc
83 mkGeneralSrcLoc = UnhelpfulLoc
85 isGoodSrcLoc :: SrcLoc -> Bool
86 isGoodSrcLoc (SrcLoc _ _ _) = True
87 isGoodSrcLoc _other = False
89 srcLocFile :: SrcLoc -> FastString
90 srcLocFile (SrcLoc fname _ _) = fname
91 srcLocFile _other = (fsLit "<unknown file")
93 srcLocLine :: SrcLoc -> Int
94 srcLocLine (SrcLoc _ l _) = l
95 srcLocLine _other = panic "srcLocLine: unknown line"
97 srcLocCol :: SrcLoc -> Int
98 srcLocCol (SrcLoc _ _ c) = c
99 srcLocCol _other = panic "srcLocCol: unknown col"
101 advanceSrcLoc :: SrcLoc -> Char -> SrcLoc
102 advanceSrcLoc (SrcLoc f l _) '\n' = SrcLoc f (l + 1) 0
103 advanceSrcLoc (SrcLoc f l c) _ = SrcLoc f l (c + 1)
104 advanceSrcLoc loc _ = loc -- Better than nothing
107 %************************************************************************
109 \subsection[SrcLoc-instances]{Instance declarations for various names}
111 %************************************************************************
114 -- SrcLoc is an instance of Ord so that we can sort error messages easily
115 instance Eq SrcLoc where
116 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
120 instance Ord SrcLoc where
123 cmpSrcLoc :: SrcLoc -> SrcLoc -> Ordering
124 cmpSrcLoc (UnhelpfulLoc s1) (UnhelpfulLoc s2) = s1 `compare` s2
125 cmpSrcLoc (UnhelpfulLoc _) _other = LT
127 cmpSrcLoc (SrcLoc s1 l1 c1) (SrcLoc s2 l2 c2)
128 = (s1 `compare` s2) `thenCmp` (l1 `compare` l2) `thenCmp` (c1 `compare` c2)
129 cmpSrcLoc (SrcLoc _ _ _) _other = GT
131 pprFastFilePath :: FastString -> SDoc
132 pprFastFilePath path = text $ normalise $ unpackFS path
134 instance Outputable SrcLoc where
135 ppr (SrcLoc src_path src_line src_col)
136 = getPprStyle $ \ sty ->
137 if userStyle sty || debugStyle sty then
138 hcat [ pprFastFilePath src_path, char ':',
140 char ':', int src_col
143 hcat [text "{-# LINE ", int src_line, space,
144 char '\"', pprFastFilePath src_path, text " #-}"]
146 ppr (UnhelpfulLoc s) = ftext s
149 %************************************************************************
151 \subsection[SrcSpan]{Source Spans}
153 %************************************************************************
157 A SrcSpan delimits a portion of a text file. It could be represented
158 by a pair of (line,column) coordinates, but in fact we optimise
159 slightly by using more compact representations for single-line and
160 zero-length spans, both of which are quite common.
162 The end position is defined to be the column *after* the end of the
163 span. That is, a span of (1,1)-(1,2) is one character long, and a
164 span of (1,1)-(1,1) is zero characters long.
167 = SrcSpanOneLine -- a common case: a single line
168 { srcSpanFile :: !FastString,
169 srcSpanLine :: {-# UNPACK #-} !Int,
170 srcSpanSCol :: {-# UNPACK #-} !Int,
171 srcSpanECol :: {-# UNPACK #-} !Int
175 { srcSpanFile :: !FastString,
176 srcSpanSLine :: {-# UNPACK #-} !Int,
177 srcSpanSCol :: {-# UNPACK #-} !Int,
178 srcSpanELine :: {-# UNPACK #-} !Int,
179 srcSpanECol :: {-# UNPACK #-} !Int
183 { srcSpanFile :: !FastString,
184 srcSpanLine :: {-# UNPACK #-} !Int,
185 srcSpanCol :: {-# UNPACK #-} !Int
188 | UnhelpfulSpan !FastString -- Just a general indication
189 -- also used to indicate an empty span
192 deriving (Eq, Show) -- Show is used by Lexer.x, becuase we
193 -- derive Show for Token
198 -- We want to order SrcSpans first by the start point, then by the end point.
199 instance Ord SrcSpan where
201 (srcSpanStart a `compare` srcSpanStart b) `thenCmp`
202 (srcSpanEnd a `compare` srcSpanEnd b)
204 noSrcSpan, wiredInSrcSpan :: SrcSpan
205 noSrcSpan = UnhelpfulSpan (fsLit "<no location info>")
206 wiredInSrcSpan = UnhelpfulSpan (fsLit "<wired into compiler>")
208 mkGeneralSrcSpan :: FastString -> SrcSpan
209 mkGeneralSrcSpan = UnhelpfulSpan
211 isGoodSrcSpan :: SrcSpan -> Bool
212 isGoodSrcSpan SrcSpanOneLine{} = True
213 isGoodSrcSpan SrcSpanMultiLine{} = True
214 isGoodSrcSpan SrcSpanPoint{} = True
215 isGoodSrcSpan _ = False
217 optSrcSpanFileName :: SrcSpan -> Maybe FastString
218 optSrcSpanFileName (SrcSpanOneLine { srcSpanFile = nm }) = Just nm
219 optSrcSpanFileName (SrcSpanMultiLine { srcSpanFile = nm }) = Just nm
220 optSrcSpanFileName (SrcSpanPoint { srcSpanFile = nm}) = Just nm
221 optSrcSpanFileName _ = Nothing
223 isOneLineSpan :: SrcSpan -> Bool
224 -- True if the span is known to straddle more than one line
225 -- By default, it returns False
227 | isGoodSrcSpan s = srcSpanStartLine s == srcSpanEndLine s
230 --------------------------------------------------------
231 -- Don't export these four;
232 -- they panic on Unhelpful.
233 -- They are for internal use only
234 -- Urk! Some are needed for Lexer.x; see comment in export list
236 srcSpanStartLine, srcSpanEndLine, srcSpanStartCol, srcSpanEndCol
239 srcSpanStartLine SrcSpanOneLine{ srcSpanLine=l } = l
240 srcSpanStartLine SrcSpanMultiLine{ srcSpanSLine=l } = l
241 srcSpanStartLine SrcSpanPoint{ srcSpanLine=l } = l
242 srcSpanStartLine _ = panic "SrcLoc.srcSpanStartLine"
244 srcSpanEndLine SrcSpanOneLine{ srcSpanLine=l } = l
245 srcSpanEndLine SrcSpanMultiLine{ srcSpanELine=l } = l
246 srcSpanEndLine SrcSpanPoint{ srcSpanLine=l } = l
247 srcSpanEndLine _ = panic "SrcLoc.srcSpanEndLine"
249 srcSpanStartCol SrcSpanOneLine{ srcSpanSCol=l } = l
250 srcSpanStartCol SrcSpanMultiLine{ srcSpanSCol=l } = l
251 srcSpanStartCol SrcSpanPoint{ srcSpanCol=l } = l
252 srcSpanStartCol _ = panic "SrcLoc.srcSpanStartCol"
254 srcSpanEndCol SrcSpanOneLine{ srcSpanECol=c } = c
255 srcSpanEndCol SrcSpanMultiLine{ srcSpanECol=c } = c
256 srcSpanEndCol SrcSpanPoint{ srcSpanCol=c } = c
257 srcSpanEndCol _ = panic "SrcLoc.srcSpanEndCol"
258 --------------------------------------------------------
260 srcSpanStart, srcSpanEnd :: SrcSpan -> SrcLoc
262 srcSpanStart (UnhelpfulSpan str) = UnhelpfulLoc str
263 srcSpanStart s = mkSrcLoc (srcSpanFile s)
267 srcSpanEnd (UnhelpfulSpan str) = UnhelpfulLoc str
269 mkSrcLoc (srcSpanFile s)
273 srcLocSpan :: SrcLoc -> SrcSpan
274 srcLocSpan (UnhelpfulLoc str) = UnhelpfulSpan str
275 srcLocSpan (SrcLoc file line col) = SrcSpanPoint file line col
277 mkSrcSpan :: SrcLoc -> SrcLoc -> SrcSpan
278 mkSrcSpan (UnhelpfulLoc str) _ = UnhelpfulSpan str
279 mkSrcSpan _ (UnhelpfulLoc str) = UnhelpfulSpan str
281 | line1 == line2 = if col1 == col2
282 then SrcSpanPoint file line1 col1
283 else SrcSpanOneLine file line1 col1 col2
284 | otherwise = SrcSpanMultiLine file line1 col1 line2 col2
286 line1 = srcLocLine loc1
287 line2 = srcLocLine loc2
288 col1 = srcLocCol loc1
289 col2 = srcLocCol loc2
290 file = srcLocFile loc1
292 combineSrcSpans :: SrcSpan -> SrcSpan -> SrcSpan
293 -- Assumes the 'file' part is the same in both
294 combineSrcSpans (UnhelpfulSpan _) r = r -- this seems more useful
295 combineSrcSpans l (UnhelpfulSpan _) = l
296 combineSrcSpans start end
297 = case line1 `compare` line2 of
298 EQ -> case col1 `compare` col2 of
299 EQ -> SrcSpanPoint file line1 col1
300 LT -> SrcSpanOneLine file line1 col1 col2
301 GT -> SrcSpanOneLine file line1 col2 col1
302 LT -> SrcSpanMultiLine file line1 col1 line2 col2
303 GT -> SrcSpanMultiLine file line2 col2 line1 col1
305 line1 = srcSpanStartLine start
306 col1 = srcSpanStartCol start
307 line2 = srcSpanEndLine end
308 col2 = srcSpanEndCol end
309 file = srcSpanFile start
311 pprDefnLoc :: SrcSpan -> SDoc
314 | isGoodSrcSpan loc = ptext (sLit "Defined at") <+> ppr loc
315 | otherwise = ppr loc
317 instance Outputable SrcSpan where
319 = getPprStyle $ \ sty ->
320 if userStyle sty || debugStyle sty then
323 hcat [text "{-# LINE ", int (srcSpanStartLine span), space,
324 char '\"', pprFastFilePath $ srcSpanFile span, text " #-}"]
327 pprUserSpan :: SrcSpan -> SDoc
328 pprUserSpan (SrcSpanOneLine src_path line start_col end_col)
329 = hcat [ pprFastFilePath src_path, char ':',
331 char ':', int start_col
333 <> if end_col - start_col <= 1
335 -- for single-character or point spans, we just output the starting
337 else char '-' <> int (end_col-1)
339 pprUserSpan (SrcSpanMultiLine src_path sline scol eline ecol)
340 = hcat [ pprFastFilePath src_path, char ':',
341 parens (int sline <> char ',' <> int scol),
343 parens (int eline <> char ',' <>
344 if ecol == 0 then int ecol else int (ecol-1))
347 pprUserSpan (SrcSpanPoint src_path line col)
348 = hcat [ pprFastFilePath src_path, char ':',
353 pprUserSpan (UnhelpfulSpan s) = ftext s
356 %************************************************************************
358 \subsection[Located]{Attaching SrcSpans to things}
360 %************************************************************************
363 -- | We attach SrcSpans to lots of things, so let's have a datatype for it.
364 data Located e = L SrcSpan e
366 unLoc :: Located e -> e
369 getLoc :: Located e -> SrcSpan
372 noLoc :: e -> Located e
373 noLoc e = L noSrcSpan e
375 combineLocs :: Located a -> Located b -> SrcSpan
376 combineLocs a b = combineSrcSpans (getLoc a) (getLoc b)
378 addCLoc :: Located a -> Located b -> c -> Located c
379 addCLoc a b c = L (combineSrcSpans (getLoc a) (getLoc b)) c
381 -- not clear whether to add a general Eq instance, but this is useful sometimes:
382 eqLocated :: Eq a => Located a -> Located a -> Bool
383 eqLocated a b = unLoc a == unLoc b
385 -- not clear whether to add a general Eq instance, but this is useful sometimes:
386 cmpLocated :: Ord a => Located a -> Located a -> Ordering
387 cmpLocated a b = unLoc a `compare` unLoc b
389 instance Functor Located where
390 fmap f (L l e) = L l (f e)
392 instance Outputable e => Outputable (Located e) where
394 -- do we want to dump the span in debugSty mode?
398 %************************************************************************
400 \subsection{Manipulating SrcSpans}
402 %************************************************************************
405 leftmost_smallest, leftmost_largest, rightmost :: SrcSpan -> SrcSpan -> Ordering
406 rightmost = flip compare
407 leftmost_smallest = compare
408 leftmost_largest a b = (srcSpanStart a `compare` srcSpanStart b)
410 (srcSpanEnd b `compare` srcSpanEnd a)
413 spans :: SrcSpan -> (Int,Int) -> Bool
414 spans span (l,c) = srcSpanStart span <= loc && loc <= srcSpanEnd span
415 where loc = mkSrcLoc (srcSpanFile span) l c
417 isSubspanOf :: SrcSpan -> SrcSpan -> Bool
418 isSubspanOf src parent
419 | optSrcSpanFileName parent /= optSrcSpanFileName src = False
420 | otherwise = srcSpanStart parent <= srcSpanStart src &&
421 srcSpanEnd parent >= srcSpanEnd src