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 _) }
318 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
320 CHAR { L _ (ITchar _) }
321 STRING { L _ (ITstring _) }
322 INTEGER { L _ (ITinteger _) }
323 RATIONAL { L _ (ITrational _) }
325 PRIMCHAR { L _ (ITprimchar _) }
326 PRIMSTRING { L _ (ITprimstring _) }
327 PRIMINTEGER { L _ (ITprimint _) }
328 PRIMWORD { L _ (ITprimword _) }
329 PRIMFLOAT { L _ (ITprimfloat _) }
330 PRIMDOUBLE { L _ (ITprimdouble _) }
332 DOCNEXT { L _ (ITdocCommentNext _) }
333 DOCPREV { L _ (ITdocCommentPrev _) }
334 DOCNAMED { L _ (ITdocCommentNamed _) }
335 DOCSECTION { L _ (ITdocSection _ _) }
338 '[|' { L _ ITopenExpQuote }
339 '[p|' { L _ ITopenPatQuote }
340 '[t|' { L _ ITopenTypQuote }
341 '[d|' { L _ ITopenDecQuote }
342 '|]' { L _ ITcloseQuote }
343 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
344 '$(' { L _ ITparenEscape } -- $( exp )
345 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
346 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
347 TH_QUASIQUOTE { L _ (ITquasiQuote _) }
349 %monad { P } { >>= } { return }
350 %lexer { lexer } { L _ ITeof }
351 %name parseModule module
352 %name parseStmt maybe_stmt
353 %name parseIdentifier identifier
354 %name parseType ctype
355 %partial parseHeader header
356 %tokentype { (Located Token) }
359 -----------------------------------------------------------------------------
360 -- Identifiers; one of the entry points
361 identifier :: { Located RdrName }
366 | '(' '->' ')' { LL $ getRdrName funTyCon }
368 -----------------------------------------------------------------------------
371 -- The place for module deprecation is really too restrictive, but if it
372 -- was allowed at its natural place just before 'module', we get an ugly
373 -- s/r conflict with the second alternative. Another solution would be the
374 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
375 -- either, and DEPRECATED is only expected to be used by people who really
376 -- know what they are doing. :-)
378 module :: { Located (HsModule RdrName) }
379 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' body
380 {% fileSrcSpan >>= \ loc -> case $1 of { (info, doc) ->
381 return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4
384 {% fileSrcSpan >>= \ loc ->
385 return (L loc (HsModule Nothing Nothing
386 (fst $1) (snd $1) Nothing emptyHaddockModInfo
389 maybedocheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
390 : moduleheader { $1 }
391 | {- empty -} { (emptyHaddockModInfo, Nothing) }
393 missing_module_keyword :: { () }
394 : {- empty -} {% pushCurrentContext }
396 maybemodwarning :: { Maybe WarningTxt }
397 : '{-# DEPRECATED' STRING '#-}' { Just (DeprecatedTxt (getSTRING $2)) }
398 | '{-# WARNING' STRING '#-}' { Just (WarningTxt (getSTRING $2)) }
399 | {- empty -} { Nothing }
401 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
403 | vocurly top close { $2 }
405 body2 :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
407 | missing_module_keyword top close { $2 }
409 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
410 : importdecls { (reverse $1,[]) }
411 | importdecls ';' cvtopdecls { (reverse $1,$3) }
412 | cvtopdecls { ([],$1) }
414 cvtopdecls :: { [LHsDecl RdrName] }
415 : topdecls { cvTopDecls $1 }
417 -----------------------------------------------------------------------------
418 -- Module declaration & imports only
420 header :: { Located (HsModule RdrName) }
421 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' header_body
422 {% fileSrcSpan >>= \ loc -> case $1 of { (info, doc) ->
423 return (L loc (HsModule (Just $3) $5 $7 [] $4
425 | missing_module_keyword importdecls
426 {% fileSrcSpan >>= \ loc ->
427 return (L loc (HsModule Nothing Nothing $2 [] Nothing
428 emptyHaddockModInfo Nothing)) }
430 header_body :: { [LImportDecl RdrName] }
431 : '{' importdecls { $2 }
432 | vocurly importdecls { $2 }
434 -----------------------------------------------------------------------------
437 maybeexports :: { Maybe [LIE RdrName] }
438 : '(' exportlist ')' { Just $2 }
439 | {- empty -} { Nothing }
441 exportlist :: { [LIE RdrName] }
442 : expdoclist ',' expdoclist { $1 ++ $3 }
445 exportlist1 :: { [LIE RdrName] }
446 : expdoclist export expdoclist ',' exportlist { $1 ++ ($2 : $3) ++ $5 }
447 | expdoclist export expdoclist { $1 ++ ($2 : $3) }
450 expdoclist :: { [LIE RdrName] }
451 : exp_doc expdoclist { $1 : $2 }
454 exp_doc :: { LIE RdrName }
455 : docsection { L1 (case (unLoc $1) of (n, doc) -> IEGroup n doc) }
456 | docnamed { L1 (IEDocNamed ((fst . unLoc) $1)) }
457 | docnext { L1 (IEDoc (unLoc $1)) }
459 -- No longer allow things like [] and (,,,) to be exported
460 -- They are built in syntax, always available
461 export :: { LIE RdrName }
462 : qvar { L1 (IEVar (unLoc $1)) }
463 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
464 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
465 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
466 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
467 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
469 qcnames :: { [RdrName] }
470 : qcnames ',' qcname_ext { unLoc $3 : $1 }
471 | qcname_ext { [unLoc $1] }
473 qcname_ext :: { Located RdrName } -- Variable or data constructor
474 -- or tagged type constructor
476 | 'type' qcon { sL (comb2 $1 $2)
477 (setRdrNameSpace (unLoc $2)
480 -- Cannot pull into qcname_ext, as qcname is also used in expression.
481 qcname :: { Located RdrName } -- Variable or data constructor
485 -----------------------------------------------------------------------------
486 -- Import Declarations
488 -- import decls can be *empty*, or even just a string of semicolons
489 -- whereas topdecls must contain at least one topdecl.
491 importdecls :: { [LImportDecl RdrName] }
492 : importdecls ';' importdecl { $3 : $1 }
493 | importdecls ';' { $1 }
494 | importdecl { [ $1 ] }
497 importdecl :: { LImportDecl RdrName }
498 : 'import' maybe_src optqualified modid maybeas maybeimpspec
499 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
501 maybe_src :: { IsBootInterface }
502 : '{-# SOURCE' '#-}' { True }
503 | {- empty -} { False }
505 optqualified :: { Bool }
506 : 'qualified' { True }
507 | {- empty -} { False }
509 maybeas :: { Located (Maybe ModuleName) }
510 : 'as' modid { LL (Just (unLoc $2)) }
511 | {- empty -} { noLoc Nothing }
513 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
514 : impspec { L1 (Just (unLoc $1)) }
515 | {- empty -} { noLoc Nothing }
517 impspec :: { Located (Bool, [LIE RdrName]) }
518 : '(' exportlist ')' { LL (False, $2) }
519 | 'hiding' '(' exportlist ')' { LL (True, $3) }
521 -----------------------------------------------------------------------------
522 -- Fixity Declarations
526 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
528 infix :: { Located FixityDirection }
529 : 'infix' { L1 InfixN }
530 | 'infixl' { L1 InfixL }
531 | 'infixr' { L1 InfixR }
533 ops :: { Located [Located RdrName] }
534 : ops ',' op { LL ($3 : unLoc $1) }
537 -----------------------------------------------------------------------------
538 -- Top-Level Declarations
540 topdecls :: { OrdList (LHsDecl RdrName) }
541 : topdecls ';' topdecl { $1 `appOL` $3 }
542 | topdecls ';' { $1 }
545 topdecl :: { OrdList (LHsDecl RdrName) }
546 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
547 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
548 | 'instance' inst_type where_inst
549 { let (binds, sigs, ats, _) = cvBindsAndSigs (unLoc $3)
551 unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs ats)))}
552 | stand_alone_deriving { unitOL (LL (DerivD (unLoc $1))) }
553 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
554 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
555 | '{-# DEPRECATED' deprecations '#-}' { $2 }
556 | '{-# WARNING' warnings '#-}' { $2 }
557 | '{-# RULES' rules '#-}' { $2 }
560 -- Template Haskell Extension
561 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
562 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
563 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
568 cl_decl :: { LTyClDecl RdrName }
569 : 'class' tycl_hdr fds where_cls
570 {% do { let { (binds, sigs, ats, docs) =
571 cvBindsAndSigs (unLoc $4)
572 ; (ctxt, tc, tvs, tparms) = unLoc $2}
573 ; checkTyVars tparms -- only type vars allowed
575 ; return $ L (comb4 $1 $2 $3 $4)
576 (mkClassDecl (ctxt, tc, tvs)
577 (unLoc $3) sigs binds ats docs) } }
579 -- Type declarations (toplevel)
581 ty_decl :: { LTyClDecl RdrName }
582 -- ordinary type synonyms
583 : 'type' type '=' ctype
584 -- Note ctype, not sigtype, on the right of '='
585 -- We allow an explicit for-all but we don't insert one
586 -- in type Foo a = (b,b)
587 -- Instead we just say b is out of scope
589 -- Note the use of type for the head; this allows
590 -- infix type constructors to be declared
591 {% do { (tc, tvs, _) <- checkSynHdr $2 False
592 ; return (L (comb2 $1 $4)
593 (TySynonym tc tvs Nothing $4))
596 -- type family declarations
597 | 'type' 'family' type opt_kind_sig
598 -- Note the use of type for the head; this allows
599 -- infix type constructors to be declared
601 {% do { (tc, tvs, _) <- checkSynHdr $3 False
602 ; return (L (comb3 $1 $3 $4)
603 (TyFamily TypeFamily tc tvs (unLoc $4)))
606 -- type instance declarations
607 | 'type' 'instance' type '=' ctype
608 -- Note the use of type for the head; this allows
609 -- infix type constructors and type patterns
611 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
612 ; return (L (comb2 $1 $5)
613 (TySynonym tc tvs (Just typats) $5))
616 -- ordinary data type or newtype declaration
617 | data_or_newtype tycl_hdr constrs deriving
618 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
619 ; checkTyVars tparms -- no type pattern
621 sL (comb4 $1 $2 $3 $4)
622 -- We need the location on tycl_hdr in case
623 -- constrs and deriving are both empty
624 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
625 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
627 -- ordinary GADT declaration
628 | data_or_newtype tycl_hdr opt_kind_sig
629 'where' gadt_constrlist
631 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
632 ; checkTyVars tparms -- can have type pats
634 sL (comb4 $1 $2 $4 $5)
635 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
636 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
638 -- data/newtype family
639 | 'data' 'family' tycl_hdr opt_kind_sig
640 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
641 ; checkTyVars tparms -- no type pattern
642 ; unless (null (unLoc ctxt)) $ -- and no context
643 parseError (getLoc ctxt)
644 "A family declaration cannot have a context"
647 (TyFamily DataFamily tc tvs (unLoc $4)) } }
649 -- data/newtype instance declaration
650 | data_or_newtype 'instance' tycl_hdr constrs deriving
651 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
652 -- can have type pats
654 L (comb4 $1 $3 $4 $5)
655 -- We need the location on tycl_hdr in case
656 -- constrs and deriving are both empty
657 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
658 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
660 -- GADT instance declaration
661 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
662 'where' gadt_constrlist
664 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
665 -- can have type pats
667 L (comb4 $1 $3 $6 $7)
668 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
669 (unLoc $4) (reverse (unLoc $6)) (unLoc $7)) } }
671 -- Associate type family declarations
673 -- * They have a different syntax than on the toplevel (no family special
676 -- * They also need to be separate from instances; otherwise, data family
677 -- declarations without a kind signature cause parsing conflicts with empty
678 -- data declarations.
680 at_decl_cls :: { LTyClDecl RdrName }
681 -- type family declarations
682 : 'type' type opt_kind_sig
683 -- Note the use of type for the head; this allows
684 -- infix type constructors to be declared
686 {% do { (tc, tvs, _) <- checkSynHdr $2 False
687 ; return (L (comb3 $1 $2 $3)
688 (TyFamily TypeFamily tc tvs (unLoc $3)))
691 -- default type instance
692 | 'type' type '=' ctype
693 -- Note the use of type for the head; this allows
694 -- infix type constructors and type patterns
696 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
697 ; return (L (comb2 $1 $4)
698 (TySynonym tc tvs (Just typats) $4))
701 -- data/newtype family declaration
702 | 'data' tycl_hdr opt_kind_sig
703 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
704 ; checkTyVars tparms -- no type pattern
705 ; unless (null (unLoc ctxt)) $ -- and no context
706 parseError (getLoc ctxt)
707 "A family declaration cannot have a context"
710 (TyFamily DataFamily tc tvs (unLoc $3))
713 -- Associate type instances
715 at_decl_inst :: { LTyClDecl RdrName }
716 -- type instance declarations
717 : 'type' type '=' ctype
718 -- Note the use of type for the head; this allows
719 -- infix type constructors and type patterns
721 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
722 ; return (L (comb2 $1 $4)
723 (TySynonym tc tvs (Just typats) $4))
726 -- data/newtype instance declaration
727 | data_or_newtype tycl_hdr constrs deriving
728 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
729 -- can have type pats
731 L (comb4 $1 $2 $3 $4)
732 -- We need the location on tycl_hdr in case
733 -- constrs and deriving are both empty
734 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
735 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
737 -- GADT instance declaration
738 | data_or_newtype tycl_hdr opt_kind_sig
739 'where' gadt_constrlist
741 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
742 -- can have type pats
744 L (comb4 $1 $2 $5 $6)
745 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
746 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
748 data_or_newtype :: { Located NewOrData }
749 : 'data' { L1 DataType }
750 | 'newtype' { L1 NewType }
752 opt_kind_sig :: { Located (Maybe Kind) }
754 | '::' kind { LL (Just (unLoc $2)) }
756 -- tycl_hdr parses the header of a class or data type decl,
757 -- which takes the form
760 -- (Eq a, Ord b) => T a b
761 -- T Int [a] -- for associated types
762 -- Rather a lot of inlining here, else we get reduce/reduce errors
763 tycl_hdr :: { Located (LHsContext RdrName,
765 [LHsTyVarBndr RdrName],
767 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
768 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
770 -----------------------------------------------------------------------------
771 -- Stand-alone deriving
773 -- Glasgow extension: stand-alone deriving declarations
774 stand_alone_deriving :: { LDerivDecl RdrName }
775 : 'deriving' 'instance' inst_type {% checkDerivDecl (LL (DerivDecl $3)) }
777 -----------------------------------------------------------------------------
778 -- Nested declarations
780 -- Declaration in class bodies
782 decl_cls :: { Located (OrdList (LHsDecl RdrName)) }
783 decl_cls : at_decl_cls { LL (unitOL (L1 (TyClD (unLoc $1)))) }
786 decls_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
787 : decls_cls ';' decl_cls { LL (unLoc $1 `appOL` unLoc $3) }
788 | decls_cls ';' { LL (unLoc $1) }
790 | {- empty -} { noLoc nilOL }
794 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
795 : '{' decls_cls '}' { LL (unLoc $2) }
796 | vocurly decls_cls close { $2 }
800 where_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
801 -- No implicit parameters
802 -- May have type declarations
803 : 'where' decllist_cls { LL (unLoc $2) }
804 | {- empty -} { noLoc nilOL }
806 -- Declarations in instance bodies
808 decl_inst :: { Located (OrdList (LHsDecl RdrName)) }
809 decl_inst : at_decl_inst { LL (unitOL (L1 (TyClD (unLoc $1)))) }
812 decls_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
813 : decls_inst ';' decl_inst { LL (unLoc $1 `appOL` unLoc $3) }
814 | decls_inst ';' { LL (unLoc $1) }
816 | {- empty -} { noLoc nilOL }
819 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
820 : '{' decls_inst '}' { LL (unLoc $2) }
821 | vocurly decls_inst close { $2 }
825 where_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
826 -- No implicit parameters
827 -- May have type declarations
828 : 'where' decllist_inst { LL (unLoc $2) }
829 | {- empty -} { noLoc nilOL }
831 -- Declarations in binding groups other than classes and instances
833 decls :: { Located (OrdList (LHsDecl RdrName)) }
834 : decls ';' decl { let { this = unLoc $3;
836 these = rest `appOL` this }
837 in rest `seq` this `seq` these `seq`
839 | decls ';' { LL (unLoc $1) }
841 | {- empty -} { noLoc nilOL }
843 decllist :: { Located (OrdList (LHsDecl RdrName)) }
844 : '{' decls '}' { LL (unLoc $2) }
845 | vocurly decls close { $2 }
847 -- Binding groups other than those of class and instance declarations
849 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
850 -- No type declarations
851 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
852 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
853 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
855 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
856 -- No type declarations
857 : 'where' binds { LL (unLoc $2) }
858 | {- empty -} { noLoc emptyLocalBinds }
861 -----------------------------------------------------------------------------
862 -- Transformation Rules
864 rules :: { OrdList (LHsDecl RdrName) }
865 : rules ';' rule { $1 `snocOL` $3 }
868 | {- empty -} { nilOL }
870 rule :: { LHsDecl RdrName }
871 : STRING activation rule_forall infixexp '=' exp
872 { LL $ RuleD (HsRule (getSTRING $1)
873 ($2 `orElse` AlwaysActive)
874 $3 $4 placeHolderNames $6 placeHolderNames) }
876 activation :: { Maybe Activation }
877 : {- empty -} { Nothing }
878 | explicit_activation { Just $1 }
880 explicit_activation :: { Activation } -- In brackets
881 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
882 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
884 rule_forall :: { [RuleBndr RdrName] }
885 : 'forall' rule_var_list '.' { $2 }
888 rule_var_list :: { [RuleBndr RdrName] }
890 | rule_var rule_var_list { $1 : $2 }
892 rule_var :: { RuleBndr RdrName }
893 : varid { RuleBndr $1 }
894 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
896 -----------------------------------------------------------------------------
897 -- Warnings and deprecations (c.f. rules)
899 warnings :: { OrdList (LHsDecl RdrName) }
900 : warnings ';' warning { $1 `appOL` $3 }
901 | warnings ';' { $1 }
903 | {- empty -} { nilOL }
905 -- SUP: TEMPORARY HACK, not checking for `module Foo'
906 warning :: { OrdList (LHsDecl RdrName) }
908 { toOL [ LL $ WarningD (Warning n (WarningTxt (getSTRING $2)))
911 deprecations :: { OrdList (LHsDecl RdrName) }
912 : deprecations ';' deprecation { $1 `appOL` $3 }
913 | deprecations ';' { $1 }
915 | {- empty -} { nilOL }
917 -- SUP: TEMPORARY HACK, not checking for `module Foo'
918 deprecation :: { OrdList (LHsDecl RdrName) }
920 { toOL [ LL $ WarningD (Warning n (DeprecatedTxt (getSTRING $2)))
924 -----------------------------------------------------------------------------
925 -- Foreign import and export declarations
927 fdecl :: { LHsDecl RdrName }
928 fdecl : 'import' callconv safety fspec
929 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
930 | 'import' callconv fspec
931 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
933 | 'export' callconv fspec
934 {% mkExport $2 (unLoc $3) >>= return.LL }
936 callconv :: { CallConv }
937 : 'stdcall' { CCall StdCallConv }
938 | 'ccall' { CCall CCallConv }
939 | 'dotnet' { DNCall }
942 : 'unsafe' { PlayRisky }
943 | 'safe' { PlaySafe False }
944 | 'threadsafe' { PlaySafe True }
946 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
947 : STRING var '::' sigtypedoc { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
948 | var '::' sigtypedoc { LL (noLoc nilFS, $1, $3) }
949 -- if the entity string is missing, it defaults to the empty string;
950 -- the meaning of an empty entity string depends on the calling
953 -----------------------------------------------------------------------------
956 opt_sig :: { Maybe (LHsType RdrName) }
957 : {- empty -} { Nothing }
958 | '::' sigtype { Just $2 }
960 opt_asig :: { Maybe (LHsType RdrName) }
961 : {- empty -} { Nothing }
962 | '::' atype { Just $2 }
964 sigtypes1 :: { [LHsType RdrName] }
966 | sigtype ',' sigtypes1 { $1 : $3 }
968 sigtype :: { LHsType RdrName }
969 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
970 -- Wrap an Implicit forall if there isn't one there already
972 sigtypedoc :: { LHsType RdrName }
973 : ctypedoc { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
974 -- Wrap an Implicit forall if there isn't one there already
976 sig_vars :: { Located [Located RdrName] }
977 : sig_vars ',' var { LL ($3 : unLoc $1) }
980 -----------------------------------------------------------------------------
983 infixtype :: { LHsType RdrName }
984 : btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
985 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
987 infixtypedoc :: { LHsType RdrName }
989 | infixtype docprev { LL $ HsDocTy $1 $2 }
991 gentypedoc :: { LHsType RdrName }
994 | infixtypedoc { $1 }
995 | btype '->' ctypedoc { LL $ HsFunTy $1 $3 }
996 | btypedoc '->' ctypedoc { LL $ HsFunTy $1 $3 }
998 ctypedoc :: { LHsType RdrName }
999 : 'forall' tv_bndrs '.' ctypedoc { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
1000 | context '=>' ctypedoc { LL $ mkImplicitHsForAllTy $1 $3 }
1001 -- A type of form (context => type) is an *implicit* HsForAllTy
1004 strict_mark :: { Located HsBang }
1005 : '!' { L1 HsStrict }
1006 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
1008 -- A ctype is a for-all type
1009 ctype :: { LHsType RdrName }
1010 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
1011 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
1012 -- A type of form (context => type) is an *implicit* HsForAllTy
1015 -- We parse a context as a btype so that we don't get reduce/reduce
1016 -- errors in ctype. The basic problem is that
1018 -- looks so much like a tuple type. We can't tell until we find the =>
1020 -- We have the t1 ~ t2 form here and in gentype, to permit an individual
1021 -- equational constraint without parenthesis.
1022 context :: { LHsContext RdrName }
1023 : btype '~' btype {% checkContext
1024 (LL $ HsPredTy (HsEqualP $1 $3)) }
1025 | btype {% checkContext $1 }
1027 type :: { LHsType RdrName }
1028 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
1031 gentype :: { LHsType RdrName }
1033 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
1034 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
1035 | btype '->' ctype { LL $ HsFunTy $1 $3 }
1036 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1038 btype :: { LHsType RdrName }
1039 : btype atype { LL $ HsAppTy $1 $2 }
1042 btypedoc :: { LHsType RdrName }
1043 : btype atype docprev { LL $ HsDocTy (L (comb2 $1 $2) (HsAppTy $1 $2)) $3 }
1044 | atype docprev { LL $ HsDocTy $1 $2 }
1046 atype :: { LHsType RdrName }
1047 : gtycon { L1 (HsTyVar (unLoc $1)) }
1048 | tyvar { L1 (HsTyVar (unLoc $1)) }
1049 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
1050 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
1051 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
1052 | '[' ctype ']' { LL $ HsListTy $2 }
1053 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
1054 | '(' ctype ')' { LL $ HsParTy $2 }
1055 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
1057 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
1059 -- An inst_type is what occurs in the head of an instance decl
1060 -- e.g. (Foo a, Gaz b) => Wibble a b
1061 -- It's kept as a single type, with a MonoDictTy at the right
1062 -- hand corner, for convenience.
1063 inst_type :: { LHsType RdrName }
1064 : sigtype {% checkInstType $1 }
1066 inst_types1 :: { [LHsType RdrName] }
1067 : inst_type { [$1] }
1068 | inst_type ',' inst_types1 { $1 : $3 }
1070 comma_types0 :: { [LHsType RdrName] }
1071 : comma_types1 { $1 }
1072 | {- empty -} { [] }
1074 comma_types1 :: { [LHsType RdrName] }
1076 | ctype ',' comma_types1 { $1 : $3 }
1078 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1079 : tv_bndr tv_bndrs { $1 : $2 }
1080 | {- empty -} { [] }
1082 tv_bndr :: { LHsTyVarBndr RdrName }
1083 : tyvar { L1 (UserTyVar (unLoc $1)) }
1084 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
1087 fds :: { Located [Located ([RdrName], [RdrName])] }
1088 : {- empty -} { noLoc [] }
1089 | '|' fds1 { LL (reverse (unLoc $2)) }
1091 fds1 :: { Located [Located ([RdrName], [RdrName])] }
1092 : fds1 ',' fd { LL ($3 : unLoc $1) }
1095 fd :: { Located ([RdrName], [RdrName]) }
1096 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
1097 (reverse (unLoc $1), reverse (unLoc $3)) }
1099 varids0 :: { Located [RdrName] }
1100 : {- empty -} { noLoc [] }
1101 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
1103 -----------------------------------------------------------------------------
1106 kind :: { Located Kind }
1108 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1110 akind :: { Located Kind }
1111 : '*' { L1 liftedTypeKind }
1112 | '!' { L1 unliftedTypeKind }
1113 | '(' kind ')' { LL (unLoc $2) }
1116 -----------------------------------------------------------------------------
1117 -- Datatype declarations
1119 gadt_constrlist :: { Located [LConDecl RdrName] }
1120 : '{' gadt_constrs '}' { LL (unLoc $2) }
1121 | vocurly gadt_constrs close { $2 }
1123 gadt_constrs :: { Located [LConDecl RdrName] }
1124 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
1125 | gadt_constrs ';' { $1 }
1126 | gadt_constr { L1 [$1] }
1128 -- We allow the following forms:
1129 -- C :: Eq a => a -> T a
1130 -- C :: forall a. Eq a => !a -> T a
1131 -- D { x,y :: a } :: T a
1132 -- forall a. Eq a => D { x,y :: a } :: T a
1134 gadt_constr :: { LConDecl RdrName }
1136 { LL (mkGadtDecl $1 $3) }
1137 -- Syntax: Maybe merge the record stuff with the single-case above?
1138 -- (to kill the mostly harmless reduce/reduce error)
1139 -- XXX revisit audreyt
1140 | constr_stuff_record '::' sigtype
1141 { let (con,details) = unLoc $1 in
1142 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3) Nothing) }
1144 | forall context '=>' constr_stuff_record '::' sigtype
1145 { let (con,details) = unLoc $4 in
1146 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6) Nothing ) }
1147 | forall constr_stuff_record '::' sigtype
1148 { let (con,details) = unLoc $2 in
1149 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4) Nothing) }
1153 constrs :: { Located [LConDecl RdrName] }
1154 : {- empty; a GHC extension -} { noLoc [] }
1155 | maybe_docnext '=' constrs1 { L (comb2 $2 $3) (addConDocs (unLoc $3) $1) }
1157 constrs1 :: { Located [LConDecl RdrName] }
1158 : constrs1 maybe_docnext '|' maybe_docprev constr { LL (addConDoc $5 $2 : addConDocFirst (unLoc $1) $4) }
1159 | constr { L1 [$1] }
1161 constr :: { LConDecl RdrName }
1162 : maybe_docnext forall context '=>' constr_stuff maybe_docprev
1163 { let (con,details) = unLoc $5 in
1164 L (comb4 $2 $3 $4 $5) (ConDecl con Explicit (unLoc $2) $3 details ResTyH98 ($1 `mplus` $6)) }
1165 | maybe_docnext forall constr_stuff maybe_docprev
1166 { let (con,details) = unLoc $3 in
1167 L (comb2 $2 $3) (ConDecl con Explicit (unLoc $2) (noLoc []) details ResTyH98 ($1 `mplus` $4)) }
1169 forall :: { Located [LHsTyVarBndr RdrName] }
1170 : 'forall' tv_bndrs '.' { LL $2 }
1171 | {- empty -} { noLoc [] }
1173 constr_stuff :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1174 -- We parse the constructor declaration
1176 -- as a btype (treating C as a type constructor) and then convert C to be
1177 -- a data constructor. Reason: it might continue like this:
1179 -- in which case C really would be a type constructor. We can't resolve this
1180 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1181 : btype {% mkPrefixCon $1 [] >>= return.LL }
1182 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
1183 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
1184 | btype conop btype { LL ($2, InfixCon $1 $3) }
1186 constr_stuff_record :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1187 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
1188 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
1190 fielddecls :: { [([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName))] }
1191 : fielddecl maybe_docnext ',' maybe_docprev fielddecls { addFieldDoc (unLoc $1) $4 : addFieldDocs $5 $2 }
1192 | fielddecl { [unLoc $1] }
1194 fielddecl :: { Located ([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName)) }
1195 : maybe_docnext sig_vars '::' ctype maybe_docprev { L (comb3 $2 $3 $4) (reverse (unLoc $2), $4, $1 `mplus` $5) }
1197 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1198 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1199 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1200 -- We don't allow a context, but that's sorted out by the type checker.
1201 deriving :: { Located (Maybe [LHsType RdrName]) }
1202 : {- empty -} { noLoc Nothing }
1203 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1204 ; p <- checkInstType (L loc (HsTyVar tv))
1205 ; return (LL (Just [p])) } }
1206 | 'deriving' '(' ')' { LL (Just []) }
1207 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1208 -- Glasgow extension: allow partial
1209 -- applications in derivings
1211 -----------------------------------------------------------------------------
1212 -- Value definitions
1214 {- There's an awkward overlap with a type signature. Consider
1215 f :: Int -> Int = ...rhs...
1216 Then we can't tell whether it's a type signature or a value
1217 definition with a result signature until we see the '='.
1218 So we have to inline enough to postpone reductions until we know.
1222 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1223 instead of qvar, we get another shift/reduce-conflict. Consider the
1226 { (^^) :: Int->Int ; } Type signature; only var allowed
1228 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1229 qvar allowed (because of instance decls)
1231 We can't tell whether to reduce var to qvar until after we've read the signatures.
1234 docdecl :: { LHsDecl RdrName }
1235 : docdecld { L1 (DocD (unLoc $1)) }
1237 docdecld :: { LDocDecl RdrName }
1238 : docnext { L1 (DocCommentNext (unLoc $1)) }
1239 | docprev { L1 (DocCommentPrev (unLoc $1)) }
1240 | docnamed { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1241 | docsection { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1243 decl :: { Located (OrdList (LHsDecl RdrName)) }
1245 | '!' aexp rhs {% do { pat <- checkPattern $2;
1246 return (LL $ unitOL $ LL $ ValD (
1247 PatBind (LL $ BangPat pat) (unLoc $3)
1248 placeHolderType placeHolderNames)) } }
1249 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1250 let { l = comb2 $1 $> };
1251 return $! (sL l (unitOL $! (sL l $ ValD r))) } }
1252 | docdecl { LL $ unitOL $1 }
1254 rhs :: { Located (GRHSs RdrName) }
1255 : '=' exp wherebinds { sL (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1256 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1258 gdrhs :: { Located [LGRHS RdrName] }
1259 : gdrhs gdrh { LL ($2 : unLoc $1) }
1262 gdrh :: { LGRHS RdrName }
1263 : '|' guardquals '=' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1265 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1266 : infixexp '::' sigtypedoc
1267 {% do s <- checkValSig $1 $3;
1268 return (LL $ unitOL (LL $ SigD s)) }
1269 -- See the above notes for why we need infixexp here
1270 | var ',' sig_vars '::' sigtypedoc
1271 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1272 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1274 | '{-# INLINE' activation qvar '#-}'
1275 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1276 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1277 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1279 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1280 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1282 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1283 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1285 -----------------------------------------------------------------------------
1288 exp :: { LHsExpr RdrName }
1289 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1290 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1291 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1292 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1293 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1296 infixexp :: { LHsExpr RdrName }
1298 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1300 exp10 :: { LHsExpr RdrName }
1301 : '\\' apat apats opt_asig '->' exp
1302 { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1305 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1306 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1307 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1308 | '-' fexp { LL $ NegApp $2 noSyntaxExpr }
1310 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1311 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1312 return (L loc (mkHsDo DoExpr stmts body)) }
1313 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1314 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1315 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1316 | scc_annot exp { LL $ if opt_SccProfilingOn
1317 then HsSCC (unLoc $1) $2
1319 | hpc_annot exp { LL $ if opt_Hpc
1320 then HsTickPragma (unLoc $1) $2
1323 | 'proc' aexp '->' exp
1324 {% checkPattern $2 >>= \ p ->
1325 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1326 placeHolderType undefined)) }
1327 -- TODO: is LL right here?
1329 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1330 -- hdaume: core annotation
1333 scc_annot :: { Located FastString }
1334 : '_scc_' STRING {% (addWarning Opt_WarnWarningsDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1335 ( do scc <- getSCC $2; return $ LL scc ) }
1336 | '{-# SCC' STRING '#-}' {% do scc <- getSCC $2; return $ LL scc }
1338 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1339 : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1340 { LL $ (getSTRING $2
1341 ,( fromInteger $ getINTEGER $3
1342 , fromInteger $ getINTEGER $5
1344 ,( fromInteger $ getINTEGER $7
1345 , fromInteger $ getINTEGER $9
1350 fexp :: { LHsExpr RdrName }
1351 : fexp aexp { LL $ HsApp $1 $2 }
1354 aexp :: { LHsExpr RdrName }
1355 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1356 | '~' aexp { LL $ ELazyPat $2 }
1359 aexp1 :: { LHsExpr RdrName }
1360 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1364 -- Here was the syntax for type applications that I was planning
1365 -- but there are difficulties (e.g. what order for type args)
1366 -- so it's not enabled yet.
1367 -- But this case *is* used for the left hand side of a generic definition,
1368 -- which is parsed as an expression before being munged into a pattern
1369 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1370 (sL (getLoc $3) (HsType $3)) }
1372 aexp2 :: { LHsExpr RdrName }
1373 : ipvar { L1 (HsIPVar $! unLoc $1) }
1374 | qcname { L1 (HsVar $! unLoc $1) }
1375 | literal { L1 (HsLit $! unLoc $1) }
1376 -- This will enable overloaded strings permanently. Normally the renamer turns HsString
1377 -- into HsOverLit when -foverloaded-strings is on.
1378 -- | STRING { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1379 | INTEGER { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1380 | RATIONAL { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1381 -- N.B.: sections get parsed by these next two productions.
1382 -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't correct Haskell98
1383 -- (you'd have to write '((+ 3), (4 -))')
1384 -- but the less cluttered version fell out of having texps.
1385 | '(' texp ')' { LL (HsPar $2) }
1386 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1387 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1388 | '[' list ']' { LL (unLoc $2) }
1389 | '[:' parr ':]' { LL (unLoc $2) }
1390 | '_' { L1 EWildPat }
1392 -- Template Haskell Extension
1393 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1394 (L1 $ HsVar (mkUnqual varName
1395 (getTH_ID_SPLICE $1)))) } -- $x
1396 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1398 | TH_QUASIQUOTE { let { loc = getLoc $1
1399 ; ITquasiQuote (quoter, quote, quoteSpan) = unLoc $1
1400 ; quoterId = mkUnqual varName quoter
1402 in sL loc $ HsQuasiQuoteE (mkHsQuasiQuote quoterId quoteSpan quote) }
1403 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1404 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1405 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1406 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1407 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1408 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1409 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1410 return (LL $ HsBracket (PatBr p)) }
1411 | '[d|' cvtopbody '|]' {% checkDecBrGroup $2 >>= \g ->
1412 return (LL $ HsBracket (DecBr g)) }
1414 -- arrow notation extension
1415 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1417 cmdargs :: { [LHsCmdTop RdrName] }
1418 : cmdargs acmd { $2 : $1 }
1419 | {- empty -} { [] }
1421 acmd :: { LHsCmdTop RdrName }
1422 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1424 cvtopbody :: { [LHsDecl RdrName] }
1425 : '{' cvtopdecls0 '}' { $2 }
1426 | vocurly cvtopdecls0 close { $2 }
1428 cvtopdecls0 :: { [LHsDecl RdrName] }
1429 : {- empty -} { [] }
1432 -- "texp" is short for tuple expressions:
1433 -- things that can appear unparenthesized as long as they're
1434 -- inside parens or delimitted by commas
1435 texp :: { LHsExpr RdrName }
1438 -- Note [Parsing sections]
1439 -- ~~~~~~~~~~~~~~~~~~~~~~~
1440 -- We include left and right sections here, which isn't
1441 -- technically right according to Haskell 98. For example
1442 -- (3 +, True) isn't legal
1443 -- However, we want to parse bang patterns like
1445 -- and it's convenient to do so here as a section
1446 -- Then when converting expr to pattern we unravel it again
1447 -- Meanwhile, the renamer checks that real sections appear
1449 | infixexp qop { LL $ SectionL $1 $2 }
1450 | qopm infixexp { LL $ SectionR $1 $2 }
1452 -- View patterns get parenthesized above
1453 | exp '->' exp { LL $ EViewPat $1 $3 }
1455 texps :: { [LHsExpr RdrName] }
1456 : texps ',' texp { $3 : $1 }
1460 -----------------------------------------------------------------------------
1463 -- The rules below are little bit contorted to keep lexps left-recursive while
1464 -- avoiding another shift/reduce-conflict.
1466 list :: { LHsExpr RdrName }
1467 : texp { L1 $ ExplicitList placeHolderType [$1] }
1468 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1469 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1470 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1471 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1472 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1473 | texp '|' flattenedpquals { sL (comb2 $1 $>) $ mkHsDo ListComp (unLoc $3) $1 }
1475 lexps :: { Located [LHsExpr RdrName] }
1476 : lexps ',' texp { LL (((:) $! $3) $! unLoc $1) }
1477 | texp ',' texp { LL [$3,$1] }
1479 -----------------------------------------------------------------------------
1480 -- List Comprehensions
1482 flattenedpquals :: { Located [LStmt RdrName] }
1483 : pquals { case (unLoc $1) of
1484 ParStmt [(qs, _)] -> L1 qs
1485 -- We just had one thing in our "parallel" list so
1486 -- we simply return that thing directly
1489 -- We actually found some actual parallel lists so
1490 -- we leave them into as a ParStmt
1493 pquals :: { LStmt RdrName }
1494 : pquals1 { L1 (ParStmt [(qs, undefined) | qs <- (reverse (unLoc $1))]) }
1496 pquals1 :: { Located [[LStmt RdrName]] }
1497 : pquals1 '|' squals { LL (unLoc $3 : unLoc $1) }
1498 | squals { L (getLoc $1) [unLoc $1] }
1500 squals :: { Located [LStmt RdrName] }
1501 : squals1 { L (getLoc $1) (reverse (unLoc $1)) }
1503 squals1 :: { Located [LStmt RdrName] }
1504 : transformquals1 { LL (unLoc $1) }
1506 transformquals1 :: { Located [LStmt RdrName] }
1507 : transformquals1 ',' transformqual { LL $ [LL ((unLoc $3) (unLoc $1))] }
1508 | transformquals1 ',' qual { LL ($3 : unLoc $1) }
1509 -- | transformquals1 ',' '{|' pquals '|}' { LL ($4 : unLoc $1) }
1510 | transformqual { LL $ [LL ((unLoc $1) [])] }
1512 -- | '{|' pquals '|}' { L1 [$2] }
1515 -- It is possible to enable bracketing (associating) qualifier lists by uncommenting the lines with {| |}
1516 -- above. Due to a lack of consensus on the syntax, this feature is not being used until we get user
1517 -- demand. Note that the {| |} symbols are reused from -XGenerics and hence if you want to compile
1518 -- a program that makes use of this temporary syntax you must supply that flag to GHC
1520 transformqual :: { Located ([LStmt RdrName] -> Stmt RdrName) }
1521 : 'then' exp { LL $ \leftStmts -> (mkTransformStmt (reverse leftStmts) $2) }
1522 | 'then' exp 'by' exp { LL $ \leftStmts -> (mkTransformByStmt (reverse leftStmts) $2 $4) }
1523 | 'then' 'group' 'by' exp { LL $ \leftStmts -> (mkGroupByStmt (reverse leftStmts) $4) }
1524 | 'then' 'group' 'using' exp { LL $ \leftStmts -> (mkGroupUsingStmt (reverse leftStmts) $4) }
1525 | 'then' 'group' 'by' exp 'using' exp { LL $ \leftStmts -> (mkGroupByUsingStmt (reverse leftStmts) $4 $6) }
1527 -----------------------------------------------------------------------------
1528 -- Parallel array expressions
1530 -- The rules below are little bit contorted; see the list case for details.
1531 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1532 -- Moreover, we allow explicit arrays with no element (represented by the nil
1533 -- constructor in the list case).
1535 parr :: { LHsExpr RdrName }
1536 : { noLoc (ExplicitPArr placeHolderType []) }
1537 | texp { L1 $ ExplicitPArr placeHolderType [$1] }
1538 | lexps { L1 $ ExplicitPArr placeHolderType
1539 (reverse (unLoc $1)) }
1540 | texp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1541 | texp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1542 | texp '|' flattenedpquals { LL $ mkHsDo PArrComp (unLoc $3) $1 }
1544 -- We are reusing `lexps' and `flattenedpquals' from the list case.
1546 -----------------------------------------------------------------------------
1549 guardquals :: { Located [LStmt RdrName] }
1550 : guardquals1 { L (getLoc $1) (reverse (unLoc $1)) }
1552 guardquals1 :: { Located [LStmt RdrName] }
1553 : guardquals1 ',' qual { LL ($3 : unLoc $1) }
1556 -----------------------------------------------------------------------------
1557 -- Case alternatives
1559 altslist :: { Located [LMatch RdrName] }
1560 : '{' alts '}' { LL (reverse (unLoc $2)) }
1561 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1563 alts :: { Located [LMatch RdrName] }
1564 : alts1 { L1 (unLoc $1) }
1565 | ';' alts { LL (unLoc $2) }
1567 alts1 :: { Located [LMatch RdrName] }
1568 : alts1 ';' alt { LL ($3 : unLoc $1) }
1569 | alts1 ';' { LL (unLoc $1) }
1572 alt :: { LMatch RdrName }
1573 : pat opt_sig alt_rhs { LL (Match [$1] $2 (unLoc $3)) }
1575 alt_rhs :: { Located (GRHSs RdrName) }
1576 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1578 ralt :: { Located [LGRHS RdrName] }
1579 : '->' exp { LL (unguardedRHS $2) }
1580 | gdpats { L1 (reverse (unLoc $1)) }
1582 gdpats :: { Located [LGRHS RdrName] }
1583 : gdpats gdpat { LL ($2 : unLoc $1) }
1586 gdpat :: { LGRHS RdrName }
1587 : '|' guardquals '->' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1589 -- 'pat' recognises a pattern, including one with a bang at the top
1590 -- e.g. "!x" or "!(x,y)" or "C a b" etc
1591 -- Bangs inside are parsed as infix operator applications, so that
1592 -- we parse them right when bang-patterns are off
1593 pat :: { LPat RdrName }
1594 pat : exp {% checkPattern $1 }
1595 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1597 apat :: { LPat RdrName }
1598 apat : aexp {% checkPattern $1 }
1599 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1601 apats :: { [LPat RdrName] }
1602 : apat apats { $1 : $2 }
1603 | {- empty -} { [] }
1605 -----------------------------------------------------------------------------
1606 -- Statement sequences
1608 stmtlist :: { Located [LStmt RdrName] }
1609 : '{' stmts '}' { LL (unLoc $2) }
1610 | vocurly stmts close { $2 }
1612 -- do { ;; s ; s ; ; s ;; }
1613 -- The last Stmt should be an expression, but that's hard to enforce
1614 -- here, because we need too much lookahead if we see do { e ; }
1615 -- So we use ExprStmts throughout, and switch the last one over
1616 -- in ParseUtils.checkDo instead
1617 stmts :: { Located [LStmt RdrName] }
1618 : stmt stmts_help { LL ($1 : unLoc $2) }
1619 | ';' stmts { LL (unLoc $2) }
1620 | {- empty -} { noLoc [] }
1622 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1623 : ';' stmts { LL (unLoc $2) }
1624 | {- empty -} { noLoc [] }
1626 -- For typing stmts at the GHCi prompt, where
1627 -- the input may consist of just comments.
1628 maybe_stmt :: { Maybe (LStmt RdrName) }
1630 | {- nothing -} { Nothing }
1632 stmt :: { LStmt RdrName }
1634 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1636 qual :: { LStmt RdrName }
1637 : pat '<-' exp { LL $ mkBindStmt $1 $3 }
1638 | exp { L1 $ mkExprStmt $1 }
1639 | 'let' binds { LL $ LetStmt (unLoc $2) }
1641 -----------------------------------------------------------------------------
1642 -- Record Field Update/Construction
1644 fbinds :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1646 | {- empty -} { ([], False) }
1648 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1649 : fbind ',' fbinds1 { case $3 of (flds, dd) -> ($1 : flds, dd) }
1650 | fbind { ([$1], False) }
1651 | '..' { ([], True) }
1653 fbind :: { HsRecField RdrName (LHsExpr RdrName) }
1654 : qvar '=' exp { HsRecField $1 $3 False }
1655 | qvar { HsRecField $1 (L (getLoc $1) (HsVar (unLoc $1))) True }
1656 -- Here's where we say that plain 'x'
1657 -- means exactly 'x = x'. The pun-flag boolean is
1658 -- there so we can still print it right
1660 -----------------------------------------------------------------------------
1661 -- Implicit Parameter Bindings
1663 dbinds :: { Located [LIPBind RdrName] }
1664 : dbinds ';' dbind { let { this = $3; rest = unLoc $1 }
1665 in rest `seq` this `seq` LL (this : rest) }
1666 | dbinds ';' { LL (unLoc $1) }
1667 | dbind { let this = $1 in this `seq` L1 [this] }
1668 -- | {- empty -} { [] }
1670 dbind :: { LIPBind RdrName }
1671 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1673 ipvar :: { Located (IPName RdrName) }
1674 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1676 -----------------------------------------------------------------------------
1677 -- Warnings and deprecations
1679 namelist :: { Located [RdrName] }
1680 namelist : name_var { L1 [unLoc $1] }
1681 | name_var ',' namelist { LL (unLoc $1 : unLoc $3) }
1683 name_var :: { Located RdrName }
1684 name_var : var { $1 }
1687 -----------------------------------------
1688 -- Data constructors
1689 qcon :: { Located RdrName }
1691 | '(' qconsym ')' { LL (unLoc $2) }
1692 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1693 -- The case of '[:' ':]' is part of the production `parr'
1695 con :: { Located RdrName }
1697 | '(' consym ')' { LL (unLoc $2) }
1698 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1700 sysdcon :: { Located DataCon } -- Wired in data constructors
1701 : '(' ')' { LL unitDataCon }
1702 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1703 | '(#' '#)' { LL $ unboxedSingletonDataCon }
1704 | '(#' commas '#)' { LL $ tupleCon Unboxed $2 }
1705 | '[' ']' { LL nilDataCon }
1707 conop :: { Located RdrName }
1709 | '`' conid '`' { LL (unLoc $2) }
1711 qconop :: { Located RdrName }
1713 | '`' qconid '`' { LL (unLoc $2) }
1715 -----------------------------------------------------------------------------
1716 -- Type constructors
1718 gtycon :: { Located RdrName } -- A "general" qualified tycon
1720 | '(' ')' { LL $ getRdrName unitTyCon }
1721 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1722 | '(#' '#)' { LL $ getRdrName unboxedSingletonTyCon }
1723 | '(#' commas '#)' { LL $ getRdrName (tupleTyCon Unboxed $2) }
1724 | '(' '->' ')' { LL $ getRdrName funTyCon }
1725 | '[' ']' { LL $ listTyCon_RDR }
1726 | '[:' ':]' { LL $ parrTyCon_RDR }
1728 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1730 | '(' qtyconsym ')' { LL (unLoc $2) }
1732 qtyconop :: { Located RdrName } -- Qualified or unqualified
1734 | '`' qtycon '`' { LL (unLoc $2) }
1736 qtycon :: { Located RdrName } -- Qualified or unqualified
1737 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1740 tycon :: { Located RdrName } -- Unqualified
1741 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1743 qtyconsym :: { Located RdrName }
1744 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1747 tyconsym :: { Located RdrName }
1748 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1750 -----------------------------------------------------------------------------
1753 op :: { Located RdrName } -- used in infix decls
1757 varop :: { Located RdrName }
1759 | '`' varid '`' { LL (unLoc $2) }
1761 qop :: { LHsExpr RdrName } -- used in sections
1762 : qvarop { L1 $ HsVar (unLoc $1) }
1763 | qconop { L1 $ HsVar (unLoc $1) }
1765 qopm :: { LHsExpr RdrName } -- used in sections
1766 : qvaropm { L1 $ HsVar (unLoc $1) }
1767 | qconop { L1 $ HsVar (unLoc $1) }
1769 qvarop :: { Located RdrName }
1771 | '`' qvarid '`' { LL (unLoc $2) }
1773 qvaropm :: { Located RdrName }
1774 : qvarsym_no_minus { $1 }
1775 | '`' qvarid '`' { LL (unLoc $2) }
1777 -----------------------------------------------------------------------------
1780 tyvar :: { Located RdrName }
1781 tyvar : tyvarid { $1 }
1782 | '(' tyvarsym ')' { LL (unLoc $2) }
1784 tyvarop :: { Located RdrName }
1785 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1788 tyvarid :: { Located RdrName }
1789 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1790 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1791 | 'unsafe' { L1 $! mkUnqual tvName (fsLit "unsafe") }
1792 | 'safe' { L1 $! mkUnqual tvName (fsLit "safe") }
1793 | 'threadsafe' { L1 $! mkUnqual tvName (fsLit "threadsafe") }
1795 tyvarsym :: { Located RdrName }
1796 -- Does not include "!", because that is used for strictness marks
1797 -- or ".", because that separates the quantified type vars from the rest
1798 -- or "*", because that's used for kinds
1799 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1801 -----------------------------------------------------------------------------
1804 var :: { Located RdrName }
1806 | '(' varsym ')' { LL (unLoc $2) }
1808 qvar :: { Located RdrName }
1810 | '(' varsym ')' { LL (unLoc $2) }
1811 | '(' qvarsym1 ')' { LL (unLoc $2) }
1812 -- We've inlined qvarsym here so that the decision about
1813 -- whether it's a qvar or a var can be postponed until
1814 -- *after* we see the close paren.
1816 qvarid :: { Located RdrName }
1818 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1820 varid :: { Located RdrName }
1821 : varid_no_unsafe { $1 }
1822 | 'unsafe' { L1 $! mkUnqual varName (fsLit "unsafe") }
1823 | 'safe' { L1 $! mkUnqual varName (fsLit "safe") }
1824 | 'threadsafe' { L1 $! mkUnqual varName (fsLit "threadsafe") }
1826 varid_no_unsafe :: { Located RdrName }
1827 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1828 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1829 | 'forall' { L1 $! mkUnqual varName (fsLit "forall") }
1830 | 'family' { L1 $! mkUnqual varName (fsLit "family") }
1832 qvarsym :: { Located RdrName }
1836 qvarsym_no_minus :: { Located RdrName }
1837 : varsym_no_minus { $1 }
1840 qvarsym1 :: { Located RdrName }
1841 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1843 varsym :: { Located RdrName }
1844 : varsym_no_minus { $1 }
1845 | '-' { L1 $ mkUnqual varName (fsLit "-") }
1847 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1848 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1849 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1852 -- These special_ids are treated as keywords in various places,
1853 -- but as ordinary ids elsewhere. 'special_id' collects all these
1854 -- except 'unsafe', 'forall', and 'family' whose treatment differs
1855 -- depending on context
1856 special_id :: { Located FastString }
1858 : 'as' { L1 (fsLit "as") }
1859 | 'qualified' { L1 (fsLit "qualified") }
1860 | 'hiding' { L1 (fsLit "hiding") }
1861 | 'export' { L1 (fsLit "export") }
1862 | 'label' { L1 (fsLit "label") }
1863 | 'dynamic' { L1 (fsLit "dynamic") }
1864 | 'stdcall' { L1 (fsLit "stdcall") }
1865 | 'ccall' { L1 (fsLit "ccall") }
1867 special_sym :: { Located FastString }
1868 special_sym : '!' { L1 (fsLit "!") }
1869 | '.' { L1 (fsLit ".") }
1870 | '*' { L1 (fsLit "*") }
1872 -----------------------------------------------------------------------------
1873 -- Data constructors
1875 qconid :: { Located RdrName } -- Qualified or unqualified
1877 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1879 conid :: { Located RdrName }
1880 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1882 qconsym :: { Located RdrName } -- Qualified or unqualified
1884 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1886 consym :: { Located RdrName }
1887 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1889 -- ':' means only list cons
1890 | ':' { L1 $ consDataCon_RDR }
1893 -----------------------------------------------------------------------------
1896 literal :: { Located HsLit }
1897 : CHAR { L1 $ HsChar $ getCHAR $1 }
1898 | STRING { L1 $ HsString $ getSTRING $1 }
1899 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1900 | PRIMWORD { L1 $ HsWordPrim $ getPRIMWORD $1 }
1901 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1902 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1903 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1904 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1906 -----------------------------------------------------------------------------
1910 : vccurly { () } -- context popped in lexer.
1911 | error {% popContext }
1913 -----------------------------------------------------------------------------
1914 -- Miscellaneous (mostly renamings)
1916 modid :: { Located ModuleName }
1917 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1918 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1921 (unpackFS mod ++ '.':unpackFS c))
1925 : commas ',' { $1 + 1 }
1928 -----------------------------------------------------------------------------
1929 -- Documentation comments
1931 docnext :: { LHsDoc RdrName }
1932 : DOCNEXT {% case parseHaddockParagraphs (tokenise (getDOCNEXT $1)) of {
1933 MyLeft err -> parseError (getLoc $1) err;
1934 MyRight doc -> return (L1 doc) } }
1936 docprev :: { LHsDoc RdrName }
1937 : DOCPREV {% case parseHaddockParagraphs (tokenise (getDOCPREV $1)) of {
1938 MyLeft err -> parseError (getLoc $1) err;
1939 MyRight doc -> return (L1 doc) } }
1941 docnamed :: { Located (String, (HsDoc RdrName)) }
1943 let string = getDOCNAMED $1
1944 (name, rest) = break isSpace string
1945 in case parseHaddockParagraphs (tokenise rest) of {
1946 MyLeft err -> parseError (getLoc $1) err;
1947 MyRight doc -> return (L1 (name, doc)) } }
1949 docsection :: { Located (Int, HsDoc RdrName) }
1950 : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1951 case parseHaddockString (tokenise doc) of {
1952 MyLeft err -> parseError (getLoc $1) err;
1953 MyRight doc -> return (L1 (n, doc)) } }
1955 moduleheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
1956 : DOCNEXT {% let string = getDOCNEXT $1 in
1957 case parseModuleHeader string of {
1958 Right (str, info) ->
1959 case parseHaddockParagraphs (tokenise str) of {
1960 MyLeft err -> parseError (getLoc $1) err;
1961 MyRight doc -> return (info, Just doc);
1963 Left err -> parseError (getLoc $1) err
1966 maybe_docprev :: { Maybe (LHsDoc RdrName) }
1967 : docprev { Just $1 }
1968 | {- empty -} { Nothing }
1970 maybe_docnext :: { Maybe (LHsDoc RdrName) }
1971 : docnext { Just $1 }
1972 | {- empty -} { Nothing }
1976 happyError = srcParseFail
1978 getVARID (L _ (ITvarid x)) = x
1979 getCONID (L _ (ITconid x)) = x
1980 getVARSYM (L _ (ITvarsym x)) = x
1981 getCONSYM (L _ (ITconsym x)) = x
1982 getQVARID (L _ (ITqvarid x)) = x
1983 getQCONID (L _ (ITqconid x)) = x
1984 getQVARSYM (L _ (ITqvarsym x)) = x
1985 getQCONSYM (L _ (ITqconsym x)) = x
1986 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1987 getCHAR (L _ (ITchar x)) = x
1988 getSTRING (L _ (ITstring x)) = x
1989 getINTEGER (L _ (ITinteger x)) = x
1990 getRATIONAL (L _ (ITrational x)) = x
1991 getPRIMCHAR (L _ (ITprimchar x)) = x
1992 getPRIMSTRING (L _ (ITprimstring x)) = x
1993 getPRIMINTEGER (L _ (ITprimint x)) = x
1994 getPRIMWORD (L _ (ITprimword x)) = x
1995 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1996 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1997 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1998 getINLINE (L _ (ITinline_prag b)) = b
1999 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
2001 getDOCNEXT (L _ (ITdocCommentNext x)) = x
2002 getDOCPREV (L _ (ITdocCommentPrev x)) = x
2003 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
2004 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
2006 getSCC :: Located Token -> P FastString
2007 getSCC lt = do let s = getSTRING lt
2008 err = "Spaces are not allowed in SCCs"
2009 -- We probably actually want to be more restrictive than this
2010 if ' ' `elem` unpackFS s
2011 then failSpanMsgP (getLoc lt) (text err)
2014 -- Utilities for combining source spans
2015 comb2 :: Located a -> Located b -> SrcSpan
2016 comb2 a b = a `seq` b `seq` combineLocs a b
2018 comb3 :: Located a -> Located b -> Located c -> SrcSpan
2019 comb3 a b c = a `seq` b `seq` c `seq`
2020 combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
2022 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
2023 comb4 a b c d = a `seq` b `seq` c `seq` d `seq`
2024 (combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
2025 combineSrcSpans (getLoc c) (getLoc d))
2027 -- strict constructor version:
2029 sL :: SrcSpan -> a -> Located a
2030 sL span a = span `seq` a `seq` L span a
2032 -- Make a source location for the file. We're a bit lazy here and just
2033 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
2034 -- try to find the span of the whole file (ToDo).
2035 fileSrcSpan :: P SrcSpan
2038 let loc = mkSrcLoc (srcLocFile l) 1 0;
2039 return (mkSrcSpan loc loc)