@@ -34,7 +34,6 @@ import Unison.ABT qualified as ABT
34
34
import Unison.Builtin.Decls qualified as DD
35
35
import Unison.ConstructorReference (ConstructorReference , GConstructorReference (.. ))
36
36
import Unison.ConstructorType qualified as CT
37
- import Unison.Debug qualified as Debug
38
37
import Unison.HashQualified qualified as HQ
39
38
import Unison.HashQualifiedPrime qualified as HQ'
40
39
import Unison.Name (Name )
@@ -111,7 +110,7 @@ rewriteBlock = do
111
110
rewriteTermlike kw mk = do
112
111
kw <- quasikeyword kw
113
112
lhs <- term
114
- (_spanAnn, rhs) <- layoutBlock " ==>"
113
+ (_openAnn, _spanAnn, rhs) <- layoutBlock " ==>"
115
114
pure (mk (ann kw <> ann rhs) lhs rhs)
116
115
rewriteTerm = rewriteTermlike " term" DD. rewriteTerm
117
116
rewriteCase = rewriteTermlike " case" DD. rewriteCase
@@ -233,10 +232,10 @@ matchCase = do
233
232
[ Nothing <$ quasikeyword " otherwise" ,
234
233
Just <$> infixAppOrBooleanOp
235
234
]
236
- (_spanAnn, t) <- layoutBlock " ->"
235
+ (_openAnn, _spanAnn, t) <- layoutBlock " ->"
237
236
pure (guard, t)
238
237
let unguardedBlock = label " case match" do
239
- (_spanAnn, t) <- layoutBlock " ->"
238
+ (_openAnn, _spanAnn, t) <- layoutBlock " ->"
240
239
pure (Nothing , t)
241
240
-- a pattern's RHS is either one or more guards, or a single unguarded block.
242
241
guardsAndBlocks <- guardedBlocks <|> (pure @ [] <$> unguardedBlock)
@@ -454,10 +453,12 @@ lam p = label "lambda" $ mkLam <$> P.try (some prefixDefinitionName <* reserved
454
453
in Term. lam' (ann (head vs) <> ann b) annotatedArgs b
455
454
456
455
letBlock , handle , ifthen :: (Monad m , Var v ) => TermP v m
457
- letBlock = label " let" $ (snd <$> layoutBlock " let" )
456
+ letBlock = label " let" $ do
457
+ (_openAnn, _spanAnn, tm) <- layoutBlock " let"
458
+ pure tm
458
459
handle = label " handle" do
459
- (handleSpan, b) <- block " handle"
460
- (_withSpan, handler) <- layoutBlock " with"
460
+ (_handleOpenAnn, handleSpan, b) <- block " handle"
461
+ (_withOpenAnn, _withSpan, handler) <- layoutBlock " with"
461
462
-- We don't use the annotation span from 'with' here because it will
462
463
-- include a dedent if it's at the end of block.
463
464
-- Meaning the newline gets overwritten when pretty-printing and it messes things up.
@@ -492,9 +493,9 @@ lamCase = do
492
493
493
494
ifthen = label " if" do
494
495
start <- peekAny
495
- (_spanAnn, c) <- block " if"
496
- (_spanAnn, t) <- block " then"
497
- (_spanAnn, f) <- layoutBlock " else"
496
+ (_ifOpenAnn, _spanAnn, c) <- block " if"
497
+ (_thenAnn, _spanAnn, t) <- block " then"
498
+ (_elseAnn, _spanAnn, f) <- layoutBlock " else"
498
499
pure $ Term. iff (ann start <> ann f) c t f
499
500
500
501
text :: (Var v ) => TermP v m
@@ -628,11 +629,17 @@ doc2Block = do
628
629
docTop d = \ case
629
630
Doc. Section title body -> pure $ Term. apps' (f d " Section" ) [docParagraph d title, Term. list (gann body) body]
630
631
Doc. Eval code ->
631
- Term. app (gann d) (f d " Eval" ) . addDelay . snd
632
- <$> subParse (block' False False " syntax.docEval" (pure $ pure () ) $ Ann. External <$ P. eof) code
632
+ let inner = do
633
+ (_openAnn, ann, tm) <- (block' False False " syntax.docEval" (pure $ pure () ) $ Ann. External <$ P. eof)
634
+ pure (ann, tm)
635
+ in Term. app (gann d) (f d " Eval" ) . addDelay . snd
636
+ <$> subParse inner code
633
637
Doc. ExampleBlock code ->
634
- Term. apps' (f d " ExampleBlock" ) . (Term. nat (gann d) 0 : ) . pure . addDelay . snd
635
- <$> subParse (block' False True " syntax.docExampleBlock" (pure $ pure () ) $ Ann. External <$ P. eof) code
638
+ let inner = do
639
+ (_openAnn, ann, tm) <- (block' False True " syntax.docExampleBlock" (pure $ pure () ) $ Ann. External <$ P. eof)
640
+ pure (ann, tm)
641
+ in Term. apps' (f d " ExampleBlock" ) . (Term. nat (gann d) 0 : ) . pure . addDelay . snd
642
+ <$> subParse inner code
636
643
Doc. CodeBlock label body ->
637
644
pure $
638
645
Term. apps'
@@ -1111,9 +1118,8 @@ delayQuote = P.label "quote" do
1111
1118
1112
1119
delayBlock :: (Monad m , Var v ) => P v m (Ann {- Ann spanning the whole block -} , Term v Ann )
1113
1120
delayBlock = P. label " do" do
1114
- (spanAnn, b) <- layoutBlock " do"
1115
- let argSpan = (ann b {- would be nice to use the annotation for 'do' here, but it's not terribly important -} )
1116
- pure $ (spanAnn, DD. delayTerm (ann b) argSpan b)
1121
+ (openAnn, spanAnn, b) <- layoutBlock " do"
1122
+ pure $ (spanAnn, DD. delayTerm (ann b) openAnn b)
1117
1123
1118
1124
bang :: (Monad m , Var v ) => TermP v m
1119
1125
bang = P. label " bang" do
@@ -1264,7 +1270,7 @@ destructuringBind = do
1264
1270
let boundVars' = snd <$> boundVars
1265
1271
_ <- P. lookAhead (openBlockWith " =" )
1266
1272
pure (p, boundVars')
1267
- (_spanAnn, scrute) <- layoutBlock " =" -- Dwight K. Scrute ("The People's Scrutinee")
1273
+ (_eqAnn, _spanAnn, scrute) <- layoutBlock " =" -- Dwight K. Scrute ("The People's Scrutinee")
1268
1274
let guard = Nothing
1269
1275
let absChain vs t = foldr (\ v t -> ABT. abs' (ann t) v t) t vs
1270
1276
thecase t = Term. MatchCase p (fmap (absChain boundVars) guard) $ absChain boundVars t
@@ -1310,7 +1316,7 @@ binding = label "binding" do
1310
1316
Nothing -> do
1311
1317
-- we haven't seen a type annotation, so lookahead to '=' before commit
1312
1318
(lhsLoc, name, args) <- P. try (lhs <* P. lookAhead (openBlockWith " =" ))
1313
- (_bodySpanAnn, body) <- block " ="
1319
+ (_eqAnn, _bodySpanAnn, body) <- block " ="
1314
1320
verifyRelativeName' (fmap Name. unsafeParseVar name)
1315
1321
let binding = mkBinding lhsLoc args body
1316
1322
-- We don't actually use the span annotation from the block (yet) because it
@@ -1323,7 +1329,7 @@ binding = label "binding" do
1323
1329
when (L. payload name /= L. payload nameT) $
1324
1330
customFailure $
1325
1331
SignatureNeedsAccompanyingBody nameT
1326
- (_bodySpanAnn, body) <- block " ="
1332
+ (_eqAnn, _bodySpanAnn, body) <- block " ="
1327
1333
let binding = mkBinding lhsLoc args body
1328
1334
-- We don't actually use the span annotation from the block (yet) because it
1329
1335
-- may contain a bunch of white-space and comments following a top-level-definition.
@@ -1339,10 +1345,30 @@ binding = label "binding" do
1339
1345
customFailure :: (P. MonadParsec e s m ) => e -> m a
1340
1346
customFailure = P. customFailure
1341
1347
1342
- block :: forall m v . (Monad m , Var v ) => String -> P v m (Ann , Term v Ann )
1348
+ block ::
1349
+ forall m v .
1350
+ (Monad m , Var v ) =>
1351
+ String ->
1352
+ P
1353
+ v
1354
+ m
1355
+ ( Ann {- annotation of block-open symbol, e.g. 'do', 'let' -} ,
1356
+ Ann {- annotation for whole block -} ,
1357
+ Term v Ann
1358
+ )
1343
1359
block s = block' False False s (openBlockWith s) closeBlock
1344
1360
1345
- layoutBlock :: forall m v . (Monad m , Var v ) => String -> P v m (Ann , Term v Ann )
1361
+ layoutBlock ::
1362
+ forall m v .
1363
+ (Monad m , Var v ) =>
1364
+ String ->
1365
+ P
1366
+ v
1367
+ m
1368
+ ( Ann {- annotation of block-open symbol, e.g. 'do', 'let' -} ,
1369
+ Ann {- annotation for whole layout block -} ,
1370
+ Term v Ann
1371
+ )
1346
1372
layoutBlock s = block' False False s (openBlockWith s) optionalCloseBlock
1347
1373
1348
1374
-- example: use Foo.bar.Baz + ++ x
@@ -1421,15 +1447,15 @@ block' ::
1421
1447
String ->
1422
1448
P v m (L. Token () ) ->
1423
1449
P v m end ->
1424
- P v m (Ann {- ann which spans the whole block -} , Term v Ann )
1450
+ P v m (Ann {- span for the opening token, e.g. the "do" or opening bracket -} , Ann {- ann which spans the whole block -} , Term v Ann )
1425
1451
block' isTop implicitUnitAtEnd s openBlock closeBlock = do
1426
1452
open <- openBlock
1427
1453
(names, imports) <- imports
1428
1454
_ <- optional semi
1429
1455
statements <- local (\ e -> e {names}) $ sepBy semi statement
1430
1456
end <- closeBlock
1431
1457
body <- substImports names imports <$> go open statements
1432
- pure (ann open <> ann end, body)
1458
+ pure (ann open, ann open <> ann end, body)
1433
1459
where
1434
1460
statement = asum [Binding <$> binding, DestructuringBind <$> destructuringBind, Action <$> blockTerm]
1435
1461
go :: L. Token () -> [BlockElement v ] -> P v m (Term v Ann )
@@ -1448,7 +1474,6 @@ block' isTop implicitUnitAtEnd s openBlock closeBlock = do
1448
1474
step elem result = case elem of
1449
1475
Binding ((a, v), tm) -> do
1450
1476
let fullLetRecSpan = ann a <> ann result
1451
- Debug. debugM Debug. Temp " letrec" (v, a)
1452
1477
pure $
1453
1478
Term. consLetRec
1454
1479
isTop
0 commit comments