@@ -1,6 +1,7 @@
11module Docs.Check (check) where
22
33import Control.Applicative ((<$>),(<*>))
4+import qualified Data.List as List
45import qualified Data.Map as Map
56import qualified Data.Set as Set
67import qualified Data.Traversable as T
@@ -20,9 +21,12 @@ import qualified Reporting.Region as R
2021import qualified Reporting.Result as R
2122
2223
24+type Result w a = R.Result w Error.Error a
25+
26+
2327-- CHECK DOCUMENTATION
2428
25−check :: [Var.Value] -> A.Located (Maybe Docs.Centralized) -> R.Result w Error.Error Docs.Checked
29+check :: [Var.Value] -> A.Located (Maybe Docs.Centralized) -> Result w Docs.Checked
2630check exports (A.A region maybeDocs) =
2731 case maybeDocs of
2832 Nothing ->
@@ -32,7 +36,7 @@ check exports (A.A region maybeDocs) =
3236 checkHelp region exports docs
3337
3438
35−checkHelp :: R.Region -> [Var.Value] -> Docs.Centralized -> R.Result w Error.Error Docs.Checked
39+checkHelp :: R.Region -> [Var.Value] -> Docs.Centralized -> Result w Docs.Checked
3640checkHelp region exports (Docs.Docs comment aliases types values) =
3741 let
3842 docNames =
@@ -44,24 +48,18 @@ checkHelp region exports (Docs.Docs comment aliases types values) =
4448 |> Map.mapWithKey checkValue
4549 |> T.traverse id
4650
47− checkValue name value =
48− const
49− <$> hasType name value
50− <*> hasComment Docs.valueComment name value
51+ exportedUnions =
52+ map (\(tipe, Var.Listing tags _) -> (tipe, tags)) (Var.getUnions exports)
5153 in
5254 (\() -> Docs.Docs comment)
53− <$> checkComment region exports docNames
54− <*> checkCategory (hasComment Docs.aliasComment) aliases
55− <*> checkCategory (hasComment Docs.unionComment) types
55+ <$> checkModuleComment region exports docNames
56+ <*> checkCategory (checkComment Docs.aliasComment) aliases
57+ <*> checkCategory (checkUnion exportedUnions) types
5658 <*> checkCategory checkValue values
5759
5860
59−hasComment
60− :: (a -> Maybe String)
61− -> String
62− -> A.Located a
63− -> R.Result w Error.Error (A.Located a)
64−hasComment getComment name (A.A region value) =
61+checkComment :: (a -> Maybe String) -> String -> A.Located a -> Result w (A.Located a)
62+checkComment getComment name (A.A region value) =
6563 case getComment value of
6664 Nothing ->
6765 R.throw region (Error.NoComment name)
@@ -70,10 +68,35 @@ hasComment getComment name (A.A region value) =
7068 R.ok (A.A region value)
7169
7270
73−hasType
74− :: String
75− -> A.Located (Docs.Value (Maybe Type.Type))
76− -> R.Result w Error.Error (A.Located (Docs.Value Type.Type))
71+checkUnion :: [(String, [String])] -> String -> A.Located Docs.Union -> Result w (A.Located Docs.Union)
72+checkUnion exportedUnions name value =
73+ const
74+ <$> filterUnionTags exportedUnions name value
75+ <*> checkComment Docs.unionComment name value
76+
77+
78+filterUnionTags :: [(String, [String])] -> String -> A.Located Docs.Union -> Result w (A.Located Docs.Union)
79+filterUnionTags exportedUnions name (A.A region union@(Docs.Union _ _ ctors)) =
80+ case List.lookup name exportedUnions of
81+ Nothing ->
82+ R.throw region (Error.OnlyInExports name)
83+
84+ Just tags ->
85+ R.ok $ A.A region $ union
86+ { Docs.unionCases =
87+ filter (\(tag, _) -> elem tag tags) ctors
88+ }
89+
90+
91+
92+checkValue :: String -> A.Located (Docs.Value (Maybe Type.Type)) -> Result w (A.Located (Docs.Value Type.Type))
93+checkValue name value =
94+ const
95+ <$> hasType name value
96+ <*> checkComment Docs.valueComment name value
97+
98+
99+hasType :: String -> A.Located (Docs.Value (Maybe Type.Type)) -> Result w (A.Located (Docs.Value Type.Type))
77100hasType name (A.A region value) =
78101 case Docs.valueType value of
79102 Just tipe ->
@@ -85,8 +108,8 @@ hasType name (A.A region value) =
85108
86109-- CHECK MODULE COMMENT
87110
88−checkComment :: R.Region -> [Var.Value] -> Set.Set String -> R.Result w Error.Error ()
89−checkComment region exports docNames =
111+checkModuleComment :: R.Region -> [Var.Value] -> Set.Set String -> Result w ()
112+checkModuleComment region exports docNames =
90113 let
91114 exportNames =
92115 Set.fromList (map valueName exports)