2 % (c) The University of Glasgow, 1992-2006
6 -- | This module contains types that relate to the positions of things
7 -- in source files, and allow tagging of those things with locations
12 -- ** Constructing SrcLoc
13 mkSrcLoc, mkGeneralSrcLoc,
15 noSrcLoc, -- "I'm sorry, I haven't a clue"
16 generatedSrcLoc, -- Code generated within the compiler
17 interactiveSrcLoc, -- Code from an interactive session
21 -- ** Unsafely deconstructing SrcLoc
22 -- These are dubious exports, because they crash on some inputs
23 srcLocFile, -- return the file name part
24 srcLocLine, -- return the line part
25 srcLocCol, -- return the column part
27 -- ** Misc. operations on SrcLoc
30 -- ** Predicates on SrcLoc
36 -- ** Constructing SrcSpan
37 mkGeneralSrcSpan, mkSrcSpan,
39 wiredInSrcSpan, -- Something wired into the compiler
43 -- ** Deconstructing SrcSpan
44 srcSpanStart, srcSpanEnd,
45 srcSpanFileName_maybe,
47 -- ** Unsafely deconstructing SrcSpan
48 -- These are dubious exports, because they crash on some inputs
50 srcSpanStartLine, srcSpanEndLine,
51 srcSpanStartCol, srcSpanEndCol,
53 -- ** Predicates on SrcSpan
54 isGoodSrcSpan, isOneLineSpan,
59 -- ** Constructing Located
62 -- ** Deconstructing Located
65 -- ** Combining and comparing Located values
66 eqLocated, cmpLocated, combineLocs, addCLoc,
67 leftmost_smallest, leftmost_largest, rightmost,
76 %************************************************************************
78 \subsection[SrcLoc-SrcLocations]{Source-location information}
80 %************************************************************************
82 We keep information about the {\em definition} point for each entity;
83 this is the obvious stuff:
85 -- | Represents a single point within a file
87 = SrcLoc FastString -- A precise location (file name)
88 {-# UNPACK #-} !Int -- line number, begins at 1
89 {-# UNPACK #-} !Int -- column number, begins at 0
90 -- Don't ask me why lines start at 1 and columns start at
91 -- zero. That's just the way it is, so there. --SDM
93 | UnhelpfulLoc FastString -- Just a general indication
96 %************************************************************************
98 \subsection[SrcLoc-access-fns]{Access functions}
100 %************************************************************************
103 mkSrcLoc :: FastString -> Int -> Int -> SrcLoc
104 mkSrcLoc x line col = SrcLoc x line col
106 -- | Built-in "bad" 'SrcLoc' values for particular locations
107 noSrcLoc, generatedSrcLoc, interactiveSrcLoc :: SrcLoc
108 noSrcLoc = UnhelpfulLoc (fsLit "<no location info>")
109 generatedSrcLoc = UnhelpfulLoc (fsLit "<compiler-generated code>")
110 interactiveSrcLoc = UnhelpfulLoc (fsLit "<interactive session>")
112 -- | Creates a "bad" 'SrcLoc' that has no detailed information about its location
113 mkGeneralSrcLoc :: FastString -> SrcLoc
114 mkGeneralSrcLoc = UnhelpfulLoc
116 -- | "Good" 'SrcLoc's have precise information about their location
117 isGoodSrcLoc :: SrcLoc -> Bool
118 isGoodSrcLoc (SrcLoc _ _ _) = True
119 isGoodSrcLoc _other = False
121 -- | Gives the filename of the 'SrcLoc' if it is available, otherwise returns a dummy value
122 srcLocFile :: SrcLoc -> FastString
123 srcLocFile (SrcLoc fname _ _) = fname
124 srcLocFile _other = (fsLit "<unknown file")
126 -- | Raises an error when used on a "bad" 'SrcLoc'
127 srcLocLine :: SrcLoc -> Int
128 srcLocLine (SrcLoc _ l _) = l
129 srcLocLine _other = panic "srcLocLine: unknown line"
131 -- | Raises an error when used on a "bad" 'SrcLoc'
132 srcLocCol :: SrcLoc -> Int
133 srcLocCol (SrcLoc _ _ c) = c
134 srcLocCol _other = panic "srcLocCol: unknown col"
136 -- | Move the 'SrcLoc' down by one line if the character is a newline
137 -- and across by one character in any other case
138 advanceSrcLoc :: SrcLoc -> Char -> SrcLoc
139 advanceSrcLoc (SrcLoc f l _) '\n' = SrcLoc f (l + 1) 0
140 advanceSrcLoc (SrcLoc f l c) _ = SrcLoc f l (c + 1)
141 advanceSrcLoc loc _ = loc -- Better than nothing
144 %************************************************************************
146 \subsection[SrcLoc-instances]{Instance declarations for various names}
148 %************************************************************************
151 -- SrcLoc is an instance of Ord so that we can sort error messages easily
152 instance Eq SrcLoc where
153 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
157 instance Ord SrcLoc where
160 cmpSrcLoc :: SrcLoc -> SrcLoc -> Ordering
161 cmpSrcLoc (UnhelpfulLoc s1) (UnhelpfulLoc s2) = s1 `compare` s2
162 cmpSrcLoc (UnhelpfulLoc _) _other = LT
164 cmpSrcLoc (SrcLoc s1 l1 c1) (SrcLoc s2 l2 c2)
165 = (s1 `compare` s2) `thenCmp` (l1 `compare` l2) `thenCmp` (c1 `compare` c2)
166 cmpSrcLoc (SrcLoc _ _ _) _other = GT
168 instance Outputable SrcLoc where
169 ppr (SrcLoc src_path src_line src_col)
170 = getPprStyle $ \ sty ->
171 if userStyle sty || debugStyle sty then
172 hcat [ pprFastFilePath src_path, char ':',
174 char ':', int src_col
177 hcat [text "{-# LINE ", int src_line, space,
178 char '\"', pprFastFilePath src_path, text " #-}"]
180 ppr (UnhelpfulLoc s) = ftext s
183 %************************************************************************
185 \subsection[SrcSpan]{Source Spans}
187 %************************************************************************
191 A SrcSpan delimits a portion of a text file. It could be represented
192 by a pair of (line,column) coordinates, but in fact we optimise
193 slightly by using more compact representations for single-line and
194 zero-length spans, both of which are quite common.
196 The end position is defined to be the column /after/ the end of the
197 span. That is, a span of (1,1)-(1,2) is one character long, and a
198 span of (1,1)-(1,1) is zero characters long.
201 = SrcSpanOneLine -- a common case: a single line
202 { srcSpanFile :: !FastString,
203 srcSpanLine :: {-# UNPACK #-} !Int,
204 srcSpanSCol :: {-# UNPACK #-} !Int,
205 srcSpanECol :: {-# UNPACK #-} !Int
209 { srcSpanFile :: !FastString,
210 srcSpanSLine :: {-# UNPACK #-} !Int,
211 srcSpanSCol :: {-# UNPACK #-} !Int,
212 srcSpanELine :: {-# UNPACK #-} !Int,
213 srcSpanECol :: {-# UNPACK #-} !Int
217 { srcSpanFile :: !FastString,
218 srcSpanLine :: {-# UNPACK #-} !Int,
219 srcSpanCol :: {-# UNPACK #-} !Int
222 | UnhelpfulSpan !FastString -- Just a general indication
223 -- also used to indicate an empty span
226 deriving (Eq, Show) -- Show is used by Lexer.x, becuase we
227 -- derive Show for Token
232 -- | Built-in "bad" 'SrcSpan's for common sources of location uncertainty
233 noSrcSpan, wiredInSrcSpan :: SrcSpan
234 noSrcSpan = UnhelpfulSpan (fsLit "<no location info>")
235 wiredInSrcSpan = UnhelpfulSpan (fsLit "<wired into compiler>")
237 -- | Create a "bad" 'SrcSpan' that has not location information
238 mkGeneralSrcSpan :: FastString -> SrcSpan
239 mkGeneralSrcSpan = UnhelpfulSpan
241 -- | Create a 'SrcSpan' corresponding to a single point
242 srcLocSpan :: SrcLoc -> SrcSpan
243 srcLocSpan (UnhelpfulLoc str) = UnhelpfulSpan str
244 srcLocSpan (SrcLoc file line col) = SrcSpanPoint file line col
246 -- | Create a 'SrcSpan' between two points in a file
247 mkSrcSpan :: SrcLoc -> SrcLoc -> SrcSpan
248 mkSrcSpan (UnhelpfulLoc str) _ = UnhelpfulSpan str
249 mkSrcSpan _ (UnhelpfulLoc str) = UnhelpfulSpan str
251 | line1 == line2 = if col1 == col2
252 then SrcSpanPoint file line1 col1
253 else SrcSpanOneLine file line1 col1 col2
254 | otherwise = SrcSpanMultiLine file line1 col1 line2 col2
256 line1 = srcLocLine loc1
257 line2 = srcLocLine loc2
258 col1 = srcLocCol loc1
259 col2 = srcLocCol loc2
260 file = srcLocFile loc1
262 -- | Combines two 'SrcSpan' into one that spans at least all the characters
263 -- within both spans. Assumes the "file" part is the same in both inputs
264 combineSrcSpans :: SrcSpan -> SrcSpan -> SrcSpan
265 combineSrcSpans (UnhelpfulSpan _) r = r -- this seems more useful
266 combineSrcSpans l (UnhelpfulSpan _) = l
267 combineSrcSpans start end
268 = case line1 `compare` line2 of
269 EQ -> case col1 `compare` col2 of
270 EQ -> SrcSpanPoint file line1 col1
271 LT -> SrcSpanOneLine file line1 col1 col2
272 GT -> SrcSpanOneLine file line1 col2 col1
273 LT -> SrcSpanMultiLine file line1 col1 line2 col2
274 GT -> SrcSpanMultiLine file line2 col2 line1 col1
276 line1 = srcSpanStartLine start
277 col1 = srcSpanStartCol start
278 line2 = srcSpanEndLine end
279 col2 = srcSpanEndCol end
280 file = srcSpanFile start
283 %************************************************************************
285 \subsection[SrcSpan-predicates]{Predicates}
287 %************************************************************************
290 -- | Test if a 'SrcSpan' is "good", i.e. has precise location information
291 isGoodSrcSpan :: SrcSpan -> Bool
292 isGoodSrcSpan SrcSpanOneLine{} = True
293 isGoodSrcSpan SrcSpanMultiLine{} = True
294 isGoodSrcSpan SrcSpanPoint{} = True
295 isGoodSrcSpan _ = False
297 isOneLineSpan :: SrcSpan -> Bool
298 -- ^ True if the span is known to straddle more than one line.
299 -- For "bad" 'SrcSpan', it returns False
301 | isGoodSrcSpan s = srcSpanStartLine s == srcSpanEndLine s
306 %************************************************************************
308 \subsection[SrcSpan-unsafe-access-fns]{Unsafe access functions}
310 %************************************************************************
314 -- | Raises an error when used on a "bad" 'SrcSpan'
315 srcSpanStartLine :: SrcSpan -> Int
316 -- | Raises an error when used on a "bad" 'SrcSpan'
317 srcSpanEndLine :: SrcSpan -> Int
318 -- | Raises an error when used on a "bad" 'SrcSpan'
319 srcSpanStartCol :: SrcSpan -> Int
320 -- | Raises an error when used on a "bad" 'SrcSpan'
321 srcSpanEndCol :: SrcSpan -> Int
323 srcSpanStartLine SrcSpanOneLine{ srcSpanLine=l } = l
324 srcSpanStartLine SrcSpanMultiLine{ srcSpanSLine=l } = l
325 srcSpanStartLine SrcSpanPoint{ srcSpanLine=l } = l
326 srcSpanStartLine _ = panic "SrcLoc.srcSpanStartLine"
328 srcSpanEndLine SrcSpanOneLine{ srcSpanLine=l } = l
329 srcSpanEndLine SrcSpanMultiLine{ srcSpanELine=l } = l
330 srcSpanEndLine SrcSpanPoint{ srcSpanLine=l } = l
331 srcSpanEndLine _ = panic "SrcLoc.srcSpanEndLine"
333 srcSpanStartCol SrcSpanOneLine{ srcSpanSCol=l } = l
334 srcSpanStartCol SrcSpanMultiLine{ srcSpanSCol=l } = l
335 srcSpanStartCol SrcSpanPoint{ srcSpanCol=l } = l
336 srcSpanStartCol _ = panic "SrcLoc.srcSpanStartCol"
338 srcSpanEndCol SrcSpanOneLine{ srcSpanECol=c } = c
339 srcSpanEndCol SrcSpanMultiLine{ srcSpanECol=c } = c
340 srcSpanEndCol SrcSpanPoint{ srcSpanCol=c } = c
341 srcSpanEndCol _ = panic "SrcLoc.srcSpanEndCol"
345 %************************************************************************
347 \subsection[SrcSpan-access-fns]{Access functions}
349 %************************************************************************
353 -- | Returns the location at the start of the 'SrcSpan' or a "bad" 'SrcSpan' if that is unavailable
354 srcSpanStart :: SrcSpan -> SrcLoc
355 -- | Returns the location at the end of the 'SrcSpan' or a "bad" 'SrcSpan' if that is unavailable
356 srcSpanEnd :: SrcSpan -> SrcLoc
358 srcSpanStart (UnhelpfulSpan str) = UnhelpfulLoc str
359 srcSpanStart s = mkSrcLoc (srcSpanFile s)
363 srcSpanEnd (UnhelpfulSpan str) = UnhelpfulLoc str
365 mkSrcLoc (srcSpanFile s)
369 -- | Obtains the filename for a 'SrcSpan' if it is "good"
370 srcSpanFileName_maybe :: SrcSpan -> Maybe FastString
371 srcSpanFileName_maybe (SrcSpanOneLine { srcSpanFile = nm }) = Just nm
372 srcSpanFileName_maybe (SrcSpanMultiLine { srcSpanFile = nm }) = Just nm
373 srcSpanFileName_maybe (SrcSpanPoint { srcSpanFile = nm}) = Just nm
374 srcSpanFileName_maybe _ = Nothing
378 %************************************************************************
380 \subsection[SrcSpan-instances]{Instances}
382 %************************************************************************
386 -- We want to order SrcSpans first by the start point, then by the end point.
387 instance Ord SrcSpan where
389 (srcSpanStart a `compare` srcSpanStart b) `thenCmp`
390 (srcSpanEnd a `compare` srcSpanEnd b)
393 instance Outputable SrcSpan where
395 = getPprStyle $ \ sty ->
396 if userStyle sty || debugStyle sty then
399 hcat [text "{-# LINE ", int (srcSpanStartLine span), space,
400 char '\"', pprFastFilePath $ srcSpanFile span, text " #-}"]
402 pprUserSpan :: SrcSpan -> SDoc
403 pprUserSpan (SrcSpanOneLine src_path line start_col end_col)
404 = hcat [ pprFastFilePath src_path, char ':',
406 char ':', int start_col
408 <> if end_col - start_col <= 1
410 -- for single-character or point spans, we just output the starting
412 else char '-' <> int (end_col-1)
414 pprUserSpan (SrcSpanMultiLine src_path sline scol eline ecol)
415 = hcat [ pprFastFilePath src_path, char ':',
416 parens (int sline <> char ',' <> int scol),
418 parens (int eline <> char ',' <>
419 if ecol == 0 then int ecol else int (ecol-1))
422 pprUserSpan (SrcSpanPoint src_path line col)
423 = hcat [ pprFastFilePath src_path, char ':',
428 pprUserSpan (UnhelpfulSpan s) = ftext s
430 pprDefnLoc :: SrcSpan -> SDoc
431 -- ^ Pretty prints information about the 'SrcSpan' in the style "defined at ..."
433 | isGoodSrcSpan loc = ptext (sLit "Defined at") <+> ppr loc
434 | otherwise = ppr loc
437 %************************************************************************
439 \subsection[Located]{Attaching SrcSpans to things}
441 %************************************************************************
444 -- | We attach SrcSpans to lots of things, so let's have a datatype for it.
445 data Located e = L SrcSpan e
447 unLoc :: Located e -> e
450 getLoc :: Located e -> SrcSpan
453 noLoc :: e -> Located e
454 noLoc e = L noSrcSpan e
456 combineLocs :: Located a -> Located b -> SrcSpan
457 combineLocs a b = combineSrcSpans (getLoc a) (getLoc b)
459 -- | Combine locations from two 'Located' things and add them to a third thing
460 addCLoc :: Located a -> Located b -> c -> Located c
461 addCLoc a b c = L (combineSrcSpans (getLoc a) (getLoc b)) c
463 -- not clear whether to add a general Eq instance, but this is useful sometimes:
465 -- | Tests whether the two located things are equal
466 eqLocated :: Eq a => Located a -> Located a -> Bool
467 eqLocated a b = unLoc a == unLoc b
469 -- not clear whether to add a general Ord instance, but this is useful sometimes:
471 -- | Tests the ordering of the two located things
472 cmpLocated :: Ord a => Located a -> Located a -> Ordering
473 cmpLocated a b = unLoc a `compare` unLoc b
475 instance Functor Located where
476 fmap f (L l e) = L l (f e)
478 instance Outputable e => Outputable (Located e) where
480 -- do we want to dump the span in debugSty mode?
483 %************************************************************************
485 \subsection{Ordering SrcSpans for InteractiveUI}
487 %************************************************************************
490 -- | Alternative strategies for ordering 'SrcSpan's
491 leftmost_smallest, leftmost_largest, rightmost :: SrcSpan -> SrcSpan -> Ordering
492 rightmost = flip compare
493 leftmost_smallest = compare
494 leftmost_largest a b = (srcSpanStart a `compare` srcSpanStart b)
496 (srcSpanEnd b `compare` srcSpanEnd a)
499 -- | Determines whether a span encloses a given line and column index
500 spans :: SrcSpan -> (Int, Int) -> Bool
501 spans span (l,c) = srcSpanStart span <= loc && loc <= srcSpanEnd span
502 where loc = mkSrcLoc (srcSpanFile span) l c
504 -- | Determines whether a span is enclosed by another one
505 isSubspanOf :: SrcSpan -- ^ The span that may be enclosed by the other
506 -> SrcSpan -- ^ The span it may be enclosed by
508 isSubspanOf src parent
509 | srcSpanFileName_maybe parent /= srcSpanFileName_maybe src = False
510 | otherwise = srcSpanStart parent <= srcSpanStart src &&
511 srcSpanEnd parent >= srcSpanEnd src