on master help sign in
conditionally gather module documentation as snippets
ca649a3Evan Czaplicki
compiler/src/Parse/Module.hs+49−50
@@ -2,8 +2,6 @@
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Parse.Module
3module Parse.Module
4 ( fromByteString
4 ( fromByteString
5− , Header(..)
6− , Effects(..)
7 , chompImports
5 , chompImports
8 )
6 )
9 where
7 where
@@ -32,9 +30,9 @@ import qualified Reporting.Error.Syntax as E
32
30
33fromByteString :: Pkg.Name -> BS.ByteString -> Either E.Error Src.Module
31fromByteString :: Pkg.Name -> BS.ByteString -> Either E.Error Src.Module
34fromByteString pkg source =
32fromByteString pkg source =
35− case P.fromByteString (chompModule pkg) source of
33+ case P.fromByteString (chompModule pkg) E.ModuleBadEnd source of
36− P.Ok modul _ -> checkModule modul
34+ Right modul -> checkModule modul
37− P.Err err -> Left (E.ParseError err)
35+ Left err -> Left (E.ParseError err)
38
36
39
37
40
38
@@ -44,7 +42,6 @@ fromByteString pkg source =
44data Module =
42data Module =
45 Module
43 Module
46 { _header :: Maybe Header
44 { _header :: Maybe Header
47− , _comment :: Maybe Src.Comment
48 , _imports :: [Src.Import]
45 , _imports :: [Src.Import]
49 , _infixes :: [A.Located Src.Infix]
46 , _infixes :: [A.Located Src.Infix]
50 , _decls :: [Decl.Decl]
47 , _decls :: [Decl.Decl]
@@ -53,14 +50,11 @@ data Module =
53
50
54chompModule :: Pkg.Name -> Parser E.Module Module
51chompModule :: Pkg.Name -> Parser E.Module Module
55chompModule pkg =
52chompModule pkg =
56− do freshLine E.FreshLine
57− header <- chompHeader
53+ do header <- chompHeader
58− comment <- chompModuleDocComment
59 imports <- chompImports (if pkg == Pkg.core then [] else Imports.defaults)
54 imports <- chompImports (if pkg == Pkg.core then [] else Imports.defaults)
60 infixes <- if Pkg.isKernel pkg then chompInfixes [] else return []
55 infixes <- if Pkg.isKernel pkg then chompInfixes [] else return []
61 decls <- specialize E.Declarations $ chompDecls []
56 decls <- specialize E.Declarations $ chompDecls []
62− endOfFile
63− return (Module header comment imports infixes decls)
57+ return (Module header imports infixes decls)
64
58
65
59
66
60
@@ -68,23 +62,18 @@ chompModule pkg =
68
62
69
63
70checkModule :: Module -> Either E.Error Src.Module
64checkModule :: Module -> Either E.Error Src.Module
71−checkModule (Module maybeHeader maybeComment imports infixes decls) =
65+checkModule (Module maybeHeader imports infixes decls) =
72 let
66 let
73 (values, unions, aliases, ports) = categorizeDecls [] [] [] [] decls
67 (values, unions, aliases, ports) = categorizeDecls [] [] [] [] decls
74−
75− docs =
76− case maybeComment of
77− Just c -> Just (Src.Docs c (getComments decls []))
78− Nothing -> Nothing
79 in
68 in
80 case maybeHeader of
69 case maybeHeader of
81− Just (Header name effects exports) ->
70+ Just (Header name effects exports docs) ->
82− Src.Module (Just name) exports docs imports values unions aliases infixes
71+ Src.Module (Just name) exports (toDocs docs decls) imports values unions aliases infixes
83 <$> checkEffects ports effects
72 <$> checkEffects ports effects
84
73
85 Nothing ->
74 Nothing ->
86 Right $
75 Right $
87− Src.Module Nothing (A.At A.one Src.Open) docs imports values unions aliases infixes $
76+ Src.Module Nothing (A.At A.one Src.Open) (Src.NoDocs A.one) imports values unions aliases infixes $
88 case ports of
77 case ports of
89 [] -> Src.NoEffects
78 [] -> Src.NoEffects
90 _:_ -> Src.Ports ports
79 _:_ -> Src.Ports ports
@@ -123,6 +112,20 @@ categorizeDecls values unions aliases ports decls =
123 Decl.Port _ port_ -> categorizeDecls values unions aliases (port_:ports) otherDecls
112 Decl.Port _ port_ -> categorizeDecls values unions aliases (port_:ports) otherDecls
124
113
125
114
115+
116+-- TO DOCS
117+
118+
119+toDocs :: Either A.Region Src.Comment -> [Decl.Decl] -> Src.Docs
120+toDocs comment decls =
121+ case comment of
122+ Right overview ->
123+ Src.YesDocs overview (getComments decls [])
124+
125+ Left region ->
126+ Src.NoDocs region
127+
128+
126getComments :: [Decl.Decl] -> [(Name.Name,Src.Comment)] -> [(Name.Name,Src.Comment)]
129getComments :: [Decl.Decl] -> [(Name.Name,Src.Comment)] -> [(Name.Name,Src.Comment)]
127getComments decls comments =
130getComments decls comments =
128 case decls of
131 case decls of
@@ -154,15 +157,6 @@ freshLine toFreshLineError =
154 Space.checkFreshLine toFreshLineError
157 Space.checkFreshLine toFreshLineError
155
158
156
159
157−endOfFile :: Parser E.Module ()
158−endOfFile =
159− P.Parser $ \state@(P.State pos end _ row col) _ eok _ eerr ->
160− if pos < end then
161− eerr row col E.ModuleEndOfFile
162− else
163− eok () state
164−
165−
166
160
167-- CHOMP DECLARATIONS
161-- CHOMP DECLARATIONS
168
162
@@ -190,16 +184,17 @@ chompInfixes infixes =
190-- MODULE DOC COMMENT
184-- MODULE DOC COMMENT
191
185
192
186
193−chompModuleDocComment :: Parser E.Module (Maybe Src.Comment)
187+chompModuleDocCommentSpace :: Parser E.Module (Either A.Region Src.Comment)
194−chompModuleDocComment =
188+chompModuleDocCommentSpace =
189+ do (A.At region ()) <- addLocation (freshLine E.FreshLine)
195− oneOfWithFallback
190+ oneOfWithFallback
196− [
191+ [
197− do docComment <- Space.docComment E.ImportStart E.ModuleSpace
192+ do docComment <- Space.docComment E.ImportStart E.ModuleSpace
198− Space.chomp E.ModuleSpace
193+ Space.chomp E.ModuleSpace
199− Space.checkFreshLine E.FreshLine
194+ Space.checkFreshLine E.FreshLine
200− return (Just docComment)
195+ return (Right docComment)
201− ]
196+ ]
202− Nothing
197+ (Left region)
203
198
204
199
205
200
@@ -207,7 +202,7 @@ chompModuleDocComment =
207
202
208
203
209data Header =
204data Header =
210− Header (A.Located Name.Name) Effects (A.Located Src.Exposing)
205+ Header (A.Located Name.Name) Effects (A.Located Src.Exposing) (Either A.Region Src.Comment)
211
206
212
207
213data Effects
208data Effects
@@ -218,40 +213,43 @@ data Effects
218
213
219chompHeader :: Parser E.Module (Maybe Header)
214chompHeader :: Parser E.Module (Maybe Header)
220chompHeader =
215chompHeader =
216+ do freshLine E.FreshLine
221− do start <- getPosition
217+ start <- getPosition
222 oneOfWithFallback
218 oneOfWithFallback
223 [
219 [
224 -- module MyThing exposing (..)
220 -- module MyThing exposing (..)
225 do Keyword.module_ E.ModuleProblem
221 do Keyword.module_ E.ModuleProblem
226− end <- getPosition
222+ effectEnd <- getPosition
227 Space.chompAndCheckIndent E.ModuleSpace E.ModuleProblem
223 Space.chompAndCheckIndent E.ModuleSpace E.ModuleProblem
228 name <- addLocation (Var.moduleName E.ModuleName)
224 name <- addLocation (Var.moduleName E.ModuleName)
229 Space.chompAndCheckIndent E.ModuleSpace E.ModuleProblem
225 Space.chompAndCheckIndent E.ModuleSpace E.ModuleProblem
230 Keyword.exposing_ E.ModuleProblem
226 Keyword.exposing_ E.ModuleProblem
231 Space.chompAndCheckIndent E.ModuleSpace E.ModuleProblem
227 Space.chompAndCheckIndent E.ModuleSpace E.ModuleProblem
232 exports <- addLocation (specialize E.ModuleExposing exposing)
228 exports <- addLocation (specialize E.ModuleExposing exposing)
233− freshLine E.FreshLine
229+ comment <- chompModuleDocCommentSpace
230+ return $ Just $
234− return (Just (Header name (NoEffects (A.Region start end)) exports))
231+ Header name (NoEffects (A.Region start effectEnd)) exports comment
235 ,
232 ,
236 -- port module MyThing exposing (..)
233 -- port module MyThing exposing (..)
237 do Keyword.port_ E.PortModuleProblem
234 do Keyword.port_ E.PortModuleProblem
238 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
235 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
239 Keyword.module_ E.PortModuleProblem
236 Keyword.module_ E.PortModuleProblem
240− end <- getPosition
237+ effectEnd <- getPosition
241 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
238 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
242 name <- addLocation (Var.moduleName E.PortModuleName)
239 name <- addLocation (Var.moduleName E.PortModuleName)
243 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
240 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
244 Keyword.exposing_ E.PortModuleProblem
241 Keyword.exposing_ E.PortModuleProblem
245 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
242 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
246 exports <- addLocation (specialize E.PortModuleExposing exposing)
243 exports <- addLocation (specialize E.PortModuleExposing exposing)
247− freshLine E.FreshLine
244+ comment <- chompModuleDocCommentSpace
245+ return $ Just $
248− return (Just (Header name (Ports (A.Region start end)) exports))
246+ Header name (Ports (A.Region start effectEnd)) exports comment
249 ,
247 ,
250 -- effect module MyThing where { command = MyCmd } exposing (..)
248 -- effect module MyThing where { command = MyCmd } exposing (..)
251 do Keyword.effect_ E.Effect
249 do Keyword.effect_ E.Effect
252 Space.chompAndCheckIndent E.ModuleSpace E.Effect
250 Space.chompAndCheckIndent E.ModuleSpace E.Effect
253 Keyword.module_ E.Effect
251 Keyword.module_ E.Effect
254− end <- getPosition
252+ effectEnd <- getPosition
255 Space.chompAndCheckIndent E.ModuleSpace E.Effect
253 Space.chompAndCheckIndent E.ModuleSpace E.Effect
256 name <- addLocation (Var.moduleName E.ModuleName)
254 name <- addLocation (Var.moduleName E.ModuleName)
257 Space.chompAndCheckIndent E.ModuleSpace E.Effect
255 Space.chompAndCheckIndent E.ModuleSpace E.Effect
@@ -262,8 +260,9 @@ chompHeader =
262 Keyword.exposing_ E.Effect
260 Keyword.exposing_ E.Effect
263 Space.chompAndCheckIndent E.ModuleSpace E.Effect
261 Space.chompAndCheckIndent E.ModuleSpace E.Effect
264 exports <- addLocation (specialize (const E.Effect) exposing)
262 exports <- addLocation (specialize (const E.Effect) exposing)
265− freshLine E.FreshLine
263+ comment <- chompModuleDocCommentSpace
264+ return $ Just $
266− return (Just (Header name (Manager (A.Region start end) manager) exports))
265+ Header name (Manager (A.Region start effectEnd) manager) exports comment
267 ]
266 ]
268 -- default header
267 -- default header
269 Nothing
268 Nothing