on master help sign in
Parse doc comments along with declarations
8f2d0f4Evan Czaplicki
compiler/src/Parse/Declaration.hs+47−27
@@ -20,6 +20,7 @@ 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)
23import Parse.Primitives hiding (State)
24import Parse.Primitives hiding (State)
24import qualified Parse.Primitives as P
25import qualified Parse.Primitives as P
25import qualified Reporting.Annotation as A
26import qualified Reporting.Annotation as A
@@ -31,29 +32,46 @@ import qualified Reporting.Error.Syntax as E
31
32
32
33
33data Decl
34data Decl
34− = Value (A.Located Src.Value)
35+ = Value (Maybe DocComment) (A.Located Src.Value)
35− | Union (A.Located Src.Union)
36+ | Union (Maybe DocComment) (A.Located Src.Union)
36− | Alias (A.Located Src.Alias)
37+ | Alias (Maybe DocComment) (A.Located Src.Alias)
37− | Port Src.Port
38+ | Port (Maybe DocComment) Src.Port
38
39
39
40
40declaration :: Space.Parser E.Decl Decl
41declaration :: Space.Parser E.Decl Decl
41declaration =
42declaration =
43+ do maybeDocs <- chompDocComment
42− do start <- getPosition
44+ start <- getPosition
43 oneOf E.DeclStart
45 oneOf E.DeclStart
44− [ typeDecl start
46+ [ typeDecl maybeDocs start
45− , portDecl
47+ , portDecl maybeDocs
46− , valueDecl start
48+ , valueDecl maybeDocs start
47 ]
49 ]
48
50
49
51
50
52
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+
51-- DEFINITION and ANNOTATION
69-- DEFINITION and ANNOTATION
52
70
53
71
54{-# INLINE valueDecl #-}
72{-# INLINE valueDecl #-}
55−valueDecl :: A.Position -> Space.Parser E.Decl Decl
73+valueDecl :: Maybe DocComment -> A.Position -> Space.Parser E.Decl Decl
56−valueDecl start =
74+valueDecl maybeDocs start =
57 do name <- Var.lower E.DeclStart
75 do name <- Var.lower E.DeclStart
58 end <- getPosition
76 end <- getPosition
59 specialize (E.DeclDef name) $
77 specialize (E.DeclDef name) $
@@ -63,28 +81,27 @@ valueDecl start =
63 do word1 0x3A {-:-} E.DeclDefEquals
81 do word1 0x3A {-:-} E.DeclDefEquals
64 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentType
82 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentType
65 (tipe, _) <- specialize E.DeclDefType Type.expression
83 (tipe, _) <- specialize E.DeclDefType Type.expression
66− Space.checkFreshLine E.DeclDefAlignment
84+ Space.checkFreshLine E.DeclDefFreshLineAfterType
67 defName <- chompMatchingName name
85 defName <- chompMatchingName name
68 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentEquals
86 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentEquals
69− chompDefArgsAndBody start defName (Just tipe) []
87+ chompDefArgsAndBody maybeDocs start defName (Just tipe) []
70 ,
88 ,
71− chompDefArgsAndBody start (A.at start end name) Nothing []
89+ chompDefArgsAndBody maybeDocs start (A.at start end name) Nothing []
72 ]
90 ]
73
91
74
92
75−chompDefArgsAndBody :: A.Position -> A.Located Name.Name -> Maybe Src.Type -> [Src.Pattern] -> Space.Parser E.DeclDef Decl
93+chompDefArgsAndBody :: Maybe DocComment -> A.Position -> A.Located Name.Name -> Maybe Src.Type -> [Src.Pattern] -> Space.Parser E.DeclDef Decl
76−chompDefArgsAndBody start name tipe revArgs =
94+chompDefArgsAndBody maybeDocs start name tipe revArgs =
77 oneOf E.DeclDefEquals
95 oneOf E.DeclDefEquals
78 [ do arg <- specialize E.DeclDefArg Pattern.term
96 [ do arg <- specialize E.DeclDefArg Pattern.term
79 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentEquals
97 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentEquals
80− chompDefArgsAndBody start name tipe (arg : revArgs)
98+ chompDefArgsAndBody maybeDocs start name tipe (arg : revArgs)
81 , do word1 0x3D {-=-} E.DeclDefEquals
99 , do word1 0x3D {-=-} E.DeclDefEquals
82 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentBody
100 Space.chompAndCheckIndent E.DeclDefSpace E.DeclDefIndentBody
83 (body, end) <- specialize E.DeclDefBody Expr.expression
101 (body, end) <- specialize E.DeclDefBody Expr.expression
84− return
85− ( Value (A.at start end (Src.Value name (reverse revArgs) body tipe))
102+ let value = Src.Value name (reverse revArgs) body tipe
86− , end
103+ let avalue = A.at start end value
87− )
104+ return (Value maybeDocs avalue, end)
88 ]
105 ]
89
106
90
107
@@ -113,8 +130,8 @@ chompMatchingName expectedName =
113
130
114
131
115{-# INLINE typeDecl #-}
132{-# INLINE typeDecl #-}
116−typeDecl :: A.Position -> Space.Parser E.Decl Decl
133+typeDecl :: Maybe DocComment -> A.Position -> Space.Parser E.Decl Decl
117−typeDecl start =
134+typeDecl maybeDocs start =
118 inContext E.DeclType (Keyword.type_ E.DeclStart) $
135 inContext E.DeclType (Keyword.type_ E.DeclStart) $
119 do Space.chompAndCheckIndent E.DT_Space E.DT_IndentName
136 do Space.chompAndCheckIndent E.DT_Space E.DT_IndentName
120 oneOf E.DT_Name
137 oneOf E.DT_Name
@@ -124,14 +141,14 @@ typeDecl start =
124 (name, args) <- chompAliasNameToEquals
141 (name, args) <- chompAliasNameToEquals
125 (tipe, end) <- specialize E.AliasBody Type.expression
142 (tipe, end) <- specialize E.AliasBody Type.expression
126 let alias = A.at start end (Src.Alias name args tipe)
143 let alias = A.at start end (Src.Alias name args tipe)
127− return (Alias alias, end)
144+ return (Alias maybeDocs alias, end)
128 ,
145 ,
129 specialize E.DT_Union $
146 specialize E.DT_Union $
130 do (name, args) <- chompCustomNameToEquals
147 do (name, args) <- chompCustomNameToEquals
131 (firstVariant, firstEnd) <- Type.variant
148 (firstVariant, firstEnd) <- Type.variant
132 (variants, end) <- chompVariants [firstVariant] firstEnd
149 (variants, end) <- chompVariants [firstVariant] firstEnd
133 let union = A.at start end (Src.Union name args variants)
150 let union = A.at start end (Src.Union name args variants)
134− return (Union union, end)
151+ return (Union maybeDocs union, end)
135 ]
152 ]
136
153
137
154
@@ -198,8 +215,8 @@ chompVariants variants end =
198
215
199
216
200{-# INLINE portDecl #-}
217{-# INLINE portDecl #-}
201−portDecl :: Space.Parser E.Decl Decl
218+portDecl :: Maybe DocComment -> Space.Parser E.Decl Decl
202−portDecl =
219+portDecl maybeDocs =
203 inContext E.Port (Keyword.port_ E.DeclStart) $
220 inContext E.Port (Keyword.port_ E.DeclStart) $
204 do Space.chompAndCheckIndent E.PortSpace E.PortIndentName
221 do Space.chompAndCheckIndent E.PortSpace E.PortIndentName
205 name <- addLocation (Var.lower E.PortName)
222 name <- addLocation (Var.lower E.PortName)
@@ -207,7 +224,10 @@ portDecl =
207 word1 0x3A {-:-} E.PortColon
224 word1 0x3A {-:-} E.PortColon
208 Space.chompAndCheckIndent E.PortSpace E.PortIndentType
225 Space.chompAndCheckIndent E.PortSpace E.PortIndentType
209 (tipe, end) <- specialize E.PortType Type.expression
226 (tipe, end) <- specialize E.PortType Type.expression
227+ return
210− return ( Port (Src.Port name tipe), end )
228+ ( Port maybeDocs (Src.Port name tipe)
229+ , end
230+ )
211
231
212
232
213
233