@@ -2,8 +2,6 @@
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Parse.Module
3module Parse.Module
4 ( fromByteString
4 ( fromByteString
7 , chompImports
5 , chompImports
@@ -32,9 +30,9 @@ import qualified Reporting.Error.Syntax as E
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)
@@ -44,7 +42,6 @@ fromByteString pkg source =
44data Module =
42data 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 =
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 []
63− return (Module header comment imports infixes decls)
57+ return (Module header imports infixes decls)
@@ -68,23 +62,18 @@ chompModule pkg =
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) =
73 (values, unions, aliases, ports) = categorizeDecls [] [] [] [] decls
67 (values, unions, aliases, ports) = categorizeDecls [] [] [] [] decls
77− Just c -> Just (Src.Docs c (getComments decls []))
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
85 Nothing ->
74 Nothing ->
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
119+toDocs :: Either A.Region Src.Comment -> [Decl.Decl] -> Src.Docs
120+toDocs comment decls =
123+ Src.YesDocs overview (getComments decls [])
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
157−endOfFile :: Parser E.Module ()
159− P.Parser $ \state@(P.State pos end _ row col) _ eok _ eerr ->
161− eerr row col E.ModuleEndOfFile
167-- CHOMP DECLARATIONS
161-- CHOMP DECLARATIONS
@@ -190,16 +184,17 @@ chompInfixes infixes =
190-- MODULE DOC COMMENT
184-- MODULE DOC COMMENT
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
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)
202− Nothing
197+ (Left region)
@@ -207,7 +202,7 @@ chompModuleDocComment =
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)
213data Effects
208data Effects
@@ -218,40 +213,43 @@ data Effects
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
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
234− return (Just (Header name (NoEffects (A.Region start end)) exports))
231+ Header name (NoEffects (A.Region start effectEnd)) exports comment
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
248− return (Just (Header name (Ports (A.Region start end)) exports))
246+ Header name (Ports (A.Region start effectEnd)) exports comment
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
266− return (Just (Header name (Manager (A.Region start end) manager) exports))
265+ Header name (Manager (A.Region start effectEnd) manager) exports comment
268 -- default header
267 -- default header