1module Transform.Check (mistakes) where
3import Transform.SortDefinitions (boundVars)
4import SourceSyntax.Everything
5import qualified SourceSyntax.Type as T
6import Data.List as List
7import qualified Data.Map as Map
8import qualified Data.Set as Set
10import Data.Generics.Uniplate.Data
11import Text.PrettyPrint as P
14mistakes :: (Data t, Data v) => [Declaration t v] -> [Doc]
16 map prettify $ concatMap findErrors (getLets decls)
18 prettify = P.sep . map P.text . words
19 findErrors defs = duplicates defs ++ badOrder defs
22getLets :: (Data t, Data v) => [Declaration t v] -> [[Def t v]]
23getLets decls = defs : concatMap getSubLets defs
25 defs = concatMap (\d -> case d of Definition d -> [d] ; _ -> []) decls
29 Def pattern expr -> [ defs | Let defs _ <- universeBi expr ]
30 TypeAnnotation _ _ -> []
33duplicates :: [Def t v] -> [String]
35 map defMsg (dup definition) ++ map annMsg (dup annotation)
37 dup f = map head . filter ((>1) . length) . List.group . List.sort $ concatMap f defs
39 msg = "Syntax Error: There can only be one "
40 defMsg x = msg ++ " definition of '" ++ x ++ "'."
41 annMsg x = msg ++ " type annotation for '" ++ x ++ "'."
44badOrder :: [Def t v] -> [String]
46 map msg $ missings (List.sort $ expectedPairs as ds) (List.sort $ actualPairs defs)
48 msg x = "Syntax Error: The type annotation for '" ++ x ++
49 "' must be directly above its definition."
50 as = List.sort $ concatMap annotation defs
51 ds = List.sort $ concatMap definition defs
53 expectedPairs :: [String] -> [String] -> [String]
56 (x:xs, y:ys) -> case compare x y of
57 LT -> expectedPairs xs (y:ys)
58 EQ -> x : expectedPairs xs ys
59 GT -> expectedPairs (x:xs) ys
64 TypeAnnotation name _ : Def (PVar name') _ : rest ->
65 (if name == name' then [name] else []) ++ actualPairs rest
66 t:s:rest -> actualPairs (s:rest)
69 missings :: [String] -> [String] -> [String]
70 missings expected actual =
71 case (expected, actual) of
72 (e:es, a:as) -> case compare e a of
73 LT -> e : missings es (a:as)
75 GT -> a : missings (e:es) as
77 ( _ , [] ) -> expected
80annotation :: Def t v -> [String]
83 TypeAnnotation name _ -> [name]
86definition :: Def t v -> [String]
89 Def pattern _ -> Set.toList (boundVars pattern)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list