2 -- ---------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1997-2003
7 -- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -- ---------------------------------------------------------------------------
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, mkArrowKind, liftedTypeKind, unliftedTypeKind )
49 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
50 Activation(..), defaultInlineSpec )
54 import {-# SOURCE #-} HaddockLex hiding ( Token )
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 }
244 'unsafe' { L _ ITunsafe }
246 'family' { L _ ITfamily }
247 'stdcall' { L _ ITstdcallconv }
248 'ccall' { L _ ITccallconv }
249 'dotnet' { L _ ITdotnet }
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 '{-# SPECIALISE' { L _ ITspec_prag }
258 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
259 '{-# SOURCE' { L _ ITsource_prag }
260 '{-# RULES' { L _ ITrules_prag }
261 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
262 '{-# SCC' { L _ ITscc_prag }
263 '{-# GENERATED' { L _ ITgenerated_prag }
264 '{-# DEPRECATED' { L _ ITdeprecated_prag }
265 '{-# WARNING' { L _ ITwarning_prag }
266 '{-# UNPACK' { L _ ITunpack_prag }
267 '#-}' { L _ ITclose_prag }
269 '..' { L _ ITdotdot } -- reserved symbols
271 '::' { L _ ITdcolon }
275 '<-' { L _ ITlarrow }
276 '->' { L _ ITrarrow }
279 '=>' { L _ ITdarrow }
283 '-<' { L _ ITlarrowtail } -- for arrow notation
284 '>-' { L _ ITrarrowtail } -- for arrow notation
285 '-<<' { L _ ITLarrowtail } -- for arrow notation
286 '>>-' { L _ ITRarrowtail } -- for arrow notation
289 '{' { L _ ITocurly } -- special symbols
291 '{|' { L _ ITocurlybar }
292 '|}' { L _ ITccurlybar }
293 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
294 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
297 '[:' { L _ ITopabrack }
298 ':]' { L _ ITcpabrack }
301 '(#' { L _ IToubxparen }
302 '#)' { L _ ITcubxparen }
303 '(|' { L _ IToparenbar }
304 '|)' { L _ ITcparenbar }
307 '`' { L _ ITbackquote }
309 VARID { L _ (ITvarid _) } -- identifiers
310 CONID { L _ (ITconid _) }
311 VARSYM { L _ (ITvarsym _) }
312 CONSYM { L _ (ITconsym _) }
313 QVARID { L _ (ITqvarid _) }
314 QCONID { L _ (ITqconid _) }
315 QVARSYM { L _ (ITqvarsym _) }
316 QCONSYM { L _ (ITqconsym _) }
317 PREFIXQVARSYM { L _ (ITprefixqvarsym _) }
318 PREFIXQCONSYM { L _ (ITprefixqconsym _) }
320 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
322 CHAR { L _ (ITchar _) }
323 STRING { L _ (ITstring _) }
324 INTEGER { L _ (ITinteger _) }
325 RATIONAL { L _ (ITrational _) }
327 PRIMCHAR { L _ (ITprimchar _) }
328 PRIMSTRING { L _ (ITprimstring _) }
329 PRIMINTEGER { L _ (ITprimint _) }
330 PRIMWORD { L _ (ITprimword _) }
331 PRIMFLOAT { L _ (ITprimfloat _) }
332 PRIMDOUBLE { L _ (ITprimdouble _) }
334 DOCNEXT { L _ (ITdocCommentNext _) }
335 DOCPREV { L _ (ITdocCommentPrev _) }
336 DOCNAMED { L _ (ITdocCommentNamed _) }
337 DOCSECTION { L _ (ITdocSection _ _) }
340 '[|' { L _ ITopenExpQuote }
341 '[p|' { L _ ITopenPatQuote }
342 '[t|' { L _ ITopenTypQuote }
343 '[d|' { L _ ITopenDecQuote }
344 '|]' { L _ ITcloseQuote }
345 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
346 '$(' { L _ ITparenEscape } -- $( exp )
347 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
348 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
349 TH_QUASIQUOTE { L _ (ITquasiQuote _) }
351 %monad { P } { >>= } { return }
352 %lexer { lexer } { L _ ITeof }
353 %name parseModule module
354 %name parseStmt maybe_stmt
355 %name parseIdentifier identifier
356 %name parseType ctype
357 %partial parseHeader header
358 %tokentype { (Located Token) }
361 -----------------------------------------------------------------------------
362 -- Identifiers; one of the entry points
363 identifier :: { Located RdrName }
368 | '(' '->' ')' { LL $ getRdrName funTyCon }
370 -----------------------------------------------------------------------------
373 -- The place for module deprecation is really too restrictive, but if it
374 -- was allowed at its natural place just before 'module', we get an ugly
375 -- s/r conflict with the second alternative. Another solution would be the
376 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
377 -- either, and DEPRECATED is only expected to be used by people who really
378 -- know what they are doing. :-)
380 module :: { Located (HsModule RdrName) }
381 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' body
382 {% fileSrcSpan >>= \ loc -> case $1 of { (info, doc) ->
383 return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4
386 {% fileSrcSpan >>= \ loc ->
387 return (L loc (HsModule Nothing Nothing
388 (fst $1) (snd $1) Nothing emptyHaddockModInfo
391 maybedocheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
392 : moduleheader { $1 }
393 | {- empty -} { (emptyHaddockModInfo, Nothing) }
395 missing_module_keyword :: { () }
396 : {- empty -} {% pushCurrentContext }
398 maybemodwarning :: { Maybe WarningTxt }
399 : '{-# DEPRECATED' STRING '#-}' { Just (DeprecatedTxt (getSTRING $2)) }
400 | '{-# WARNING' STRING '#-}' { Just (WarningTxt (getSTRING $2)) }
401 | {- empty -} { Nothing }
403 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
405 | vocurly top close { $2 }
407 body2 :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
409 | missing_module_keyword top close { $2 }
411 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
412 : importdecls { (reverse $1,[]) }
413 | importdecls ';' cvtopdecls { (reverse $1,$3) }
414 | cvtopdecls { ([],$1) }
416 cvtopdecls :: { [LHsDecl RdrName] }
417 : topdecls { cvTopDecls $1 }
419 -----------------------------------------------------------------------------
420 -- Module declaration & imports only
422 header :: { Located (HsModule RdrName) }
423 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' header_body
424 {% fileSrcSpan >>= \ loc -> case $1 of { (info, doc) ->
425 return (L loc (HsModule (Just $3) $5 $7 [] $4
427 | missing_module_keyword importdecls
428 {% fileSrcSpan >>= \ loc ->
429 return (L loc (HsModule Nothing Nothing $2 [] Nothing
430 emptyHaddockModInfo Nothing)) }
432 header_body :: { [LImportDecl RdrName] }
433 : '{' importdecls { $2 }
434 | vocurly importdecls { $2 }
436 -----------------------------------------------------------------------------
439 maybeexports :: { Maybe [LIE RdrName] }
440 : '(' exportlist ')' { Just $2 }
441 | {- empty -} { Nothing }
443 exportlist :: { [LIE RdrName] }
444 : expdoclist ',' expdoclist { $1 ++ $3 }
447 exportlist1 :: { [LIE RdrName] }
448 : expdoclist export expdoclist ',' exportlist { $1 ++ ($2 : $3) ++ $5 }
449 | expdoclist export expdoclist { $1 ++ ($2 : $3) }
452 expdoclist :: { [LIE RdrName] }
453 : exp_doc expdoclist { $1 : $2 }
456 exp_doc :: { LIE RdrName }
457 : docsection { L1 (case (unLoc $1) of (n, doc) -> IEGroup n doc) }
458 | docnamed { L1 (IEDocNamed ((fst . unLoc) $1)) }
459 | docnext { L1 (IEDoc (unLoc $1)) }
461 -- No longer allow things like [] and (,,,) to be exported
462 -- They are built in syntax, always available
463 export :: { LIE RdrName }
464 : qvar { L1 (IEVar (unLoc $1)) }
465 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
466 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
467 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
468 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
469 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
471 qcnames :: { [RdrName] }
472 : qcnames ',' qcname_ext { unLoc $3 : $1 }
473 | qcname_ext { [unLoc $1] }
475 qcname_ext :: { Located RdrName } -- Variable or data constructor
476 -- or tagged type constructor
478 | 'type' qcon { sL (comb2 $1 $2)
479 (setRdrNameSpace (unLoc $2)
482 -- Cannot pull into qcname_ext, as qcname is also used in expression.
483 qcname :: { Located RdrName } -- Variable or data constructor
487 -----------------------------------------------------------------------------
488 -- Import Declarations
490 -- import decls can be *empty*, or even just a string of semicolons
491 -- whereas topdecls must contain at least one topdecl.
493 importdecls :: { [LImportDecl RdrName] }
494 : importdecls ';' importdecl { $3 : $1 }
495 | importdecls ';' { $1 }
496 | importdecl { [ $1 ] }
499 importdecl :: { LImportDecl RdrName }
500 : 'import' maybe_src optqualified maybe_pkg modid maybeas maybeimpspec
501 { L (comb4 $1 $5 $6 $7) (ImportDecl $5 $4 $2 $3 (unLoc $6) (unLoc $7)) }
503 maybe_src :: { IsBootInterface }
504 : '{-# SOURCE' '#-}' { True }
505 | {- empty -} { False }
507 maybe_pkg :: { Maybe FastString }
508 : STRING { Just (getSTRING $1) }
509 | {- empty -} { Nothing }
511 optqualified :: { Bool }
512 : 'qualified' { True }
513 | {- empty -} { False }
515 maybeas :: { Located (Maybe ModuleName) }
516 : 'as' modid { LL (Just (unLoc $2)) }
517 | {- empty -} { noLoc Nothing }
519 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
520 : impspec { L1 (Just (unLoc $1)) }
521 | {- empty -} { noLoc Nothing }
523 impspec :: { Located (Bool, [LIE RdrName]) }
524 : '(' exportlist ')' { LL (False, $2) }
525 | 'hiding' '(' exportlist ')' { LL (True, $3) }
527 -----------------------------------------------------------------------------
528 -- Fixity Declarations
532 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
534 infix :: { Located FixityDirection }
535 : 'infix' { L1 InfixN }
536 | 'infixl' { L1 InfixL }
537 | 'infixr' { L1 InfixR }
539 ops :: { Located [Located RdrName] }
540 : ops ',' op { LL ($3 : unLoc $1) }
543 -----------------------------------------------------------------------------
544 -- Top-Level Declarations
546 topdecls :: { OrdList (LHsDecl RdrName) }
547 : topdecls ';' topdecl { $1 `appOL` $3 }
548 | topdecls ';' { $1 }
551 topdecl :: { OrdList (LHsDecl RdrName) }
552 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
553 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
554 | 'instance' inst_type where_inst
555 { let (binds, sigs, ats, _) = cvBindsAndSigs (unLoc $3)
557 unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs ats)))}
558 | stand_alone_deriving { unitOL (LL (DerivD (unLoc $1))) }
559 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
560 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
561 | '{-# DEPRECATED' deprecations '#-}' { $2 }
562 | '{-# WARNING' warnings '#-}' { $2 }
563 | '{-# RULES' rules '#-}' { $2 }
566 -- Template Haskell Extension
567 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
568 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
569 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
574 cl_decl :: { LTyClDecl RdrName }
575 : 'class' tycl_hdr fds where_cls
576 {% do { let { (binds, sigs, ats, docs) =
577 cvBindsAndSigs (unLoc $4)
578 ; (ctxt, tc, tvs, tparms) = unLoc $2}
579 ; checkTyVars tparms -- only type vars allowed
581 ; return $ L (comb4 $1 $2 $3 $4)
582 (mkClassDecl (ctxt, tc, tvs)
583 (unLoc $3) sigs binds ats docs) } }
585 -- Type declarations (toplevel)
587 ty_decl :: { LTyClDecl RdrName }
588 -- ordinary type synonyms
589 : 'type' type '=' ctype
590 -- Note ctype, not sigtype, on the right of '='
591 -- We allow an explicit for-all but we don't insert one
592 -- in type Foo a = (b,b)
593 -- Instead we just say b is out of scope
595 -- Note the use of type for the head; this allows
596 -- infix type constructors to be declared
597 {% do { (tc, tvs, _) <- checkSynHdr $2 False
598 ; return (L (comb2 $1 $4)
599 (TySynonym tc tvs Nothing $4))
602 -- type family declarations
603 | 'type' 'family' type opt_kind_sig
604 -- Note the use of type for the head; this allows
605 -- infix type constructors to be declared
607 {% do { (tc, tvs, _) <- checkSynHdr $3 False
608 ; return (L (comb3 $1 $3 $4)
609 (TyFamily TypeFamily tc tvs (unLoc $4)))
612 -- type instance declarations
613 | 'type' 'instance' type '=' ctype
614 -- Note the use of type for the head; this allows
615 -- infix type constructors and type patterns
617 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
618 ; return (L (comb2 $1 $5)
619 (TySynonym tc tvs (Just typats) $5))
622 -- ordinary data type or newtype declaration
623 | data_or_newtype tycl_hdr constrs deriving
624 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
625 ; checkTyVars tparms -- no type pattern
627 sL (comb4 $1 $2 $3 $4)
628 -- We need the location on tycl_hdr in case
629 -- constrs and deriving are both empty
630 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
631 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
633 -- ordinary GADT declaration
634 | data_or_newtype tycl_hdr opt_kind_sig
635 'where' gadt_constrlist
637 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
638 ; checkTyVars tparms -- can have type pats
640 sL (comb4 $1 $2 $4 $5)
641 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
642 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
644 -- data/newtype family
645 | 'data' 'family' tycl_hdr opt_kind_sig
646 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
647 ; checkTyVars tparms -- no type pattern
648 ; unless (null (unLoc ctxt)) $ -- and no context
649 parseError (getLoc ctxt)
650 "A family declaration cannot have a context"
653 (TyFamily DataFamily tc tvs (unLoc $4)) } }
655 -- data/newtype instance declaration
656 | data_or_newtype 'instance' tycl_hdr constrs deriving
657 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
658 -- can have type pats
660 L (comb4 $1 $3 $4 $5)
661 -- We need the location on tycl_hdr in case
662 -- constrs and deriving are both empty
663 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
664 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
666 -- GADT instance declaration
667 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
668 'where' gadt_constrlist
670 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
671 -- can have type pats
673 L (comb4 $1 $3 $6 $7)
674 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
675 (unLoc $4) (reverse (unLoc $6)) (unLoc $7)) } }
677 -- Associate type family declarations
679 -- * They have a different syntax than on the toplevel (no family special
682 -- * They also need to be separate from instances; otherwise, data family
683 -- declarations without a kind signature cause parsing conflicts with empty
684 -- data declarations.
686 at_decl_cls :: { LTyClDecl RdrName }
687 -- type family declarations
688 : 'type' type opt_kind_sig
689 -- Note the use of type for the head; this allows
690 -- infix type constructors to be declared
692 {% do { (tc, tvs, _) <- checkSynHdr $2 False
693 ; return (L (comb3 $1 $2 $3)
694 (TyFamily TypeFamily tc tvs (unLoc $3)))
697 -- default type instance
698 | 'type' type '=' ctype
699 -- Note the use of type for the head; this allows
700 -- infix type constructors and type patterns
702 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
703 ; return (L (comb2 $1 $4)
704 (TySynonym tc tvs (Just typats) $4))
707 -- data/newtype family declaration
708 | 'data' tycl_hdr opt_kind_sig
709 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
710 ; checkTyVars tparms -- no type pattern
711 ; unless (null (unLoc ctxt)) $ -- and no context
712 parseError (getLoc ctxt)
713 "A family declaration cannot have a context"
716 (TyFamily DataFamily tc tvs (unLoc $3))
719 -- Associate type instances
721 at_decl_inst :: { LTyClDecl RdrName }
722 -- type instance declarations
723 : 'type' type '=' ctype
724 -- Note the use of type for the head; this allows
725 -- infix type constructors and type patterns
727 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
728 ; return (L (comb2 $1 $4)
729 (TySynonym tc tvs (Just typats) $4))
732 -- data/newtype instance declaration
733 | data_or_newtype tycl_hdr constrs deriving
734 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
735 -- can have type pats
737 L (comb4 $1 $2 $3 $4)
738 -- We need the location on tycl_hdr in case
739 -- constrs and deriving are both empty
740 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
741 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
743 -- GADT instance declaration
744 | data_or_newtype tycl_hdr opt_kind_sig
745 'where' gadt_constrlist
747 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
748 -- can have type pats
750 L (comb4 $1 $2 $5 $6)
751 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
752 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
754 data_or_newtype :: { Located NewOrData }
755 : 'data' { L1 DataType }
756 | 'newtype' { L1 NewType }
758 opt_kind_sig :: { Located (Maybe Kind) }
760 | '::' kind { LL (Just (unLoc $2)) }
762 -- tycl_hdr parses the header of a class or data type decl,
763 -- which takes the form
766 -- (Eq a, Ord b) => T a b
767 -- T Int [a] -- for associated types
768 -- Rather a lot of inlining here, else we get reduce/reduce errors
769 tycl_hdr :: { Located (LHsContext RdrName,
771 [LHsTyVarBndr RdrName],
773 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
774 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
776 -----------------------------------------------------------------------------
777 -- Stand-alone deriving
779 -- Glasgow extension: stand-alone deriving declarations
780 stand_alone_deriving :: { LDerivDecl RdrName }
781 : 'deriving' 'instance' inst_type {% checkDerivDecl (LL (DerivDecl $3)) }
783 -----------------------------------------------------------------------------
784 -- Nested declarations
786 -- Declaration in class bodies
788 decl_cls :: { Located (OrdList (LHsDecl RdrName)) }
789 decl_cls : at_decl_cls { LL (unitOL (L1 (TyClD (unLoc $1)))) }
792 decls_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
793 : decls_cls ';' decl_cls { LL (unLoc $1 `appOL` unLoc $3) }
794 | decls_cls ';' { LL (unLoc $1) }
796 | {- empty -} { noLoc nilOL }
800 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
801 : '{' decls_cls '}' { LL (unLoc $2) }
802 | vocurly decls_cls close { $2 }
806 where_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
807 -- No implicit parameters
808 -- May have type declarations
809 : 'where' decllist_cls { LL (unLoc $2) }
810 | {- empty -} { noLoc nilOL }
812 -- Declarations in instance bodies
814 decl_inst :: { Located (OrdList (LHsDecl RdrName)) }
815 decl_inst : at_decl_inst { LL (unitOL (L1 (TyClD (unLoc $1)))) }
818 decls_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
819 : decls_inst ';' decl_inst { LL (unLoc $1 `appOL` unLoc $3) }
820 | decls_inst ';' { LL (unLoc $1) }
822 | {- empty -} { noLoc nilOL }
825 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
826 : '{' decls_inst '}' { LL (unLoc $2) }
827 | vocurly decls_inst close { $2 }
831 where_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
832 -- No implicit parameters
833 -- May have type declarations
834 : 'where' decllist_inst { LL (unLoc $2) }
835 | {- empty -} { noLoc nilOL }
837 -- Declarations in binding groups other than classes and instances
839 decls :: { Located (OrdList (LHsDecl RdrName)) }
840 : decls ';' decl { let { this = unLoc $3;
842 these = rest `appOL` this }
843 in rest `seq` this `seq` these `seq`
845 | decls ';' { LL (unLoc $1) }
847 | {- empty -} { noLoc nilOL }
849 decllist :: { Located (OrdList (LHsDecl RdrName)) }
850 : '{' decls '}' { LL (unLoc $2) }
851 | vocurly decls close { $2 }
853 -- Binding groups other than those of class and instance declarations
855 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
856 -- No type declarations
857 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
858 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
859 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
861 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
862 -- No type declarations
863 : 'where' binds { LL (unLoc $2) }
864 | {- empty -} { noLoc emptyLocalBinds }
867 -----------------------------------------------------------------------------
868 -- Transformation Rules
870 rules :: { OrdList (LHsDecl RdrName) }
871 : rules ';' rule { $1 `snocOL` $3 }
874 | {- empty -} { nilOL }
876 rule :: { LHsDecl RdrName }
877 : STRING activation rule_forall infixexp '=' exp
878 { LL $ RuleD (HsRule (getSTRING $1)
879 ($2 `orElse` AlwaysActive)
880 $3 $4 placeHolderNames $6 placeHolderNames) }
882 activation :: { Maybe Activation }
883 : {- empty -} { Nothing }
884 | explicit_activation { Just $1 }
886 explicit_activation :: { Activation } -- In brackets
887 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
888 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
890 rule_forall :: { [RuleBndr RdrName] }
891 : 'forall' rule_var_list '.' { $2 }
894 rule_var_list :: { [RuleBndr RdrName] }
896 | rule_var rule_var_list { $1 : $2 }
898 rule_var :: { RuleBndr RdrName }
899 : varid { RuleBndr $1 }
900 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
902 -----------------------------------------------------------------------------
903 -- Warnings and deprecations (c.f. rules)
905 warnings :: { OrdList (LHsDecl RdrName) }
906 : warnings ';' warning { $1 `appOL` $3 }
907 | warnings ';' { $1 }
909 | {- empty -} { nilOL }
911 -- SUP: TEMPORARY HACK, not checking for `module Foo'
912 warning :: { OrdList (LHsDecl RdrName) }
914 { toOL [ LL $ WarningD (Warning n (WarningTxt (getSTRING $2)))
917 deprecations :: { OrdList (LHsDecl RdrName) }
918 : deprecations ';' deprecation { $1 `appOL` $3 }
919 | deprecations ';' { $1 }
921 | {- empty -} { nilOL }
923 -- SUP: TEMPORARY HACK, not checking for `module Foo'
924 deprecation :: { OrdList (LHsDecl RdrName) }
926 { toOL [ LL $ WarningD (Warning n (DeprecatedTxt (getSTRING $2)))
930 -----------------------------------------------------------------------------
931 -- Foreign import and export declarations
933 fdecl :: { LHsDecl RdrName }
934 fdecl : 'import' callconv safety fspec
935 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
936 | 'import' callconv fspec
937 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
939 | 'export' callconv fspec
940 {% mkExport $2 (unLoc $3) >>= return.LL }
942 callconv :: { CallConv }
943 : 'stdcall' { CCall StdCallConv }
944 | 'ccall' { CCall CCallConv }
945 | 'dotnet' { DNCall }
948 : 'unsafe' { PlayRisky }
949 | 'safe' { PlaySafe False }
950 | 'threadsafe' { PlaySafe True }
952 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
953 : STRING var '::' sigtypedoc { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
954 | var '::' sigtypedoc { LL (noLoc nilFS, $1, $3) }
955 -- if the entity string is missing, it defaults to the empty string;
956 -- the meaning of an empty entity string depends on the calling
959 -----------------------------------------------------------------------------
962 opt_sig :: { Maybe (LHsType RdrName) }
963 : {- empty -} { Nothing }
964 | '::' sigtype { Just $2 }
966 opt_asig :: { Maybe (LHsType RdrName) }
967 : {- empty -} { Nothing }
968 | '::' atype { Just $2 }
970 sigtypes1 :: { [LHsType RdrName] }
972 | sigtype ',' sigtypes1 { $1 : $3 }
974 sigtype :: { LHsType RdrName }
975 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
976 -- Wrap an Implicit forall if there isn't one there already
978 sigtypedoc :: { LHsType RdrName }
979 : ctypedoc { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
980 -- Wrap an Implicit forall if there isn't one there already
982 sig_vars :: { Located [Located RdrName] }
983 : sig_vars ',' var { LL ($3 : unLoc $1) }
986 -----------------------------------------------------------------------------
989 infixtype :: { LHsType RdrName }
990 : btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
991 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
993 infixtypedoc :: { LHsType RdrName }
995 | infixtype docprev { LL $ HsDocTy $1 $2 }
997 gentypedoc :: { LHsType RdrName }
1000 | infixtypedoc { $1 }
1001 | btype '->' ctypedoc { LL $ HsFunTy $1 $3 }
1002 | btypedoc '->' ctypedoc { LL $ HsFunTy $1 $3 }
1004 ctypedoc :: { LHsType RdrName }
1005 : 'forall' tv_bndrs '.' ctypedoc { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
1006 | context '=>' ctypedoc { LL $ mkImplicitHsForAllTy $1 $3 }
1007 -- A type of form (context => type) is an *implicit* HsForAllTy
1010 strict_mark :: { Located HsBang }
1011 : '!' { L1 HsStrict }
1012 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
1014 -- A ctype is a for-all type
1015 ctype :: { LHsType RdrName }
1016 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
1017 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
1018 -- A type of form (context => type) is an *implicit* HsForAllTy
1021 -- We parse a context as a btype so that we don't get reduce/reduce
1022 -- errors in ctype. The basic problem is that
1024 -- looks so much like a tuple type. We can't tell until we find the =>
1026 -- We have the t1 ~ t2 form here and in gentype, to permit an individual
1027 -- equational constraint without parenthesis.
1028 context :: { LHsContext RdrName }
1029 : btype '~' btype {% checkContext
1030 (LL $ HsPredTy (HsEqualP $1 $3)) }
1031 | btype {% checkContext $1 }
1033 type :: { LHsType RdrName }
1034 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
1037 gentype :: { LHsType RdrName }
1039 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
1040 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
1041 | btype '->' ctype { LL $ HsFunTy $1 $3 }
1042 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1044 btype :: { LHsType RdrName }
1045 : btype atype { LL $ HsAppTy $1 $2 }
1048 btypedoc :: { LHsType RdrName }
1049 : btype atype docprev { LL $ HsDocTy (L (comb2 $1 $2) (HsAppTy $1 $2)) $3 }
1050 | atype docprev { LL $ HsDocTy $1 $2 }
1052 atype :: { LHsType RdrName }
1053 : gtycon { L1 (HsTyVar (unLoc $1)) }
1054 | tyvar { L1 (HsTyVar (unLoc $1)) }
1055 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
1056 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
1057 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
1058 | '[' ctype ']' { LL $ HsListTy $2 }
1059 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
1060 | '(' ctype ')' { LL $ HsParTy $2 }
1061 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
1063 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
1065 -- An inst_type is what occurs in the head of an instance decl
1066 -- e.g. (Foo a, Gaz b) => Wibble a b
1067 -- It's kept as a single type, with a MonoDictTy at the right
1068 -- hand corner, for convenience.
1069 inst_type :: { LHsType RdrName }
1070 : sigtype {% checkInstType $1 }
1072 inst_types1 :: { [LHsType RdrName] }
1073 : inst_type { [$1] }
1074 | inst_type ',' inst_types1 { $1 : $3 }
1076 comma_types0 :: { [LHsType RdrName] }
1077 : comma_types1 { $1 }
1078 | {- empty -} { [] }
1080 comma_types1 :: { [LHsType RdrName] }
1082 | ctype ',' comma_types1 { $1 : $3 }
1084 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1085 : tv_bndr tv_bndrs { $1 : $2 }
1086 | {- empty -} { [] }
1088 tv_bndr :: { LHsTyVarBndr RdrName }
1089 : tyvar { L1 (UserTyVar (unLoc $1)) }
1090 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
1093 fds :: { Located [Located ([RdrName], [RdrName])] }
1094 : {- empty -} { noLoc [] }
1095 | '|' fds1 { LL (reverse (unLoc $2)) }
1097 fds1 :: { Located [Located ([RdrName], [RdrName])] }
1098 : fds1 ',' fd { LL ($3 : unLoc $1) }
1101 fd :: { Located ([RdrName], [RdrName]) }
1102 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
1103 (reverse (unLoc $1), reverse (unLoc $3)) }
1105 varids0 :: { Located [RdrName] }
1106 : {- empty -} { noLoc [] }
1107 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
1109 -----------------------------------------------------------------------------
1112 kind :: { Located Kind }
1114 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1116 akind :: { Located Kind }
1117 : '*' { L1 liftedTypeKind }
1118 | '!' { L1 unliftedTypeKind }
1119 | '(' kind ')' { LL (unLoc $2) }
1122 -----------------------------------------------------------------------------
1123 -- Datatype declarations
1125 gadt_constrlist :: { Located [LConDecl RdrName] }
1126 : '{' gadt_constrs '}' { LL (unLoc $2) }
1127 | vocurly gadt_constrs close { $2 }
1129 gadt_constrs :: { Located [LConDecl RdrName] }
1130 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
1131 | gadt_constrs ';' { $1 }
1132 | gadt_constr { L1 [$1] }
1134 -- We allow the following forms:
1135 -- C :: Eq a => a -> T a
1136 -- C :: forall a. Eq a => !a -> T a
1137 -- D { x,y :: a } :: T a
1138 -- forall a. Eq a => D { x,y :: a } :: T a
1140 gadt_constr :: { LConDecl RdrName }
1142 { LL (mkGadtDecl $1 $3) }
1143 -- Syntax: Maybe merge the record stuff with the single-case above?
1144 -- (to kill the mostly harmless reduce/reduce error)
1145 -- XXX revisit audreyt
1146 | constr_stuff_record '::' sigtype
1147 { let (con,details) = unLoc $1 in
1148 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3) Nothing) }
1150 | forall context '=>' constr_stuff_record '::' sigtype
1151 { let (con,details) = unLoc $4 in
1152 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6) Nothing ) }
1153 | forall constr_stuff_record '::' sigtype
1154 { let (con,details) = unLoc $2 in
1155 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4) Nothing) }
1159 constrs :: { Located [LConDecl RdrName] }
1160 : {- empty; a GHC extension -} { noLoc [] }
1161 | maybe_docnext '=' constrs1 { L (comb2 $2 $3) (addConDocs (unLoc $3) $1) }
1163 constrs1 :: { Located [LConDecl RdrName] }
1164 : constrs1 maybe_docnext '|' maybe_docprev constr { LL (addConDoc $5 $2 : addConDocFirst (unLoc $1) $4) }
1165 | constr { L1 [$1] }
1167 constr :: { LConDecl RdrName }
1168 : maybe_docnext forall context '=>' constr_stuff maybe_docprev
1169 { let (con,details) = unLoc $5 in
1170 L (comb4 $2 $3 $4 $5) (ConDecl con Explicit (unLoc $2) $3 details ResTyH98 ($1 `mplus` $6)) }
1171 | maybe_docnext forall constr_stuff maybe_docprev
1172 { let (con,details) = unLoc $3 in
1173 L (comb2 $2 $3) (ConDecl con Explicit (unLoc $2) (noLoc []) details ResTyH98 ($1 `mplus` $4)) }
1175 forall :: { Located [LHsTyVarBndr RdrName] }
1176 : 'forall' tv_bndrs '.' { LL $2 }
1177 | {- empty -} { noLoc [] }
1179 constr_stuff :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1180 -- We parse the constructor declaration
1182 -- as a btype (treating C as a type constructor) and then convert C to be
1183 -- a data constructor. Reason: it might continue like this:
1185 -- in which case C really would be a type constructor. We can't resolve this
1186 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1187 : btype {% mkPrefixCon $1 [] >>= return.LL }
1188 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
1189 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
1190 | btype conop btype { LL ($2, InfixCon $1 $3) }
1192 constr_stuff_record :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1193 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
1194 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
1196 fielddecls :: { [([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName))] }
1197 : fielddecl maybe_docnext ',' maybe_docprev fielddecls { addFieldDoc (unLoc $1) $4 : addFieldDocs $5 $2 }
1198 | fielddecl { [unLoc $1] }
1200 fielddecl :: { Located ([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName)) }
1201 : maybe_docnext sig_vars '::' ctype maybe_docprev { L (comb3 $2 $3 $4) (reverse (unLoc $2), $4, $1 `mplus` $5) }
1203 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1204 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1205 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1206 -- We don't allow a context, but that's sorted out by the type checker.
1207 deriving :: { Located (Maybe [LHsType RdrName]) }
1208 : {- empty -} { noLoc Nothing }
1209 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1210 ; p <- checkInstType (L loc (HsTyVar tv))
1211 ; return (LL (Just [p])) } }
1212 | 'deriving' '(' ')' { LL (Just []) }
1213 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1214 -- Glasgow extension: allow partial
1215 -- applications in derivings
1217 -----------------------------------------------------------------------------
1218 -- Value definitions
1220 {- There's an awkward overlap with a type signature. Consider
1221 f :: Int -> Int = ...rhs...
1222 Then we can't tell whether it's a type signature or a value
1223 definition with a result signature until we see the '='.
1224 So we have to inline enough to postpone reductions until we know.
1228 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1229 instead of qvar, we get another shift/reduce-conflict. Consider the
1232 { (^^) :: Int->Int ; } Type signature; only var allowed
1234 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1235 qvar allowed (because of instance decls)
1237 We can't tell whether to reduce var to qvar until after we've read the signatures.
1240 docdecl :: { LHsDecl RdrName }
1241 : docdecld { L1 (DocD (unLoc $1)) }
1243 docdecld :: { LDocDecl RdrName }
1244 : docnext { L1 (DocCommentNext (unLoc $1)) }
1245 | docprev { L1 (DocCommentPrev (unLoc $1)) }
1246 | docnamed { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1247 | docsection { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1249 decl :: { Located (OrdList (LHsDecl RdrName)) }
1251 | '!' aexp rhs {% do { pat <- checkPattern $2;
1252 return (LL $ unitOL $ LL $ ValD (
1253 PatBind (LL $ BangPat pat) (unLoc $3)
1254 placeHolderType placeHolderNames)) } }
1255 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1256 let { l = comb2 $1 $> };
1257 return $! (sL l (unitOL $! (sL l $ ValD r))) } }
1258 | docdecl { LL $ unitOL $1 }
1260 rhs :: { Located (GRHSs RdrName) }
1261 : '=' exp wherebinds { sL (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1262 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1264 gdrhs :: { Located [LGRHS RdrName] }
1265 : gdrhs gdrh { LL ($2 : unLoc $1) }
1268 gdrh :: { LGRHS RdrName }
1269 : '|' guardquals '=' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1271 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1272 : infixexp '::' sigtypedoc
1273 {% do s <- checkValSig $1 $3;
1274 return (LL $ unitOL (LL $ SigD s)) }
1275 -- See the above notes for why we need infixexp here
1276 | var ',' sig_vars '::' sigtypedoc
1277 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1278 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1280 | '{-# INLINE' activation qvar '#-}'
1281 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1282 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1283 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1285 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1286 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1288 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1289 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1291 -----------------------------------------------------------------------------
1294 exp :: { LHsExpr RdrName }
1295 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1296 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1297 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1298 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1299 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1302 infixexp :: { LHsExpr RdrName }
1304 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1306 exp10 :: { LHsExpr RdrName }
1307 : '\\' apat apats opt_asig '->' exp
1308 { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1311 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1312 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1313 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1314 | '-' fexp { LL $ NegApp $2 noSyntaxExpr }
1316 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1317 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1318 return (L loc (mkHsDo DoExpr stmts body)) }
1319 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1320 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1321 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1322 | scc_annot exp { LL $ if opt_SccProfilingOn
1323 then HsSCC (unLoc $1) $2
1325 | hpc_annot exp { LL $ if opt_Hpc
1326 then HsTickPragma (unLoc $1) $2
1329 | 'proc' aexp '->' exp
1330 {% checkPattern $2 >>= \ p ->
1331 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1332 placeHolderType undefined)) }
1333 -- TODO: is LL right here?
1335 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1336 -- hdaume: core annotation
1339 scc_annot :: { Located FastString }
1340 : '_scc_' STRING {% (addWarning Opt_WarnWarningsDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1341 ( do scc <- getSCC $2; return $ LL scc ) }
1342 | '{-# SCC' STRING '#-}' {% do scc <- getSCC $2; return $ LL scc }
1344 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1345 : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1346 { LL $ (getSTRING $2
1347 ,( fromInteger $ getINTEGER $3
1348 , fromInteger $ getINTEGER $5
1350 ,( fromInteger $ getINTEGER $7
1351 , fromInteger $ getINTEGER $9
1356 fexp :: { LHsExpr RdrName }
1357 : fexp aexp { LL $ HsApp $1 $2 }
1360 aexp :: { LHsExpr RdrName }
1361 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1362 | '~' aexp { LL $ ELazyPat $2 }
1365 aexp1 :: { LHsExpr RdrName }
1366 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1370 -- Here was the syntax for type applications that I was planning
1371 -- but there are difficulties (e.g. what order for type args)
1372 -- so it's not enabled yet.
1373 -- But this case *is* used for the left hand side of a generic definition,
1374 -- which is parsed as an expression before being munged into a pattern
1375 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1376 (sL (getLoc $3) (HsType $3)) }
1378 aexp2 :: { LHsExpr RdrName }
1379 : ipvar { L1 (HsIPVar $! unLoc $1) }
1380 | qcname { L1 (HsVar $! unLoc $1) }
1381 | literal { L1 (HsLit $! unLoc $1) }
1382 -- This will enable overloaded strings permanently. Normally the renamer turns HsString
1383 -- into HsOverLit when -foverloaded-strings is on.
1384 -- | STRING { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1385 | INTEGER { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1386 | RATIONAL { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1387 -- N.B.: sections get parsed by these next two productions.
1388 -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't correct Haskell98
1389 -- (you'd have to write '((+ 3), (4 -))')
1390 -- but the less cluttered version fell out of having texps.
1391 | '(' texp ')' { LL (HsPar $2) }
1392 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1393 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1394 | '[' list ']' { LL (unLoc $2) }
1395 | '[:' parr ':]' { LL (unLoc $2) }
1396 | '_' { L1 EWildPat }
1398 -- Template Haskell Extension
1399 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1400 (L1 $ HsVar (mkUnqual varName
1401 (getTH_ID_SPLICE $1)))) } -- $x
1402 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1404 | TH_QUASIQUOTE { let { loc = getLoc $1
1405 ; ITquasiQuote (quoter, quote, quoteSpan) = unLoc $1
1406 ; quoterId = mkUnqual varName quoter
1408 in sL loc $ HsQuasiQuoteE (mkHsQuasiQuote quoterId quoteSpan quote) }
1409 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1410 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1411 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1412 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1413 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1414 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1415 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1416 return (LL $ HsBracket (PatBr p)) }
1417 | '[d|' cvtopbody '|]' {% checkDecBrGroup $2 >>= \g ->
1418 return (LL $ HsBracket (DecBr g)) }
1420 -- arrow notation extension
1421 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1423 cmdargs :: { [LHsCmdTop RdrName] }
1424 : cmdargs acmd { $2 : $1 }
1425 | {- empty -} { [] }
1427 acmd :: { LHsCmdTop RdrName }
1428 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1430 cvtopbody :: { [LHsDecl RdrName] }
1431 : '{' cvtopdecls0 '}' { $2 }
1432 | vocurly cvtopdecls0 close { $2 }
1434 cvtopdecls0 :: { [LHsDecl RdrName] }
1435 : {- empty -} { [] }
1438 -- "texp" is short for tuple expressions:
1439 -- things that can appear unparenthesized as long as they're
1440 -- inside parens or delimitted by commas
1441 texp :: { LHsExpr RdrName }
1444 -- Note [Parsing sections]
1445 -- ~~~~~~~~~~~~~~~~~~~~~~~
1446 -- We include left and right sections here, which isn't
1447 -- technically right according to Haskell 98. For example
1448 -- (3 +, True) isn't legal
1449 -- However, we want to parse bang patterns like
1451 -- and it's convenient to do so here as a section
1452 -- Then when converting expr to pattern we unravel it again
1453 -- Meanwhile, the renamer checks that real sections appear
1455 | infixexp qop { LL $ SectionL $1 $2 }
1456 | qopm infixexp { LL $ SectionR $1 $2 }
1458 -- View patterns get parenthesized above
1459 | exp '->' exp { LL $ EViewPat $1 $3 }
1461 texps :: { [LHsExpr RdrName] }
1462 : texps ',' texp { $3 : $1 }
1466 -----------------------------------------------------------------------------
1469 -- The rules below are little bit contorted to keep lexps left-recursive while
1470 -- avoiding another shift/reduce-conflict.
1472 list :: { LHsExpr RdrName }
1473 : texp { L1 $ ExplicitList placeHolderType [$1] }
1474 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1475 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1476 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1477 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1478 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1479 | texp '|' flattenedpquals { sL (comb2 $1 $>) $ mkHsDo ListComp (unLoc $3) $1 }
1481 lexps :: { Located [LHsExpr RdrName] }
1482 : lexps ',' texp { LL (((:) $! $3) $! unLoc $1) }
1483 | texp ',' texp { LL [$3,$1] }
1485 -----------------------------------------------------------------------------
1486 -- List Comprehensions
1488 flattenedpquals :: { Located [LStmt RdrName] }
1489 : pquals { case (unLoc $1) of
1490 ParStmt [(qs, _)] -> L1 qs
1491 -- We just had one thing in our "parallel" list so
1492 -- we simply return that thing directly
1495 -- We actually found some actual parallel lists so
1496 -- we leave them into as a ParStmt
1499 pquals :: { LStmt RdrName }
1500 : pquals1 { L1 (ParStmt [(qs, undefined) | qs <- (reverse (unLoc $1))]) }
1502 pquals1 :: { Located [[LStmt RdrName]] }
1503 : pquals1 '|' squals { LL (unLoc $3 : unLoc $1) }
1504 | squals { L (getLoc $1) [unLoc $1] }
1506 squals :: { Located [LStmt RdrName] }
1507 : squals1 { L (getLoc $1) (reverse (unLoc $1)) }
1509 squals1 :: { Located [LStmt RdrName] }
1510 : transformquals1 { LL (unLoc $1) }
1512 transformquals1 :: { Located [LStmt RdrName] }
1513 : transformquals1 ',' transformqual { LL $ [LL ((unLoc $3) (unLoc $1))] }
1514 | transformquals1 ',' qual { LL ($3 : unLoc $1) }
1515 -- | transformquals1 ',' '{|' pquals '|}' { LL ($4 : unLoc $1) }
1516 | transformqual { LL $ [LL ((unLoc $1) [])] }
1518 -- | '{|' pquals '|}' { L1 [$2] }
1521 -- It is possible to enable bracketing (associating) qualifier lists by uncommenting the lines with {| |}
1522 -- above. Due to a lack of consensus on the syntax, this feature is not being used until we get user
1523 -- demand. Note that the {| |} symbols are reused from -XGenerics and hence if you want to compile
1524 -- a program that makes use of this temporary syntax you must supply that flag to GHC
1526 transformqual :: { Located ([LStmt RdrName] -> Stmt RdrName) }
1527 : 'then' exp { LL $ \leftStmts -> (mkTransformStmt (reverse leftStmts) $2) }
1528 | 'then' exp 'by' exp { LL $ \leftStmts -> (mkTransformByStmt (reverse leftStmts) $2 $4) }
1529 | 'then' 'group' 'by' exp { LL $ \leftStmts -> (mkGroupByStmt (reverse leftStmts) $4) }
1530 | 'then' 'group' 'using' exp { LL $ \leftStmts -> (mkGroupUsingStmt (reverse leftStmts) $4) }
1531 | 'then' 'group' 'by' exp 'using' exp { LL $ \leftStmts -> (mkGroupByUsingStmt (reverse leftStmts) $4 $6) }
1533 -----------------------------------------------------------------------------
1534 -- Parallel array expressions
1536 -- The rules below are little bit contorted; see the list case for details.
1537 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1538 -- Moreover, we allow explicit arrays with no element (represented by the nil
1539 -- constructor in the list case).
1541 parr :: { LHsExpr RdrName }
1542 : { noLoc (ExplicitPArr placeHolderType []) }
1543 | texp { L1 $ ExplicitPArr placeHolderType [$1] }
1544 | lexps { L1 $ ExplicitPArr placeHolderType
1545 (reverse (unLoc $1)) }
1546 | texp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1547 | texp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1548 | texp '|' flattenedpquals { LL $ mkHsDo PArrComp (unLoc $3) $1 }
1550 -- We are reusing `lexps' and `flattenedpquals' from the list case.
1552 -----------------------------------------------------------------------------
1555 guardquals :: { Located [LStmt RdrName] }
1556 : guardquals1 { L (getLoc $1) (reverse (unLoc $1)) }
1558 guardquals1 :: { Located [LStmt RdrName] }
1559 : guardquals1 ',' qual { LL ($3 : unLoc $1) }
1562 -----------------------------------------------------------------------------
1563 -- Case alternatives
1565 altslist :: { Located [LMatch RdrName] }
1566 : '{' alts '}' { LL (reverse (unLoc $2)) }
1567 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1569 alts :: { Located [LMatch RdrName] }
1570 : alts1 { L1 (unLoc $1) }
1571 | ';' alts { LL (unLoc $2) }
1573 alts1 :: { Located [LMatch RdrName] }
1574 : alts1 ';' alt { LL ($3 : unLoc $1) }
1575 | alts1 ';' { LL (unLoc $1) }
1578 alt :: { LMatch RdrName }
1579 : pat opt_sig alt_rhs { LL (Match [$1] $2 (unLoc $3)) }
1581 alt_rhs :: { Located (GRHSs RdrName) }
1582 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1584 ralt :: { Located [LGRHS RdrName] }
1585 : '->' exp { LL (unguardedRHS $2) }
1586 | gdpats { L1 (reverse (unLoc $1)) }
1588 gdpats :: { Located [LGRHS RdrName] }
1589 : gdpats gdpat { LL ($2 : unLoc $1) }
1592 gdpat :: { LGRHS RdrName }
1593 : '|' guardquals '->' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1595 -- 'pat' recognises a pattern, including one with a bang at the top
1596 -- e.g. "!x" or "!(x,y)" or "C a b" etc
1597 -- Bangs inside are parsed as infix operator applications, so that
1598 -- we parse them right when bang-patterns are off
1599 pat :: { LPat RdrName }
1600 pat : exp {% checkPattern $1 }
1601 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1603 apat :: { LPat RdrName }
1604 apat : aexp {% checkPattern $1 }
1605 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1607 apats :: { [LPat RdrName] }
1608 : apat apats { $1 : $2 }
1609 | {- empty -} { [] }
1611 -----------------------------------------------------------------------------
1612 -- Statement sequences
1614 stmtlist :: { Located [LStmt RdrName] }
1615 : '{' stmts '}' { LL (unLoc $2) }
1616 | vocurly stmts close { $2 }
1618 -- do { ;; s ; s ; ; s ;; }
1619 -- The last Stmt should be an expression, but that's hard to enforce
1620 -- here, because we need too much lookahead if we see do { e ; }
1621 -- So we use ExprStmts throughout, and switch the last one over
1622 -- in ParseUtils.checkDo instead
1623 stmts :: { Located [LStmt RdrName] }
1624 : stmt stmts_help { LL ($1 : unLoc $2) }
1625 | ';' stmts { LL (unLoc $2) }
1626 | {- empty -} { noLoc [] }
1628 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1629 : ';' stmts { LL (unLoc $2) }
1630 | {- empty -} { noLoc [] }
1632 -- For typing stmts at the GHCi prompt, where
1633 -- the input may consist of just comments.
1634 maybe_stmt :: { Maybe (LStmt RdrName) }
1636 | {- nothing -} { Nothing }
1638 stmt :: { LStmt RdrName }
1640 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1642 qual :: { LStmt RdrName }
1643 : pat '<-' exp { LL $ mkBindStmt $1 $3 }
1644 | exp { L1 $ mkExprStmt $1 }
1645 | 'let' binds { LL $ LetStmt (unLoc $2) }
1647 -----------------------------------------------------------------------------
1648 -- Record Field Update/Construction
1650 fbinds :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1652 | {- empty -} { ([], False) }
1654 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1655 : fbind ',' fbinds1 { case $3 of (flds, dd) -> ($1 : flds, dd) }
1656 | fbind { ([$1], False) }
1657 | '..' { ([], True) }
1659 fbind :: { HsRecField RdrName (LHsExpr RdrName) }
1660 : qvar '=' exp { HsRecField $1 $3 False }
1661 | qvar { HsRecField $1 (L (getLoc $1) (HsVar (unLoc $1))) True }
1662 -- Here's where we say that plain 'x'
1663 -- means exactly 'x = x'. The pun-flag boolean is
1664 -- there so we can still print it right
1666 -----------------------------------------------------------------------------
1667 -- Implicit Parameter Bindings
1669 dbinds :: { Located [LIPBind RdrName] }
1670 : dbinds ';' dbind { let { this = $3; rest = unLoc $1 }
1671 in rest `seq` this `seq` LL (this : rest) }
1672 | dbinds ';' { LL (unLoc $1) }
1673 | dbind { let this = $1 in this `seq` L1 [this] }
1674 -- | {- empty -} { [] }
1676 dbind :: { LIPBind RdrName }
1677 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1679 ipvar :: { Located (IPName RdrName) }
1680 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1682 -----------------------------------------------------------------------------
1683 -- Warnings and deprecations
1685 namelist :: { Located [RdrName] }
1686 namelist : name_var { L1 [unLoc $1] }
1687 | name_var ',' namelist { LL (unLoc $1 : unLoc $3) }
1689 name_var :: { Located RdrName }
1690 name_var : var { $1 }
1693 -----------------------------------------
1694 -- Data constructors
1695 qcon :: { Located RdrName }
1697 | '(' qconsym ')' { LL (unLoc $2) }
1698 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1699 -- The case of '[:' ':]' is part of the production `parr'
1701 con :: { Located RdrName }
1703 | '(' consym ')' { LL (unLoc $2) }
1704 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1706 sysdcon :: { Located DataCon } -- Wired in data constructors
1707 : '(' ')' { LL unitDataCon }
1708 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1709 | '(#' '#)' { LL $ unboxedSingletonDataCon }
1710 | '(#' commas '#)' { LL $ tupleCon Unboxed $2 }
1711 | '[' ']' { LL nilDataCon }
1713 conop :: { Located RdrName }
1715 | '`' conid '`' { LL (unLoc $2) }
1717 qconop :: { Located RdrName }
1719 | '`' qconid '`' { LL (unLoc $2) }
1721 -----------------------------------------------------------------------------
1722 -- Type constructors
1724 gtycon :: { Located RdrName } -- A "general" qualified tycon
1726 | '(' ')' { LL $ getRdrName unitTyCon }
1727 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1728 | '(#' '#)' { LL $ getRdrName unboxedSingletonTyCon }
1729 | '(#' commas '#)' { LL $ getRdrName (tupleTyCon Unboxed $2) }
1730 | '(' '->' ')' { LL $ getRdrName funTyCon }
1731 | '[' ']' { LL $ listTyCon_RDR }
1732 | '[:' ':]' { LL $ parrTyCon_RDR }
1734 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1736 | '(' qtyconsym ')' { LL (unLoc $2) }
1738 qtyconop :: { Located RdrName } -- Qualified or unqualified
1740 | '`' qtycon '`' { LL (unLoc $2) }
1742 qtycon :: { Located RdrName } -- Qualified or unqualified
1743 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1744 | PREFIXQCONSYM { L1 $! mkQual tcClsName (getPREFIXQCONSYM $1) }
1747 tycon :: { Located RdrName } -- Unqualified
1748 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1750 qtyconsym :: { Located RdrName }
1751 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1754 tyconsym :: { Located RdrName }
1755 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1757 -----------------------------------------------------------------------------
1760 op :: { Located RdrName } -- used in infix decls
1764 varop :: { Located RdrName }
1766 | '`' varid '`' { LL (unLoc $2) }
1768 qop :: { LHsExpr RdrName } -- used in sections
1769 : qvarop { L1 $ HsVar (unLoc $1) }
1770 | qconop { L1 $ HsVar (unLoc $1) }
1772 qopm :: { LHsExpr RdrName } -- used in sections
1773 : qvaropm { L1 $ HsVar (unLoc $1) }
1774 | qconop { L1 $ HsVar (unLoc $1) }
1776 qvarop :: { Located RdrName }
1778 | '`' qvarid '`' { LL (unLoc $2) }
1780 qvaropm :: { Located RdrName }
1781 : qvarsym_no_minus { $1 }
1782 | '`' qvarid '`' { LL (unLoc $2) }
1784 -----------------------------------------------------------------------------
1787 tyvar :: { Located RdrName }
1788 tyvar : tyvarid { $1 }
1789 | '(' tyvarsym ')' { LL (unLoc $2) }
1791 tyvarop :: { Located RdrName }
1792 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1795 tyvarid :: { Located RdrName }
1796 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1797 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1798 | 'unsafe' { L1 $! mkUnqual tvName (fsLit "unsafe") }
1799 | 'safe' { L1 $! mkUnqual tvName (fsLit "safe") }
1800 | 'threadsafe' { L1 $! mkUnqual tvName (fsLit "threadsafe") }
1802 tyvarsym :: { Located RdrName }
1803 -- Does not include "!", because that is used for strictness marks
1804 -- or ".", because that separates the quantified type vars from the rest
1805 -- or "*", because that's used for kinds
1806 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1808 -----------------------------------------------------------------------------
1811 var :: { Located RdrName }
1813 | '(' varsym ')' { LL (unLoc $2) }
1815 qvar :: { Located RdrName }
1817 | '(' varsym ')' { LL (unLoc $2) }
1818 | '(' qvarsym1 ')' { LL (unLoc $2) }
1819 -- We've inlined qvarsym here so that the decision about
1820 -- whether it's a qvar or a var can be postponed until
1821 -- *after* we see the close paren.
1823 qvarid :: { Located RdrName }
1825 | QVARID { L1 $! mkQual varName (getQVARID $1) }
1826 | PREFIXQVARSYM { L1 $! mkQual varName (getPREFIXQVARSYM $1) }
1828 varid :: { Located RdrName }
1829 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1830 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1831 | 'unsafe' { L1 $! mkUnqual varName (fsLit "unsafe") }
1832 | 'safe' { L1 $! mkUnqual varName (fsLit "safe") }
1833 | 'threadsafe' { L1 $! mkUnqual varName (fsLit "threadsafe") }
1834 | 'forall' { L1 $! mkUnqual varName (fsLit "forall") }
1835 | 'family' { L1 $! mkUnqual varName (fsLit "family") }
1837 qvarsym :: { Located RdrName }
1841 qvarsym_no_minus :: { Located RdrName }
1842 : varsym_no_minus { $1 }
1845 qvarsym1 :: { Located RdrName }
1846 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1848 varsym :: { Located RdrName }
1849 : varsym_no_minus { $1 }
1850 | '-' { L1 $ mkUnqual varName (fsLit "-") }
1852 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1853 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1854 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1857 -- These special_ids are treated as keywords in various places,
1858 -- but as ordinary ids elsewhere. 'special_id' collects all these
1859 -- except 'unsafe', 'forall', and 'family' whose treatment differs
1860 -- depending on context
1861 special_id :: { Located FastString }
1863 : 'as' { L1 (fsLit "as") }
1864 | 'qualified' { L1 (fsLit "qualified") }
1865 | 'hiding' { L1 (fsLit "hiding") }
1866 | 'export' { L1 (fsLit "export") }
1867 | 'label' { L1 (fsLit "label") }
1868 | 'dynamic' { L1 (fsLit "dynamic") }
1869 | 'stdcall' { L1 (fsLit "stdcall") }
1870 | 'ccall' { L1 (fsLit "ccall") }
1872 special_sym :: { Located FastString }
1873 special_sym : '!' { L1 (fsLit "!") }
1874 | '.' { L1 (fsLit ".") }
1875 | '*' { L1 (fsLit "*") }
1877 -----------------------------------------------------------------------------
1878 -- Data constructors
1880 qconid :: { Located RdrName } -- Qualified or unqualified
1882 | QCONID { L1 $! mkQual dataName (getQCONID $1) }
1883 | PREFIXQCONSYM { L1 $! mkQual dataName (getPREFIXQCONSYM $1) }
1885 conid :: { Located RdrName }
1886 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1888 qconsym :: { Located RdrName } -- Qualified or unqualified
1890 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1892 consym :: { Located RdrName }
1893 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1895 -- ':' means only list cons
1896 | ':' { L1 $ consDataCon_RDR }
1899 -----------------------------------------------------------------------------
1902 literal :: { Located HsLit }
1903 : CHAR { L1 $ HsChar $ getCHAR $1 }
1904 | STRING { L1 $ HsString $ getSTRING $1 }
1905 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1906 | PRIMWORD { L1 $ HsWordPrim $ getPRIMWORD $1 }
1907 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1908 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1909 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1910 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1912 -----------------------------------------------------------------------------
1916 : vccurly { () } -- context popped in lexer.
1917 | error {% popContext }
1919 -----------------------------------------------------------------------------
1920 -- Miscellaneous (mostly renamings)
1922 modid :: { Located ModuleName }
1923 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1924 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1927 (unpackFS mod ++ '.':unpackFS c))
1931 : commas ',' { $1 + 1 }
1934 -----------------------------------------------------------------------------
1935 -- Documentation comments
1937 docnext :: { LHsDoc RdrName }
1938 : DOCNEXT {% case parseHaddockParagraphs (tokenise (getDOCNEXT $1)) of {
1939 MyLeft err -> parseError (getLoc $1) err;
1940 MyRight doc -> return (L1 doc) } }
1942 docprev :: { LHsDoc RdrName }
1943 : DOCPREV {% case parseHaddockParagraphs (tokenise (getDOCPREV $1)) of {
1944 MyLeft err -> parseError (getLoc $1) err;
1945 MyRight doc -> return (L1 doc) } }
1947 docnamed :: { Located (String, (HsDoc RdrName)) }
1949 let string = getDOCNAMED $1
1950 (name, rest) = break isSpace string
1951 in case parseHaddockParagraphs (tokenise rest) of {
1952 MyLeft err -> parseError (getLoc $1) err;
1953 MyRight doc -> return (L1 (name, doc)) } }
1955 docsection :: { Located (Int, HsDoc RdrName) }
1956 : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1957 case parseHaddockString (tokenise doc) of {
1958 MyLeft err -> parseError (getLoc $1) err;
1959 MyRight doc -> return (L1 (n, doc)) } }
1961 moduleheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
1962 : DOCNEXT {% let string = getDOCNEXT $1 in
1963 case parseModuleHeader string of {
1964 Right (str, info) ->
1965 case parseHaddockParagraphs (tokenise str) of {
1966 MyLeft err -> parseError (getLoc $1) err;
1967 MyRight doc -> return (info, Just doc);
1969 Left err -> parseError (getLoc $1) err
1972 maybe_docprev :: { Maybe (LHsDoc RdrName) }
1973 : docprev { Just $1 }
1974 | {- empty -} { Nothing }
1976 maybe_docnext :: { Maybe (LHsDoc RdrName) }
1977 : docnext { Just $1 }
1978 | {- empty -} { Nothing }
1982 happyError = srcParseFail
1984 getVARID (L _ (ITvarid x)) = x
1985 getCONID (L _ (ITconid x)) = x
1986 getVARSYM (L _ (ITvarsym x)) = x
1987 getCONSYM (L _ (ITconsym x)) = x
1988 getQVARID (L _ (ITqvarid x)) = x
1989 getQCONID (L _ (ITqconid x)) = x
1990 getQVARSYM (L _ (ITqvarsym x)) = x
1991 getQCONSYM (L _ (ITqconsym x)) = x
1992 getPREFIXQVARSYM (L _ (ITprefixqvarsym x)) = x
1993 getPREFIXQCONSYM (L _ (ITprefixqconsym x)) = x
1994 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1995 getCHAR (L _ (ITchar x)) = x
1996 getSTRING (L _ (ITstring x)) = x
1997 getINTEGER (L _ (ITinteger x)) = x
1998 getRATIONAL (L _ (ITrational x)) = x
1999 getPRIMCHAR (L _ (ITprimchar x)) = x
2000 getPRIMSTRING (L _ (ITprimstring x)) = x
2001 getPRIMINTEGER (L _ (ITprimint x)) = x
2002 getPRIMWORD (L _ (ITprimword x)) = x
2003 getPRIMFLOAT (L _ (ITprimfloat x)) = x
2004 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
2005 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
2006 getINLINE (L _ (ITinline_prag b)) = b
2007 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
2009 getDOCNEXT (L _ (ITdocCommentNext x)) = x
2010 getDOCPREV (L _ (ITdocCommentPrev x)) = x
2011 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
2012 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
2014 getSCC :: Located Token -> P FastString
2015 getSCC lt = do let s = getSTRING lt
2016 err = "Spaces are not allowed in SCCs"
2017 -- We probably actually want to be more restrictive than this
2018 if ' ' `elem` unpackFS s
2019 then failSpanMsgP (getLoc lt) (text err)
2022 -- Utilities for combining source spans
2023 comb2 :: Located a -> Located b -> SrcSpan
2024 comb2 a b = a `seq` b `seq` combineLocs a b
2026 comb3 :: Located a -> Located b -> Located c -> SrcSpan
2027 comb3 a b c = a `seq` b `seq` c `seq`
2028 combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
2030 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
2031 comb4 a b c d = a `seq` b `seq` c `seq` d `seq`
2032 (combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
2033 combineSrcSpans (getLoc c) (getLoc d))
2035 -- strict constructor version:
2037 sL :: SrcSpan -> a -> Located a
2038 sL span a = span `seq` a `seq` L span a
2040 -- Make a source location for the file. We're a bit lazy here and just
2041 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
2042 -- try to find the span of the whole file (ToDo).
2043 fileSrcSpan :: P SrcSpan
2046 let loc = mkSrcLoc (srcLocFile l) 1 0;
2047 return (mkSrcSpan loc loc)