2 -- ---------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1997-2003
7 -- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -- ---------------------------------------------------------------------------
11 module Parser ( parseModule, parseStmt, parseIdentifier, parseType,
14 #define INCLUDE #include
15 INCLUDE "HsVersions.h"
19 import HscTypes ( IsBootInterface, DeprecTxt )
22 import TysWiredIn ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
23 listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
24 import Type ( funTyCon )
25 import ForeignCall ( Safety(..), CExportSpec(..), CLabelString,
26 CCallConv(..), CCallTarget(..), defaultCCallConv
28 import OccName ( varName, dataName, tcClsName, tvName )
29 import DataCon ( DataCon, dataConName )
30 import SrcLoc ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
31 SrcSpan, combineLocs, srcLocFile,
34 import StaticFlags ( opt_SccProfilingOn )
35 import Type ( Kind, mkArrowKind, liftedTypeKind, unliftedTypeKind )
36 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
37 Activation(..), defaultInlineSpec )
41 import Maybes ( orElse )
48 -----------------------------------------------------------------------------
51 Conflicts: 37 shift/reduce
54 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
55 would think the two should never occur in the same context.
59 -----------------------------------------------------------------------------
60 Conflicts: 36 shift/reduce (1.25)
62 10 for abiguity in 'if x then y else z + 1' [State 178]
63 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
64 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
66 1 for ambiguity in 'if x then y else z :: T' [State 178]
67 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
69 4 for ambiguity in 'if x then y else z -< e' [State 178]
70 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
71 There are four such operators: -<, >-, -<<, >>-
74 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
75 Which of these two is intended?
77 (x::T) -> T -- Rhs is T
80 (x::T -> T) -> .. -- Rhs is ...
82 10 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
85 As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
86 Same duplication between states 11 and 253 as the previous case
88 1 for ambiguity in 'let ?x ...' [State 329]
89 the parser can't tell whether the ?x is the lhs of a normal binding or
90 an implicit binding. Fortunately resolving as shift gives it the only
91 sensible meaning, namely the lhs of an implicit binding.
93 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
94 we don't know whether the '[' starts the activation or not: it
95 might be the start of the declaration with the activation being
98 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
99 since 'forall' is a valid variable name, we don't know whether
100 to treat a forall on the input as the beginning of a quantifier
101 or the beginning of the rule itself. Resolving to shift means
102 it's always treated as a quantifier, hence the above is disallowed.
103 This saves explicitly defining a grammar for the rule lhs that
104 doesn't include 'forall'.
106 -- ---------------------------------------------------------------------------
107 -- Adding location info
109 This is done in a stylised way using the three macros below, L0, L1
110 and LL. Each of these macros can be thought of as having type
112 L0, L1, LL :: a -> Located a
114 They each add a SrcSpan to their argument.
116 L0 adds 'noSrcSpan', used for empty productions
117 -- This doesn't seem to work anymore -=chak
119 L1 for a production with a single token on the lhs. Grabs the SrcSpan
122 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
123 the first and last tokens.
125 These suffice for the majority of cases. However, we must be
126 especially careful with empty productions: LL won't work if the first
127 or last token on the lhs can represent an empty span. In these cases,
128 we have to calculate the span using more of the tokens from the lhs, eg.
130 | 'newtype' tycl_hdr '=' newconstr deriving
132 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
134 We provide comb3 and comb4 functions which are useful in such cases.
136 Be careful: there's no checking that you actually got this right, the
137 only symptom will be that the SrcSpans of your syntax will be
141 * We must expand these macros *before* running Happy, which is why this file is
142 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
144 #define L0 L noSrcSpan
145 #define L1 sL (getLoc $1)
146 #define LL sL (comb2 $1 $>)
148 -- -----------------------------------------------------------------------------
153 '_' { L _ ITunderscore } -- Haskell keywords
155 'case' { L _ ITcase }
156 'class' { L _ ITclass }
157 'data' { L _ ITdata }
158 'default' { L _ ITdefault }
159 'deriving' { L _ ITderiving }
161 'else' { L _ ITelse }
163 'hiding' { L _ IThiding }
165 'import' { L _ ITimport }
167 'infix' { L _ ITinfix }
168 'infixl' { L _ ITinfixl }
169 'infixr' { L _ ITinfixr }
170 'instance' { L _ ITinstance }
172 'module' { L _ ITmodule }
173 'newtype' { L _ ITnewtype }
175 'qualified' { L _ ITqualified }
176 'then' { L _ ITthen }
177 'type' { L _ ITtype }
178 'where' { L _ ITwhere }
179 '_scc_' { L _ ITscc } -- ToDo: remove
181 'forall' { L _ ITforall } -- GHC extension keywords
182 'foreign' { L _ ITforeign }
183 'export' { L _ ITexport }
184 'label' { L _ ITlabel }
185 'dynamic' { L _ ITdynamic }
186 'safe' { L _ ITsafe }
187 'threadsafe' { L _ ITthreadsafe }
188 'unsafe' { L _ ITunsafe }
191 'family' { L _ ITfamily }
192 'stdcall' { L _ ITstdcallconv }
193 'ccall' { L _ ITccallconv }
194 'dotnet' { L _ ITdotnet }
195 'proc' { L _ ITproc } -- for arrow notation extension
196 'rec' { L _ ITrec } -- for arrow notation extension
198 '{-# INLINE' { L _ (ITinline_prag _) }
199 '{-# SPECIALISE' { L _ ITspec_prag }
200 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
201 '{-# SOURCE' { L _ ITsource_prag }
202 '{-# RULES' { L _ ITrules_prag }
203 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
204 '{-# SCC' { L _ ITscc_prag }
205 '{-# DEPRECATED' { L _ ITdeprecated_prag }
206 '{-# UNPACK' { L _ ITunpack_prag }
207 '#-}' { L _ ITclose_prag }
209 '..' { L _ ITdotdot } -- reserved symbols
211 '::' { L _ ITdcolon }
215 '<-' { L _ ITlarrow }
216 '->' { L _ ITrarrow }
219 '=>' { L _ ITdarrow }
223 '-<' { L _ ITlarrowtail } -- for arrow notation
224 '>-' { L _ ITrarrowtail } -- for arrow notation
225 '-<<' { L _ ITLarrowtail } -- for arrow notation
226 '>>-' { L _ ITRarrowtail } -- for arrow notation
229 '{' { L _ ITocurly } -- special symbols
231 '{|' { L _ ITocurlybar }
232 '|}' { L _ ITccurlybar }
233 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
234 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
237 '[:' { L _ ITopabrack }
238 ':]' { L _ ITcpabrack }
241 '(#' { L _ IToubxparen }
242 '#)' { L _ ITcubxparen }
243 '(|' { L _ IToparenbar }
244 '|)' { L _ ITcparenbar }
247 '`' { L _ ITbackquote }
249 VARID { L _ (ITvarid _) } -- identifiers
250 CONID { L _ (ITconid _) }
251 VARSYM { L _ (ITvarsym _) }
252 CONSYM { L _ (ITconsym _) }
253 QVARID { L _ (ITqvarid _) }
254 QCONID { L _ (ITqconid _) }
255 QVARSYM { L _ (ITqvarsym _) }
256 QCONSYM { L _ (ITqconsym _) }
258 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
260 CHAR { L _ (ITchar _) }
261 STRING { L _ (ITstring _) }
262 INTEGER { L _ (ITinteger _) }
263 RATIONAL { L _ (ITrational _) }
265 PRIMCHAR { L _ (ITprimchar _) }
266 PRIMSTRING { L _ (ITprimstring _) }
267 PRIMINTEGER { L _ (ITprimint _) }
268 PRIMFLOAT { L _ (ITprimfloat _) }
269 PRIMDOUBLE { L _ (ITprimdouble _) }
272 '[|' { L _ ITopenExpQuote }
273 '[p|' { L _ ITopenPatQuote }
274 '[t|' { L _ ITopenTypQuote }
275 '[d|' { L _ ITopenDecQuote }
276 '|]' { L _ ITcloseQuote }
277 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
278 '$(' { L _ ITparenEscape } -- $( exp )
279 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
280 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
282 %monad { P } { >>= } { return }
283 %lexer { lexer } { L _ ITeof }
284 %name parseModule module
285 %name parseStmt maybe_stmt
286 %name parseIdentifier identifier
287 %name parseType ctype
288 %partial parseHeader header
289 %tokentype { (Located Token) }
292 -----------------------------------------------------------------------------
293 -- Identifiers; one of the entry points
294 identifier :: { Located RdrName }
300 -----------------------------------------------------------------------------
303 -- The place for module deprecation is really too restrictive, but if it
304 -- was allowed at its natural place just before 'module', we get an ugly
305 -- s/r conflict with the second alternative. Another solution would be the
306 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
307 -- either, and DEPRECATED is only expected to be used by people who really
308 -- know what they are doing. :-)
310 module :: { Located (HsModule RdrName) }
311 : 'module' modid maybemoddeprec maybeexports 'where' body
312 {% fileSrcSpan >>= \ loc ->
313 return (L loc (HsModule (Just $2) $4 (fst $6) (snd $6) $3)) }
314 | missing_module_keyword top close
315 {% fileSrcSpan >>= \ loc ->
316 return (L loc (HsModule Nothing Nothing
317 (fst $2) (snd $2) Nothing)) }
319 missing_module_keyword :: { () }
320 : {- empty -} {% pushCurrentContext }
322 maybemoddeprec :: { Maybe DeprecTxt }
323 : '{-# DEPRECATED' STRING '#-}' { Just (getSTRING $2) }
324 | {- empty -} { Nothing }
326 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
328 | vocurly top close { $2 }
330 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
331 : importdecls { (reverse $1,[]) }
332 | importdecls ';' cvtopdecls { (reverse $1,$3) }
333 | cvtopdecls { ([],$1) }
335 cvtopdecls :: { [LHsDecl RdrName] }
336 : topdecls { cvTopDecls $1 }
338 -----------------------------------------------------------------------------
339 -- Module declaration & imports only
341 header :: { Located (HsModule RdrName) }
342 : 'module' modid maybemoddeprec maybeexports 'where' header_body
343 {% fileSrcSpan >>= \ loc ->
344 return (L loc (HsModule (Just $2) $4 $6 [] $3)) }
345 | missing_module_keyword importdecls
346 {% fileSrcSpan >>= \ loc ->
347 return (L loc (HsModule Nothing Nothing $2 [] Nothing)) }
349 header_body :: { [LImportDecl RdrName] }
350 : '{' importdecls { $2 }
351 | vocurly importdecls { $2 }
353 -----------------------------------------------------------------------------
356 maybeexports :: { Maybe [LIE RdrName] }
357 : '(' exportlist ')' { Just $2 }
358 | {- empty -} { Nothing }
360 exportlist :: { [LIE RdrName] }
364 exportlist1 :: { [LIE RdrName] }
366 | export ',' exportlist { $1 : $3 }
369 -- No longer allow things like [] and (,,,) to be exported
370 -- They are built in syntax, always available
371 export :: { LIE RdrName }
372 : qvar { L1 (IEVar (unLoc $1)) }
373 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
374 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
375 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
376 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
377 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
379 qcnames :: { [RdrName] }
380 : qcnames ',' qcname_ext { unLoc $3 : $1 }
381 | qcname_ext { [unLoc $1] }
383 qcname_ext :: { Located RdrName } -- Variable or data constructor
384 -- or tagged type constructor
386 | 'type' qcon { sL (comb2 $1 $2)
387 (setRdrNameSpace (unLoc $2)
390 -- Cannot pull into qcname_ext, as qcname is also used in expression.
391 qcname :: { Located RdrName } -- Variable or data constructor
395 -----------------------------------------------------------------------------
396 -- Import Declarations
398 -- import decls can be *empty*, or even just a string of semicolons
399 -- whereas topdecls must contain at least one topdecl.
401 importdecls :: { [LImportDecl RdrName] }
402 : importdecls ';' importdecl { $3 : $1 }
403 | importdecls ';' { $1 }
404 | importdecl { [ $1 ] }
407 importdecl :: { LImportDecl RdrName }
408 : 'import' maybe_src optqualified modid maybeas maybeimpspec
409 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
411 maybe_src :: { IsBootInterface }
412 : '{-# SOURCE' '#-}' { True }
413 | {- empty -} { False }
415 optqualified :: { Bool }
416 : 'qualified' { True }
417 | {- empty -} { False }
419 maybeas :: { Located (Maybe ModuleName) }
420 : 'as' modid { LL (Just (unLoc $2)) }
421 | {- empty -} { noLoc Nothing }
423 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
424 : impspec { L1 (Just (unLoc $1)) }
425 | {- empty -} { noLoc Nothing }
427 impspec :: { Located (Bool, [LIE RdrName]) }
428 : '(' exportlist ')' { LL (False, $2) }
429 | 'hiding' '(' exportlist ')' { LL (True, $3) }
431 -----------------------------------------------------------------------------
432 -- Fixity Declarations
436 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
438 infix :: { Located FixityDirection }
439 : 'infix' { L1 InfixN }
440 | 'infixl' { L1 InfixL }
441 | 'infixr' { L1 InfixR }
443 ops :: { Located [Located RdrName] }
444 : ops ',' op { LL ($3 : unLoc $1) }
447 -----------------------------------------------------------------------------
448 -- Top-Level Declarations
450 topdecls :: { OrdList (LHsDecl RdrName) }
451 : topdecls ';' topdecl { $1 `appOL` $3 }
452 | topdecls ';' { $1 }
455 topdecl :: { OrdList (LHsDecl RdrName) }
456 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
457 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
458 | 'instance' inst_type where
459 { let (binds, sigs, ats) = cvBindsAndSigs (unLoc $3)
460 in unitOL (L (comb3 $1 $2 $3)
461 (InstD (InstDecl $2 binds sigs ats))) }
462 | stand_alone_deriving { unitOL (LL (DerivD (unLoc $1))) }
463 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
464 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
465 | '{-# DEPRECATED' deprecations '#-}' { $2 }
466 | '{-# RULES' rules '#-}' { $2 }
469 -- Template Haskell Extension
470 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
471 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
472 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
477 cl_decl :: { LTyClDecl RdrName }
478 : 'class' tycl_hdr fds where
479 {% do { let { (binds, sigs, ats) =
480 cvBindsAndSigs (unLoc $4)
481 ; (ctxt, tc, tvs, tparms) = unLoc $2}
482 ; checkTyVars tparms -- only type vars allowed
484 ; return $ L (comb4 $1 $2 $3 $4)
485 (mkClassDecl (ctxt, tc, tvs)
486 (unLoc $3) sigs binds ats) } }
488 -- Type declarations (toplevel)
490 ty_decl :: { LTyClDecl RdrName }
491 -- ordinary type synonyms
492 : 'type' type '=' ctype
493 -- Note ctype, not sigtype, on the right of '='
494 -- We allow an explicit for-all but we don't insert one
495 -- in type Foo a = (b,b)
496 -- Instead we just say b is out of scope
498 -- Note the use of type for the head; this allows
499 -- infix type constructors to be declared
500 {% do { (tc, tvs, _) <- checkSynHdr $2 False
501 ; return (L (comb2 $1 $4)
502 (TySynonym tc tvs Nothing $4))
505 -- type family declarations
506 | 'type' 'family' type opt_kind_sig
507 -- Note the use of type for the head; this allows
508 -- infix type constructors to be declared
510 {% do { (tc, tvs, _) <- checkSynHdr $3 False
511 ; let kind = case unLoc $4 of
512 Nothing -> liftedTypeKind
514 ; return (L (comb3 $1 $3 $4)
515 (TyFunction tc tvs False kind))
518 -- type instance declarations
519 | 'type' 'instance' type '=' ctype
520 -- Note the use of type for the head; this allows
521 -- infix type constructors and type patterns
523 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
524 ; return (L (comb2 $1 $5)
525 (TySynonym tc tvs (Just typats) $5))
528 -- ordinary data type or newtype declaration
529 | data_or_newtype tycl_hdr constrs deriving
530 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
531 ; checkTyVars tparms -- no type pattern
533 L (comb4 $1 $2 $3 $4)
534 -- We need the location on tycl_hdr in case
535 -- constrs and deriving are both empty
536 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
537 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
539 -- ordinary GADT declaration
540 | data_or_newtype tycl_hdr opt_kind_sig
541 'where' gadt_constrlist
543 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
544 ; checkTyVars tparms -- can have type pats
546 L (comb4 $1 $2 $4 $5)
547 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
548 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
550 -- data/newtype family
551 | data_or_newtype 'family' tycl_hdr opt_kind_sig
552 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
553 ; checkTyVars tparms -- no type pattern
554 ; let kind = case unLoc $4 of
555 Nothing -> liftedTypeKind
559 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
560 (Just kind) [] Nothing) } }
562 -- data/newtype instance declaration
563 | data_or_newtype 'instance' tycl_hdr constrs deriving
564 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
565 -- can have type pats
567 L (comb4 $1 $3 $4 $5)
568 -- We need the location on tycl_hdr in case
569 -- constrs and deriving are both empty
570 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
571 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
573 -- GADT instance declaration
574 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
575 'where' gadt_constrlist
577 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
578 -- can have type pats
580 L (comb4 $1 $3 $6 $7)
581 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
582 (unLoc $4) (reverse (unLoc $6)) (unLoc $7)) } }
584 -- Associate type declarations
586 at_decl :: { LTyClDecl RdrName }
587 -- type family declarations
588 : 'type' type opt_kind_sig
589 -- Note the use of type for the head; this allows
590 -- infix type constructors to be declared
592 {% do { (tc, tvs, _) <- checkSynHdr $2 False
593 ; let kind = case unLoc $3 of
594 Nothing -> liftedTypeKind
596 ; return (L (comb3 $1 $2 $3)
597 (TyFunction tc tvs False kind))
600 -- type instance declarations
601 | 'type' type '=' ctype
602 -- Note the use of type for the head; this allows
603 -- infix type constructors and type patterns
605 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
606 ; return (L (comb2 $1 $4)
607 (TySynonym tc tvs (Just typats) $4))
610 -- data/newtype family
611 | data_or_newtype tycl_hdr '::' kind
612 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
613 ; checkTyVars tparms -- no type pattern
616 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
617 (Just (unLoc $4)) [] Nothing) } }
619 -- data/newtype instance declaration
620 | data_or_newtype tycl_hdr constrs deriving
621 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
622 -- can have type pats
624 L (comb4 $1 $2 $3 $4)
625 -- We need the location on tycl_hdr in case
626 -- constrs and deriving are both empty
627 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
628 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
630 -- GADT instance declaration
631 | data_or_newtype tycl_hdr opt_kind_sig
632 'where' gadt_constrlist
634 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
635 -- can have type pats
637 L (comb4 $1 $2 $5 $6)
638 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
639 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
645 data_or_newtype :: { Located NewOrData }
646 : 'data' { L1 DataType }
647 | 'newtype' { L1 NewType }
649 opt_kind_sig :: { Located (Maybe Kind) }
651 | '::' kind { LL (Just (unLoc $2)) }
653 -- tycl_hdr parses the header of a class or data type decl,
654 -- which takes the form
657 -- (Eq a, Ord b) => T a b
658 -- T Int [a] -- for associated types
659 -- Rather a lot of inlining here, else we get reduce/reduce errors
660 tycl_hdr :: { Located (LHsContext RdrName,
662 [LHsTyVarBndr RdrName],
664 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
665 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
667 -----------------------------------------------------------------------------
668 -- Stand-alone deriving
670 -- Glasgow extension: stand-alone deriving declarations
671 stand_alone_deriving :: { LDerivDecl RdrName }
672 : 'deriving' qtycon 'for' qtycon {% do { p <- checkInstType (fmap HsTyVar $2)
673 ; checkDerivDecl (LL (DerivDecl p $4)) } }
675 | 'deriving' '(' inst_type ')' 'for' qtycon {% checkDerivDecl (LL (DerivDecl $3 $6)) }
677 -----------------------------------------------------------------------------
678 -- Nested declarations
680 -- Type declaration or value declaration
682 tydecl :: { Located (OrdList (LHsDecl RdrName)) }
683 tydecl : at_decl { LL (unitOL (L1 (TyClD (unLoc $1)))) }
686 tydecls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
687 : tydecls ';' tydecl { LL (unLoc $1 `appOL` unLoc $3) }
688 | tydecls ';' { LL (unLoc $1) }
690 | {- empty -} { noLoc nilOL }
694 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
695 : '{' tydecls '}' { LL (unLoc $2) }
696 | vocurly tydecls close { $2 }
698 -- Form of the body of class and instance declarations
700 where :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
701 -- No implicit parameters
702 -- May have type declarations
703 : 'where' tydecllist { LL (unLoc $2) }
704 | {- empty -} { noLoc nilOL }
706 decls :: { Located (OrdList (LHsDecl RdrName)) }
707 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
708 | decls ';' { LL (unLoc $1) }
710 | {- empty -} { noLoc nilOL }
713 decllist :: { Located (OrdList (LHsDecl RdrName)) }
714 : '{' decls '}' { LL (unLoc $2) }
715 | vocurly decls close { $2 }
717 -- Binding groups other than those of class and instance declarations
719 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
720 -- No type declarations
721 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
722 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
723 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
725 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
726 -- No type declarations
727 : 'where' binds { LL (unLoc $2) }
728 | {- empty -} { noLoc emptyLocalBinds }
731 -----------------------------------------------------------------------------
732 -- Transformation Rules
734 rules :: { OrdList (LHsDecl RdrName) }
735 : rules ';' rule { $1 `snocOL` $3 }
738 | {- empty -} { nilOL }
740 rule :: { LHsDecl RdrName }
741 : STRING activation rule_forall infixexp '=' exp
742 { LL $ RuleD (HsRule (getSTRING $1)
743 ($2 `orElse` AlwaysActive)
744 $3 $4 placeHolderNames $6 placeHolderNames) }
746 activation :: { Maybe Activation }
747 : {- empty -} { Nothing }
748 | explicit_activation { Just $1 }
750 explicit_activation :: { Activation } -- In brackets
751 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
752 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
754 rule_forall :: { [RuleBndr RdrName] }
755 : 'forall' rule_var_list '.' { $2 }
758 rule_var_list :: { [RuleBndr RdrName] }
760 | rule_var rule_var_list { $1 : $2 }
762 rule_var :: { RuleBndr RdrName }
763 : varid { RuleBndr $1 }
764 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
766 -----------------------------------------------------------------------------
767 -- Deprecations (c.f. rules)
769 deprecations :: { OrdList (LHsDecl RdrName) }
770 : deprecations ';' deprecation { $1 `appOL` $3 }
771 | deprecations ';' { $1 }
773 | {- empty -} { nilOL }
775 -- SUP: TEMPORARY HACK, not checking for `module Foo'
776 deprecation :: { OrdList (LHsDecl RdrName) }
778 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
782 -----------------------------------------------------------------------------
783 -- Foreign import and export declarations
785 fdecl :: { LHsDecl RdrName }
786 fdecl : 'import' callconv safety fspec
787 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
788 | 'import' callconv fspec
789 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
791 | 'export' callconv fspec
792 {% mkExport $2 (unLoc $3) >>= return.LL }
794 callconv :: { CallConv }
795 : 'stdcall' { CCall StdCallConv }
796 | 'ccall' { CCall CCallConv }
797 | 'dotnet' { DNCall }
800 : 'unsafe' { PlayRisky }
801 | 'safe' { PlaySafe False }
802 | 'threadsafe' { PlaySafe True }
804 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
805 : STRING var '::' sigtype { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
806 | var '::' sigtype { LL (noLoc nilFS, $1, $3) }
807 -- if the entity string is missing, it defaults to the empty string;
808 -- the meaning of an empty entity string depends on the calling
811 -----------------------------------------------------------------------------
814 opt_sig :: { Maybe (LHsType RdrName) }
815 : {- empty -} { Nothing }
816 | '::' sigtype { Just $2 }
818 opt_asig :: { Maybe (LHsType RdrName) }
819 : {- empty -} { Nothing }
820 | '::' atype { Just $2 }
822 sigtypes1 :: { [LHsType RdrName] }
824 | sigtype ',' sigtypes1 { $1 : $3 }
826 sigtype :: { LHsType RdrName }
827 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
828 -- Wrap an Implicit forall if there isn't one there already
830 sig_vars :: { Located [Located RdrName] }
831 : sig_vars ',' var { LL ($3 : unLoc $1) }
834 -----------------------------------------------------------------------------
837 strict_mark :: { Located HsBang }
838 : '!' { L1 HsStrict }
839 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
841 -- A ctype is a for-all type
842 ctype :: { LHsType RdrName }
843 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
844 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
845 -- A type of form (context => type) is an *implicit* HsForAllTy
848 -- We parse a context as a btype so that we don't get reduce/reduce
849 -- errors in ctype. The basic problem is that
851 -- looks so much like a tuple type. We can't tell until we find the =>
852 context :: { LHsContext RdrName }
853 : btype {% checkContext $1 }
855 type :: { LHsType RdrName }
856 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
859 gentype :: { LHsType RdrName }
861 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
862 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
863 | btype '->' ctype { LL $ HsFunTy $1 $3 }
865 btype :: { LHsType RdrName }
866 : btype atype { LL $ HsAppTy $1 $2 }
869 atype :: { LHsType RdrName }
870 : gtycon { L1 (HsTyVar (unLoc $1)) }
871 | tyvar { L1 (HsTyVar (unLoc $1)) }
872 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
873 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
874 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
875 | '[' ctype ']' { LL $ HsListTy $2 }
876 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
877 | '(' ctype ')' { LL $ HsParTy $2 }
878 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
880 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
882 -- An inst_type is what occurs in the head of an instance decl
883 -- e.g. (Foo a, Gaz b) => Wibble a b
884 -- It's kept as a single type, with a MonoDictTy at the right
885 -- hand corner, for convenience.
886 inst_type :: { LHsType RdrName }
887 : sigtype {% checkInstType $1 }
889 inst_types1 :: { [LHsType RdrName] }
891 | inst_type ',' inst_types1 { $1 : $3 }
893 comma_types0 :: { [LHsType RdrName] }
894 : comma_types1 { $1 }
897 comma_types1 :: { [LHsType RdrName] }
899 | ctype ',' comma_types1 { $1 : $3 }
901 tv_bndrs :: { [LHsTyVarBndr RdrName] }
902 : tv_bndr tv_bndrs { $1 : $2 }
905 tv_bndr :: { LHsTyVarBndr RdrName }
906 : tyvar { L1 (UserTyVar (unLoc $1)) }
907 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
910 fds :: { Located [Located ([RdrName], [RdrName])] }
911 : {- empty -} { noLoc [] }
912 | '|' fds1 { LL (reverse (unLoc $2)) }
914 fds1 :: { Located [Located ([RdrName], [RdrName])] }
915 : fds1 ',' fd { LL ($3 : unLoc $1) }
918 fd :: { Located ([RdrName], [RdrName]) }
919 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
920 (reverse (unLoc $1), reverse (unLoc $3)) }
922 varids0 :: { Located [RdrName] }
923 : {- empty -} { noLoc [] }
924 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
926 -----------------------------------------------------------------------------
929 kind :: { Located Kind }
931 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
933 akind :: { Located Kind }
934 : '*' { L1 liftedTypeKind }
935 | '!' { L1 unliftedTypeKind }
936 | '(' kind ')' { LL (unLoc $2) }
939 -----------------------------------------------------------------------------
940 -- Datatype declarations
942 gadt_constrlist :: { Located [LConDecl RdrName] }
943 : '{' gadt_constrs '}' { LL (unLoc $2) }
944 | vocurly gadt_constrs close { $2 }
946 gadt_constrs :: { Located [LConDecl RdrName] }
947 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
948 | gadt_constrs ';' { $1 }
949 | gadt_constr { L1 [$1] }
951 -- We allow the following forms:
952 -- C :: Eq a => a -> T a
953 -- C :: forall a. Eq a => !a -> T a
954 -- D { x,y :: a } :: T a
955 -- forall a. Eq a => D { x,y :: a } :: T a
957 gadt_constr :: { LConDecl RdrName }
959 { LL (mkGadtDecl $1 $3) }
960 -- Syntax: Maybe merge the record stuff with the single-case above?
961 -- (to kill the mostly harmless reduce/reduce error)
962 -- XXX revisit audreyt
963 | constr_stuff_record '::' sigtype
964 { let (con,details) = unLoc $1 in
965 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
967 | forall context '=>' constr_stuff_record '::' sigtype
968 { let (con,details) = unLoc $4 in
969 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
970 | forall constr_stuff_record '::' sigtype
971 { let (con,details) = unLoc $2 in
972 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
976 constrs :: { Located [LConDecl RdrName] }
977 : {- empty; a GHC extension -} { noLoc [] }
978 | '=' constrs1 { LL (unLoc $2) }
980 constrs1 :: { Located [LConDecl RdrName] }
981 : constrs1 '|' constr { LL ($3 : unLoc $1) }
984 constr :: { LConDecl RdrName }
985 : forall context '=>' constr_stuff
986 { let (con,details) = unLoc $4 in
987 LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
988 | forall constr_stuff
989 { let (con,details) = unLoc $2 in
990 LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
992 forall :: { Located [LHsTyVarBndr RdrName] }
993 : 'forall' tv_bndrs '.' { LL $2 }
994 | {- empty -} { noLoc [] }
996 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
997 -- We parse the constructor declaration
999 -- as a btype (treating C as a type constructor) and then convert C to be
1000 -- a data constructor. Reason: it might continue like this:
1002 -- in which case C really would be a type constructor. We can't resolve this
1003 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1004 : btype {% mkPrefixCon $1 [] >>= return.LL }
1005 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
1006 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
1007 | btype conop btype { LL ($2, InfixCon $1 $3) }
1009 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
1010 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
1011 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
1013 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
1014 : fielddecl ',' fielddecls { unLoc $1 : $3 }
1015 | fielddecl { [unLoc $1] }
1017 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
1018 : sig_vars '::' ctype { LL (reverse (unLoc $1), $3) }
1020 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1021 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1022 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1023 -- We don't allow a context, but that's sorted out by the type checker.
1024 deriving :: { Located (Maybe [LHsType RdrName]) }
1025 : {- empty -} { noLoc Nothing }
1026 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1027 ; p <- checkInstType (L loc (HsTyVar tv))
1028 ; return (LL (Just [p])) } }
1029 | 'deriving' '(' ')' { LL (Just []) }
1030 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1031 -- Glasgow extension: allow partial
1032 -- applications in derivings
1034 -----------------------------------------------------------------------------
1035 -- Value definitions
1037 {- There's an awkward overlap with a type signature. Consider
1038 f :: Int -> Int = ...rhs...
1039 Then we can't tell whether it's a type signature or a value
1040 definition with a result signature until we see the '='.
1041 So we have to inline enough to postpone reductions until we know.
1045 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1046 instead of qvar, we get another shift/reduce-conflict. Consider the
1049 { (^^) :: Int->Int ; } Type signature; only var allowed
1051 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1052 qvar allowed (because of instance decls)
1054 We can't tell whether to reduce var to qvar until after we've read the signatures.
1057 decl :: { Located (OrdList (LHsDecl RdrName)) }
1059 | '!' infixexp rhs {% do { pat <- checkPattern $2;
1060 return (LL $ unitOL $ LL $ ValD $
1061 PatBind (LL $ BangPat pat) (unLoc $3)
1062 placeHolderType placeHolderNames) } }
1063 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1064 return (LL $ unitOL (LL $ ValD r)) } }
1066 rhs :: { Located (GRHSs RdrName) }
1067 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1068 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1070 gdrhs :: { Located [LGRHS RdrName] }
1071 : gdrhs gdrh { LL ($2 : unLoc $1) }
1074 gdrh :: { LGRHS RdrName }
1075 : '|' quals '=' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1077 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1078 : infixexp '::' sigtype
1079 {% do s <- checkValSig $1 $3;
1080 return (LL $ unitOL (LL $ SigD s)) }
1081 -- See the above notes for why we need infixexp here
1082 | var ',' sig_vars '::' sigtype
1083 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1084 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1086 | '{-# INLINE' activation qvar '#-}'
1087 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1088 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1089 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1091 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1092 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1094 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1095 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1097 -----------------------------------------------------------------------------
1100 exp :: { LHsExpr RdrName }
1101 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1102 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1103 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1104 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1105 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1108 infixexp :: { LHsExpr RdrName }
1110 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1112 exp10 :: { LHsExpr RdrName }
1113 : '\\' aexp aexps opt_asig '->' exp
1114 {% checkPatterns ($2 : reverse $3) >>= \ ps ->
1115 return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1116 (GRHSs (unguardedRHS $6) emptyLocalBinds
1118 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1119 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1120 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1121 | '-' fexp { LL $ mkHsNegApp $2 }
1123 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1124 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1125 return (L loc (mkHsDo DoExpr stmts body)) }
1126 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1127 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1128 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1129 | scc_annot exp { LL $ if opt_SccProfilingOn
1130 then HsSCC (unLoc $1) $2
1133 | 'proc' aexp '->' exp
1134 {% checkPattern $2 >>= \ p ->
1135 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1136 placeHolderType undefined)) }
1137 -- TODO: is LL right here?
1139 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1140 -- hdaume: core annotation
1143 scc_annot :: { Located FastString }
1144 : '_scc_' STRING { LL $ getSTRING $2 }
1145 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
1147 fexp :: { LHsExpr RdrName }
1148 : fexp aexp { LL $ HsApp $1 $2 }
1151 aexps :: { [LHsExpr RdrName] }
1152 : aexps aexp { $2 : $1 }
1153 | {- empty -} { [] }
1155 aexp :: { LHsExpr RdrName }
1156 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1157 | '~' aexp { LL $ ELazyPat $2 }
1158 -- | '!' aexp { LL $ EBangPat $2 }
1161 aexp1 :: { LHsExpr RdrName }
1162 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1167 -- Here was the syntax for type applications that I was planning
1168 -- but there are difficulties (e.g. what order for type args)
1169 -- so it's not enabled yet.
1170 -- But this case *is* used for the left hand side of a generic definition,
1171 -- which is parsed as an expression before being munged into a pattern
1172 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1173 (sL (getLoc $3) (HsType $3)) }
1175 aexp2 :: { LHsExpr RdrName }
1176 : ipvar { L1 (HsIPVar $! unLoc $1) }
1177 | qcname { L1 (HsVar $! unLoc $1) }
1178 | literal { L1 (HsLit $! unLoc $1) }
1179 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1180 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1181 | '(' exp ')' { LL (HsPar $2) }
1182 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1183 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1184 | '[' list ']' { LL (unLoc $2) }
1185 | '[:' parr ':]' { LL (unLoc $2) }
1186 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1187 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1188 | '_' { L1 EWildPat }
1190 -- Template Haskell Extension
1191 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1192 (L1 $ HsVar (mkUnqual varName
1193 (getTH_ID_SPLICE $1)))) } -- $x
1194 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1196 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1197 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1198 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1199 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1200 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1201 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1202 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1203 return (LL $ HsBracket (PatBr p)) }
1204 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBr (mkGroup $2)) }
1206 -- arrow notation extension
1207 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1209 cmdargs :: { [LHsCmdTop RdrName] }
1210 : cmdargs acmd { $2 : $1 }
1211 | {- empty -} { [] }
1213 acmd :: { LHsCmdTop RdrName }
1214 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1216 cvtopbody :: { [LHsDecl RdrName] }
1217 : '{' cvtopdecls0 '}' { $2 }
1218 | vocurly cvtopdecls0 close { $2 }
1220 cvtopdecls0 :: { [LHsDecl RdrName] }
1221 : {- empty -} { [] }
1224 texp :: { LHsExpr RdrName }
1226 | qopm infixexp { LL $ SectionR $1 $2 }
1227 -- The second production is really here only for bang patterns
1230 texps :: { [LHsExpr RdrName] }
1231 : texps ',' texp { $3 : $1 }
1235 -----------------------------------------------------------------------------
1238 -- The rules below are little bit contorted to keep lexps left-recursive while
1239 -- avoiding another shift/reduce-conflict.
1241 list :: { LHsExpr RdrName }
1242 : texp { L1 $ ExplicitList placeHolderType [$1] }
1243 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1244 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1245 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1246 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1247 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1248 | texp pquals { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1250 lexps :: { Located [LHsExpr RdrName] }
1251 : lexps ',' texp { LL ($3 : unLoc $1) }
1252 | texp ',' texp { LL [$3,$1] }
1254 -----------------------------------------------------------------------------
1255 -- List Comprehensions
1257 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1258 -- or a reversed list of Stmts
1259 : pquals1 { case unLoc $1 of
1261 qss -> L1 [L1 (ParStmt stmtss)]
1263 stmtss = [ (reverse qs, undefined)
1267 pquals1 :: { Located [[LStmt RdrName]] }
1268 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1269 | '|' quals { L (getLoc $2) [unLoc $2] }
1271 quals :: { Located [LStmt RdrName] }
1272 : quals ',' qual { LL ($3 : unLoc $1) }
1275 -----------------------------------------------------------------------------
1276 -- Parallel array expressions
1278 -- The rules below are little bit contorted; see the list case for details.
1279 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1280 -- Moreover, we allow explicit arrays with no element (represented by the nil
1281 -- constructor in the list case).
1283 parr :: { LHsExpr RdrName }
1284 : { noLoc (ExplicitPArr placeHolderType []) }
1285 | exp { L1 $ ExplicitPArr placeHolderType [$1] }
1286 | lexps { L1 $ ExplicitPArr placeHolderType
1287 (reverse (unLoc $1)) }
1288 | exp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1289 | exp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1290 | exp pquals { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1292 -- We are reusing `lexps' and `pquals' from the list case.
1294 -----------------------------------------------------------------------------
1295 -- Case alternatives
1297 altslist :: { Located [LMatch RdrName] }
1298 : '{' alts '}' { LL (reverse (unLoc $2)) }
1299 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1301 alts :: { Located [LMatch RdrName] }
1302 : alts1 { L1 (unLoc $1) }
1303 | ';' alts { LL (unLoc $2) }
1305 alts1 :: { Located [LMatch RdrName] }
1306 : alts1 ';' alt { LL ($3 : unLoc $1) }
1307 | alts1 ';' { LL (unLoc $1) }
1310 alt :: { LMatch RdrName }
1311 : infixexp opt_sig alt_rhs {% checkPattern $1 >>= \p ->
1312 return (LL (Match [p] $2 (unLoc $3))) }
1313 | '!' infixexp opt_sig alt_rhs {% checkPattern $2 >>= \p ->
1314 return (LL (Match [LL $ BangPat p] $3 (unLoc $4))) }
1316 alt_rhs :: { Located (GRHSs RdrName) }
1317 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1319 ralt :: { Located [LGRHS RdrName] }
1320 : '->' exp { LL (unguardedRHS $2) }
1321 | gdpats { L1 (reverse (unLoc $1)) }
1323 gdpats :: { Located [LGRHS RdrName] }
1324 : gdpats gdpat { LL ($2 : unLoc $1) }
1327 gdpat :: { LGRHS RdrName }
1328 : '|' quals '->' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1330 -----------------------------------------------------------------------------
1331 -- Statement sequences
1333 stmtlist :: { Located [LStmt RdrName] }
1334 : '{' stmts '}' { LL (unLoc $2) }
1335 | vocurly stmts close { $2 }
1337 -- do { ;; s ; s ; ; s ;; }
1338 -- The last Stmt should be an expression, but that's hard to enforce
1339 -- here, because we need too much lookahead if we see do { e ; }
1340 -- So we use ExprStmts throughout, and switch the last one over
1341 -- in ParseUtils.checkDo instead
1342 stmts :: { Located [LStmt RdrName] }
1343 : stmt stmts_help { LL ($1 : unLoc $2) }
1344 | ';' stmts { LL (unLoc $2) }
1345 | {- empty -} { noLoc [] }
1347 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1348 : ';' stmts { LL (unLoc $2) }
1349 | {- empty -} { noLoc [] }
1351 -- For typing stmts at the GHCi prompt, where
1352 -- the input may consist of just comments.
1353 maybe_stmt :: { Maybe (LStmt RdrName) }
1355 | {- nothing -} { Nothing }
1357 stmt :: { LStmt RdrName }
1359 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1360 return (LL $ mkBindStmt p $1) }
1361 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1363 qual :: { LStmt RdrName }
1364 : exp '<-' exp {% checkPattern $1 >>= \p ->
1365 return (LL $ mkBindStmt p $3) }
1366 | exp { L1 $ mkExprStmt $1 }
1367 | 'let' binds { LL $ LetStmt (unLoc $2) }
1369 -----------------------------------------------------------------------------
1370 -- Record Field Update/Construction
1372 fbinds :: { HsRecordBinds RdrName }
1374 | {- empty -} { [] }
1376 fbinds1 :: { HsRecordBinds RdrName }
1377 : fbinds1 ',' fbind { $3 : $1 }
1380 fbind :: { (Located RdrName, LHsExpr RdrName) }
1381 : qvar '=' exp { ($1,$3) }
1383 -----------------------------------------------------------------------------
1384 -- Implicit Parameter Bindings
1386 dbinds :: { Located [LIPBind RdrName] }
1387 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1388 | dbinds ';' { LL (unLoc $1) }
1390 -- | {- empty -} { [] }
1392 dbind :: { LIPBind RdrName }
1393 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1395 ipvar :: { Located (IPName RdrName) }
1396 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1398 -----------------------------------------------------------------------------
1401 depreclist :: { Located [RdrName] }
1402 depreclist : deprec_var { L1 [unLoc $1] }
1403 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1405 deprec_var :: { Located RdrName }
1406 deprec_var : var { $1 }
1409 -----------------------------------------
1410 -- Data constructors
1411 qcon :: { Located RdrName }
1413 | '(' qconsym ')' { LL (unLoc $2) }
1414 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1415 -- The case of '[:' ':]' is part of the production `parr'
1417 con :: { Located RdrName }
1419 | '(' consym ')' { LL (unLoc $2) }
1420 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1422 sysdcon :: { Located DataCon } -- Wired in data constructors
1423 : '(' ')' { LL unitDataCon }
1424 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1425 | '[' ']' { LL nilDataCon }
1427 conop :: { Located RdrName }
1429 | '`' conid '`' { LL (unLoc $2) }
1431 qconop :: { Located RdrName }
1433 | '`' qconid '`' { LL (unLoc $2) }
1435 -----------------------------------------------------------------------------
1436 -- Type constructors
1438 gtycon :: { Located RdrName } -- A "general" qualified tycon
1440 | '(' ')' { LL $ getRdrName unitTyCon }
1441 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1442 | '(' '->' ')' { LL $ getRdrName funTyCon }
1443 | '[' ']' { LL $ listTyCon_RDR }
1444 | '[:' ':]' { LL $ parrTyCon_RDR }
1446 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1448 | '(' qtyconsym ')' { LL (unLoc $2) }
1450 qtyconop :: { Located RdrName } -- Qualified or unqualified
1452 | '`' qtycon '`' { LL (unLoc $2) }
1454 qtycon :: { Located RdrName } -- Qualified or unqualified
1455 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1458 tycon :: { Located RdrName } -- Unqualified
1459 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1461 qtyconsym :: { Located RdrName }
1462 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1465 tyconsym :: { Located RdrName }
1466 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1468 -----------------------------------------------------------------------------
1471 op :: { Located RdrName } -- used in infix decls
1475 varop :: { Located RdrName }
1477 | '`' varid '`' { LL (unLoc $2) }
1479 qop :: { LHsExpr RdrName } -- used in sections
1480 : qvarop { L1 $ HsVar (unLoc $1) }
1481 | qconop { L1 $ HsVar (unLoc $1) }
1483 qopm :: { LHsExpr RdrName } -- used in sections
1484 : qvaropm { L1 $ HsVar (unLoc $1) }
1485 | qconop { L1 $ HsVar (unLoc $1) }
1487 qvarop :: { Located RdrName }
1489 | '`' qvarid '`' { LL (unLoc $2) }
1491 qvaropm :: { Located RdrName }
1492 : qvarsym_no_minus { $1 }
1493 | '`' qvarid '`' { LL (unLoc $2) }
1495 -----------------------------------------------------------------------------
1498 tyvar :: { Located RdrName }
1499 tyvar : tyvarid { $1 }
1500 | '(' tyvarsym ')' { LL (unLoc $2) }
1502 tyvarop :: { Located RdrName }
1503 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1506 tyvarid :: { Located RdrName }
1507 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1508 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1509 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1510 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1511 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1513 tyvarsym :: { Located RdrName }
1514 -- Does not include "!", because that is used for strictness marks
1515 -- or ".", because that separates the quantified type vars from the rest
1516 -- or "*", because that's used for kinds
1517 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1519 -----------------------------------------------------------------------------
1522 var :: { Located RdrName }
1524 | '(' varsym ')' { LL (unLoc $2) }
1526 qvar :: { Located RdrName }
1528 | '(' varsym ')' { LL (unLoc $2) }
1529 | '(' qvarsym1 ')' { LL (unLoc $2) }
1530 -- We've inlined qvarsym here so that the decision about
1531 -- whether it's a qvar or a var can be postponed until
1532 -- *after* we see the close paren.
1534 qvarid :: { Located RdrName }
1536 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1538 varid :: { Located RdrName }
1539 : varid_no_unsafe { $1 }
1540 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1541 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1542 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1544 varid_no_unsafe :: { Located RdrName }
1545 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1546 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1547 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1548 | 'iso' { L1 $! mkUnqual varName FSLIT("iso") }
1549 | 'family' { L1 $! mkUnqual varName FSLIT("family") }
1551 qvarsym :: { Located RdrName }
1555 qvarsym_no_minus :: { Located RdrName }
1556 : varsym_no_minus { $1 }
1559 qvarsym1 :: { Located RdrName }
1560 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1562 varsym :: { Located RdrName }
1563 : varsym_no_minus { $1 }
1564 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1566 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1567 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1568 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1571 -- These special_ids are treated as keywords in various places,
1572 -- but as ordinary ids elsewhere. 'special_id' collects all these
1573 -- except 'unsafe', 'forall', 'family', and 'iso' whose treatment differs
1574 -- depending on context
1575 special_id :: { Located FastString }
1577 : 'as' { L1 FSLIT("as") }
1578 | 'qualified' { L1 FSLIT("qualified") }
1579 | 'hiding' { L1 FSLIT("hiding") }
1580 | 'for' { L1 FSLIT("for") }
1581 | 'export' { L1 FSLIT("export") }
1582 | 'label' { L1 FSLIT("label") }
1583 | 'dynamic' { L1 FSLIT("dynamic") }
1584 | 'stdcall' { L1 FSLIT("stdcall") }
1585 | 'ccall' { L1 FSLIT("ccall") }
1587 special_sym :: { Located FastString }
1588 special_sym : '!' { L1 FSLIT("!") }
1589 | '.' { L1 FSLIT(".") }
1590 | '*' { L1 FSLIT("*") }
1592 -----------------------------------------------------------------------------
1593 -- Data constructors
1595 qconid :: { Located RdrName } -- Qualified or unqualified
1597 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1599 conid :: { Located RdrName }
1600 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1602 qconsym :: { Located RdrName } -- Qualified or unqualified
1604 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1606 consym :: { Located RdrName }
1607 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1609 -- ':' means only list cons
1610 | ':' { L1 $ consDataCon_RDR }
1613 -----------------------------------------------------------------------------
1616 literal :: { Located HsLit }
1617 : CHAR { L1 $ HsChar $ getCHAR $1 }
1618 | STRING { L1 $ HsString $ getSTRING $1 }
1619 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1620 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1621 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1622 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1623 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1625 -----------------------------------------------------------------------------
1629 : vccurly { () } -- context popped in lexer.
1630 | error {% popContext }
1632 -----------------------------------------------------------------------------
1633 -- Miscellaneous (mostly renamings)
1635 modid :: { Located ModuleName }
1636 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1637 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1640 (unpackFS mod ++ '.':unpackFS c))
1644 : commas ',' { $1 + 1 }
1647 -----------------------------------------------------------------------------
1651 happyError = srcParseFail
1653 getVARID (L _ (ITvarid x)) = x
1654 getCONID (L _ (ITconid x)) = x
1655 getVARSYM (L _ (ITvarsym x)) = x
1656 getCONSYM (L _ (ITconsym x)) = x
1657 getQVARID (L _ (ITqvarid x)) = x
1658 getQCONID (L _ (ITqconid x)) = x
1659 getQVARSYM (L _ (ITqvarsym x)) = x
1660 getQCONSYM (L _ (ITqconsym x)) = x
1661 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1662 getCHAR (L _ (ITchar x)) = x
1663 getSTRING (L _ (ITstring x)) = x
1664 getINTEGER (L _ (ITinteger x)) = x
1665 getRATIONAL (L _ (ITrational x)) = x
1666 getPRIMCHAR (L _ (ITprimchar x)) = x
1667 getPRIMSTRING (L _ (ITprimstring x)) = x
1668 getPRIMINTEGER (L _ (ITprimint x)) = x
1669 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1670 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1671 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1672 getINLINE (L _ (ITinline_prag b)) = b
1673 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1675 -- Utilities for combining source spans
1676 comb2 :: Located a -> Located b -> SrcSpan
1679 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1680 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1682 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1683 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1684 combineSrcSpans (getLoc c) (getLoc d)
1686 -- strict constructor version:
1688 sL :: SrcSpan -> a -> Located a
1689 sL span a = span `seq` L span a
1691 -- Make a source location for the file. We're a bit lazy here and just
1692 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1693 -- try to find the span of the whole file (ToDo).
1694 fileSrcSpan :: P SrcSpan
1697 let loc = mkSrcLoc (srcLocFile l) 1 0;
1698 return (mkSrcSpan loc loc)