mirrored from https://gitlab.haskell.org/ghc/ghc.git
-
Notifications
You must be signed in to change notification settings - Fork 737
Expand file tree
/
Copy pathParser.y
More file actions
4818 lines (4046 loc) · 232 KB
/
Copy pathParser.y
File metadata and controls
4818 lines (4046 loc) · 232 KB
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
-- -*-haskell-*-
-- ---------------------------------------------------------------------------
-- (c) The University of Glasgow 1997-2003
---
-- The GHC grammar.
--
-- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
-- ---------------------------------------------------------------------------
{
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MonadComprehensions #-}
-- | This module provides the generated Happy parser for Haskell. It exports
-- a number of parsers which may be used in any library that uses the GHC API.
-- A common usage pattern is to initialize the parser state with a given string
-- and then parse that string:
--
-- @
-- runParser :: ParserOpts -> String -> P a -> ParseResult a
-- runParser opts str parser = unP parser parseState
-- where
-- filename = "\<interactive\>"
-- location = mkRealSrcLoc (mkFastString filename) 1 1
-- buffer = stringToStringBuffer str
-- parseState = initParserState opts buffer location
-- @
module GHC.Parser
( parseModule, parseSignature, parseImport, parseStatement, parseBackpack
, parseDeclaration, parseExpression, parsePattern
, parseTypeSignature
, parseStmt, parseIdentifier
, parseType, parseHeader
, parseModuleNoHaddock
)
where
-- base
import Control.Monad ( unless, liftM, when, (<=<) )
import GHC.Exts
import Data.Maybe ( maybeToList )
import Data.List.NonEmpty ( NonEmpty(..), head, init, last, tail )
import qualified Data.List.NonEmpty as NE
import qualified Prelude -- for happy-generated code
import GHC.Hs
import GHC.Hs.Decls.Overlap ( OverlapMode(..) )
import GHC.Driver.Backpack.Syntax
import GHC.Unit.Info
import GHC.Unit.Module
import GHC.Unit.Module.Warnings
import GHC.Data.OrdList
import GHC.Data.BooleanFormula ( BooleanFormula(..), LBooleanFormula, mkTrue )
import GHC.Data.FastString
import GHC.Data.Maybe ( orElse )
import GHC.Utils.Outputable
import GHC.Utils.Error
import GHC.Utils.Misc ( looksLikePackageName, fstOf3, sndOf3, thdOf3 )
import GHC.Utils.Panic
import GHC.Prelude hiding ( head, init, last, tail )
import qualified GHC.Data.Strict as Strict
import GHC.Types.Name.Reader
import GHC.Types.Name.Occurrence ( varName, dataName, tcClsName, tvName, occNameFS, occNameString, mkVarOccFS )
import GHC.Types.UnresolvedImport ( ImportDeclOrigin(..) )
import GHC.Types.SrcLoc
import GHC.Types.Basic
import GHC.Types.Error ( GhcHint(..) )
import GHC.Types.Fixity
import GHC.Types.ForeignCall
import GHC.Types.InlinePragma
import GHC.Types.SourceFile
import GHC.Types.SourceText
import GHC.Types.PkgQual
import GHC.Core.Type ( Specificity(..) )
import GHC.Core.Class ( FunDep )
import GHC.Core.DataCon ( DataCon, dataConName )
import GHC.Parser.PostProcess
import GHC.Parser.PostProcess.Haddock
import GHC.Parser.Lexer
import GHC.Parser.HaddockLex
import GHC.Parser.Annotation
import GHC.Parser.Errors.Types
import GHC.Parser.Errors.Ppr ()
import GHC.Parser.String
import GHC.Builtin.WiredIn.Types
( unitTyCon, unitDataCon, sumTyCon,
tupleTyCon, tupleDataCon, nilDataCon,
unboxedUnitTyCon, unboxedUnitDataCon,
listTyConName, consDataConName,
unrestrictedFunTyCon )
import Language.Haskell.Syntax.Basic (FieldLabelString(..))
import Language.Haskell.Syntax.Text
import qualified Data.Semigroup as Semi
import qualified Data.Text as T
}
%expect 0 -- shift/reduce conflicts
{- Note [shift/reduce conflicts]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 'happy' tool turns this grammar into an efficient parser that follows the
shift-reduce parsing model. There's a parse stack that contains items parsed so
far (both terminals and non-terminals). Every next token produced by the lexer
results in one of two actions:
SHIFT: push the token onto the parse stack
REDUCE: pop a few items off the parse stack and combine them
with a function (reduction rule)
However, sometimes it's unclear which of the two actions to take.
Consider this code example:
if x then y else f z
There are two ways to parse it:
(if x then y else f) z
if x then y else (f z)
How is this determined? At some point, the parser gets to the following state:
parse stack: 'if' exp 'then' exp 'else' "f"
next token: "z"
Scenario A (simplified):
1. REDUCE, parse stack: 'if' exp 'then' exp 'else' exp
next token: "z"
(Note that "f" reduced to exp here)
2. REDUCE, parse stack: exp
next token: "z"
3. SHIFT, parse stack: exp "z"
next token: ...
4. REDUCE, parse stack: exp
next token: ...
This way we get: (if x then y else f) z
Scenario B (simplified):
1. SHIFT, parse stack: 'if' exp 'then' exp 'else' "f" "z"
next token: ...
2. REDUCE, parse stack: 'if' exp 'then' exp 'else' exp
next token: ...
3. REDUCE, parse stack: exp
next token: ...
This way we get: if x then y else (f z)
The end result is determined by the chosen action. When Happy detects this, it
reports a shift/reduce conflict. At the top of the file, we have the following
directive:
%expect 0
It means that we expect no unresolved shift/reduce conflicts in this grammar.
If you modify the grammar and get shift/reduce conflicts, follow the steps
below to resolve them.
STEP ONE
is to figure out what causes the conflict.
That's where the -i flag comes in handy:
happy -agc --strict compiler/GHC/Parser.y -idetailed-info
By analysing the output of this command, in a new file `detailed-info`, you
can figure out which reduction rule causes the issue. At the top of the
generated report, you will see a line like this:
state 147 contains 67 shift/reduce conflicts.
Scroll down to section State 147 (in your case it could be a different
state). The start of the section lists the reduction rules that can fire
and shows their context:
exp10 -> fexp . (rule 492)
fexp -> fexp . aexp (rule 498)
fexp -> fexp . PREFIX_AT atype (rule 499)
And then, for every token, it tells you the parsing action:
']' reduce using rule 492
'::' reduce using rule 492
'(' shift, and enter state 178
QVARID shift, and enter state 44
DO shift, and enter state 182
...
But if you look closer, some of these tokens also have another parsing action
in parentheses:
QVARID shift, and enter state 44
(reduce using rule 492)
That's how you know rule 492 is causing trouble.
Scroll back to the top to see what this rule is:
----------------------------------
Grammar
----------------------------------
...
...
exp10 -> fexp (492)
optSemi -> ';' (493)
...
...
Hence the shift/reduce conflict is caused by this parser production:
exp10 :: { ECP }
: '-' fexp { ... }
| fexp { ... } -- problematic rule
STEP TWO
is to mark the problematic rule with the %shift pragma. This signals to
'happy' that any shift/reduce conflicts involving this rule must be resolved
in favor of a shift. There's currently no dedicated pragma to resolve in
favor of the reduce.
STEP THREE
is to add a dedicated Note for this specific conflict, as is done for all
other conflicts below.
-}
{- Note [%shift: rule_activation -> {- empty -}]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
rule -> STRING . rule_activation rule_foralls infixexp '=' exp
Example:
{-# RULES "name" [0] f = rhs #-}
Ambiguity:
If we reduced, then we'd get an empty activation rule, and [0] would be
parsed as part of the left-hand side expression.
We shift, so [0] is parsed as an activation rule.
-}
{- Note [%shift: rule_foralls -> {- empty -}]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
rule -> STRING rule_activation . rule_foralls infixexp '=' exp
Example:
{-# RULES "name" forall a1. lhs = rhs #-}
Ambiguity:
If we reduced, then we would get an empty rule_foralls; the 'forall', being
a valid term-level identifier, would be parsed as part of the left-hand
side expression.
We shift, so the 'forall' is parsed as part of rule_foralls.
-}
{- Note [%shift: type -> btype]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
context -> btype .
type -> btype .
type -> btype . '->' ctype
type -> btype . '->.' ctype
Example:
a :: Maybe Integer -> Bool
Ambiguity:
If we reduced, we would get: (a :: Maybe Integer) -> Bool
We shift to get this instead: a :: (Maybe Integer -> Bool)
-}
{- Note [%shift: infixtype -> ftype]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
infixtype -> ftype .
infixtype -> ftype . tyop infixtype
ftype -> ftype . tyarg
ftype -> ftype . PREFIX_AT tyarg
Example:
a :: Maybe Integer
Ambiguity:
If we reduced, we would get: (a :: Maybe) Integer
We shift to get this instead: a :: (Maybe Integer)
-}
{- Note [%shift: atype -> tyvar]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
atype -> tyvar .
tv_bndr_no_braces -> '(' tyvar . '::' kind ')'
Example:
class C a where type D a = (a :: Type ...
Ambiguity:
If we reduced, we could specify a default for an associated type like this:
class C a where type D a
type D a = (a :: Type)
But we shift in order to allow injectivity signatures like this:
class C a where type D a = (r :: Type) | r -> a
-}
{- Note [%shift: exp -> infixexp]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
exp -> infixexp . '::' sigtype
exp -> infixexp . '-<' exp
exp -> infixexp . '>-' exp
exp -> infixexp . '-<<' exp
exp -> infixexp . '>>-' exp
exp -> infixexp .
infixexp -> infixexp . qop exp10p
Examples:
1) if x then y else z -< e
2) if x then y else z :: T
3) if x then y else z + 1 -- (NB: '+' is in VARSYM)
Ambiguity:
If we reduced, we would get:
1) (if x then y else z) -< e
2) (if x then y else z) :: T
3) (if x then y else z) + 1
We shift to get this instead:
1) if x then y else (z -< e)
2) if x then y else (z :: T)
3) if x then y else (z + 1)
-}
{- Note [%shift: exp10 -> '-' fexp]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
exp10 -> '-' fexp .
fexp -> fexp . aexp
fexp -> fexp . PREFIX_AT atype
Examples & Ambiguity:
Same as in Note [%shift: exp10 -> fexp],
but with a '-' in front.
-}
{- Note [%shift: exp10 -> fexp]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
exp10 -> fexp .
fexp -> fexp . aexp
fexp -> fexp . PREFIX_AT atype
Examples:
1) if x then y else f z
2) if x then y else f @z
Ambiguity:
If we reduced, we would get:
1) (if x then y else f) z
2) (if x then y else f) @z
We shift to get this instead:
1) if x then y else (f z)
2) if x then y else (f @z)
-}
{- Note [%shift: aexp2 -> ipvar]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
aexp2 -> ipvar .
dbind -> ipvar . '=' exp
Example:
let ?x = ...
Ambiguity:
If we reduced, ?x would be parsed as the LHS of a normal binding,
eventually producing an error.
We shift, so it is parsed as the LHS of an implicit binding.
-}
{- Note [%shift: aexp2 -> TH_TY_QUOTE]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
aexp2 -> TH_TY_QUOTE . tyvar
aexp2 -> TH_TY_QUOTE . gtycon
aexp2 -> TH_TY_QUOTE .
Examples:
1) x = ''
2) x = ''a
3) x = ''T
Ambiguity:
If we reduced, the '' would result in reportEmptyDoubleQuotes even when
followed by a type variable or a type constructor. But the only reason
this reduction rule exists is to improve error messages.
Naturally, we shift instead, so that ''a and ''T work as expected.
-}
{- Note [%shift: tup_tail -> {- empty -}]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
tup_exprs -> commas . tup_tail
sysdcon_nolist -> '(' commas . ')'
sysdcon_nolist -> '(#' commas . '#)'
commas -> commas . ','
Example:
(,,)
Ambiguity:
A tuple section with no components is indistinguishable from the Haskell98
data constructor for a tuple.
If we reduced, (,,) would be parsed as a tuple section.
We shift, so (,,) is parsed as a data constructor.
This is preferable because we want to accept (,,) without -XTupleSections.
See also Note [ExplicitTuple] in GHC.Hs.Expr.
-}
{- Note [%shift: qtyconop -> qtyconsym]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
oqtycon -> '(' qtyconsym . ')'
qtyconop -> qtyconsym .
Example:
foo :: (:%)
Ambiguity:
If we reduced, (:%) would be parsed as a parenthesized infix type
expression without arguments, resulting in the 'failOpFewArgs' error.
We shift, so it is parsed as a type constructor.
-}
{- Note [%shift: special_id -> 'group']
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
transformqual -> 'then' 'group' . 'using' exp
transformqual -> 'then' 'group' . 'by' exp 'using' exp
special_id -> 'group' .
Example:
[ ... | then group by dept using groupWith
, then take 5 ]
Ambiguity:
If we reduced, 'group' would be parsed as a term-level identifier, just as
'take' in the other clause.
We shift, so it is parsed as part of the 'group by' clause introduced by
the -XTransformListComp extension.
-}
{- Note [%shift: activation -> {- empty -}]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
sigdecl -> '{-# INLINE' . activation qvarcon '#-}'
activation -> {- empty -}
activation -> explicit_activation
Example:
{-# INLINE [0] Something #-}
Ambiguity:
We don't know whether the '[' is the start of the activation or the beginning
of the [] data constructor.
We parse this as having '[0]' activation for inlining 'Something', rather than
empty activation and inlining '[0] Something'.
-}
{- Note [%shift: orpats -> exp]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Context:
texp -> exp .
orpats -> exp .
texp -> exp . '->' texp
orpats -> exp . ';' orpats
in Lookahead ')': reduce/reduce conflict between the two first productions
Example:
f (True) = 3
----^
Ambiguity:
We don't know whether the ')' encloses a parenthesized pat (reduce with
first production) or a unary Or pattern (reduce with second production).
We want to parse it as a parenthesized pat, because
* That is the status quo
* Parsing it as a unary Or patterns prompts the user to activate -XOrPatterns.
Thus, we add a %shift pragma to `orpats -> exp` to lower its precedence,
which has the effect of letting `texp -> exp` win (!).
An alternative to resolve this ambiguity would be to accept only OrPatterns
with at least two patterns in `orpats`, just as in `tup_exprs`.
But the present code seems simpler, because it just needs one non-terminal,
at the expense of using a small pragma.
-}
{- Note [Parser API Annotations]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A lot of the productions are now cluttered with calls to
aa,am,acs,acsA etc.
These are helper functions to make sure that the locations of the
various keywords such as do / let / in are captured for use by tools
that want to do source to source conversions, such as refactorers or
structured editors.
The helper functions are defined at the bottom of this file.
See
https://gitlab.haskell.org/ghc/ghc/wikis/api-annotations and
https://gitlab.haskell.org/ghc/ghc/wikis/ghc-ast-annotations
for some background.
-}
{- Note [Parsing lists]
~~~~~~~~~~~~~~~~~~~~~~~
You might be wondering why we spend so much effort encoding our lists this
way:
importdecls
: importdecls ';' importdecl
| importdecls ';'
| importdecl
| {- empty -}
This might seem like an awfully roundabout way to declare a list; plus, to add
insult to injury you have to reverse the results at the end. The answer is that
left recursion prevents us from running out of stack space when parsing long
sequences. See:
https://haskell-happy.readthedocs.io/en/latest/using.html#parsing-sequences
for more guidance.
By adding/removing branches, you can affect what lists are accepted. Here
are the most common patterns, rewritten as regular expressions for clarity:
-- Equivalent to: ';'* (x ';'+)* x? (can be empty, permits leading/trailing semis)
xs : xs ';' x
| xs ';'
| x
| {- empty -}
-- Equivalent to x (';' x)* ';'* (non-empty, permits trailing semis)
xs : xs ';' x
| xs ';'
| x
-- Equivalent to ';'* alts (';' alts)* ';'* (non-empty, permits leading/trailing semis)
alts : alts1
| ';' alts
alts1 : alts1 ';' alt
| alts1 ';'
| alt
-- Equivalent to x (',' x)+ (non-empty, no trailing semis)
xs : x
| x ',' xs
-}
%token
'_' { L _ ITunderscore } -- Haskell keywords
'as' { L _ ITas }
'case' { L _ ITcase }
'class' { L _ ITclass }
'data' { L _ ITdata }
'default' { L _ ITdefault }
'deriving' { L _ ITderiving }
'else' { L _ ITelse }
'hiding' { L _ IThiding }
'if' { L _ ITif }
'import' { L _ ITimport }
'in' { L _ ITin }
'infix' { L _ ITinfix }
'infixl' { L _ ITinfixl }
'infixr' { L _ ITinfixr }
'instance' { L _ ITinstance }
'let' { L _ ITlet }
'module' { L _ ITmodule }
'newtype' { L _ ITnewtype }
'of' { L _ ITof }
'qualified' { L _ ITqualified }
'then' { L _ ITthen }
'type' { L _ ITtype }
'where' { L _ ITwhere }
'forall' { L _ (ITforall _) } -- GHC extension keywords
'foreign' { L _ ITforeign }
'export' { L _ ITexport }
'label' { L _ ITlabel }
'dynamic' { L _ ITdynamic }
'safe' { L _ ITsafe }
'interruptible' { L _ ITinterruptible }
'unsafe' { L _ ITunsafe }
'family' { L _ ITfamily }
'role' { L _ ITrole }
'stdcall' { L _ ITstdcallconv }
'ccall' { L _ ITccallconv }
'capi' { L _ ITcapiconv }
'prim' { L _ ITprimcallconv }
'javascript' { L _ ITjavascriptcallconv }
'proc' { L _ ITproc } -- for arrow notation extension
'rec' { L _ ITrec } -- for arrow notation extension
'group' { L _ ITgroup } -- for list transform extension
'by' { L _ ITby } -- for list transform extension
'using' { L _ ITusing } -- for list transform extension
'pattern' { L _ ITpattern } -- for pattern synonyms
'static' { L _ ITstatic } -- for static pointers extension
'stock' { L _ ITstock } -- for DerivingStrategies extension
'anyclass' { L _ ITanyclass } -- for DerivingStrategies extension
'via' { L _ ITvia } -- for DerivingStrategies extension
'splice' { L _ ITsplice } -- For StagedImports extension
'quote' { L _ ITquote } -- For StagedImports extension
'unit' { L _ ITunit }
'signature' { L _ ITsignature }
'dependency' { L _ ITdependency }
'{-# INLINE' { L _ (ITinline_prag _ _ _) } -- INLINE or INLINABLE
'{-# OPAQUE' { L _ (ITopaque_prag _) }
'{-# SPECIALISE' { L _ (ITspec_prag _) }
'{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _ _) }
'{-# SOURCE' { L _ (ITsource_prag _) }
'{-# RULES' { L _ (ITrules_prag _) }
'{-# SCC' { L _ (ITscc_prag _)}
'{-# DEPRECATED' { L _ (ITdeprecated_prag _) }
'{-# WARNING' { L _ (ITwarning_prag _) }
'{-# UNPACK' { L _ (ITunpack_prag _) }
'{-# NOUNPACK' { L _ (ITnounpack_prag _) }
'{-# ANN' { L _ (ITann_prag _) }
'{-# MINIMAL' { L _ (ITminimal_prag _) }
'{-# CTYPE' { L _ (ITctype _) }
'{-# OVERLAPPING' { L _ (IToverlapping_prag _) }
'{-# OVERLAPPABLE' { L _ (IToverlappable_prag _) }
'{-# OVERLAPS' { L _ (IToverlaps_prag _) }
'{-# INCOHERENT' { L _ (ITincoherent_prag _) }
'{-# COMPLETE' { L _ (ITcomplete_prag _) }
'#-}' { L _ ITclose_prag }
'..' { L _ ITdotdot } -- reserved symbols
':' { L _ ITcolon }
'::' { L _ (ITdcolon _) }
'=' { L _ ITequal }
'\\' { L _ ITlam }
'lcase' { L _ ITlcase }
'lcases' { L _ ITlcases }
'|' { L _ ITvbar }
'<-' { L _ (ITlarrow _) }
'->' { L _ (ITrarrow _) }
'->.' { L _ ITlolly }
TIGHT_INFIX_AT { L _ ITat }
'=>' { L _ (ITdarrow _) }
'-' { L _ ITminus }
PREFIX_TILDE { L _ ITtilde }
PREFIX_BANG { L _ ITbang }
PREFIX_MINUS { L _ ITprefixminus }
'*' { L _ (ITstar _) }
'-<' { L _ (ITlarrowtail _) } -- for arrow notation
'>-' { L _ (ITrarrowtail _) } -- for arrow notation
'-<<' { L _ (ITLarrowtail _) } -- for arrow notation
'>>-' { L _ (ITRarrowtail _) } -- for arrow notation
'.' { L _ ITdot }
PREFIX_PROJ { L _ (ITproj True) } -- RecordDotSyntax
TIGHT_INFIX_PROJ { L _ (ITproj False) } -- RecordDotSyntax
PREFIX_AT { L _ ITtypeApp }
PREFIX_PERCENT { L _ ITpercent } -- for linear types
'{' { L _ ITocurly } -- special symbols
'}' { L _ ITccurly }
vocurly { L _ ITvocurly } -- virtual open curly (from layout)
vccurly { L _ ITvccurly } -- virtual close curly (from layout)
'[' { L _ ITobrack }
']' { L _ ITcbrack }
'(' { L _ IToparen }
')' { L _ ITcparen }
'(#' { L _ IToubxparen }
'#)' { L _ ITcubxparen }
'(|' { L _ (IToparenbar _) }
'|)' { L _ (ITcparenbar _) }
';' { L _ ITsemi }
',' { L _ ITcomma }
'`' { L _ ITbackquote }
SIMPLEQUOTE { L _ ITsimpleQuote } -- 'x
VARID { L _ (ITvarid _) } -- identifiers
CONID { L _ (ITconid _) }
VARSYM { L _ (ITvarsym _) }
CONSYM { L _ (ITconsym _) }
QVARID { L _ (ITqvarid _) }
QCONID { L _ (ITqconid _) }
QVARSYM { L _ (ITqvarsym _) }
QCONSYM { L _ (ITqconsym _) }
-- QualifiedDo
DO { L _ (ITdo _) }
MDO { L _ (ITmdo _) }
IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
LABELVARID { L _ (ITlabelvarid _ _) }
CHAR { L _ (ITchar _ _) }
QUALSTRING { L _ (ITstring _ StringMeta{strMetaQualified = Just _} _) }
STRING { L _ (ITstring _ _ _) }
INTEGER { L _ (ITinteger _) }
RATIONAL { L _ (ITrational _) }
PRIMCHAR { L _ (ITprimchar _ _) }
PRIMSTRING { L _ (ITprimstring _ _) }
PRIMINTEGER { L _ (ITprimint _ _) }
PRIMWORD { L _ (ITprimword _ _) }
PRIMINTEGER8 { L _ (ITprimint8 _ _) }
PRIMINTEGER16 { L _ (ITprimint16 _ _) }
PRIMINTEGER32 { L _ (ITprimint32 _ _) }
PRIMINTEGER64 { L _ (ITprimint64 _ _) }
PRIMWORD8 { L _ (ITprimword8 _ _) }
PRIMWORD16 { L _ (ITprimword16 _ _) }
PRIMWORD32 { L _ (ITprimword32 _ _) }
PRIMWORD64 { L _ (ITprimword64 _ _) }
PRIMFLOAT { L _ (ITprimfloat _) }
PRIMDOUBLE { L _ (ITprimdouble _) }
-- Template Haskell
'[|' { L _ (ITopenExpQuote _ _) }
'[p|' { L _ ITopenPatQuote }
'[t|' { L _ ITopenTypQuote }
'[d|' { L _ ITopenDecQuote }
'|]' { L _ (ITcloseQuote _) }
'[||' { L _ (ITopenTExpQuote _) }
'||]' { L _ ITcloseTExpQuote }
PREFIX_DOLLAR { L _ ITdollar }
PREFIX_DOLLAR_DOLLAR { L _ ITdollardollar }
TH_TY_QUOTE { L _ ITtyQuote } -- ''T
TH_QUASIQUOTE { L _ (ITquasiQuote _) }
TH_QQUASIQUOTE { L _ (ITqQuasiQuote _) }
%monad { P } { >>= } { return }
%lexer { (lexer True) } { L _ ITeof }
-- Replace 'lexer' above with 'lexerDbg'
-- to dump the tokens fed to the parser.
%tokentype { (Located Token) }
-- Exported parsers
%name parseModuleNoHaddock module
%name parseSignatureNoHaddock signature
%name parseImport importdecl
%name parseStatement e_stmt
%name parseDeclaration topdecl
%name parseExpression exp
%name parsePattern pat
%name parseTypeSignature sigdecl
%name parseStmt maybe_stmt
%name parseIdentifier identifier
%name parseType ktype
%name parseBackpack backpack
%partial parseHeader header
%%
-----------------------------------------------------------------------------
-- Identifiers; one of the entry points
identifier :: { LocatedN RdrName }
: qvar { $1 }
| qcon { $1 }
| qvarop { $1 }
| qconop { $1 }
| '->' {% amsr (sLL $1 $> $ getRdrName unrestrictedFunTyCon)
(NameAnnRArrow Nothing (epUniTok $1) Nothing []) }
-----------------------------------------------------------------------------
-- Backpack stuff
backpack :: { [LHsUnit PackageName] }
: implicit_top units close { fromOL $2 }
| '{' units '}' { fromOL $2 }
units :: { OrdList (LHsUnit PackageName) }
: units ';' unit { $1 `appOL` unitOL $3 }
| units ';' { $1 }
| unit { unitOL $1 }
unit :: { LHsUnit PackageName }
: 'unit' pkgname 'where' unitbody
{ sL1 $1 $ HsUnit { hsunitName = $2
, hsunitBody = fromOL $4 } }
unitid :: { LHsUnitId PackageName }
: pkgname { sL1 $1 $ HsUnitId $1 [] }
| pkgname '[' msubsts ']' { sLL $1 $> $ HsUnitId $1 (fromOL $3) }
msubsts :: { OrdList (LHsModuleSubst PackageName) }
: msubsts ',' msubst { $1 `appOL` unitOL $3 }
| msubsts ',' { $1 }
| msubst { unitOL $1 }
msubst :: { LHsModuleSubst PackageName }
: modid '=' moduleid { sLL $1 $> $ (reLoc $1, $3) }
| modid VARSYM modid VARSYM { sLL $1 $> $ (reLoc $1, sLL $2 $> $ HsModuleVar (reLoc $3)) }
moduleid :: { LHsModuleId PackageName }
: VARSYM modid VARSYM { sLL $1 $> $ HsModuleVar (reLoc $2) }
| unitid ':' modid { sLL $1 $> $ HsModuleId $1 (reLoc $3) }
pkgname :: { Located PackageName }
: STRING { sL1 $1 $ PackageName (mkFastStringShortText $ getSTRING $1) }
| litpkgname { sL1 $1 $ PackageName (unLoc $1) }
litpkgname_segment :: { Located FastString }
: VARID { sL1 $1 $ getVARID $1 }
| CONID { sL1 $1 $ getCONID $1 }
| special_id { $1 }
-- Parse a minus sign regardless of whether -XLexicalNegation is turned on or off.
-- See Note [Minus tokens] in GHC.Parser.Lexer
HYPHEN :: { () }
: '-' { () }
| PREFIX_MINUS { () }
| VARSYM { () }
litpkgname :: { Located FastString }
: litpkgname_segment { $1 }
-- a bit of a hack, means p - b is parsed same as p-b, enough for now.
| litpkgname_segment HYPHEN litpkgname { sLL $1 $> $ concatFS [unLoc $1, fsLit "-", (unLoc $3)] }
mayberns :: { Maybe [LRenaming] }
: {- empty -} { Nothing }
| '(' rns ')' { Just (fromOL $2) }
rns :: { OrdList LRenaming }
: rns ',' rn { $1 `appOL` unitOL $3 }
| rns ',' { $1 }
| rn { unitOL $1 }
rn :: { LRenaming }
: modid 'as' modid { sLL $1 $> $ Renaming (reLoc $1) (Just (reLoc $3)) }
| modid { sL1 $1 $ Renaming (reLoc $1) Nothing }
unitbody :: { OrdList (LHsUnitDecl PackageName) }
: '{' unitdecls '}' { $2 }
| vocurly unitdecls close { $2 }
unitdecls :: { OrdList (LHsUnitDecl PackageName) }
: unitdecls ';' unitdecl { $1 `appOL` unitOL $3 }
| unitdecls ';' { $1 }
| unitdecl { unitOL $1 }
unitdecl :: { LHsUnitDecl PackageName }
: 'module' maybe_src modid maybe_warning_pragma maybeexports 'where' body
-- XXX not accurate
{ sL1 $1 $ DeclD
(case snd $2 of
NotBoot -> HsSrcFile
IsBoot -> HsBootFile)
(reLoc $3)
(sL1 $1 (HsModule (XModulePs noAnn (thdOf3 $7) $4 Nothing) (Just $3) (snd $5) (fst $ sndOf3 $7) (snd $ sndOf3 $7))) }
| 'signature' modid maybe_warning_pragma maybeexports 'where' body
{ sL1 $1 $ DeclD
HsigFile
(reLoc $2)
(sL1 $1 (HsModule (XModulePs noAnn (thdOf3 $6) $3 Nothing) (Just $2) (snd $4) (fst $ sndOf3 $6) (snd $ sndOf3 $6))) }
| 'dependency' unitid mayberns
{ sL1 $1 $ IncludeD (IncludeDecl { idUnitId = $2
, idModRenaming = $3
, idSignatureInclude = False }) }
| 'dependency' 'signature' unitid
{ sL1 $1 $ IncludeD (IncludeDecl { idUnitId = $3
, idModRenaming = Nothing
, idSignatureInclude = True }) }
-----------------------------------------------------------------------------
-- Module Header
-- The place for module deprecation is really too restrictive, but if it
-- was allowed at its natural place just before 'module', we get an ugly
-- s/r conflict with the second alternative. Another solution would be the
-- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
-- either, and DEPRECATED is only expected to be used by people who really
-- know what they are doing. :-)
signature :: { Located (HsModule GhcPs) }
: 'signature' modid maybe_warning_pragma maybeexports 'where' body
{% fileSrcSpan >>= \ loc ->
acs loc (\loc cs-> (L loc (HsModule (XModulePs
(EpAnn (spanAsAnchor loc) (AnnsModule (epTok $1) noEpTok (epTok $5) (fst $4) (fstOf3 $6) [] Nothing) cs)
(thdOf3 $6) $3 Nothing)
(Just $2) (snd $4) (fst $ sndOf3 $6)
(snd $ sndOf3 $6)))
) }
module :: { Located (HsModule GhcPs) }
: 'module' modid maybe_warning_pragma maybeexports 'where' body
{% fileSrcSpan >>= \ loc ->
acsFinal (\cs eof -> (L loc (HsModule (XModulePs
(EpAnn (spanAsAnchor loc) (AnnsModule noEpTok (epTok $1) (epTok $5) (fst $4) (fstOf3 $6) [] eof) cs)
(thdOf3 $6) $3 Nothing)
(Just $2) (snd $4) (fst $ sndOf3 $6)
(snd $ sndOf3 $6))
)) }
| body2
{% fileSrcSpan >>= \ loc ->
acsFinal (\cs eof -> (L loc (HsModule (XModulePs
(EpAnn (spanAsAnchor loc) (AnnsModule noEpTok noEpTok noEpTok (noEpTok, noEpTok, []) (fstOf3 $1) [] eof) cs)
(thdOf3 $1) Nothing Nothing)
Nothing Nothing
(fst $ sndOf3 $1) (snd $ sndOf3 $1)))) }
missing_module_keyword :: { () }
: {- empty -} {% pushModuleContext }
implicit_top :: { () }
: {- empty -} {% pushModuleContext }
body :: { ([TrailingAnn]
,([LImportDecl GhcPs], [LHsDecl GhcPs])
,EpLayout) }
: '{' top '}' { (fst $2, snd $2, epExplicitBraces $1 $3) }
| vocurly top close { (fst $2, snd $2, EpVirtualBraces (getVOCURLY $1)) }
body2 :: { ([TrailingAnn]
,([LImportDecl GhcPs], [LHsDecl GhcPs])
,EpLayout) }
: '{' top '}' { (fst $2, snd $2, epExplicitBraces $1 $3) }
| missing_module_keyword top close { ([], snd $2, EpVirtualBraces leftmostColumn) }
top :: { ([TrailingAnn]
,([LImportDecl GhcPs], [LHsDecl GhcPs])) }
: semis top1 { (reverse $1, $2) }
top1 :: { ([LImportDecl GhcPs], [LHsDecl GhcPs]) }
: importdecls_semi topdecls_cs_semi { (reverse $1, cvTopDecls $2) }
| importdecls_semi topdecls_cs { (reverse $1, cvTopDecls $2) }
| importdecls { (reverse $1, []) }
-----------------------------------------------------------------------------
-- Module declaration & imports only
header :: { Located (HsModule GhcPs) }
: 'module' modid maybe_warning_pragma maybeexports 'where' header_body
{% fileSrcSpan >>= \ loc ->
acs loc (\loc cs -> (L loc (HsModule (XModulePs
(EpAnn (spanAsAnchor loc) (AnnsModule noEpTok (epTok $1) (epTok $5) (fst $4) [] [] Nothing) cs)
EpNoLayout $3 Nothing)
(Just $2) (snd $4) $6 []
))) }
| 'signature' modid maybe_warning_pragma maybeexports 'where' header_body
{% fileSrcSpan >>= \ loc ->
acs loc (\loc cs -> (L loc (HsModule (XModulePs
(EpAnn (spanAsAnchor loc) (AnnsModule noEpTok (epTok $1) (epTok $5) (fst $4) [] [] Nothing) cs)
EpNoLayout $3 Nothing)
(Just $2) (snd $4) $6 []
))) }
| header_body2
{% fileSrcSpan >>= \ loc ->
return (L loc (HsModule (XModulePs noAnn EpNoLayout Nothing Nothing) Nothing Nothing $1 [])) }
header_body :: { [LImportDecl GhcPs] }
: '{' header_top { $2 }
| vocurly header_top { $2 }
header_body2 :: { [LImportDecl GhcPs] }
: '{' header_top { $2 }
| missing_module_keyword header_top { $2 }
header_top :: { [LImportDecl GhcPs] }
: semis header_top_importdecls { $2 }
header_top_importdecls :: { [LImportDecl GhcPs] }
: importdecls_semi { $1 }