on master help sign in
Update parser to use Src.Comment for doc comments
405621bEvan Czaplicki
compiler/src/Parse/Declaration.hs+11−12
@@ -20,7 +20,6 @@ import qualified Parse.Space as Space
20import qualified Parse.Symbol as Symbol
20import qualified Parse.Symbol as Symbol
21import qualified Parse.Type as Type
21import qualified Parse.Type as Type
22import qualified Parse.Variable as Var
22import qualified Parse.Variable as Var
23−import Parse.Space (DocComment)
24import Parse.Primitives hiding (State)
23import Parse.Primitives hiding (State)
25import qualified Parse.Primitives as P
24import qualified Parse.Primitives as P
26import qualified Reporting.Annotation as A
25import qualified Reporting.Annotation as A
@@ -32,10 +31,10 @@ import qualified Reporting.Error.Syntax as E
32
31
33
32
34data Decl
33data Decl
35− = Value (Maybe DocComment) (A.Located Src.Value)
34+ = Value (Maybe Src.Comment) (A.Located Src.Value)
36− | Union (Maybe DocComment) (A.Located Src.Union)
35+ | Union (Maybe Src.Comment) (A.Located Src.Union)
37− | Alias (Maybe DocComment) (A.Located Src.Alias)
36+ | Alias (Maybe Src.Comment) (A.Located Src.Alias)
38− | Port (Maybe DocComment) Src.Port
37+ | Port (Maybe Src.Comment) Src.Port
39
38
40
39
41declaration :: Space.Parser E.Decl Decl
40declaration :: Space.Parser E.Decl Decl
@@ -53,7 +52,7 @@ declaration =
53-- DOC COMMENT
52-- DOC COMMENT
54
53
55
54
56−chompDocComment :: Parser E.Decl (Maybe DocComment)
55+chompDocComment :: Parser E.Decl (Maybe Src.Comment)
57chompDocComment =
56chompDocComment =
58 oneOfWithFallback
57 oneOfWithFallback
59 [
58 [
@@ -70,7 +69,7 @@ chompDocComment =
70
69
71
70
72{-# INLINE valueDecl #-}
71{-# 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
74valueDecl maybeDocs start =
73valueDecl maybeDocs start =
75 do name <- Var.lower E.DeclStart
74 do name <- Var.lower E.DeclStart
76 end <- getPosition
75 end <- getPosition
@@ -90,7 +89,7 @@ valueDecl maybeDocs start =
90 ]
89 ]
91
90
92
91
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
94chompDefArgsAndBody maybeDocs start name tipe revArgs =
93chompDefArgsAndBody maybeDocs start name tipe revArgs =
95 oneOf E.DeclDefEquals
94 oneOf E.DeclDefEquals
96 [ do arg <- specialize E.DeclDefArg Pattern.term
95 [ do arg <- specialize E.DeclDefArg Pattern.term
@@ -130,7 +129,7 @@ chompMatchingName expectedName =
130
129
131
130
132{-# INLINE typeDecl #-}
131{-# 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
134typeDecl maybeDocs start =
133typeDecl maybeDocs start =
135 inContext E.DeclType (Keyword.type_ E.DeclStart) $
134 inContext E.DeclType (Keyword.type_ E.DeclStart) $
136 do Space.chompAndCheckIndent E.DT_Space E.DT_IndentName
135 do Space.chompAndCheckIndent E.DT_Space E.DT_IndentName
@@ -215,7 +214,7 @@ chompVariants variants end =
215
214
216
215
217{-# INLINE portDecl #-}
216{-# INLINE portDecl #-}
218−portDecl :: Maybe DocComment -> Space.Parser E.Decl Decl
217+portDecl :: Maybe Src.Comment -> Space.Parser E.Decl Decl
219portDecl maybeDocs =
218portDecl maybeDocs =
220 inContext E.Port (Keyword.port_ E.DeclStart) $
219 inContext E.Port (Keyword.port_ E.DeclStart) $
221 do Space.chompAndCheckIndent E.PortSpace E.PortIndentName
220 do Space.chompAndCheckIndent E.PortSpace E.PortIndentName
@@ -236,7 +235,7 @@ portDecl maybeDocs =
236
235
237-- INVARIANT: always chomps to a freshline
236-- INVARIANT: always chomps to a freshline
238--
237--
239−infix_ :: Parser E.Module (A.Located Src.Binop)
238+infix_ :: Parser E.Module (A.Located Src.Infix)
240infix_ =
239infix_ =
241 let
240 let
242 err = E.Infix
241 err = E.Infix
@@ -264,4 +263,4 @@ infix_ =
264 end <- getPosition
263 end <- getPosition
265 Space.chomp _err
264 Space.chomp _err
266 Space.checkFreshLine err
265 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
32
32
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 =
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
35+ case P.fromByteString (chompModule pkg) source of
42− P.Ok outcome _ ->
36+ P.Ok modul _ -> checkModule modul
43− case outcome of
44− Right modul -> Right modul
45− Left err -> Left err
46−
47− P.Err err ->
48− Left (E.ParseError err)
37+ P.Err err -> Left (E.ParseError err)
49
38
50
39
51
40
52-- MODULE
41-- MODULE
53
42
54
43
55−normal :: Parser E.Module (Either E.Error Src.Module)
44+data Module =
56−normal =
45+ 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]
62− endOfFile
51+ }
63− return (checkModule header comment (Imports.addDefaults imports) [] decls)
64
52
65
53
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 []
74 endOfFile
62 endOfFile
75− return (checkModule header comment imports binops decls)
63+ return (Module header comment imports infixes 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)
88
64
89
65
90
66
91-- CHECK MODULE
67-- CHECK MODULE
92
68
93
69
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) =
96 let
72 let
97 (values, unions, aliases, ports) = categorizeDecls [] [] [] [] decls
73 (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
98 in
79 in
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
103
84
104 Nothing ->
85 Nothing ->
105 Right $
86 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 $
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
143
124
144
125
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+
145
147
146-- FRESH LINES
148-- FRESH LINES
147
149
@@ -175,20 +177,20 @@ chompDecls decls =
175 (reverse (decl:decls))
177 (reverse (decl:decls))
176
178
177
179
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)
183 ]
185 ]
184− binops
186+ infixes
185
187
186
188
187
189
188-- MODULE DOC COMMENT
190-- MODULE DOC COMMENT
189
191
190
192
191−chompModuleDocComment :: Parser E.Module (Maybe Space.DocComment)
193+chompModuleDocComment :: Parser E.Module (Maybe Src.Comment)
192chompModuleDocComment =
194chompModuleDocComment =
193 oneOfWithFallback
195 oneOfWithFallback
194 [
196 [
compiler/src/Parse/Space.hs+3−10
@@ -10,7 +10,6 @@ module Parse.Space
10 , checkAligned
10 , checkAligned
11 , checkFreshLine
11 , checkFreshLine
12 --
12 --
13− , DocComment(..)
14 , docComment
13 , docComment
15 )
14 )
16 where
15 where
@@ -19,6 +18,7 @@ module Parse.Space
19import Data.Word (Word8, Word16)
18import Data.Word (Word8, Word16)
20import Foreign.Ptr (Ptr, plusPtr)
19import Foreign.Ptr (Ptr, plusPtr)
21
20
21+import qualified AST.Source as Src
22import Parse.Primitives (Row, Col)
22import Parse.Primitives (Row, Col)
23import qualified Parse.Primitives as P
23import qualified Parse.Primitives as P
24import qualified Reporting.Annotation as A
24import qualified Reporting.Annotation as A
@@ -238,14 +238,7 @@ eatMultiCommentHelp pos end row col openComments =
238-- DOCUMENTATION COMMENT
238-- DOCUMENTATION COMMENT
239
239
240
240
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
249docComment toExpectation toSpaceError =
242docComment toExpectation toSpaceError =
250 P.Parser $ \(P.State pos end indent row col) cok _ cerr eerr ->
243 P.Parser $ \(P.State pos end indent row col) cok _ cerr eerr ->
251 let
244 let
@@ -263,7 +256,7 @@ docComment toExpectation toSpaceError =
263 case status of
256 case status of
264 MultiGood ->
257 MultiGood ->
265 let
258 let
266− !comment = DocComment pos3 (plusPtr newPos (-2))
259+ !comment = Src.Comment pos3 (plusPtr newPos (-2))
267 !newState = P.State newPos end indent newRow newCol
260 !newState = P.State newPos end indent newRow newCol
268 in
261 in
269 cok comment newState
262 cok comment newState