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 )
47 -----------------------------------------------------------------------------
50 Conflicts: 37 shift/reduce
53 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
54 would think the two should never occur in the same context.
58 -----------------------------------------------------------------------------
59 Conflicts: 36 shift/reduce (1.25)
61 10 for abiguity in 'if x then y else z + 1' [State 178]
62 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
63 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
65 1 for ambiguity in 'if x then y else z :: T' [State 178]
66 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
68 4 for ambiguity in 'if x then y else z -< e' [State 178]
69 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
70 There are four such operators: -<, >-, -<<, >>-
73 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
74 Which of these two is intended?
76 (x::T) -> T -- Rhs is T
79 (x::T -> T) -> .. -- Rhs is ...
81 10 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
84 As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
85 Same duplication between states 11 and 253 as the previous case
87 1 for ambiguity in 'let ?x ...' [State 329]
88 the parser can't tell whether the ?x is the lhs of a normal binding or
89 an implicit binding. Fortunately resolving as shift gives it the only
90 sensible meaning, namely the lhs of an implicit binding.
92 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
93 we don't know whether the '[' starts the activation or not: it
94 might be the start of the declaration with the activation being
97 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
98 since 'forall' is a valid variable name, we don't know whether
99 to treat a forall on the input as the beginning of a quantifier
100 or the beginning of the rule itself. Resolving to shift means
101 it's always treated as a quantifier, hence the above is disallowed.
102 This saves explicitly defining a grammar for the rule lhs that
103 doesn't include 'forall'.
105 -- ---------------------------------------------------------------------------
106 -- Adding location info
108 This is done in a stylised way using the three macros below, L0, L1
109 and LL. Each of these macros can be thought of as having type
111 L0, L1, LL :: a -> Located a
113 They each add a SrcSpan to their argument.
115 L0 adds 'noSrcSpan', used for empty productions
116 -- This doesn't seem to work anymore -=chak
118 L1 for a production with a single token on the lhs. Grabs the SrcSpan
121 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
122 the first and last tokens.
124 These suffice for the majority of cases. However, we must be
125 especially careful with empty productions: LL won't work if the first
126 or last token on the lhs can represent an empty span. In these cases,
127 we have to calculate the span using more of the tokens from the lhs, eg.
129 | 'newtype' tycl_hdr '=' newconstr deriving
131 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
133 We provide comb3 and comb4 functions which are useful in such cases.
135 Be careful: there's no checking that you actually got this right, the
136 only symptom will be that the SrcSpans of your syntax will be
140 * We must expand these macros *before* running Happy, which is why this file is
141 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
143 #define L0 L noSrcSpan
144 #define L1 sL (getLoc $1)
145 #define LL sL (comb2 $1 $>)
147 -- -----------------------------------------------------------------------------
152 '_' { L _ ITunderscore } -- Haskell keywords
154 'case' { L _ ITcase }
155 'class' { L _ ITclass }
156 'data' { L _ ITdata }
157 'default' { L _ ITdefault }
158 'deriving' { L _ ITderiving }
160 'else' { L _ ITelse }
161 'hiding' { L _ IThiding }
163 'import' { L _ ITimport }
165 'infix' { L _ ITinfix }
166 'infixl' { L _ ITinfixl }
167 'infixr' { L _ ITinfixr }
168 'instance' { L _ ITinstance }
170 'module' { L _ ITmodule }
171 'newtype' { L _ ITnewtype }
173 'qualified' { L _ ITqualified }
174 'then' { L _ ITthen }
175 'type' { L _ ITtype }
176 'where' { L _ ITwhere }
177 '_scc_' { L _ ITscc } -- ToDo: remove
179 'forall' { L _ ITforall } -- GHC extension keywords
180 'foreign' { L _ ITforeign }
181 'export' { L _ ITexport }
182 'label' { L _ ITlabel }
183 'dynamic' { L _ ITdynamic }
184 'safe' { L _ ITsafe }
185 'threadsafe' { L _ ITthreadsafe }
186 'unsafe' { L _ ITunsafe }
189 'family' { L _ ITfamily }
190 'stdcall' { L _ ITstdcallconv }
191 'ccall' { L _ ITccallconv }
192 'dotnet' { L _ ITdotnet }
193 'proc' { L _ ITproc } -- for arrow notation extension
194 'rec' { L _ ITrec } -- for arrow notation extension
196 '{-# INLINE' { L _ (ITinline_prag _) }
197 '{-# SPECIALISE' { L _ ITspec_prag }
198 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
199 '{-# SOURCE' { L _ ITsource_prag }
200 '{-# RULES' { L _ ITrules_prag }
201 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
202 '{-# SCC' { L _ ITscc_prag }
203 '{-# DEPRECATED' { L _ ITdeprecated_prag }
204 '{-# UNPACK' { L _ ITunpack_prag }
205 '#-}' { L _ ITclose_prag }
207 '..' { L _ ITdotdot } -- reserved symbols
209 '::' { L _ ITdcolon }
213 '<-' { L _ ITlarrow }
214 '->' { L _ ITrarrow }
217 '=>' { L _ ITdarrow }
221 '-<' { L _ ITlarrowtail } -- for arrow notation
222 '>-' { L _ ITrarrowtail } -- for arrow notation
223 '-<<' { L _ ITLarrowtail } -- for arrow notation
224 '>>-' { L _ ITRarrowtail } -- for arrow notation
227 '{' { L _ ITocurly } -- special symbols
229 '{|' { L _ ITocurlybar }
230 '|}' { L _ ITccurlybar }
231 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
232 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
235 '[:' { L _ ITopabrack }
236 ':]' { L _ ITcpabrack }
239 '(#' { L _ IToubxparen }
240 '#)' { L _ ITcubxparen }
241 '(|' { L _ IToparenbar }
242 '|)' { L _ ITcparenbar }
245 '`' { L _ ITbackquote }
247 VARID { L _ (ITvarid _) } -- identifiers
248 CONID { L _ (ITconid _) }
249 VARSYM { L _ (ITvarsym _) }
250 CONSYM { L _ (ITconsym _) }
251 QVARID { L _ (ITqvarid _) }
252 QCONID { L _ (ITqconid _) }
253 QVARSYM { L _ (ITqvarsym _) }
254 QCONSYM { L _ (ITqconsym _) }
256 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
257 IPSPLITVARID { L _ (ITsplitipvarid _) } -- GHC extension
259 CHAR { L _ (ITchar _) }
260 STRING { L _ (ITstring _) }
261 INTEGER { L _ (ITinteger _) }
262 RATIONAL { L _ (ITrational _) }
264 PRIMCHAR { L _ (ITprimchar _) }
265 PRIMSTRING { L _ (ITprimstring _) }
266 PRIMINTEGER { L _ (ITprimint _) }
267 PRIMFLOAT { L _ (ITprimfloat _) }
268 PRIMDOUBLE { L _ (ITprimdouble _) }
271 '[|' { L _ ITopenExpQuote }
272 '[p|' { L _ ITopenPatQuote }
273 '[t|' { L _ ITopenTypQuote }
274 '[d|' { L _ ITopenDecQuote }
275 '|]' { L _ ITcloseQuote }
276 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
277 '$(' { L _ ITparenEscape } -- $( exp )
278 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
279 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
281 %monad { P } { >>= } { return }
282 %lexer { lexer } { L _ ITeof }
283 %name parseModule module
284 %name parseStmt maybe_stmt
285 %name parseIdentifier identifier
286 %name parseType ctype
287 %partial parseHeader header
288 %tokentype { (Located Token) }
291 -----------------------------------------------------------------------------
292 -- Identifiers; one of the entry points
293 identifier :: { Located RdrName }
299 -----------------------------------------------------------------------------
302 -- The place for module deprecation is really too restrictive, but if it
303 -- was allowed at its natural place just before 'module', we get an ugly
304 -- s/r conflict with the second alternative. Another solution would be the
305 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
306 -- either, and DEPRECATED is only expected to be used by people who really
307 -- know what they are doing. :-)
309 module :: { Located (HsModule RdrName) }
310 : 'module' modid maybemoddeprec maybeexports 'where' body
311 {% fileSrcSpan >>= \ loc ->
312 return (L loc (HsModule (Just $2) $4 (fst $6) (snd $6) $3)) }
313 | missing_module_keyword top close
314 {% fileSrcSpan >>= \ loc ->
315 return (L loc (HsModule Nothing Nothing
316 (fst $2) (snd $2) Nothing)) }
318 missing_module_keyword :: { () }
319 : {- empty -} {% pushCurrentContext }
321 maybemoddeprec :: { Maybe DeprecTxt }
322 : '{-# DEPRECATED' STRING '#-}' { Just (getSTRING $2) }
323 | {- empty -} { Nothing }
325 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
327 | vocurly top close { $2 }
329 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
330 : importdecls { (reverse $1,[]) }
331 | importdecls ';' cvtopdecls { (reverse $1,$3) }
332 | cvtopdecls { ([],$1) }
334 cvtopdecls :: { [LHsDecl RdrName] }
335 : topdecls { cvTopDecls $1 }
337 -----------------------------------------------------------------------------
338 -- Module declaration & imports only
340 header :: { Located (HsModule RdrName) }
341 : 'module' modid maybemoddeprec maybeexports 'where' header_body
342 {% fileSrcSpan >>= \ loc ->
343 return (L loc (HsModule (Just $2) $4 $6 [] $3)) }
344 | missing_module_keyword importdecls
345 {% fileSrcSpan >>= \ loc ->
346 return (L loc (HsModule Nothing Nothing $2 [] Nothing)) }
348 header_body :: { [LImportDecl RdrName] }
349 : '{' importdecls { $2 }
350 | vocurly importdecls { $2 }
352 -----------------------------------------------------------------------------
355 maybeexports :: { Maybe [LIE RdrName] }
356 : '(' exportlist ')' { Just $2 }
357 | {- empty -} { Nothing }
359 exportlist :: { [LIE RdrName] }
363 exportlist1 :: { [LIE RdrName] }
365 | export ',' exportlist { $1 : $3 }
368 -- No longer allow things like [] and (,,,) to be exported
369 -- They are built in syntax, always available
370 export :: { LIE RdrName }
371 : qvar { L1 (IEVar (unLoc $1)) }
372 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
373 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
374 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
375 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
376 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
378 qcnames :: { [RdrName] }
379 : qcnames ',' qcname_ext { unLoc $3 : $1 }
380 | qcname_ext { [unLoc $1] }
382 qcname_ext :: { Located RdrName } -- Variable or data constructor
383 -- or tagged type constructor
385 | 'type' qcon { sL (comb2 $1 $2)
386 (setRdrNameSpace (unLoc $2)
389 -- Cannot pull into qcname_ext, as qcname is also used in expression.
390 qcname :: { Located RdrName } -- Variable or data constructor
394 -----------------------------------------------------------------------------
395 -- Import Declarations
397 -- import decls can be *empty*, or even just a string of semicolons
398 -- whereas topdecls must contain at least one topdecl.
400 importdecls :: { [LImportDecl RdrName] }
401 : importdecls ';' importdecl { $3 : $1 }
402 | importdecls ';' { $1 }
403 | importdecl { [ $1 ] }
406 importdecl :: { LImportDecl RdrName }
407 : 'import' maybe_src optqualified modid maybeas maybeimpspec
408 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
410 maybe_src :: { IsBootInterface }
411 : '{-# SOURCE' '#-}' { True }
412 | {- empty -} { False }
414 optqualified :: { Bool }
415 : 'qualified' { True }
416 | {- empty -} { False }
418 maybeas :: { Located (Maybe ModuleName) }
419 : 'as' modid { LL (Just (unLoc $2)) }
420 | {- empty -} { noLoc Nothing }
422 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
423 : impspec { L1 (Just (unLoc $1)) }
424 | {- empty -} { noLoc Nothing }
426 impspec :: { Located (Bool, [LIE RdrName]) }
427 : '(' exportlist ')' { LL (False, $2) }
428 | 'hiding' '(' exportlist ')' { LL (True, $3) }
430 -----------------------------------------------------------------------------
431 -- Fixity Declarations
435 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
437 infix :: { Located FixityDirection }
438 : 'infix' { L1 InfixN }
439 | 'infixl' { L1 InfixL }
440 | 'infixr' { L1 InfixR }
442 ops :: { Located [Located RdrName] }
443 : ops ',' op { LL ($3 : unLoc $1) }
446 -----------------------------------------------------------------------------
447 -- Top-Level Declarations
449 topdecls :: { OrdList (LHsDecl RdrName) }
450 : topdecls ';' topdecl { $1 `appOL` $3 }
451 | topdecls ';' { $1 }
454 topdecl :: { OrdList (LHsDecl RdrName) }
455 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
456 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
457 | 'instance' inst_type where
458 { let (binds, sigs, ats) = cvBindsAndSigs (unLoc $3)
459 in unitOL (L (comb3 $1 $2 $3)
460 (InstD (InstDecl $2 binds sigs ats))) }
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) } }
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' opt_iso type '::' kind
505 -- Note the use of type for the head; this allows
506 -- infix type constructors to be declared
508 {% do { (tc, tvs, _) <- checkSynHdr $4 False
509 ; return (L (comb3 $1 $4 $6)
510 (TyFunction tc tvs $3 (unLoc $6)))
513 -- type instance declarations
514 | 'type' 'instance' type '=' ctype
515 -- Note the use of type for the head; this allows
516 -- infix type constructors and type patterns
518 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
519 ; return (L (comb2 $1 $5)
520 (TySynonym tc tvs (Just typats) $5))
523 -- ordinary data type or newtype declaration
524 | data_or_newtype tycl_hdr constrs deriving
525 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
526 ; checkTyVars tparms -- no type pattern
528 L (comb4 $1 $2 $3 $4)
529 -- We need the location on tycl_hdr in case
530 -- constrs and deriving are both empty
531 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
532 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
534 -- ordinary GADT declaration
535 | data_or_newtype tycl_hdr opt_kind_sig
536 'where' gadt_constrlist
538 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
539 ; checkTyVars tparms -- can have type pats
541 L (comb4 $1 $2 $4 $5)
542 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing) $3
543 (reverse (unLoc $5)) (unLoc $6)) } }
545 -- data/newtype family
546 | data_or_newtype 'family' tycl_hdr '::' kind
547 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
548 ; checkTyVars tparms -- no type pattern
551 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
552 (Just (unLoc $5)) [] Nothing) } }
554 -- data/newtype instance declaration
555 | data_or_newtype 'instance' tycl_hdr constrs deriving
556 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
557 -- can have type pats
559 L (comb4 $1 $3 $4 $5)
560 -- We need the location on tycl_hdr in case
561 -- constrs and deriving are both empty
562 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
563 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
565 -- GADT instance declaration
566 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
567 'where' gadt_constrlist
569 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
570 -- can have type pats
572 L (comb4 $1 $3 $6 $7)
573 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
574 $4 (reverse (unLoc $6)) (unLoc $7)) } }
580 data_or_newtype :: { Located NewOrData }
581 : 'data' { L1 DataType }
582 | 'newtype' { L1 NewType }
584 opt_kind_sig :: { Maybe Kind }
586 | '::' kind { Just (unLoc $2) }
588 -- tycl_hdr parses the header of a class or data type decl,
589 -- which takes the form
592 -- (Eq a, Ord b) => T a b
593 -- T Int [a] -- for associated types
594 -- Rather a lot of inlining here, else we get reduce/reduce errors
595 tycl_hdr :: { Located (LHsContext RdrName,
597 [LHsTyVarBndr RdrName],
599 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
600 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
602 -----------------------------------------------------------------------------
603 -- Nested declarations
605 -- Type declaration or value declaration
607 tydecl :: { Located (OrdList (LHsDecl RdrName)) }
608 tydecl : ty_decl { LL (unitOL (L1 (TyClD (unLoc $1)))) }
611 tydecls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
612 : tydecls ';' tydecl { LL (unLoc $1 `appOL` unLoc $3) }
613 | tydecls ';' { LL (unLoc $1) }
615 | {- empty -} { noLoc nilOL }
619 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
620 : '{' tydecls '}' { LL (unLoc $2) }
621 | vocurly tydecls close { $2 }
623 -- Form of the body of class and instance declarations
625 where :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
626 -- No implicit parameters
627 -- May have type declarations
628 : 'where' tydecllist { LL (unLoc $2) }
629 | {- empty -} { noLoc nilOL }
631 decls :: { Located (OrdList (LHsDecl RdrName)) }
632 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
633 | decls ';' { LL (unLoc $1) }
635 | {- empty -} { noLoc nilOL }
638 decllist :: { Located (OrdList (LHsDecl RdrName)) }
639 : '{' decls '}' { LL (unLoc $2) }
640 | vocurly decls close { $2 }
642 -- Binding groups other than those of class and instance declarations
644 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
645 -- No type declarations
646 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
647 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
648 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
650 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
651 -- No type declarations
652 : 'where' binds { LL (unLoc $2) }
653 | {- empty -} { noLoc emptyLocalBinds }
656 -----------------------------------------------------------------------------
657 -- Transformation Rules
659 rules :: { OrdList (LHsDecl RdrName) }
660 : rules ';' rule { $1 `snocOL` $3 }
663 | {- empty -} { nilOL }
665 rule :: { LHsDecl RdrName }
666 : STRING activation rule_forall infixexp '=' exp
667 { LL $ RuleD (HsRule (getSTRING $1)
668 ($2 `orElse` AlwaysActive)
669 $3 $4 placeHolderNames $6 placeHolderNames) }
671 activation :: { Maybe Activation }
672 : {- empty -} { Nothing }
673 | explicit_activation { Just $1 }
675 explicit_activation :: { Activation } -- In brackets
676 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
677 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
679 rule_forall :: { [RuleBndr RdrName] }
680 : 'forall' rule_var_list '.' { $2 }
683 rule_var_list :: { [RuleBndr RdrName] }
685 | rule_var rule_var_list { $1 : $2 }
687 rule_var :: { RuleBndr RdrName }
688 : varid { RuleBndr $1 }
689 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
691 -----------------------------------------------------------------------------
692 -- Deprecations (c.f. rules)
694 deprecations :: { OrdList (LHsDecl RdrName) }
695 : deprecations ';' deprecation { $1 `appOL` $3 }
696 | deprecations ';' { $1 }
698 | {- empty -} { nilOL }
700 -- SUP: TEMPORARY HACK, not checking for `module Foo'
701 deprecation :: { OrdList (LHsDecl RdrName) }
703 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
707 -----------------------------------------------------------------------------
708 -- Foreign import and export declarations
710 fdecl :: { LHsDecl RdrName }
711 fdecl : 'import' callconv safety fspec
712 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
713 | 'import' callconv fspec
714 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
716 | 'export' callconv fspec
717 {% mkExport $2 (unLoc $3) >>= return.LL }
719 callconv :: { CallConv }
720 : 'stdcall' { CCall StdCallConv }
721 | 'ccall' { CCall CCallConv }
722 | 'dotnet' { DNCall }
725 : 'unsafe' { PlayRisky }
726 | 'safe' { PlaySafe False }
727 | 'threadsafe' { PlaySafe True }
729 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
730 : STRING var '::' sigtype { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
731 | var '::' sigtype { LL (noLoc nilFS, $1, $3) }
732 -- if the entity string is missing, it defaults to the empty string;
733 -- the meaning of an empty entity string depends on the calling
736 -----------------------------------------------------------------------------
739 opt_sig :: { Maybe (LHsType RdrName) }
740 : {- empty -} { Nothing }
741 | '::' sigtype { Just $2 }
743 opt_asig :: { Maybe (LHsType RdrName) }
744 : {- empty -} { Nothing }
745 | '::' atype { Just $2 }
747 sigtypes1 :: { [LHsType RdrName] }
749 | sigtype ',' sigtypes1 { $1 : $3 }
751 sigtype :: { LHsType RdrName }
752 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
753 -- Wrap an Implicit forall if there isn't one there already
755 sig_vars :: { Located [Located RdrName] }
756 : sig_vars ',' var { LL ($3 : unLoc $1) }
759 -----------------------------------------------------------------------------
762 strict_mark :: { Located HsBang }
763 : '!' { L1 HsStrict }
764 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
766 -- A ctype is a for-all type
767 ctype :: { LHsType RdrName }
768 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
769 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
770 -- A type of form (context => type) is an *implicit* HsForAllTy
773 -- We parse a context as a btype so that we don't get reduce/reduce
774 -- errors in ctype. The basic problem is that
776 -- looks so much like a tuple type. We can't tell until we find the =>
777 context :: { LHsContext RdrName }
778 : btype {% checkContext $1 }
780 type :: { LHsType RdrName }
781 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
784 gentype :: { LHsType RdrName }
786 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
787 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
788 | btype '->' ctype { LL $ HsFunTy $1 $3 }
790 btype :: { LHsType RdrName }
791 : btype atype { LL $ HsAppTy $1 $2 }
794 atype :: { LHsType RdrName }
795 : gtycon { L1 (HsTyVar (unLoc $1)) }
796 | tyvar { L1 (HsTyVar (unLoc $1)) }
797 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
798 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
799 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
800 | '[' ctype ']' { LL $ HsListTy $2 }
801 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
802 | '(' ctype ')' { LL $ HsParTy $2 }
803 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
805 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
807 -- An inst_type is what occurs in the head of an instance decl
808 -- e.g. (Foo a, Gaz b) => Wibble a b
809 -- It's kept as a single type, with a MonoDictTy at the right
810 -- hand corner, for convenience.
811 inst_type :: { LHsType RdrName }
812 : sigtype {% checkInstType $1 }
814 inst_types1 :: { [LHsType RdrName] }
816 | inst_type ',' inst_types1 { $1 : $3 }
818 comma_types0 :: { [LHsType RdrName] }
819 : comma_types1 { $1 }
822 comma_types1 :: { [LHsType RdrName] }
824 | ctype ',' comma_types1 { $1 : $3 }
826 tv_bndrs :: { [LHsTyVarBndr RdrName] }
827 : tv_bndr tv_bndrs { $1 : $2 }
830 tv_bndr :: { LHsTyVarBndr RdrName }
831 : tyvar { L1 (UserTyVar (unLoc $1)) }
832 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
835 fds :: { Located [Located ([RdrName], [RdrName])] }
836 : {- empty -} { noLoc [] }
837 | '|' fds1 { LL (reverse (unLoc $2)) }
839 fds1 :: { Located [Located ([RdrName], [RdrName])] }
840 : fds1 ',' fd { LL ($3 : unLoc $1) }
843 fd :: { Located ([RdrName], [RdrName]) }
844 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
845 (reverse (unLoc $1), reverse (unLoc $3)) }
847 varids0 :: { Located [RdrName] }
848 : {- empty -} { noLoc [] }
849 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
851 -----------------------------------------------------------------------------
854 kind :: { Located Kind }
856 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
858 akind :: { Located Kind }
859 : '*' { L1 liftedTypeKind }
860 | '!' { L1 unliftedTypeKind }
861 | '(' kind ')' { LL (unLoc $2) }
864 -----------------------------------------------------------------------------
865 -- Datatype declarations
867 gadt_constrlist :: { Located [LConDecl RdrName] }
868 : '{' gadt_constrs '}' { LL (unLoc $2) }
869 | vocurly gadt_constrs close { $2 }
871 gadt_constrs :: { Located [LConDecl RdrName] }
872 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
873 | gadt_constrs ';' { $1 }
874 | gadt_constr { L1 [$1] }
876 -- We allow the following forms:
877 -- C :: Eq a => a -> T a
878 -- C :: forall a. Eq a => !a -> T a
879 -- D { x,y :: a } :: T a
880 -- forall a. Eq a => D { x,y :: a } :: T a
882 gadt_constr :: { LConDecl RdrName }
884 { LL (mkGadtDecl $1 $3) }
885 -- Syntax: Maybe merge the record stuff with the single-case above?
886 -- (to kill the mostly harmless reduce/reduce error)
887 -- XXX revisit audreyt
888 | constr_stuff_record '::' sigtype
889 { let (con,details) = unLoc $1 in
890 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
892 | forall context '=>' constr_stuff_record '::' sigtype
893 { let (con,details) = unLoc $4 in
894 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
895 | forall constr_stuff_record '::' sigtype
896 { let (con,details) = unLoc $2 in
897 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
901 constrs :: { Located [LConDecl RdrName] }
902 : {- empty; a GHC extension -} { noLoc [] }
903 | '=' constrs1 { LL (unLoc $2) }
905 constrs1 :: { Located [LConDecl RdrName] }
906 : constrs1 '|' constr { LL ($3 : unLoc $1) }
909 constr :: { LConDecl RdrName }
910 : forall context '=>' constr_stuff
911 { let (con,details) = unLoc $4 in
912 LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
913 | forall constr_stuff
914 { let (con,details) = unLoc $2 in
915 LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
917 forall :: { Located [LHsTyVarBndr RdrName] }
918 : 'forall' tv_bndrs '.' { LL $2 }
919 | {- empty -} { noLoc [] }
921 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
922 -- We parse the constructor declaration
924 -- as a btype (treating C as a type constructor) and then convert C to be
925 -- a data constructor. Reason: it might continue like this:
927 -- in which case C really would be a type constructor. We can't resolve this
928 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
929 : btype {% mkPrefixCon $1 [] >>= return.LL }
930 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
931 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
932 | btype conop btype { LL ($2, InfixCon $1 $3) }
934 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
935 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
936 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
938 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
939 : fielddecl ',' fielddecls { unLoc $1 : $3 }
940 | fielddecl { [unLoc $1] }
942 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
943 : sig_vars '::' ctype { LL (reverse (unLoc $1), $3) }
945 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
946 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
947 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
948 -- We don't allow a context, but that's sorted out by the type checker.
949 deriving :: { Located (Maybe [LHsType RdrName]) }
950 : {- empty -} { noLoc Nothing }
951 | 'deriving' qtycon {% do { let { L loc tv = $2 }
952 ; p <- checkInstType (L loc (HsTyVar tv))
953 ; return (LL (Just [p])) } }
954 | 'deriving' '(' ')' { LL (Just []) }
955 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
956 -- Glasgow extension: allow partial
957 -- applications in derivings
959 -----------------------------------------------------------------------------
962 {- There's an awkward overlap with a type signature. Consider
963 f :: Int -> Int = ...rhs...
964 Then we can't tell whether it's a type signature or a value
965 definition with a result signature until we see the '='.
966 So we have to inline enough to postpone reductions until we know.
970 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
971 instead of qvar, we get another shift/reduce-conflict. Consider the
974 { (^^) :: Int->Int ; } Type signature; only var allowed
976 { (^^) :: Int->Int = ... ; } Value defn with result signature;
977 qvar allowed (because of instance decls)
979 We can't tell whether to reduce var to qvar until after we've read the signatures.
982 decl :: { Located (OrdList (LHsDecl RdrName)) }
984 | '!' infixexp rhs {% do { pat <- checkPattern $2;
985 return (LL $ unitOL $ LL $ ValD $
986 PatBind (LL $ BangPat pat) (unLoc $3)
987 placeHolderType placeHolderNames) } }
988 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
989 return (LL $ unitOL (LL $ ValD r)) } }
991 rhs :: { Located (GRHSs RdrName) }
992 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
993 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
995 gdrhs :: { Located [LGRHS RdrName] }
996 : gdrhs gdrh { LL ($2 : unLoc $1) }
999 gdrh :: { LGRHS RdrName }
1000 : '|' quals '=' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1002 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1003 : infixexp '::' sigtype
1004 {% do s <- checkValSig $1 $3;
1005 return (LL $ unitOL (LL $ SigD s)) }
1006 -- See the above notes for why we need infixexp here
1007 | var ',' sig_vars '::' sigtype
1008 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1009 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1011 | '{-# INLINE' activation qvar '#-}'
1012 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1013 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1014 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1016 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1017 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1019 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1020 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1022 -----------------------------------------------------------------------------
1025 exp :: { LHsExpr RdrName }
1026 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1027 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1028 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1029 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1030 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1033 infixexp :: { LHsExpr RdrName }
1035 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1037 exp10 :: { LHsExpr RdrName }
1038 : '\\' aexp aexps opt_asig '->' exp
1039 {% checkPatterns ($2 : reverse $3) >>= \ ps ->
1040 return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1041 (GRHSs (unguardedRHS $6) emptyLocalBinds
1043 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1044 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1045 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1046 | '-' fexp { LL $ mkHsNegApp $2 }
1048 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1049 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1050 return (L loc (mkHsDo DoExpr stmts body)) }
1051 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1052 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1053 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1054 | scc_annot exp { LL $ if opt_SccProfilingOn
1055 then HsSCC (unLoc $1) $2
1058 | 'proc' aexp '->' exp
1059 {% checkPattern $2 >>= \ p ->
1060 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1061 placeHolderType undefined)) }
1062 -- TODO: is LL right here?
1064 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1065 -- hdaume: core annotation
1068 scc_annot :: { Located FastString }
1069 : '_scc_' STRING { LL $ getSTRING $2 }
1070 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
1072 fexp :: { LHsExpr RdrName }
1073 : fexp aexp { LL $ HsApp $1 $2 }
1076 aexps :: { [LHsExpr RdrName] }
1077 : aexps aexp { $2 : $1 }
1078 | {- empty -} { [] }
1080 aexp :: { LHsExpr RdrName }
1081 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1082 | '~' aexp { LL $ ELazyPat $2 }
1083 -- | '!' aexp { LL $ EBangPat $2 }
1086 aexp1 :: { LHsExpr RdrName }
1087 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1092 -- Here was the syntax for type applications that I was planning
1093 -- but there are difficulties (e.g. what order for type args)
1094 -- so it's not enabled yet.
1095 -- But this case *is* used for the left hand side of a generic definition,
1096 -- which is parsed as an expression before being munged into a pattern
1097 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1098 (sL (getLoc $3) (HsType $3)) }
1100 aexp2 :: { LHsExpr RdrName }
1101 : ipvar { L1 (HsIPVar $! unLoc $1) }
1102 | qcname { L1 (HsVar $! unLoc $1) }
1103 | literal { L1 (HsLit $! unLoc $1) }
1104 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1105 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1106 | '(' exp ')' { LL (HsPar $2) }
1107 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1108 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1109 | '[' list ']' { LL (unLoc $2) }
1110 | '[:' parr ':]' { LL (unLoc $2) }
1111 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1112 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1113 | '_' { L1 EWildPat }
1115 -- Template Haskell Extension
1116 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1117 (L1 $ HsVar (mkUnqual varName
1118 (getTH_ID_SPLICE $1)))) } -- $x
1119 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1121 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1122 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1123 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1124 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1125 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1126 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1127 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1128 return (LL $ HsBracket (PatBr p)) }
1129 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBr (mkGroup $2)) }
1131 -- arrow notation extension
1132 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1134 cmdargs :: { [LHsCmdTop RdrName] }
1135 : cmdargs acmd { $2 : $1 }
1136 | {- empty -} { [] }
1138 acmd :: { LHsCmdTop RdrName }
1139 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1141 cvtopbody :: { [LHsDecl RdrName] }
1142 : '{' cvtopdecls0 '}' { $2 }
1143 | vocurly cvtopdecls0 close { $2 }
1145 cvtopdecls0 :: { [LHsDecl RdrName] }
1146 : {- empty -} { [] }
1149 texp :: { LHsExpr RdrName }
1151 | qopm infixexp { LL $ SectionR $1 $2 }
1152 -- The second production is really here only for bang patterns
1155 texps :: { [LHsExpr RdrName] }
1156 : texps ',' texp { $3 : $1 }
1160 -----------------------------------------------------------------------------
1163 -- The rules below are little bit contorted to keep lexps left-recursive while
1164 -- avoiding another shift/reduce-conflict.
1166 list :: { LHsExpr RdrName }
1167 : texp { L1 $ ExplicitList placeHolderType [$1] }
1168 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1169 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1170 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1171 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1172 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1173 | texp pquals { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1175 lexps :: { Located [LHsExpr RdrName] }
1176 : lexps ',' texp { LL ($3 : unLoc $1) }
1177 | texp ',' texp { LL [$3,$1] }
1179 -----------------------------------------------------------------------------
1180 -- List Comprehensions
1182 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1183 -- or a reversed list of Stmts
1184 : pquals1 { case unLoc $1 of
1186 qss -> L1 [L1 (ParStmt stmtss)]
1188 stmtss = [ (reverse qs, undefined)
1192 pquals1 :: { Located [[LStmt RdrName]] }
1193 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1194 | '|' quals { L (getLoc $2) [unLoc $2] }
1196 quals :: { Located [LStmt RdrName] }
1197 : quals ',' qual { LL ($3 : unLoc $1) }
1200 -----------------------------------------------------------------------------
1201 -- Parallel array expressions
1203 -- The rules below are little bit contorted; see the list case for details.
1204 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1205 -- Moreover, we allow explicit arrays with no element (represented by the nil
1206 -- constructor in the list case).
1208 parr :: { LHsExpr RdrName }
1209 : { noLoc (ExplicitPArr placeHolderType []) }
1210 | exp { L1 $ ExplicitPArr placeHolderType [$1] }
1211 | lexps { L1 $ ExplicitPArr placeHolderType
1212 (reverse (unLoc $1)) }
1213 | exp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1214 | exp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1215 | exp pquals { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1217 -- We are reusing `lexps' and `pquals' from the list case.
1219 -----------------------------------------------------------------------------
1220 -- Case alternatives
1222 altslist :: { Located [LMatch RdrName] }
1223 : '{' alts '}' { LL (reverse (unLoc $2)) }
1224 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1226 alts :: { Located [LMatch RdrName] }
1227 : alts1 { L1 (unLoc $1) }
1228 | ';' alts { LL (unLoc $2) }
1230 alts1 :: { Located [LMatch RdrName] }
1231 : alts1 ';' alt { LL ($3 : unLoc $1) }
1232 | alts1 ';' { LL (unLoc $1) }
1235 alt :: { LMatch RdrName }
1236 : infixexp opt_sig alt_rhs {% checkPattern $1 >>= \p ->
1237 return (LL (Match [p] $2 (unLoc $3))) }
1238 | '!' infixexp opt_sig alt_rhs {% checkPattern $2 >>= \p ->
1239 return (LL (Match [LL $ BangPat p] $3 (unLoc $4))) }
1241 alt_rhs :: { Located (GRHSs RdrName) }
1242 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1244 ralt :: { Located [LGRHS RdrName] }
1245 : '->' exp { LL (unguardedRHS $2) }
1246 | gdpats { L1 (reverse (unLoc $1)) }
1248 gdpats :: { Located [LGRHS RdrName] }
1249 : gdpats gdpat { LL ($2 : unLoc $1) }
1252 gdpat :: { LGRHS RdrName }
1253 : '|' quals '->' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1255 -----------------------------------------------------------------------------
1256 -- Statement sequences
1258 stmtlist :: { Located [LStmt RdrName] }
1259 : '{' stmts '}' { LL (unLoc $2) }
1260 | vocurly stmts close { $2 }
1262 -- do { ;; s ; s ; ; s ;; }
1263 -- The last Stmt should be an expression, but that's hard to enforce
1264 -- here, because we need too much lookahead if we see do { e ; }
1265 -- So we use ExprStmts throughout, and switch the last one over
1266 -- in ParseUtils.checkDo instead
1267 stmts :: { Located [LStmt RdrName] }
1268 : stmt stmts_help { LL ($1 : unLoc $2) }
1269 | ';' stmts { LL (unLoc $2) }
1270 | {- empty -} { noLoc [] }
1272 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1273 : ';' stmts { LL (unLoc $2) }
1274 | {- empty -} { noLoc [] }
1276 -- For typing stmts at the GHCi prompt, where
1277 -- the input may consist of just comments.
1278 maybe_stmt :: { Maybe (LStmt RdrName) }
1280 | {- nothing -} { Nothing }
1282 stmt :: { LStmt RdrName }
1284 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1285 return (LL $ mkBindStmt p $1) }
1286 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1288 qual :: { LStmt RdrName }
1289 : exp '<-' exp {% checkPattern $1 >>= \p ->
1290 return (LL $ mkBindStmt p $3) }
1291 | exp { L1 $ mkExprStmt $1 }
1292 | 'let' binds { LL $ LetStmt (unLoc $2) }
1294 -----------------------------------------------------------------------------
1295 -- Record Field Update/Construction
1297 fbinds :: { HsRecordBinds RdrName }
1299 | {- empty -} { [] }
1301 fbinds1 :: { HsRecordBinds RdrName }
1302 : fbinds1 ',' fbind { $3 : $1 }
1305 fbind :: { (Located RdrName, LHsExpr RdrName) }
1306 : qvar '=' exp { ($1,$3) }
1308 -----------------------------------------------------------------------------
1309 -- Implicit Parameter Bindings
1311 dbinds :: { Located [LIPBind RdrName] }
1312 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1313 | dbinds ';' { LL (unLoc $1) }
1315 -- | {- empty -} { [] }
1317 dbind :: { LIPBind RdrName }
1318 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1320 ipvar :: { Located (IPName RdrName) }
1321 : IPDUPVARID { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1322 | IPSPLITVARID { L1 (Linear (mkUnqual varName (getIPSPLITVARID $1))) }
1324 -----------------------------------------------------------------------------
1327 depreclist :: { Located [RdrName] }
1328 depreclist : deprec_var { L1 [unLoc $1] }
1329 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1331 deprec_var :: { Located RdrName }
1332 deprec_var : var { $1 }
1335 -----------------------------------------
1336 -- Data constructors
1337 qcon :: { Located RdrName }
1339 | '(' qconsym ')' { LL (unLoc $2) }
1340 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1341 -- The case of '[:' ':]' is part of the production `parr'
1343 con :: { Located RdrName }
1345 | '(' consym ')' { LL (unLoc $2) }
1346 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1348 sysdcon :: { Located DataCon } -- Wired in data constructors
1349 : '(' ')' { LL unitDataCon }
1350 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1351 | '[' ']' { LL nilDataCon }
1353 conop :: { Located RdrName }
1355 | '`' conid '`' { LL (unLoc $2) }
1357 qconop :: { Located RdrName }
1359 | '`' qconid '`' { LL (unLoc $2) }
1361 -----------------------------------------------------------------------------
1362 -- Type constructors
1364 gtycon :: { Located RdrName } -- A "general" qualified tycon
1366 | '(' ')' { LL $ getRdrName unitTyCon }
1367 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1368 | '(' '->' ')' { LL $ getRdrName funTyCon }
1369 | '[' ']' { LL $ listTyCon_RDR }
1370 | '[:' ':]' { LL $ parrTyCon_RDR }
1372 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1374 | '(' qtyconsym ')' { LL (unLoc $2) }
1376 qtyconop :: { Located RdrName } -- Qualified or unqualified
1378 | '`' qtycon '`' { LL (unLoc $2) }
1380 qtycon :: { Located RdrName } -- Qualified or unqualified
1381 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1384 tycon :: { Located RdrName } -- Unqualified
1385 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1387 qtyconsym :: { Located RdrName }
1388 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1391 tyconsym :: { Located RdrName }
1392 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1394 -----------------------------------------------------------------------------
1397 op :: { Located RdrName } -- used in infix decls
1401 varop :: { Located RdrName }
1403 | '`' varid '`' { LL (unLoc $2) }
1405 qop :: { LHsExpr RdrName } -- used in sections
1406 : qvarop { L1 $ HsVar (unLoc $1) }
1407 | qconop { L1 $ HsVar (unLoc $1) }
1409 qopm :: { LHsExpr RdrName } -- used in sections
1410 : qvaropm { L1 $ HsVar (unLoc $1) }
1411 | qconop { L1 $ HsVar (unLoc $1) }
1413 qvarop :: { Located RdrName }
1415 | '`' qvarid '`' { LL (unLoc $2) }
1417 qvaropm :: { Located RdrName }
1418 : qvarsym_no_minus { $1 }
1419 | '`' qvarid '`' { LL (unLoc $2) }
1421 -----------------------------------------------------------------------------
1424 tyvar :: { Located RdrName }
1425 tyvar : tyvarid { $1 }
1426 | '(' tyvarsym ')' { LL (unLoc $2) }
1428 tyvarop :: { Located RdrName }
1429 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1432 tyvarid :: { Located RdrName }
1433 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1434 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1435 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1436 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1437 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1439 tyvarsym :: { Located RdrName }
1440 -- Does not include "!", because that is used for strictness marks
1441 -- or ".", because that separates the quantified type vars from the rest
1442 -- or "*", because that's used for kinds
1443 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1445 -----------------------------------------------------------------------------
1448 var :: { Located RdrName }
1450 | '(' varsym ')' { LL (unLoc $2) }
1452 qvar :: { Located RdrName }
1454 | '(' varsym ')' { LL (unLoc $2) }
1455 | '(' qvarsym1 ')' { LL (unLoc $2) }
1456 -- We've inlined qvarsym here so that the decision about
1457 -- whether it's a qvar or a var can be postponed until
1458 -- *after* we see the close paren.
1460 qvarid :: { Located RdrName }
1462 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1464 varid :: { Located RdrName }
1465 : varid_no_unsafe { $1 }
1466 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1467 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1468 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1470 varid_no_unsafe :: { Located RdrName }
1471 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1472 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1473 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1474 | 'iso' { L1 $! mkUnqual varName FSLIT("iso") }
1475 | 'family' { L1 $! mkUnqual varName FSLIT("family") }
1477 qvarsym :: { Located RdrName }
1481 qvarsym_no_minus :: { Located RdrName }
1482 : varsym_no_minus { $1 }
1485 qvarsym1 :: { Located RdrName }
1486 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1488 varsym :: { Located RdrName }
1489 : varsym_no_minus { $1 }
1490 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1492 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1493 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1494 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1497 -- These special_ids are treated as keywords in various places,
1498 -- but as ordinary ids elsewhere. 'special_id' collects all these
1499 -- except 'unsafe', 'forall', 'family', and 'iso' whose treatment differs
1500 -- depending on context
1501 special_id :: { Located FastString }
1503 : 'as' { L1 FSLIT("as") }
1504 | 'qualified' { L1 FSLIT("qualified") }
1505 | 'hiding' { L1 FSLIT("hiding") }
1506 | 'export' { L1 FSLIT("export") }
1507 | 'label' { L1 FSLIT("label") }
1508 | 'dynamic' { L1 FSLIT("dynamic") }
1509 | 'stdcall' { L1 FSLIT("stdcall") }
1510 | 'ccall' { L1 FSLIT("ccall") }
1512 special_sym :: { Located FastString }
1513 special_sym : '!' { L1 FSLIT("!") }
1514 | '.' { L1 FSLIT(".") }
1515 | '*' { L1 FSLIT("*") }
1517 -----------------------------------------------------------------------------
1518 -- Data constructors
1520 qconid :: { Located RdrName } -- Qualified or unqualified
1522 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1524 conid :: { Located RdrName }
1525 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1527 qconsym :: { Located RdrName } -- Qualified or unqualified
1529 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1531 consym :: { Located RdrName }
1532 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1534 -- ':' means only list cons
1535 | ':' { L1 $ consDataCon_RDR }
1538 -----------------------------------------------------------------------------
1541 literal :: { Located HsLit }
1542 : CHAR { L1 $ HsChar $ getCHAR $1 }
1543 | STRING { L1 $ HsString $ getSTRING $1 }
1544 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1545 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1546 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1547 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1548 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1550 -----------------------------------------------------------------------------
1554 : vccurly { () } -- context popped in lexer.
1555 | error {% popContext }
1557 -----------------------------------------------------------------------------
1558 -- Miscellaneous (mostly renamings)
1560 modid :: { Located ModuleName }
1561 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1562 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1565 (unpackFS mod ++ '.':unpackFS c))
1569 : commas ',' { $1 + 1 }
1572 -----------------------------------------------------------------------------
1576 happyError = srcParseFail
1578 getVARID (L _ (ITvarid x)) = x
1579 getCONID (L _ (ITconid x)) = x
1580 getVARSYM (L _ (ITvarsym x)) = x
1581 getCONSYM (L _ (ITconsym x)) = x
1582 getQVARID (L _ (ITqvarid x)) = x
1583 getQCONID (L _ (ITqconid x)) = x
1584 getQVARSYM (L _ (ITqvarsym x)) = x
1585 getQCONSYM (L _ (ITqconsym x)) = x
1586 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1587 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1588 getCHAR (L _ (ITchar x)) = x
1589 getSTRING (L _ (ITstring x)) = x
1590 getINTEGER (L _ (ITinteger x)) = x
1591 getRATIONAL (L _ (ITrational x)) = x
1592 getPRIMCHAR (L _ (ITprimchar x)) = x
1593 getPRIMSTRING (L _ (ITprimstring x)) = x
1594 getPRIMINTEGER (L _ (ITprimint x)) = x
1595 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1596 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1597 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1598 getINLINE (L _ (ITinline_prag b)) = b
1599 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1601 -- Utilities for combining source spans
1602 comb2 :: Located a -> Located b -> SrcSpan
1605 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1606 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1608 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1609 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1610 combineSrcSpans (getLoc c) (getLoc d)
1612 -- strict constructor version:
1614 sL :: SrcSpan -> a -> Located a
1615 sL span a = span `seq` L span a
1617 -- Make a source location for the file. We're a bit lazy here and just
1618 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1619 -- try to find the span of the whole file (ToDo).
1620 fileSrcSpan :: P SrcSpan
1623 let loc = mkSrcLoc (srcLocFile l) 1 0;
1624 return (mkSrcSpan loc loc)