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' type opt_kind_sig
506 -- Note the use of type for the head; this allows
507 -- infix type constructors to be declared
509 {% do { (tc, tvs, _) <- checkSynHdr $3 False
510 ; let kind = case unLoc $4 of
511 Nothing -> liftedTypeKind
513 ; return (L (comb3 $1 $3 $4)
514 (TyFunction tc tvs False kind))
517 -- type instance declarations
518 | 'type' 'instance' type '=' ctype
519 -- Note the use of type for the head; this allows
520 -- infix type constructors and type patterns
522 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
523 ; return (L (comb2 $1 $5)
524 (TySynonym tc tvs (Just typats) $5))
527 -- ordinary data type or newtype declaration
528 | data_or_newtype tycl_hdr constrs deriving
529 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
530 ; checkTyVars tparms -- no type pattern
532 L (comb4 $1 $2 $3 $4)
533 -- We need the location on tycl_hdr in case
534 -- constrs and deriving are both empty
535 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
536 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
538 -- ordinary GADT declaration
539 | data_or_newtype tycl_hdr opt_kind_sig
540 'where' gadt_constrlist
542 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
543 ; checkTyVars tparms -- can have type pats
545 L (comb4 $1 $2 $4 $5)
546 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
547 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
549 -- data/newtype family
550 | data_or_newtype 'family' tycl_hdr opt_kind_sig
551 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
552 ; checkTyVars tparms -- no type pattern
553 ; let kind = case unLoc $4 of
554 Nothing -> liftedTypeKind
558 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
559 (Just kind) [] Nothing) } }
561 -- data/newtype instance declaration
562 | data_or_newtype 'instance' tycl_hdr constrs deriving
563 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
564 -- can have type pats
566 L (comb4 $1 $3 $4 $5)
567 -- We need the location on tycl_hdr in case
568 -- constrs and deriving are both empty
569 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
570 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
572 -- GADT instance declaration
573 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
574 'where' gadt_constrlist
576 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
577 -- can have type pats
579 L (comb4 $1 $3 $6 $7)
580 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
581 (unLoc $4) (reverse (unLoc $6)) (unLoc $7)) } }
583 -- Associate type declarations
585 at_decl :: { LTyClDecl RdrName }
586 -- type family declarations
587 : 'type' type opt_kind_sig
588 -- Note the use of type for the head; this allows
589 -- infix type constructors to be declared
591 {% do { (tc, tvs, _) <- checkSynHdr $2 False
592 ; let kind = case unLoc $3 of
593 Nothing -> liftedTypeKind
595 ; return (L (comb3 $1 $2 $3)
596 (TyFunction tc tvs False kind))
599 -- type instance declarations
600 | 'type' type '=' ctype
601 -- Note the use of type for the head; this allows
602 -- infix type constructors and type patterns
604 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
605 ; return (L (comb2 $1 $4)
606 (TySynonym tc tvs (Just typats) $4))
609 -- data/newtype family
610 | data_or_newtype tycl_hdr '::' kind
611 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
612 ; checkTyVars tparms -- no type pattern
615 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
616 (Just (unLoc $4)) [] Nothing) } }
618 -- data/newtype instance declaration
619 | data_or_newtype tycl_hdr constrs deriving
620 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
621 -- can have type pats
623 L (comb4 $1 $2 $3 $4)
624 -- We need the location on tycl_hdr in case
625 -- constrs and deriving are both empty
626 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
627 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
629 -- GADT instance declaration
630 | data_or_newtype tycl_hdr opt_kind_sig
631 'where' gadt_constrlist
633 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
634 -- can have type pats
636 L (comb4 $1 $2 $5 $6)
637 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
638 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
644 data_or_newtype :: { Located NewOrData }
645 : 'data' { L1 DataType }
646 | 'newtype' { L1 NewType }
648 opt_kind_sig :: { Located (Maybe Kind) }
650 | '::' kind { LL (Just (unLoc $2)) }
652 -- tycl_hdr parses the header of a class or data type decl,
653 -- which takes the form
656 -- (Eq a, Ord b) => T a b
657 -- T Int [a] -- for associated types
658 -- Rather a lot of inlining here, else we get reduce/reduce errors
659 tycl_hdr :: { Located (LHsContext RdrName,
661 [LHsTyVarBndr RdrName],
663 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
664 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
666 -----------------------------------------------------------------------------
667 -- Nested declarations
669 -- Type declaration or value declaration
671 tydecl :: { Located (OrdList (LHsDecl RdrName)) }
672 tydecl : at_decl { LL (unitOL (L1 (TyClD (unLoc $1)))) }
675 tydecls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
676 : tydecls ';' tydecl { LL (unLoc $1 `appOL` unLoc $3) }
677 | tydecls ';' { LL (unLoc $1) }
679 | {- empty -} { noLoc nilOL }
683 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
684 : '{' tydecls '}' { LL (unLoc $2) }
685 | vocurly tydecls close { $2 }
687 -- Form of the body of class and instance declarations
689 where :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
690 -- No implicit parameters
691 -- May have type declarations
692 : 'where' tydecllist { LL (unLoc $2) }
693 | {- empty -} { noLoc nilOL }
695 decls :: { Located (OrdList (LHsDecl RdrName)) }
696 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
697 | decls ';' { LL (unLoc $1) }
699 | {- empty -} { noLoc nilOL }
702 decllist :: { Located (OrdList (LHsDecl RdrName)) }
703 : '{' decls '}' { LL (unLoc $2) }
704 | vocurly decls close { $2 }
706 -- Binding groups other than those of class and instance declarations
708 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
709 -- No type declarations
710 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
711 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
712 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
714 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
715 -- No type declarations
716 : 'where' binds { LL (unLoc $2) }
717 | {- empty -} { noLoc emptyLocalBinds }
720 -----------------------------------------------------------------------------
721 -- Transformation Rules
723 rules :: { OrdList (LHsDecl RdrName) }
724 : rules ';' rule { $1 `snocOL` $3 }
727 | {- empty -} { nilOL }
729 rule :: { LHsDecl RdrName }
730 : STRING activation rule_forall infixexp '=' exp
731 { LL $ RuleD (HsRule (getSTRING $1)
732 ($2 `orElse` AlwaysActive)
733 $3 $4 placeHolderNames $6 placeHolderNames) }
735 activation :: { Maybe Activation }
736 : {- empty -} { Nothing }
737 | explicit_activation { Just $1 }
739 explicit_activation :: { Activation } -- In brackets
740 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
741 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
743 rule_forall :: { [RuleBndr RdrName] }
744 : 'forall' rule_var_list '.' { $2 }
747 rule_var_list :: { [RuleBndr RdrName] }
749 | rule_var rule_var_list { $1 : $2 }
751 rule_var :: { RuleBndr RdrName }
752 : varid { RuleBndr $1 }
753 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
755 -----------------------------------------------------------------------------
756 -- Deprecations (c.f. rules)
758 deprecations :: { OrdList (LHsDecl RdrName) }
759 : deprecations ';' deprecation { $1 `appOL` $3 }
760 | deprecations ';' { $1 }
762 | {- empty -} { nilOL }
764 -- SUP: TEMPORARY HACK, not checking for `module Foo'
765 deprecation :: { OrdList (LHsDecl RdrName) }
767 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
771 -----------------------------------------------------------------------------
772 -- Foreign import and export declarations
774 fdecl :: { LHsDecl RdrName }
775 fdecl : 'import' callconv safety fspec
776 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
777 | 'import' callconv fspec
778 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
780 | 'export' callconv fspec
781 {% mkExport $2 (unLoc $3) >>= return.LL }
783 callconv :: { CallConv }
784 : 'stdcall' { CCall StdCallConv }
785 | 'ccall' { CCall CCallConv }
786 | 'dotnet' { DNCall }
789 : 'unsafe' { PlayRisky }
790 | 'safe' { PlaySafe False }
791 | 'threadsafe' { PlaySafe True }
793 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
794 : STRING var '::' sigtype { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
795 | var '::' sigtype { LL (noLoc nilFS, $1, $3) }
796 -- if the entity string is missing, it defaults to the empty string;
797 -- the meaning of an empty entity string depends on the calling
800 -----------------------------------------------------------------------------
803 opt_sig :: { Maybe (LHsType RdrName) }
804 : {- empty -} { Nothing }
805 | '::' sigtype { Just $2 }
807 opt_asig :: { Maybe (LHsType RdrName) }
808 : {- empty -} { Nothing }
809 | '::' atype { Just $2 }
811 sigtypes1 :: { [LHsType RdrName] }
813 | sigtype ',' sigtypes1 { $1 : $3 }
815 sigtype :: { LHsType RdrName }
816 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
817 -- Wrap an Implicit forall if there isn't one there already
819 sig_vars :: { Located [Located RdrName] }
820 : sig_vars ',' var { LL ($3 : unLoc $1) }
823 -----------------------------------------------------------------------------
826 strict_mark :: { Located HsBang }
827 : '!' { L1 HsStrict }
828 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
830 -- A ctype is a for-all type
831 ctype :: { LHsType RdrName }
832 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
833 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
834 -- A type of form (context => type) is an *implicit* HsForAllTy
837 -- We parse a context as a btype so that we don't get reduce/reduce
838 -- errors in ctype. The basic problem is that
840 -- looks so much like a tuple type. We can't tell until we find the =>
841 context :: { LHsContext RdrName }
842 : btype {% checkContext $1 }
844 type :: { LHsType RdrName }
845 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
848 gentype :: { LHsType RdrName }
850 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
851 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
852 | btype '->' ctype { LL $ HsFunTy $1 $3 }
854 btype :: { LHsType RdrName }
855 : btype atype { LL $ HsAppTy $1 $2 }
858 atype :: { LHsType RdrName }
859 : gtycon { L1 (HsTyVar (unLoc $1)) }
860 | tyvar { L1 (HsTyVar (unLoc $1)) }
861 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
862 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
863 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
864 | '[' ctype ']' { LL $ HsListTy $2 }
865 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
866 | '(' ctype ')' { LL $ HsParTy $2 }
867 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
869 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
871 -- An inst_type is what occurs in the head of an instance decl
872 -- e.g. (Foo a, Gaz b) => Wibble a b
873 -- It's kept as a single type, with a MonoDictTy at the right
874 -- hand corner, for convenience.
875 inst_type :: { LHsType RdrName }
876 : sigtype {% checkInstType $1 }
878 inst_types1 :: { [LHsType RdrName] }
880 | inst_type ',' inst_types1 { $1 : $3 }
882 comma_types0 :: { [LHsType RdrName] }
883 : comma_types1 { $1 }
886 comma_types1 :: { [LHsType RdrName] }
888 | ctype ',' comma_types1 { $1 : $3 }
890 tv_bndrs :: { [LHsTyVarBndr RdrName] }
891 : tv_bndr tv_bndrs { $1 : $2 }
894 tv_bndr :: { LHsTyVarBndr RdrName }
895 : tyvar { L1 (UserTyVar (unLoc $1)) }
896 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
899 fds :: { Located [Located ([RdrName], [RdrName])] }
900 : {- empty -} { noLoc [] }
901 | '|' fds1 { LL (reverse (unLoc $2)) }
903 fds1 :: { Located [Located ([RdrName], [RdrName])] }
904 : fds1 ',' fd { LL ($3 : unLoc $1) }
907 fd :: { Located ([RdrName], [RdrName]) }
908 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
909 (reverse (unLoc $1), reverse (unLoc $3)) }
911 varids0 :: { Located [RdrName] }
912 : {- empty -} { noLoc [] }
913 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
915 -----------------------------------------------------------------------------
918 kind :: { Located Kind }
920 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
922 akind :: { Located Kind }
923 : '*' { L1 liftedTypeKind }
924 | '!' { L1 unliftedTypeKind }
925 | '(' kind ')' { LL (unLoc $2) }
928 -----------------------------------------------------------------------------
929 -- Datatype declarations
931 gadt_constrlist :: { Located [LConDecl RdrName] }
932 : '{' gadt_constrs '}' { LL (unLoc $2) }
933 | vocurly gadt_constrs close { $2 }
935 gadt_constrs :: { Located [LConDecl RdrName] }
936 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
937 | gadt_constrs ';' { $1 }
938 | gadt_constr { L1 [$1] }
940 -- We allow the following forms:
941 -- C :: Eq a => a -> T a
942 -- C :: forall a. Eq a => !a -> T a
943 -- D { x,y :: a } :: T a
944 -- forall a. Eq a => D { x,y :: a } :: T a
946 gadt_constr :: { LConDecl RdrName }
948 { LL (mkGadtDecl $1 $3) }
949 -- Syntax: Maybe merge the record stuff with the single-case above?
950 -- (to kill the mostly harmless reduce/reduce error)
951 -- XXX revisit audreyt
952 | constr_stuff_record '::' sigtype
953 { let (con,details) = unLoc $1 in
954 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
956 | forall context '=>' constr_stuff_record '::' sigtype
957 { let (con,details) = unLoc $4 in
958 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
959 | forall constr_stuff_record '::' sigtype
960 { let (con,details) = unLoc $2 in
961 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
965 constrs :: { Located [LConDecl RdrName] }
966 : {- empty; a GHC extension -} { noLoc [] }
967 | '=' constrs1 { LL (unLoc $2) }
969 constrs1 :: { Located [LConDecl RdrName] }
970 : constrs1 '|' constr { LL ($3 : unLoc $1) }
973 constr :: { LConDecl RdrName }
974 : forall context '=>' constr_stuff
975 { let (con,details) = unLoc $4 in
976 LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
977 | forall constr_stuff
978 { let (con,details) = unLoc $2 in
979 LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
981 forall :: { Located [LHsTyVarBndr RdrName] }
982 : 'forall' tv_bndrs '.' { LL $2 }
983 | {- empty -} { noLoc [] }
985 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
986 -- We parse the constructor declaration
988 -- as a btype (treating C as a type constructor) and then convert C to be
989 -- a data constructor. Reason: it might continue like this:
991 -- in which case C really would be a type constructor. We can't resolve this
992 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
993 : btype {% mkPrefixCon $1 [] >>= return.LL }
994 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
995 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
996 | btype conop btype { LL ($2, InfixCon $1 $3) }
998 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
999 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
1000 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
1002 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
1003 : fielddecl ',' fielddecls { unLoc $1 : $3 }
1004 | fielddecl { [unLoc $1] }
1006 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
1007 : sig_vars '::' ctype { LL (reverse (unLoc $1), $3) }
1009 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1010 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1011 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1012 -- We don't allow a context, but that's sorted out by the type checker.
1013 deriving :: { Located (Maybe [LHsType RdrName]) }
1014 : {- empty -} { noLoc Nothing }
1015 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1016 ; p <- checkInstType (L loc (HsTyVar tv))
1017 ; return (LL (Just [p])) } }
1018 | 'deriving' '(' ')' { LL (Just []) }
1019 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1020 -- Glasgow extension: allow partial
1021 -- applications in derivings
1023 -----------------------------------------------------------------------------
1024 -- Value definitions
1026 {- There's an awkward overlap with a type signature. Consider
1027 f :: Int -> Int = ...rhs...
1028 Then we can't tell whether it's a type signature or a value
1029 definition with a result signature until we see the '='.
1030 So we have to inline enough to postpone reductions until we know.
1034 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1035 instead of qvar, we get another shift/reduce-conflict. Consider the
1038 { (^^) :: Int->Int ; } Type signature; only var allowed
1040 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1041 qvar allowed (because of instance decls)
1043 We can't tell whether to reduce var to qvar until after we've read the signatures.
1046 decl :: { Located (OrdList (LHsDecl RdrName)) }
1048 | '!' infixexp rhs {% do { pat <- checkPattern $2;
1049 return (LL $ unitOL $ LL $ ValD $
1050 PatBind (LL $ BangPat pat) (unLoc $3)
1051 placeHolderType placeHolderNames) } }
1052 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1053 return (LL $ unitOL (LL $ ValD r)) } }
1055 rhs :: { Located (GRHSs RdrName) }
1056 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1057 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1059 gdrhs :: { Located [LGRHS RdrName] }
1060 : gdrhs gdrh { LL ($2 : unLoc $1) }
1063 gdrh :: { LGRHS RdrName }
1064 : '|' quals '=' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1066 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1067 : infixexp '::' sigtype
1068 {% do s <- checkValSig $1 $3;
1069 return (LL $ unitOL (LL $ SigD s)) }
1070 -- See the above notes for why we need infixexp here
1071 | var ',' sig_vars '::' sigtype
1072 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1073 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1075 | '{-# INLINE' activation qvar '#-}'
1076 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1077 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1078 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1080 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1081 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1083 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1084 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1086 -----------------------------------------------------------------------------
1089 exp :: { LHsExpr RdrName }
1090 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1091 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1092 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1093 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1094 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1097 infixexp :: { LHsExpr RdrName }
1099 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1101 exp10 :: { LHsExpr RdrName }
1102 : '\\' aexp aexps opt_asig '->' exp
1103 {% checkPatterns ($2 : reverse $3) >>= \ ps ->
1104 return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1105 (GRHSs (unguardedRHS $6) emptyLocalBinds
1107 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1108 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1109 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1110 | '-' fexp { LL $ mkHsNegApp $2 }
1112 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1113 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1114 return (L loc (mkHsDo DoExpr stmts body)) }
1115 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1116 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1117 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1118 | scc_annot exp { LL $ if opt_SccProfilingOn
1119 then HsSCC (unLoc $1) $2
1122 | 'proc' aexp '->' exp
1123 {% checkPattern $2 >>= \ p ->
1124 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1125 placeHolderType undefined)) }
1126 -- TODO: is LL right here?
1128 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1129 -- hdaume: core annotation
1132 scc_annot :: { Located FastString }
1133 : '_scc_' STRING { LL $ getSTRING $2 }
1134 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
1136 fexp :: { LHsExpr RdrName }
1137 : fexp aexp { LL $ HsApp $1 $2 }
1140 aexps :: { [LHsExpr RdrName] }
1141 : aexps aexp { $2 : $1 }
1142 | {- empty -} { [] }
1144 aexp :: { LHsExpr RdrName }
1145 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1146 | '~' aexp { LL $ ELazyPat $2 }
1147 -- | '!' aexp { LL $ EBangPat $2 }
1150 aexp1 :: { LHsExpr RdrName }
1151 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1156 -- Here was the syntax for type applications that I was planning
1157 -- but there are difficulties (e.g. what order for type args)
1158 -- so it's not enabled yet.
1159 -- But this case *is* used for the left hand side of a generic definition,
1160 -- which is parsed as an expression before being munged into a pattern
1161 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1162 (sL (getLoc $3) (HsType $3)) }
1164 aexp2 :: { LHsExpr RdrName }
1165 : ipvar { L1 (HsIPVar $! unLoc $1) }
1166 | qcname { L1 (HsVar $! unLoc $1) }
1167 | literal { L1 (HsLit $! unLoc $1) }
1168 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1169 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1170 | '(' exp ')' { LL (HsPar $2) }
1171 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1172 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1173 | '[' list ']' { LL (unLoc $2) }
1174 | '[:' parr ':]' { LL (unLoc $2) }
1175 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1176 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1177 | '_' { L1 EWildPat }
1179 -- Template Haskell Extension
1180 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1181 (L1 $ HsVar (mkUnqual varName
1182 (getTH_ID_SPLICE $1)))) } -- $x
1183 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1185 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1186 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1187 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1188 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1189 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1190 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1191 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1192 return (LL $ HsBracket (PatBr p)) }
1193 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBr (mkGroup $2)) }
1195 -- arrow notation extension
1196 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1198 cmdargs :: { [LHsCmdTop RdrName] }
1199 : cmdargs acmd { $2 : $1 }
1200 | {- empty -} { [] }
1202 acmd :: { LHsCmdTop RdrName }
1203 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1205 cvtopbody :: { [LHsDecl RdrName] }
1206 : '{' cvtopdecls0 '}' { $2 }
1207 | vocurly cvtopdecls0 close { $2 }
1209 cvtopdecls0 :: { [LHsDecl RdrName] }
1210 : {- empty -} { [] }
1213 texp :: { LHsExpr RdrName }
1215 | qopm infixexp { LL $ SectionR $1 $2 }
1216 -- The second production is really here only for bang patterns
1219 texps :: { [LHsExpr RdrName] }
1220 : texps ',' texp { $3 : $1 }
1224 -----------------------------------------------------------------------------
1227 -- The rules below are little bit contorted to keep lexps left-recursive while
1228 -- avoiding another shift/reduce-conflict.
1230 list :: { LHsExpr RdrName }
1231 : texp { L1 $ ExplicitList placeHolderType [$1] }
1232 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1233 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1234 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1235 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1236 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1237 | texp pquals { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1239 lexps :: { Located [LHsExpr RdrName] }
1240 : lexps ',' texp { LL ($3 : unLoc $1) }
1241 | texp ',' texp { LL [$3,$1] }
1243 -----------------------------------------------------------------------------
1244 -- List Comprehensions
1246 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1247 -- or a reversed list of Stmts
1248 : pquals1 { case unLoc $1 of
1250 qss -> L1 [L1 (ParStmt stmtss)]
1252 stmtss = [ (reverse qs, undefined)
1256 pquals1 :: { Located [[LStmt RdrName]] }
1257 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1258 | '|' quals { L (getLoc $2) [unLoc $2] }
1260 quals :: { Located [LStmt RdrName] }
1261 : quals ',' qual { LL ($3 : unLoc $1) }
1264 -----------------------------------------------------------------------------
1265 -- Parallel array expressions
1267 -- The rules below are little bit contorted; see the list case for details.
1268 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1269 -- Moreover, we allow explicit arrays with no element (represented by the nil
1270 -- constructor in the list case).
1272 parr :: { LHsExpr RdrName }
1273 : { noLoc (ExplicitPArr placeHolderType []) }
1274 | exp { L1 $ ExplicitPArr placeHolderType [$1] }
1275 | lexps { L1 $ ExplicitPArr placeHolderType
1276 (reverse (unLoc $1)) }
1277 | exp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1278 | exp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1279 | exp pquals { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1281 -- We are reusing `lexps' and `pquals' from the list case.
1283 -----------------------------------------------------------------------------
1284 -- Case alternatives
1286 altslist :: { Located [LMatch RdrName] }
1287 : '{' alts '}' { LL (reverse (unLoc $2)) }
1288 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1290 alts :: { Located [LMatch RdrName] }
1291 : alts1 { L1 (unLoc $1) }
1292 | ';' alts { LL (unLoc $2) }
1294 alts1 :: { Located [LMatch RdrName] }
1295 : alts1 ';' alt { LL ($3 : unLoc $1) }
1296 | alts1 ';' { LL (unLoc $1) }
1299 alt :: { LMatch RdrName }
1300 : infixexp opt_sig alt_rhs {% checkPattern $1 >>= \p ->
1301 return (LL (Match [p] $2 (unLoc $3))) }
1302 | '!' infixexp opt_sig alt_rhs {% checkPattern $2 >>= \p ->
1303 return (LL (Match [LL $ BangPat p] $3 (unLoc $4))) }
1305 alt_rhs :: { Located (GRHSs RdrName) }
1306 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1308 ralt :: { Located [LGRHS RdrName] }
1309 : '->' exp { LL (unguardedRHS $2) }
1310 | gdpats { L1 (reverse (unLoc $1)) }
1312 gdpats :: { Located [LGRHS RdrName] }
1313 : gdpats gdpat { LL ($2 : unLoc $1) }
1316 gdpat :: { LGRHS RdrName }
1317 : '|' quals '->' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1319 -----------------------------------------------------------------------------
1320 -- Statement sequences
1322 stmtlist :: { Located [LStmt RdrName] }
1323 : '{' stmts '}' { LL (unLoc $2) }
1324 | vocurly stmts close { $2 }
1326 -- do { ;; s ; s ; ; s ;; }
1327 -- The last Stmt should be an expression, but that's hard to enforce
1328 -- here, because we need too much lookahead if we see do { e ; }
1329 -- So we use ExprStmts throughout, and switch the last one over
1330 -- in ParseUtils.checkDo instead
1331 stmts :: { Located [LStmt RdrName] }
1332 : stmt stmts_help { LL ($1 : unLoc $2) }
1333 | ';' stmts { LL (unLoc $2) }
1334 | {- empty -} { noLoc [] }
1336 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1337 : ';' stmts { LL (unLoc $2) }
1338 | {- empty -} { noLoc [] }
1340 -- For typing stmts at the GHCi prompt, where
1341 -- the input may consist of just comments.
1342 maybe_stmt :: { Maybe (LStmt RdrName) }
1344 | {- nothing -} { Nothing }
1346 stmt :: { LStmt RdrName }
1348 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1349 return (LL $ mkBindStmt p $1) }
1350 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1352 qual :: { LStmt RdrName }
1353 : exp '<-' exp {% checkPattern $1 >>= \p ->
1354 return (LL $ mkBindStmt p $3) }
1355 | exp { L1 $ mkExprStmt $1 }
1356 | 'let' binds { LL $ LetStmt (unLoc $2) }
1358 -----------------------------------------------------------------------------
1359 -- Record Field Update/Construction
1361 fbinds :: { HsRecordBinds RdrName }
1363 | {- empty -} { [] }
1365 fbinds1 :: { HsRecordBinds RdrName }
1366 : fbinds1 ',' fbind { $3 : $1 }
1369 fbind :: { (Located RdrName, LHsExpr RdrName) }
1370 : qvar '=' exp { ($1,$3) }
1372 -----------------------------------------------------------------------------
1373 -- Implicit Parameter Bindings
1375 dbinds :: { Located [LIPBind RdrName] }
1376 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1377 | dbinds ';' { LL (unLoc $1) }
1379 -- | {- empty -} { [] }
1381 dbind :: { LIPBind RdrName }
1382 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1384 ipvar :: { Located (IPName RdrName) }
1385 : IPDUPVARID { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1386 | IPSPLITVARID { L1 (Linear (mkUnqual varName (getIPSPLITVARID $1))) }
1388 -----------------------------------------------------------------------------
1391 depreclist :: { Located [RdrName] }
1392 depreclist : deprec_var { L1 [unLoc $1] }
1393 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1395 deprec_var :: { Located RdrName }
1396 deprec_var : var { $1 }
1399 -----------------------------------------
1400 -- Data constructors
1401 qcon :: { Located RdrName }
1403 | '(' qconsym ')' { LL (unLoc $2) }
1404 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1405 -- The case of '[:' ':]' is part of the production `parr'
1407 con :: { Located RdrName }
1409 | '(' consym ')' { LL (unLoc $2) }
1410 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1412 sysdcon :: { Located DataCon } -- Wired in data constructors
1413 : '(' ')' { LL unitDataCon }
1414 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1415 | '[' ']' { LL nilDataCon }
1417 conop :: { Located RdrName }
1419 | '`' conid '`' { LL (unLoc $2) }
1421 qconop :: { Located RdrName }
1423 | '`' qconid '`' { LL (unLoc $2) }
1425 -----------------------------------------------------------------------------
1426 -- Type constructors
1428 gtycon :: { Located RdrName } -- A "general" qualified tycon
1430 | '(' ')' { LL $ getRdrName unitTyCon }
1431 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1432 | '(' '->' ')' { LL $ getRdrName funTyCon }
1433 | '[' ']' { LL $ listTyCon_RDR }
1434 | '[:' ':]' { LL $ parrTyCon_RDR }
1436 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1438 | '(' qtyconsym ')' { LL (unLoc $2) }
1440 qtyconop :: { Located RdrName } -- Qualified or unqualified
1442 | '`' qtycon '`' { LL (unLoc $2) }
1444 qtycon :: { Located RdrName } -- Qualified or unqualified
1445 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1448 tycon :: { Located RdrName } -- Unqualified
1449 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1451 qtyconsym :: { Located RdrName }
1452 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1455 tyconsym :: { Located RdrName }
1456 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1458 -----------------------------------------------------------------------------
1461 op :: { Located RdrName } -- used in infix decls
1465 varop :: { Located RdrName }
1467 | '`' varid '`' { LL (unLoc $2) }
1469 qop :: { LHsExpr RdrName } -- used in sections
1470 : qvarop { L1 $ HsVar (unLoc $1) }
1471 | qconop { L1 $ HsVar (unLoc $1) }
1473 qopm :: { LHsExpr RdrName } -- used in sections
1474 : qvaropm { L1 $ HsVar (unLoc $1) }
1475 | qconop { L1 $ HsVar (unLoc $1) }
1477 qvarop :: { Located RdrName }
1479 | '`' qvarid '`' { LL (unLoc $2) }
1481 qvaropm :: { Located RdrName }
1482 : qvarsym_no_minus { $1 }
1483 | '`' qvarid '`' { LL (unLoc $2) }
1485 -----------------------------------------------------------------------------
1488 tyvar :: { Located RdrName }
1489 tyvar : tyvarid { $1 }
1490 | '(' tyvarsym ')' { LL (unLoc $2) }
1492 tyvarop :: { Located RdrName }
1493 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1496 tyvarid :: { Located RdrName }
1497 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1498 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1499 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1500 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1501 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1503 tyvarsym :: { Located RdrName }
1504 -- Does not include "!", because that is used for strictness marks
1505 -- or ".", because that separates the quantified type vars from the rest
1506 -- or "*", because that's used for kinds
1507 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1509 -----------------------------------------------------------------------------
1512 var :: { Located RdrName }
1514 | '(' varsym ')' { LL (unLoc $2) }
1516 qvar :: { Located RdrName }
1518 | '(' varsym ')' { LL (unLoc $2) }
1519 | '(' qvarsym1 ')' { LL (unLoc $2) }
1520 -- We've inlined qvarsym here so that the decision about
1521 -- whether it's a qvar or a var can be postponed until
1522 -- *after* we see the close paren.
1524 qvarid :: { Located RdrName }
1526 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1528 varid :: { Located RdrName }
1529 : varid_no_unsafe { $1 }
1530 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1531 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1532 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1534 varid_no_unsafe :: { Located RdrName }
1535 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1536 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1537 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1538 | 'iso' { L1 $! mkUnqual varName FSLIT("iso") }
1539 | 'family' { L1 $! mkUnqual varName FSLIT("family") }
1541 qvarsym :: { Located RdrName }
1545 qvarsym_no_minus :: { Located RdrName }
1546 : varsym_no_minus { $1 }
1549 qvarsym1 :: { Located RdrName }
1550 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1552 varsym :: { Located RdrName }
1553 : varsym_no_minus { $1 }
1554 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1556 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1557 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1558 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1561 -- These special_ids are treated as keywords in various places,
1562 -- but as ordinary ids elsewhere. 'special_id' collects all these
1563 -- except 'unsafe', 'forall', 'family', and 'iso' whose treatment differs
1564 -- depending on context
1565 special_id :: { Located FastString }
1567 : 'as' { L1 FSLIT("as") }
1568 | 'qualified' { L1 FSLIT("qualified") }
1569 | 'hiding' { L1 FSLIT("hiding") }
1570 | 'export' { L1 FSLIT("export") }
1571 | 'label' { L1 FSLIT("label") }
1572 | 'dynamic' { L1 FSLIT("dynamic") }
1573 | 'stdcall' { L1 FSLIT("stdcall") }
1574 | 'ccall' { L1 FSLIT("ccall") }
1576 special_sym :: { Located FastString }
1577 special_sym : '!' { L1 FSLIT("!") }
1578 | '.' { L1 FSLIT(".") }
1579 | '*' { L1 FSLIT("*") }
1581 -----------------------------------------------------------------------------
1582 -- Data constructors
1584 qconid :: { Located RdrName } -- Qualified or unqualified
1586 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1588 conid :: { Located RdrName }
1589 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1591 qconsym :: { Located RdrName } -- Qualified or unqualified
1593 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1595 consym :: { Located RdrName }
1596 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1598 -- ':' means only list cons
1599 | ':' { L1 $ consDataCon_RDR }
1602 -----------------------------------------------------------------------------
1605 literal :: { Located HsLit }
1606 : CHAR { L1 $ HsChar $ getCHAR $1 }
1607 | STRING { L1 $ HsString $ getSTRING $1 }
1608 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1609 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1610 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1611 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1612 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1614 -----------------------------------------------------------------------------
1618 : vccurly { () } -- context popped in lexer.
1619 | error {% popContext }
1621 -----------------------------------------------------------------------------
1622 -- Miscellaneous (mostly renamings)
1624 modid :: { Located ModuleName }
1625 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1626 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1629 (unpackFS mod ++ '.':unpackFS c))
1633 : commas ',' { $1 + 1 }
1636 -----------------------------------------------------------------------------
1640 happyError = srcParseFail
1642 getVARID (L _ (ITvarid x)) = x
1643 getCONID (L _ (ITconid x)) = x
1644 getVARSYM (L _ (ITvarsym x)) = x
1645 getCONSYM (L _ (ITconsym x)) = x
1646 getQVARID (L _ (ITqvarid x)) = x
1647 getQCONID (L _ (ITqconid x)) = x
1648 getQVARSYM (L _ (ITqvarsym x)) = x
1649 getQCONSYM (L _ (ITqconsym x)) = x
1650 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1651 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1652 getCHAR (L _ (ITchar x)) = x
1653 getSTRING (L _ (ITstring x)) = x
1654 getINTEGER (L _ (ITinteger x)) = x
1655 getRATIONAL (L _ (ITrational x)) = x
1656 getPRIMCHAR (L _ (ITprimchar x)) = x
1657 getPRIMSTRING (L _ (ITprimstring x)) = x
1658 getPRIMINTEGER (L _ (ITprimint x)) = x
1659 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1660 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1661 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1662 getINLINE (L _ (ITinline_prag b)) = b
1663 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1665 -- Utilities for combining source spans
1666 comb2 :: Located a -> Located b -> SrcSpan
1669 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1670 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1672 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1673 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1674 combineSrcSpans (getLoc c) (getLoc d)
1676 -- strict constructor version:
1678 sL :: SrcSpan -> a -> Located a
1679 sL span a = span `seq` L span a
1681 -- Make a source location for the file. We're a bit lazy here and just
1682 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1683 -- try to find the span of the whole file (ToDo).
1684 fileSrcSpan :: P SrcSpan
1687 let loc = mkSrcLoc (srcLocFile l) 1 0;
1688 return (mkSrcSpan loc loc)