2 -- ---------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1997-2003
7 -- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -- ---------------------------------------------------------------------------
11 {-# OPTIONS -Wwarn -w #-}
12 -- The above warning supression flag is a temporary kludge.
13 -- While working on this module you are encouraged to remove it and fix
14 -- any warnings in the module. See
15 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
18 {-# OPTIONS_GHC -O0 -fno-ignore-interface-pragmas #-}
20 Careful optimisation of the parser: we don't want to throw everything
21 at it, because that takes too long and doesn't buy much, but we do want
22 to inline certain key external functions, so we instruct GHC not to
23 throw away inlinings as it would normally do in -O0 mode.
26 module Parser ( parseModule, parseStmt, parseIdentifier, parseType,
31 import HscTypes ( IsBootInterface, WarningTxt(..) )
34 import TysWiredIn ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
35 unboxedSingletonTyCon, unboxedSingletonDataCon,
36 listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
37 import Type ( funTyCon )
38 import ForeignCall ( Safety(..), CExportSpec(..), CLabelString,
39 CCallConv(..), CCallTarget(..), defaultCCallConv
41 import OccName ( varName, dataName, tcClsName, tvName )
42 import DataCon ( DataCon, dataConName )
43 import SrcLoc ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
44 SrcSpan, combineLocs, srcLocFile,
47 import StaticFlags ( opt_SccProfilingOn, opt_Hpc )
48 import Type ( Kind, liftedTypeKind, unliftedTypeKind )
49 import Coercion ( mkArrowKind )
50 import Class ( FunDep )
51 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
52 Activation(..), RuleMatchInfo(..), defaultInlinePragma )
58 import Maybes ( orElse )
61 import Control.Monad ( unless )
64 import Control.Monad ( mplus )
68 -----------------------------------------------------------------------------
71 Conflicts: 33 shift/reduce
74 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
75 would think the two should never occur in the same context.
79 -----------------------------------------------------------------------------
82 Conflicts: 34 shift/reduce
85 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
86 would think the two should never occur in the same context.
90 -----------------------------------------------------------------------------
93 Conflicts: 32 shift/reduce
96 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
97 would think the two should never occur in the same context.
101 -----------------------------------------------------------------------------
104 Conflicts: 37 shift/reduce
107 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
108 would think the two should never occur in the same context.
112 -----------------------------------------------------------------------------
113 Conflicts: 38 shift/reduce (1.25)
115 10 for abiguity in 'if x then y else z + 1' [State 178]
116 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
117 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
119 1 for ambiguity in 'if x then y else z :: T' [State 178]
120 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
122 4 for ambiguity in 'if x then y else z -< e' [State 178]
123 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
124 There are four such operators: -<, >-, -<<, >>-
127 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
128 Which of these two is intended?
130 (x::T) -> T -- Rhs is T
133 (x::T -> T) -> .. -- Rhs is ...
135 10 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
138 As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
139 Same duplication between states 11 and 253 as the previous case
141 1 for ambiguity in 'let ?x ...' [State 329]
142 the parser can't tell whether the ?x is the lhs of a normal binding or
143 an implicit binding. Fortunately resolving as shift gives it the only
144 sensible meaning, namely the lhs of an implicit binding.
146 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
147 we don't know whether the '[' starts the activation or not: it
148 might be the start of the declaration with the activation being
149 empty. --SDM 1/4/2002
151 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
152 since 'forall' is a valid variable name, we don't know whether
153 to treat a forall on the input as the beginning of a quantifier
154 or the beginning of the rule itself. Resolving to shift means
155 it's always treated as a quantifier, hence the above is disallowed.
156 This saves explicitly defining a grammar for the rule lhs that
157 doesn't include 'forall'.
159 1 for ambiguity when the source file starts with "-- | doc". We need another
160 token of lookahead to determine if a top declaration or the 'module' keyword
161 follows. Shift parses as if the 'module' keyword follows.
163 -- ---------------------------------------------------------------------------
164 -- Adding location info
166 This is done in a stylised way using the three macros below, L0, L1
167 and LL. Each of these macros can be thought of as having type
169 L0, L1, LL :: a -> Located a
171 They each add a SrcSpan to their argument.
173 L0 adds 'noSrcSpan', used for empty productions
174 -- This doesn't seem to work anymore -=chak
176 L1 for a production with a single token on the lhs. Grabs the SrcSpan
179 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
180 the first and last tokens.
182 These suffice for the majority of cases. However, we must be
183 especially careful with empty productions: LL won't work if the first
184 or last token on the lhs can represent an empty span. In these cases,
185 we have to calculate the span using more of the tokens from the lhs, eg.
187 | 'newtype' tycl_hdr '=' newconstr deriving
189 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
191 We provide comb3 and comb4 functions which are useful in such cases.
193 Be careful: there's no checking that you actually got this right, the
194 only symptom will be that the SrcSpans of your syntax will be
198 * We must expand these macros *before* running Happy, which is why this file is
199 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
201 #define L0 L noSrcSpan
202 #define L1 sL (getLoc $1)
203 #define LL sL (comb2 $1 $>)
205 -- -----------------------------------------------------------------------------
210 '_' { L _ ITunderscore } -- Haskell keywords
212 'case' { L _ ITcase }
213 'class' { L _ ITclass }
214 'data' { L _ ITdata }
215 'default' { L _ ITdefault }
216 'deriving' { L _ ITderiving }
218 'else' { L _ ITelse }
219 'hiding' { L _ IThiding }
221 'import' { L _ ITimport }
223 'infix' { L _ ITinfix }
224 'infixl' { L _ ITinfixl }
225 'infixr' { L _ ITinfixr }
226 'instance' { L _ ITinstance }
228 'module' { L _ ITmodule }
229 'newtype' { L _ ITnewtype }
231 'qualified' { L _ ITqualified }
232 'then' { L _ ITthen }
233 'type' { L _ ITtype }
234 'where' { L _ ITwhere }
235 '_scc_' { L _ ITscc } -- ToDo: remove
237 'forall' { L _ ITforall } -- GHC extension keywords
238 'foreign' { L _ ITforeign }
239 'export' { L _ ITexport }
240 'label' { L _ ITlabel }
241 'dynamic' { L _ ITdynamic }
242 'safe' { L _ ITsafe }
243 'threadsafe' { L _ ITthreadsafe } -- ToDo: remove deprecated alias
244 'unsafe' { L _ ITunsafe }
246 'family' { L _ ITfamily }
247 'stdcall' { L _ ITstdcallconv }
248 'ccall' { L _ ITccallconv }
249 'prim' { L _ ITprimcallconv }
250 'proc' { L _ ITproc } -- for arrow notation extension
251 'rec' { L _ ITrec } -- for arrow notation extension
252 'group' { L _ ITgroup } -- for list transform extension
253 'by' { L _ ITby } -- for list transform extension
254 'using' { L _ ITusing } -- for list transform extension
256 '{-# INLINE' { L _ (ITinline_prag _) }
257 '{-# INLINE_CONLIKE' { L _ (ITinline_conlike_prag _) }
258 '{-# SPECIALISE' { L _ ITspec_prag }
259 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
260 '{-# SOURCE' { L _ ITsource_prag }
261 '{-# RULES' { L _ ITrules_prag }
262 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
263 '{-# SCC' { L _ ITscc_prag }
264 '{-# GENERATED' { L _ ITgenerated_prag }
265 '{-# DEPRECATED' { L _ ITdeprecated_prag }
266 '{-# WARNING' { L _ ITwarning_prag }
267 '{-# UNPACK' { L _ ITunpack_prag }
268 '{-# ANN' { L _ ITann_prag }
269 '#-}' { L _ ITclose_prag }
271 '..' { L _ ITdotdot } -- reserved symbols
273 '::' { L _ ITdcolon }
277 '<-' { L _ ITlarrow }
278 '->' { L _ ITrarrow }
281 '=>' { L _ ITdarrow }
285 '-<' { L _ ITlarrowtail } -- for arrow notation
286 '>-' { L _ ITrarrowtail } -- for arrow notation
287 '-<<' { L _ ITLarrowtail } -- for arrow notation
288 '>>-' { L _ ITRarrowtail } -- for arrow notation
291 '{' { L _ ITocurly } -- special symbols
293 '{|' { L _ ITocurlybar }
294 '|}' { L _ ITccurlybar }
295 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
296 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
299 '[:' { L _ ITopabrack }
300 ':]' { L _ ITcpabrack }
303 '(#' { L _ IToubxparen }
304 '#)' { L _ ITcubxparen }
305 '(|' { L _ IToparenbar }
306 '|)' { L _ ITcparenbar }
309 '`' { L _ ITbackquote }
311 VARID { L _ (ITvarid _) } -- identifiers
312 CONID { L _ (ITconid _) }
313 VARSYM { L _ (ITvarsym _) }
314 CONSYM { L _ (ITconsym _) }
315 QVARID { L _ (ITqvarid _) }
316 QCONID { L _ (ITqconid _) }
317 QVARSYM { L _ (ITqvarsym _) }
318 QCONSYM { L _ (ITqconsym _) }
319 PREFIXQVARSYM { L _ (ITprefixqvarsym _) }
320 PREFIXQCONSYM { L _ (ITprefixqconsym _) }
322 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
324 CHAR { L _ (ITchar _) }
325 STRING { L _ (ITstring _) }
326 INTEGER { L _ (ITinteger _) }
327 RATIONAL { L _ (ITrational _) }
329 PRIMCHAR { L _ (ITprimchar _) }
330 PRIMSTRING { L _ (ITprimstring _) }
331 PRIMINTEGER { L _ (ITprimint _) }
332 PRIMWORD { L _ (ITprimword _) }
333 PRIMFLOAT { L _ (ITprimfloat _) }
334 PRIMDOUBLE { L _ (ITprimdouble _) }
336 DOCNEXT { L _ (ITdocCommentNext _) }
337 DOCPREV { L _ (ITdocCommentPrev _) }
338 DOCNAMED { L _ (ITdocCommentNamed _) }
339 DOCSECTION { L _ (ITdocSection _ _) }
342 '[|' { L _ ITopenExpQuote }
343 '[p|' { L _ ITopenPatQuote }
344 '[t|' { L _ ITopenTypQuote }
345 '[d|' { L _ ITopenDecQuote }
346 '|]' { L _ ITcloseQuote }
347 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
348 '$(' { L _ ITparenEscape } -- $( exp )
349 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
350 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
351 TH_QUASIQUOTE { L _ (ITquasiQuote _) }
353 %monad { P } { >>= } { return }
354 %lexer { lexer } { L _ ITeof }
355 %name parseModule module
356 %name parseStmt maybe_stmt
357 %name parseIdentifier identifier
358 %name parseType ctype
359 %partial parseHeader header
360 %tokentype { (Located Token) }
363 -----------------------------------------------------------------------------
364 -- Identifiers; one of the entry points
365 identifier :: { Located RdrName }
370 | '(' '->' ')' { LL $ getRdrName funTyCon }
372 -----------------------------------------------------------------------------
375 -- The place for module deprecation is really too restrictive, but if it
376 -- was allowed at its natural place just before 'module', we get an ugly
377 -- s/r conflict with the second alternative. Another solution would be the
378 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
379 -- either, and DEPRECATED is only expected to be used by people who really
380 -- know what they are doing. :-)
382 module :: { Located (HsModule RdrName) }
383 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' body
384 {% fileSrcSpan >>= \ loc ->
385 return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4 $1
388 {% fileSrcSpan >>= \ loc ->
389 return (L loc (HsModule Nothing Nothing
390 (fst $1) (snd $1) Nothing Nothing
393 maybedocheader :: { Maybe LHsDocString }
394 : moduleheader { $1 }
395 | {- empty -} { Nothing }
397 missing_module_keyword :: { () }
398 : {- empty -} {% pushCurrentContext }
400 maybemodwarning :: { Maybe WarningTxt }
401 : '{-# DEPRECATED' strings '#-}' { Just (DeprecatedTxt $ unLoc $2) }
402 | '{-# WARNING' strings '#-}' { Just (WarningTxt $ unLoc $2) }
403 | {- empty -} { Nothing }
405 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
407 | vocurly top close { $2 }
409 body2 :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
411 | missing_module_keyword top close { $2 }
413 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
414 : importdecls { (reverse $1,[]) }
415 | importdecls ';' cvtopdecls { (reverse $1,$3) }
416 | cvtopdecls { ([],$1) }
418 cvtopdecls :: { [LHsDecl RdrName] }
419 : topdecls { cvTopDecls $1 }
421 -----------------------------------------------------------------------------
422 -- Module declaration & imports only
424 header :: { Located (HsModule RdrName) }
425 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' header_body
426 {% fileSrcSpan >>= \ loc ->
427 return (L loc (HsModule (Just $3) $5 $7 [] $4 $1
429 | missing_module_keyword importdecls
430 {% fileSrcSpan >>= \ loc ->
431 return (L loc (HsModule Nothing Nothing $2 [] Nothing
434 header_body :: { [LImportDecl RdrName] }
435 : '{' importdecls { $2 }
436 | vocurly importdecls { $2 }
438 -----------------------------------------------------------------------------
441 maybeexports :: { Maybe [LIE RdrName] }
442 : '(' exportlist ')' { Just $2 }
443 | {- empty -} { Nothing }
445 exportlist :: { [LIE RdrName] }
446 : expdoclist ',' expdoclist { $1 ++ $3 }
449 exportlist1 :: { [LIE RdrName] }
450 : expdoclist export expdoclist ',' exportlist { $1 ++ ($2 : $3) ++ $5 }
451 | expdoclist export expdoclist { $1 ++ ($2 : $3) }
454 expdoclist :: { [LIE RdrName] }
455 : exp_doc expdoclist { $1 : $2 }
458 exp_doc :: { LIE RdrName }
459 : docsection { L1 (case (unLoc $1) of (n, doc) -> IEGroup n doc) }
460 | docnamed { L1 (IEDocNamed ((fst . unLoc) $1)) }
461 | docnext { L1 (IEDoc (unLoc $1)) }
463 -- No longer allow things like [] and (,,,) to be exported
464 -- They are built in syntax, always available
465 export :: { LIE RdrName }
466 : qvar { L1 (IEVar (unLoc $1)) }
467 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
468 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
469 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
470 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
471 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
473 qcnames :: { [RdrName] }
474 : qcnames ',' qcname_ext { unLoc $3 : $1 }
475 | qcname_ext { [unLoc $1] }
477 qcname_ext :: { Located RdrName } -- Variable or data constructor
478 -- or tagged type constructor
480 | 'type' qcon { sL (comb2 $1 $2)
481 (setRdrNameSpace (unLoc $2)
484 -- Cannot pull into qcname_ext, as qcname is also used in expression.
485 qcname :: { Located RdrName } -- Variable or data constructor
489 -----------------------------------------------------------------------------
490 -- Import Declarations
492 -- import decls can be *empty*, or even just a string of semicolons
493 -- whereas topdecls must contain at least one topdecl.
495 importdecls :: { [LImportDecl RdrName] }
496 : importdecls ';' importdecl { $3 : $1 }
497 | importdecls ';' { $1 }
498 | importdecl { [ $1 ] }
501 importdecl :: { LImportDecl RdrName }
502 : 'import' maybe_src optqualified maybe_pkg modid maybeas maybeimpspec
503 { L (comb4 $1 $5 $6 $7) (ImportDecl $5 $4 $2 $3 (unLoc $6) (unLoc $7)) }
505 maybe_src :: { IsBootInterface }
506 : '{-# SOURCE' '#-}' { True }
507 | {- empty -} { False }
509 maybe_pkg :: { Maybe FastString }
510 : STRING { Just (getSTRING $1) }
511 | {- empty -} { Nothing }
513 optqualified :: { Bool }
514 : 'qualified' { True }
515 | {- empty -} { False }
517 maybeas :: { Located (Maybe ModuleName) }
518 : 'as' modid { LL (Just (unLoc $2)) }
519 | {- empty -} { noLoc Nothing }
521 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
522 : impspec { L1 (Just (unLoc $1)) }
523 | {- empty -} { noLoc Nothing }
525 impspec :: { Located (Bool, [LIE RdrName]) }
526 : '(' exportlist ')' { LL (False, $2) }
527 | 'hiding' '(' exportlist ')' { LL (True, $3) }
529 -----------------------------------------------------------------------------
530 -- Fixity Declarations
534 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
536 infix :: { Located FixityDirection }
537 : 'infix' { L1 InfixN }
538 | 'infixl' { L1 InfixL }
539 | 'infixr' { L1 InfixR }
541 ops :: { Located [Located RdrName] }
542 : ops ',' op { LL ($3 : unLoc $1) }
545 -----------------------------------------------------------------------------
546 -- Top-Level Declarations
548 topdecls :: { OrdList (LHsDecl RdrName) }
549 : topdecls ';' topdecl { $1 `appOL` $3 }
550 | topdecls ';' { $1 }
553 topdecl :: { OrdList (LHsDecl RdrName) }
554 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
555 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
556 | 'instance' inst_type where_inst
557 { let (binds, sigs, ats, _) = cvBindsAndSigs (unLoc $3)
559 unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs ats)))}
560 | stand_alone_deriving { unitOL (LL (DerivD (unLoc $1))) }
561 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
562 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
563 | '{-# DEPRECATED' deprecations '#-}' { $2 }
564 | '{-# WARNING' warnings '#-}' { $2 }
565 | '{-# RULES' rules '#-}' { $2 }
566 | annotation { unitOL $1 }
569 -- Template Haskell Extension
570 -- The $(..) form is one possible form of infixexp
571 -- but we treat an arbitrary expression just as if
572 -- it had a $(..) wrapped around it
573 | infixexp { unitOL (LL $ mkTopSpliceDecl $1) }
577 cl_decl :: { LTyClDecl RdrName }
578 : 'class' tycl_hdr fds where_cls {% mkClassDecl (comb4 $1 $2 $3 $4) $2 $3 $4 }
580 -- Type declarations (toplevel)
582 ty_decl :: { LTyClDecl RdrName }
583 -- ordinary type synonyms
584 : 'type' type '=' ctypedoc
585 -- Note ctype, not sigtype, on the right of '='
586 -- We allow an explicit for-all but we don't insert one
587 -- in type Foo a = (b,b)
588 -- Instead we just say b is out of scope
590 -- Note the use of type for the head; this allows
591 -- infix type constructors to be declared
592 {% mkTySynonym (comb2 $1 $4) False $2 $4 }
594 -- type family declarations
595 | 'type' 'family' type opt_kind_sig
596 -- Note the use of type for the head; this allows
597 -- infix type constructors to be declared
598 {% mkTyFamily (comb3 $1 $3 $4) TypeFamily $3 (unLoc $4) }
600 -- type instance declarations
601 | 'type' 'instance' type '=' ctype
602 -- Note the use of type for the head; this allows
603 -- infix type constructors and type patterns
604 {% mkTySynonym (comb2 $1 $5) True $3 $5 }
606 -- ordinary data type or newtype declaration
607 | data_or_newtype tycl_hdr constrs deriving
608 {% mkTyData (comb4 $1 $2 $3 $4) (unLoc $1) False $2
609 Nothing (reverse (unLoc $3)) (unLoc $4) }
610 -- We need the location on tycl_hdr in case
611 -- constrs and deriving are both empty
613 -- ordinary GADT declaration
614 | data_or_newtype tycl_hdr opt_kind_sig
617 {% mkTyData (comb4 $1 $2 $4 $5) (unLoc $1) False $2
618 (unLoc $3) (unLoc $4) (unLoc $5) }
619 -- We need the location on tycl_hdr in case
620 -- constrs and deriving are both empty
622 -- data/newtype family
623 | 'data' 'family' type opt_kind_sig
624 {% mkTyFamily (comb3 $1 $2 $4) DataFamily $3 (unLoc $4) }
626 -- data/newtype instance declaration
627 | data_or_newtype 'instance' tycl_hdr constrs deriving
628 {% mkTyData (comb4 $1 $3 $4 $5) (unLoc $1) True $3
629 Nothing (reverse (unLoc $4)) (unLoc $5) }
631 -- GADT instance declaration
632 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
635 {% mkTyData (comb4 $1 $3 $5 $6) (unLoc $1) True $3
636 (unLoc $4) (unLoc $5) (unLoc $6) }
638 -- Associated type family declarations
640 -- * They have a different syntax than on the toplevel (no family special
643 -- * They also need to be separate from instances; otherwise, data family
644 -- declarations without a kind signature cause parsing conflicts with empty
645 -- data declarations.
647 at_decl_cls :: { LTyClDecl RdrName }
648 -- type family declarations
649 : 'type' type opt_kind_sig
650 -- Note the use of type for the head; this allows
651 -- infix type constructors to be declared
652 {% mkTyFamily (comb3 $1 $2 $3) TypeFamily $2 (unLoc $3) }
654 -- default type instance
655 | 'type' type '=' ctype
656 -- Note the use of type for the head; this allows
657 -- infix type constructors and type patterns
658 {% mkTySynonym (comb2 $1 $4) True $2 $4 }
660 -- data/newtype family declaration
661 | 'data' type opt_kind_sig
662 {% mkTyFamily (comb3 $1 $2 $3) DataFamily $2 (unLoc $3) }
664 -- Associated type instances
666 at_decl_inst :: { LTyClDecl RdrName }
667 -- type instance declarations
668 : 'type' type '=' ctype
669 -- Note the use of type for the head; this allows
670 -- infix type constructors and type patterns
671 {% mkTySynonym (comb2 $1 $4) True $2 $4 }
673 -- data/newtype instance declaration
674 | data_or_newtype tycl_hdr constrs deriving
675 {% mkTyData (comb4 $1 $2 $3 $4) (unLoc $1) True $2
676 Nothing (reverse (unLoc $3)) (unLoc $4) }
678 -- GADT instance declaration
679 | data_or_newtype tycl_hdr opt_kind_sig
682 {% mkTyData (comb4 $1 $2 $4 $5) (unLoc $1) True $2
683 (unLoc $3) (unLoc $4) (unLoc $5) }
685 data_or_newtype :: { Located NewOrData }
686 : 'data' { L1 DataType }
687 | 'newtype' { L1 NewType }
689 opt_kind_sig :: { Located (Maybe Kind) }
691 | '::' kind { LL (Just (unLoc $2)) }
693 -- tycl_hdr parses the header of a class or data type decl,
694 -- which takes the form
697 -- (Eq a, Ord b) => T a b
698 -- T Int [a] -- for associated types
699 -- Rather a lot of inlining here, else we get reduce/reduce errors
700 tycl_hdr :: { Located (LHsContext RdrName, LHsType RdrName) }
701 : context '=>' type { LL ($1, $3) }
702 | type { L1 (noLoc [], $1) }
704 -----------------------------------------------------------------------------
705 -- Stand-alone deriving
707 -- Glasgow extension: stand-alone deriving declarations
708 stand_alone_deriving :: { LDerivDecl RdrName }
709 : 'deriving' 'instance' inst_type { LL (DerivDecl $3) }
711 -----------------------------------------------------------------------------
712 -- Nested declarations
714 -- Declaration in class bodies
716 decl_cls :: { Located (OrdList (LHsDecl RdrName)) }
717 decl_cls : at_decl_cls { LL (unitOL (L1 (TyClD (unLoc $1)))) }
720 decls_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
721 : decls_cls ';' decl_cls { LL (unLoc $1 `appOL` unLoc $3) }
722 | decls_cls ';' { LL (unLoc $1) }
724 | {- empty -} { noLoc nilOL }
728 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
729 : '{' decls_cls '}' { LL (unLoc $2) }
730 | vocurly decls_cls close { $2 }
734 where_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
735 -- No implicit parameters
736 -- May have type declarations
737 : 'where' decllist_cls { LL (unLoc $2) }
738 | {- empty -} { noLoc nilOL }
740 -- Declarations in instance bodies
742 decl_inst :: { Located (OrdList (LHsDecl RdrName)) }
743 decl_inst : at_decl_inst { LL (unitOL (L1 (TyClD (unLoc $1)))) }
746 decls_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
747 : decls_inst ';' decl_inst { LL (unLoc $1 `appOL` unLoc $3) }
748 | decls_inst ';' { LL (unLoc $1) }
750 | {- empty -} { noLoc nilOL }
753 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
754 : '{' decls_inst '}' { LL (unLoc $2) }
755 | vocurly decls_inst close { $2 }
759 where_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
760 -- No implicit parameters
761 -- May have type declarations
762 : 'where' decllist_inst { LL (unLoc $2) }
763 | {- empty -} { noLoc nilOL }
765 -- Declarations in binding groups other than classes and instances
767 decls :: { Located (OrdList (LHsDecl RdrName)) }
768 : decls ';' decl { let { this = unLoc $3;
770 these = rest `appOL` this }
771 in rest `seq` this `seq` these `seq`
773 | decls ';' { LL (unLoc $1) }
775 | {- empty -} { noLoc nilOL }
777 decllist :: { Located (OrdList (LHsDecl RdrName)) }
778 : '{' decls '}' { LL (unLoc $2) }
779 | vocurly decls close { $2 }
781 -- Binding groups other than those of class and instance declarations
783 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
784 -- No type declarations
785 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
786 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
787 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
789 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
790 -- No type declarations
791 : 'where' binds { LL (unLoc $2) }
792 | {- empty -} { noLoc emptyLocalBinds }
795 -----------------------------------------------------------------------------
796 -- Transformation Rules
798 rules :: { OrdList (LHsDecl RdrName) }
799 : rules ';' rule { $1 `snocOL` $3 }
802 | {- empty -} { nilOL }
804 rule :: { LHsDecl RdrName }
805 : STRING activation rule_forall infixexp '=' exp
806 { LL $ RuleD (HsRule (getSTRING $1)
807 ($2 `orElse` AlwaysActive)
808 $3 $4 placeHolderNames $6 placeHolderNames) }
810 activation :: { Maybe Activation }
811 : {- empty -} { Nothing }
812 | explicit_activation { Just $1 }
814 explicit_activation :: { Activation } -- In brackets
815 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
816 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
818 rule_forall :: { [RuleBndr RdrName] }
819 : 'forall' rule_var_list '.' { $2 }
822 rule_var_list :: { [RuleBndr RdrName] }
824 | rule_var rule_var_list { $1 : $2 }
826 rule_var :: { RuleBndr RdrName }
827 : varid { RuleBndr $1 }
828 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
830 -----------------------------------------------------------------------------
831 -- Warnings and deprecations (c.f. rules)
833 warnings :: { OrdList (LHsDecl RdrName) }
834 : warnings ';' warning { $1 `appOL` $3 }
835 | warnings ';' { $1 }
837 | {- empty -} { nilOL }
839 -- SUP: TEMPORARY HACK, not checking for `module Foo'
840 warning :: { OrdList (LHsDecl RdrName) }
842 { toOL [ LL $ WarningD (Warning n (WarningTxt $ unLoc $2))
845 deprecations :: { OrdList (LHsDecl RdrName) }
846 : deprecations ';' deprecation { $1 `appOL` $3 }
847 | deprecations ';' { $1 }
849 | {- empty -} { nilOL }
851 -- SUP: TEMPORARY HACK, not checking for `module Foo'
852 deprecation :: { OrdList (LHsDecl RdrName) }
854 { toOL [ LL $ WarningD (Warning n (DeprecatedTxt $ unLoc $2))
857 strings :: { Located [FastString] }
858 : STRING { L1 [getSTRING $1] }
859 | '[' stringlist ']' { LL $ fromOL (unLoc $2) }
861 stringlist :: { Located (OrdList FastString) }
862 : stringlist ',' STRING { LL (unLoc $1 `snocOL` getSTRING $3) }
863 | STRING { LL (unitOL (getSTRING $1)) }
865 -----------------------------------------------------------------------------
867 annotation :: { LHsDecl RdrName }
868 : '{-# ANN' name_var aexp '#-}' { LL (AnnD $ HsAnnotation (ValueAnnProvenance (unLoc $2)) $3) }
869 | '{-# ANN' 'type' tycon aexp '#-}' { LL (AnnD $ HsAnnotation (TypeAnnProvenance (unLoc $3)) $4) }
870 | '{-# ANN' 'module' aexp '#-}' { LL (AnnD $ HsAnnotation ModuleAnnProvenance $3) }
873 -----------------------------------------------------------------------------
874 -- Foreign import and export declarations
876 fdecl :: { LHsDecl RdrName }
877 fdecl : 'import' callconv safety fspec
878 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
879 | 'import' callconv fspec
880 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
882 | 'export' callconv fspec
883 {% mkExport $2 (unLoc $3) >>= return.LL }
885 callconv :: { CCallConv }
886 : 'stdcall' { StdCallConv }
887 | 'ccall' { CCallConv }
888 | 'prim' { PrimCallConv}
891 : 'unsafe' { PlayRisky }
892 | 'safe' { PlaySafe False }
893 | 'threadsafe' { PlaySafe True } -- deprecated alias
895 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
896 : STRING var '::' sigtypedoc { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
897 | var '::' sigtypedoc { LL (noLoc nilFS, $1, $3) }
898 -- if the entity string is missing, it defaults to the empty string;
899 -- the meaning of an empty entity string depends on the calling
902 -----------------------------------------------------------------------------
905 opt_sig :: { Maybe (LHsType RdrName) }
906 : {- empty -} { Nothing }
907 | '::' sigtype { Just $2 }
909 opt_asig :: { Maybe (LHsType RdrName) }
910 : {- empty -} { Nothing }
911 | '::' atype { Just $2 }
913 sigtype :: { LHsType RdrName } -- Always a HsForAllTy,
914 -- to tell the renamer where to generalise
915 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
916 -- Wrap an Implicit forall if there isn't one there already
918 sigtypedoc :: { LHsType RdrName } -- Always a HsForAllTy
919 : ctypedoc { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
920 -- Wrap an Implicit forall if there isn't one there already
922 sig_vars :: { Located [Located RdrName] }
923 : sig_vars ',' var { LL ($3 : unLoc $1) }
926 sigtypes1 :: { [LHsType RdrName] } -- Always HsForAllTys
928 | sigtype ',' sigtypes1 { $1 : $3 }
930 -----------------------------------------------------------------------------
933 infixtype :: { LHsType RdrName }
934 : btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
935 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
937 strict_mark :: { Located HsBang }
938 : '!' { L1 HsStrict }
939 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
941 -- A ctype is a for-all type
942 ctype :: { LHsType RdrName }
943 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
944 | context '=>' ctype { LL $ mkImplicitHsForAllTy $1 $3 }
945 -- A type of form (context => type) is an *implicit* HsForAllTy
946 | ipvar '::' type { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
949 ----------------------
950 -- Notes for 'ctypedoc'
951 -- It would have been nice to simplify the grammar by unifying `ctype` and
952 -- ctypedoc` into one production, allowing comments on types everywhere (and
953 -- rejecting them after parsing, where necessary). This is however not possible
954 -- since it leads to ambiguity. The reason is the support for comments on record
956 -- data R = R { field :: Int -- ^ comment on the field }
957 -- If we allow comments on types here, it's not clear if the comment applies
958 -- to 'field' or to 'Int'. So we must use `ctype` to describe the type.
960 ctypedoc :: { LHsType RdrName }
961 : 'forall' tv_bndrs '.' ctypedoc { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
962 | context '=>' ctypedoc { LL $ mkImplicitHsForAllTy $1 $3 }
963 -- A type of form (context => type) is an *implicit* HsForAllTy
964 | ipvar '::' type { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
967 ----------------------
968 -- Notes for 'context'
969 -- We parse a context as a btype so that we don't get reduce/reduce
970 -- errors in ctype. The basic problem is that
972 -- looks so much like a tuple type. We can't tell until we find the =>
974 -- We have the t1 ~ t2 form both in 'context' and in type,
975 -- to permit an individual equational constraint without parenthesis.
976 -- Thus for some reason we allow f :: a~b => blah
977 -- but not f :: ?x::Int => blah
978 context :: { LHsContext RdrName }
979 : btype '~' btype {% checkContext
980 (LL $ HsPredTy (HsEqualP $1 $3)) }
981 | btype {% checkContext $1 }
983 type :: { LHsType RdrName }
985 | btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
986 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
987 | btype '->' ctype { LL $ HsFunTy $1 $3 }
988 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
990 typedoc :: { LHsType RdrName }
992 | btype docprev { LL $ HsDocTy $1 $2 }
993 | btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
994 | btype qtyconop type docprev { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
995 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
996 | btype tyvarop type docprev { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
997 | btype '->' ctypedoc { LL $ HsFunTy $1 $3 }
998 | btype docprev '->' ctypedoc { LL $ HsFunTy (L (comb2 $1 $2) (HsDocTy $1 $2)) $4 }
999 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1001 btype :: { LHsType RdrName }
1002 : btype atype { LL $ HsAppTy $1 $2 }
1005 atype :: { LHsType RdrName }
1006 : gtycon { L1 (HsTyVar (unLoc $1)) }
1007 | tyvar { L1 (HsTyVar (unLoc $1)) }
1008 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) } -- Constructor sigs only
1009 | '{' fielddecls '}' { LL $ HsRecTy $2 } -- Constructor sigs only
1010 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
1011 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
1012 | '[' ctype ']' { LL $ HsListTy $2 }
1013 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
1014 | '(' ctype ')' { LL $ HsParTy $2 }
1015 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
1016 | quasiquote { L1 (HsQuasiQuoteTy (unLoc $1)) }
1017 | '$(' exp ')' { LL $ HsSpliceTy (mkHsSplice $2 ) }
1018 | TH_ID_SPLICE { LL $ HsSpliceTy (mkHsSplice
1019 (L1 $ HsVar (mkUnqual varName
1020 (getTH_ID_SPLICE $1)))) } -- $x
1023 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
1025 -- An inst_type is what occurs in the head of an instance decl
1026 -- e.g. (Foo a, Gaz b) => Wibble a b
1027 -- It's kept as a single type, with a MonoDictTy at the right
1028 -- hand corner, for convenience.
1029 inst_type :: { LHsType RdrName }
1030 : sigtype {% checkInstType $1 }
1032 inst_types1 :: { [LHsType RdrName] }
1033 : inst_type { [$1] }
1034 | inst_type ',' inst_types1 { $1 : $3 }
1036 comma_types0 :: { [LHsType RdrName] }
1037 : comma_types1 { $1 }
1038 | {- empty -} { [] }
1040 comma_types1 :: { [LHsType RdrName] }
1042 | ctype ',' comma_types1 { $1 : $3 }
1044 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1045 : tv_bndr tv_bndrs { $1 : $2 }
1046 | {- empty -} { [] }
1048 tv_bndr :: { LHsTyVarBndr RdrName }
1049 : tyvar { L1 (UserTyVar (unLoc $1)) }
1050 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
1053 fds :: { Located [Located (FunDep RdrName)] }
1054 : {- empty -} { noLoc [] }
1055 | '|' fds1 { LL (reverse (unLoc $2)) }
1057 fds1 :: { Located [Located (FunDep RdrName)] }
1058 : fds1 ',' fd { LL ($3 : unLoc $1) }
1061 fd :: { Located (FunDep RdrName) }
1062 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
1063 (reverse (unLoc $1), reverse (unLoc $3)) }
1065 varids0 :: { Located [RdrName] }
1066 : {- empty -} { noLoc [] }
1067 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
1069 -----------------------------------------------------------------------------
1072 kind :: { Located Kind }
1074 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1076 akind :: { Located Kind }
1077 : '*' { L1 liftedTypeKind }
1078 | '!' { L1 unliftedTypeKind }
1079 | '(' kind ')' { LL (unLoc $2) }
1082 -----------------------------------------------------------------------------
1083 -- Datatype declarations
1085 gadt_constrlist :: { Located [LConDecl RdrName] } -- Returned in order
1086 : 'where' '{' gadt_constrs '}' { L (comb2 $1 $3) (unLoc $3) }
1087 | 'where' vocurly gadt_constrs close { L (comb2 $1 $3) (unLoc $3) }
1088 | {- empty -} { noLoc [] }
1090 gadt_constrs :: { Located [LConDecl RdrName] }
1091 : gadt_constr ';' gadt_constrs { L (comb2 (head $1) $3) ($1 ++ unLoc $3) }
1092 | gadt_constr { L (getLoc (head $1)) $1 }
1093 | {- empty -} { noLoc [] }
1095 -- We allow the following forms:
1096 -- C :: Eq a => a -> T a
1097 -- C :: forall a. Eq a => !a -> T a
1098 -- D { x,y :: a } :: T a
1099 -- forall a. Eq a => D { x,y :: a } :: T a
1101 gadt_constr :: { [LConDecl RdrName] } -- Returns a list because of: C,D :: ty
1102 : con_list '::' sigtype
1103 { map (sL (comb2 $1 $3)) (mkGadtDecl (unLoc $1) $3) }
1105 -- Deprecated syntax for GADT record declarations
1106 | oqtycon '{' fielddecls '}' '::' sigtype
1107 {% do { cd <- mkDeprecatedGadtRecordDecl (comb2 $1 $6) $1 $3 $6
1110 constrs :: { Located [LConDecl RdrName] }
1111 : maybe_docnext '=' constrs1 { L (comb2 $2 $3) (addConDocs (unLoc $3) $1) }
1113 constrs1 :: { Located [LConDecl RdrName] }
1114 : constrs1 maybe_docnext '|' maybe_docprev constr { LL (addConDoc $5 $2 : addConDocFirst (unLoc $1) $4) }
1115 | constr { L1 [$1] }
1117 constr :: { LConDecl RdrName }
1118 : maybe_docnext forall context '=>' constr_stuff maybe_docprev
1119 { let (con,details) = unLoc $5 in
1120 addConDoc (L (comb4 $2 $3 $4 $5) (mkSimpleConDecl con (unLoc $2) $3 details))
1122 | maybe_docnext forall constr_stuff maybe_docprev
1123 { let (con,details) = unLoc $3 in
1124 addConDoc (L (comb2 $2 $3) (mkSimpleConDecl con (unLoc $2) (noLoc []) details))
1127 forall :: { Located [LHsTyVarBndr RdrName] }
1128 : 'forall' tv_bndrs '.' { LL $2 }
1129 | {- empty -} { noLoc [] }
1131 constr_stuff :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1132 -- We parse the constructor declaration
1134 -- as a btype (treating C as a type constructor) and then convert C to be
1135 -- a data constructor. Reason: it might continue like this:
1137 -- in which case C really would be a type constructor. We can't resolve this
1138 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1139 : btype {% splitCon $1 >>= return.LL }
1140 | btype conop btype { LL ($2, InfixCon $1 $3) }
1142 fielddecls :: { [ConDeclField RdrName] }
1143 : {- empty -} { [] }
1144 | fielddecls1 { $1 }
1146 fielddecls1 :: { [ConDeclField RdrName] }
1147 : fielddecl maybe_docnext ',' maybe_docprev fielddecls1
1148 { [ addFieldDoc f $4 | f <- $1 ] ++ addFieldDocs $5 $2 }
1149 -- This adds the doc $4 to each field separately
1152 fielddecl :: { [ConDeclField RdrName] } -- A list because of f,g :: Int
1153 : maybe_docnext sig_vars '::' ctype maybe_docprev { [ ConDeclField fld $4 ($1 `mplus` $5)
1154 | fld <- reverse (unLoc $2) ] }
1156 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1157 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1158 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1159 -- We don't allow a context, but that's sorted out by the type checker.
1160 deriving :: { Located (Maybe [LHsType RdrName]) }
1161 : {- empty -} { noLoc Nothing }
1162 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1163 ; p <- checkInstType (L loc (HsTyVar tv))
1164 ; return (LL (Just [p])) } }
1165 | 'deriving' '(' ')' { LL (Just []) }
1166 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1167 -- Glasgow extension: allow partial
1168 -- applications in derivings
1170 -----------------------------------------------------------------------------
1171 -- Value definitions
1173 {- Note [Declaration/signature overlap]
1174 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1175 There's an awkward overlap with a type signature. Consider
1176 f :: Int -> Int = ...rhs...
1177 Then we can't tell whether it's a type signature or a value
1178 definition with a result signature until we see the '='.
1179 So we have to inline enough to postpone reductions until we know.
1183 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1184 instead of qvar, we get another shift/reduce-conflict. Consider the
1187 { (^^) :: Int->Int ; } Type signature; only var allowed
1189 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1190 qvar allowed (because of instance decls)
1192 We can't tell whether to reduce var to qvar until after we've read the signatures.
1195 docdecl :: { LHsDecl RdrName }
1196 : docdecld { L1 (DocD (unLoc $1)) }
1198 docdecld :: { LDocDecl }
1199 : docnext { L1 (DocCommentNext (unLoc $1)) }
1200 | docprev { L1 (DocCommentPrev (unLoc $1)) }
1201 | docnamed { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1202 | docsection { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1204 decl :: { Located (OrdList (LHsDecl RdrName)) }
1206 | '!' aexp rhs {% do { pat <- checkPattern $2;
1207 return (LL $ unitOL $ LL $ ValD (
1208 PatBind (LL $ BangPat pat) (unLoc $3)
1209 placeHolderType placeHolderNames)) } }
1210 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1211 let { l = comb2 $1 $> };
1212 return $! (sL l (unitOL $! (sL l $ ValD r))) } }
1213 | docdecl { LL $ unitOL $1 }
1215 rhs :: { Located (GRHSs RdrName) }
1216 : '=' exp wherebinds { sL (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1217 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1219 gdrhs :: { Located [LGRHS RdrName] }
1220 : gdrhs gdrh { LL ($2 : unLoc $1) }
1223 gdrh :: { LGRHS RdrName }
1224 : '|' guardquals '=' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1226 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1227 : infixexp '::' sigtypedoc {% do s <- checkValSig $1 $3
1228 ; return (LL $ unitOL (LL $ SigD s)) }
1229 -- See Note [Declaration/signature overlap] for why we need infixexp here
1230 | var ',' sig_vars '::' sigtypedoc
1231 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1232 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1234 | '{-# INLINE' activation qvar '#-}'
1235 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlinePragma $2 FunLike (getINLINE $1)))) }
1236 | '{-# INLINE_CONLIKE' activation qvar '#-}'
1237 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlinePragma $2 ConLike (getINLINE_CONLIKE $1)))) }
1238 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1239 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlinePragma)
1241 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1242 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlinePragma $2 FunLike (getSPEC_INLINE $1)))
1244 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1245 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1247 -----------------------------------------------------------------------------
1250 quasiquote :: { Located (HsQuasiQuote RdrName) }
1251 : TH_QUASIQUOTE { let { loc = getLoc $1
1252 ; ITquasiQuote (quoter, quote, quoteSpan) = unLoc $1
1253 ; quoterId = mkUnqual varName quoter }
1254 in L1 (mkHsQuasiQuote quoterId quoteSpan quote) }
1256 exp :: { LHsExpr RdrName }
1257 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1258 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1259 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1260 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1261 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1264 infixexp :: { LHsExpr RdrName }
1266 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1268 exp10 :: { LHsExpr RdrName }
1269 : '\\' apat apats opt_asig '->' exp
1270 { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1273 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1274 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1275 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1276 | '-' fexp { LL $ NegApp $2 noSyntaxExpr }
1278 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1279 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1280 return (L loc (mkHsDo DoExpr stmts body)) }
1281 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1282 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1283 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1284 | scc_annot exp { LL $ if opt_SccProfilingOn
1285 then HsSCC (unLoc $1) $2
1287 | hpc_annot exp { LL $ if opt_Hpc
1288 then HsTickPragma (unLoc $1) $2
1291 | 'proc' aexp '->' exp
1292 {% checkPattern $2 >>= \ p ->
1293 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1294 placeHolderType undefined)) }
1295 -- TODO: is LL right here?
1297 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1298 -- hdaume: core annotation
1301 scc_annot :: { Located FastString }
1302 : '_scc_' STRING {% (addWarning Opt_WarnWarningsDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1303 ( do scc <- getSCC $2; return $ LL scc ) }
1304 | '{-# SCC' STRING '#-}' {% do scc <- getSCC $2; return $ LL scc }
1306 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1307 : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1308 { LL $ (getSTRING $2
1309 ,( fromInteger $ getINTEGER $3
1310 , fromInteger $ getINTEGER $5
1312 ,( fromInteger $ getINTEGER $7
1313 , fromInteger $ getINTEGER $9
1318 fexp :: { LHsExpr RdrName }
1319 : fexp aexp { LL $ HsApp $1 $2 }
1322 aexp :: { LHsExpr RdrName }
1323 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1324 | '~' aexp { LL $ ELazyPat $2 }
1327 aexp1 :: { LHsExpr RdrName }
1328 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1332 -- Here was the syntax for type applications that I was planning
1333 -- but there are difficulties (e.g. what order for type args)
1334 -- so it's not enabled yet.
1335 -- But this case *is* used for the left hand side of a generic definition,
1336 -- which is parsed as an expression before being munged into a pattern
1337 | qcname '{|' type '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1338 (sL (getLoc $3) (HsType $3)) }
1340 aexp2 :: { LHsExpr RdrName }
1341 : ipvar { L1 (HsIPVar $! unLoc $1) }
1342 | qcname { L1 (HsVar $! unLoc $1) }
1343 | literal { L1 (HsLit $! unLoc $1) }
1344 -- This will enable overloaded strings permanently. Normally the renamer turns HsString
1345 -- into HsOverLit when -foverloaded-strings is on.
1346 -- | STRING { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1347 | INTEGER { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1348 | RATIONAL { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1350 -- N.B.: sections get parsed by these next two productions.
1351 -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't correct Haskell98
1352 -- (you'd have to write '((+ 3), (4 -))')
1353 -- but the less cluttered version fell out of having texps.
1354 | '(' texp ')' { LL (HsPar $2) }
1355 | '(' tup_exprs ')' { LL (ExplicitTuple $2 Boxed) }
1357 | '(#' texp '#)' { LL (ExplicitTuple [Present $2] Unboxed) }
1358 | '(#' tup_exprs '#)' { LL (ExplicitTuple $2 Unboxed) }
1360 | '[' list ']' { LL (unLoc $2) }
1361 | '[:' parr ':]' { LL (unLoc $2) }
1362 | '_' { L1 EWildPat }
1364 -- Template Haskell Extension
1365 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1366 (L1 $ HsVar (mkUnqual varName
1367 (getTH_ID_SPLICE $1)))) } -- $x
1368 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1371 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1372 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1373 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1374 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1375 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1376 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1377 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1378 return (LL $ HsBracket (PatBr p)) }
1379 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBrL $2) }
1380 | quasiquote { L1 (HsQuasiQuoteE (unLoc $1)) }
1382 -- arrow notation extension
1383 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1385 cmdargs :: { [LHsCmdTop RdrName] }
1386 : cmdargs acmd { $2 : $1 }
1387 | {- empty -} { [] }
1389 acmd :: { LHsCmdTop RdrName }
1390 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1392 cvtopbody :: { [LHsDecl RdrName] }
1393 : '{' cvtopdecls0 '}' { $2 }
1394 | vocurly cvtopdecls0 close { $2 }
1396 cvtopdecls0 :: { [LHsDecl RdrName] }
1397 : {- empty -} { [] }
1400 -----------------------------------------------------------------------------
1401 -- Tuple expressions
1403 -- "texp" is short for tuple expressions:
1404 -- things that can appear unparenthesized as long as they're
1405 -- inside parens or delimitted by commas
1406 texp :: { LHsExpr RdrName }
1409 -- Note [Parsing sections]
1410 -- ~~~~~~~~~~~~~~~~~~~~~~~
1411 -- We include left and right sections here, which isn't
1412 -- technically right according to Haskell 98. For example
1413 -- (3 +, True) isn't legal
1414 -- However, we want to parse bang patterns like
1416 -- and it's convenient to do so here as a section
1417 -- Then when converting expr to pattern we unravel it again
1418 -- Meanwhile, the renamer checks that real sections appear
1420 | infixexp qop { LL $ SectionL $1 $2 }
1421 | qopm infixexp { LL $ SectionR $1 $2 }
1423 -- View patterns get parenthesized above
1424 | exp '->' texp { LL $ EViewPat $1 $3 }
1426 -- Always at least one comma
1427 tup_exprs :: { [HsTupArg RdrName] }
1428 : texp commas_tup_tail { Present $1 : $2 }
1429 | commas tup_tail { replicate $1 missingTupArg ++ $2 }
1431 -- Always starts with commas; always follows an expr
1432 commas_tup_tail :: { [HsTupArg RdrName] }
1433 commas_tup_tail : commas tup_tail { replicate ($1-1) missingTupArg ++ $2 }
1435 -- Always follows a comma
1436 tup_tail :: { [HsTupArg RdrName] }
1437 : texp commas_tup_tail { Present $1 : $2 }
1438 | texp { [Present $1] }
1439 | {- empty -} { [missingTupArg] }
1441 -----------------------------------------------------------------------------
1444 -- The rules below are little bit contorted to keep lexps left-recursive while
1445 -- avoiding another shift/reduce-conflict.
1447 list :: { LHsExpr RdrName }
1448 : texp { L1 $ ExplicitList placeHolderType [$1] }
1449 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1450 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1451 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1452 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1453 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1454 | texp '|' flattenedpquals { sL (comb2 $1 $>) $ mkHsDo ListComp (unLoc $3) $1 }
1456 lexps :: { Located [LHsExpr RdrName] }
1457 : lexps ',' texp { LL (((:) $! $3) $! unLoc $1) }
1458 | texp ',' texp { LL [$3,$1] }
1460 -----------------------------------------------------------------------------
1461 -- List Comprehensions
1463 flattenedpquals :: { Located [LStmt RdrName] }
1464 : pquals { case (unLoc $1) of
1466 -- We just had one thing in our "parallel" list so
1467 -- we simply return that thing directly
1469 qss -> L1 [L1 $ ParStmt [(qs, undefined) | qs <- qss]]
1470 -- We actually found some actual parallel lists so
1471 -- we wrap them into as a ParStmt
1474 pquals :: { Located [[LStmt RdrName]] }
1475 : squals '|' pquals { L (getLoc $2) (reverse (unLoc $1) : unLoc $3) }
1476 | squals { L (getLoc $1) [reverse (unLoc $1)] }
1478 squals :: { Located [LStmt RdrName] } -- In reverse order, because the last
1479 -- one can "grab" the earlier ones
1480 : squals ',' transformqual { LL [L (getLoc $3) ((unLoc $3) (reverse (unLoc $1)))] }
1481 | squals ',' qual { LL ($3 : unLoc $1) }
1482 | transformqual { LL [L (getLoc $1) ((unLoc $1) [])] }
1484 -- | transformquals1 ',' '{|' pquals '|}' { LL ($4 : unLoc $1) }
1485 -- | '{|' pquals '|}' { L1 [$2] }
1488 -- It is possible to enable bracketing (associating) qualifier lists by uncommenting the lines with {| |}
1489 -- above. Due to a lack of consensus on the syntax, this feature is not being used until we get user
1490 -- demand. Note that the {| |} symbols are reused from -XGenerics and hence if you want to compile
1491 -- a program that makes use of this temporary syntax you must supply that flag to GHC
1493 transformqual :: { Located ([LStmt RdrName] -> Stmt RdrName) }
1494 -- Function is applied to a list of stmts *in order*
1495 : 'then' exp { LL $ \leftStmts -> (mkTransformStmt leftStmts $2) }
1497 | 'then' exp 'by' exp { LL $ \leftStmts -> (mkTransformByStmt leftStmts $2 $4) }
1498 | 'then' 'group' 'by' exp { LL $ \leftStmts -> (mkGroupByStmt leftStmts $4) }
1500 -- These two productions deliberately have a shift-reduce conflict. I have made 'group' into a special_id,
1501 -- which means you can enable TransformListComp while still using Data.List.group. However, this makes the two
1502 -- productions ambiguous. I've set things up so that Happy chooses to resolve the conflict in that case by
1503 -- choosing the "group by" variant, which is what we want.
1505 -- This is rather dubious: the user might be confused as to how to parse this statement. However, it is a good
1506 -- practical choice. NB: Data.List.group :: [a] -> [[a]], so using the first production would not even type check
1507 -- if /that/ is the group function we conflict with.
1508 | 'then' 'group' 'using' exp { LL $ \leftStmts -> (mkGroupUsingStmt leftStmts $4) }
1509 | 'then' 'group' 'by' exp 'using' exp { LL $ \leftStmts -> (mkGroupByUsingStmt leftStmts $4 $6) }
1511 -----------------------------------------------------------------------------
1512 -- Parallel array expressions
1514 -- The rules below are little bit contorted; see the list case for details.
1515 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1516 -- Moreover, we allow explicit arrays with no element (represented by the nil
1517 -- constructor in the list case).
1519 parr :: { LHsExpr RdrName }
1520 : { noLoc (ExplicitPArr placeHolderType []) }
1521 | texp { L1 $ ExplicitPArr placeHolderType [$1] }
1522 | lexps { L1 $ ExplicitPArr placeHolderType
1523 (reverse (unLoc $1)) }
1524 | texp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1525 | texp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1526 | texp '|' flattenedpquals { LL $ mkHsDo PArrComp (unLoc $3) $1 }
1528 -- We are reusing `lexps' and `flattenedpquals' from the list case.
1530 -----------------------------------------------------------------------------
1533 guardquals :: { Located [LStmt RdrName] }
1534 : guardquals1 { L (getLoc $1) (reverse (unLoc $1)) }
1536 guardquals1 :: { Located [LStmt RdrName] }
1537 : guardquals1 ',' qual { LL ($3 : unLoc $1) }
1540 -----------------------------------------------------------------------------
1541 -- Case alternatives
1543 altslist :: { Located [LMatch RdrName] }
1544 : '{' alts '}' { LL (reverse (unLoc $2)) }
1545 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1547 alts :: { Located [LMatch RdrName] }
1548 : alts1 { L1 (unLoc $1) }
1549 | ';' alts { LL (unLoc $2) }
1551 alts1 :: { Located [LMatch RdrName] }
1552 : alts1 ';' alt { LL ($3 : unLoc $1) }
1553 | alts1 ';' { LL (unLoc $1) }
1556 alt :: { LMatch RdrName }
1557 : pat opt_sig alt_rhs { LL (Match [$1] $2 (unLoc $3)) }
1559 alt_rhs :: { Located (GRHSs RdrName) }
1560 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1562 ralt :: { Located [LGRHS RdrName] }
1563 : '->' exp { LL (unguardedRHS $2) }
1564 | gdpats { L1 (reverse (unLoc $1)) }
1566 gdpats :: { Located [LGRHS RdrName] }
1567 : gdpats gdpat { LL ($2 : unLoc $1) }
1570 gdpat :: { LGRHS RdrName }
1571 : '|' guardquals '->' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1573 -- 'pat' recognises a pattern, including one with a bang at the top
1574 -- e.g. "!x" or "!(x,y)" or "C a b" etc
1575 -- Bangs inside are parsed as infix operator applications, so that
1576 -- we parse them right when bang-patterns are off
1577 pat :: { LPat RdrName }
1578 pat : exp {% checkPattern $1 }
1579 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1581 apat :: { LPat RdrName }
1582 apat : aexp {% checkPattern $1 }
1583 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1585 apats :: { [LPat RdrName] }
1586 : apat apats { $1 : $2 }
1587 | {- empty -} { [] }
1589 -----------------------------------------------------------------------------
1590 -- Statement sequences
1592 stmtlist :: { Located [LStmt RdrName] }
1593 : '{' stmts '}' { LL (unLoc $2) }
1594 | vocurly stmts close { $2 }
1596 -- do { ;; s ; s ; ; s ;; }
1597 -- The last Stmt should be an expression, but that's hard to enforce
1598 -- here, because we need too much lookahead if we see do { e ; }
1599 -- So we use ExprStmts throughout, and switch the last one over
1600 -- in ParseUtils.checkDo instead
1601 stmts :: { Located [LStmt RdrName] }
1602 : stmt stmts_help { LL ($1 : unLoc $2) }
1603 | ';' stmts { LL (unLoc $2) }
1604 | {- empty -} { noLoc [] }
1606 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1607 : ';' stmts { LL (unLoc $2) }
1608 | {- empty -} { noLoc [] }
1610 -- For typing stmts at the GHCi prompt, where
1611 -- the input may consist of just comments.
1612 maybe_stmt :: { Maybe (LStmt RdrName) }
1614 | {- nothing -} { Nothing }
1616 stmt :: { LStmt RdrName }
1618 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1620 qual :: { LStmt RdrName }
1621 : pat '<-' exp { LL $ mkBindStmt $1 $3 }
1622 | exp { L1 $ mkExprStmt $1 }
1623 | 'let' binds { LL $ LetStmt (unLoc $2) }
1625 -----------------------------------------------------------------------------
1626 -- Record Field Update/Construction
1628 fbinds :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1630 | {- empty -} { ([], False) }
1632 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1633 : fbind ',' fbinds1 { case $3 of (flds, dd) -> ($1 : flds, dd) }
1634 | fbind { ([$1], False) }
1635 | '..' { ([], True) }
1637 fbind :: { HsRecField RdrName (LHsExpr RdrName) }
1638 : qvar '=' exp { HsRecField $1 $3 False }
1639 | qvar { HsRecField $1 (L (getLoc $1) placeHolderPunRhs) True }
1640 -- Here's where we say that plain 'x'
1641 -- means exactly 'x = x'. The pun-flag boolean is
1642 -- there so we can still print it right
1644 -----------------------------------------------------------------------------
1645 -- Implicit Parameter Bindings
1647 dbinds :: { Located [LIPBind RdrName] }
1648 : dbinds ';' dbind { let { this = $3; rest = unLoc $1 }
1649 in rest `seq` this `seq` LL (this : rest) }
1650 | dbinds ';' { LL (unLoc $1) }
1651 | dbind { let this = $1 in this `seq` L1 [this] }
1652 -- | {- empty -} { [] }
1654 dbind :: { LIPBind RdrName }
1655 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1657 ipvar :: { Located (IPName RdrName) }
1658 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1660 -----------------------------------------------------------------------------
1661 -- Warnings and deprecations
1663 namelist :: { Located [RdrName] }
1664 namelist : name_var { L1 [unLoc $1] }
1665 | name_var ',' namelist { LL (unLoc $1 : unLoc $3) }
1667 name_var :: { Located RdrName }
1668 name_var : var { $1 }
1671 -----------------------------------------
1672 -- Data constructors
1673 qcon :: { Located RdrName }
1675 | '(' qconsym ')' { LL (unLoc $2) }
1676 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1677 -- The case of '[:' ':]' is part of the production `parr'
1679 con :: { Located RdrName }
1681 | '(' consym ')' { LL (unLoc $2) }
1682 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1684 con_list :: { Located [Located RdrName] }
1685 con_list : con { L1 [$1] }
1686 | con ',' con_list { LL ($1 : unLoc $3) }
1688 sysdcon :: { Located DataCon } -- Wired in data constructors
1689 : '(' ')' { LL unitDataCon }
1690 | '(' commas ')' { LL $ tupleCon Boxed ($2 + 1) }
1691 | '(#' '#)' { LL $ unboxedSingletonDataCon }
1692 | '(#' commas '#)' { LL $ tupleCon Unboxed ($2 + 1) }
1693 | '[' ']' { LL nilDataCon }
1695 conop :: { Located RdrName }
1697 | '`' conid '`' { LL (unLoc $2) }
1699 qconop :: { Located RdrName }
1701 | '`' qconid '`' { LL (unLoc $2) }
1703 -----------------------------------------------------------------------------
1704 -- Type constructors
1706 gtycon :: { Located RdrName } -- A "general" qualified tycon
1708 | '(' ')' { LL $ getRdrName unitTyCon }
1709 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed ($2 + 1)) }
1710 | '(#' '#)' { LL $ getRdrName unboxedSingletonTyCon }
1711 | '(#' commas '#)' { LL $ getRdrName (tupleTyCon Unboxed ($2 + 1)) }
1712 | '(' '->' ')' { LL $ getRdrName funTyCon }
1713 | '[' ']' { LL $ listTyCon_RDR }
1714 | '[:' ':]' { LL $ parrTyCon_RDR }
1716 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1718 | '(' qtyconsym ')' { LL (unLoc $2) }
1720 qtyconop :: { Located RdrName } -- Qualified or unqualified
1722 | '`' qtycon '`' { LL (unLoc $2) }
1724 qtycon :: { Located RdrName } -- Qualified or unqualified
1725 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1726 | PREFIXQCONSYM { L1 $! mkQual tcClsName (getPREFIXQCONSYM $1) }
1729 tycon :: { Located RdrName } -- Unqualified
1730 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1732 qtyconsym :: { Located RdrName }
1733 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1736 tyconsym :: { Located RdrName }
1737 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1739 -----------------------------------------------------------------------------
1742 op :: { Located RdrName } -- used in infix decls
1746 varop :: { Located RdrName }
1748 | '`' varid '`' { LL (unLoc $2) }
1750 qop :: { LHsExpr RdrName } -- used in sections
1751 : qvarop { L1 $ HsVar (unLoc $1) }
1752 | qconop { L1 $ HsVar (unLoc $1) }
1754 qopm :: { LHsExpr RdrName } -- used in sections
1755 : qvaropm { L1 $ HsVar (unLoc $1) }
1756 | qconop { L1 $ HsVar (unLoc $1) }
1758 qvarop :: { Located RdrName }
1760 | '`' qvarid '`' { LL (unLoc $2) }
1762 qvaropm :: { Located RdrName }
1763 : qvarsym_no_minus { $1 }
1764 | '`' qvarid '`' { LL (unLoc $2) }
1766 -----------------------------------------------------------------------------
1769 tyvar :: { Located RdrName }
1770 tyvar : tyvarid { $1 }
1771 | '(' tyvarsym ')' { LL (unLoc $2) }
1773 tyvarop :: { Located RdrName }
1774 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1776 | '.' {% parseErrorSDoc (getLoc $1)
1777 (vcat [ptext (sLit "Illegal symbol '.' in type"),
1778 ptext (sLit "Perhaps you intended -XRankNTypes or similar flag"),
1779 ptext (sLit "to enable explicit-forall syntax: forall <tvs>. <type>")])
1782 tyvarid :: { Located RdrName }
1783 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1784 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1785 | 'unsafe' { L1 $! mkUnqual tvName (fsLit "unsafe") }
1786 | 'safe' { L1 $! mkUnqual tvName (fsLit "safe") }
1787 | 'threadsafe' { L1 $! mkUnqual tvName (fsLit "threadsafe") }
1789 tyvarsym :: { Located RdrName }
1790 -- Does not include "!", because that is used for strictness marks
1791 -- or ".", because that separates the quantified type vars from the rest
1792 -- or "*", because that's used for kinds
1793 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1795 -----------------------------------------------------------------------------
1798 var :: { Located RdrName }
1800 | '(' varsym ')' { LL (unLoc $2) }
1802 qvar :: { Located RdrName }
1804 | '(' varsym ')' { LL (unLoc $2) }
1805 | '(' qvarsym1 ')' { LL (unLoc $2) }
1806 -- We've inlined qvarsym here so that the decision about
1807 -- whether it's a qvar or a var can be postponed until
1808 -- *after* we see the close paren.
1810 qvarid :: { Located RdrName }
1812 | QVARID { L1 $! mkQual varName (getQVARID $1) }
1813 | PREFIXQVARSYM { L1 $! mkQual varName (getPREFIXQVARSYM $1) }
1815 varid :: { Located RdrName }
1816 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1817 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1818 | 'unsafe' { L1 $! mkUnqual varName (fsLit "unsafe") }
1819 | 'safe' { L1 $! mkUnqual varName (fsLit "safe") }
1820 | 'threadsafe' { L1 $! mkUnqual varName (fsLit "threadsafe") }
1821 | 'forall' { L1 $! mkUnqual varName (fsLit "forall") }
1822 | 'family' { L1 $! mkUnqual varName (fsLit "family") }
1824 qvarsym :: { Located RdrName }
1828 qvarsym_no_minus :: { Located RdrName }
1829 : varsym_no_minus { $1 }
1832 qvarsym1 :: { Located RdrName }
1833 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1835 varsym :: { Located RdrName }
1836 : varsym_no_minus { $1 }
1837 | '-' { L1 $ mkUnqual varName (fsLit "-") }
1839 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1840 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1841 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1844 -- These special_ids are treated as keywords in various places,
1845 -- but as ordinary ids elsewhere. 'special_id' collects all these
1846 -- except 'unsafe', 'forall', and 'family' whose treatment differs
1847 -- depending on context
1848 special_id :: { Located FastString }
1850 : 'as' { L1 (fsLit "as") }
1851 | 'qualified' { L1 (fsLit "qualified") }
1852 | 'hiding' { L1 (fsLit "hiding") }
1853 | 'export' { L1 (fsLit "export") }
1854 | 'label' { L1 (fsLit "label") }
1855 | 'dynamic' { L1 (fsLit "dynamic") }
1856 | 'stdcall' { L1 (fsLit "stdcall") }
1857 | 'ccall' { L1 (fsLit "ccall") }
1858 | 'prim' { L1 (fsLit "prim") }
1859 | 'group' { L1 (fsLit "group") }
1861 special_sym :: { Located FastString }
1862 special_sym : '!' { L1 (fsLit "!") }
1863 | '.' { L1 (fsLit ".") }
1864 | '*' { L1 (fsLit "*") }
1866 -----------------------------------------------------------------------------
1867 -- Data constructors
1869 qconid :: { Located RdrName } -- Qualified or unqualified
1871 | QCONID { L1 $! mkQual dataName (getQCONID $1) }
1872 | PREFIXQCONSYM { L1 $! mkQual dataName (getPREFIXQCONSYM $1) }
1874 conid :: { Located RdrName }
1875 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1877 qconsym :: { Located RdrName } -- Qualified or unqualified
1879 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1881 consym :: { Located RdrName }
1882 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1884 -- ':' means only list cons
1885 | ':' { L1 $ consDataCon_RDR }
1888 -----------------------------------------------------------------------------
1891 literal :: { Located HsLit }
1892 : CHAR { L1 $ HsChar $ getCHAR $1 }
1893 | STRING { L1 $ HsString $ getSTRING $1 }
1894 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1895 | PRIMWORD { L1 $ HsWordPrim $ getPRIMWORD $1 }
1896 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1897 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1898 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1899 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1901 -----------------------------------------------------------------------------
1905 : vccurly { () } -- context popped in lexer.
1906 | error {% popContext }
1908 -----------------------------------------------------------------------------
1909 -- Miscellaneous (mostly renamings)
1911 modid :: { Located ModuleName }
1912 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1913 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1916 (unpackFS mod ++ '.':unpackFS c))
1920 : commas ',' { $1 + 1 }
1923 -----------------------------------------------------------------------------
1924 -- Documentation comments
1926 docnext :: { LHsDocString }
1927 : DOCNEXT {% return (L1 (HsDocString (mkFastString (getDOCNEXT $1)))) }
1929 docprev :: { LHsDocString }
1930 : DOCPREV {% return (L1 (HsDocString (mkFastString (getDOCPREV $1)))) }
1932 docnamed :: { Located (String, HsDocString) }
1934 let string = getDOCNAMED $1
1935 (name, rest) = break isSpace string
1936 in return (L1 (name, HsDocString (mkFastString rest))) }
1938 docsection :: { Located (Int, HsDocString) }
1939 : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1940 return (L1 (n, HsDocString (mkFastString doc))) }
1942 moduleheader :: { Maybe LHsDocString }
1943 : DOCNEXT {% let string = getDOCNEXT $1 in
1944 return (Just (L1 (HsDocString (mkFastString string)))) }
1946 maybe_docprev :: { Maybe LHsDocString }
1947 : docprev { Just $1 }
1948 | {- empty -} { Nothing }
1950 maybe_docnext :: { Maybe LHsDocString }
1951 : docnext { Just $1 }
1952 | {- empty -} { Nothing }
1956 happyError = srcParseFail
1958 getVARID (L _ (ITvarid x)) = x
1959 getCONID (L _ (ITconid x)) = x
1960 getVARSYM (L _ (ITvarsym x)) = x
1961 getCONSYM (L _ (ITconsym x)) = x
1962 getQVARID (L _ (ITqvarid x)) = x
1963 getQCONID (L _ (ITqconid x)) = x
1964 getQVARSYM (L _ (ITqvarsym x)) = x
1965 getQCONSYM (L _ (ITqconsym x)) = x
1966 getPREFIXQVARSYM (L _ (ITprefixqvarsym x)) = x
1967 getPREFIXQCONSYM (L _ (ITprefixqconsym x)) = x
1968 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1969 getCHAR (L _ (ITchar x)) = x
1970 getSTRING (L _ (ITstring x)) = x
1971 getINTEGER (L _ (ITinteger x)) = x
1972 getRATIONAL (L _ (ITrational x)) = x
1973 getPRIMCHAR (L _ (ITprimchar x)) = x
1974 getPRIMSTRING (L _ (ITprimstring x)) = x
1975 getPRIMINTEGER (L _ (ITprimint x)) = x
1976 getPRIMWORD (L _ (ITprimword x)) = x
1977 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1978 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1979 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1980 getINLINE (L _ (ITinline_prag b)) = b
1981 getINLINE_CONLIKE (L _ (ITinline_conlike_prag b)) = b
1982 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1984 getDOCNEXT (L _ (ITdocCommentNext x)) = x
1985 getDOCPREV (L _ (ITdocCommentPrev x)) = x
1986 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
1987 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
1989 getSCC :: Located Token -> P FastString
1990 getSCC lt = do let s = getSTRING lt
1991 err = "Spaces are not allowed in SCCs"
1992 -- We probably actually want to be more restrictive than this
1993 if ' ' `elem` unpackFS s
1994 then failSpanMsgP (getLoc lt) (text err)
1997 -- Utilities for combining source spans
1998 comb2 :: Located a -> Located b -> SrcSpan
1999 comb2 a b = a `seq` b `seq` combineLocs a b
2001 comb3 :: Located a -> Located b -> Located c -> SrcSpan
2002 comb3 a b c = a `seq` b `seq` c `seq`
2003 combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
2005 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
2006 comb4 a b c d = a `seq` b `seq` c `seq` d `seq`
2007 (combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
2008 combineSrcSpans (getLoc c) (getLoc d))
2010 -- strict constructor version:
2012 sL :: SrcSpan -> a -> Located a
2013 sL span a = span `seq` a `seq` L span a
2015 -- Make a source location for the file. We're a bit lazy here and just
2016 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
2017 -- try to find the span of the whole file (ToDo).
2018 fileSrcSpan :: P SrcSpan
2021 let loc = mkSrcLoc (srcLocFile l) 1 1;
2022 return (mkSrcSpan loc loc)