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 }
162 'hiding' { L _ IThiding }
164 'import' { L _ ITimport }
166 'infix' { L _ ITinfix }
167 'infixl' { L _ ITinfixl }
168 'infixr' { L _ ITinfixr }
169 'instance' { L _ ITinstance }
171 'module' { L _ ITmodule }
172 'newtype' { L _ ITnewtype }
174 'qualified' { L _ ITqualified }
175 'then' { L _ ITthen }
176 'type' { L _ ITtype }
177 'where' { L _ ITwhere }
178 '_scc_' { L _ ITscc } -- ToDo: remove
180 'forall' { L _ ITforall } -- GHC extension keywords
181 'foreign' { L _ ITforeign }
182 'export' { L _ ITexport }
183 'label' { L _ ITlabel }
184 'dynamic' { L _ ITdynamic }
185 'safe' { L _ ITsafe }
186 'threadsafe' { L _ ITthreadsafe }
187 'unsafe' { L _ ITunsafe }
190 'family' { L _ ITfamily }
191 'stdcall' { L _ ITstdcallconv }
192 'ccall' { L _ ITccallconv }
193 'dotnet' { L _ ITdotnet }
194 'proc' { L _ ITproc } -- for arrow notation extension
195 'rec' { L _ ITrec } -- for arrow notation extension
197 '{-# INLINE' { L _ (ITinline_prag _) }
198 '{-# SPECIALISE' { L _ ITspec_prag }
199 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
200 '{-# SOURCE' { L _ ITsource_prag }
201 '{-# RULES' { L _ ITrules_prag }
202 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
203 '{-# SCC' { L _ ITscc_prag }
204 '{-# DEPRECATED' { L _ ITdeprecated_prag }
205 '{-# UNPACK' { L _ ITunpack_prag }
206 '#-}' { L _ ITclose_prag }
208 '..' { L _ ITdotdot } -- reserved symbols
210 '::' { L _ ITdcolon }
214 '<-' { L _ ITlarrow }
215 '->' { L _ ITrarrow }
218 '=>' { L _ ITdarrow }
222 '-<' { L _ ITlarrowtail } -- for arrow notation
223 '>-' { L _ ITrarrowtail } -- for arrow notation
224 '-<<' { L _ ITLarrowtail } -- for arrow notation
225 '>>-' { L _ ITRarrowtail } -- for arrow notation
228 '{' { L _ ITocurly } -- special symbols
230 '{|' { L _ ITocurlybar }
231 '|}' { L _ ITccurlybar }
232 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
233 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
236 '[:' { L _ ITopabrack }
237 ':]' { L _ ITcpabrack }
240 '(#' { L _ IToubxparen }
241 '#)' { L _ ITcubxparen }
242 '(|' { L _ IToparenbar }
243 '|)' { L _ ITcparenbar }
246 '`' { L _ ITbackquote }
248 VARID { L _ (ITvarid _) } -- identifiers
249 CONID { L _ (ITconid _) }
250 VARSYM { L _ (ITvarsym _) }
251 CONSYM { L _ (ITconsym _) }
252 QVARID { L _ (ITqvarid _) }
253 QCONID { L _ (ITqconid _) }
254 QVARSYM { L _ (ITqvarsym _) }
255 QCONSYM { L _ (ITqconsym _) }
257 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
258 IPSPLITVARID { L _ (ITsplitipvarid _) } -- 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 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
463 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
464 | '{-# DEPRECATED' deprecations '#-}' { $2 }
465 | '{-# RULES' rules '#-}' { $2 }
468 -- Template Haskell Extension
469 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
470 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
471 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
476 cl_decl :: { LTyClDecl RdrName }
477 : 'class' tycl_hdr fds where
478 {% do { let { (binds, sigs, ats) =
479 cvBindsAndSigs (unLoc $4)
480 ; (ctxt, tc, tvs, tparms) = unLoc $2}
481 ; checkTyVars tparms -- only type vars allowed
483 ; return $ L (comb4 $1 $2 $3 $4)
484 (mkClassDecl (ctxt, tc, tvs)
485 (unLoc $3) sigs binds ats) } }
487 -- Type declarations (toplevel)
489 ty_decl :: { LTyClDecl RdrName }
490 -- ordinary type synonyms
491 : 'type' type '=' ctype
492 -- Note ctype, not sigtype, on the right of '='
493 -- We allow an explicit for-all but we don't insert one
494 -- in type Foo a = (b,b)
495 -- Instead we just say b is out of scope
497 -- Note the use of type for the head; this allows
498 -- infix type constructors to be declared
499 {% do { (tc, tvs, _) <- checkSynHdr $2 False
500 ; return (L (comb2 $1 $4)
501 (TySynonym tc tvs Nothing $4))
504 -- type family declarations
505 | 'type' 'family' opt_iso type '::' kind
506 -- Note the use of type for the head; this allows
507 -- infix type constructors to be declared
509 {% do { (tc, tvs, _) <- checkSynHdr $4 False
510 ; return (L (comb3 $1 $4 $6)
511 (TyFunction tc tvs $3 (unLoc $6)))
514 -- type instance declarations
515 | 'type' 'instance' type '=' ctype
516 -- Note the use of type for the head; this allows
517 -- infix type constructors and type patterns
519 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
520 ; return (L (comb2 $1 $5)
521 (TySynonym tc tvs (Just typats) $5))
524 -- ordinary data type or newtype declaration
525 | data_or_newtype tycl_hdr constrs deriving
526 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
527 ; checkTyVars tparms -- no type pattern
529 L (comb4 $1 $2 $3 $4)
530 -- We need the location on tycl_hdr in case
531 -- constrs and deriving are both empty
532 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
533 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
535 -- ordinary GADT declaration
536 | data_or_newtype tycl_hdr opt_kind_sig
537 'where' gadt_constrlist
539 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
540 ; checkTyVars tparms -- can have type pats
542 L (comb4 $1 $2 $4 $5)
543 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing) $3
544 (reverse (unLoc $5)) (unLoc $6)) } }
546 -- data/newtype family
547 | data_or_newtype 'family' tycl_hdr '::' kind
548 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
549 ; checkTyVars tparms -- no type pattern
552 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
553 (Just (unLoc $5)) [] Nothing) } }
555 -- data/newtype instance declaration
556 | data_or_newtype 'instance' tycl_hdr constrs deriving
557 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
558 -- can have type pats
560 L (comb4 $1 $3 $4 $5)
561 -- We need the location on tycl_hdr in case
562 -- constrs and deriving are both empty
563 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
564 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
566 -- GADT instance declaration
567 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
568 'where' gadt_constrlist
570 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
571 -- can have type pats
573 L (comb4 $1 $3 $6 $7)
574 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
575 $4 (reverse (unLoc $6)) (unLoc $7)) } }
577 -- Associate type declarations
579 at_decl :: { LTyClDecl RdrName }
580 -- type family declarations
581 : 'type' opt_iso type '::' kind
582 -- Note the use of type for the head; this allows
583 -- infix type constructors to be declared
585 {% do { (tc, tvs, _) <- checkSynHdr $3 False
586 ; return (L (comb3 $1 $3 $5)
587 (TyFunction tc tvs $2 (unLoc $5)))
590 -- type instance declarations
591 | 'type' opt_iso type '=' ctype
592 -- Note the use of type for the head; this allows
593 -- infix type constructors and type patterns
596 parseError (comb2 $1 $>) "Misplaced iso keyword"
597 ; (tc, tvs, typats) <- checkSynHdr $3 True
598 ; return (L (comb2 $1 $5)
599 (TySynonym tc tvs (Just typats) $5))
602 -- data/newtype family
603 | data_or_newtype tycl_hdr '::' kind
604 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
605 ; checkTyVars tparms -- no type pattern
608 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
609 (Just (unLoc $4)) [] Nothing) } }
611 -- data/newtype instance declaration
612 | data_or_newtype tycl_hdr constrs deriving
613 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
614 -- can have type pats
616 L (comb4 $1 $2 $3 $4)
617 -- We need the location on tycl_hdr in case
618 -- constrs and deriving are both empty
619 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
620 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
622 -- GADT instance declaration
623 | data_or_newtype tycl_hdr opt_kind_sig
624 'where' gadt_constrlist
626 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
627 -- can have type pats
629 L (comb4 $1 $2 $5 $6)
630 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
631 $3 (reverse (unLoc $5)) (unLoc $6)) } }
637 data_or_newtype :: { Located NewOrData }
638 : 'data' { L1 DataType }
639 | 'newtype' { L1 NewType }
641 opt_kind_sig :: { Maybe Kind }
643 | '::' kind { Just (unLoc $2) }
645 -- tycl_hdr parses the header of a class or data type decl,
646 -- which takes the form
649 -- (Eq a, Ord b) => T a b
650 -- T Int [a] -- for associated types
651 -- Rather a lot of inlining here, else we get reduce/reduce errors
652 tycl_hdr :: { Located (LHsContext RdrName,
654 [LHsTyVarBndr RdrName],
656 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
657 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
659 -----------------------------------------------------------------------------
660 -- Nested declarations
662 -- Type declaration or value declaration
664 tydecl :: { Located (OrdList (LHsDecl RdrName)) }
665 tydecl : at_decl { LL (unitOL (L1 (TyClD (unLoc $1)))) }
668 tydecls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
669 : tydecls ';' tydecl { LL (unLoc $1 `appOL` unLoc $3) }
670 | tydecls ';' { LL (unLoc $1) }
672 | {- empty -} { noLoc nilOL }
676 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
677 : '{' tydecls '}' { LL (unLoc $2) }
678 | vocurly tydecls close { $2 }
680 -- Form of the body of class and instance declarations
682 where :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
683 -- No implicit parameters
684 -- May have type declarations
685 : 'where' tydecllist { LL (unLoc $2) }
686 | {- empty -} { noLoc nilOL }
688 decls :: { Located (OrdList (LHsDecl RdrName)) }
689 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
690 | decls ';' { LL (unLoc $1) }
692 | {- empty -} { noLoc nilOL }
695 decllist :: { Located (OrdList (LHsDecl RdrName)) }
696 : '{' decls '}' { LL (unLoc $2) }
697 | vocurly decls close { $2 }
699 -- Binding groups other than those of class and instance declarations
701 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
702 -- No type declarations
703 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
704 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
705 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
707 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
708 -- No type declarations
709 : 'where' binds { LL (unLoc $2) }
710 | {- empty -} { noLoc emptyLocalBinds }
713 -----------------------------------------------------------------------------
714 -- Transformation Rules
716 rules :: { OrdList (LHsDecl RdrName) }
717 : rules ';' rule { $1 `snocOL` $3 }
720 | {- empty -} { nilOL }
722 rule :: { LHsDecl RdrName }
723 : STRING activation rule_forall infixexp '=' exp
724 { LL $ RuleD (HsRule (getSTRING $1)
725 ($2 `orElse` AlwaysActive)
726 $3 $4 placeHolderNames $6 placeHolderNames) }
728 activation :: { Maybe Activation }
729 : {- empty -} { Nothing }
730 | explicit_activation { Just $1 }
732 explicit_activation :: { Activation } -- In brackets
733 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
734 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
736 rule_forall :: { [RuleBndr RdrName] }
737 : 'forall' rule_var_list '.' { $2 }
740 rule_var_list :: { [RuleBndr RdrName] }
742 | rule_var rule_var_list { $1 : $2 }
744 rule_var :: { RuleBndr RdrName }
745 : varid { RuleBndr $1 }
746 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
748 -----------------------------------------------------------------------------
749 -- Deprecations (c.f. rules)
751 deprecations :: { OrdList (LHsDecl RdrName) }
752 : deprecations ';' deprecation { $1 `appOL` $3 }
753 | deprecations ';' { $1 }
755 | {- empty -} { nilOL }
757 -- SUP: TEMPORARY HACK, not checking for `module Foo'
758 deprecation :: { OrdList (LHsDecl RdrName) }
760 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
764 -----------------------------------------------------------------------------
765 -- Foreign import and export declarations
767 fdecl :: { LHsDecl RdrName }
768 fdecl : 'import' callconv safety fspec
769 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
770 | 'import' callconv fspec
771 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
773 | 'export' callconv fspec
774 {% mkExport $2 (unLoc $3) >>= return.LL }
776 callconv :: { CallConv }
777 : 'stdcall' { CCall StdCallConv }
778 | 'ccall' { CCall CCallConv }
779 | 'dotnet' { DNCall }
782 : 'unsafe' { PlayRisky }
783 | 'safe' { PlaySafe False }
784 | 'threadsafe' { PlaySafe True }
786 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
787 : STRING var '::' sigtype { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
788 | var '::' sigtype { LL (noLoc nilFS, $1, $3) }
789 -- if the entity string is missing, it defaults to the empty string;
790 -- the meaning of an empty entity string depends on the calling
793 -----------------------------------------------------------------------------
796 opt_sig :: { Maybe (LHsType RdrName) }
797 : {- empty -} { Nothing }
798 | '::' sigtype { Just $2 }
800 opt_asig :: { Maybe (LHsType RdrName) }
801 : {- empty -} { Nothing }
802 | '::' atype { Just $2 }
804 sigtypes1 :: { [LHsType RdrName] }
806 | sigtype ',' sigtypes1 { $1 : $3 }
808 sigtype :: { LHsType RdrName }
809 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
810 -- Wrap an Implicit forall if there isn't one there already
812 sig_vars :: { Located [Located RdrName] }
813 : sig_vars ',' var { LL ($3 : unLoc $1) }
816 -----------------------------------------------------------------------------
819 strict_mark :: { Located HsBang }
820 : '!' { L1 HsStrict }
821 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
823 -- A ctype is a for-all type
824 ctype :: { LHsType RdrName }
825 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
826 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
827 -- A type of form (context => type) is an *implicit* HsForAllTy
830 -- We parse a context as a btype so that we don't get reduce/reduce
831 -- errors in ctype. The basic problem is that
833 -- looks so much like a tuple type. We can't tell until we find the =>
834 context :: { LHsContext RdrName }
835 : btype {% checkContext $1 }
837 type :: { LHsType RdrName }
838 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
841 gentype :: { LHsType RdrName }
843 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
844 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
845 | btype '->' ctype { LL $ HsFunTy $1 $3 }
847 btype :: { LHsType RdrName }
848 : btype atype { LL $ HsAppTy $1 $2 }
851 atype :: { LHsType RdrName }
852 : gtycon { L1 (HsTyVar (unLoc $1)) }
853 | tyvar { L1 (HsTyVar (unLoc $1)) }
854 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
855 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
856 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
857 | '[' ctype ']' { LL $ HsListTy $2 }
858 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
859 | '(' ctype ')' { LL $ HsParTy $2 }
860 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
862 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
864 -- An inst_type is what occurs in the head of an instance decl
865 -- e.g. (Foo a, Gaz b) => Wibble a b
866 -- It's kept as a single type, with a MonoDictTy at the right
867 -- hand corner, for convenience.
868 inst_type :: { LHsType RdrName }
869 : sigtype {% checkInstType $1 }
871 inst_types1 :: { [LHsType RdrName] }
873 | inst_type ',' inst_types1 { $1 : $3 }
875 comma_types0 :: { [LHsType RdrName] }
876 : comma_types1 { $1 }
879 comma_types1 :: { [LHsType RdrName] }
881 | ctype ',' comma_types1 { $1 : $3 }
883 tv_bndrs :: { [LHsTyVarBndr RdrName] }
884 : tv_bndr tv_bndrs { $1 : $2 }
887 tv_bndr :: { LHsTyVarBndr RdrName }
888 : tyvar { L1 (UserTyVar (unLoc $1)) }
889 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
892 fds :: { Located [Located ([RdrName], [RdrName])] }
893 : {- empty -} { noLoc [] }
894 | '|' fds1 { LL (reverse (unLoc $2)) }
896 fds1 :: { Located [Located ([RdrName], [RdrName])] }
897 : fds1 ',' fd { LL ($3 : unLoc $1) }
900 fd :: { Located ([RdrName], [RdrName]) }
901 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
902 (reverse (unLoc $1), reverse (unLoc $3)) }
904 varids0 :: { Located [RdrName] }
905 : {- empty -} { noLoc [] }
906 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
908 -----------------------------------------------------------------------------
911 kind :: { Located Kind }
913 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
915 akind :: { Located Kind }
916 : '*' { L1 liftedTypeKind }
917 | '!' { L1 unliftedTypeKind }
918 | '(' kind ')' { LL (unLoc $2) }
921 -----------------------------------------------------------------------------
922 -- Datatype declarations
924 gadt_constrlist :: { Located [LConDecl RdrName] }
925 : '{' gadt_constrs '}' { LL (unLoc $2) }
926 | vocurly gadt_constrs close { $2 }
928 gadt_constrs :: { Located [LConDecl RdrName] }
929 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
930 | gadt_constrs ';' { $1 }
931 | gadt_constr { L1 [$1] }
933 -- We allow the following forms:
934 -- C :: Eq a => a -> T a
935 -- C :: forall a. Eq a => !a -> T a
936 -- D { x,y :: a } :: T a
937 -- forall a. Eq a => D { x,y :: a } :: T a
939 gadt_constr :: { LConDecl RdrName }
941 { LL (mkGadtDecl $1 $3) }
942 -- Syntax: Maybe merge the record stuff with the single-case above?
943 -- (to kill the mostly harmless reduce/reduce error)
944 -- XXX revisit audreyt
945 | constr_stuff_record '::' sigtype
946 { let (con,details) = unLoc $1 in
947 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
949 | forall context '=>' constr_stuff_record '::' sigtype
950 { let (con,details) = unLoc $4 in
951 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
952 | forall constr_stuff_record '::' sigtype
953 { let (con,details) = unLoc $2 in
954 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
958 constrs :: { Located [LConDecl RdrName] }
959 : {- empty; a GHC extension -} { noLoc [] }
960 | '=' constrs1 { LL (unLoc $2) }
962 constrs1 :: { Located [LConDecl RdrName] }
963 : constrs1 '|' constr { LL ($3 : unLoc $1) }
966 constr :: { LConDecl RdrName }
967 : forall context '=>' constr_stuff
968 { let (con,details) = unLoc $4 in
969 LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
970 | forall constr_stuff
971 { let (con,details) = unLoc $2 in
972 LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
974 forall :: { Located [LHsTyVarBndr RdrName] }
975 : 'forall' tv_bndrs '.' { LL $2 }
976 | {- empty -} { noLoc [] }
978 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
979 -- We parse the constructor declaration
981 -- as a btype (treating C as a type constructor) and then convert C to be
982 -- a data constructor. Reason: it might continue like this:
984 -- in which case C really would be a type constructor. We can't resolve this
985 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
986 : btype {% mkPrefixCon $1 [] >>= return.LL }
987 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
988 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
989 | btype conop btype { LL ($2, InfixCon $1 $3) }
991 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
992 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
993 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
995 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
996 : fielddecl ',' fielddecls { unLoc $1 : $3 }
997 | fielddecl { [unLoc $1] }
999 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
1000 : sig_vars '::' ctype { LL (reverse (unLoc $1), $3) }
1002 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1003 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1004 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1005 -- We don't allow a context, but that's sorted out by the type checker.
1006 deriving :: { Located (Maybe [LHsType RdrName]) }
1007 : {- empty -} { noLoc Nothing }
1008 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1009 ; p <- checkInstType (L loc (HsTyVar tv))
1010 ; return (LL (Just [p])) } }
1011 | 'deriving' '(' ')' { LL (Just []) }
1012 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1013 -- Glasgow extension: allow partial
1014 -- applications in derivings
1016 -----------------------------------------------------------------------------
1017 -- Value definitions
1019 {- There's an awkward overlap with a type signature. Consider
1020 f :: Int -> Int = ...rhs...
1021 Then we can't tell whether it's a type signature or a value
1022 definition with a result signature until we see the '='.
1023 So we have to inline enough to postpone reductions until we know.
1027 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1028 instead of qvar, we get another shift/reduce-conflict. Consider the
1031 { (^^) :: Int->Int ; } Type signature; only var allowed
1033 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1034 qvar allowed (because of instance decls)
1036 We can't tell whether to reduce var to qvar until after we've read the signatures.
1039 decl :: { Located (OrdList (LHsDecl RdrName)) }
1041 | '!' infixexp rhs {% do { pat <- checkPattern $2;
1042 return (LL $ unitOL $ LL $ ValD $
1043 PatBind (LL $ BangPat pat) (unLoc $3)
1044 placeHolderType placeHolderNames) } }
1045 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1046 return (LL $ unitOL (LL $ ValD r)) } }
1048 rhs :: { Located (GRHSs RdrName) }
1049 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1050 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1052 gdrhs :: { Located [LGRHS RdrName] }
1053 : gdrhs gdrh { LL ($2 : unLoc $1) }
1056 gdrh :: { LGRHS RdrName }
1057 : '|' quals '=' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1059 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1060 : infixexp '::' sigtype
1061 {% do s <- checkValSig $1 $3;
1062 return (LL $ unitOL (LL $ SigD s)) }
1063 -- See the above notes for why we need infixexp here
1064 | var ',' sig_vars '::' sigtype
1065 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1066 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1068 | '{-# INLINE' activation qvar '#-}'
1069 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1070 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1071 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1073 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1074 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1076 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1077 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1079 -----------------------------------------------------------------------------
1082 exp :: { LHsExpr RdrName }
1083 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1084 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1085 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1086 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1087 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1090 infixexp :: { LHsExpr RdrName }
1092 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1094 exp10 :: { LHsExpr RdrName }
1095 : '\\' aexp aexps opt_asig '->' exp
1096 {% checkPatterns ($2 : reverse $3) >>= \ ps ->
1097 return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1098 (GRHSs (unguardedRHS $6) emptyLocalBinds
1100 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1101 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1102 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1103 | '-' fexp { LL $ mkHsNegApp $2 }
1105 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1106 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1107 return (L loc (mkHsDo DoExpr stmts body)) }
1108 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1109 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1110 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1111 | scc_annot exp { LL $ if opt_SccProfilingOn
1112 then HsSCC (unLoc $1) $2
1115 | 'proc' aexp '->' exp
1116 {% checkPattern $2 >>= \ p ->
1117 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1118 placeHolderType undefined)) }
1119 -- TODO: is LL right here?
1121 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1122 -- hdaume: core annotation
1125 scc_annot :: { Located FastString }
1126 : '_scc_' STRING { LL $ getSTRING $2 }
1127 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
1129 fexp :: { LHsExpr RdrName }
1130 : fexp aexp { LL $ HsApp $1 $2 }
1133 aexps :: { [LHsExpr RdrName] }
1134 : aexps aexp { $2 : $1 }
1135 | {- empty -} { [] }
1137 aexp :: { LHsExpr RdrName }
1138 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1139 | '~' aexp { LL $ ELazyPat $2 }
1140 -- | '!' aexp { LL $ EBangPat $2 }
1143 aexp1 :: { LHsExpr RdrName }
1144 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1149 -- Here was the syntax for type applications that I was planning
1150 -- but there are difficulties (e.g. what order for type args)
1151 -- so it's not enabled yet.
1152 -- But this case *is* used for the left hand side of a generic definition,
1153 -- which is parsed as an expression before being munged into a pattern
1154 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1155 (sL (getLoc $3) (HsType $3)) }
1157 aexp2 :: { LHsExpr RdrName }
1158 : ipvar { L1 (HsIPVar $! unLoc $1) }
1159 | qcname { L1 (HsVar $! unLoc $1) }
1160 | literal { L1 (HsLit $! unLoc $1) }
1161 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1162 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1163 | '(' exp ')' { LL (HsPar $2) }
1164 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1165 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1166 | '[' list ']' { LL (unLoc $2) }
1167 | '[:' parr ':]' { LL (unLoc $2) }
1168 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1169 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1170 | '_' { L1 EWildPat }
1172 -- Template Haskell Extension
1173 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1174 (L1 $ HsVar (mkUnqual varName
1175 (getTH_ID_SPLICE $1)))) } -- $x
1176 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1178 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1179 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1180 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1181 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1182 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1183 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1184 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1185 return (LL $ HsBracket (PatBr p)) }
1186 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBr (mkGroup $2)) }
1188 -- arrow notation extension
1189 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1191 cmdargs :: { [LHsCmdTop RdrName] }
1192 : cmdargs acmd { $2 : $1 }
1193 | {- empty -} { [] }
1195 acmd :: { LHsCmdTop RdrName }
1196 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1198 cvtopbody :: { [LHsDecl RdrName] }
1199 : '{' cvtopdecls0 '}' { $2 }
1200 | vocurly cvtopdecls0 close { $2 }
1202 cvtopdecls0 :: { [LHsDecl RdrName] }
1203 : {- empty -} { [] }
1206 texp :: { LHsExpr RdrName }
1208 | qopm infixexp { LL $ SectionR $1 $2 }
1209 -- The second production is really here only for bang patterns
1212 texps :: { [LHsExpr RdrName] }
1213 : texps ',' texp { $3 : $1 }
1217 -----------------------------------------------------------------------------
1220 -- The rules below are little bit contorted to keep lexps left-recursive while
1221 -- avoiding another shift/reduce-conflict.
1223 list :: { LHsExpr RdrName }
1224 : texp { L1 $ ExplicitList placeHolderType [$1] }
1225 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1226 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1227 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1228 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1229 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1230 | texp pquals { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1232 lexps :: { Located [LHsExpr RdrName] }
1233 : lexps ',' texp { LL ($3 : unLoc $1) }
1234 | texp ',' texp { LL [$3,$1] }
1236 -----------------------------------------------------------------------------
1237 -- List Comprehensions
1239 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1240 -- or a reversed list of Stmts
1241 : pquals1 { case unLoc $1 of
1243 qss -> L1 [L1 (ParStmt stmtss)]
1245 stmtss = [ (reverse qs, undefined)
1249 pquals1 :: { Located [[LStmt RdrName]] }
1250 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1251 | '|' quals { L (getLoc $2) [unLoc $2] }
1253 quals :: { Located [LStmt RdrName] }
1254 : quals ',' qual { LL ($3 : unLoc $1) }
1257 -----------------------------------------------------------------------------
1258 -- Parallel array expressions
1260 -- The rules below are little bit contorted; see the list case for details.
1261 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1262 -- Moreover, we allow explicit arrays with no element (represented by the nil
1263 -- constructor in the list case).
1265 parr :: { LHsExpr RdrName }
1266 : { noLoc (ExplicitPArr placeHolderType []) }
1267 | exp { L1 $ ExplicitPArr placeHolderType [$1] }
1268 | lexps { L1 $ ExplicitPArr placeHolderType
1269 (reverse (unLoc $1)) }
1270 | exp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1271 | exp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1272 | exp pquals { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1274 -- We are reusing `lexps' and `pquals' from the list case.
1276 -----------------------------------------------------------------------------
1277 -- Case alternatives
1279 altslist :: { Located [LMatch RdrName] }
1280 : '{' alts '}' { LL (reverse (unLoc $2)) }
1281 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1283 alts :: { Located [LMatch RdrName] }
1284 : alts1 { L1 (unLoc $1) }
1285 | ';' alts { LL (unLoc $2) }
1287 alts1 :: { Located [LMatch RdrName] }
1288 : alts1 ';' alt { LL ($3 : unLoc $1) }
1289 | alts1 ';' { LL (unLoc $1) }
1292 alt :: { LMatch RdrName }
1293 : infixexp opt_sig alt_rhs {% checkPattern $1 >>= \p ->
1294 return (LL (Match [p] $2 (unLoc $3))) }
1295 | '!' infixexp opt_sig alt_rhs {% checkPattern $2 >>= \p ->
1296 return (LL (Match [LL $ BangPat p] $3 (unLoc $4))) }
1298 alt_rhs :: { Located (GRHSs RdrName) }
1299 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1301 ralt :: { Located [LGRHS RdrName] }
1302 : '->' exp { LL (unguardedRHS $2) }
1303 | gdpats { L1 (reverse (unLoc $1)) }
1305 gdpats :: { Located [LGRHS RdrName] }
1306 : gdpats gdpat { LL ($2 : unLoc $1) }
1309 gdpat :: { LGRHS RdrName }
1310 : '|' quals '->' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1312 -----------------------------------------------------------------------------
1313 -- Statement sequences
1315 stmtlist :: { Located [LStmt RdrName] }
1316 : '{' stmts '}' { LL (unLoc $2) }
1317 | vocurly stmts close { $2 }
1319 -- do { ;; s ; s ; ; s ;; }
1320 -- The last Stmt should be an expression, but that's hard to enforce
1321 -- here, because we need too much lookahead if we see do { e ; }
1322 -- So we use ExprStmts throughout, and switch the last one over
1323 -- in ParseUtils.checkDo instead
1324 stmts :: { Located [LStmt RdrName] }
1325 : stmt stmts_help { LL ($1 : unLoc $2) }
1326 | ';' stmts { LL (unLoc $2) }
1327 | {- empty -} { noLoc [] }
1329 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1330 : ';' stmts { LL (unLoc $2) }
1331 | {- empty -} { noLoc [] }
1333 -- For typing stmts at the GHCi prompt, where
1334 -- the input may consist of just comments.
1335 maybe_stmt :: { Maybe (LStmt RdrName) }
1337 | {- nothing -} { Nothing }
1339 stmt :: { LStmt RdrName }
1341 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1342 return (LL $ mkBindStmt p $1) }
1343 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1345 qual :: { LStmt RdrName }
1346 : exp '<-' exp {% checkPattern $1 >>= \p ->
1347 return (LL $ mkBindStmt p $3) }
1348 | exp { L1 $ mkExprStmt $1 }
1349 | 'let' binds { LL $ LetStmt (unLoc $2) }
1351 -----------------------------------------------------------------------------
1352 -- Record Field Update/Construction
1354 fbinds :: { HsRecordBinds RdrName }
1356 | {- empty -} { [] }
1358 fbinds1 :: { HsRecordBinds RdrName }
1359 : fbinds1 ',' fbind { $3 : $1 }
1362 fbind :: { (Located RdrName, LHsExpr RdrName) }
1363 : qvar '=' exp { ($1,$3) }
1365 -----------------------------------------------------------------------------
1366 -- Implicit Parameter Bindings
1368 dbinds :: { Located [LIPBind RdrName] }
1369 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1370 | dbinds ';' { LL (unLoc $1) }
1372 -- | {- empty -} { [] }
1374 dbind :: { LIPBind RdrName }
1375 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1377 ipvar :: { Located (IPName RdrName) }
1378 : IPDUPVARID { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1379 | IPSPLITVARID { L1 (Linear (mkUnqual varName (getIPSPLITVARID $1))) }
1381 -----------------------------------------------------------------------------
1384 depreclist :: { Located [RdrName] }
1385 depreclist : deprec_var { L1 [unLoc $1] }
1386 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1388 deprec_var :: { Located RdrName }
1389 deprec_var : var { $1 }
1392 -----------------------------------------
1393 -- Data constructors
1394 qcon :: { Located RdrName }
1396 | '(' qconsym ')' { LL (unLoc $2) }
1397 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1398 -- The case of '[:' ':]' is part of the production `parr'
1400 con :: { Located RdrName }
1402 | '(' consym ')' { LL (unLoc $2) }
1403 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1405 sysdcon :: { Located DataCon } -- Wired in data constructors
1406 : '(' ')' { LL unitDataCon }
1407 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1408 | '[' ']' { LL nilDataCon }
1410 conop :: { Located RdrName }
1412 | '`' conid '`' { LL (unLoc $2) }
1414 qconop :: { Located RdrName }
1416 | '`' qconid '`' { LL (unLoc $2) }
1418 -----------------------------------------------------------------------------
1419 -- Type constructors
1421 gtycon :: { Located RdrName } -- A "general" qualified tycon
1423 | '(' ')' { LL $ getRdrName unitTyCon }
1424 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1425 | '(' '->' ')' { LL $ getRdrName funTyCon }
1426 | '[' ']' { LL $ listTyCon_RDR }
1427 | '[:' ':]' { LL $ parrTyCon_RDR }
1429 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1431 | '(' qtyconsym ')' { LL (unLoc $2) }
1433 qtyconop :: { Located RdrName } -- Qualified or unqualified
1435 | '`' qtycon '`' { LL (unLoc $2) }
1437 qtycon :: { Located RdrName } -- Qualified or unqualified
1438 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1441 tycon :: { Located RdrName } -- Unqualified
1442 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1444 qtyconsym :: { Located RdrName }
1445 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1448 tyconsym :: { Located RdrName }
1449 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1451 -----------------------------------------------------------------------------
1454 op :: { Located RdrName } -- used in infix decls
1458 varop :: { Located RdrName }
1460 | '`' varid '`' { LL (unLoc $2) }
1462 qop :: { LHsExpr RdrName } -- used in sections
1463 : qvarop { L1 $ HsVar (unLoc $1) }
1464 | qconop { L1 $ HsVar (unLoc $1) }
1466 qopm :: { LHsExpr RdrName } -- used in sections
1467 : qvaropm { L1 $ HsVar (unLoc $1) }
1468 | qconop { L1 $ HsVar (unLoc $1) }
1470 qvarop :: { Located RdrName }
1472 | '`' qvarid '`' { LL (unLoc $2) }
1474 qvaropm :: { Located RdrName }
1475 : qvarsym_no_minus { $1 }
1476 | '`' qvarid '`' { LL (unLoc $2) }
1478 -----------------------------------------------------------------------------
1481 tyvar :: { Located RdrName }
1482 tyvar : tyvarid { $1 }
1483 | '(' tyvarsym ')' { LL (unLoc $2) }
1485 tyvarop :: { Located RdrName }
1486 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1489 tyvarid :: { Located RdrName }
1490 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1491 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1492 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1493 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1494 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1496 tyvarsym :: { Located RdrName }
1497 -- Does not include "!", because that is used for strictness marks
1498 -- or ".", because that separates the quantified type vars from the rest
1499 -- or "*", because that's used for kinds
1500 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1502 -----------------------------------------------------------------------------
1505 var :: { Located RdrName }
1507 | '(' varsym ')' { LL (unLoc $2) }
1509 qvar :: { Located RdrName }
1511 | '(' varsym ')' { LL (unLoc $2) }
1512 | '(' qvarsym1 ')' { LL (unLoc $2) }
1513 -- We've inlined qvarsym here so that the decision about
1514 -- whether it's a qvar or a var can be postponed until
1515 -- *after* we see the close paren.
1517 qvarid :: { Located RdrName }
1519 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1521 varid :: { Located RdrName }
1522 : varid_no_unsafe { $1 }
1523 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1524 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1525 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1527 varid_no_unsafe :: { Located RdrName }
1528 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1529 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1530 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1531 | 'iso' { L1 $! mkUnqual varName FSLIT("iso") }
1532 | 'family' { L1 $! mkUnqual varName FSLIT("family") }
1534 qvarsym :: { Located RdrName }
1538 qvarsym_no_minus :: { Located RdrName }
1539 : varsym_no_minus { $1 }
1542 qvarsym1 :: { Located RdrName }
1543 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1545 varsym :: { Located RdrName }
1546 : varsym_no_minus { $1 }
1547 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1549 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1550 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1551 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1554 -- These special_ids are treated as keywords in various places,
1555 -- but as ordinary ids elsewhere. 'special_id' collects all these
1556 -- except 'unsafe', 'forall', 'family', and 'iso' whose treatment differs
1557 -- depending on context
1558 special_id :: { Located FastString }
1560 : 'as' { L1 FSLIT("as") }
1561 | 'qualified' { L1 FSLIT("qualified") }
1562 | 'hiding' { L1 FSLIT("hiding") }
1563 | 'export' { L1 FSLIT("export") }
1564 | 'label' { L1 FSLIT("label") }
1565 | 'dynamic' { L1 FSLIT("dynamic") }
1566 | 'stdcall' { L1 FSLIT("stdcall") }
1567 | 'ccall' { L1 FSLIT("ccall") }
1569 special_sym :: { Located FastString }
1570 special_sym : '!' { L1 FSLIT("!") }
1571 | '.' { L1 FSLIT(".") }
1572 | '*' { L1 FSLIT("*") }
1574 -----------------------------------------------------------------------------
1575 -- Data constructors
1577 qconid :: { Located RdrName } -- Qualified or unqualified
1579 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1581 conid :: { Located RdrName }
1582 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1584 qconsym :: { Located RdrName } -- Qualified or unqualified
1586 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1588 consym :: { Located RdrName }
1589 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1591 -- ':' means only list cons
1592 | ':' { L1 $ consDataCon_RDR }
1595 -----------------------------------------------------------------------------
1598 literal :: { Located HsLit }
1599 : CHAR { L1 $ HsChar $ getCHAR $1 }
1600 | STRING { L1 $ HsString $ getSTRING $1 }
1601 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1602 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1603 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1604 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1605 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1607 -----------------------------------------------------------------------------
1611 : vccurly { () } -- context popped in lexer.
1612 | error {% popContext }
1614 -----------------------------------------------------------------------------
1615 -- Miscellaneous (mostly renamings)
1617 modid :: { Located ModuleName }
1618 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1619 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1622 (unpackFS mod ++ '.':unpackFS c))
1626 : commas ',' { $1 + 1 }
1629 -----------------------------------------------------------------------------
1633 happyError = srcParseFail
1635 getVARID (L _ (ITvarid x)) = x
1636 getCONID (L _ (ITconid x)) = x
1637 getVARSYM (L _ (ITvarsym x)) = x
1638 getCONSYM (L _ (ITconsym x)) = x
1639 getQVARID (L _ (ITqvarid x)) = x
1640 getQCONID (L _ (ITqconid x)) = x
1641 getQVARSYM (L _ (ITqvarsym x)) = x
1642 getQCONSYM (L _ (ITqconsym x)) = x
1643 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1644 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1645 getCHAR (L _ (ITchar x)) = x
1646 getSTRING (L _ (ITstring x)) = x
1647 getINTEGER (L _ (ITinteger x)) = x
1648 getRATIONAL (L _ (ITrational x)) = x
1649 getPRIMCHAR (L _ (ITprimchar x)) = x
1650 getPRIMSTRING (L _ (ITprimstring x)) = x
1651 getPRIMINTEGER (L _ (ITprimint x)) = x
1652 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1653 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1654 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1655 getINLINE (L _ (ITinline_prag b)) = b
1656 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1658 -- Utilities for combining source spans
1659 comb2 :: Located a -> Located b -> SrcSpan
1662 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1663 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1665 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1666 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1667 combineSrcSpans (getLoc c) (getLoc d)
1669 -- strict constructor version:
1671 sL :: SrcSpan -> a -> Located a
1672 sL span a = span `seq` L span a
1674 -- Make a source location for the file. We're a bit lazy here and just
1675 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1676 -- try to find the span of the whole file (ToDo).
1677 fileSrcSpan :: P SrcSpan
1680 let loc = mkSrcLoc (srcLocFile l) 1 0;
1681 return (mkSrcSpan loc loc)