on master help sign in
Add source code to locations for better error messages
177a037Evan Czaplicki
compiler/SourceSyntax/Location.hs+33−17
@@ -1,40 +1,56 @@
1{-# LANGUAGE DeriveDataTypeable #-}
1{-# LANGUAGE DeriveDataTypeable #-}
2module SourceSyntax.Location where
2module SourceSyntax.Location where
3
3
4−import qualified Text.Parsec.Pos as Parsec
5import Data.Data
4import Data.Data
5+import Text.PrettyPrint
6+import SourceSyntax.PrettyPrint
7+import qualified Text.Parsec.Pos as Parsec
6
8
7data SrcPos = Pos { line :: Int, column :: Int }
9data SrcPos = Pos { line :: Int, column :: Int }
8 deriving (Eq, Ord, Data, Typeable)
10 deriving (Eq, Ord, Data, Typeable)
9
11
10−data SrcSpan = Span SrcPos SrcPos | NoSpan
12+data SrcSpan = Span SrcPos SrcPos String | NoSpan String
11 deriving (Eq, Ord, Data, Typeable)
13 deriving (Eq, Ord, Data, Typeable)
12
14
13data Located e = L SrcSpan e
15data Located e = L SrcSpan e
14 deriving (Eq, Ord, Data, Typeable)
16 deriving (Eq, Ord, Data, Typeable)
15
17
18+none e = L (NoSpan (render $ pretty e)) e
19+noneNoDocs = L (NoSpan "")
20+
21+at start end e = L (Span (Pos (Parsec.sourceLine start) (Parsec.sourceColumn start))
22+ (Pos (Parsec.sourceLine end ) (Parsec.sourceColumn end ))
23+ (render $ pretty e)) e
24+
25+merge (L s1 _) (L s2 _) e = L (span (render $ pretty e)) e
26+ where span = case (s1,s2) of
27+ (Span start _ _, Span _ end _) -> Span start end
28+ (Span start end _, _) -> Span start end
29+ (_, Span start end _) -> Span start end
30+ (_, _) -> NoSpan
31+
32+mergeOldDocs (L s1 _) (L s2 _) e = L span e
33+ where span = case (s1,s2) of
34+ (Span start _ d1, Span _ end d2) -> Span start end (d1 ++ "\n\n" ++ d2)
35+ (Span _ _ _, _) -> s1
36+ (_, Span _ _ _) -> s2
37+ (_, _) -> NoSpan ""
38+
39+sameAs (L s _) = L s
40+
16
41
17instance Show SrcPos where
42instance Show SrcPos where
18− show (Pos r c) = "Line " ++ show r ++ ", Column " ++ show c
43+ show (Pos r c) = show r ++ "," ++ show c
19
44
20instance Show SrcSpan where
45instance Show SrcSpan where
21 show span =
46 show span =
22 case span of
47 case span of
23− Span start end -> show start
48+ Span start end _ -> show start ++ " to " ++ show end
24− NoSpan -> ""
49+ NoSpan str -> str
25
50
26instance Show e => Show (Located e) where
51instance Show e => Show (Located e) where
27− show (L _ e) = show e
52+ show (L s e) = "L (" ++ show s ++ ") (" ++ show e ++ ")"
28
53
29−none = L NoSpan
54+instance Pretty a => Pretty (Located a) where
30−
55+ pretty (L _ e) = pretty e
31−at start end = L (Span (Pos (Parsec.sourceLine start) (Parsec.sourceColumn start))
32− (Pos (Parsec.sourceLine end ) (Parsec.sourceColumn end )))
33−
34−merge (L s1 _) (L s2 _) = L span
35− where span = case (s1,s2) of
36− (Span start _, Span _ end) -> Span start end
37− (_, NoSpan) -> s1
38− (NoSpan, _) -> s2
39
56
40−sameAs (L s _) = L s
compiler/SourceSyntax/PrettyPrint.hs−4
@@ -2,15 +2,11 @@
2module SourceSyntax.PrettyPrint where
2module SourceSyntax.PrettyPrint where
3
3
4import Text.PrettyPrint
4import Text.PrettyPrint
5−import SourceSyntax.Location
6import qualified SourceSyntax.Helpers as Help
5import qualified SourceSyntax.Helpers as Help
7
6
8class Pretty a where
7class Pretty a where
9 pretty :: a -> Doc
8 pretty :: a -> Doc
10
9
11−instance Pretty a => Pretty (Located a) where
12− pretty (L _ e) = pretty e
13−
14instance Pretty () where
10instance Pretty () where
15 pretty () = empty
11 pretty () = empty
16
12