-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathInternalParser.ly
2284 lines (1844 loc) · 112 KB
/
InternalParser.ly
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 CPP #-}
> {-# OPTIONS_HADDOCK hide #-}
> -----------------------------------------------------------------------------
> -- |
> -- Module : Language.Haskell.Exts.Annotated.Parser
> -- Copyright : (c) Niklas Broberg 2004-2009,
> -- Original (c) Simon Marlow, Sven Panne 1997-2000
> -- License : BSD-style (see the file LICENSE.txt)
> --
> -- Maintainer : Niklas Broberg, [email protected]
> -- Stability : stable
> -- Portability : portable
> --
> --
> -----------------------------------------------------------------------------
>
> module Language.Haskell.Exts.InternalParser (
> mparseModule,
> mparseExp,
> mparsePat,
> mparseDecl,
> mparseType,
> mparseStmt,
> mparseImportDecl,
> ngparseModulePragmas,
> ngparseModuleHeadAndImports,
> ngparsePragmasAndModuleHead,
> ngparsePragmasAndModuleName
> ) where
>
> import Language.Haskell.Exts.Syntax hiding ( Type(..), Exp(..), Asst(..), XAttr(..), FieldUpdate(..) )
> import Language.Haskell.Exts.Syntax ( Type, Exp, Asst )
> import Language.Haskell.Exts.ParseMonad
> import Language.Haskell.Exts.InternalLexer
> import Language.Haskell.Exts.ParseUtils
> import Language.Haskell.Exts.Fixity
> import Language.Haskell.Exts.SrcLoc
> import Language.Haskell.Exts.Extension
> import Control.Monad ( liftM, (<=<), when )
> import Control.Applicative ( (<$>) )
> import Data.Maybe
> #if MIN_VERSION_base(4,11,0)
> import Prelude hiding ((<>))
> #endif
import Debug.Trace (trace)
> }
-----------------------------------------------------------------------------
This module comprises a parser for Haskell 98 with the following extensions
* Multi-parameter type classes with functional dependencies
* Implicit parameters
* Pattern guards
* Mdo notation
* FFI
* HaRP
* HSP
Most of the code is blatantly stolen from the GHC module Language.Haskell.Parser.
Some of the code for extensions is greatly influenced by GHC's internal parser
library, ghc/compiler/parser/Parser.y.
-----------------------------------------------------------------------------
Conflicts: 7 shift/reduce
2 for ambiguity in 'case x of y | let z = y in z :: Bool -> b' [State 99, 186]
(don't know whether to reduce 'Bool' as a btype or shift the '->'.
Similarly lambda and if. The default resolution in favour of the
shift means that a guard can never end with a type signature.
In mitigation: it's a rare case and no Haskell implementation
allows these, because it would require unbounded lookahead.)
There are 2 conflicts rather than one because contexts are parsed
as btypes (cf ctype).
1 for ambiguity in 'let ?x ...' [State 604]
the parser can't tell whether the ?x is the lhs of a normal binding or
an implicit binding. Fortunately resolving as shift gives it the only
sensible meaning, namely the lhs of an implicit binding.
1 for ambiguity using hybrid modules [State 159]
For HSP pages that start with a <% %> block, the parser cannot tell whether
to reduce a srcloc or shift the starting <%. Since any other body could not
start with <%, shifting is the only sensible thing to do.
1 for ambiguity using toplevel xml modules [State 158]
For HSP xml pages starting with a <, the parser cannot tell whether to shift
that < or reduce an implicit 'open'. Since no other body could possibly start
with <, shifting is the only sensible thing to do.
1 for ambiguity in '{-# RULES "name" [ ... #-}' [State 177]
we don't know whether the '[' starts the activation or not: it
might be the start of the declaration with the activation being
empty. Resolving with shift means the declaration cannot start with '['.
1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 544]
since 'forall' is a valid variable name, we don't know whether
to treat a forall on the input as the beginning of a quantifier
or the beginning of the rule itself. Resolving to shift means
it's always treated as a quantifier, hence the above is disallowed.
This saves explicitly defining a grammar for the rule lhs that
doesn't include 'forall'.
-----------------------------------------------------------------------------
> %token
> VARID { Loc _ (VarId _) } -- 1
> LABELVARID { Loc _ (LabelVarId _) }
> QVARID { Loc _ (QVarId _) }
> IDUPID { Loc _ (IDupVarId _) } -- duplicable implicit parameter ?x
> ILINID { Loc _ (ILinVarId _) } -- linear implicit parameter %x
> CONID { Loc _ (ConId _) }
> QCONID { Loc _ (QConId _) }
> DVARID { Loc _ (DVarId _) } -- VARID containing dashes
> VARSYM { Loc _ (VarSym _) }
> CONSYM { Loc _ (ConSym _) }
> QVARSYM { Loc _ (QVarSym _) } -- 10
> QCONSYM { Loc _ (QConSym _) }
> INT { Loc _ (IntTok _) }
> RATIONAL { Loc _ (FloatTok _) }
> CHAR { Loc _ (Character _) }
> STRING { Loc _ (StringTok _) }
> PRIMINT { Loc _ (IntTokHash _) }
> PRIMWORD { Loc _ (WordTokHash _) }
> PRIMFLOAT { Loc _ (FloatTokHash _) }
> PRIMDOUBLE { Loc _ (DoubleTokHash _) }
> PRIMCHAR { Loc _ (CharacterHash _) } -- 20
> PRIMSTRING { Loc _ (StringHash _) }
Symbols
> '(' { Loc $$ LeftParen }
> ')' { Loc $$ RightParen }
> '(#' { Loc $$ LeftHashParen }
> '#)' { Loc $$ RightHashParen }
> ';' { Loc $$ SemiColon }
> '{' { Loc $$ LeftCurly }
> '}' { Loc $$ RightCurly } -- 30
> vccurly { Loc $$ VRightCurly } -- a virtual close brace
> '[' { Loc $$ LeftSquare }
> ']' { Loc $$ RightSquare }
> '[:' { Loc $$ ParArrayLeftSquare }
> ':]' { Loc $$ ParArrayRightSquare }
> ',' { Loc $$ Comma }
> '_' { Loc $$ Underscore }
> '`' { Loc $$ BackQuote }
Reserved operators
> '.' { Loc $$ Dot }
> '..' { Loc $$ DotDot }
> ':' { Loc $$ Colon }
> '::' { Loc $$ DoubleColon } -- 40
> '=' { Loc $$ Equals }
> '\\' { Loc $$ Backslash }
> '|' { Loc $$ Bar }
> '<-' { Loc $$ LeftArrow }
> '->' { Loc $$ RightArrow }
> '@' { Loc $$ At }
> TYPEAPP { Loc $$ TApp }
> '~' { Loc $$ Tilde }
> '=>' { Loc $$ DoubleArrow }
> '-' { Loc $$ Minus }
> '!' { Loc $$ Exclamation } -- 50
> '*' { Loc $$ Star }
Arrows
> '-<' { Loc $$ LeftArrowTail }
> '>-' { Loc $$ RightArrowTail }
> '-<<' { Loc $$ LeftDblArrowTail }
> '>>-' { Loc $$ RightDblArrowTail }
> '(|' { Loc $$ OpenArrowBracket }
> '|)' { Loc $$ CloseArrowBracket }
Harp
> '(/' { Loc $$ RPGuardOpen }
> '/)' { Loc $$ RPGuardClose }
> '@:' { Loc $$ RPCAt }
Template Haskell
> IDSPLICE { Loc _ (THIdEscape _) } -- $x
> TIDSPLICE { Loc _ (THTIdEscape _) } -- $$x
> '$(' { Loc $$ THParenEscape } -- 60
> '$$(' { Loc $$ THTParenEscape }
> '[|' { Loc $$ THExpQuote }
> '[||' { Loc $$ THTExpQuote }
> '[p|' { Loc $$ THPatQuote }
> '[t|' { Loc $$ THTypQuote }
> '[d|' { Loc $$ THDecQuote }
> '|]' { Loc $$ THCloseQuote }
> '||]' { Loc $$ THTCloseQuote }
> VARQUOTE { Loc $$ THVarQuote } -- 'x
> TYPQUOTE { Loc $$ THTyQuote } -- ''T
> QUASIQUOTE { Loc _ (THQuasiQuote _) }
Hsx
> PCDATA { Loc _ (XPCDATA _) }
> '<' { Loc $$ XStdTagOpen } -- 70
> '</' { Loc $$ XCloseTagOpen }
> '<%' { Loc $$ XCodeTagOpen }
> '<%>' { Loc $$ XChildTagOpen }
> '>' { Loc $$ XStdTagClose }
> '/>' { Loc $$ XEmptyTagClose }
> '%>' { Loc $$ XCodeTagClose }
> '<[' { Loc $$ XRPatOpen }
> ']>' { Loc $$ XRPatClose }
FFI
> 'foreign' { Loc $$ KW_Foreign }
> 'export' { Loc $$ KW_Export } -- 80
> 'safe' { Loc $$ KW_Safe }
> 'unsafe' { Loc $$ KW_Unsafe }
> 'threadsafe' { Loc $$ KW_Threadsafe }
> 'interruptible' { Loc $$ KW_Interruptible }
> 'stdcall' { Loc $$ KW_StdCall }
> 'ccall' { Loc $$ KW_CCall }
> 'cplusplus' { Loc $$ KW_CPlusPlus }
> 'dotnet' { Loc $$ KW_DotNet }
> 'jvm' { Loc $$ KW_Jvm }
> 'js' { Loc $$ KW_Js } -- 90
> 'javascript' { Loc $$ KW_JavaScript }
> 'capi' { Loc $$ KW_CApi }
Reserved Ids
> 'as' { Loc $$ KW_As }
> 'by' { Loc $$ KW_By } -- transform list comprehensions
> 'case' { Loc $$ KW_Case }
> 'class' { Loc $$ KW_Class }
> 'data' { Loc $$ KW_Data }
> 'default' { Loc $$ KW_Default }
> 'deriving' { Loc $$ KW_Deriving }
> 'do' { Loc $$ KW_Do }
> 'else' { Loc $$ KW_Else } -- 100
> 'family' { Loc $$ KW_Family } -- indexed type families
> 'forall' { Loc $$ KW_Forall } -- universal/existential qualification
> 'group' { Loc $$ KW_Group } -- transform list comprehensions
> 'hiding' { Loc $$ KW_Hiding }
> 'if' { Loc $$ KW_If }
> 'import' { Loc $$ KW_Import }
> 'in' { Loc $$ KW_In }
> 'infix' { Loc $$ KW_Infix }
> 'infixl' { Loc $$ KW_InfixL }
> 'infixr' { Loc $$ KW_InfixR } -- 110
> 'instance' { Loc $$ KW_Instance }
> 'let' { Loc $$ KW_Let }
> 'mdo' { Loc $$ KW_MDo }
> 'module' { Loc $$ KW_Module } -- 114
> 'newtype' { Loc $$ KW_NewType }
> 'of' { Loc $$ KW_Of }
> 'proc' { Loc $$ KW_Proc } -- arrows
> 'rec' { Loc $$ KW_Rec } -- arrows or RecursiveDo
> 'then' { Loc $$ KW_Then }
> 'type' { Loc $$ KW_Type } -- 120
> 'using' { Loc $$ KW_Using } -- transform list comprehensions
> 'where' { Loc $$ KW_Where }
> 'qualified' { Loc $$ KW_Qualified }
> 'role' { Loc $$ KW_Role }
> 'pattern' { Loc $$ KW_Pattern }
> 'stock' { Loc $$ KW_Stock } -- for DerivingStrategies extension
> 'anyclass' { Loc $$ KW_Anyclass } -- for DerivingStrategies extension
> 'via' { Loc $$ KW_Via } -- for DerivingStrategies extension
Pragmas
> '{-# INLINE' { Loc _ (INLINE _) }
> '{-# INLINE CONLIKE' { Loc $$ INLINE_CONLIKE }
> '{-# SPECIALISE' { Loc $$ SPECIALISE }
> '{-# SPECIALISE INLINE' { Loc _ (SPECIALISE_INLINE _) }
> '{-# SOURCE' { Loc $$ SOURCE }
> '{-# RULES' { Loc $$ RULES }
> '{-# CORE' { Loc $$ CORE } -- 130
> '{-# SCC' { Loc $$ SCC }
> '{-# GENERATED' { Loc $$ GENERATED }
> '{-# DEPRECATED' { Loc $$ DEPRECATED }
> '{-# WARNING' { Loc $$ WARNING }
> '{-# UNPACK' { Loc $$ UNPACK }
> '{-# NOUNPACK' { Loc $$ NOUNPACK }
> '{-# OPTIONS' { Loc _ (OPTIONS _) }
'{-# CFILES' { Loc _ (CFILES _) }
'{-# INCLUDE' { Loc _ (INCLUDE _) }
> '{-# LANGUAGE' { Loc $$ LANGUAGE } -- 137
> '{-# ANN' { Loc $$ ANN }
> '{-# MINIMAL' { Loc $$ MINIMAL }
> '{-# NO_OVERLAP' { Loc $$ NO_OVERLAP }
> '{-# OVERLAP' { Loc $$ OVERLAP }
> '{-# OVERLAPS' { Loc $$ OVERLAPS }
> '{-# OVERLAPPING' { Loc $$ OVERLAPPING }
> '{-# OVERLAPPABLE' { Loc $$ OVERLAPPABLE }
> '{-# INCOHERENT' { Loc $$ INCOHERENT }
> '{-# COMPLETE' { Loc $$ COMPLETE }
> '#-}' { Loc $$ PragmaEnd } -- 139
Utility
> NEVER { Loc $$@SrcSpan{srcSpanStartLine= -1} _ } -- never-matching terminal of type SrcSpan
> %monad { P }
> %lexer { lexer } { Loc _ EOF }
> %error { parseError }
> %name mparseModule page
> %name mparseExp trueexp
> %name mparsePat pat
> %name mparseDeclAux body
> %name mparseType truectype
> %name mparseStmt stmt
> %name mparseImportDecl impdecl
> %partial ngparseModulePragmas toppragmas
> %partial ngparseModuleHeadAndImports moduletopimps
> %partial ngparsePragmasAndModuleHead moduletophead
> %partial ngparsePragmasAndModuleName moduletopname
> %tokentype { Loc Token }
> %expect 13
> %%
-----------------------------------------------------------------------------
Testing multiple modules in one file
> modules :: { [Module L] }
> : toppragmas modules1 { let (os,ss,l) = $1 in map (\x -> x os ss l) $2 }
> modules1 :: { [[ModulePragma L] -> [S] -> L -> Module L] }
> : module modules1 { $1 : $2 }
> | module { [$1] }
-----------------------------------------------------------------------------
HSP Pages
Any HSP-specific parts requiring the XmlSyntax extension enabled will
be governed by the lexing, since all productions require at least one
special lexeme.
TODO: Yuck, this is messy, needs fixing in the AST!
> page :: { Module L }
> : toppragmas topxml {% checkPageModule $2 $1 }
> | toppragmas '<%' module '%>' topxml {% let (os,ss,l) = $1 in checkHybridModule $5 ($3 os ss l) $2 $4 }
> | toppragmas module { let (os,ss,l) = $1 in $2 os ss l }
> topxml :: { PExp L }
> : '<' name attrs mattr '>' children '</' name '>' {% do { n <- checkEqNames $2 $8;
> let { cn = reverse $6;
> as = reverse $3; };
> return $ XTag ($1 <^^> $9 <** [$1,$5,$7,$9]) n as $4 cn } }
> | '<' name attrs mattr '/>' { XETag ($1 <^^> $5 <** [$1,$5]) $2 (reverse $3) $4 }
> toppragmas :: { ([ModulePragma L],[S],L) }
> : open toppragmasaux close { let (os,ss,ml) = $2 in (os,$1:ss++[$3],$1 <^^> $3) }
> toppragmasaux :: { ([ModulePragma L],[S],Maybe L) }
> : toppragma optsemis toppragmasaux { let (os,ss,ml) = $3;
> ss' = reverse $2 ++ ss;
> l' = case $2 of
> [] -> ann $1
> _ -> ann $1 <++> nIS (last $2);
> in ($1 : os, ss', Just $ l' <+?> ml) }
> | {- nothing -} { ([],[],Nothing) }
> toppragma :: { ModulePragma L }
> : '{-# LANGUAGE' conids optsemis '#-}' { LanguagePragma ($1 <^^> $4 <** ($1:snd $2 ++ reverse $3 ++ [$4])) (fst $2) }
> | '{-# OPTIONS' optsemis '#-}' { let Loc l (OPTIONS (mc, s)) = $1
> in OptionsPragma (l <^^> $3 <** (l:reverse $2 ++ [$3])) (readTool mc) s }
> | '{-# ANN' annotation '#-}' { AnnModulePragma ($1 <^^> $3 <** [$1,$3]) $2 }
> conids :: { ([Name L],[S]) }
> : conids ',' conid { (fst $1 ++ [$3], snd $1 ++ [$2]) }
> | optsemis conid { ([$2],[]) }
-----------------------------------------------------------------------------
Module Header
> module :: { [ModulePragma L] -> [S] -> L -> Module L }
> : optmodulehead body
> { let (is,ds,ss1,inf) = $2
> in \os ss l -> Module (l <++> inf <** (ss ++ ss1)) $1 os is ds }
> optmodulehead :: { Maybe (ModuleHead L) }
> : 'module' modid maybemodwarning maybeexports 'where' { Just $ ModuleHead ($1 <^^> $5 <** [$1,$5]) $2 $3 $4 }
> | {- empty -} { Nothing }
> maybemodwarning :: { Maybe (WarningText L) }
> : '{-# DEPRECATED' STRING '#-}' { let Loc l (StringTok (s,_)) = $2 in Just $ DeprText ($1 <^^> $3 <** [$1,l,$3]) s }
> | '{-# WARNING' STRING '#-}' { let Loc l (StringTok (s,_)) = $2 in Just $ WarnText ($1 <^^> $3 <** [$1,l,$3]) s }
> | {- empty -} { Nothing }
> body :: { ([ImportDecl L],[Decl L],[S],L) }
> : '{' bodyaux '}' { let (is,ds,ss) = $2 in (is,ds,$1:ss ++ [$3], $1 <^^> $3) }
Trailing optsemis in the next line is a workaround for #25. Having the optsemis
here causes one more shift/reduce conflict.
> | open bodyaux close optsemis { let (is,ds,ss) = $2 in (is,ds,$1:ss ++ [$3], $1 <^^> $3) }
> bodyaux :: { ([ImportDecl L],[Decl L],[S]) }
> : optsemis impdecls semis topdecls { (reverse (fst $2), fst $4, reverse $1 ++ snd $2 ++ reverse $3 ++ snd $4) }
> | optsemis topdecls { ([], fst $2, reverse $1 ++ snd $2) }
> | optsemis impdecls optsemis { (reverse (fst $2), [], reverse $1 ++ snd $2 ++ reverse $3) }
> | optsemis { ([], [], reverse $1) }
> semis :: { [S] }
> : optsemis ';' { $2 : $1 }
> optsemis :: { [S] }
> : semis { $1 }
> | {- empty -} { [] }
-----------------------------------------------------------------------------
The Export List
> maybeexports :: { Maybe (ExportSpecList L) }
> : exports { Just $1 }
> | {- empty -} { Nothing }
> exports :: { ExportSpecList L }
> : '(' exportlist optcomma ')' { ExportSpecList ($1 <^^> $4 <** ($1:reverse (snd $2) ++ $3 ++ [$4])) (reverse (fst $2)) }
> | '(' optcomma ')' { ExportSpecList ($1 <^^> $3 <** ($1:$2++[$3])) [] }
> optcomma :: { [S] }
> : ',' { [$1] }
> | {- empty -} { [ ] }
> exportlist :: { ([ExportSpec L],[S]) }
> : exportlist ',' export { ($3 : fst $1, $2 : snd $1) }
> | export { ([$1],[]) }
> export :: { ExportSpec L }
> : qvar { EVar (ann $1) $1 }
> | 'type' qcname {% do { checkEnabled ExplicitNamespaces;
> return (EAbs (nIS $1 <++> ann $2 <** [$1, srcInfoSpan (ann $2)]) (TypeNamespace (nIS $1 <** [$1])) $2) } }
> | qtyconorcls { EAbs (ann $1) (NoNamespace (ann $1)) $1 }
> | qtyconorcls '(' ')' { EThingWith (ann $1 <++> nIS $3 <** [$2,$3]) (NoWildcard noSrcSpan) $1 [] }
> | qtyconorcls '(' export_names ')' {% mkEThingWith (ann $1 <++> nIS $4 <** ($2:reverse (snd $3) ++ [$4])) $1 (reverse $ fst $3) }
> | 'module' modid { EModuleContents (nIS $1 <++> ann $2 <** [$1]) $2 }
> | 'pattern' qcon {% do { checkEnabled PatternSynonyms;
> return $ EAbs (nIS $1 <++> (ann $2) <** [$1])
> (PatternNamespace (nIS $1)) $2 }}
> export_names :: { ([Either S (CName L)],[S]) }
> : export_names ',' cname_w_wildcard { ($3 : fst $1, $2 : snd $1) }
> | cname_w_wildcard { ([$1],[]) }
> cname_w_wildcard :: { Either S (CName L) }
> : '..' { Left $1 }
> | cname { Right $1 }
>
> qcname :: { QName L }
> : qvar { $1 }
> | qcon { $1 }
-----------------------------------------------------------------------------
Import Declarations
> impdecls :: { ([ImportDecl L],[S]) }
> : impdecls semis impdecl { ($3 : fst $1, snd $1 ++ reverse $2) }
> | impdecl { ([$1],[]) }
> impdecl :: { ImportDecl L }
> : 'import' optsrc optsafe optqualified maybepkg modid maybeas maybeimpspec
> { let { (mmn,ss,ml) = $7 ;
> l = nIS $1 <++> ann $6 <+?> ml <+?> (fmap ann) $8 <** ($1:snd $2 ++ snd $3 ++ snd $4 ++ snd $5 ++ ss)}
> in ImportDecl l $6 (fst $4) (fst $2) (fst $3) (fst $5) mmn $8 }
> optsrc :: { (Bool,[S]) }
> : '{-# SOURCE' '#-}' { (True,[$1,$2]) }
> | {- empty -} { (False,[]) }
> optsafe :: { (Bool,[S]) }
> : 'safe' {% do { checkEnabledOneOf [Safe, SafeImports, Trustworthy] ;
> return (True, [$1]) } }
> | {- empty -} { (False, []) }
> optqualified :: { (Bool,[S]) }
> : 'qualified' { (True,[$1]) }
> | {- empty -} { (False, []) }
Requires the PackageImports extension enabled.
> maybepkg :: { (Maybe String,[S]) }
> : STRING {% do { checkEnabled PackageImports ;
> let { Loc l (StringTok (s,_)) = $1 } ;
> return $ (Just s,[l]) } }
> | {- empty -} { (Nothing,[]) }
> maybeas :: { (Maybe (ModuleName L),[S],Maybe L) }
> : 'as' modid { (Just $2,[$1],Just (nIS $1 <++> ann $2)) }
> | {- empty -} { (Nothing,[],Nothing) }
> maybeimpspec :: { Maybe (ImportSpecList L) }
> : impspec { Just $1 }
> | {- empty -} { Nothing }
> impspec :: { ImportSpecList L }
> : opthiding '(' importlist optcomma ')' { let {(b,ml,s) = $1 ;
> l = (ml <?+> ($2 <^^> $5)) <** (s ++ $2:reverse (snd $3) ++ $4 ++ [$5])}
> in ImportSpecList l b (reverse (fst $3)) }
> | opthiding '(' optcomma ')' { let {(b,ml,s) = $1 ; l = (ml <?+> ($2 <^^> $4)) <** (s ++ $2:$3 ++ [$4])}
> in ImportSpecList l b [] }
> opthiding :: { (Bool, Maybe L,[S]) }
> : 'hiding' { (True,Just (nIS $1),[$1]) }
> | {- empty -} { (False,Nothing,[]) }
> importlist :: { ([ImportSpec L],[S]) }
> : importlist ',' importspec { ($3 : fst $1, $2 : snd $1) }
> | importspec { ([$1],[]) }
> importspec :: { ImportSpec L }
> : var { IVar (ann $1) $1 }
> | 'type' var {% do { checkEnabled ExplicitNamespaces;
> return (IAbs (nIS $1 <++> ann $2 <** [$1, srcInfoSpan (ann $2)]) (TypeNamespace (nIS $1 <** [$1])) $2) } }
> | 'pattern' con {% do { checkEnabled PatternSynonyms;
> return (IAbs (nIS $1 <++> ann $2 <** [$1, srcInfoSpan (ann $2)]) (PatternNamespace (nIS $1 <** [$1])) $2) } }
> | tyconorcls { IAbs (ann $1) (NoNamespace (ann $1)) $1 }
> | tyconorcls '(' '..' ')' { IThingAll (ann $1 <++> nIS $4 <** [$2,$3,$4]) $1 }
> | tyconorcls '(' ')' { IThingWith (ann $1 <++> nIS $3 <** [$2,$3]) $1 [] }
> | tyconorcls '(' import_names ')' { IThingWith (ann $1 <++> nIS $4 <** ($2:reverse (snd $3) ++ [$4])) $1 (reverse (fst $3)) }
> import_names :: { ([CName L],[S]) }
> : import_names ',' cname { ($3 : fst $1, $2 : snd $1) }
> | cname { ([$1],[]) }
> cname :: { CName L }
> : var { VarName (ann $1) $1 }
> | con { ConName (ann $1) $1 }
-----------------------------------------------------------------------------
Fixity Declarations
> fixdecl :: { Decl L }
> : infix prec ops { let (ops,ss,l) = $3
> in InfixDecl (ann $1 <++> l <** (snd $2 ++ reverse ss)) $1 (fst $2) (reverse ops) }
> prec :: { (Maybe Int, [S]) }
> : {- empty -} { (Nothing, []) }
> | INT {% let Loc l (IntTok (i,_)) = $1 in checkPrec i >>= \i -> return (Just i, [l]) }
> infix :: { Assoc L }
> : 'infix' { AssocNone $ nIS $1 }
> | 'infixl' { AssocLeft $ nIS $1 }
> | 'infixr' { AssocRight $ nIS $1 }
> ops :: { ([Op L],[S],L) }
> : ops ',' op { let (ops,ss,l) = $1 in ($3 : ops, $2 : ss, l <++> ann $3) }
> | op { ([$1],[],ann $1) }
> opt_injectivity_info :: { Maybe (InjectivityInfo L) }
> : {- empty -} { Nothing }
> | injectivity_info { Just $1 }
> injectivity_info :: { InjectivityInfo L }
> : '|' tyvarid '->' inj_varids
> { InjectivityInfo (nIS $1 <++> ann (last $4) <** [$1,$3]) $2 (reverse $4) }
>
>
> inj_varids :: { [Name L] }
> : inj_varids tyvarid { $2 : $1 }
> | tyvarid { [$1] }
-----------------------------------------------------------------------------
Top-Level Declarations
Note: The report allows topdecls to be empty. This would result in another
shift/reduce-conflict, so we don't handle this case here, but in bodyaux.
> topdecls :: { ([Decl L],[S]) }
> : topdecls1 optsemis {% checkRevDecls (fst $1) >>= \ds -> return (ds, snd $1 ++ reverse $2) }
> topdecls1 :: { ([Decl L],[S]) }
> : topdecls1 semis topdecl { ($3 : fst $1, snd $1 ++ reverse $2) }
> | topdecl { ([$1],[]) }
> gtycons :: { [QName L] }
> : gtycon { [$1] }
> | gtycon ',' gtycons { ($1 : $3) }
> topdecl :: { Decl L }
> : role_annot {% checkEnabled RoleAnnotations >> return $1 }
> | 'type' dtype '=' truectype
> {% do { dh <- checkSimpleType $2;
> let {l = nIS $1 <++> ann $4 <** [$1,$3]};
> return (TypeDecl l dh $4) } }
Requires the StandaloneKindSignatures extension.
> | 'type' gtycons '::' truectype
> {% do { checkEnabled StandaloneKindSignatures;
> names <- mapM checkUnQual $2;
> let {l = nIS $1 <++> ann $4 <** [$1,$3]};
> return (TypeKindSig l names $4) } }
Requires the TypeFamilies extension enabled, but the lexer will handle
that through the 'family' keyword.
> | 'type' 'family' type opt_tyfam_kind_sig opt_injectivity_info where_type_family
> {% do { dh <- checkSimpleType $3;
> let {l = nIS $1 <++> ann $3 <** [$1,$2]};
> case $6 of {
> Nothing -> return (TypeFamDecl l dh $4 $5);
> Just (x,a) -> return (ClosedTypeFamDecl (l <** [a]) dh $4 $5 x); }}}
Here there is no special keyword so we must do the check.
> | 'type' 'instance' truedtype '=' truectype
> {% do { -- no checkSimpleType $4 since dtype may contain type patterns
> checkEnabled TypeFamilies ;
> let {l = nIS $1 <++> ann $5 <** [$1,$2,$4]};
> return (TypeInsDecl l $3 $5) } }
> | data_or_newtype ctype constrs0 maybe_derivings
> {% do { (cs,dh) <- checkDataHeader $2;
> let { (qds,ss,minf) = $3;
> l = $1 <> $2 <+?> minf <+?> fmap ann (listToMaybe $4) <** ss};
> checkDataOrNew $1 qds;
> return (DataDecl l $1 cs dh (reverse qds) (reverse $4)) } }
Requires the GADTs extension enabled, handled in gadtlist.
> | data_or_newtype ctype optkind gadtlist maybe_derivings
> {% do { (cs,dh) <- checkDataHeader $2;
> let { (gs,ss,minf) = $4;
> derivs' = reverse $5;
> l = ann $1 <+?> minf <+?> fmap ann (listToMaybe $5) <** (snd $3 ++ ss)};
> checkDataOrNewG $1 gs;
> case (gs, fst $3) of
> ([], Nothing) -> return (DataDecl l $1 cs dh [] derivs')
> _ -> checkEnabled GADTs >> return (GDataDecl l $1 cs dh (fst $3) (reverse gs) derivs') } }
Same as above, lexer will handle it through the 'family' keyword.
> | 'data' 'family' ctype opt_datafam_kind_sig
> {% do { (cs,dh) <- checkDataHeader $3;
> let {l = nIS $1 <++> ann $3 <+?> (fmap ann) $4 <** [$1,$2]};
> return (DataFamDecl l cs dh $4) } }
Here we must check for TypeFamilies.
> | data_or_newtype 'instance' truectype constrs0 maybe_derivings
> {% do { -- (cs,c,t) <- checkDataHeader $4;
> checkEnabled TypeFamilies ;
> let { (qds,ss,minf) = $4 ;
> l = $1 <> $3 <+?> minf <+?> fmap ann (listToMaybe $5) <** $2:ss };
> checkDataOrNew $1 qds;
> return (DataInsDecl l $1 $3 (reverse qds) (reverse $5)) } }
This style requires both TypeFamilies and GADTs, the latter is handled in gadtlist.
> | data_or_newtype 'instance' truectype optkind gadtlist maybe_derivings
> {% do { -- (cs,c,t) <- checkDataHeader $4;
> checkEnabled TypeFamilies ;
> let {(gs,ss,minf) = $5;
> derivs' = reverse $6;
> l = ann $1 <+?> minf <+?> fmap ann (listToMaybe derivs') <** ($2:snd $4 ++ ss)};
> checkDataOrNewG $1 gs;
> return (GDataInsDecl l $1 $3 (fst $4) (reverse gs) derivs') } }
> | 'class' ctype fds optcbody
> {% do { (cs,dh) <- checkClassHeader $2;
> let {(fds,ss1,minf1) = $3;(mcs,ss2,minf2) = $4} ;
> let { l = nIS $1 <++> ann $2 <+?> minf1 <+?> minf2 <** ($1:ss1 ++ ss2)} ;
> return (ClassDecl l cs dh fds mcs) } }
> | 'instance' optoverlap ctype optvaldefs
> {% do { ih <- checkInstHeader $3;
> let {(mis,ss,minf) = $4};
> return (InstDecl (nIS $1 <++> ann $3 <+?> minf <** ($1:ss)) $2 ih mis) } }
Requires the StandaloneDeriving extension enabled.
> | 'deriving' deriv_standalone_strategy 'instance' optoverlap ctype
> {% do { checkEnabled StandaloneDeriving ;
> ih <- checkInstHeader $5;
> let {l = nIS $1 <++> ann $5 <** [$1,$3]};
> return (DerivDecl l $2 $4 ih) } }
> | 'default' '(' typelist ')'
> { DefaultDecl ($1 <^^> $4 <** ($1:$2 : snd $3 ++ [$4])) (fst $3) }
Requires the TemplateHaskell extension, but the lexer will handle that
through the '$(' lexeme.
CHANGE: Arbitrary top-level expressions are considered implicit splices
> | exp0 {% do
> checkToplevel $1
> checkExpr $1 >>= \e -> return (SpliceDecl (ann e) e)
> }
| '$(' trueexp ')' { let l = $1 <^^> $3 <** [$1,$3] in SpliceDecl l $ ParenSplice l $2 }
| '$$(' trueexp ')' { let l = $1 <^^> $3 <** [$1,$3] in TSpliceDecl l $ TParenSplice l $2 }
These require the ForeignFunctionInterface extension, handled by the
lexer through the 'foreign' (and 'export') keyword.
> | 'foreign' 'import' callconv safety fspec
> { let (s,n,t,ss) = $5 in ForImp (nIS $1 <++> ann t <** ($1:$2:ss)) $3 $4 s n t }
> | 'foreign' 'export' callconv fspec
> { let (s,n,t,ss) = $4 in ForExp (nIS $1 <++> ann t <** ($1:$2:ss)) $3 s n t }
> | '{-# RULES' rules '#-}' { RulePragmaDecl ($1 <^^> $3 <** [$1,$3]) $ reverse $2 }
> | '{-# DEPRECATED' warndeprs '#-}' { DeprPragmaDecl ($1 <^^> $3 <** ($1:snd $2++[$3])) $ reverse (fst $2) }
> | '{-# WARNING' warndeprs '#-}' { WarnPragmaDecl ($1 <^^> $3 <** ($1:snd $2++[$3])) $ reverse (fst $2) }
> | '{-# ANN' annotation '#-}' { AnnPragma ($1 <^^> $3 <** [$1,$3]) $2 }
> | '{-# COMPLETE' con_list opt_tyconsig '#-}'
> { let com = maybe [] ((:[]) . fst) $3; ts = fmap snd $3 in
> (CompletePragma ($1 <^^> $4 <** ([$1] ++ fst $2 ++ com ++ [$4])) (snd $2) ts) }
> | decl { $1 }
> -- Family result/return kind signatures
>
> opt_datafam_kind_sig :: { Maybe (ResultSig L) }
> : { Nothing }
> | '::' kind { (Just $ KindSig (nIS $1 <++> ann $2 <** [$1]) $2) }
>
> opt_tyfam_kind_sig :: { Maybe (ResultSig L) }
> : { Nothing }
> | '::' kind { (Just $ KindSig (nIS $1 <++> ann $2 <** [$1]) $2) }
> | '=' ktyvar { (Just $ TyVarSig (nIS $1 <++> ann $2 <** [$1]) $2) }
>
> opt_at_kind_inj_sig :: { (Maybe (ResultSig L), Maybe (InjectivityInfo L))}
> : { (Nothing, Nothing) }
> | '::' kind { (Just (KindSig (nIS $1 <++> ann $2 <** [$1]) $2), Nothing) }
> | '=' ktyvar injectivity_info
> { (Just (TyVarSig (nIS $1 <++> ann $2 <** [$1]) $2), Just $3) }
> opt_at_kind_inj_sig2 :: { (Maybe (ResultSig L), Maybe (S, Type L), Maybe (InjectivityInfo L))}
> : { (Nothing, Nothing, Nothing) }
> | '::' kind { (Just (KindSig (nIS $1 <++> ann $2 <** [$1]) $2), Nothing, Nothing) }
> | '=' truectype opt_injectivity_info { (Nothing, Just ($1, $2), $3) }
Role annotations
> role_annot :: { Decl L }
> role_annot : 'type' 'role' otycon roles
> {% mkRoleAnnotDecl $1 $2 $3 (reverse $4) }
-- Reversed!
> roles :: { [(Maybe String, L)] }
> roles : {- empty -} { [] }
> | roles role { $2 : $1 }
> -- read it in as a varid for better error messages
> role :: { (Maybe String, L) }
> role : VARID { let (VarId v) = unLoc $1 in (Just v, nIS $ loc $1) }
> | '_' { (Nothing, nIS $1) }
> optoverlap :: { Maybe (Overlap L) }
> : '{-# OVERLAP' '#-}' { Just (Overlap (nIS $1)) }
> | '{-# OVERLAPS' '#-}' { Just (Overlaps (nIS $1)) }
> | '{-# OVERLAPPING' '#-}' { Just (Overlapping (nIS $1)) }
> | '{-# OVERLAPPABLE' '#-}' { Just (Overlappable (nIS $1)) }
> | '{-# INCOHERENT' '#-}' { Just (Incoherent (nIS $1)) }
> | '{-# NO_OVERLAP' '#-}' { Just (NoOverlap (nIS $1)) }
> | {- empty -} { Nothing }
Parsing the body of a closed type family, partially stolen from the source of GHC.
> where_type_family :: { Maybe ([TypeEqn L], S) }
> : {- empty -} { Nothing }
> | 'where' ty_fam_inst_eqn_list { Just ($2, $1) }
> ty_fam_inst_eqn_list :: { [TypeEqn L] }
> : '{' ty_fam_inst_eqns '}' { $2 }
> | open ty_fam_inst_eqns close { $2 }
> ty_fam_inst_eqns :: { [TypeEqn L] }
> : ty_fam_inst_eqns ';' ty_fam_inst_eqn { $1 ++ [$3] }
> | ty_fam_inst_eqns ';' { $1 }
> | ty_fam_inst_eqn { [$1] }
> | { [] }
> ty_fam_inst_eqn :: { TypeEqn L }
> : truectype '=' truectype
> {% do { checkEnabled TypeFamilies ;
> return (TypeEqn (ann $1 <++> ann $3 <** [$2]) $1 $3) } }
> data_or_newtype :: { DataOrNew L }
> : 'data' { DataType $ nIS $1 }
> | 'newtype' { NewType $ nIS $1 }
> typelist :: { ([Type L],[S]) }
> : types {% do { ts <- mapM checkType (fst $1);
> return $ (reverse ts, reverse (snd $1)) } }
> | truetype { ([$1],[]) }
> | {- empty -} { ([],[]) }
> decls :: { ([Decl L],[S]) }
> : optsemis decls1 optsemis {% checkRevDecls (fst $2) >>= \ds -> return (ds, reverse $1 ++ snd $2 ++ reverse $3) }
> | optsemis { ([],reverse $1) }
> decls1 :: { ([Decl L],[S]) }
> : decls1 semis decl { ($3 : fst $1, snd $1 ++ reverse $2) }
> | decl { ([$1],[]) }
> decl :: { Decl L }
> : signdecl { $1 }
> | fixdecl { $1 }
> | valdef { $1 }
> | pat_syn { $1 }
> | pattern_synonym_sig { $1 }
> decllist :: { Binds L }
> : '{' decls '}' { BDecls ($1 <^^> $3 <** ($1:snd $2++[$3])) (fst $2) }
> | open decls close { let l' = if null (fst $2) then nIS $3 else (ann . last $ fst $2)
> in BDecls (nIS $1 <++> l' <** ($1:snd $2++[$3])) (fst $2) }
> signdecl :: { Decl L }
> : signdecl0 { $1 }
> | specinldecl { $1 }
> signdecl0 :: { Decl L }
> : exp0b '::' truectype {% do { v <- checkSigVar $1;
> return $ TypeSig ($1 <> $3 <** [$2]) [v] $3 } }
> | exp0b ',' vars '::' truectype {% do { v <- checkSigVar $1;
> let {(vs,ss,_) = $3 ; l = $1 <> $5 <** ($2 : reverse ss ++ [$4]) } ;
> return $ TypeSig l (v : reverse vs) $5 } }
> specinldecl :: { Decl L }
> : '{-# INLINE' activation qvar '#-}' { let Loc l (INLINE s) = $1 in InlineSig (l <^^> $4 <** [l,$4]) s $2 $3 }
> | '{-# INLINE CONLIKE' activation qvar '#-}' { InlineConlikeSig ($1 <^^> $4 <** [$1,$4]) $2 $3 }
> | '{-# SPECIALISE' activation qvar '::' sigtypes '#-}'
> { SpecSig ($1 <^^> $6 <** ($1: $4 : snd $5 ++ [$6])) $2 $3 (fst $5) }
> | '{-# SPECIALISE INLINE' activation qvar '::' sigtypes '#-}'
> { let Loc l (SPECIALISE_INLINE s) = $1
> in SpecInlineSig (l <^^> $6 <** (l:$4:snd $5++[$6])) s $2 $3 (fst $5) }
> | '{-# SPECIALISE' 'instance' ctype '#-}' {% do { ih <- checkInstHeader $3;
> let {l = $1 <^^> $4 <** [$1,$2,$4]};
> return $ InstSig l ih } }
> | '{-# MINIMAL' name_boolformula '#-}' { MinimalPragma ($1 <^^> $3 <** [$1,$3]) $2 }
> sigtypes :: { ([Type L],[S]) }
> : sigtype { ([$1],[]) }
> | sigtype ',' sigtypes { ($1 : fst $3, $2 : snd $3) }
> sigtype :: { Type L }
> : ctype {% checkType $ mkTyForall (ann $1) Nothing InvisibleQuantification Nothing $1 }
> name_boolformula :: { Maybe (BooleanFormula L) }
> : name_boolformula1 { Just $1 }
> | {- empty -} { Nothing }
> name_boolformula1 :: { BooleanFormula L }
> : name_boolformula_and { $1 }
> | name_boolformula_and '|' name_boolformula1 { OrFormula (ann $1 <++> ann $3 <** [$2]) [$1,$3] }
> name_boolformula_and :: { BooleanFormula L }
> : name_boolformula_atom { $1 }
> | name_boolformula_atom ',' name_boolformula_and { AndFormula (ann $1 <++> ann $3 <** [$2]) [$1,$3] }
> name_boolformula_atom :: { BooleanFormula L }
> : '(' name_boolformula1 ')' { ParenFormula ($1 <^^> $3 <** [$1,$3]) $2 }
> | var { VarFormula (ann $1) $1 }
Binding can be either of implicit parameters, or it can be a normal sequence
of declarations. The two kinds cannot be mixed within the same block of
binding.
> binds :: { Binds L }
> : decllist { $1 }
> | '{' ipbinds '}' { IPBinds ($1 <^^> $3 <** snd $2) (fst $2) }
> | open ipbinds close { let l' = ann . last $ fst $2
> in IPBinds (nIS $1 <++> l' <** snd $2) (fst $2) }
ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
instead of qvar, we get another shift/reduce-conflict. Consider the
following programs:
{ (+) :: ... } only var
{ (+) x y = ... } could (incorrectly) be qvar
We re-use expressions for patterns, so a qvar would be allowed in patterns
instead of a var only (which would be correct). But deciding what the + is,
would require more lookahead. So let's check for ourselves...
> vars :: { ([Name L],[S],L) }
> : vars ',' var { let (ns,ss,l) = $1 in ($3 : ns, $2 : ss, l <++> ann $3) }
> | qvar {% do { n <- checkUnQual $1;
> return ([n],[],ann n) } }
-----------------------------------------------------------------------------
FFI
These will only be called on in the presence of a 'foreign' keyword,
so no need to check for extensions.
> callconv :: { CallConv L }
> : 'stdcall' { StdCall (nIS $1) }
> | 'ccall' { CCall (nIS $1) }
> | 'cplusplus' { CPlusPlus (nIS $1) }
> | 'dotnet' { DotNet (nIS $1) }
> | 'jvm' { Jvm (nIS $1) }
> | 'js' { Js (nIS $1) }
> | 'javascript' { JavaScript (nIS $1) }
> | 'capi' { CApi (nIS $1) }
> safety :: { Maybe (Safety L) }
> : 'safe' { Just $ PlaySafe (nIS $1) False }
> | 'unsafe' { Just $ PlayRisky (nIS $1) }
> | 'threadsafe' { Just $ PlaySafe (nIS $1) True }
> | 'interruptible' { Just $ PlayInterruptible (nIS $1) }
> | {- empty -} { Nothing }
> fspec :: { (Maybe String, Name L, Type L, [S]) }
> : STRING var_no_safety '::' truedtype { let Loc l (StringTok (s,_)) = $1 in (Just s, $2, $4, [l,$3]) }
> | var_no_safety '::' truedtype { (Nothing, $1, $3, [$2]) }
-----------------------------------------------------------------------------
Pragmas
> rules :: { [Rule L] }
> : rules ';'rule { $3 : $1 }
> | rules ';' { $1 }
> | rule { [$1] }
> | {- empty -} { [] }
> rule :: { Rule L }
> : STRING activation ruleforall exp0 '=' trueexp {% do { let {Loc l (StringTok (s,_)) = $1};
> e <- checkRuleExpr $4;
> return $ Rule (nIS l <++> ann $6 <** l:snd $3 ++ [$5]) s $2 (fst $3) e $6 } }
> activation :: { Maybe (Activation L) }
> : {- empty -} { Nothing }
> | '[' INT ']' { let Loc l (IntTok (i,_)) = $2 in Just $ ActiveFrom ($1 <^^> $3 <** [$1,l,$3]) (fromInteger i) }
> | '[' '~' INT ']' { let Loc l (IntTok (i,_)) = $3 in Just $ ActiveUntil ($1 <^^> $4 <** [$1,$2,l,$4]) (fromInteger i) }
> ruleforall :: { (Maybe [RuleVar L],[S]) }
> : {- empty -} { (Nothing,[]) }
> | 'forall' rulevars '.' { (Just $2,[$1,$3]) }
> rulevars :: { [RuleVar L] }
> : rulevar { [$1] }
> | rulevar rulevars { $1 : $2 }
> rulevar :: { RuleVar L }
> : varid { RuleVar (ann $1) $1 }
> | '(' varid '::' truectype ')' { TypedRuleVar ($1 <^^> $5 <** [$1,$3,$5]) $2 $4 }
> warndeprs :: { ([([Name L],String)],[S]) }
> : warndeprs ';' warndepr { (fst $3 : fst $1, snd $1 ++ ($2:snd $3)) }
> | warndeprs ';' { (fst $1, snd $1 ++ [$2]) }
> | warndepr { ([fst $1],snd $1) }
> | {- empty -} { ([],[]) }
> warndepr :: { (([Name L], String),[S]) }
> : namevars STRING { let Loc l (StringTok (s,_)) = $2 in ((fst $1,s),snd $1 ++ [l]) }
> namevars :: { ([Name L],[S]) }
> : namevar { ([$1],[]) }
> | namevar ',' namevars { ($1 : fst $3, $2 : snd $3) }
> namevar :: { Name L }
> : con { $1 }
> | var { $1 }
> annotation :: { Annotation L }
> : 'type' conid aexp {% checkExpr $3 >>= \e -> return (TypeAnn (nIS $1 <++> ann e <** [$1]) $2 e) }
> | 'module' aexp {% checkExpr $2 >>= \e -> return (ModuleAnn (nIS $1 <++> ann e <** [$1]) e) }
> | namevar aexp {% checkExpr $2 >>= \e -> return (Ann ($1 <> e) $1 e) }
-----------------------------------------------------------------------------
Types
Type equality contraints need the TypeFamilies extension.
> truedtype :: { Type L }
> : dtype {% checkType $1 }
> dtype :: { PType L }
> : dtype_('*',NEVER) { $1 }
> dtype_(ostar,kstar) :: { PType L }
> : btype_(ostar,kstar) { splitTilde $1 }
> | btype_(ostar,kstar) qtyconop dtype_(ostar,kstar) { TyInfix ($1 <> $3) $1 $2 $3 }
> | btype_(ostar,kstar) qtyvarop_(ostar) dtype_(ostar,kstar) { TyInfix ($1 <> $3) $1 (UnpromotedName (ann $2) $2) $3 } -- FIXME
> | btype_(ostar,kstar) '->' ctype_(ostar,kstar) { TyFun ($1 <> $3 <** [$2]) (splitTilde $1) $3 }
| btype_(ostar,kstar) '~' btype_(ostar,kstar) {% do { checkEnabledOneOf [TypeFamilies, GADTs] ;
let {l = $1 <> $3 <** [$2]};
return $ TyPred l $ EqualP l $1 $3 } }
Implicit parameters can occur in normal types, as well as in contexts.
> truetype :: { Type L }
> : type {% checkType $1 }
> type :: { PType L }
> : type_('*',NEVER) { $1 }
> type_(ostar,kstar) :: { PType L }
> : ivar '::' dtype_(ostar,kstar) { let l = ($1 <> $3 <** [$2]) in TyPred l $ IParam l $1 $3 }
> | dtype_(ostar,kstar) { $1 }
> truebtype :: { Type L }
> : btype {% checkType (splitTilde $1) }
> trueatype :: { Type L }
> : atype {% checkType $1 }
> btype :: { PType L }
> : btype_('*',NEVER) { $1 }
> btype_(ostar,kstar) :: { PType L }
> : btype_(ostar,kstar) atype_(ostar,kstar) { TyApp ($1 <> $2) $1 $2 }
> | atype_(ostar,kstar) { $1 }