@@ -20,6 +20,7 @@ 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)
2324import Parse.Primitives hiding (State)
2425import qualified Parse.Primitives as P
2526import qualified Reporting.Annotation as A
@@ -31,29 +32,46 @@ import qualified Reporting.Error.Syntax as E
3132
3233
3334data Decl
34− = Value (A.Located Src.Value)
35− | Union (A.Located Src.Union)
36− | Alias (A.Located Src.Alias)
37− | Port Src.Port
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
3839
3940
4041declaration :: Space.Parser E.Decl Decl
4142declaration =
42− do start <- getPosition
43+ do maybeDocs <- chompDocComment
44+ start <- getPosition
4345 oneOf E.DeclStart
44− [ typeDecl start
45− , portDecl
46− , valueDecl start
46+ [ typeDecl maybeDocs start
47+ , portDecl maybeDocs
48+ , valueDecl maybeDocs start
4749 ]
4850
4951
5052
53+-- DOC COMMENT
54+
55+
56+chompDocComment :: Parser E.Decl (Maybe DocComment)
57+chompDocComment =
58+ oneOfWithFallback
59+ [
60+ do docComment <- Space.docComment E.DeclStart E.DeclSpace
61+ Space.chomp E.DeclSpace
62+ Space.checkFreshLine E.DeclFreshLineAfterDocComment
63+ return (Just docComment)
64+ ]
65+ Nothing
66+
67+
68+
5169-- DEFINITION and ANNOTATION
5270
5371
5472{-# INLINE valueDecl #-}
55−valueDecl :: A.Position -> Space.Parser E.Decl Decl
56−valueDecl start =
73+valueDecl :: Maybe DocComment -> A.Position -> Space.Parser E.Decl Decl
74+valueDecl maybeDocs start =
5775 do name <- Var.lower E.DeclStart
5876 end <- getPosition
5977 specialize (E.DeclDef name) $
@@ -63,28 +81,27 @@ valueDecl start =
6381 do word1 0x3A {-:-} E.DeclDefEquals
6482 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentType
6583 (tipe, _) <- specialize E.DeclDefType Type.expression
66− Space.checkFreshLine E.DeclDefAlignment
84+ Space.checkFreshLine E.DeclDefFreshLineAfterType
6785 defName <- chompMatchingName name
6886 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentEquals
69− chompDefArgsAndBody start defName (Just tipe) []
87+ chompDefArgsAndBody maybeDocs start defName (Just tipe) []
7088 ,
71− chompDefArgsAndBody start (A.at start end name) Nothing []
89+ chompDefArgsAndBody maybeDocs start (A.at start end name) Nothing []
7290 ]
7391
7492
75−chompDefArgsAndBody :: A.Position -> A.Located Name.Name -> Maybe Src.Type -> [Src.Pattern] -> Space.Parser E.DeclDef Decl
76−chompDefArgsAndBody start name tipe revArgs =
93+chompDefArgsAndBody :: Maybe DocComment -> A.Position -> A.Located Name.Name -> Maybe Src.Type -> [Src.Pattern] -> Space.Parser E.DeclDef Decl
94+chompDefArgsAndBody maybeDocs start name tipe revArgs =
7795 oneOf E.DeclDefEquals
7896 [ do arg <- specialize E.DeclDefArg Pattern.term
7997 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentEquals
80− chompDefArgsAndBody start name tipe (arg : revArgs)
98+ chompDefArgsAndBody maybeDocs start name tipe (arg : revArgs)
8199 , do word1 0x3D {-=-} E.DeclDefEquals
82100 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentBody
83101 (body, end) <- specialize E.DeclDefBody Expr.expression
84− return
85− ( Value (A.at start end (Src.Value name (reverse revArgs) body tipe))
86− , end
87− )
102+ let value = Src.Value name (reverse revArgs) body tipe
103+ let avalue = A.at start end value
104+ return (Value maybeDocs avalue, end)
88105 ]
89106
90107
@@ -113,8 +130,8 @@ chompMatchingName expectedName =
113130
114131
115132{-# INLINE typeDecl #-}
116−typeDecl :: A.Position -> Space.Parser E.Decl Decl
117−typeDecl start =
133+typeDecl :: Maybe DocComment -> A.Position -> Space.Parser E.Decl Decl
134+typeDecl maybeDocs start =
118135 inContext E.DeclType (Keyword.type_ E.DeclStart) $
119136 do Space.chompAndCheckIndent E.DT_Space E.DT_IndentName
120137 oneOf E.DT_Name
@@ -124,14 +141,14 @@ typeDecl start =
124141 (name, args) <- chompAliasNameToEquals
125142 (tipe, end) <- specialize E.AliasBody Type.expression
126143 let alias = A.at start end (Src.Alias name args tipe)
127− return (Alias alias, end)
144+ return (Alias maybeDocs alias, end)
128145 ,
129146 specialize E.DT_Union $
130147 do (name, args) <- chompCustomNameToEquals
131148 (firstVariant, firstEnd) <- Type.variant
132149 (variants, end) <- chompVariants [firstVariant] firstEnd
133150 let union = A.at start end (Src.Union name args variants)
134− return (Union union, end)
151+ return (Union maybeDocs union, end)
135152 ]
136153
137154
@@ -198,8 +215,8 @@ chompVariants variants end =
198215
199216
200217{-# INLINE portDecl #-}
201−portDecl :: Space.Parser E.Decl Decl
202−portDecl =
218+portDecl :: Maybe DocComment -> Space.Parser E.Decl Decl
219+portDecl maybeDocs =
203220 inContext E.Port (Keyword.port_ E.DeclStart) $
204221 do Space.chompAndCheckIndent E.PortSpace E.PortIndentName
205222 name <- addLocation (Var.lower E.PortName)
@@ -207,7 +224,10 @@ portDecl =
207224 word1 0x3A {-:-} E.PortColon
208225 Space.chompAndCheckIndent E.PortSpace E.PortIndentType
209226 (tipe, end) <- specialize E.PortType Type.expression
210− return ( Port (Src.Port name tipe), end )
227+ return
228+ ( Port maybeDocs (Src.Port name tipe)
229+ , end
230+ )
211231
212232
213233