-
Notifications
You must be signed in to change notification settings - Fork 1
/
Parse.hs
1069 lines (971 loc) · 36.1 KB
/
Parse.hs
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
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE TypeFamilies #-}
module Inferno.Parse
( Comment (..),
OpsTable,
TopLevelDefn (..),
QQDefinition (..),
InfernoParsingError (..),
topLevel,
expr,
parseExpr,
parseType,
parseTCScheme,
modulesParser,
prettyError,
rws,
)
where
import Control.Applicative (Alternative ((<|>)))
import Control.Monad (foldM, void, when)
import Control.Monad.Combinators.Expr
( Operator (InfixL, InfixN, InfixR, Prefix),
makeExprParser,
)
import Control.Monad.Reader (ReaderT (..), ask, withReaderT)
import Control.Monad.Writer (WriterT (..), tell)
import Data.Bifunctor (Bifunctor (first))
import Data.Char (isAlphaNum, isSpace)
import Data.Data (Data)
import Data.Either (partitionEithers)
import Data.Functor (($>))
import qualified Data.IntMap as IntMap
import qualified Data.List as List
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NEList
import qualified Data.Map as Map
import Data.Monoid (Endo (..))
import qualified Data.Set as Set
import Data.Text (Text, pack, singleton, unpack)
import qualified Data.Text as Text
import Inferno.Infer.Env (closeOver)
import Inferno.Parse.Error (prettyError)
import Inferno.Types.Syntax
( Comment (..),
CustomType (),
Expr (..),
ExtIdent (ExtIdent),
Fixity (..),
Ident (Ident),
ImplExpl (Expl, Impl),
Import (..),
InfixFixity (..),
Lit (..),
ModuleName (..),
Pat (..),
RestOfRecord (..),
Scoped (..),
SigVar (..),
TList (..),
fromEitherList,
fromScoped,
rws,
tListFromList,
)
import Inferno.Types.Type
( BaseType (..),
ImplType (..),
InfernoType (..),
TCScheme (..),
TV (TV),
TypeClass (TypeClass),
)
import Inferno.Utils.Prettyprinter (renderPretty)
import Text.Megaparsec
( MonadParsec
( eof,
hidden,
label,
notFollowedBy,
takeWhile1P,
takeWhileP,
try
),
ParseError,
ParseErrorBundle (ParseErrorBundle),
Parsec,
ShowErrorComponent (..),
SourcePos (..),
anySingle,
attachSourcePos,
between,
choice,
customFailure,
errorOffset,
getSourcePos,
many,
manyTill,
optional,
runParser,
satisfy,
sepBy1,
some,
unPos,
(<?>),
)
import Text.Megaparsec.Char
( alphaNumChar,
char,
char',
letterChar,
spaceChar,
string,
)
import qualified Text.Megaparsec.Char.Lexer as Lexer
-- | Converts a curried function to a function on a triple.
uncurry3 :: (a -> b -> c -> d) -> ((a, b, c) -> d)
uncurry3 f ~(a, b, c) = f a b c
choiceOf :: (t -> Parser a) -> [t] -> Parser a
choiceOf _ [] = fail "none of the operators matched"
choiceOf p [x] = p x
choiceOf p (x : xs) = p x <|> choiceOf p xs
tryMany :: (t -> Parser a) -> [t] -> Parser a
tryMany _ [] = fail "none of the operators matched"
tryMany p [x] = p x
tryMany p (x : xs) = try (p x) <|> tryMany p xs
type Comments = Endo [Comment SourcePos]
output :: Comment SourcePos -> SomeParser r ()
output x = tell $ Endo ([x] <>)
type Parser = ReaderT (OpsTable, Map.Map ModuleName OpsTable, [CustomType]) (WriterT Comments (Parsec InfernoParsingError Text))
type SomeParser r = ReaderT r (WriterT Comments (Parsec InfernoParsingError Text))
skipLineComment :: SomeParser r ()
skipLineComment = do
startPos <- getSourcePos
comment <- string "//" *> takeWhileP (Just "character") (/= '\n')
endPos <- getSourcePos
output $ LineComment startPos (Text.strip comment) endPos
skipBlockComment :: SomeParser r ()
skipBlockComment = do
startPos@(SourcePos _ _ col) <- getSourcePos
comment <- string "/*" >> manyTill anySingle (string "*/")
endPos <- getSourcePos
output $
BlockComment
startPos
(Text.replace (pack $ '\n' : List.replicate (unPos col - 1) ' ') "\n" $ Text.pack comment)
endPos
sc :: SomeParser r ()
sc = Lexer.space (void spaceChar) skipLineComment skipBlockComment
lexeme :: SomeParser r a -> SomeParser r a
lexeme = Lexer.lexeme sc
symbol :: Text -> SomeParser r Text
symbol = Lexer.symbol sc
-- | 'parens' parses something between parenthesis.
parens :: SomeParser r a -> SomeParser r a
parens p = hidden $ between (symbol "(") (symbol ")") p
-- | 'rword' for parsing reserved words.
rword :: Text -> SomeParser r ()
rword w = string w *> notFollowedBy alphaNumCharOrSeparator *> sc
isAlphaNumOrSeparator :: Char -> Bool
isAlphaNumOrSeparator c = isAlphaNum c || c == '_'
{-# INLINE isAlphaNumOrSeparator #-}
alphaNumCharOrSeparator :: SomeParser r Char
alphaNumCharOrSeparator = satisfy isAlphaNumOrSeparator <?> "alphanumeric character or '_'"
{-# INLINE alphaNumCharOrSeparator #-}
variable :: Parser Text
variable = do
(opsTable, _, _) <- ask
try (p >>= check opsTable)
where
p = pack <$> (((:) <$> letterChar <*> hidden (many alphaNumCharOrSeparator)) <?> "a variable")
check oT x =
if x `elem` rws ++ map (\(_, _, i) -> i) (concat oT)
then fail $ "Keyword " <> show x <> " cannot be a variable/function name"
else return x
mIdent :: Parser (SourcePos, Maybe Ident)
mIdent = lexeme $ do
startPos <- getSourcePos
(startPos,) . Just . Ident <$> variable
<|> (char '_' *> takeWhileP Nothing isAlphaNumOrSeparator $> (startPos, Nothing))
<?> "a wildcard parameter '_'"
mExtIdent :: Parser (SourcePos, Maybe ExtIdent)
mExtIdent = lexeme $ do
startPos <- getSourcePos
(startPos,) . Just . ExtIdent . Right <$> variable
<|> (char '_' *> takeWhileP Nothing isAlphaNumOrSeparator $> (startPos, Nothing))
<?> "a wildcard parameter '_'"
implicitVariable :: Parser Text
implicitVariable = hidden $ char '?' *> (Text.cons <$> letterChar <*> takeWhileP Nothing isAlphaNumOrSeparator)
enumConstructor :: SomeParser r Ident
enumConstructor =
Ident
<$> lexeme (char '#' *> takeWhile1P Nothing isAlphaNumOrSeparator)
<?> "an enum constructor\nfor example: #true, #false"
-- | 'signedInteger' parses an integer with an optional sign (with no space)
signedInteger :: Num a => Parser a
signedInteger = Lexer.signed (takeWhileP Nothing isHSpace $> ()) Lexer.decimal
-- | 'signedInteger' parses a float/double with an optional sign (with no space)
signedFloat :: Parser Double
signedFloat = Lexer.signed (takeWhileP Nothing isHSpace $> ()) Lexer.float
enumE :: (SourcePos -> () -> Scoped ModuleName -> Ident -> f) -> Parser f
enumE f = do
startPos <- getSourcePos
lexeme $
try (f startPos () <$> (Scope . ModuleName <$> variable) <*> (char '.' *> enumConstructor))
<|> f startPos () LocalScope <$> enumConstructor
implVarE :: Parser (Expr () SourcePos)
implVarE = do
startPos <- getSourcePos
lexeme $ Var startPos () LocalScope . Impl . ExtIdent . Right <$> implicitVariable
-- | 'intE' and 'doubleE' parse unsigned numbers
intE, doubleE :: Parser (Expr () SourcePos)
intE = label "a number\nfor example: 42, 3.1415, (-6)" $ do
startPos <- getSourcePos
lexeme $ Lit startPos . LInt <$> Lexer.decimal
doubleE = label "a number\nfor example: 42, 3.1415, (-6)" $ do
startPos <- getSourcePos
lexeme $ Lit startPos . LDouble <$> Lexer.float
hexadecimal :: (SourcePos -> Lit -> f SourcePos) -> Parser (f SourcePos)
hexadecimal f = label "a hexadecimal number\nfor example: 0xE907, 0XE907" $ do
startPos <- getSourcePos
lexeme $ f startPos . LHex <$> (char '0' *> char' 'x' *> Lexer.hexadecimal)
signedIntE, signedDoubleE :: (SourcePos -> Lit -> f SourcePos) -> Parser (f SourcePos)
signedIntE f = label "a number\nfor example: 42, 3.1415, (-6)" $ do
startPos <- getSourcePos
lexeme $ f startPos . LInt <$> signedInteger
signedDoubleE f = label "a number\nfor example: 42, 3.1415, (-6)" $ do
startPos <- getSourcePos
lexeme $ f startPos . LDouble <$> signedFloat
noneE :: (SourcePos -> a) -> Parser a
noneE e = label "an optional\nfor example: Some x, None" $ do
startPos <- getSourcePos
lexeme (e startPos <$ hidden (string "None"))
someE :: (SourcePos -> t -> a) -> Parser t -> Parser a
someE f p = label "an optional\nfor example: Some x, None" $ do
startPos <- getSourcePos
lexeme (hidden $ string "Some")
f startPos <$> p
stringE :: (SourcePos -> Lit -> f SourcePos) -> Parser (f SourcePos)
stringE f = label "a string\nfor example: \"hello world\"" $ do
startPos <- getSourcePos
lexeme $ f startPos . LText . pack <$> (char '\"' *> manyTill charNoNewline (char '\"'))
where
charNoNewline = notFollowedBy (char '\n') *> Lexer.charLiteral
interpolatedStringE, arrayComprE :: Parser (Expr () SourcePos)
interpolatedStringE = label "an interpolated string\nfor example: `hello ${1 + 2}`" $
lexeme $ do
startPos@(SourcePos _ _ col) <- getSourcePos
es <- mkInterpolatedString <$> (char '`' *> go)
InterpolatedString startPos (fromEitherList $ fixSpacing (unPos col) es) <$> getSourcePos
where
go =
[] <$ char '`'
<|> try ((:) . Left . singleton <$> hidden (char '\\' *> char '\\') <*> go)
<|> try ((:) . Left . singleton <$> hidden (char '\\' *> char '`') <*> go)
<|> try ((:) . Left . singleton <$> hidden (char '\\' *> char '$') <*> go)
<|> (:) . Right
<$> hidden
( do
startPos <- getSourcePos
e <- char '$' *> char '{' *> sc *> expr <* char '}'
endPos <- getSourcePos
pure (startPos, e, endPos)
)
<*> go
<|> (:) . Left . singleton <$> Lexer.charLiteral <*> go
fixSpacing newlineSpaceLength =
map (first (Text.replace (pack $ '\n' : List.replicate (newlineSpaceLength - 1) ' ') "\n"))
arrayComprE = label "array builder\nfor example: [n * 2 + 1 | n <- range 0 10, if n % 2 == 0]" $
lexeme $ do
startPos <- getSourcePos
symbol "["
e <- expr
midPos <- getSourcePos
symbol "|"
(sels, cond) <- rhsE
endPos <- getSourcePos
char ']'
return $ ArrayComp startPos e midPos (NEList.fromList sels) cond endPos
where
selectE :: Parser (SourcePos, Ident, SourcePos, Expr () SourcePos)
selectE = do
startPos <- getSourcePos
var <- lexeme $ Ident <$> variable
arrPos <- getSourcePos
e <- symbol "<-" *> expr
return (startPos, var, arrPos, e)
rhsE :: Parser ([(SourcePos, Ident, SourcePos, Expr () SourcePos, Maybe SourcePos)], Maybe (SourcePos, Expr () SourcePos))
rhsE =
try
( do
(startPos, var, arrPos, e) <- selectE
pos <- getSourcePos <* symbol ","
(xs, mcond) <- try rhsE <|> (\c -> ([], Just c)) <$> condE
pure ((startPos, var, arrPos, e, Just pos) : xs, mcond)
)
<|> (\(startPos, var, arrPos, e) -> ([(startPos, var, arrPos, e, Nothing)], Nothing)) <$> selectE
condE :: Parser (SourcePos, Expr () SourcePos)
condE = do
ifPos <- getSourcePos
(ifPos,) <$> (rword "if" *> expr)
array :: Parser a -> Parser (SourcePos, [(a, Maybe SourcePos)], SourcePos)
array p = label "array\nfor example: [1,2,3,4,5]" $
lexeme $ do
startPos <- getSourcePos
symbol "["
args <- argsE
endPos <- getSourcePos
char ']'
return (startPos, args, endPos)
where
argsE =
try
( do
e <- p
commaPos <- getSourcePos
symbol ","
es <- argsE
return ((e, Just commaPos) : es)
)
<|> try
( do
e1 <- p
return [(e1, Nothing)]
)
<|> pure []
record :: Parser a -> Parser (SourcePos, [(Ident, a, Maybe SourcePos)], SourcePos)
record p = label "record\nfor example: {name = \"Zaphod\"; age = 391}" $
lexeme $ do
startPos <- getSourcePos
symbol "{"
args <- argsE
endPos <- getSourcePos
char '}'
return (startPos, args, endPos)
where
argsE =
try
( do
f <- lexeme $ Ident <$> variable
symbol "="
e <- p
commaPos <- getSourcePos
symbol ";"
es <- argsE
return ((f, e, Just commaPos) : es)
)
<|> try
( do
f <- lexeme $ Ident <$> variable
symbol "="
e1 <- p
return [(f, e1, Nothing)]
)
<|> pure []
mkInterpolatedString :: [Either Text e] -> [Either Text e]
mkInterpolatedString [] = []
mkInterpolatedString (Left x : Left y : xs) = mkInterpolatedString (Left (x <> y) : xs)
mkInterpolatedString (x : xs) = x : mkInterpolatedString xs
funE :: Parser (Expr () SourcePos)
funE = label "a function\nfor example: fun x y -> x + y" $ do
startPos <- getSourcePos
hidden $ rword "fun"
args <- some mExtIdent
arrPos <- getSourcePos
symbol "->" <?> "'->'"
Lam startPos (NEList.fromList args) arrPos <$> expr
renameModE :: Parser (Expr () SourcePos)
renameModE = label "a 'let module' expression\nfor example: let module A = Base in A.#true" $
do
hidden $ rword "let"
hidden $ rword "module"
newNmPos <- getSourcePos
newNm <- lexeme (ModuleName <$> variable <?> "module name")
symbol "=" <?> "'='"
oldNmPos <- getSourcePos
oldNm <- lexeme (ModuleName <$> variable <?> "name of an existing module")
inPos <- getSourcePos
(opsTable, modOpsTables, customTypes) <- ask
case Map.lookup oldNm modOpsTables of
Nothing -> customFailure $ ModuleNotFound oldNm
Just opsTableOldNm -> do
let opsTable' = IntMap.unionWith (<>) opsTable $ IntMap.map (\xs -> [(fix, Scope newNm, op) | (fix, _, op) <- xs]) opsTableOldNm
let modOpsTables' = Map.insert newNm opsTableOldNm modOpsTables
e <- withReaderT (const (opsTable', modOpsTables', customTypes)) $ (rword "in" <?> "_the 'in' keyword") *> expr
pure $ RenameModule newNmPos newNm oldNmPos oldNm inPos e
data InfernoParsingError
= ModuleNotFound ModuleName
| InfixOpNotFound ModuleName Ident
| UnboundTyVar Text
| ImplicitVarTypeAnnot
deriving (Eq, Show, Ord)
instance ShowErrorComponent InfernoParsingError where
showErrorComponent (ModuleNotFound (ModuleName modNm)) =
"Module '" <> unpack modNm <> "' could not be found"
showErrorComponent (InfixOpNotFound (ModuleName modNm) (Ident op)) =
"Module " <> unpack modNm <> " does not export `(" <> unpack op <> ")`"
showErrorComponent (UnboundTyVar ty) =
"Unbound type variable '" <> unpack ty
showErrorComponent ImplicitVarTypeAnnot =
"Implicit variables cannot have type annotations"
openModArgs :: ModuleName -> Parser ([(Import SourcePos, Maybe SourcePos)], SourcePos, Expr () SourcePos)
openModArgs modNm = do
symbol "("
is <- go
symbol ")"
(opsTable, modOpsTables, customTypes) <- ask
opsTable' <-
foldM
( \oTbl i -> case i of
IOpVar _ op -> do
foundOpTbl <- findOp op modOpsTables
return $ IntMap.unionWith (<>) oTbl foundOpTbl
_ -> pure oTbl
)
opsTable
(map fst is)
inPos <- getSourcePos
e <- withReaderT (const (opsTable', modOpsTables, customTypes)) $ (rword "in" <?> "_the 'in' keyword") *> expr
return (is, inPos, e)
where
findOp :: Ident -> Map.Map ModuleName OpsTable -> Parser OpsTable
findOp i@(Ident op) modOpsTables =
case Map.lookup modNm modOpsTables of
Just opsTable -> do
let filteredOpsTable =
IntMap.mapMaybe
(\xs -> let xs' = [(fix, LocalScope, op') | (fix, _modNm, op') <- xs, op == op'] in if null xs' then Nothing else Just xs')
opsTable
when (null filteredOpsTable) $ customFailure $ InfixOpNotFound modNm i
return filteredOpsTable
Nothing -> customFailure $ ModuleNotFound modNm
go =
try
((:) <$> ((,) <$> parseImport <*> lexeme (Just <$> getSourcePos <* symbol ",")) <*> go)
<|> (\i -> [(i, Nothing)]) <$> parseImport
parseImport =
try (IOpVar <$> getSourcePos <*> lexeme (char '(' *> (Ident <$> takeWhile1P Nothing isAlphaNum) <* char ')'))
<|> try (IEnum <$> lexeme (getSourcePos <* string "enum") <*> getSourcePos <*> lexeme (Ident <$> variable))
<|> IVar <$> getSourcePos <*> lexeme (Ident <$> variable)
openModE :: Parser (Expr () SourcePos)
openModE = label "an 'open' module expression\nfor example: open A in ..." $
do
hidden $ rword "open"
nmPos <- getSourcePos
nm <- lexeme (ModuleName <$> variable <?> "module name")
uncurry3 (OpenModule nmPos () nm) <$> (try (openModArgs nm) <|> (\inPos e -> ([], inPos, e)) <$> getSourcePos <*> openAll nm)
where
openAll modNm = do
(opsTable, modOpsTables, customTypes) <- ask
case Map.lookup modNm modOpsTables of
Just opsTable' ->
withReaderT (const (IntMap.unionWith (<>) opsTable opsTable', modOpsTables, customTypes)) $
(rword "in" <?> "_the 'in' keyword") *> expr
Nothing -> customFailure $ ModuleNotFound modNm
letE :: Parser (Expr () SourcePos)
letE = label ("a 'let' expression" ++ example "x") $
do
startPos <- getSourcePos
hidden $ rword "let"
varPos <- getSourcePos
x <- lexeme ((Expl . ExtIdent . Right <$> variable <|> Impl . ExtIdent . Right <$> implicitVariable) <?> "a variable")
let xStr = unpack $ renderPretty x
tPos <- getSourcePos
maybeTy <-
optional $
symbol ":"
*> withReaderT (\(ops, m, customTypes) -> (mempty, ops, m, customTypes)) schemeParser
eqPos <- getSourcePos
symbol "=" <?> "'='"
e1 <- expr <?> ("an expression to bind to '" ++ xStr ++ "'" ++ example xStr)
inPos <- getSourcePos
e2 <- (rword "in" <?> "_the 'in' keyword") *> expr
case (x, maybeTy) of
(Expl x', Just t) -> pure $ LetAnnot startPos varPos x' tPos t eqPos e1 inPos e2
(Impl _, Just _) -> customFailure ImplicitVarTypeAnnot
(_, Nothing) -> pure $ Let startPos varPos x eqPos e1 inPos e2
where
example x = "\nfor example: let " ++ x ++ " = 2 * 5 in ...\nor: let " ++ x ++ " : double = 1 + 2 in ..."
pat :: Parser (Pat () SourcePos)
pat =
uncurry3 PArray <$> array pat
<|> uncurry3 PRecord <$> record pat
<|> try (uncurry3 PTuple <$> tuple pat)
<|> parens pat
<|> try (hexadecimal PLit)
<|> try (signedDoubleE PLit)
<|> signedIntE PLit
<|> enumE PEnum
<|> uncurry PVar <$> mIdent
<|> noneE PEmpty
<|> someE POne pat
<|> stringE PLit
casePatts :: Parser [(SourcePos, Pat () SourcePos, SourcePos, Expr () SourcePos)]
casePatts = do
-- The | is optional before the first match clause:
_ <- optional (symbol "|" <?> "'|'")
onePat `sepBy1` (symbol "|" <?> "'|'")
where
onePat :: Parser (SourcePos, Pat () SourcePos, SourcePos, Expr () SourcePos)
onePat = label "_a pattern match clause\nfor example: #true -> ..." $ do
startPos <- getSourcePos
p <- pat
arrPos <- getSourcePos
symbol "->" <?> "'->'"
e <- expr
return (startPos, p, arrPos, e)
caseE :: Parser (Expr () SourcePos)
caseE = label "a pattern-match expression\nfor example: match x with { 1 -> #true | _ -> #false }" $
lexeme $ do
startPos <- getSourcePos
rword "match"
e <- expr <?> "an expression to pattern match on\nfor example: match (x, y) with { ... }"
rword "with"
brPos <- getSourcePos
symbol "{"
cs <- casePatts
endPos <- getSourcePos
char '}'
return $ Case startPos e brPos (NEList.fromList cs) endPos
tupleArgs :: SomeParser r a -> SomeParser r [(a, Maybe SourcePos)]
tupleArgs p =
try
( do
e <- p
commaPos <- lexeme $ char ',' *> getSourcePos
es <- tupleArgs p
return ((e, Just commaPos) : es)
)
<|> do
e1 <- p
commaPos <- lexeme $ char ',' *> getSourcePos
e2 <- p
return [(e1, Just commaPos), (e2, Nothing)]
isHSpace :: Char -> Bool
isHSpace x = isSpace x && x /= '\n' && x /= '\r'
tuple :: SomeParser r a -> SomeParser r (SourcePos, TList (a, Maybe SourcePos), SourcePos)
tuple p = label "a tuple\nfor example: (2, #true, 4.4)" $
lexeme $ do
startPos <- getSourcePos
symbol "("
r <- tListFromList <$> tupleArgs p <|> takeWhileP Nothing isHSpace $> TNil
endPos <- getSourcePos
char ')'
return (startPos, r, endPos)
assertE :: Parser (Expr () SourcePos)
assertE = label "an assertion\nfor example: assert x > 10 in ..." $ do
startPos <- getSourcePos
hidden $ rword "assert"
e1 <- expr <?> "a boolean expression\nfor example: x > 10 && x <= 25"
inPos <- getSourcePos
e2 <- (rword "in" <?> "_the 'in' keyword") *> expr
return $ Assert startPos e1 inPos e2
ifE :: Parser (Expr () SourcePos)
ifE = do
ifPos <- getSourcePos
hidden $ rword "if"
cond <- hidden expr <?> "_a conditional expression\nfor example: x > 2"
thenPos <- getSourcePos
tr <- (rword "then" *> expr) <?> "_the 'then' branch\nfor example: if x > 2 then 1 else 0"
elsePos <- getSourcePos
fl <- (rword "else" *> expr) <?> "_the 'else' branch\nfor example: if x > 2 then 1 else 0"
return $ If ifPos cond thenPos tr elsePos fl
-- | Parses an op in prefix syntax WITHOUT opening paren @(@ but with closing paren @)@
-- E.g. @+)@
prefixOpsWithoutModule :: SourcePos -> Parser (Expr () SourcePos)
prefixOpsWithoutModule startPos =
hidden (ask >>= choiceOf (prefixOp startPos) . opsInLocalScope)
where
opsInLocalScope (ops, _, _) = [s | (_, modNm, s) <- concat ops, modNm == LocalScope]
prefixOp :: SourcePos -> Text -> Parser (Expr () SourcePos)
prefixOp pos s = do
_ <- symbol $ s <> ")"
return $ OpVar pos () LocalScope $ Ident s
-- | Parses a op in prefix syntax of the form @Mod.(+)@
prefixOpsWithModule :: SourcePos -> Parser (Expr () SourcePos)
prefixOpsWithModule startPos =
hidden (ask >>= tryMany prefixOp . opsNotInLocal)
where
opsNotInLocal (ops, _, _) = [(modNm, s) | (_, modNm, s) <- concat ops, modNm /= LocalScope]
prefixOp :: (Scoped ModuleName, Text) -> Parser (Expr () SourcePos)
prefixOp (modNm, s) = do
_ <- symbol $ fromScoped "" (unModuleName <$> modNm) <> ".(" <> s <> ")"
return $ OpVar startPos () modNm $ Ident s
-- | Parses a tuple @a, b, c, ...)@ WITHOUT opening paren @(@ but with closing paren @)@
-- Returns (list of (expr, commaPos), endParenPos)
tupleElems :: Parser ([(Expr () SourcePos, Maybe SourcePos)], SourcePos)
tupleElems =
( do
e <- expr
( do
char ')'
endPos <- getSourcePos
return ([(e, Nothing)], endPos)
)
<|> ( do
commaPos <- lexeme $ char ',' *> getSourcePos
(es, endPos) <- tupleElems
return ((e, Just commaPos) : es, endPos)
)
)
<|> do
char ')'
endPos <- getSourcePos
return ([], endPos)
-- | Parses any bracketed expression: tuples, bracketed exprs (1 + 2), and prefix ops (+)
bracketedE :: Parser (Expr () SourcePos)
bracketedE = do
startPos <- getSourcePos
symbol "("
-- Either a prefix op or a tuple. bracketed exprs are 1-tuples
prefixOpsWithoutModule startPos <|> bracketedOrTuple startPos
where
bracketedOrTuple startPos = do
(es, endPos) <- tupleElems
lexeme $ pure $ case es of
[] -> Tuple startPos TNil endPos
[(e, _)] -> Bracketed startPos e endPos
_ -> Tuple startPos (tListFromList es) endPos
term :: Parser (Expr () SourcePos)
term =
bracketedE
<|> try (hexadecimal Lit)
<|> try doubleE
<|> intE
<|> enumE Enum
<|> do
-- Variable: foo or Mod.foo or Mod.(+)
-- Record field access rec.field is also parsed as Var and converted to RecordField
-- in pinExpr
startPos <- getSourcePos
lexeme $
try
( ( \nmspc x ->
Var startPos () (Scope $ ModuleName nmspc) $
Expl $
ExtIdent $
Right x
)
<$> variable
<*> (char '.' *> variable)
)
<|> prefixOpsWithModule startPos
<|> try
( Var startPos () LocalScope . Expl . ExtIdent . Right
<$> variable
<* notFollowedBy (char '.')
)
<|> noneE Empty
<|> someE One expr
<|> ifE
<|> try renameModE
<|> letE
<|> openModE
<|> assertE
<|> funE
<|> caseE
<|> try implVarE
<|> stringE Lit
<|> interpolatedStringE
<|> try (uncurry3 Array <$> array expr)
<|> try (uncurry3 Record <$> record expr)
<|> arrayComprE
app :: Parser (Expr () SourcePos)
app =
term >>= \x ->
(some term >>= \xs -> return (foldl App x xs))
<|> return x
expr :: Parser (Expr () SourcePos)
expr = ask >>= \(opsTable, _, _) -> makeExprParser app $ mkOperators opsTable
mkOperators :: OpsTable -> [[Operator Parser (Expr () SourcePos)]]
mkOperators opsTable =
[ map (uncurry3 $ mkOperatorP prec) opGrp | (prec, opGrp) <- IntMap.toDescList opsTable
]
where
infixLabel = label "an infix operator\nfor example: +, *, ==, >, <"
prefixLabel = label "a prefix operator\nfor example: -, !"
opString :: Scoped ModuleName -> Text -> Text
opString modNm s = case modNm of
LocalScope -> s
Scope (ModuleName ns) -> ns <> "." <> s
mkOperatorP :: Int -> Fixity -> Scoped ModuleName -> Text -> Operator Parser (Expr () SourcePos)
mkOperatorP prec (InfixOp NoFix) ns o =
InfixN $
infixLabel $
(\pos e1 e2 -> Op e1 pos () (prec, NoFix) ns (Ident o) e2) <$> lexeme (getSourcePos <* string (opString ns o))
mkOperatorP prec (InfixOp LeftFix) ns o =
InfixL $
infixLabel $
(\pos e1 e2 -> Op e1 pos () (prec, LeftFix) ns (Ident o) e2) <$> lexeme (getSourcePos <* string (opString ns o))
mkOperatorP prec (InfixOp RightFix) ns o =
InfixR $
infixLabel $
(\pos e1 e2 -> Op e1 pos () (prec, RightFix) ns (Ident o) e2) <$> lexeme (getSourcePos <* string (opString ns o))
mkOperatorP prec PrefixOp ns o =
Prefix $
prefixLabel $
(\pos e -> PreOp pos () prec ns (Ident o) e) <$> lexeme (getSourcePos <* string (opString ns o))
parseExpr ::
OpsTable ->
Map.Map ModuleName OpsTable ->
[CustomType] ->
Text ->
Either
(NonEmpty (ParseError Text InfernoParsingError, SourcePos))
(Expr () SourcePos, [Comment SourcePos])
parseExpr opsTable modOpsTables customTypes s =
case runParser (runWriterT $ flip runReaderT (opsTable, modOpsTables, customTypes) $ topLevel expr) "<stdin>" s of
Left (ParseErrorBundle errs pos) -> Left $ fst $ attachSourcePos errorOffset errs pos
Right (e, comments) -> Right (e, appEndo comments [])
-- parsing types
type TyParser = ReaderT (Map.Map Text Int, OpsTable, Map.Map ModuleName OpsTable, [CustomType]) (WriterT Comments (Parsec InfernoParsingError Text))
rws_type :: [Text] -- list of reserved type sig words
rws_type = ["define", "on", "forall"]
typeIdent :: TyParser Text
typeIdent = try (p >>= check)
where
p = pack <$> (((:) <$> letterChar <*> hidden (many alphaNumChar)) <?> "a type")
check x =
if x `elem` rws_type
then fail $ "Keyword " <> show x <> " cannot be a variable/function name"
else return x
baseType :: TyParser InfernoType
baseType = do
(_, _, _, customTypes) <- ask
TBase
<$> ( symbol "int" $> TInt
<|> symbol "double" $> TDouble
<|> symbol "word16" $> TWord16
<|> symbol "word32" $> TWord32
<|> symbol "word64" $> TWord64
<|> symbol "text" $> TText
<|> try (symbol "timeDiff" $> TTimeDiff)
<|> symbol "time" $> TTime
<|> symbol "resolution" $> TResolution
<|> choice (map (\t -> symbol (pack t) $> TCustom t) customTypes)
)
type_variable_raw :: TyParser Text
type_variable_raw = char '\'' *> takeWhile1P Nothing isAlphaNum
type_variable :: TyParser Int
type_variable = do
nm <- type_variable_raw
(tys, _, _, _) <- ask
case Map.lookup nm tys of
Just i -> return i
Nothing -> customFailure $ UnboundTyVar nm
recordType :: TyParser (Map.Map Ident InfernoType, RestOfRecord)
recordType = label "record type\nfor example: {name: text; age: int}" $
lexeme $ do
symbol "{"
(fields, rest) <- argsE
return (Map.fromList fields, rest)
where
argsE =
try
( do
f <- lexeme $ Ident <$> fieldName
symbol ":"
e <- typeParser
symbol ";"
(fields, rest) <- argsE
return ((f, e) : fields, rest)
)
<|> ( do
f <- lexeme $ Ident <$> fieldName
symbol ":"
e1 <- typeParser
char '}'
return ([(f, e1)], RowAbsent)
)
<|> ( do
t <- type_variable
char '}'
return ([], RowVar $ TV t)
)
<|> ( do
char '}'
pure ([], RowAbsent)
)
keywords = Set.fromList $ rws ++ rws_type
fieldName :: TyParser Text
fieldName = f >>= check
where
f = pack <$> (((:) <$> letterChar <*> hidden (many alphaNumCharOrSeparator)) <?> "a record field name")
check x =
if x `Set.member` keywords
then fail $ "Keyword " <> show x <> " cannot be a record field name"
else return x
typeParserBase :: TyParser InfernoType
typeParserBase =
try ((\(_, tys, _) -> TTuple $ fmap fst tys) <$> tuple typeParser)
<|> parens typeParser
<|> try (lexeme baseType)
<|> uncurry TRecord <$> recordType
<|> lexeme (TBase <$> (TEnum <$> typeIdent <*> (Set.fromList <$> (symbol "{" *> enumList <* symbol "}"))))
<|> lexeme (TVar . TV <$> type_variable)
where
enumList =
try
((:) <$> enumConstructor <* symbol "," <*> enumList)
<|> (: []) <$> enumConstructor
typeParser :: TyParser InfernoType
typeParser =
makeExprParser
typeParserBase
[ [ Prefix (TArray <$ rword "array" <* rword "of"),
Prefix (TSeries <$ rword "series" <* rword "of"),
Prefix (TOptional <$ rword "option" <* rword "of")
],
[ InfixR (TArr <$ symbol "->"),
InfixR (TArr <$ symbol "→")
]
]
parseType ::
Text ->
Either
(NonEmpty (ParseError Text InfernoParsingError, SourcePos))
InfernoType
parseType s = case runParser (runWriterT $ flip runReaderT (mempty, mempty, mempty, []) $ topLevel typeParser) "<stdin>" s of
Left (ParseErrorBundle errs pos) -> Left $ fst $ attachSourcePos errorOffset errs pos
Right (e, _) -> Right e
parseTCScheme :: Text -> Either String TCScheme
parseTCScheme s =
case runParser (runWriterT $ flip runReaderT (mempty, mempty, mempty, []) $ topLevel schemeParser) "<stdin>" s of
Left (ParseErrorBundle errs pos) -> Left $ show $ fst $ attachSourcePos errorOffset errs pos
Right (e, _) -> Right e
listParser :: TyParser a -> TyParser [a]
listParser p =
try
( do
e <- p
symbol ","
es <- listParser p
return (e : es)
)
<|> do
e1 <- p
return [e1]
tyContext :: TyParser [Either TypeClass (Text, InfernoType)]
tyContext = lexeme $ do
_ <- symbol "{"
res <- listParser tyContextSingle
_ <- symbol "}"
_ <- symbol "=>" <|> symbol "⇒"
return res
typeClass :: TyParser TypeClass
typeClass = TypeClass <$> (lexeme typeIdent <* symbol "on") <*> many typeParser
tyContextSingle :: TyParser (Either TypeClass (Text, InfernoType))
tyContextSingle = Left <$> (symbol "requires" *> typeClass) <|> Right <$> ((,) <$> (symbol "implicit" *> lexeme (withReaderT (\(_, ops, m, customTypes) -> (ops, m, customTypes)) variable)) <*> (symbol ":" *> typeParser))
schemeParser :: TyParser TCScheme
schemeParser = do
vars <- try (rword "forall" *> many (lexeme type_variable_raw) <* rword ".") <|> pure mempty
withReaderT (\(_, ops, m, ts) -> (Map.fromList $ zip vars [0 ..], ops, m, ts)) $
constructScheme <$> (try tyContext <|> pure []) <*> typeParser
where
constructScheme :: [Either TypeClass (Text, InfernoType)] -> InfernoType -> TCScheme
constructScheme cs t =
let (tcs, impls) = partitionEithers cs
in closeOver (Set.fromList tcs) $ ImplType (Map.fromList $ map (first (ExtIdent . Right)) impls) t
doc :: Parser Text
doc = do
_ <- symbol "@doc"
txt <- takeWhileP Nothing (/= ';')
_ <- symbol ";"
return txt
data TopLevelDefn def
= Signature
{ documentation :: Maybe Text,
name :: SigVar,
def :: def
}
| EnumDef (Maybe Text) Text [Ident]
| TypeClassInstance TypeClass
| Export ModuleName
deriving (Eq, Show, Data)
enumConstructors :: Parser [Ident]
enumConstructors = try ((:) <$> (lexeme enumConstructor <* symbol "|") <*> enumConstructors) <|> (: []) <$> lexeme enumConstructor
sigVariable :: Parser SigVar
sigVariable =
ask >>= \(opsTable, _, _) ->
let opList =
concatMap
( \case
(InfixOp _, _ns, i) -> [i]
_ -> []
)
$ concat
$ IntMap.elems opsTable
preOpList =
concatMap
( \case
(PrefixOp, _ns, i) -> [i]
_ -> []
)
$ concat
$ IntMap.elems opsTable
in lexeme $
tryMany (\op -> char '(' *> (SigOpVar <$> string op) <* char ')') opList
<|> tryMany (fmap SigVar . string) preOpList
<|> SigVar <$> variable
data QQDefinition = QQRawDef String | QQToValueDef String | InlineDef (Expr () SourcePos) deriving (Data)
exprOrBuiltin :: Parser QQDefinition
exprOrBuiltin =
try (QQToValueDef . unpack <$> lexeme (string "###" *> withReaderT (const (mempty, mempty, [])) variable <* string "###"))
<|> try (QQRawDef . unpack <$> lexeme (string "###!" *> withReaderT (const (mempty, mempty, [])) variable <* string "###"))
<|> InlineDef <$> expr
sigParser :: Parser (TopLevelDefn (Maybe TCScheme, QQDefinition))
sigParser =
( try (Signature <$> (try (Just <$> doc) <|> pure Nothing) <*> sigVariable <*> ((,) <$> (try (Just <$> (symbol ":" *> withReaderT (\(ops, m, customTypes) -> (mempty, ops, m, customTypes)) schemeParser)) <|> pure Nothing) <*> (symbol ":=" *> exprOrBuiltin)))