1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
|
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE EmptyDataDeriving #-}
-- I don't want a warning for 'head' and 'tail' in this file. But I also don't
-- want GHCs before 9.8 to complain that they don't know the x-partial warning.
{-# OPTIONS_GHC -Wno-unrecognised-warning-flags -Wno-x-partial #-}
module HSVIS.Parser (
StageParsed,
parse,
-- * Staged AST synonyms
PProgram, PDataDef, PFunDef, PFunEq, PType, PPattern, PRHS, PExpr,
E(NoTypeSig),
) where
-- import Control.Applicative
import Control.Monad
import Control.Monad.Chronicle
import Control.Monad.Reader
import Control.Monad.State.Lazy
import Data.Char
import Data.Bifunctor (second)
import Data.Either (partitionEithers)
import Data.Foldable
import Data.List.NonEmpty (NonEmpty(..))
import Data.These
-- import Debug.Trace
import Control.FAlternative
import Data.Bag
import Data.List.NonEmpty.Util
import HSVIS.AST
import HSVIS.Diagnostic
import HSVIS.Pretty
-- | The stage for the parser
data StageParsed
type instance X DataDef StageParsed = Range
type instance X DataField StageParsed = ()
type instance X FunDef StageParsed = Range
type instance X TypeSig StageParsed = ()
type instance X FunEq StageParsed = Range
type instance X Type StageParsed = Range
type instance X Pattern StageParsed = Range
type instance X RHS StageParsed = Range
type instance X Expr StageParsed = Range
data instance E TypeSig StageParsed = NoTypeSig deriving (Show)
data instance E Type StageParsed deriving (Show)
data instance E Kind StageParsed deriving (Show)
type PProgram = Program StageParsed
type PDataDef = DataDef StageParsed
type PFunDef = FunDef StageParsed
-- type PTypeSig = TypeSig StageParsed
type PFunEq = FunEq StageParsed
type PType = Type StageParsed
type PPattern = Pattern StageParsed
type PRHS = RHS StageParsed
type PExpr = Expr StageParsed
-- Positions are zero-based in both dimensions.
-- See 'isInsideBlock' and 'isAtBlockLeft' for the two relevant "inside the
-- block" conditions.
data PS = PS
{ psBlk :: Pos -- ^ Start of current layout block
, psCur :: Pos -- ^ Current parsing position
, psRest :: String -- ^ Rest of the input
}
deriving (Show)
data Context = Context
{ ctxFile :: FilePath
, ctxLines :: [String] -- ^ The file contents, split up in lines
, ctxStack :: [String] -- ^ Stack of syntax scopes, for error reporting
}
deriving (Show)
type family BacktrackPath fail r where
BacktrackPath 'Fallible r = r
BacktrackPath 'Infallible r = ()
newtype Parser fail a = Parser
{ runParser
:: forall r.
Context
-> PS
-> (PS -> Bag Diagnostic -> a -> r) -- ^ OK: some diagnostics, but parsing succeeded
-> (Bag Diagnostic -> r) -- ^ Fatal: error that prevented parsing from proceeding
-> BacktrackPath fail r -- ^ Backtrack: alternative was exhausted without success
-> r }
type IParser = Parser 'Infallible
type FParser = Parser 'Fallible
instance Functor (Parser fail) where
fmap f (Parser g) = Parser (\ctx ps kok kfat kbt ->
g ctx ps (\ps' errs x -> kok ps' errs (f x)) kfat kbt)
instance Applicative (Parser fail) where
pure x = Parser (\_ ps kok _ _ -> kok ps mempty x)
(<*>) = ap
instance Monad (Parser fail) where
Parser g >>= f = Parser $ \ctx ps kok kfat kbt ->
g ctx ps
(\ps1 errs x ->
x `seq`
runParser (f x) ctx ps1
(\ps2 errs' y -> kok ps2 (errs <> errs') y)
(\errs' -> kfat (errs <> errs'))
kbt)
(\errs -> kfat errs)
kbt
instance FAlternative Parser where
faempty = Parser (\_ _ _ _ kbt -> kbt)
Parser f <|>> Parser g = Parser $ \ctx ps kok kfat kbt ->
f ctx ps kok kfat (g ctx ps kok kfat kbt)
noFail (Parser f) = Parser $ \ctx ps kok kfat _ -> f ctx ps kok kfat ()
toFallible :: forall fail a. KnownFallible fail => Parser fail a -> Parser 'Fallible a
toFallible (Parser f) = Parser $ \ctx ps kok kfat kbt ->
f ctx ps kok kfat (case knownFallible @fail of
SFallible -> kbt
SInfallible -> ())
instance MonadState PS (Parser fail) where
state f = Parser $ \_ ps kok _ _ ->
let (x, ps') = f ps
in kok ps' mempty x
instance MonadReader Context (Parser fail) where
reader f = Parser $ \ctx ps kok _ _ -> kok ps mempty (f ctx)
local f (Parser g) = Parser (\ctx -> g (f ctx))
instance KnownFallible fail => MonadChronicle (Bag Diagnostic) (Parser fail) where
dictate errs = Parser $ \_ ps kok _ _ -> kok ps errs ()
confess errs = Parser $ \_ _ _ kfat _ -> kfat errs
memento (Parser f) = Parser $ \ctx ps kok _ kbt ->
f ctx ps
(\ps' errs x -> kok ps' errs (Right x))
(\errs -> kok ps mempty (Left errs))
kbt
absolve def (toFallible -> Parser f) = Parser $ \ctx ps kok _ _ ->
f ctx ps
kok
(\_ -> kok ps mempty def)
(kok ps mempty def)
condemn (Parser f) = Parser $ \ctx ps kok kfat kbt ->
f ctx ps
(\ps' errs x -> if null errs
then kok ps' mempty x
else kfat errs)
kfat
kbt
retcon g (Parser f) = Parser $ \ctx ps kok kfat kbt ->
f ctx ps
(\ps' errs x -> kok ps' (g errs) x)
(\errs -> kfat (g errs))
kbt
chronicle th = case th of
This errs -> Parser (\_ _ _ kfat _ -> kfat errs)
That res -> Parser (\_ ps kok _ _ -> kok ps mempty res)
These errs res -> Parser (\_ ps kok _ _ -> kok ps errs res)
parse :: FilePath -> String -> ([Diagnostic], Maybe PProgram)
parse fp source =
runParser pProgram (Context fp (lines source) []) (PS (Pos 0 0) (Pos 0 0) source)
(\_ errs res -> if null errs
then ([], Just res)
else (toList errs, Just res))
(\errs -> (toList errs, Nothing))
() -- the program parser cannot fail! :D
pProgram :: IParser PProgram
pProgram = do
defs <- pTopDefs
let (datadefs, fundefs) = partitionEithers defs
skipWhiteComment
assertEOF Error
return (Program datadefs fundefs)
pTopDefs :: IParser [Either PDataDef PFunDef]
pTopDefs = do
faoptional pTopDef >>= \case
Nothing -> do
skipWhiteComment
faoptional eof >>= \case
Nothing -> do
raise Error "Unparseable content"
readWhileInline (const True)
pTopDefs -- will skip the possible newline
Just () -> return []
Just defs -> do
defs2 <- pTopDefs
return (defs ++ defs2)
pTopDef :: FParser [Either PDataDef PFunDef]
pTopDef = do
noFail skipWhiteComment
noFail isAtBlockLeft >>= \case
True -> map Left <$> pDataDef0 <|>> map Right <$> pFunDef0
False -> do
raise Error "Skipping unparseable content"
noFail $ readWhileInline (const True)
pTopDef
pDataDef0 :: FParser [PDataDef]
pDataDef0 = do
pos1 <- gets psCur
pKeyword "data"
noFail $ do
inlineWhite
faoptional (pIdentifier0 InBlock Uppercase WCAssume) >>= \case
Nothing -> do
raise Error "Expected data declaration after 'data'"
return []
Just name -> do
params <- famany (inlineWhite >> ranged (pIdentifier0 InBlock Lowercase WCBacktrack))
cons <- pDatacons "="
pos2 <- gets psCur
return [DataDef (Range pos1 pos2) name params (map (second (map (DataField ()))) cons)]
where
pDatacons :: String -> IParser [(Name, [PType])]
pDatacons leader = do
inlineWhite
facatch (return []) $ do
pKeySym0 leader
inlineWhite
name <- pIdentifier0 InBlock Uppercase WCAssume
fields <- noFail $ famany pTypeAtom
rest <- noFail $ pDatacons "|"
return ((name, fields) : rest)
data FunEqContext
= FirstLine
| WithTypeSig Name
| Continue Name
deriving (Show)
pFunDef0 :: FParser [PFunDef]
pFunDef0 =
faasum'
[do (sigrange, (name, typ)) <- ranged pStandaloneTypesig0
noFail $ do
faoptional (pFunEq (WithTypeSig name)) >>= \case
Just (clause1 : clauses1) -> do
clauses <- concat <$> famany (pFunEq (Continue name))
let clauses' = clause1 :| clauses1 ++ clauses
let rng = sigrange <> foldMapne extOf clauses'
return [FunDef rng name (TypeSig () typ) clauses']
_ -> do
pos <- gets psCur
raise Error $ "Expected function equation for " ++ pretty name ++
" after type signature"
let rng = Range pos pos
return [FunDef sigrange name (TypeSig () typ)
(FunEq rng name [] (Plain rng (EError rng)) :| [])]
,do pFunEq FirstLine >>= \case
clause1@(FunEq _ name _ _) : clauses1 -> noFail $ do
clauses <- concat <$> famany (pFunEq (Continue name))
let clauses' = clause1 :| clauses1 ++ clauses
let rng = foldMapne extOf clauses'
return [FunDef rng name (TypeSigExt () NoTypeSig) clauses']
[] -> faempty]
-- | Given the name from the type signature or a previous clause, if any.
pFunEq :: FunEqContext -> FParser [PFunEq]
pFunEq fectx = do
noFail skipWhiteComment
faguardM isAtBlockLeft
pushLocatedContext "function equation" $ do
pos1 <- gets psCur
name <- pIdentifier0 AtLeft Lowercase WCAssume
-- We want to do various checks with what came before, and there are
-- multiple branches where we decide to continue parsing this equation. To
-- avoid code duplication or an early exit monad, we use a boolean here.
success <- case fectx of
FirstLine -> return True
WithTypeSig checkName
| name == checkName -> return True
| otherwise -> noFail $ do
raise Error $ "Name of function clause does not correspond with type signature: " ++
pretty checkName
return False
Continue checkName -> do
faguard (name == checkName) -- this can still backtrack out of pFunEq
return True
noFail $ if success
then do
pats <- famany (pPattern 11)
rhs <- pRHS "="
pos2 <- gets psCur
return [FunEq (Range pos1 pos2) name pats rhs]
else return []
-- | Pass "=" for function definitions and "->" for case clauses.
pRHS :: String -> IParser PRHS
pRHS sepsym = do
-- TODO: parse guards
inlineWhite
pKeySym0 sepsym <|>> raise Error ("Expected " ++ show sepsym)
expr <- pExpr <|>> expectedExpression
return (Plain (extOf expr) expr)
pPattern :: Int -> FParser PPattern
pPattern d = inlineWhite >> pPattern0 d
pPattern0 :: Int -> FParser PPattern
pPattern0 d = do
pos1 <- gets psCur
p0 <- pPatExprAtom0 (max 10 d)
climbRight pPattern (pInfixOp Uppercase) POp d pos1 p0 Nothing
pExpr :: FParser PExpr
pExpr = do
inlineWhite
-- basics: lit, list, var, con, tup
-- expression atom: application of basics
-- expression parser: op
-- around: let, case, if
pushLocatedContext "expression" $ do
faasum' [pELet0
,pECase0
,pEIf0
,pExprOpExpr0 0]
pPatExprAtom0 :: Int -> FParser PPattern
pPatExprAtom0 d =
faasum' [pPatWildcard0
,pPatVarOrAs0
,pPatCon0
,pPatList0
,pPatParens0]
where
pPatWildcard0 = PWildcard <$> ranged' (pKeySym0 "_")
pPatVarOrAs0 = do
(varrng@(Range pos1 _), var) <- ranged $ pIdentifier0 InBlock Lowercase WCBacktrack
facatch (return (PVar varrng var)) $ do
inlineWhite
pKeySym0 "@"
noFail $ do
pos <- gets psCur
facatch (do raise Error "Expected pattern after '@'"
return (PWildcard (Range pos pos))) $ do
pat <- pPattern 11
pos2 <- gets psCur
return (PAs (Range pos1 pos2) var pat)
pPatCon0 = do
(conrng@(Range pos1 _), con) <- ranged $ pIdentifier0 InBlock Uppercase WCBacktrack
noFail $ if d > 10
then return (PCon conrng con [])
else do args <- famany (pPattern 11)
pos2 <- gets psCur
return (PCon (Range pos1 pos2) con args)
pPatList0 = do
pos1 <- gets psCur
char '[' -- special syntax, no need for pKeySym
noFail $ do
ps <- pPattern 0 `sepBy` (inlineWhite >> char ',')
inlineWhite
char ']' <|>> raise Error "Expected ']'"
pos2 <- gets psCur
return (PList (Range pos1 pos2) ps)
pPatParens0 = do
pos1 <- gets psCur
char '('
inlineWhite
faasum'
[do char ')'
pos2 <- gets psCur
return (PTup (Range pos1 pos2) [])
,do p <- pPattern0 0
inlineWhite
faasum'
[do char ')'
return p
,do char ','
ps <- pPattern 0 `sepBy1` (inlineWhite >> char ',')
inlineWhite
char ')' <|>> raise Error "Expected ')'"
pos2 <- gets psCur
return (PTup (Range pos1 pos2) (p : ps))]]
pELet0 :: FParser PExpr
pELet0 = do
pos1 <- gets psCur
pKeyword "let"
noFail $ do
inlineWhite
defss <- startLayoutBlock $ do
-- The first occurrence is also going to skip whitespace in front,
-- which is redundant -- but not harmful.
famany $ do
noFail skipWhiteComment
-- Note: now not necessarily in the indented block. Which is
-- precisely what we need here. If we see "in", let the 'many'
-- choice fail so that the defs loop ends. But let it fail outside
-- this asum so that it is the many that picks it up, not this
-- asum.
res <- faasum' [Nothing <$ pKeyword "in" -- note: will be dropped due to the empty backtrack
,Just <$> pFunDef0]
case res of
Nothing -> faempty
Just defs -> return defs
let defs = concat defss
inlineWhite
facatch (do pos2 <- gets psCur
raise Error "Expected 'in' after 'let'"
return (ELet (Range pos1 pos2) defs (EError (Range pos2 pos2)))) $ do
pKeyword "in"
noFail $ do
inlineWhite
body <- pExpr <|>> expectedExpression
pos2 <- gets psCur
return (ELet (Range pos1 pos2) defs body)
pECase0 :: FParser PExpr
pECase0 = do
pos1 <- gets psCur
pKeyword "case"
noFail $ do
e <- pExpr <|>> expectedExpression
inlineWhite
facatch (do pos2 <- gets psCur
raise Error "Expected 'of' after 'case'"
return (ECase (Range pos1 pos2) e [])) $ do
pKeyword "of"
noFail $ do
inlineWhite
startLayoutBlock $ do
-- The first clause is going to skip whitespace, but that's harmless
-- (though redundant).
let pClause = do
noFail $ skipWhiteComment
whenM (noFail $ not <$> isInsideBlock) (() <$ faempty)
pat <- pPattern0 0
noFail $ do
rhs <- pRHS "->"
return (pat, rhs)
clauses <- famany pClause
pos2 <- gets psCur
return (ECase (Range pos1 pos2) e clauses)
pEIf0 :: FParser PExpr
pEIf0 = do
pos1 <- gets psCur
pKeyword "if"
noFail $ do
e1 <- pExpr <|>> expectedExpression
inlineWhite
facatch (do pos2 <- gets psCur
raise Error "Expected 'then' after 'if'"
let rng = Range pos2 pos2
return (EIf (Range pos1 pos2) e1 (EError rng) (EError rng))) $ do
pKeyword "then"
noFail $ do
e2 <- pExpr <|>> expectedExpression
inlineWhite
facatch (do pos2 <- gets psCur
raise Error "Expected else after 'then'"
let rng = Range pos2 pos2
return (EIf (Range pos1 pos2) e1 e2 (EError rng))) $ do
pKeyword "else"
noFail $ do
e3 <- pExpr <|>> expectedExpression
pos2 <- gets psCur
return (EIf (Range pos1 pos2) e1 e2 e3)
pExprOpExpr :: Int -> FParser PExpr
pExprOpExpr d = inlineWhite >> pExprOpExpr0 d
pExprOpExpr0 :: Int -> FParser PExpr
pExprOpExpr0 d = do
pos1 <- gets psCur
e0 <- pEApp0
climbRight pExprOpExpr (snd <$> pInfixOp Don'tCare) EOp d pos1 e0 Nothing
climbRight
:: (Int -> FParser e) -- ^ Parse an expression at the given precedence level
-> FParser ParsedOperator -- ^ Parse an operator
-> (Range -> e -> Operator -> e -> e) -- ^ Build an operator application experssion
-> Int -- ^ Ambient precedence level: minimum precedence of top-level operator in result
-> Pos -- ^ Start of lhs
-> e -- ^ lhs: Initial non-operator expression already parsed
-> Maybe ParsedOperator -- ^ Top-level operator in lhs (initialise with Nothing)
-> FParser e
climbRight pExpr' pOper makeOp d lhspos lhs mlhsop =
facatch (return lhs) $ do
paop@(PaOp op d2 a2 _) <- pOper
faguard (d2 >= d) -- respect global minimum precedence
case mlhsop of -- check operator compatibility
Just (PaOp _ d1 a1 _) ->
faguard (d1 > d2 || (d1 == d2 && a1 == a2 && a1 /= AssocNone))
Nothing ->
return ()
let oprhsd = case a2 of AssocRight -> d2 ; _ -> d2 + 1
rhs <- pExpr' oprhsd
pos2 <- gets psCur
climbRight pExpr' pOper makeOp d lhspos (makeOp (Range lhspos pos2) lhs op rhs) (Just paop)
pEApp0 :: FParser PExpr
pEApp0 = do
pos1 <- gets psCur
e1 <- pEAtom0
es <- noFail $ famany (inlineWhite >> pEAtom0)
pos2 <- gets psCur
case es of
[] -> return e1
_ -> return (EApp (Range pos1 pos2) e1 es)
pEAtom0 :: FParser PExpr
pEAtom0 = faasum'
[uncurry ELit <$> ranged pLiteral0
,pEList0
,pEVarOrCon0
,pEParens0]
pLiteral0 :: FParser Literal
pLiteral0 = faasum'
[do as <- toList <$> fasome (satisfy isDigit)
let a = read as :: Integer
facatch (return (LInt a)) $ do
char '.'
bs <- toList <$> fasome (satisfy isDigit)
let b = read bs :: Integer
cs <- noFail $ faoptional $ do
char 'e'
cs <- toList <$> fasome (satisfy isDigit)
return cs
let c = maybe 0 read cs :: Integer
return (LFloat ((fromIntegral a + fromIntegral b / 10 ^ length bs) * 10 ^ c))
,do char '\''
facatch (raise Error "Unclosed character literal" >> return (LChar '?')) $ do
cs <- noFail $ famany pStringChar
char '\''
noFail $ do
c <- case cs of
[c] -> return c
_ -> raise Error "Character literal must contain one character" >> return '?'
return (LChar c)
,do char '"'
noFail $ do
s <- famany pStringChar
char '"' <|>> raise Error "Unclosed string literal"
return (LString s)]
pStringChar :: FParser Char
pStringChar = faasum'
[do char '\\'
char 'x'
let hexdig = do
c <- satisfy $ \c' ->
let c = toLower c'
in 'a' <= c && c <= 'f' || '0' <= c && c <= '9'
return $ if 'a' <= c then 10 + ord c - ord 'a'
else ord c - ord '0'
digs <- toList <$> fasome hexdig
return (chr (sum (zipWith (*) (reverse digs) (iterate (*16) 1))))
,do char '\\'
satisfy (const True) >>= \case
'n' -> return '\n'
'r' -> return '\r'
't' -> return '\t'
'a' -> return '\a'
'b' -> return '\b'
'\'' -> return '\''
'\"' -> return '\"'
'0' -> return '\0'
c -> do raise Error $ "Invalid escape sequence: \\" ++ [c]
return '?'
,do satisfy (\c -> c `notElem` "\n\r\\\'")]
pEList0 :: FParser PExpr
pEList0 = do
pos1 <- gets psCur
char '[' -- special syntax, no need for pKeySym
noFail $ do
es <- sepBy pExpr (inlineWhite >> char ',')
inlineWhite
char ']' <|>> raise Error "Expected closing ']'"
pos2 <- gets psCur
return (EList (Range pos1 pos2) es)
pEVarOrCon0 :: FParser PExpr
pEVarOrCon0 =
ranged (pIdentifier0 InBlock Don'tCare ()) >>= \case
(rng, (Lowercase, name)) -> return (EVar rng name)
(rng, (Uppercase, name)) -> return (ECon rng name)
pEParens0 :: FParser PExpr
pEParens0 = do
char '('
noFail $ do
e <- pExpr <|>> expectedExpression
inlineWhite
char ')' <|>> raise Error "Expected closing ')'"
return e
expectedExpression :: IParser PExpr
expectedExpression = do
pos <- gets psCur
raise Error "Expected expression"
return (EError (Range pos pos))
data Associativity = AssocLeft | AssocRight | AssocNone
deriving (Show, Eq)
data ParsedOperator = PaOp Operator Int Associativity Range
deriving (Show)
pInfixOp :: Case care -> FParser (WithCaseOutput care ParsedOperator)
pInfixOp cs = do
inlineWhite
case cs of
Lowercase -> pLowerInfixOp0
Uppercase -> pUpperInfixOp0
Don'tCare -> faasum' [(Lowercase,) <$> pLowerInfixOp0
,(Uppercase,) <$> pUpperInfixOp0]
pLowerInfixOp0 :: FParser ParsedOperator
pLowerInfixOp0 =
faasum' [PaOp OEqu 4 AssocNone <$> ranged' (pKeySym0 "==")
,PaOp OAdd 6 AssocLeft <$> ranged' (pKeySym0 "+")
,PaOp OSub 6 AssocLeft <$> ranged' (pKeySym0 "-")
,PaOp OMul 7 AssocLeft <$> ranged' (pKeySym0 "*")
,PaOp ODiv 7 AssocLeft <$> ranged' (pKeySym0 "/")
,PaOp OMod 7 AssocLeft <$> ranged' (pKeySym0 "%")
,PaOp OPow 8 AssocRight <$> ranged' (pKeySym0 "^")
]
pUpperInfixOp0 :: FParser ParsedOperator
pUpperInfixOp0 =
faasum' [PaOp OCons 5 AssocRight <$> ranged' (pKeySym0 ":")]
pStandaloneTypesig0 :: FParser (Name, PType)
pStandaloneTypesig0 = do
name <- pIdentifier0 AtLeft Lowercase WCBacktrack
inlineWhite
pKeySym0 "::"
ty <- pType <|>> (raise Error "Expected type" >> faempty)
return (name, ty)
pType :: FParser PType
pType = do
pos1 <- gets psCur
ty1 <- pTypeApp
facatch (return ty1) $ do
inlineWhite
pKeySym0 "->"
noFail $ do
pos2 <- gets psCur
ty2 <- pType <|>> (raise Error "Expected type" >> return (TTup (Range pos2 pos2) []))
return (TFun (Range pos1 pos2) ty1 ty2)
pTypeApp :: FParser PType
pTypeApp = do
pos1 <- gets psCur
ts <- fasome pTypeAtom
pos2 <- gets psCur
case ts of
t :| [] -> return t
t :| ts' -> return (TApp (Range pos1 pos2) t ts')
pTypeAtom :: FParser PType
pTypeAtom = faasum' [pTypeParens, pTypeList, pTypeName]
where
pTypeParens = do
inlineWhite
pos1 <- gets psCur
char '('
faasum'
[do inlineWhite
char ')'
pos2 <- gets psCur
return (TTup (Range pos1 pos2) [])
,do ty1 <- pType
noFail $ do
ty2s <- famany $ do
inlineWhite
char ','
pos <- gets psCur
noFail $ pType <|>> (raise Error "Expected type" >> return (TTup (Range pos pos) []))
inlineWhite
char ')' <|>> raise Error "Expected closing ')'"
pos2 <- gets psCur
case ty2s of
[] -> return ty1
_ -> return (TTup (Range pos1 pos2) (ty1 : ty2s))]
pTypeList = do
inlineWhite
pos1 <- gets psCur
char '['
ty <- pType
noFail $ char ']' <|>> raise Error "Expected closing ']'"
pos2 <- gets psCur
return (TList (Range pos1 pos2) ty)
pTypeName = do
inlineWhite
(rng, (cs, name)) <- ranged $ pIdentifier0 InBlock Don'tCare ()
case cs of
Uppercase -> return (TCon rng name)
Lowercase -> return (TVar rng name)
-- | Parse the given name-like keyword, ensuring that it is the entire word.
pKeyword :: String -> FParser ()
pKeyword s = do
string s
-- traceM $ "pKeyword: parsed " ++ show s
notFollowedBy (() <$ satisfy isNameContChar)
-- | Parse the given symbol-like keyword, ensuring that it is the entire symbol.
pKeySym0 :: String -> FParser ()
pKeySym0 s = do
string s
notFollowedBy (() <$ satisfy isSymbolChar)
ranged :: Parser fail a -> Parser fail (Range, a)
ranged p = do
pos1 <- gets psCur
res <- p
pos2 <- gets psCur
return (Range pos1 pos2, res)
ranged' :: Parser fail () -> Parser fail (Range)
ranged' p = fst <$> ranged p
data Case care where
Uppercase :: Case 'True
Lowercase :: Case 'True
Don'tCare :: Case 'False
deriving instance Show (Case care)
type family WithCaseOutput care a where
WithCaseOutput 'True a = a
WithCaseOutput 'False a = (Case 'True, a)
data WrongCaseBacktrack
= WCBacktrack -- ^ If a word was found but it had the wrong case, fail and backtrack.
| WCAssume -- ^ Be certain that this case is expected here, and assume incorrect
-- case is a typo.
deriving (Show)
-- | Consumes an identifier (word or parenthesised operator) at the current
-- position. The `var` production in Haskell2010.
-- var -> varid | "(" varsym ")"
pIdentifier0 :: BlockPos -> Case care -> If care WrongCaseBacktrack () -> FParser (WithCaseOutput care Name)
pIdentifier0 bpos cs wrongcase =
pAlphaName0 bpos cs wrongcase <|>> pParens0 (pSymbol0 bpos cs)
where
-- | Parser between parens, with the opening paren at the current position.
pParens0 :: FParser a -> FParser a
pParens0 p = do
char '('
inlineWhite
res <- p
inlineWhite
char ')'
return res
-- | Consumes a word-like name at the current position with the given case. The
-- `varid` production in Haskell2010 for 'Lowercase', `conid' for 'Uppercase'.
--
-- > varid -> (small {small | large | digit | "'"}) without reservedid
pAlphaName0 :: BlockPos -> Case care -> If care WrongCaseBacktrack () -> FParser (WithCaseOutput care Name)
pAlphaName0 bpos cs wrongcase = do
startPos <- gets psCur
(_, s) <- readToken
bpos
(\atfst mc -> case (atfst, mc) of
(True , Just c) | isNameHeadChar c -> Just (Right False)
(True , _ ) -> Nothing
(False, Just c) | isNameContChar c -> Just (Right False)
(False, _ ) -> Just (Left ()))
True
faguard (s `notElem` ["case", "class", "data", "default", "deriving", "do", "else"
,"foreign", "if", "import", "in", "infix", "infixl"
,"infixr", "instance", "let", "module", "newtype", "of"
,"then", "type", "where", "_"])
(name, adjoin) <- case cs of
Uppercase
| isLower (head s) -> case wrongcase of
WCBacktrack -> faempty
WCAssume -> noFail $ do
raiseAt startPos Error "Unexpected uppercase word at this position, assuming typo"
return (toUpper (head s) : tail s, id)
| otherwise -> return (s, id)
Lowercase
| isUpper (head s) -> case wrongcase of
WCBacktrack -> faempty
WCAssume -> noFail $ do
raiseAt startPos Error "Unexpected lowercase word at this position, assuming typo"
return (toLower (head s) : tail s, id)
| otherwise -> return (s, id)
Don'tCare
| isLower (head s) -> return (s, (Lowercase,))
| otherwise -> return (s, (Uppercase,))
return (adjoin (Name name))
isNameHeadChar :: Char -> Bool
isNameHeadChar c = isLetter c || c == '_'
isNameContChar :: Char -> Bool
isNameContChar c = isNameHeadChar c || isDigit c || c == '\''
-- | Consumes a symbol at the current position. The `varsym` production in
-- Haskell2010 for 'Lowercase', `consym` otherwise, and either if 'Don'tCare'.
--
-- > varsym -> ((symbol without ":") {symbol}) without (reservedop | dashes)
-- > consym -> (":" {symbol}) without reservedop
-- > symbol -> ascSymbol | uniSymbol without (special | "_" | "\"" | "'")
-- > ascSymbol -> ```!#$%&⋆+./<=>?@^|-~:```
-- > uniSymbol -> unicode symbol or punctuation
-- > dashes -> "--" {"-"}
-- > special -> ```(),;[]`{}```
-- > reservedop -> ".." | ":" | "::" | "=" | "\" | "|" | "<-" | "->" | "@" | "~" | "=>"
pSymbol0 :: BlockPos -> Case care -> FParser (WithCaseOutput care Name)
pSymbol0 bpos cs = do
case bpos of
AtLeft -> whenM (noFail $ not <$> isAtBlockLeft) (() <$ faempty)
InBlock -> whenM (noFail $ not <$> isInsideBlock) faempty
(c1, adjoin) <-
case cs of Lowercase -> (,id) <$> satisfy (\c -> isSymbolChar c && c /= ':')
Uppercase -> (,id) <$> satisfy (== ':')
Don'tCare -> do c1 <- satisfy (\c -> isSymbolChar c)
return (c1, if c1 == ':' then (Uppercase,) else (Lowercase,))
crest <- noFail $ famany (satisfy isSymbolChar)
let name = c1 : crest
faguard (name `notElem` ["..", ":", "::", "=", "\\", "|", "<-", "->", "@", "~", "=>"])
faguard (take 2 name /= "--")
return (adjoin (Name name))
isSymbolChar :: Char -> Bool
isSymbolChar c = (isAscSymbol || isUniSymbol) && not isSpecialExt
where
isSpecialExt = c `elem` "(),;[]`{}_\"'"
isAscSymbol = c `elem` "!#$%&⋆+./<=>?@^|-~:"
isUniSymbol = ord c > 127 && (isSymbol c || isPunctuation c)
sepBy1 :: FParser a -> FParser sep -> FParser [a]
sepBy1 p psep = do
x1 <- p
(psep >> (x1 :) <$> sepBy1 p psep) <|>> pure [x1]
sepBy :: FParser a -> FParser sep -> IParser [a]
sepBy p psep = sepBy1 p psep <|>> pure []
-- | Start a new layout block at the current position. The old layout block is
-- restored after completion of this subparser.
startLayoutBlock :: IParser a -> IParser a
startLayoutBlock p = do
ps0 <- get
put (ps0 { psBlk = psCur ps0 })
res <- p
modify (\ps -> ps { psBlk = psBlk ps0 })
return res
data Fatality fatal where
Error :: Fatality 'False
-- Fatal :: Fatality 'True
deriving instance Show (Fatality fatal)
type family FatalCtx fatal a where
FatalCtx 'False a = a ~ ()
FatalCtx 'True a = ()
raise_ :: KnownFallible fail => Fatality fatal -> String -> Parser fail ()
raise_ Error = raise Error
-- raise_ Fatal = raise Fatal
raise :: (KnownFallible fail, FatalCtx fatal a) => Fatality fatal -> String -> Parser fail a
raise fat msg = gets psCur >>= \pos -> raiseAt pos fat msg
-- | Raise an error with the given fatality and description.
raiseAt :: (KnownFallible fail, FatalCtx fatal a) => Pos -> Fatality fatal -> String -> Parser fail a
raiseAt pos fat msg = do
Context { ctxFile = fp , ctxStack = stk, ctxLines = srcLines } <- ask
let err = Diagnostic SError fp (Range pos pos) stk (srcLines !! posLine pos) msg
case fat of
Error -> dictate (pure err)
-- Fatal -> confess (pure err)
describeLocation :: IParser String
describeLocation = do
fp <- asks ctxFile
cur <- gets psCur
return $ fp ++ ":" ++ show (posLine cur + 1) ++ ":" ++ show (posCol cur + 1)
-- | Registers a scope description on the stack for error reporting.
pushContext :: String -> Parser fail a -> Parser fail a
pushContext descr = local (\c -> c { ctxStack = descr : ctxStack c })
-- | Registers a scope description on the stack for error reporting, suffixed
-- with the current parsing location.
pushLocatedContext :: String -> Parser fail a -> Parser fail a
pushLocatedContext descr p = do
loc <- noFail describeLocation
pushContext (descr ++ " at " ++ loc) p
data BlockPos = AtLeft | InBlock
deriving (Show)
-- | Consumes a token at the current position, asserting that we are
-- in the position indicated by the 'BlockPos' argument. The token is defined
-- by a pure stateful parser. If encountering a newline or EOF, the parser is
-- run on this character ('Nothing' for EOF); if this produces a result, the
-- result is returned; otherwise, the parser fails. The newline is not consumed.
readToken :: BlockPos -> (s -> Maybe Char -> Maybe (Either r s)) -> s -> FParser (r, String)
readToken bpos f s0 = do
case bpos of
AtLeft -> whenM (noFail $ not <$> isAtBlockLeft) (() <$ faempty)
InBlock -> whenM (noFail $ not <$> isInsideBlock) faempty
let loop :: (s -> Maybe Char -> Maybe (Either r s)) -> s -> FParser (r, String)
loop f' st = do
ps <- get
case psRest ps of
[] | Just (Left res) <- f' st Nothing -> return (res, "")
| otherwise -> faempty
'\n' : _ | Just (Left res) <- f' st (Just '\n') -> return (res, "")
c : cs -> case f' st (Just c) of
Nothing -> faempty
Just (Left res) -> return (res, "")
Just (Right st') -> do
let Pos line col = psCur ps
put (ps { psCur = Pos line (col + 1), psRest = cs })
fmap (c :) <$> loop f' st'
loop f s0
-- | Consumes all whitespace and comments (including newlines), but only if
-- this then leaves the parser inside the current block. If not, succeeds and
-- consumes nothing.
inlineWhite :: Parser fail ()
inlineWhite = do
ps <- get
noFail skipWhiteComment
whenM (noFail $ not <$> isInsideBlock) $ put ps
-- | Consumes all whitespace and comments (including newlines). Note: this may
-- end outside the current block.
skipWhiteComment :: IParser ()
skipWhiteComment = do
inlineSpaces
_ <- famany (blockComment >> noFail inlineSpaces)
optional_ lineComment0
optional_ (consumeNewline >> noFail skipWhiteComment)
where
-- | Consumes some inline whitespace. Stops before newlines.
inlineSpaces :: IParser ()
inlineSpaces = readWhileInline isSpace
-- | Consumes an delimited comment including both end markers. Note: this may
-- end outside the current block.
blockComment :: FParser ()
blockComment = do
string "{-" -- no need for pKeySym here
let loop = do
faasum [string "-}"
,eof >> raise Error "Unfinished {- -} comment at end of file"
,blockComment >> noFail loop
,consumeNewline >> noFail loop]
(readWhileInline (`notElem` "{-")) -- "-}" also starts with '-'
noFail loop
-- | Consumes a line comment marker and the rest of the line, excluding
-- newline.
lineComment0 :: FParser ()
lineComment0 = do
-- '--!' is an operator, so we need to parse a whole symbol here.
pKeySym0 "--"
noFail $ readWhileInline (const True)
-- | Raises an error if we're not currently at EOF.
assertEOF :: Fatality fatal -> IParser ()
assertEOF fat = gets psRest >>= \case
[] -> return ()
_ -> raise_ fat "Unexpected stuff"
-- | Returns whether the current position is _within_ the current block, for
-- soft-wrapping content. This means that col > blkCol.
isInsideBlock :: IParser Bool
isInsideBlock = do
PS { psCur = cur, psBlk = blk } <- get
return $ posLine cur >= posLine blk && posCol cur > posCol blk
-- | Returns whether the current position is at the left border of the block;
-- this is for list content such as function definitions or let bindings. This
-- means that col == blkCol.
isAtBlockLeft :: IParser Bool
isAtBlockLeft = do
PS { psCur = cur, psBlk = blk } <- get
return $ posLine cur >= posLine blk && posCol cur == posCol blk
-- | Consumes characters while the predicate holds or until (and excluding)
-- a newline, whichever comes first.
readWhileInline :: (Char -> Bool) -> IParser ()
readWhileInline p = do
(taken, rest) <- span (\c -> p c && c /= '\n') <$> gets psRest
modify (\ps -> ps { psCur = let Pos line col = psCur ps
in Pos line (col + length taken)
, psRest = rest })
-- | Consumes exactly one newline at the current position.
consumeNewline :: FParser ()
consumeNewline = gets psRest >>= \case
'\n' : rest -> modify (\ps -> ps { psCur = Pos (posLine (psCur ps) + 1) 0
, psRest = rest })
_ -> faempty
-- | Consumes exactly one character, unequal to newline, at the current position.
satisfy :: (Char -> Bool) -> FParser Char
satisfy p = do
-- traceM "entering satisfy"
r <- gets psRest
-- traceM "got rest"
r `seq` return ()
-- traceM "seqd rest"
-- traceM ("rest is " ++ show r)
case r of
c : rest | c /= '\n', p c -> do
modify (\ps -> let Pos line col = psCur ps
in ps { psCur = Pos line (col + 1)
, psRest = rest })
return c
_ -> faempty
-- | Consumes exactly this character at the current position. Must not be a
-- newline.
char :: Char -> FParser ()
char c = string [c]
-- | Consumes exactly this string at the current position. The string must not
-- contain a newline.
string :: String -> FParser ()
string s | any (== '\n') s = error "Newline in 'string' argument"
string s = do
ps <- get
let Pos line col = psCur ps
if take (length s) (psRest ps) == s
then put (ps { psCur = Pos line (col + length s)
, psRest = drop (length s) (psRest ps) })
else faempty
-- lookAhead :: FParser a -> FParser a
-- lookAhead p = do
-- ps <- get
-- success <- (Just <$> p) <|>> pure Nothing
-- put ps -- restore state, as if nothing happened
-- case success of
-- Nothing -> faempty
-- Just x -> return x
notFollowedBy :: FParser () -> FParser ()
notFollowedBy p = do
ps <- get
success <- (False <$ p) <|>> pure True
put ps -- restore state, as if nothing happened
when (not success) faempty
-- | Only succeeds at EOF.
eof :: FParser ()
eof = gets psRest >>= \case [] -> return ()
_ -> faempty
whenM :: (Monad m, Monoid a) => m Bool -> m a -> m a
whenM mb mx = mb >>= \b -> if b then mx else return mempty
optional_ :: FAlternative f => f 'Fallible a -> f 'Infallible ()
optional_ a = (() <$ a) <|>> pure ()
|