on master help sign in
Update parser to use Src.Comment for doc comments

Also unify the three separate module parsers into one

commit405621b54ca696fa772c20c034ea316400a6b89f authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 14 May 2019, 18:19 (7 years ago) parent23c9c2a
3 files changed+65−71
compiler/src/Parse/Declaration.hs+11−12 compiler/src/Parse/Module.hs+51−49 compiler/src/Parse/Space.hs+3−10
compiler/src/Parse/Declaration.hs+11−12
@@ -20,7 +20,6 @@ import qualified Parse.Space as Space
2020import qualified Parse.Symbol as Symbol
2121import qualified Parse.Type as Type
2222import qualified Parse.Variable as Var
23−import Parse.Space (DocComment)
2423import Parse.Primitives hiding (State)
2524import qualified Parse.Primitives as P
2625import qualified Reporting.Annotation as A
@@ -32,10 +31,10 @@ import qualified Reporting.Error.Syntax as E
3231
3332
3433data Decl
35− = Value (Maybe DocComment) (A.Located Src.Value)
36− | Union (Maybe DocComment) (A.Located Src.Union)
37− | Alias (Maybe DocComment) (A.Located Src.Alias)
38− | Port (Maybe DocComment) Src.Port
34+ = Value (Maybe Src.Comment) (A.Located Src.Value)
35+ | Union (Maybe Src.Comment) (A.Located Src.Union)
36+ | Alias (Maybe Src.Comment) (A.Located Src.Alias)
37+ | Port (Maybe Src.Comment) Src.Port
3938
4039
4140declaration :: Space.Parser E.Decl Decl
@@ -53,7 +52,7 @@ declaration =
5352-- DOC COMMENT
5453
5554
56−chompDocComment :: Parser E.Decl (Maybe DocComment)
55+chompDocComment :: Parser E.Decl (Maybe Src.Comment)
5756chompDocComment =
5857 oneOfWithFallback
5958 [
@@ -70,7 +69,7 @@ chompDocComment =
7069
7170
7271{-# INLINE valueDecl #-}
73−valueDecl :: Maybe DocComment -> A.Position -> Space.Parser E.Decl Decl
72+valueDecl :: Maybe Src.Comment -> A.Position -> Space.Parser E.Decl Decl
7473valueDecl maybeDocs start =
7574 do name <- Var.lower E.DeclStart
7675 end <- getPosition
@@ -90,7 +89,7 @@ valueDecl maybeDocs start =
9089 ]
9190
9291
93−chompDefArgsAndBody :: Maybe DocComment -> A.Position -> A.Located Name.Name -> Maybe Src.Type -> [Src.Pattern] -> Space.Parser E.DeclDef Decl
92+chompDefArgsAndBody :: Maybe Src.Comment -> A.Position -> A.Located Name.Name -> Maybe Src.Type -> [Src.Pattern] -> Space.Parser E.DeclDef Decl
9493chompDefArgsAndBody maybeDocs start name tipe revArgs =
9594 oneOf E.DeclDefEquals
9695 [ do arg <- specialize E.DeclDefArg Pattern.term
@@ -130,7 +129,7 @@ chompMatchingName expectedName =
130129
131130
132131{-# INLINE typeDecl #-}
133−typeDecl :: Maybe DocComment -> A.Position -> Space.Parser E.Decl Decl
132+typeDecl :: Maybe Src.Comment -> A.Position -> Space.Parser E.Decl Decl
134133typeDecl maybeDocs start =
135134 inContext E.DeclType (Keyword.type_ E.DeclStart) $
136135 do Space.chompAndCheckIndent E.DT_Space E.DT_IndentName
@@ -215,7 +214,7 @@ chompVariants variants end =
215214
216215
217216{-# INLINE portDecl #-}
218−portDecl :: Maybe DocComment -> Space.Parser E.Decl Decl
217+portDecl :: Maybe Src.Comment -> Space.Parser E.Decl Decl
219218portDecl maybeDocs =
220219 inContext E.Port (Keyword.port_ E.DeclStart) $
221220 do Space.chompAndCheckIndent E.PortSpace E.PortIndentName
@@ -236,7 +235,7 @@ portDecl maybeDocs =
236235
237236-- INVARIANT: always chomps to a freshline
238237--
239−infix_ :: Parser E.Module (A.Located Src.Binop)
238+infix_ :: Parser E.Module (A.Located Src.Infix)
240239infix_ =
241240 let
242241 err = E.Infix
@@ -264,4 +263,4 @@ infix_ =
264263 end <- getPosition
265264 Space.chomp _err
266265 Space.checkFreshLine err
267− return (A.at start end (Src.Binop op associativity precedence name))
266+ return (A.at start end (Src.Infix op associativity precedence name))
compiler/src/Parse/Module.hs+51−49
@@ -32,78 +32,59 @@ import qualified Reporting.Error.Syntax as E
3232
3333fromByteString :: Pkg.Name -> BS.ByteString -> Either E.Error Src.Module
3434fromByteString pkg source =
35− let
36− parser
37− | pkg == Pkg.core = core
38− | Pkg.isKernel pkg = platform
39− | otherwise = normal
40− in
41− case P.fromByteString parser source of
42− P.Ok outcome _ ->
43− case outcome of
44− Right modul -> Right modul
45− Left err -> Left err
46−
47− P.Err err ->
48− Left (E.ParseError err)
35+ case P.fromByteString (chompModule pkg) source of
36+ P.Ok modul _ -> checkModule modul
37+ P.Err err -> Left (E.ParseError err)
4938
5039
5140
5241-- MODULE
5342
5443
55−normal :: Parser E.Module (Either E.Error Src.Module)
56−normal =
57− do freshLine E.FreshLine
58− header <- chompHeader
59− comment <- chompModuleDocComment
60− imports <- chompImports []
61− decls <- specialize E.Declarations $ chompDecls []
62− endOfFile
63− return (checkModule header comment (Imports.addDefaults imports) [] decls)
44+data Module =
45+ Module
46+ { _header :: Maybe Header
47+ , _comment :: Maybe Src.Comment
48+ , _imports :: [Src.Import]
49+ , _infixes :: [A.Located Src.Infix]
50+ , _decls :: [Decl.Decl]
51+ }
6452
6553
66−core :: Parser E.Module (Either E.Error Src.Module)
67−core =
54+chompModule :: Pkg.Name -> Parser E.Module Module
55+chompModule pkg =
6856 do freshLine E.FreshLine
6957 header <- chompHeader
7058 comment <- chompModuleDocComment
71− imports <- chompImports []
72− binops <- chompBinops []
59+ imports <- chompImports (if pkg == Pkg.core then [] else Imports.defaults)
60+ infixes <- if Pkg.isKernel pkg then chompInfixes [] else return []
7361 decls <- specialize E.Declarations $ chompDecls []
7462 endOfFile
75− return (checkModule header comment imports binops decls)
76−
77−
78−platform :: Parser E.Module (Either E.Error Src.Module)
79−platform =
80− do freshLine E.FreshLine
81− header <- chompHeader
82− comment <- chompModuleDocComment
83− imports <- chompImports []
84− binops <- chompBinops []
85− decls <- specialize E.Declarations $ chompDecls []
86− endOfFile
87− return (checkModule header comment (Imports.addDefaults imports) binops decls)
63+ return (Module header comment imports infixes decls)
8864
8965
9066
9167-- CHECK MODULE
9268
9369
94−checkModule :: Maybe Header -> Maybe Space.DocComment -> [Src.Import] -> [A.Located Src.Binop] -> [Decl.Decl] -> Either E.Error Src.Module
95−checkModule maybeHeader _maybeDocs imports binops decls =
70+checkModule :: Module -> Either E.Error Src.Module
71+checkModule (Module maybeHeader maybeComment imports infixes decls) =
9672 let
9773 (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
9879 in
9980 case maybeHeader of
10081 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
10283 <$> checkEffects ports effects
10384
10485 Nothing ->
10586 Right $
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 $
10788 case ports of
10889 [] -> Src.NoEffects
10990 _:_ -> Src.Ports ports
@@ -142,6 +123,27 @@ categorizeDecls values unions aliases ports decls =
142123 Decl.Port _ port_ -> categorizeDecls values unions aliases (port_:ports) otherDecls
143124
144125
126+getComments :: [Decl.Decl] -> [(Name.Name,Src.Comment)] -> [(Name.Name,Src.Comment)]
127+getComments decls comments =
128+ case decls of
129+ [] ->
130+ comments
131+
132+ decl:otherDecls ->
133+ case decl of
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)
138+
139+
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
144+ Nothing -> comments
145+
146+
145147
146148-- FRESH LINES
147149
@@ -175,20 +177,20 @@ chompDecls decls =
175177 (reverse (decl:decls))
176178
177179
178−chompBinops :: [A.Located Src.Binop] -> Parser E.Module [A.Located Src.Binop]
179−chompBinops binops =
180+chompInfixes :: [A.Located Src.Infix] -> Parser E.Module [A.Located Src.Infix]
181+chompInfixes infixes =
180182 oneOfWithFallback
181183 [ do binop <- Decl.infix_
182− chompBinops (binop:binops)
184+ chompInfixes (binop:infixes)
183185 ]
184− binops
186+ infixes
185187
186188
187189
188190-- MODULE DOC COMMENT
189191
190192
191−chompModuleDocComment :: Parser E.Module (Maybe Space.DocComment)
193+chompModuleDocComment :: Parser E.Module (Maybe Src.Comment)
192194chompModuleDocComment =
193195 oneOfWithFallback
194196 [
compiler/src/Parse/Space.hs+3−10
@@ -10,7 +10,6 @@ module Parse.Space
1010 , checkAligned
1111 , checkFreshLine
1212 --
13− , DocComment(..)
1413 , docComment
1514 )
1615 where
@@ -19,6 +18,7 @@ module Parse.Space
1918import Data.Word (Word8, Word16)
2019import Foreign.Ptr (Ptr, plusPtr)
2120
21+import qualified AST.Source as Src
2222import Parse.Primitives (Row, Col)
2323import qualified Parse.Primitives as P
2424import qualified Reporting.Annotation as A
@@ -238,14 +238,7 @@ eatMultiCommentHelp pos end row col openComments =
238238-- DOCUMENTATION COMMENT
239239
240240
241−data DocComment =
242− DocComment
243− { _start :: Ptr Word8
244− , _end :: Ptr Word8
245− }
246−
247−
248−docComment :: (Row -> Col -> x) -> (E.Space -> Row -> Col -> x) -> P.Parser x DocComment
241+docComment :: (Row -> Col -> x) -> (E.Space -> Row -> Col -> x) -> P.Parser x Src.Comment
249242docComment toExpectation toSpaceError =
250243 P.Parser $ \(P.State pos end indent row col) cok _ cerr eerr ->
251244 let
@@ -263,7 +256,7 @@ docComment toExpectation toSpaceError =
263256 case status of
264257 MultiGood ->
265258 let
266− !comment = DocComment pos3 (plusPtr newPos (-2))
259+ !comment = Src.Comment pos3 (plusPtr newPos (-2))
267260 !newState = P.State newPos end indent newRow newCol
268261 in
269262 cok comment newState