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 maybe_pkg modid maybeas maybeimpspec
499 { L (comb4 $1 $5 $6 $7) (ImportDecl $5 $4 $2 $3 (unLoc $6) (unLoc $7)) }
501 maybe_src :: { IsBootInterface }
502 : '{-# SOURCE' '#-}' { True }
503 | {- empty -} { False }
505 maybe_pkg :: { Maybe FastString }
506 : STRING { Just (getSTRING $1) }
507 | {- empty -} { Nothing }
509 optqualified :: { Bool }
510 : 'qualified' { True }
511 | {- empty -} { False }
513 maybeas :: { Located (Maybe ModuleName) }
514 : 'as' modid { LL (Just (unLoc $2)) }
515 | {- empty -} { noLoc Nothing }
517 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
518 : impspec { L1 (Just (unLoc $1)) }
519 | {- empty -} { noLoc Nothing }
521 impspec :: { Located (Bool, [LIE RdrName]) }
522 : '(' exportlist ')' { LL (False, $2) }
523 | 'hiding' '(' exportlist ')' { LL (True, $3) }
525 -----------------------------------------------------------------------------
526 -- Fixity Declarations
530 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
532 infix :: { Located FixityDirection }
533 : 'infix' { L1 InfixN }
534 | 'infixl' { L1 InfixL }
535 | 'infixr' { L1 InfixR }
537 ops :: { Located [Located RdrName] }
538 : ops ',' op { LL ($3 : unLoc $1) }
541 -----------------------------------------------------------------------------
542 -- Top-Level Declarations
544 topdecls :: { OrdList (LHsDecl RdrName) }
545 : topdecls ';' topdecl { $1 `appOL` $3 }
546 | topdecls ';' { $1 }
549 topdecl :: { OrdList (LHsDecl RdrName) }
550 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
551 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
552 | 'instance' inst_type where_inst
553 { let (binds, sigs, ats, _) = cvBindsAndSigs (unLoc $3)
555 unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs ats)))}
556 | stand_alone_deriving { unitOL (LL (DerivD (unLoc $1))) }
557 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
558 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
559 | '{-# DEPRECATED' deprecations '#-}' { $2 }
560 | '{-# WARNING' warnings '#-}' { $2 }
561 | '{-# RULES' rules '#-}' { $2 }
564 -- Template Haskell Extension
565 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
566 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
567 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
572 cl_decl :: { LTyClDecl RdrName }
573 : 'class' tycl_hdr fds where_cls
574 {% do { let { (binds, sigs, ats, docs) =
575 cvBindsAndSigs (unLoc $4)
576 ; (ctxt, tc, tvs, tparms) = unLoc $2}
577 ; checkTyVars tparms -- only type vars allowed
579 ; return $ L (comb4 $1 $2 $3 $4)
580 (mkClassDecl (ctxt, tc, tvs)
581 (unLoc $3) sigs binds ats docs) } }
583 -- Type declarations (toplevel)
585 ty_decl :: { LTyClDecl RdrName }
586 -- ordinary type synonyms
587 : 'type' type '=' ctype
588 -- Note ctype, not sigtype, on the right of '='
589 -- We allow an explicit for-all but we don't insert one
590 -- in type Foo a = (b,b)
591 -- Instead we just say b is out of scope
593 -- Note the use of type for the head; this allows
594 -- infix type constructors to be declared
595 {% do { (tc, tvs, _) <- checkSynHdr $2 False
596 ; return (L (comb2 $1 $4)
597 (TySynonym tc tvs Nothing $4))
600 -- type family declarations
601 | 'type' 'family' type opt_kind_sig
602 -- Note the use of type for the head; this allows
603 -- infix type constructors to be declared
605 {% do { (tc, tvs, _) <- checkSynHdr $3 False
606 ; return (L (comb3 $1 $3 $4)
607 (TyFamily TypeFamily tc tvs (unLoc $4)))
610 -- type instance declarations
611 | 'type' 'instance' type '=' ctype
612 -- Note the use of type for the head; this allows
613 -- infix type constructors and type patterns
615 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
616 ; return (L (comb2 $1 $5)
617 (TySynonym tc tvs (Just typats) $5))
620 -- ordinary data type or newtype declaration
621 | data_or_newtype tycl_hdr constrs deriving
622 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
623 ; checkTyVars tparms -- no type pattern
625 sL (comb4 $1 $2 $3 $4)
626 -- We need the location on tycl_hdr in case
627 -- constrs and deriving are both empty
628 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
629 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
631 -- ordinary GADT declaration
632 | data_or_newtype tycl_hdr opt_kind_sig
633 'where' gadt_constrlist
635 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
636 ; checkTyVars tparms -- can have type pats
638 sL (comb4 $1 $2 $4 $5)
639 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
640 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
642 -- data/newtype family
643 | 'data' 'family' tycl_hdr opt_kind_sig
644 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
645 ; checkTyVars tparms -- no type pattern
646 ; unless (null (unLoc ctxt)) $ -- and no context
647 parseError (getLoc ctxt)
648 "A family declaration cannot have a context"
651 (TyFamily DataFamily tc tvs (unLoc $4)) } }
653 -- data/newtype instance declaration
654 | data_or_newtype 'instance' tycl_hdr constrs deriving
655 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
656 -- can have type pats
658 L (comb4 $1 $3 $4 $5)
659 -- We need the location on tycl_hdr in case
660 -- constrs and deriving are both empty
661 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
662 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
664 -- GADT instance declaration
665 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
666 'where' gadt_constrlist
668 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
669 -- can have type pats
671 L (comb4 $1 $3 $6 $7)
672 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
673 (unLoc $4) (reverse (unLoc $6)) (unLoc $7)) } }
675 -- Associate type family declarations
677 -- * They have a different syntax than on the toplevel (no family special
680 -- * They also need to be separate from instances; otherwise, data family
681 -- declarations without a kind signature cause parsing conflicts with empty
682 -- data declarations.
684 at_decl_cls :: { LTyClDecl RdrName }
685 -- type family declarations
686 : 'type' type opt_kind_sig
687 -- Note the use of type for the head; this allows
688 -- infix type constructors to be declared
690 {% do { (tc, tvs, _) <- checkSynHdr $2 False
691 ; return (L (comb3 $1 $2 $3)
692 (TyFamily TypeFamily tc tvs (unLoc $3)))
695 -- default type instance
696 | 'type' type '=' ctype
697 -- Note the use of type for the head; this allows
698 -- infix type constructors and type patterns
700 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
701 ; return (L (comb2 $1 $4)
702 (TySynonym tc tvs (Just typats) $4))
705 -- data/newtype family declaration
706 | 'data' tycl_hdr opt_kind_sig
707 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
708 ; checkTyVars tparms -- no type pattern
709 ; unless (null (unLoc ctxt)) $ -- and no context
710 parseError (getLoc ctxt)
711 "A family declaration cannot have a context"
714 (TyFamily DataFamily tc tvs (unLoc $3))
717 -- Associate type instances
719 at_decl_inst :: { LTyClDecl RdrName }
720 -- type instance declarations
721 : 'type' type '=' ctype
722 -- Note the use of type for the head; this allows
723 -- infix type constructors and type patterns
725 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
726 ; return (L (comb2 $1 $4)
727 (TySynonym tc tvs (Just typats) $4))
730 -- data/newtype instance declaration
731 | data_or_newtype tycl_hdr constrs deriving
732 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
733 -- can have type pats
735 L (comb4 $1 $2 $3 $4)
736 -- We need the location on tycl_hdr in case
737 -- constrs and deriving are both empty
738 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
739 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
741 -- GADT instance declaration
742 | data_or_newtype tycl_hdr opt_kind_sig
743 'where' gadt_constrlist
745 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
746 -- can have type pats
748 L (comb4 $1 $2 $5 $6)
749 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
750 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
752 data_or_newtype :: { Located NewOrData }
753 : 'data' { L1 DataType }
754 | 'newtype' { L1 NewType }
756 opt_kind_sig :: { Located (Maybe Kind) }
758 | '::' kind { LL (Just (unLoc $2)) }
760 -- tycl_hdr parses the header of a class or data type decl,
761 -- which takes the form
764 -- (Eq a, Ord b) => T a b
765 -- T Int [a] -- for associated types
766 -- Rather a lot of inlining here, else we get reduce/reduce errors
767 tycl_hdr :: { Located (LHsContext RdrName,
769 [LHsTyVarBndr RdrName],
771 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
772 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
774 -----------------------------------------------------------------------------
775 -- Stand-alone deriving
777 -- Glasgow extension: stand-alone deriving declarations
778 stand_alone_deriving :: { LDerivDecl RdrName }
779 : 'deriving' 'instance' inst_type {% checkDerivDecl (LL (DerivDecl $3)) }
781 -----------------------------------------------------------------------------
782 -- Nested declarations
784 -- Declaration in class bodies
786 decl_cls :: { Located (OrdList (LHsDecl RdrName)) }
787 decl_cls : at_decl_cls { LL (unitOL (L1 (TyClD (unLoc $1)))) }
790 decls_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
791 : decls_cls ';' decl_cls { LL (unLoc $1 `appOL` unLoc $3) }
792 | decls_cls ';' { LL (unLoc $1) }
794 | {- empty -} { noLoc nilOL }
798 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
799 : '{' decls_cls '}' { LL (unLoc $2) }
800 | vocurly decls_cls close { $2 }
804 where_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
805 -- No implicit parameters
806 -- May have type declarations
807 : 'where' decllist_cls { LL (unLoc $2) }
808 | {- empty -} { noLoc nilOL }
810 -- Declarations in instance bodies
812 decl_inst :: { Located (OrdList (LHsDecl RdrName)) }
813 decl_inst : at_decl_inst { LL (unitOL (L1 (TyClD (unLoc $1)))) }
816 decls_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
817 : decls_inst ';' decl_inst { LL (unLoc $1 `appOL` unLoc $3) }
818 | decls_inst ';' { LL (unLoc $1) }
820 | {- empty -} { noLoc nilOL }
823 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
824 : '{' decls_inst '}' { LL (unLoc $2) }
825 | vocurly decls_inst close { $2 }
829 where_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
830 -- No implicit parameters
831 -- May have type declarations
832 : 'where' decllist_inst { LL (unLoc $2) }
833 | {- empty -} { noLoc nilOL }
835 -- Declarations in binding groups other than classes and instances
837 decls :: { Located (OrdList (LHsDecl RdrName)) }
838 : decls ';' decl { let { this = unLoc $3;
840 these = rest `appOL` this }
841 in rest `seq` this `seq` these `seq`
843 | decls ';' { LL (unLoc $1) }
845 | {- empty -} { noLoc nilOL }
847 decllist :: { Located (OrdList (LHsDecl RdrName)) }
848 : '{' decls '}' { LL (unLoc $2) }
849 | vocurly decls close { $2 }
851 -- Binding groups other than those of class and instance declarations
853 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
854 -- No type declarations
855 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
856 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
857 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
859 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
860 -- No type declarations
861 : 'where' binds { LL (unLoc $2) }
862 | {- empty -} { noLoc emptyLocalBinds }
865 -----------------------------------------------------------------------------
866 -- Transformation Rules
868 rules :: { OrdList (LHsDecl RdrName) }
869 : rules ';' rule { $1 `snocOL` $3 }
872 | {- empty -} { nilOL }
874 rule :: { LHsDecl RdrName }
875 : STRING activation rule_forall infixexp '=' exp
876 { LL $ RuleD (HsRule (getSTRING $1)
877 ($2 `orElse` AlwaysActive)
878 $3 $4 placeHolderNames $6 placeHolderNames) }
880 activation :: { Maybe Activation }
881 : {- empty -} { Nothing }
882 | explicit_activation { Just $1 }
884 explicit_activation :: { Activation } -- In brackets
885 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
886 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
888 rule_forall :: { [RuleBndr RdrName] }
889 : 'forall' rule_var_list '.' { $2 }
892 rule_var_list :: { [RuleBndr RdrName] }
894 | rule_var rule_var_list { $1 : $2 }
896 rule_var :: { RuleBndr RdrName }
897 : varid { RuleBndr $1 }
898 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
900 -----------------------------------------------------------------------------
901 -- Warnings and deprecations (c.f. rules)
903 warnings :: { OrdList (LHsDecl RdrName) }
904 : warnings ';' warning { $1 `appOL` $3 }
905 | warnings ';' { $1 }
907 | {- empty -} { nilOL }
909 -- SUP: TEMPORARY HACK, not checking for `module Foo'
910 warning :: { OrdList (LHsDecl RdrName) }
912 { toOL [ LL $ WarningD (Warning n (WarningTxt (getSTRING $2)))
915 deprecations :: { OrdList (LHsDecl RdrName) }
916 : deprecations ';' deprecation { $1 `appOL` $3 }
917 | deprecations ';' { $1 }
919 | {- empty -} { nilOL }
921 -- SUP: TEMPORARY HACK, not checking for `module Foo'
922 deprecation :: { OrdList (LHsDecl RdrName) }
924 { toOL [ LL $ WarningD (Warning n (DeprecatedTxt (getSTRING $2)))
928 -----------------------------------------------------------------------------
929 -- Foreign import and export declarations
931 fdecl :: { LHsDecl RdrName }
932 fdecl : 'import' callconv safety fspec
933 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
934 | 'import' callconv fspec
935 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
937 | 'export' callconv fspec
938 {% mkExport $2 (unLoc $3) >>= return.LL }
940 callconv :: { CallConv }
941 : 'stdcall' { CCall StdCallConv }
942 | 'ccall' { CCall CCallConv }
943 | 'dotnet' { DNCall }
946 : 'unsafe' { PlayRisky }
947 | 'safe' { PlaySafe False }
948 | 'threadsafe' { PlaySafe True }
950 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
951 : STRING var '::' sigtypedoc { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
952 | var '::' sigtypedoc { LL (noLoc nilFS, $1, $3) }
953 -- if the entity string is missing, it defaults to the empty string;
954 -- the meaning of an empty entity string depends on the calling
957 -----------------------------------------------------------------------------
960 opt_sig :: { Maybe (LHsType RdrName) }
961 : {- empty -} { Nothing }
962 | '::' sigtype { Just $2 }
964 opt_asig :: { Maybe (LHsType RdrName) }
965 : {- empty -} { Nothing }
966 | '::' atype { Just $2 }
968 sigtypes1 :: { [LHsType RdrName] }
970 | sigtype ',' sigtypes1 { $1 : $3 }
972 sigtype :: { LHsType RdrName }
973 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
974 -- Wrap an Implicit forall if there isn't one there already
976 sigtypedoc :: { LHsType RdrName }
977 : ctypedoc { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
978 -- Wrap an Implicit forall if there isn't one there already
980 sig_vars :: { Located [Located RdrName] }
981 : sig_vars ',' var { LL ($3 : unLoc $1) }
984 -----------------------------------------------------------------------------
987 infixtype :: { LHsType RdrName }
988 : btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
989 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
991 infixtypedoc :: { LHsType RdrName }
993 | infixtype docprev { LL $ HsDocTy $1 $2 }
995 gentypedoc :: { LHsType RdrName }
998 | infixtypedoc { $1 }
999 | btype '->' ctypedoc { LL $ HsFunTy $1 $3 }
1000 | btypedoc '->' ctypedoc { LL $ HsFunTy $1 $3 }
1002 ctypedoc :: { LHsType RdrName }
1003 : 'forall' tv_bndrs '.' ctypedoc { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
1004 | context '=>' ctypedoc { LL $ mkImplicitHsForAllTy $1 $3 }
1005 -- A type of form (context => type) is an *implicit* HsForAllTy
1008 strict_mark :: { Located HsBang }
1009 : '!' { L1 HsStrict }
1010 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
1012 -- A ctype is a for-all type
1013 ctype :: { LHsType RdrName }
1014 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
1015 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
1016 -- A type of form (context => type) is an *implicit* HsForAllTy
1019 -- We parse a context as a btype so that we don't get reduce/reduce
1020 -- errors in ctype. The basic problem is that
1022 -- looks so much like a tuple type. We can't tell until we find the =>
1024 -- We have the t1 ~ t2 form here and in gentype, to permit an individual
1025 -- equational constraint without parenthesis.
1026 context :: { LHsContext RdrName }
1027 : btype '~' btype {% checkContext
1028 (LL $ HsPredTy (HsEqualP $1 $3)) }
1029 | btype {% checkContext $1 }
1031 type :: { LHsType RdrName }
1032 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
1035 gentype :: { LHsType RdrName }
1037 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
1038 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
1039 | btype '->' ctype { LL $ HsFunTy $1 $3 }
1040 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1042 btype :: { LHsType RdrName }
1043 : btype atype { LL $ HsAppTy $1 $2 }
1046 btypedoc :: { LHsType RdrName }
1047 : btype atype docprev { LL $ HsDocTy (L (comb2 $1 $2) (HsAppTy $1 $2)) $3 }
1048 | atype docprev { LL $ HsDocTy $1 $2 }
1050 atype :: { LHsType RdrName }
1051 : gtycon { L1 (HsTyVar (unLoc $1)) }
1052 | tyvar { L1 (HsTyVar (unLoc $1)) }
1053 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
1054 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
1055 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
1056 | '[' ctype ']' { LL $ HsListTy $2 }
1057 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
1058 | '(' ctype ')' { LL $ HsParTy $2 }
1059 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
1061 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
1063 -- An inst_type is what occurs in the head of an instance decl
1064 -- e.g. (Foo a, Gaz b) => Wibble a b
1065 -- It's kept as a single type, with a MonoDictTy at the right
1066 -- hand corner, for convenience.
1067 inst_type :: { LHsType RdrName }
1068 : sigtype {% checkInstType $1 }
1070 inst_types1 :: { [LHsType RdrName] }
1071 : inst_type { [$1] }
1072 | inst_type ',' inst_types1 { $1 : $3 }
1074 comma_types0 :: { [LHsType RdrName] }
1075 : comma_types1 { $1 }
1076 | {- empty -} { [] }
1078 comma_types1 :: { [LHsType RdrName] }
1080 | ctype ',' comma_types1 { $1 : $3 }
1082 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1083 : tv_bndr tv_bndrs { $1 : $2 }
1084 | {- empty -} { [] }
1086 tv_bndr :: { LHsTyVarBndr RdrName }
1087 : tyvar { L1 (UserTyVar (unLoc $1)) }
1088 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
1091 fds :: { Located [Located ([RdrName], [RdrName])] }
1092 : {- empty -} { noLoc [] }
1093 | '|' fds1 { LL (reverse (unLoc $2)) }
1095 fds1 :: { Located [Located ([RdrName], [RdrName])] }
1096 : fds1 ',' fd { LL ($3 : unLoc $1) }
1099 fd :: { Located ([RdrName], [RdrName]) }
1100 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
1101 (reverse (unLoc $1), reverse (unLoc $3)) }
1103 varids0 :: { Located [RdrName] }
1104 : {- empty -} { noLoc [] }
1105 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
1107 -----------------------------------------------------------------------------
1110 kind :: { Located Kind }
1112 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1114 akind :: { Located Kind }
1115 : '*' { L1 liftedTypeKind }
1116 | '!' { L1 unliftedTypeKind }
1117 | '(' kind ')' { LL (unLoc $2) }
1120 -----------------------------------------------------------------------------
1121 -- Datatype declarations
1123 gadt_constrlist :: { Located [LConDecl RdrName] }
1124 : '{' gadt_constrs '}' { LL (unLoc $2) }
1125 | vocurly gadt_constrs close { $2 }
1127 gadt_constrs :: { Located [LConDecl RdrName] }
1128 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
1129 | gadt_constrs ';' { $1 }
1130 | gadt_constr { L1 [$1] }
1132 -- We allow the following forms:
1133 -- C :: Eq a => a -> T a
1134 -- C :: forall a. Eq a => !a -> T a
1135 -- D { x,y :: a } :: T a
1136 -- forall a. Eq a => D { x,y :: a } :: T a
1138 gadt_constr :: { LConDecl RdrName }
1140 { LL (mkGadtDecl $1 $3) }
1141 -- Syntax: Maybe merge the record stuff with the single-case above?
1142 -- (to kill the mostly harmless reduce/reduce error)
1143 -- XXX revisit audreyt
1144 | constr_stuff_record '::' sigtype
1145 { let (con,details) = unLoc $1 in
1146 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3) Nothing) }
1148 | forall context '=>' constr_stuff_record '::' sigtype
1149 { let (con,details) = unLoc $4 in
1150 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6) Nothing ) }
1151 | forall constr_stuff_record '::' sigtype
1152 { let (con,details) = unLoc $2 in
1153 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4) Nothing) }
1157 constrs :: { Located [LConDecl RdrName] }
1158 : {- empty; a GHC extension -} { noLoc [] }
1159 | maybe_docnext '=' constrs1 { L (comb2 $2 $3) (addConDocs (unLoc $3) $1) }
1161 constrs1 :: { Located [LConDecl RdrName] }
1162 : constrs1 maybe_docnext '|' maybe_docprev constr { LL (addConDoc $5 $2 : addConDocFirst (unLoc $1) $4) }
1163 | constr { L1 [$1] }
1165 constr :: { LConDecl RdrName }
1166 : maybe_docnext forall context '=>' constr_stuff maybe_docprev
1167 { let (con,details) = unLoc $5 in
1168 L (comb4 $2 $3 $4 $5) (ConDecl con Explicit (unLoc $2) $3 details ResTyH98 ($1 `mplus` $6)) }
1169 | maybe_docnext forall constr_stuff maybe_docprev
1170 { let (con,details) = unLoc $3 in
1171 L (comb2 $2 $3) (ConDecl con Explicit (unLoc $2) (noLoc []) details ResTyH98 ($1 `mplus` $4)) }
1173 forall :: { Located [LHsTyVarBndr RdrName] }
1174 : 'forall' tv_bndrs '.' { LL $2 }
1175 | {- empty -} { noLoc [] }
1177 constr_stuff :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1178 -- We parse the constructor declaration
1180 -- as a btype (treating C as a type constructor) and then convert C to be
1181 -- a data constructor. Reason: it might continue like this:
1183 -- in which case C really would be a type constructor. We can't resolve this
1184 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1185 : btype {% mkPrefixCon $1 [] >>= return.LL }
1186 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
1187 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
1188 | btype conop btype { LL ($2, InfixCon $1 $3) }
1190 constr_stuff_record :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1191 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
1192 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
1194 fielddecls :: { [([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName))] }
1195 : fielddecl maybe_docnext ',' maybe_docprev fielddecls { addFieldDoc (unLoc $1) $4 : addFieldDocs $5 $2 }
1196 | fielddecl { [unLoc $1] }
1198 fielddecl :: { Located ([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName)) }
1199 : maybe_docnext sig_vars '::' ctype maybe_docprev { L (comb3 $2 $3 $4) (reverse (unLoc $2), $4, $1 `mplus` $5) }
1201 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1202 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1203 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1204 -- We don't allow a context, but that's sorted out by the type checker.
1205 deriving :: { Located (Maybe [LHsType RdrName]) }
1206 : {- empty -} { noLoc Nothing }
1207 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1208 ; p <- checkInstType (L loc (HsTyVar tv))
1209 ; return (LL (Just [p])) } }
1210 | 'deriving' '(' ')' { LL (Just []) }
1211 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1212 -- Glasgow extension: allow partial
1213 -- applications in derivings
1215 -----------------------------------------------------------------------------
1216 -- Value definitions
1218 {- There's an awkward overlap with a type signature. Consider
1219 f :: Int -> Int = ...rhs...
1220 Then we can't tell whether it's a type signature or a value
1221 definition with a result signature until we see the '='.
1222 So we have to inline enough to postpone reductions until we know.
1226 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1227 instead of qvar, we get another shift/reduce-conflict. Consider the
1230 { (^^) :: Int->Int ; } Type signature; only var allowed
1232 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1233 qvar allowed (because of instance decls)
1235 We can't tell whether to reduce var to qvar until after we've read the signatures.
1238 docdecl :: { LHsDecl RdrName }
1239 : docdecld { L1 (DocD (unLoc $1)) }
1241 docdecld :: { LDocDecl RdrName }
1242 : docnext { L1 (DocCommentNext (unLoc $1)) }
1243 | docprev { L1 (DocCommentPrev (unLoc $1)) }
1244 | docnamed { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1245 | docsection { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1247 decl :: { Located (OrdList (LHsDecl RdrName)) }
1249 | '!' aexp rhs {% do { pat <- checkPattern $2;
1250 return (LL $ unitOL $ LL $ ValD (
1251 PatBind (LL $ BangPat pat) (unLoc $3)
1252 placeHolderType placeHolderNames)) } }
1253 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1254 let { l = comb2 $1 $> };
1255 return $! (sL l (unitOL $! (sL l $ ValD r))) } }
1256 | docdecl { LL $ unitOL $1 }
1258 rhs :: { Located (GRHSs RdrName) }
1259 : '=' exp wherebinds { sL (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1260 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1262 gdrhs :: { Located [LGRHS RdrName] }
1263 : gdrhs gdrh { LL ($2 : unLoc $1) }
1266 gdrh :: { LGRHS RdrName }
1267 : '|' guardquals '=' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1269 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1270 : infixexp '::' sigtypedoc
1271 {% do s <- checkValSig $1 $3;
1272 return (LL $ unitOL (LL $ SigD s)) }
1273 -- See the above notes for why we need infixexp here
1274 | var ',' sig_vars '::' sigtypedoc
1275 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1276 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1278 | '{-# INLINE' activation qvar '#-}'
1279 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1280 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1281 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1283 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1284 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1286 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1287 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1289 -----------------------------------------------------------------------------
1292 exp :: { LHsExpr RdrName }
1293 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1294 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1295 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1296 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1297 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1300 infixexp :: { LHsExpr RdrName }
1302 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1304 exp10 :: { LHsExpr RdrName }
1305 : '\\' apat apats opt_asig '->' exp
1306 { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1309 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1310 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1311 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1312 | '-' fexp { LL $ NegApp $2 noSyntaxExpr }
1314 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1315 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1316 return (L loc (mkHsDo DoExpr stmts body)) }
1317 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1318 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1319 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1320 | scc_annot exp { LL $ if opt_SccProfilingOn
1321 then HsSCC (unLoc $1) $2
1323 | hpc_annot exp { LL $ if opt_Hpc
1324 then HsTickPragma (unLoc $1) $2
1327 | 'proc' aexp '->' exp
1328 {% checkPattern $2 >>= \ p ->
1329 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1330 placeHolderType undefined)) }
1331 -- TODO: is LL right here?
1333 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1334 -- hdaume: core annotation
1337 scc_annot :: { Located FastString }
1338 : '_scc_' STRING {% (addWarning Opt_WarnWarningsDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1339 ( do scc <- getSCC $2; return $ LL scc ) }
1340 | '{-# SCC' STRING '#-}' {% do scc <- getSCC $2; return $ LL scc }
1342 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1343 : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1344 { LL $ (getSTRING $2
1345 ,( fromInteger $ getINTEGER $3
1346 , fromInteger $ getINTEGER $5
1348 ,( fromInteger $ getINTEGER $7
1349 , fromInteger $ getINTEGER $9
1354 fexp :: { LHsExpr RdrName }
1355 : fexp aexp { LL $ HsApp $1 $2 }
1358 aexp :: { LHsExpr RdrName }
1359 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1360 | '~' aexp { LL $ ELazyPat $2 }
1363 aexp1 :: { LHsExpr RdrName }
1364 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1368 -- Here was the syntax for type applications that I was planning
1369 -- but there are difficulties (e.g. what order for type args)
1370 -- so it's not enabled yet.
1371 -- But this case *is* used for the left hand side of a generic definition,
1372 -- which is parsed as an expression before being munged into a pattern
1373 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1374 (sL (getLoc $3) (HsType $3)) }
1376 aexp2 :: { LHsExpr RdrName }
1377 : ipvar { L1 (HsIPVar $! unLoc $1) }
1378 | qcname { L1 (HsVar $! unLoc $1) }
1379 | literal { L1 (HsLit $! unLoc $1) }
1380 -- This will enable overloaded strings permanently. Normally the renamer turns HsString
1381 -- into HsOverLit when -foverloaded-strings is on.
1382 -- | STRING { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1383 | INTEGER { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1384 | RATIONAL { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1385 -- N.B.: sections get parsed by these next two productions.
1386 -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't correct Haskell98
1387 -- (you'd have to write '((+ 3), (4 -))')
1388 -- but the less cluttered version fell out of having texps.
1389 | '(' texp ')' { LL (HsPar $2) }
1390 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1391 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1392 | '[' list ']' { LL (unLoc $2) }
1393 | '[:' parr ':]' { LL (unLoc $2) }
1394 | '_' { L1 EWildPat }
1396 -- Template Haskell Extension
1397 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1398 (L1 $ HsVar (mkUnqual varName
1399 (getTH_ID_SPLICE $1)))) } -- $x
1400 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1402 | TH_QUASIQUOTE { let { loc = getLoc $1
1403 ; ITquasiQuote (quoter, quote, quoteSpan) = unLoc $1
1404 ; quoterId = mkUnqual varName quoter
1406 in sL loc $ HsQuasiQuoteE (mkHsQuasiQuote quoterId quoteSpan quote) }
1407 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1408 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1409 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1410 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1411 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1412 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1413 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1414 return (LL $ HsBracket (PatBr p)) }
1415 | '[d|' cvtopbody '|]' {% checkDecBrGroup $2 >>= \g ->
1416 return (LL $ HsBracket (DecBr g)) }
1418 -- arrow notation extension
1419 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1421 cmdargs :: { [LHsCmdTop RdrName] }
1422 : cmdargs acmd { $2 : $1 }
1423 | {- empty -} { [] }
1425 acmd :: { LHsCmdTop RdrName }
1426 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1428 cvtopbody :: { [LHsDecl RdrName] }
1429 : '{' cvtopdecls0 '}' { $2 }
1430 | vocurly cvtopdecls0 close { $2 }
1432 cvtopdecls0 :: { [LHsDecl RdrName] }
1433 : {- empty -} { [] }
1436 -- "texp" is short for tuple expressions:
1437 -- things that can appear unparenthesized as long as they're
1438 -- inside parens or delimitted by commas
1439 texp :: { LHsExpr RdrName }
1442 -- Note [Parsing sections]
1443 -- ~~~~~~~~~~~~~~~~~~~~~~~
1444 -- We include left and right sections here, which isn't
1445 -- technically right according to Haskell 98. For example
1446 -- (3 +, True) isn't legal
1447 -- However, we want to parse bang patterns like
1449 -- and it's convenient to do so here as a section
1450 -- Then when converting expr to pattern we unravel it again
1451 -- Meanwhile, the renamer checks that real sections appear
1453 | infixexp qop { LL $ SectionL $1 $2 }
1454 | qopm infixexp { LL $ SectionR $1 $2 }
1456 -- View patterns get parenthesized above
1457 | exp '->' exp { LL $ EViewPat $1 $3 }
1459 texps :: { [LHsExpr RdrName] }
1460 : texps ',' texp { $3 : $1 }
1464 -----------------------------------------------------------------------------
1467 -- The rules below are little bit contorted to keep lexps left-recursive while
1468 -- avoiding another shift/reduce-conflict.
1470 list :: { LHsExpr RdrName }
1471 : texp { L1 $ ExplicitList placeHolderType [$1] }
1472 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1473 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1474 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1475 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1476 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1477 | texp '|' flattenedpquals { sL (comb2 $1 $>) $ mkHsDo ListComp (unLoc $3) $1 }
1479 lexps :: { Located [LHsExpr RdrName] }
1480 : lexps ',' texp { LL (((:) $! $3) $! unLoc $1) }
1481 | texp ',' texp { LL [$3,$1] }
1483 -----------------------------------------------------------------------------
1484 -- List Comprehensions
1486 flattenedpquals :: { Located [LStmt RdrName] }
1487 : pquals { case (unLoc $1) of
1488 ParStmt [(qs, _)] -> L1 qs
1489 -- We just had one thing in our "parallel" list so
1490 -- we simply return that thing directly
1493 -- We actually found some actual parallel lists so
1494 -- we leave them into as a ParStmt
1497 pquals :: { LStmt RdrName }
1498 : pquals1 { L1 (ParStmt [(qs, undefined) | qs <- (reverse (unLoc $1))]) }
1500 pquals1 :: { Located [[LStmt RdrName]] }
1501 : pquals1 '|' squals { LL (unLoc $3 : unLoc $1) }
1502 | squals { L (getLoc $1) [unLoc $1] }
1504 squals :: { Located [LStmt RdrName] }
1505 : squals1 { L (getLoc $1) (reverse (unLoc $1)) }
1507 squals1 :: { Located [LStmt RdrName] }
1508 : transformquals1 { LL (unLoc $1) }
1510 transformquals1 :: { Located [LStmt RdrName] }
1511 : transformquals1 ',' transformqual { LL $ [LL ((unLoc $3) (unLoc $1))] }
1512 | transformquals1 ',' qual { LL ($3 : unLoc $1) }
1513 -- | transformquals1 ',' '{|' pquals '|}' { LL ($4 : unLoc $1) }
1514 | transformqual { LL $ [LL ((unLoc $1) [])] }
1516 -- | '{|' pquals '|}' { L1 [$2] }
1519 -- It is possible to enable bracketing (associating) qualifier lists by uncommenting the lines with {| |}
1520 -- above. Due to a lack of consensus on the syntax, this feature is not being used until we get user
1521 -- demand. Note that the {| |} symbols are reused from -XGenerics and hence if you want to compile
1522 -- a program that makes use of this temporary syntax you must supply that flag to GHC
1524 transformqual :: { Located ([LStmt RdrName] -> Stmt RdrName) }
1525 : 'then' exp { LL $ \leftStmts -> (mkTransformStmt (reverse leftStmts) $2) }
1526 | 'then' exp 'by' exp { LL $ \leftStmts -> (mkTransformByStmt (reverse leftStmts) $2 $4) }
1527 | 'then' 'group' 'by' exp { LL $ \leftStmts -> (mkGroupByStmt (reverse leftStmts) $4) }
1528 | 'then' 'group' 'using' exp { LL $ \leftStmts -> (mkGroupUsingStmt (reverse leftStmts) $4) }
1529 | 'then' 'group' 'by' exp 'using' exp { LL $ \leftStmts -> (mkGroupByUsingStmt (reverse leftStmts) $4 $6) }
1531 -----------------------------------------------------------------------------
1532 -- Parallel array expressions
1534 -- The rules below are little bit contorted; see the list case for details.
1535 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1536 -- Moreover, we allow explicit arrays with no element (represented by the nil
1537 -- constructor in the list case).
1539 parr :: { LHsExpr RdrName }
1540 : { noLoc (ExplicitPArr placeHolderType []) }
1541 | texp { L1 $ ExplicitPArr placeHolderType [$1] }
1542 | lexps { L1 $ ExplicitPArr placeHolderType
1543 (reverse (unLoc $1)) }
1544 | texp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1545 | texp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1546 | texp '|' flattenedpquals { LL $ mkHsDo PArrComp (unLoc $3) $1 }
1548 -- We are reusing `lexps' and `flattenedpquals' from the list case.
1550 -----------------------------------------------------------------------------
1553 guardquals :: { Located [LStmt RdrName] }
1554 : guardquals1 { L (getLoc $1) (reverse (unLoc $1)) }
1556 guardquals1 :: { Located [LStmt RdrName] }
1557 : guardquals1 ',' qual { LL ($3 : unLoc $1) }
1560 -----------------------------------------------------------------------------
1561 -- Case alternatives
1563 altslist :: { Located [LMatch RdrName] }
1564 : '{' alts '}' { LL (reverse (unLoc $2)) }
1565 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1567 alts :: { Located [LMatch RdrName] }
1568 : alts1 { L1 (unLoc $1) }
1569 | ';' alts { LL (unLoc $2) }
1571 alts1 :: { Located [LMatch RdrName] }
1572 : alts1 ';' alt { LL ($3 : unLoc $1) }
1573 | alts1 ';' { LL (unLoc $1) }
1576 alt :: { LMatch RdrName }
1577 : pat opt_sig alt_rhs { LL (Match [$1] $2 (unLoc $3)) }
1579 alt_rhs :: { Located (GRHSs RdrName) }
1580 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1582 ralt :: { Located [LGRHS RdrName] }
1583 : '->' exp { LL (unguardedRHS $2) }
1584 | gdpats { L1 (reverse (unLoc $1)) }
1586 gdpats :: { Located [LGRHS RdrName] }
1587 : gdpats gdpat { LL ($2 : unLoc $1) }
1590 gdpat :: { LGRHS RdrName }
1591 : '|' guardquals '->' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1593 -- 'pat' recognises a pattern, including one with a bang at the top
1594 -- e.g. "!x" or "!(x,y)" or "C a b" etc
1595 -- Bangs inside are parsed as infix operator applications, so that
1596 -- we parse them right when bang-patterns are off
1597 pat :: { LPat RdrName }
1598 pat : exp {% checkPattern $1 }
1599 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1601 apat :: { LPat RdrName }
1602 apat : aexp {% checkPattern $1 }
1603 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1605 apats :: { [LPat RdrName] }
1606 : apat apats { $1 : $2 }
1607 | {- empty -} { [] }
1609 -----------------------------------------------------------------------------
1610 -- Statement sequences
1612 stmtlist :: { Located [LStmt RdrName] }
1613 : '{' stmts '}' { LL (unLoc $2) }
1614 | vocurly stmts close { $2 }
1616 -- do { ;; s ; s ; ; s ;; }
1617 -- The last Stmt should be an expression, but that's hard to enforce
1618 -- here, because we need too much lookahead if we see do { e ; }
1619 -- So we use ExprStmts throughout, and switch the last one over
1620 -- in ParseUtils.checkDo instead
1621 stmts :: { Located [LStmt RdrName] }
1622 : stmt stmts_help { LL ($1 : unLoc $2) }
1623 | ';' stmts { LL (unLoc $2) }
1624 | {- empty -} { noLoc [] }
1626 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1627 : ';' stmts { LL (unLoc $2) }
1628 | {- empty -} { noLoc [] }
1630 -- For typing stmts at the GHCi prompt, where
1631 -- the input may consist of just comments.
1632 maybe_stmt :: { Maybe (LStmt RdrName) }
1634 | {- nothing -} { Nothing }
1636 stmt :: { LStmt RdrName }
1638 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1640 qual :: { LStmt RdrName }
1641 : pat '<-' exp { LL $ mkBindStmt $1 $3 }
1642 | exp { L1 $ mkExprStmt $1 }
1643 | 'let' binds { LL $ LetStmt (unLoc $2) }
1645 -----------------------------------------------------------------------------
1646 -- Record Field Update/Construction
1648 fbinds :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1650 | {- empty -} { ([], False) }
1652 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1653 : fbind ',' fbinds1 { case $3 of (flds, dd) -> ($1 : flds, dd) }
1654 | fbind { ([$1], False) }
1655 | '..' { ([], True) }
1657 fbind :: { HsRecField RdrName (LHsExpr RdrName) }
1658 : qvar '=' exp { HsRecField $1 $3 False }
1659 | qvar { HsRecField $1 (L (getLoc $1) (HsVar (unLoc $1))) True }
1660 -- Here's where we say that plain 'x'
1661 -- means exactly 'x = x'. The pun-flag boolean is
1662 -- there so we can still print it right
1664 -----------------------------------------------------------------------------
1665 -- Implicit Parameter Bindings
1667 dbinds :: { Located [LIPBind RdrName] }
1668 : dbinds ';' dbind { let { this = $3; rest = unLoc $1 }
1669 in rest `seq` this `seq` LL (this : rest) }
1670 | dbinds ';' { LL (unLoc $1) }
1671 | dbind { let this = $1 in this `seq` L1 [this] }
1672 -- | {- empty -} { [] }
1674 dbind :: { LIPBind RdrName }
1675 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1677 ipvar :: { Located (IPName RdrName) }
1678 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1680 -----------------------------------------------------------------------------
1681 -- Warnings and deprecations
1683 namelist :: { Located [RdrName] }
1684 namelist : name_var { L1 [unLoc $1] }
1685 | name_var ',' namelist { LL (unLoc $1 : unLoc $3) }
1687 name_var :: { Located RdrName }
1688 name_var : var { $1 }
1691 -----------------------------------------
1692 -- Data constructors
1693 qcon :: { Located RdrName }
1695 | '(' qconsym ')' { LL (unLoc $2) }
1696 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1697 -- The case of '[:' ':]' is part of the production `parr'
1699 con :: { Located RdrName }
1701 | '(' consym ')' { LL (unLoc $2) }
1702 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1704 sysdcon :: { Located DataCon } -- Wired in data constructors
1705 : '(' ')' { LL unitDataCon }
1706 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1707 | '(#' '#)' { LL $ unboxedSingletonDataCon }
1708 | '(#' commas '#)' { LL $ tupleCon Unboxed $2 }
1709 | '[' ']' { LL nilDataCon }
1711 conop :: { Located RdrName }
1713 | '`' conid '`' { LL (unLoc $2) }
1715 qconop :: { Located RdrName }
1717 | '`' qconid '`' { LL (unLoc $2) }
1719 -----------------------------------------------------------------------------
1720 -- Type constructors
1722 gtycon :: { Located RdrName } -- A "general" qualified tycon
1724 | '(' ')' { LL $ getRdrName unitTyCon }
1725 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1726 | '(#' '#)' { LL $ getRdrName unboxedSingletonTyCon }
1727 | '(#' commas '#)' { LL $ getRdrName (tupleTyCon Unboxed $2) }
1728 | '(' '->' ')' { LL $ getRdrName funTyCon }
1729 | '[' ']' { LL $ listTyCon_RDR }
1730 | '[:' ':]' { LL $ parrTyCon_RDR }
1732 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1734 | '(' qtyconsym ')' { LL (unLoc $2) }
1736 qtyconop :: { Located RdrName } -- Qualified or unqualified
1738 | '`' qtycon '`' { LL (unLoc $2) }
1740 qtycon :: { Located RdrName } -- Qualified or unqualified
1741 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1744 tycon :: { Located RdrName } -- Unqualified
1745 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1747 qtyconsym :: { Located RdrName }
1748 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1751 tyconsym :: { Located RdrName }
1752 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1754 -----------------------------------------------------------------------------
1757 op :: { Located RdrName } -- used in infix decls
1761 varop :: { Located RdrName }
1763 | '`' varid '`' { LL (unLoc $2) }
1765 qop :: { LHsExpr RdrName } -- used in sections
1766 : qvarop { L1 $ HsVar (unLoc $1) }
1767 | qconop { L1 $ HsVar (unLoc $1) }
1769 qopm :: { LHsExpr RdrName } -- used in sections
1770 : qvaropm { L1 $ HsVar (unLoc $1) }
1771 | qconop { L1 $ HsVar (unLoc $1) }
1773 qvarop :: { Located RdrName }
1775 | '`' qvarid '`' { LL (unLoc $2) }
1777 qvaropm :: { Located RdrName }
1778 : qvarsym_no_minus { $1 }
1779 | '`' qvarid '`' { LL (unLoc $2) }
1781 -----------------------------------------------------------------------------
1784 tyvar :: { Located RdrName }
1785 tyvar : tyvarid { $1 }
1786 | '(' tyvarsym ')' { LL (unLoc $2) }
1788 tyvarop :: { Located RdrName }
1789 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1792 tyvarid :: { Located RdrName }
1793 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1794 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1795 | 'unsafe' { L1 $! mkUnqual tvName (fsLit "unsafe") }
1796 | 'safe' { L1 $! mkUnqual tvName (fsLit "safe") }
1797 | 'threadsafe' { L1 $! mkUnqual tvName (fsLit "threadsafe") }
1799 tyvarsym :: { Located RdrName }
1800 -- Does not include "!", because that is used for strictness marks
1801 -- or ".", because that separates the quantified type vars from the rest
1802 -- or "*", because that's used for kinds
1803 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1805 -----------------------------------------------------------------------------
1808 var :: { Located RdrName }
1810 | '(' varsym ')' { LL (unLoc $2) }
1812 qvar :: { Located RdrName }
1814 | '(' varsym ')' { LL (unLoc $2) }
1815 | '(' qvarsym1 ')' { LL (unLoc $2) }
1816 -- We've inlined qvarsym here so that the decision about
1817 -- whether it's a qvar or a var can be postponed until
1818 -- *after* we see the close paren.
1820 qvarid :: { Located RdrName }
1822 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1824 varid :: { Located RdrName }
1825 : varid_no_unsafe { $1 }
1826 | 'unsafe' { L1 $! mkUnqual varName (fsLit "unsafe") }
1827 | 'safe' { L1 $! mkUnqual varName (fsLit "safe") }
1828 | 'threadsafe' { L1 $! mkUnqual varName (fsLit "threadsafe") }
1830 varid_no_unsafe :: { Located RdrName }
1831 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1832 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1833 | 'forall' { L1 $! mkUnqual varName (fsLit "forall") }
1834 | 'family' { L1 $! mkUnqual varName (fsLit "family") }
1836 qvarsym :: { Located RdrName }
1840 qvarsym_no_minus :: { Located RdrName }
1841 : varsym_no_minus { $1 }
1844 qvarsym1 :: { Located RdrName }
1845 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1847 varsym :: { Located RdrName }
1848 : varsym_no_minus { $1 }
1849 | '-' { L1 $ mkUnqual varName (fsLit "-") }
1851 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1852 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1853 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1856 -- These special_ids are treated as keywords in various places,
1857 -- but as ordinary ids elsewhere. 'special_id' collects all these
1858 -- except 'unsafe', 'forall', and 'family' whose treatment differs
1859 -- depending on context
1860 special_id :: { Located FastString }
1862 : 'as' { L1 (fsLit "as") }
1863 | 'qualified' { L1 (fsLit "qualified") }
1864 | 'hiding' { L1 (fsLit "hiding") }
1865 | 'export' { L1 (fsLit "export") }
1866 | 'label' { L1 (fsLit "label") }
1867 | 'dynamic' { L1 (fsLit "dynamic") }
1868 | 'stdcall' { L1 (fsLit "stdcall") }
1869 | 'ccall' { L1 (fsLit "ccall") }
1871 special_sym :: { Located FastString }
1872 special_sym : '!' { L1 (fsLit "!") }
1873 | '.' { L1 (fsLit ".") }
1874 | '*' { L1 (fsLit "*") }
1876 -----------------------------------------------------------------------------
1877 -- Data constructors
1879 qconid :: { Located RdrName } -- Qualified or unqualified
1881 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1883 conid :: { Located RdrName }
1884 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1886 qconsym :: { Located RdrName } -- Qualified or unqualified
1888 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1890 consym :: { Located RdrName }
1891 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1893 -- ':' means only list cons
1894 | ':' { L1 $ consDataCon_RDR }
1897 -----------------------------------------------------------------------------
1900 literal :: { Located HsLit }
1901 : CHAR { L1 $ HsChar $ getCHAR $1 }
1902 | STRING { L1 $ HsString $ getSTRING $1 }
1903 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1904 | PRIMWORD { L1 $ HsWordPrim $ getPRIMWORD $1 }
1905 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1906 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1907 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1908 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1910 -----------------------------------------------------------------------------
1914 : vccurly { () } -- context popped in lexer.
1915 | error {% popContext }
1917 -----------------------------------------------------------------------------
1918 -- Miscellaneous (mostly renamings)
1920 modid :: { Located ModuleName }
1921 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1922 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1925 (unpackFS mod ++ '.':unpackFS c))
1929 : commas ',' { $1 + 1 }
1932 -----------------------------------------------------------------------------
1933 -- Documentation comments
1935 docnext :: { LHsDoc RdrName }
1936 : DOCNEXT {% case parseHaddockParagraphs (tokenise (getDOCNEXT $1)) of {
1937 MyLeft err -> parseError (getLoc $1) err;
1938 MyRight doc -> return (L1 doc) } }
1940 docprev :: { LHsDoc RdrName }
1941 : DOCPREV {% case parseHaddockParagraphs (tokenise (getDOCPREV $1)) of {
1942 MyLeft err -> parseError (getLoc $1) err;
1943 MyRight doc -> return (L1 doc) } }
1945 docnamed :: { Located (String, (HsDoc RdrName)) }
1947 let string = getDOCNAMED $1
1948 (name, rest) = break isSpace string
1949 in case parseHaddockParagraphs (tokenise rest) of {
1950 MyLeft err -> parseError (getLoc $1) err;
1951 MyRight doc -> return (L1 (name, doc)) } }
1953 docsection :: { Located (Int, HsDoc RdrName) }
1954 : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1955 case parseHaddockString (tokenise doc) of {
1956 MyLeft err -> parseError (getLoc $1) err;
1957 MyRight doc -> return (L1 (n, doc)) } }
1959 moduleheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
1960 : DOCNEXT {% let string = getDOCNEXT $1 in
1961 case parseModuleHeader string of {
1962 Right (str, info) ->
1963 case parseHaddockParagraphs (tokenise str) of {
1964 MyLeft err -> parseError (getLoc $1) err;
1965 MyRight doc -> return (info, Just doc);
1967 Left err -> parseError (getLoc $1) err
1970 maybe_docprev :: { Maybe (LHsDoc RdrName) }
1971 : docprev { Just $1 }
1972 | {- empty -} { Nothing }
1974 maybe_docnext :: { Maybe (LHsDoc RdrName) }
1975 : docnext { Just $1 }
1976 | {- empty -} { Nothing }
1980 happyError = srcParseFail
1982 getVARID (L _ (ITvarid x)) = x
1983 getCONID (L _ (ITconid x)) = x
1984 getVARSYM (L _ (ITvarsym x)) = x
1985 getCONSYM (L _ (ITconsym x)) = x
1986 getQVARID (L _ (ITqvarid x)) = x
1987 getQCONID (L _ (ITqconid x)) = x
1988 getQVARSYM (L _ (ITqvarsym x)) = x
1989 getQCONSYM (L _ (ITqconsym x)) = x
1990 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1991 getCHAR (L _ (ITchar x)) = x
1992 getSTRING (L _ (ITstring x)) = x
1993 getINTEGER (L _ (ITinteger x)) = x
1994 getRATIONAL (L _ (ITrational x)) = x
1995 getPRIMCHAR (L _ (ITprimchar x)) = x
1996 getPRIMSTRING (L _ (ITprimstring x)) = x
1997 getPRIMINTEGER (L _ (ITprimint x)) = x
1998 getPRIMWORD (L _ (ITprimword x)) = x
1999 getPRIMFLOAT (L _ (ITprimfloat x)) = x
2000 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
2001 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
2002 getINLINE (L _ (ITinline_prag b)) = b
2003 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
2005 getDOCNEXT (L _ (ITdocCommentNext x)) = x
2006 getDOCPREV (L _ (ITdocCommentPrev x)) = x
2007 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
2008 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
2010 getSCC :: Located Token -> P FastString
2011 getSCC lt = do let s = getSTRING lt
2012 err = "Spaces are not allowed in SCCs"
2013 -- We probably actually want to be more restrictive than this
2014 if ' ' `elem` unpackFS s
2015 then failSpanMsgP (getLoc lt) (text err)
2018 -- Utilities for combining source spans
2019 comb2 :: Located a -> Located b -> SrcSpan
2020 comb2 a b = a `seq` b `seq` combineLocs a b
2022 comb3 :: Located a -> Located b -> Located c -> SrcSpan
2023 comb3 a b c = a `seq` b `seq` c `seq`
2024 combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
2026 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
2027 comb4 a b c d = a `seq` b `seq` c `seq` d `seq`
2028 (combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
2029 combineSrcSpans (getLoc c) (getLoc d))
2031 -- strict constructor version:
2033 sL :: SrcSpan -> a -> Located a
2034 sL span a = span `seq` a `seq` L span a
2036 -- Make a source location for the file. We're a bit lazy here and just
2037 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
2038 -- try to find the span of the whole file (ToDo).
2039 fileSrcSpan :: P SrcSpan
2042 let loc = mkSrcLoc (srcLocFile l) 1 0;
2043 return (mkSrcSpan loc loc)