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) = cvBindsAndSigs (unLoc $3)
460 in unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs))) }
461 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
462 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
463 | '{-# DEPRECATED' deprecations '#-}' { $2 }
464 | '{-# RULES' rules '#-}' { $2 }
467 -- Template Haskell Extension
468 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
469 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
470 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
475 cl_decl :: { LTyClDecl RdrName }
476 : 'class' tycl_hdr fds where
477 {% do { let { (binds, sigs, ats) =
478 cvBindsAndSigs (unLoc $4)
479 ; (ctxt, tc, tvs, tparms) = unLoc $2}
480 ; checkTyVars tparms -- only type vars allowed
482 ; return $ L (comb4 $1 $2 $3 $4)
483 (mkClassDecl (ctxt, tc, tvs)
484 (unLoc $3) sigs binds ats) } }
486 -- Type declarations (toplevel)
488 ty_decl :: { LTyClDecl RdrName }
489 -- ordinary type synonyms
490 : 'type' type '=' ctype
491 -- Note ctype, not sigtype, on the right of '='
492 -- We allow an explicit for-all but we don't insert one
493 -- in type Foo a = (b,b)
494 -- Instead we just say b is out of scope
496 -- Note the use of type for the head; this allows
497 -- infix type constructors to be declared
498 {% do { (tc, tvs, _) <- checkSynHdr $2 False
499 ; return (L (comb2 $1 $4)
500 (TySynonym tc tvs Nothing $4))
503 -- type family declarations
504 | 'type' 'family' type opt_kind_sig
505 -- Note the use of type for the head; this allows
506 -- infix type constructors to be declared
508 {% do { (tc, tvs, _) <- checkSynHdr $3 False
509 ; let kind = case unLoc $4 of
510 Nothing -> liftedTypeKind
512 ; return (L (comb3 $1 $3 $4)
513 (TyFunction tc tvs False kind))
516 -- type instance declarations
517 | 'type' 'instance' type '=' ctype
518 -- Note the use of type for the head; this allows
519 -- infix type constructors and type patterns
521 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
522 ; return (L (comb2 $1 $5)
523 (TySynonym tc tvs (Just typats) $5))
526 -- ordinary data type or newtype declaration
527 | data_or_newtype tycl_hdr constrs deriving
528 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
529 ; checkTyVars tparms -- no type pattern
531 L (comb4 $1 $2 $3 $4)
532 -- We need the location on tycl_hdr in case
533 -- constrs and deriving are both empty
534 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
535 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
537 -- ordinary GADT declaration
538 | data_or_newtype tycl_hdr opt_kind_sig
539 'where' gadt_constrlist
541 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
542 ; checkTyVars tparms -- can have type pats
544 L (comb4 $1 $2 $4 $5)
545 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
546 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
548 -- data/newtype family
549 | data_or_newtype 'family' tycl_hdr opt_kind_sig
550 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
551 ; checkTyVars tparms -- no type pattern
552 ; let kind = case unLoc $4 of
553 Nothing -> liftedTypeKind
557 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
558 (Just kind) [] Nothing) } }
560 -- data/newtype instance declaration
561 | data_or_newtype 'instance' tycl_hdr constrs deriving
562 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
563 -- can have type pats
565 L (comb4 $1 $3 $4 $5)
566 -- We need the location on tycl_hdr in case
567 -- constrs and deriving are both empty
568 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
569 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
571 -- GADT instance declaration
572 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
573 'where' gadt_constrlist
575 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
576 -- can have type pats
578 L (comb4 $1 $3 $6 $7)
579 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
580 (unLoc $4) (reverse (unLoc $6)) (unLoc $7)) } }
582 -- Associate type declarations
584 at_decl :: { LTyClDecl RdrName }
585 -- type family declarations
586 : 'type' type opt_kind_sig
587 -- Note the use of type for the head; this allows
588 -- infix type constructors to be declared
590 {% do { (tc, tvs, _) <- checkSynHdr $2 False
591 ; let kind = case unLoc $3 of
592 Nothing -> liftedTypeKind
594 ; return (L (comb3 $1 $2 $3)
595 (TyFunction tc tvs False kind))
598 -- type instance declarations
599 | 'type' type '=' ctype
600 -- Note the use of type for the head; this allows
601 -- infix type constructors and type patterns
603 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
604 ; return (L (comb2 $1 $4)
605 (TySynonym tc tvs (Just typats) $4))
608 -- data/newtype family
609 | data_or_newtype tycl_hdr '::' kind
610 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
611 ; checkTyVars tparms -- no type pattern
614 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
615 (Just (unLoc $4)) [] Nothing) } }
617 -- data/newtype instance declaration
618 | data_or_newtype tycl_hdr constrs deriving
619 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
620 -- can have type pats
622 L (comb4 $1 $2 $3 $4)
623 -- We need the location on tycl_hdr in case
624 -- constrs and deriving are both empty
625 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
626 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
628 -- GADT instance declaration
629 | data_or_newtype tycl_hdr opt_kind_sig
630 'where' gadt_constrlist
632 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
633 -- can have type pats
635 L (comb4 $1 $2 $5 $6)
636 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
637 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
643 data_or_newtype :: { Located NewOrData }
644 : 'data' { L1 DataType }
645 | 'newtype' { L1 NewType }
647 opt_kind_sig :: { Located (Maybe Kind) }
649 | '::' kind { LL (Just (unLoc $2)) }
651 -- tycl_hdr parses the header of a class or data type decl,
652 -- which takes the form
655 -- (Eq a, Ord b) => T a b
656 -- T Int [a] -- for associated types
657 -- Rather a lot of inlining here, else we get reduce/reduce errors
658 tycl_hdr :: { Located (LHsContext RdrName,
660 [LHsTyVarBndr RdrName],
662 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
663 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
665 -----------------------------------------------------------------------------
666 -- Stand-alone deriving
668 -- Glasgow extension: stand-alone deriving declarations
669 stand_alone_deriving :: { LDerivDecl RdrName }
670 : 'deriving' qtycon 'for' qtycon {% do { p <- checkInstType (fmap HsTyVar $2)
671 ; checkDerivDecl (LL (DerivDecl p $4)) } }
673 | 'deriving' '(' inst_type ')' 'for' qtycon {% checkDerivDecl (LL (DerivDecl $3 $6)) }
675 -----------------------------------------------------------------------------
676 -- Nested declarations
678 -- Type declaration or value declaration
680 tydecl :: { Located (OrdList (LHsDecl RdrName)) }
681 tydecl : at_decl { LL (unitOL (L1 (TyClD (unLoc $1)))) }
684 tydecls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
685 : tydecls ';' tydecl { LL (unLoc $1 `appOL` unLoc $3) }
686 | tydecls ';' { LL (unLoc $1) }
688 | {- empty -} { noLoc nilOL }
692 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
693 : '{' tydecls '}' { LL (unLoc $2) }
694 | vocurly tydecls close { $2 }
696 -- Form of the body of class and instance declarations
698 where :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
699 -- No implicit parameters
700 -- May have type declarations
701 : 'where' tydecllist { LL (unLoc $2) }
702 | {- empty -} { noLoc nilOL }
704 decls :: { Located (OrdList (LHsDecl RdrName)) }
705 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
706 | decls ';' { LL (unLoc $1) }
708 | {- empty -} { noLoc nilOL }
711 decllist :: { Located (OrdList (LHsDecl RdrName)) }
712 : '{' decls '}' { LL (unLoc $2) }
713 | vocurly decls close { $2 }
715 -- Binding groups other than those of class and instance declarations
717 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
718 -- No type declarations
719 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
720 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
721 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
723 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
724 -- No type declarations
725 : 'where' binds { LL (unLoc $2) }
726 | {- empty -} { noLoc emptyLocalBinds }
729 -----------------------------------------------------------------------------
730 -- Transformation Rules
732 rules :: { OrdList (LHsDecl RdrName) }
733 : rules ';' rule { $1 `snocOL` $3 }
736 | {- empty -} { nilOL }
738 rule :: { LHsDecl RdrName }
739 : STRING activation rule_forall infixexp '=' exp
740 { LL $ RuleD (HsRule (getSTRING $1)
741 ($2 `orElse` AlwaysActive)
742 $3 $4 placeHolderNames $6 placeHolderNames) }
744 activation :: { Maybe Activation }
745 : {- empty -} { Nothing }
746 | explicit_activation { Just $1 }
748 explicit_activation :: { Activation } -- In brackets
749 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
750 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
752 rule_forall :: { [RuleBndr RdrName] }
753 : 'forall' rule_var_list '.' { $2 }
756 rule_var_list :: { [RuleBndr RdrName] }
758 | rule_var rule_var_list { $1 : $2 }
760 rule_var :: { RuleBndr RdrName }
761 : varid { RuleBndr $1 }
762 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
764 -----------------------------------------------------------------------------
765 -- Deprecations (c.f. rules)
767 deprecations :: { OrdList (LHsDecl RdrName) }
768 : deprecations ';' deprecation { $1 `appOL` $3 }
769 | deprecations ';' { $1 }
771 | {- empty -} { nilOL }
773 -- SUP: TEMPORARY HACK, not checking for `module Foo'
774 deprecation :: { OrdList (LHsDecl RdrName) }
776 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
780 -----------------------------------------------------------------------------
781 -- Foreign import and export declarations
783 fdecl :: { LHsDecl RdrName }
784 fdecl : 'import' callconv safety fspec
785 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
786 | 'import' callconv fspec
787 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
789 | 'export' callconv fspec
790 {% mkExport $2 (unLoc $3) >>= return.LL }
792 callconv :: { CallConv }
793 : 'stdcall' { CCall StdCallConv }
794 | 'ccall' { CCall CCallConv }
795 | 'dotnet' { DNCall }
798 : 'unsafe' { PlayRisky }
799 | 'safe' { PlaySafe False }
800 | 'threadsafe' { PlaySafe True }
802 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
803 : STRING var '::' sigtype { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
804 | var '::' sigtype { LL (noLoc nilFS, $1, $3) }
805 -- if the entity string is missing, it defaults to the empty string;
806 -- the meaning of an empty entity string depends on the calling
809 -----------------------------------------------------------------------------
812 opt_sig :: { Maybe (LHsType RdrName) }
813 : {- empty -} { Nothing }
814 | '::' sigtype { Just $2 }
816 opt_asig :: { Maybe (LHsType RdrName) }
817 : {- empty -} { Nothing }
818 | '::' atype { Just $2 }
820 sigtypes1 :: { [LHsType RdrName] }
822 | sigtype ',' sigtypes1 { $1 : $3 }
824 sigtype :: { LHsType RdrName }
825 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
826 -- Wrap an Implicit forall if there isn't one there already
828 sig_vars :: { Located [Located RdrName] }
829 : sig_vars ',' var { LL ($3 : unLoc $1) }
832 -----------------------------------------------------------------------------
835 strict_mark :: { Located HsBang }
836 : '!' { L1 HsStrict }
837 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
839 -- A ctype is a for-all type
840 ctype :: { LHsType RdrName }
841 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
842 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
843 -- A type of form (context => type) is an *implicit* HsForAllTy
846 -- We parse a context as a btype so that we don't get reduce/reduce
847 -- errors in ctype. The basic problem is that
849 -- looks so much like a tuple type. We can't tell until we find the =>
850 context :: { LHsContext RdrName }
851 : btype {% checkContext $1 }
853 type :: { LHsType RdrName }
854 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
857 gentype :: { LHsType RdrName }
859 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
860 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
861 | btype '->' ctype { LL $ HsFunTy $1 $3 }
863 btype :: { LHsType RdrName }
864 : btype atype { LL $ HsAppTy $1 $2 }
867 atype :: { LHsType RdrName }
868 : gtycon { L1 (HsTyVar (unLoc $1)) }
869 | tyvar { L1 (HsTyVar (unLoc $1)) }
870 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
871 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
872 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
873 | '[' ctype ']' { LL $ HsListTy $2 }
874 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
875 | '(' ctype ')' { LL $ HsParTy $2 }
876 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
878 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
880 -- An inst_type is what occurs in the head of an instance decl
881 -- e.g. (Foo a, Gaz b) => Wibble a b
882 -- It's kept as a single type, with a MonoDictTy at the right
883 -- hand corner, for convenience.
884 inst_type :: { LHsType RdrName }
885 : sigtype {% checkInstType $1 }
887 inst_types1 :: { [LHsType RdrName] }
889 | inst_type ',' inst_types1 { $1 : $3 }
891 comma_types0 :: { [LHsType RdrName] }
892 : comma_types1 { $1 }
895 comma_types1 :: { [LHsType RdrName] }
897 | ctype ',' comma_types1 { $1 : $3 }
899 tv_bndrs :: { [LHsTyVarBndr RdrName] }
900 : tv_bndr tv_bndrs { $1 : $2 }
903 tv_bndr :: { LHsTyVarBndr RdrName }
904 : tyvar { L1 (UserTyVar (unLoc $1)) }
905 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
908 fds :: { Located [Located ([RdrName], [RdrName])] }
909 : {- empty -} { noLoc [] }
910 | '|' fds1 { LL (reverse (unLoc $2)) }
912 fds1 :: { Located [Located ([RdrName], [RdrName])] }
913 : fds1 ',' fd { LL ($3 : unLoc $1) }
916 fd :: { Located ([RdrName], [RdrName]) }
917 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
918 (reverse (unLoc $1), reverse (unLoc $3)) }
920 varids0 :: { Located [RdrName] }
921 : {- empty -} { noLoc [] }
922 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
924 -----------------------------------------------------------------------------
927 kind :: { Located Kind }
929 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
931 akind :: { Located Kind }
932 : '*' { L1 liftedTypeKind }
933 | '!' { L1 unliftedTypeKind }
934 | '(' kind ')' { LL (unLoc $2) }
937 -----------------------------------------------------------------------------
938 -- Datatype declarations
940 gadt_constrlist :: { Located [LConDecl RdrName] }
941 : '{' gadt_constrs '}' { LL (unLoc $2) }
942 | vocurly gadt_constrs close { $2 }
944 gadt_constrs :: { Located [LConDecl RdrName] }
945 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
946 | gadt_constrs ';' { $1 }
947 | gadt_constr { L1 [$1] }
949 -- We allow the following forms:
950 -- C :: Eq a => a -> T a
951 -- C :: forall a. Eq a => !a -> T a
952 -- D { x,y :: a } :: T a
953 -- forall a. Eq a => D { x,y :: a } :: T a
955 gadt_constr :: { LConDecl RdrName }
957 { LL (mkGadtDecl $1 $3) }
958 -- Syntax: Maybe merge the record stuff with the single-case above?
959 -- (to kill the mostly harmless reduce/reduce error)
960 -- XXX revisit audreyt
961 | constr_stuff_record '::' sigtype
962 { let (con,details) = unLoc $1 in
963 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
965 | forall context '=>' constr_stuff_record '::' sigtype
966 { let (con,details) = unLoc $4 in
967 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
968 | forall constr_stuff_record '::' sigtype
969 { let (con,details) = unLoc $2 in
970 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
974 constrs :: { Located [LConDecl RdrName] }
975 : {- empty; a GHC extension -} { noLoc [] }
976 | '=' constrs1 { LL (unLoc $2) }
978 constrs1 :: { Located [LConDecl RdrName] }
979 : constrs1 '|' constr { LL ($3 : unLoc $1) }
982 constr :: { LConDecl RdrName }
983 : forall context '=>' constr_stuff
984 { let (con,details) = unLoc $4 in
985 LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
986 | forall constr_stuff
987 { let (con,details) = unLoc $2 in
988 LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
990 forall :: { Located [LHsTyVarBndr RdrName] }
991 : 'forall' tv_bndrs '.' { LL $2 }
992 | {- empty -} { noLoc [] }
994 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
995 -- We parse the constructor declaration
997 -- as a btype (treating C as a type constructor) and then convert C to be
998 -- a data constructor. Reason: it might continue like this:
1000 -- in which case C really would be a type constructor. We can't resolve this
1001 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1002 : btype {% mkPrefixCon $1 [] >>= return.LL }
1003 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
1004 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
1005 | btype conop btype { LL ($2, InfixCon $1 $3) }
1007 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
1008 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
1009 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
1011 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
1012 : fielddecl ',' fielddecls { unLoc $1 : $3 }
1013 | fielddecl { [unLoc $1] }
1015 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
1016 : sig_vars '::' ctype { LL (reverse (unLoc $1), $3) }
1018 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1019 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1020 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1021 -- We don't allow a context, but that's sorted out by the type checker.
1022 deriving :: { Located (Maybe [LHsType RdrName]) }
1023 : {- empty -} { noLoc Nothing }
1024 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1025 ; p <- checkInstType (L loc (HsTyVar tv))
1026 ; return (LL (Just [p])) } }
1027 | 'deriving' '(' ')' { LL (Just []) }
1028 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1029 -- Glasgow extension: allow partial
1030 -- applications in derivings
1032 -----------------------------------------------------------------------------
1033 -- Value definitions
1035 {- There's an awkward overlap with a type signature. Consider
1036 f :: Int -> Int = ...rhs...
1037 Then we can't tell whether it's a type signature or a value
1038 definition with a result signature until we see the '='.
1039 So we have to inline enough to postpone reductions until we know.
1043 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1044 instead of qvar, we get another shift/reduce-conflict. Consider the
1047 { (^^) :: Int->Int ; } Type signature; only var allowed
1049 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1050 qvar allowed (because of instance decls)
1052 We can't tell whether to reduce var to qvar until after we've read the signatures.
1055 decl :: { Located (OrdList (LHsDecl RdrName)) }
1057 | '!' infixexp rhs {% do { pat <- checkPattern $2;
1058 return (LL $ unitOL $ LL $ ValD $
1059 PatBind (LL $ BangPat pat) (unLoc $3)
1060 placeHolderType placeHolderNames) } }
1061 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1062 return (LL $ unitOL (LL $ ValD r)) } }
1064 rhs :: { Located (GRHSs RdrName) }
1065 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1066 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1068 gdrhs :: { Located [LGRHS RdrName] }
1069 : gdrhs gdrh { LL ($2 : unLoc $1) }
1072 gdrh :: { LGRHS RdrName }
1073 : '|' quals '=' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1075 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1076 : infixexp '::' sigtype
1077 {% do s <- checkValSig $1 $3;
1078 return (LL $ unitOL (LL $ SigD s)) }
1079 -- See the above notes for why we need infixexp here
1080 | var ',' sig_vars '::' sigtype
1081 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1082 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1084 | '{-# INLINE' activation qvar '#-}'
1085 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1086 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1087 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1089 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1090 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1092 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1093 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1095 -----------------------------------------------------------------------------
1098 exp :: { LHsExpr RdrName }
1099 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1100 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1101 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1102 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1103 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1106 infixexp :: { LHsExpr RdrName }
1108 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1110 exp10 :: { LHsExpr RdrName }
1111 : '\\' aexp aexps opt_asig '->' exp
1112 {% checkPatterns ($2 : reverse $3) >>= \ ps ->
1113 return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1114 (GRHSs (unguardedRHS $6) emptyLocalBinds
1116 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1117 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1118 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1119 | '-' fexp { LL $ mkHsNegApp $2 }
1121 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1122 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1123 return (L loc (mkHsDo DoExpr stmts body)) }
1124 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1125 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1126 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1127 | scc_annot exp { LL $ if opt_SccProfilingOn
1128 then HsSCC (unLoc $1) $2
1131 | 'proc' aexp '->' exp
1132 {% checkPattern $2 >>= \ p ->
1133 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1134 placeHolderType undefined)) }
1135 -- TODO: is LL right here?
1137 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1138 -- hdaume: core annotation
1141 scc_annot :: { Located FastString }
1142 : '_scc_' STRING { LL $ getSTRING $2 }
1143 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
1145 fexp :: { LHsExpr RdrName }
1146 : fexp aexp { LL $ HsApp $1 $2 }
1149 aexps :: { [LHsExpr RdrName] }
1150 : aexps aexp { $2 : $1 }
1151 | {- empty -} { [] }
1153 aexp :: { LHsExpr RdrName }
1154 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1155 | '~' aexp { LL $ ELazyPat $2 }
1156 -- | '!' aexp { LL $ EBangPat $2 }
1159 aexp1 :: { LHsExpr RdrName }
1160 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1165 -- Here was the syntax for type applications that I was planning
1166 -- but there are difficulties (e.g. what order for type args)
1167 -- so it's not enabled yet.
1168 -- But this case *is* used for the left hand side of a generic definition,
1169 -- which is parsed as an expression before being munged into a pattern
1170 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1171 (sL (getLoc $3) (HsType $3)) }
1173 aexp2 :: { LHsExpr RdrName }
1174 : ipvar { L1 (HsIPVar $! unLoc $1) }
1175 | qcname { L1 (HsVar $! unLoc $1) }
1176 | literal { L1 (HsLit $! unLoc $1) }
1177 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1178 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1179 | '(' exp ')' { LL (HsPar $2) }
1180 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1181 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1182 | '[' list ']' { LL (unLoc $2) }
1183 | '[:' parr ':]' { LL (unLoc $2) }
1184 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1185 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1186 | '_' { L1 EWildPat }
1188 -- Template Haskell Extension
1189 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1190 (L1 $ HsVar (mkUnqual varName
1191 (getTH_ID_SPLICE $1)))) } -- $x
1192 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1194 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1195 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1196 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1197 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1198 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1199 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1200 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1201 return (LL $ HsBracket (PatBr p)) }
1202 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBr (mkGroup $2)) }
1204 -- arrow notation extension
1205 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1207 cmdargs :: { [LHsCmdTop RdrName] }
1208 : cmdargs acmd { $2 : $1 }
1209 | {- empty -} { [] }
1211 acmd :: { LHsCmdTop RdrName }
1212 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1214 cvtopbody :: { [LHsDecl RdrName] }
1215 : '{' cvtopdecls0 '}' { $2 }
1216 | vocurly cvtopdecls0 close { $2 }
1218 cvtopdecls0 :: { [LHsDecl RdrName] }
1219 : {- empty -} { [] }
1222 texp :: { LHsExpr RdrName }
1224 | qopm infixexp { LL $ SectionR $1 $2 }
1225 -- The second production is really here only for bang patterns
1228 texps :: { [LHsExpr RdrName] }
1229 : texps ',' texp { $3 : $1 }
1233 -----------------------------------------------------------------------------
1236 -- The rules below are little bit contorted to keep lexps left-recursive while
1237 -- avoiding another shift/reduce-conflict.
1239 list :: { LHsExpr RdrName }
1240 : texp { L1 $ ExplicitList placeHolderType [$1] }
1241 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1242 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1243 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1244 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1245 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1246 | texp pquals { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1248 lexps :: { Located [LHsExpr RdrName] }
1249 : lexps ',' texp { LL ($3 : unLoc $1) }
1250 | texp ',' texp { LL [$3,$1] }
1252 -----------------------------------------------------------------------------
1253 -- List Comprehensions
1255 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1256 -- or a reversed list of Stmts
1257 : pquals1 { case unLoc $1 of
1259 qss -> L1 [L1 (ParStmt stmtss)]
1261 stmtss = [ (reverse qs, undefined)
1265 pquals1 :: { Located [[LStmt RdrName]] }
1266 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1267 | '|' quals { L (getLoc $2) [unLoc $2] }
1269 quals :: { Located [LStmt RdrName] }
1270 : quals ',' qual { LL ($3 : unLoc $1) }
1273 -----------------------------------------------------------------------------
1274 -- Parallel array expressions
1276 -- The rules below are little bit contorted; see the list case for details.
1277 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1278 -- Moreover, we allow explicit arrays with no element (represented by the nil
1279 -- constructor in the list case).
1281 parr :: { LHsExpr RdrName }
1282 : { noLoc (ExplicitPArr placeHolderType []) }
1283 | exp { L1 $ ExplicitPArr placeHolderType [$1] }
1284 | lexps { L1 $ ExplicitPArr placeHolderType
1285 (reverse (unLoc $1)) }
1286 | exp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1287 | exp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1288 | exp pquals { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1290 -- We are reusing `lexps' and `pquals' from the list case.
1292 -----------------------------------------------------------------------------
1293 -- Case alternatives
1295 altslist :: { Located [LMatch RdrName] }
1296 : '{' alts '}' { LL (reverse (unLoc $2)) }
1297 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1299 alts :: { Located [LMatch RdrName] }
1300 : alts1 { L1 (unLoc $1) }
1301 | ';' alts { LL (unLoc $2) }
1303 alts1 :: { Located [LMatch RdrName] }
1304 : alts1 ';' alt { LL ($3 : unLoc $1) }
1305 | alts1 ';' { LL (unLoc $1) }
1308 alt :: { LMatch RdrName }
1309 : infixexp opt_sig alt_rhs {% checkPattern $1 >>= \p ->
1310 return (LL (Match [p] $2 (unLoc $3))) }
1311 | '!' infixexp opt_sig alt_rhs {% checkPattern $2 >>= \p ->
1312 return (LL (Match [LL $ BangPat p] $3 (unLoc $4))) }
1314 alt_rhs :: { Located (GRHSs RdrName) }
1315 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1317 ralt :: { Located [LGRHS RdrName] }
1318 : '->' exp { LL (unguardedRHS $2) }
1319 | gdpats { L1 (reverse (unLoc $1)) }
1321 gdpats :: { Located [LGRHS RdrName] }
1322 : gdpats gdpat { LL ($2 : unLoc $1) }
1325 gdpat :: { LGRHS RdrName }
1326 : '|' quals '->' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1328 -----------------------------------------------------------------------------
1329 -- Statement sequences
1331 stmtlist :: { Located [LStmt RdrName] }
1332 : '{' stmts '}' { LL (unLoc $2) }
1333 | vocurly stmts close { $2 }
1335 -- do { ;; s ; s ; ; s ;; }
1336 -- The last Stmt should be an expression, but that's hard to enforce
1337 -- here, because we need too much lookahead if we see do { e ; }
1338 -- So we use ExprStmts throughout, and switch the last one over
1339 -- in ParseUtils.checkDo instead
1340 stmts :: { Located [LStmt RdrName] }
1341 : stmt stmts_help { LL ($1 : unLoc $2) }
1342 | ';' stmts { LL (unLoc $2) }
1343 | {- empty -} { noLoc [] }
1345 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1346 : ';' stmts { LL (unLoc $2) }
1347 | {- empty -} { noLoc [] }
1349 -- For typing stmts at the GHCi prompt, where
1350 -- the input may consist of just comments.
1351 maybe_stmt :: { Maybe (LStmt RdrName) }
1353 | {- nothing -} { Nothing }
1355 stmt :: { LStmt RdrName }
1357 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1358 return (LL $ mkBindStmt p $1) }
1359 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1361 qual :: { LStmt RdrName }
1362 : exp '<-' exp {% checkPattern $1 >>= \p ->
1363 return (LL $ mkBindStmt p $3) }
1364 | exp { L1 $ mkExprStmt $1 }
1365 | 'let' binds { LL $ LetStmt (unLoc $2) }
1367 -----------------------------------------------------------------------------
1368 -- Record Field Update/Construction
1370 fbinds :: { HsRecordBinds RdrName }
1372 | {- empty -} { [] }
1374 fbinds1 :: { HsRecordBinds RdrName }
1375 : fbinds1 ',' fbind { $3 : $1 }
1378 fbind :: { (Located RdrName, LHsExpr RdrName) }
1379 : qvar '=' exp { ($1,$3) }
1381 -----------------------------------------------------------------------------
1382 -- Implicit Parameter Bindings
1384 dbinds :: { Located [LIPBind RdrName] }
1385 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1386 | dbinds ';' { LL (unLoc $1) }
1388 -- | {- empty -} { [] }
1390 dbind :: { LIPBind RdrName }
1391 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1393 ipvar :: { Located (IPName RdrName) }
1394 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1396 -----------------------------------------------------------------------------
1399 depreclist :: { Located [RdrName] }
1400 depreclist : deprec_var { L1 [unLoc $1] }
1401 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1403 deprec_var :: { Located RdrName }
1404 deprec_var : var { $1 }
1407 -----------------------------------------
1408 -- Data constructors
1409 qcon :: { Located RdrName }
1411 | '(' qconsym ')' { LL (unLoc $2) }
1412 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1413 -- The case of '[:' ':]' is part of the production `parr'
1415 con :: { Located RdrName }
1417 | '(' consym ')' { LL (unLoc $2) }
1418 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1420 sysdcon :: { Located DataCon } -- Wired in data constructors
1421 : '(' ')' { LL unitDataCon }
1422 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1423 | '[' ']' { LL nilDataCon }
1425 conop :: { Located RdrName }
1427 | '`' conid '`' { LL (unLoc $2) }
1429 qconop :: { Located RdrName }
1431 | '`' qconid '`' { LL (unLoc $2) }
1433 -----------------------------------------------------------------------------
1434 -- Type constructors
1436 gtycon :: { Located RdrName } -- A "general" qualified tycon
1438 | '(' ')' { LL $ getRdrName unitTyCon }
1439 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1440 | '(' '->' ')' { LL $ getRdrName funTyCon }
1441 | '[' ']' { LL $ listTyCon_RDR }
1442 | '[:' ':]' { LL $ parrTyCon_RDR }
1444 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1446 | '(' qtyconsym ')' { LL (unLoc $2) }
1448 qtyconop :: { Located RdrName } -- Qualified or unqualified
1450 | '`' qtycon '`' { LL (unLoc $2) }
1452 qtycon :: { Located RdrName } -- Qualified or unqualified
1453 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1456 tycon :: { Located RdrName } -- Unqualified
1457 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1459 qtyconsym :: { Located RdrName }
1460 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1463 tyconsym :: { Located RdrName }
1464 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1466 -----------------------------------------------------------------------------
1469 op :: { Located RdrName } -- used in infix decls
1473 varop :: { Located RdrName }
1475 | '`' varid '`' { LL (unLoc $2) }
1477 qop :: { LHsExpr RdrName } -- used in sections
1478 : qvarop { L1 $ HsVar (unLoc $1) }
1479 | qconop { L1 $ HsVar (unLoc $1) }
1481 qopm :: { LHsExpr RdrName } -- used in sections
1482 : qvaropm { L1 $ HsVar (unLoc $1) }
1483 | qconop { L1 $ HsVar (unLoc $1) }
1485 qvarop :: { Located RdrName }
1487 | '`' qvarid '`' { LL (unLoc $2) }
1489 qvaropm :: { Located RdrName }
1490 : qvarsym_no_minus { $1 }
1491 | '`' qvarid '`' { LL (unLoc $2) }
1493 -----------------------------------------------------------------------------
1496 tyvar :: { Located RdrName }
1497 tyvar : tyvarid { $1 }
1498 | '(' tyvarsym ')' { LL (unLoc $2) }
1500 tyvarop :: { Located RdrName }
1501 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1504 tyvarid :: { Located RdrName }
1505 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1506 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1507 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1508 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1509 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1511 tyvarsym :: { Located RdrName }
1512 -- Does not include "!", because that is used for strictness marks
1513 -- or ".", because that separates the quantified type vars from the rest
1514 -- or "*", because that's used for kinds
1515 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1517 -----------------------------------------------------------------------------
1520 var :: { Located RdrName }
1522 | '(' varsym ')' { LL (unLoc $2) }
1524 qvar :: { Located RdrName }
1526 | '(' varsym ')' { LL (unLoc $2) }
1527 | '(' qvarsym1 ')' { LL (unLoc $2) }
1528 -- We've inlined qvarsym here so that the decision about
1529 -- whether it's a qvar or a var can be postponed until
1530 -- *after* we see the close paren.
1532 qvarid :: { Located RdrName }
1534 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1536 varid :: { Located RdrName }
1537 : varid_no_unsafe { $1 }
1538 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1539 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1540 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1542 varid_no_unsafe :: { Located RdrName }
1543 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1544 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1545 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1546 | 'iso' { L1 $! mkUnqual varName FSLIT("iso") }
1547 | 'family' { L1 $! mkUnqual varName FSLIT("family") }
1549 qvarsym :: { Located RdrName }
1553 qvarsym_no_minus :: { Located RdrName }
1554 : varsym_no_minus { $1 }
1557 qvarsym1 :: { Located RdrName }
1558 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1560 varsym :: { Located RdrName }
1561 : varsym_no_minus { $1 }
1562 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1564 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1565 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1566 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1569 -- These special_ids are treated as keywords in various places,
1570 -- but as ordinary ids elsewhere. 'special_id' collects all these
1571 -- except 'unsafe', 'forall', 'family', and 'iso' whose treatment differs
1572 -- depending on context
1573 special_id :: { Located FastString }
1575 : 'as' { L1 FSLIT("as") }
1576 | 'qualified' { L1 FSLIT("qualified") }
1577 | 'hiding' { L1 FSLIT("hiding") }
1578 | 'export' { L1 FSLIT("export") }
1579 | 'label' { L1 FSLIT("label") }
1580 | 'dynamic' { L1 FSLIT("dynamic") }
1581 | 'stdcall' { L1 FSLIT("stdcall") }
1582 | 'ccall' { L1 FSLIT("ccall") }
1584 special_sym :: { Located FastString }
1585 special_sym : '!' { L1 FSLIT("!") }
1586 | '.' { L1 FSLIT(".") }
1587 | '*' { L1 FSLIT("*") }
1589 -----------------------------------------------------------------------------
1590 -- Data constructors
1592 qconid :: { Located RdrName } -- Qualified or unqualified
1594 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1596 conid :: { Located RdrName }
1597 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1599 qconsym :: { Located RdrName } -- Qualified or unqualified
1601 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1603 consym :: { Located RdrName }
1604 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1606 -- ':' means only list cons
1607 | ':' { L1 $ consDataCon_RDR }
1610 -----------------------------------------------------------------------------
1613 literal :: { Located HsLit }
1614 : CHAR { L1 $ HsChar $ getCHAR $1 }
1615 | STRING { L1 $ HsString $ getSTRING $1 }
1616 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1617 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1618 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1619 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1620 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1622 -----------------------------------------------------------------------------
1626 : vccurly { () } -- context popped in lexer.
1627 | error {% popContext }
1629 -----------------------------------------------------------------------------
1630 -- Miscellaneous (mostly renamings)
1632 modid :: { Located ModuleName }
1633 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1634 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1637 (unpackFS mod ++ '.':unpackFS c))
1641 : commas ',' { $1 + 1 }
1644 -----------------------------------------------------------------------------
1648 happyError = srcParseFail
1650 getVARID (L _ (ITvarid x)) = x
1651 getCONID (L _ (ITconid x)) = x
1652 getVARSYM (L _ (ITvarsym x)) = x
1653 getCONSYM (L _ (ITconsym x)) = x
1654 getQVARID (L _ (ITqvarid x)) = x
1655 getQCONID (L _ (ITqconid x)) = x
1656 getQVARSYM (L _ (ITqvarsym x)) = x
1657 getQCONSYM (L _ (ITqconsym x)) = x
1658 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1659 getCHAR (L _ (ITchar x)) = x
1660 getSTRING (L _ (ITstring x)) = x
1661 getINTEGER (L _ (ITinteger x)) = x
1662 getRATIONAL (L _ (ITrational x)) = x
1663 getPRIMCHAR (L _ (ITprimchar x)) = x
1664 getPRIMSTRING (L _ (ITprimstring x)) = x
1665 getPRIMINTEGER (L _ (ITprimint x)) = x
1666 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1667 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1668 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1669 getINLINE (L _ (ITinline_prag b)) = b
1670 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1672 -- Utilities for combining source spans
1673 comb2 :: Located a -> Located b -> SrcSpan
1676 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1677 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1679 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1680 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1681 combineSrcSpans (getLoc c) (getLoc d)
1683 -- strict constructor version:
1685 sL :: SrcSpan -> a -> Located a
1686 sL span a = span `seq` L span a
1688 -- Make a source location for the file. We're a bit lazy here and just
1689 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1690 -- try to find the span of the whole file (ToDo).
1691 fileSrcSpan :: P SrcSpan
1694 let loc = mkSrcLoc (srcLocFile l) 1 0;
1695 return (mkSrcSpan loc loc)