@@ -32,78 +32,59 @@ import qualified Reporting.Error.Syntax as E
33fromByteString :: Pkg.Name -> BS.ByteString -> Either E.Error Src.Module
33fromByteString :: Pkg.Name -> BS.ByteString -> Either E.Error Src.Module
34fromByteString pkg source =
34fromByteString pkg source =
37− | pkg == Pkg.core = core
38− | Pkg.isKernel pkg = platform
41− case P.fromByteString parser source of
35+ case P.fromByteString (chompModule pkg) source of
42− P.Ok outcome _ ->
36+ P.Ok modul _ -> checkModule modul
44− Right modul -> Right modul
48− Left (E.ParseError err)
37+ P.Err err -> Left (E.ParseError err)
55−normal :: Parser E.Module (Either E.Error Src.Module)
44+data Module =
57− do freshLine E.FreshLine
46+ { _header :: Maybe Header
58− header <- chompHeader
47+ , _comment :: Maybe Src.Comment
59− comment <- chompModuleDocComment
48+ , _imports :: [Src.Import]
60− imports <- chompImports []
49+ , _infixes :: [A.Located Src.Infix]
61− decls <- specialize E.Declarations $ chompDecls []
50+ , _decls :: [Decl.Decl]
63− return (checkModule header comment (Imports.addDefaults imports) [] decls)
66−core :: Parser E.Module (Either E.Error Src.Module)
54+chompModule :: Pkg.Name -> Parser E.Module Module
67−core =
55+chompModule pkg =
68 do freshLine E.FreshLine
56 do freshLine E.FreshLine
69 header <- chompHeader
57 header <- chompHeader
70 comment <- chompModuleDocComment
58 comment <- chompModuleDocComment
71− imports <- chompImports []
59+ imports <- chompImports (if pkg == Pkg.core then [] else Imports.defaults)
72− binops <- chompBinops []
60+ infixes <- if Pkg.isKernel pkg then chompInfixes [] else return []
73 decls <- specialize E.Declarations $ chompDecls []
61 decls <- specialize E.Declarations $ chompDecls []
75− return (checkModule header comment imports binops decls)
63+ return (Module header comment imports infixes decls)
78−platform :: Parser E.Module (Either E.Error Src.Module)
80− do freshLine E.FreshLine
81− header <- chompHeader
82− comment <- chompModuleDocComment
83− imports <- chompImports []
84− binops <- chompBinops []
85− decls <- specialize E.Declarations $ chompDecls []
87− return (checkModule header comment (Imports.addDefaults imports) binops decls)
91-- CHECK MODULE
67-- CHECK MODULE
94−checkModule :: Maybe Header -> Maybe Space.DocComment -> [Src.Import] -> [A.Located Src.Binop] -> [Decl.Decl] -> Either E.Error Src.Module
70+checkModule :: Module -> Either E.Error Src.Module
95−checkModule maybeHeader _maybeDocs imports binops decls =
71+checkModule (Module maybeHeader maybeComment imports infixes decls) =
97 (values, unions, aliases, ports) = categorizeDecls [] [] [] [] decls
73 (values, unions, aliases, ports) = categorizeDecls [] [] [] [] decls
77+ Just c -> Just (Src.Docs c (getComments decls []))
99 case maybeHeader of
80 case maybeHeader of
100 Just (Header name effects exports) ->
81 Just (Header name effects exports) ->
101− Src.Module (Just name) exports imports values unions aliases binops
82+ Src.Module (Just name) exports docs imports values unions aliases infixes
102 <$> checkEffects ports effects
83 <$> checkEffects ports effects
104 Nothing ->
85 Nothing ->
106− Src.Module Nothing (A.At A.one Src.Open) imports values unions aliases binops $
87+ Src.Module Nothing (A.At A.one Src.Open) docs imports values unions aliases infixes $
107 case ports of
88 case ports of
108 [] -> Src.NoEffects
89 [] -> Src.NoEffects
109 _:_ -> Src.Ports ports
90 _:_ -> Src.Ports ports
@@ -142,6 +123,27 @@ categorizeDecls values unions aliases ports decls =
142 Decl.Port _ port_ -> categorizeDecls values unions aliases (port_:ports) otherDecls
123 Decl.Port _ port_ -> categorizeDecls values unions aliases (port_:ports) otherDecls
126+getComments :: [Decl.Decl] -> [(Name.Name,Src.Comment)] -> [(Name.Name,Src.Comment)]
127+getComments decls comments =
134+ Decl.Value c (A.At _ (Src.Value n _ _ _)) -> getComments otherDecls (addComment c n comments)
135+ Decl.Union c (A.At _ (Src.Union n _ _ )) -> getComments otherDecls (addComment c n comments)
136+ Decl.Alias c (A.At _ (Src.Alias n _ _ )) -> getComments otherDecls (addComment c n comments)
137+ Decl.Port c (Src.Port n _ ) -> getComments otherDecls (addComment c n comments)
140+addComment :: Maybe Src.Comment -> A.Located Name.Name -> [(Name.Name,Src.Comment)] -> [(Name.Name,Src.Comment)]
141+addComment maybeComment (A.At _ name) comments =
142+ case maybeComment of
143+ Just comment -> (name, comment) : comments
146-- FRESH LINES
148-- FRESH LINES
@@ -175,20 +177,20 @@ chompDecls decls =
175 (reverse (decl:decls))
177 (reverse (decl:decls))
178−chompBinops :: [A.Located Src.Binop] -> Parser E.Module [A.Located Src.Binop]
180+chompInfixes :: [A.Located Src.Infix] -> Parser E.Module [A.Located Src.Infix]
179−chompBinops binops =
181+chompInfixes infixes =
180 oneOfWithFallback
182 oneOfWithFallback
181 [ do binop <- Decl.infix_
183 [ do binop <- Decl.infix_
182− chompBinops (binop:binops)
184+ chompInfixes (binop:infixes)
188-- MODULE DOC COMMENT
190-- MODULE DOC COMMENT
191−chompModuleDocComment :: Parser E.Module (Maybe Space.DocComment)
193+chompModuleDocComment :: Parser E.Module (Maybe Src.Comment)
192chompModuleDocComment =
194chompModuleDocComment =
193 oneOfWithFallback
195 oneOfWithFallback