on master help sign in
conditionally gather module documentation as snippets

Only hold on to the snippets if there is a module comment. Without that, it'll be impossible to generate docs, so we can let all the snippets get deallocated.

commitca649a36e2e9ee4869784a818d2523d9c30cf599 authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 3 Jun 2019, 11:53 (7 years ago) parentbb0e3b1
1 file changed+49−50
compiler/src/Parse/Module.hs+49−50
compiler/src/Parse/Module.hs+49−50
@@ -2,8 +2,6 @@
22{-# LANGUAGE OverloadedStrings #-}
33module Parse.Module
44 ( fromByteString
5− , Header(..)
6− , Effects(..)
75 , chompImports
86 )
97 where
@@ -32,9 +30,9 @@ import qualified Reporting.Error.Syntax as E
3230
3331fromByteString :: Pkg.Name -> BS.ByteString -> Either E.Error Src.Module
3432fromByteString pkg source =
35− case P.fromByteString (chompModule pkg) source of
36− P.Ok modul _ -> checkModule modul
37− P.Err err -> Left (E.ParseError err)
33+ case P.fromByteString (chompModule pkg) E.ModuleBadEnd source of
34+ Right modul -> checkModule modul
35+ Left err -> Left (E.ParseError err)
3836
3937
4038
@@ -44,7 +42,6 @@ fromByteString pkg source =
4442data Module =
4543 Module
4644 { _header :: Maybe Header
47− , _comment :: Maybe Src.Comment
4845 , _imports :: [Src.Import]
4946 , _infixes :: [A.Located Src.Infix]
5047 , _decls :: [Decl.Decl]
@@ -53,14 +50,11 @@ data Module =
5350
5451chompModule :: Pkg.Name -> Parser E.Module Module
5552chompModule pkg =
56− do freshLine E.FreshLine
57− header <- chompHeader
58− comment <- chompModuleDocComment
53+ do header <- chompHeader
5954 imports <- chompImports (if pkg == Pkg.core then [] else Imports.defaults)
6055 infixes <- if Pkg.isKernel pkg then chompInfixes [] else return []
6156 decls <- specialize E.Declarations $ chompDecls []
62− endOfFile
63− return (Module header comment imports infixes decls)
57+ return (Module header imports infixes decls)
6458
6559
6660
@@ -68,23 +62,18 @@ chompModule pkg =
6862
6963
7064checkModule :: Module -> Either E.Error Src.Module
71−checkModule (Module maybeHeader maybeComment imports infixes decls) =
65+checkModule (Module maybeHeader imports infixes decls) =
7266 let
7367 (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
7968 in
8069 case maybeHeader of
81− Just (Header name effects exports) ->
82− Src.Module (Just name) exports docs imports values unions aliases infixes
70+ Just (Header name effects exports docs) ->
71+ Src.Module (Just name) exports (toDocs docs decls) imports values unions aliases infixes
8372 <$> checkEffects ports effects
8473
8574 Nothing ->
8675 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 $
8877 case ports of
8978 [] -> Src.NoEffects
9079 _:_ -> Src.Ports ports
@@ -123,6 +112,20 @@ categorizeDecls values unions aliases ports decls =
123112 Decl.Port _ port_ -> categorizeDecls values unions aliases (port_:ports) otherDecls
124113
125114
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+
126129getComments :: [Decl.Decl] -> [(Name.Name,Src.Comment)] -> [(Name.Name,Src.Comment)]
127130getComments decls comments =
128131 case decls of
@@ -154,15 +157,6 @@ freshLine toFreshLineError =
154157 Space.checkFreshLine toFreshLineError
155158
156159
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−
166160
167161-- CHOMP DECLARATIONS
168162
@@ -190,16 +184,17 @@ chompInfixes infixes =
190184-- MODULE DOC COMMENT
191185
192186
193−chompModuleDocComment :: Parser E.Module (Maybe Src.Comment)
194−chompModuleDocComment =
195− oneOfWithFallback
196− [
197− do docComment <- Space.docComment E.ImportStart E.ModuleSpace
198− Space.chomp E.ModuleSpace
199− Space.checkFreshLine E.FreshLine
200− return (Just docComment)
201− ]
202− Nothing
187+chompModuleDocCommentSpace :: Parser E.Module (Either A.Region Src.Comment)
188+chompModuleDocCommentSpace =
189+ do (A.At region ()) <- addLocation (freshLine E.FreshLine)
190+ oneOfWithFallback
191+ [
192+ do docComment <- Space.docComment E.ImportStart E.ModuleSpace
193+ Space.chomp E.ModuleSpace
194+ Space.checkFreshLine E.FreshLine
195+ return (Right docComment)
196+ ]
197+ (Left region)
203198
204199
205200
@@ -207,7 +202,7 @@ chompModuleDocComment =
207202
208203
209204data 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)
211206
212207
213208data Effects
@@ -218,40 +213,43 @@ data Effects
218213
219214chompHeader :: Parser E.Module (Maybe Header)
220215chompHeader =
221− do start <- getPosition
216+ do freshLine E.FreshLine
217+ start <- getPosition
222218 oneOfWithFallback
223219 [
224220 -- module MyThing exposing (..)
225221 do Keyword.module_ E.ModuleProblem
226− end <- getPosition
222+ effectEnd <- getPosition
227223 Space.chompAndCheckIndent E.ModuleSpace E.ModuleProblem
228224 name <- addLocation (Var.moduleName E.ModuleName)
229225 Space.chompAndCheckIndent E.ModuleSpace E.ModuleProblem
230226 Keyword.exposing_ E.ModuleProblem
231227 Space.chompAndCheckIndent E.ModuleSpace E.ModuleProblem
232228 exports <- addLocation (specialize E.ModuleExposing exposing)
233− freshLine E.FreshLine
234− return (Just (Header name (NoEffects (A.Region start end)) exports))
229+ comment <- chompModuleDocCommentSpace
230+ return $ Just $
231+ Header name (NoEffects (A.Region start effectEnd)) exports comment
235232 ,
236233 -- port module MyThing exposing (..)
237234 do Keyword.port_ E.PortModuleProblem
238235 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
239236 Keyword.module_ E.PortModuleProblem
240− end <- getPosition
237+ effectEnd <- getPosition
241238 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
242239 name <- addLocation (Var.moduleName E.PortModuleName)
243240 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
244241 Keyword.exposing_ E.PortModuleProblem
245242 Space.chompAndCheckIndent E.ModuleSpace E.PortModuleProblem
246243 exports <- addLocation (specialize E.PortModuleExposing exposing)
247− freshLine E.FreshLine
248− return (Just (Header name (Ports (A.Region start end)) exports))
244+ comment <- chompModuleDocCommentSpace
245+ return $ Just $
246+ Header name (Ports (A.Region start effectEnd)) exports comment
249247 ,
250248 -- effect module MyThing where { command = MyCmd } exposing (..)
251249 do Keyword.effect_ E.Effect
252250 Space.chompAndCheckIndent E.ModuleSpace E.Effect
253251 Keyword.module_ E.Effect
254− end <- getPosition
252+ effectEnd <- getPosition
255253 Space.chompAndCheckIndent E.ModuleSpace E.Effect
256254 name <- addLocation (Var.moduleName E.ModuleName)
257255 Space.chompAndCheckIndent E.ModuleSpace E.Effect
@@ -262,8 +260,9 @@ chompHeader =
262260 Keyword.exposing_ E.Effect
263261 Space.chompAndCheckIndent E.ModuleSpace E.Effect
264262 exports <- addLocation (specialize (const E.Effect) exposing)
265− freshLine E.FreshLine
266− return (Just (Header name (Manager (A.Region start end) manager) exports))
263+ comment <- chompModuleDocCommentSpace
264+ return $ Just $
265+ Header name (Manager (A.Region start effectEnd) manager) exports comment
267266 ]
268267 -- default header
269268 Nothing