on master help sign in
start with def name when the first REPL lines are a type annotation
243dcc6Evan Czaplicki
terminal/src/Repl.hs+44−9
@@ -43,6 +43,9 @@ import qualified Parse.Expression as PE
43import qualified Parse.Declaration as PD
43import qualified Parse.Declaration as PD
44import qualified Parse.Module as PM
44import qualified Parse.Module as PM
45import qualified Parse.Primitives as P
45import qualified Parse.Primitives as P
46+import qualified Parse.Space as PS
47+import qualified Parse.Type as PT
48+import qualified Parse.Variable as PV
46import Parse.Primitives (Row, Col)
49import Parse.Primitives (Row, Col)
47import qualified Reporting
50import qualified Reporting
48import qualified Reporting.Annotation as A
51import qualified Reporting.Annotation as A
@@ -172,12 +175,12 @@ read =
172 in
175 in
173 case categorize lines of
176 case categorize lines of
174 Done input -> return input
177 Done input -> return input
175− Continue -> readMore lines
178+ Continue p -> readMore lines p
176
179
177
180
178−readMore :: Lines -> Repl.InputT M Input
181+readMore :: Lines -> Prefill -> Repl.InputT M Input
179−readMore previousLines =
182+readMore previousLines prefill =
180− do input <- Repl.getInputLineWithInitial "| " (" ","")
183+ do input <- Repl.getInputLineWithInitial "| " (renderPrefill prefill, "")
181 case input of
184 case input of
182 Nothing ->
185 Nothing ->
183 return Skip
186 return Skip
@@ -188,7 +191,22 @@ readMore previousLines =
188 in
191 in
189 case categorize lines of
192 case categorize lines of
190 Done input -> return input
193 Done input -> return input
191− Continue -> readMore lines
194+ Continue p -> readMore lines p
195+
196+
197+data Prefill
198+ = Indent
199+ | DefStart N.Name
200+
201+
202+renderPrefill :: Prefill -> String
203+renderPrefill lineStart =
204+ case lineStart of
205+ Indent ->
206+ " "
207+
208+ DefStart name ->
209+ N.toChars name ++ " "
192
210
193
211
194
212
@@ -241,7 +259,7 @@ getFirstLine (Lines x xs) =
241
259
242data CategorizedInput
260data CategorizedInput
243 = Done Input
261 = Done Input
244− | Continue
262+ | Continue Prefill
245
263
246
264
247categorize :: Lines -> CategorizedInput
265categorize :: Lines -> CategorizedInput
@@ -270,14 +288,14 @@ ifFail :: Lines -> Input -> CategorizedInput
270ifFail lines input =
288ifFail lines input =
271 if endsWithBlankLine lines
289 if endsWithBlankLine lines
272 then Done input
290 then Done input
273− else Continue
291+ else Continue Indent
274
292
275
293
276ifDone :: Lines -> Input -> CategorizedInput
294ifDone :: Lines -> Input -> CategorizedInput
277ifDone lines input =
295ifDone lines input =
278 if isSingleLine lines || endsWithBlankLine lines
296 if isSingleLine lines || endsWithBlankLine lines
279 then Done input
297 then Done input
280− else Continue
298+ else Continue Indent
281
299
282
300
283attemptDeclOrExpr :: Lines -> CategorizedInput
301attemptDeclOrExpr :: Lines -> CategorizedInput
@@ -311,7 +329,9 @@ attemptDeclOrExpr lines =
311 if exprPosition >= declPosition then
329 if exprPosition >= declPosition then
312 ifFail lines (Expr src)
330 ifFail lines (Expr src)
313 else
331 else
332+ case P.fromByteString annotation (\_ _ -> ()) src of
333+ Right name -> Continue (DefStart name)
314− ifFail lines (Decl "ERR" src)
334+ Left () -> ifFail lines (Decl "ERR" src)
315
335
316
336
317startsWithColon :: Lines -> Bool
337startsWithColon :: Lines -> Bool
@@ -361,6 +381,21 @@ toDeclPosition src decl r c =
361 (row, col)
381 (row, col)
362
382
363
383
384+annotation :: P.Parser () N.Name
385+annotation =
386+ let
387+ err _ _ = ()
388+ err_ _ _ _ = ()
389+ in
390+ do name <- PV.lower err
391+ PS.chompAndCheckIndent err_ err
392+ P.word1 0x3A {-:-} err
393+ PS.chompAndCheckIndent err_ err
394+ (_, _) <- P.specialize err_ PT.expression
395+ PS.checkFreshLine err
396+ return name
397+
398+
364
399
365-- STATE
400-- STATE
366
401