2 -- ---------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1997-2003
7 -- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -- ---------------------------------------------------------------------------
11 {-# OPTIONS -Wwarn -w #-}
12 -- The above warning supression flag is a temporary kludge.
13 -- While working on this module you are encouraged to remove it and fix
14 -- any warnings in the module. See
15 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
18 {-# OPTIONS_GHC -O0 -fno-ignore-interface-pragmas #-}
20 Careful optimisation of the parser: we don't want to throw everything
21 at it, because that takes too long and doesn't buy much, but we do want
22 to inline certain key external functions, so we instruct GHC not to
23 throw away inlinings as it would normally do in -O0 mode.
26 module Parser ( parseModule, parseStmt, parseIdentifier, parseType,
31 import HscTypes ( IsBootInterface, WarningTxt(..) )
34 import TysWiredIn ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
35 unboxedSingletonTyCon, unboxedSingletonDataCon,
36 listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
37 import Type ( funTyCon )
38 import ForeignCall ( Safety(..), CExportSpec(..), CLabelString,
39 CCallConv(..), CCallTarget(..), defaultCCallConv
41 import OccName ( varName, dataName, tcClsName, tvName )
42 import DataCon ( DataCon, dataConName )
43 import SrcLoc ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
44 SrcSpan, combineLocs, srcLocFile,
47 import StaticFlags ( opt_SccProfilingOn, opt_Hpc )
48 import Type ( Kind, mkArrowKind, liftedTypeKind, unliftedTypeKind )
49 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
50 Activation(..), RuleMatchInfo(..), defaultInlineSpec )
54 import {-# SOURCE #-} HaddockLex hiding ( Token )
58 import Maybes ( orElse )
61 import Control.Monad ( unless )
64 import Control.Monad ( mplus )
68 -----------------------------------------------------------------------------
71 Conflicts: 33 shift/reduce
74 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
75 would think the two should never occur in the same context.
79 -----------------------------------------------------------------------------
82 Conflicts: 34 shift/reduce
85 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
86 would think the two should never occur in the same context.
90 -----------------------------------------------------------------------------
93 Conflicts: 32 shift/reduce
96 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
97 would think the two should never occur in the same context.
101 -----------------------------------------------------------------------------
104 Conflicts: 37 shift/reduce
107 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
108 would think the two should never occur in the same context.
112 -----------------------------------------------------------------------------
113 Conflicts: 38 shift/reduce (1.25)
115 10 for abiguity in 'if x then y else z + 1' [State 178]
116 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
117 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
119 1 for ambiguity in 'if x then y else z :: T' [State 178]
120 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
122 4 for ambiguity in 'if x then y else z -< e' [State 178]
123 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
124 There are four such operators: -<, >-, -<<, >>-
127 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
128 Which of these two is intended?
130 (x::T) -> T -- Rhs is T
133 (x::T -> T) -> .. -- Rhs is ...
135 10 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
138 As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
139 Same duplication between states 11 and 253 as the previous case
141 1 for ambiguity in 'let ?x ...' [State 329]
142 the parser can't tell whether the ?x is the lhs of a normal binding or
143 an implicit binding. Fortunately resolving as shift gives it the only
144 sensible meaning, namely the lhs of an implicit binding.
146 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
147 we don't know whether the '[' starts the activation or not: it
148 might be the start of the declaration with the activation being
149 empty. --SDM 1/4/2002
151 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
152 since 'forall' is a valid variable name, we don't know whether
153 to treat a forall on the input as the beginning of a quantifier
154 or the beginning of the rule itself. Resolving to shift means
155 it's always treated as a quantifier, hence the above is disallowed.
156 This saves explicitly defining a grammar for the rule lhs that
157 doesn't include 'forall'.
159 1 for ambiguity when the source file starts with "-- | doc". We need another
160 token of lookahead to determine if a top declaration or the 'module' keyword
161 follows. Shift parses as if the 'module' keyword follows.
163 -- ---------------------------------------------------------------------------
164 -- Adding location info
166 This is done in a stylised way using the three macros below, L0, L1
167 and LL. Each of these macros can be thought of as having type
169 L0, L1, LL :: a -> Located a
171 They each add a SrcSpan to their argument.
173 L0 adds 'noSrcSpan', used for empty productions
174 -- This doesn't seem to work anymore -=chak
176 L1 for a production with a single token on the lhs. Grabs the SrcSpan
179 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
180 the first and last tokens.
182 These suffice for the majority of cases. However, we must be
183 especially careful with empty productions: LL won't work if the first
184 or last token on the lhs can represent an empty span. In these cases,
185 we have to calculate the span using more of the tokens from the lhs, eg.
187 | 'newtype' tycl_hdr '=' newconstr deriving
189 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
191 We provide comb3 and comb4 functions which are useful in such cases.
193 Be careful: there's no checking that you actually got this right, the
194 only symptom will be that the SrcSpans of your syntax will be
198 * We must expand these macros *before* running Happy, which is why this file is
199 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
201 #define L0 L noSrcSpan
202 #define L1 sL (getLoc $1)
203 #define LL sL (comb2 $1 $>)
205 -- -----------------------------------------------------------------------------
210 '_' { L _ ITunderscore } -- Haskell keywords
212 'case' { L _ ITcase }
213 'class' { L _ ITclass }
214 'data' { L _ ITdata }
215 'default' { L _ ITdefault }
216 'deriving' { L _ ITderiving }
218 'else' { L _ ITelse }
219 'hiding' { L _ IThiding }
221 'import' { L _ ITimport }
223 'infix' { L _ ITinfix }
224 'infixl' { L _ ITinfixl }
225 'infixr' { L _ ITinfixr }
226 'instance' { L _ ITinstance }
228 'module' { L _ ITmodule }
229 'newtype' { L _ ITnewtype }
231 'qualified' { L _ ITqualified }
232 'then' { L _ ITthen }
233 'type' { L _ ITtype }
234 'where' { L _ ITwhere }
235 '_scc_' { L _ ITscc } -- ToDo: remove
237 'forall' { L _ ITforall } -- GHC extension keywords
238 'foreign' { L _ ITforeign }
239 'export' { L _ ITexport }
240 'label' { L _ ITlabel }
241 'dynamic' { L _ ITdynamic }
242 'safe' { L _ ITsafe }
243 'threadsafe' { L _ ITthreadsafe }
244 'unsafe' { L _ ITunsafe }
246 'family' { L _ ITfamily }
247 'stdcall' { L _ ITstdcallconv }
248 'ccall' { L _ ITccallconv }
249 'dotnet' { L _ ITdotnet }
250 'proc' { L _ ITproc } -- for arrow notation extension
251 'rec' { L _ ITrec } -- for arrow notation extension
252 'group' { L _ ITgroup } -- for list transform extension
253 'by' { L _ ITby } -- for list transform extension
254 'using' { L _ ITusing } -- for list transform extension
256 '{-# INLINE' { L _ (ITinline_prag _) }
257 '{-# INLINE_CONLIKE' { L _ (ITinline_conlike_prag _) }
258 '{-# SPECIALISE' { L _ ITspec_prag }
259 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
260 '{-# SOURCE' { L _ ITsource_prag }
261 '{-# RULES' { L _ ITrules_prag }
262 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
263 '{-# SCC' { L _ ITscc_prag }
264 '{-# GENERATED' { L _ ITgenerated_prag }
265 '{-# DEPRECATED' { L _ ITdeprecated_prag }
266 '{-# WARNING' { L _ ITwarning_prag }
267 '{-# UNPACK' { L _ ITunpack_prag }
268 '{-# ANN' { L _ ITann_prag }
269 '#-}' { L _ ITclose_prag }
271 '..' { L _ ITdotdot } -- reserved symbols
273 '::' { L _ ITdcolon }
277 '<-' { L _ ITlarrow }
278 '->' { L _ ITrarrow }
281 '=>' { L _ ITdarrow }
285 '-<' { L _ ITlarrowtail } -- for arrow notation
286 '>-' { L _ ITrarrowtail } -- for arrow notation
287 '-<<' { L _ ITLarrowtail } -- for arrow notation
288 '>>-' { L _ ITRarrowtail } -- for arrow notation
291 '{' { L _ ITocurly } -- special symbols
293 '{|' { L _ ITocurlybar }
294 '|}' { L _ ITccurlybar }
295 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
296 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
299 '[:' { L _ ITopabrack }
300 ':]' { L _ ITcpabrack }
303 '(#' { L _ IToubxparen }
304 '#)' { L _ ITcubxparen }
305 '(|' { L _ IToparenbar }
306 '|)' { L _ ITcparenbar }
309 '`' { L _ ITbackquote }
311 VARID { L _ (ITvarid _) } -- identifiers
312 CONID { L _ (ITconid _) }
313 VARSYM { L _ (ITvarsym _) }
314 CONSYM { L _ (ITconsym _) }
315 QVARID { L _ (ITqvarid _) }
316 QCONID { L _ (ITqconid _) }
317 QVARSYM { L _ (ITqvarsym _) }
318 QCONSYM { L _ (ITqconsym _) }
319 PREFIXQVARSYM { L _ (ITprefixqvarsym _) }
320 PREFIXQCONSYM { L _ (ITprefixqconsym _) }
322 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
324 CHAR { L _ (ITchar _) }
325 STRING { L _ (ITstring _) }
326 INTEGER { L _ (ITinteger _) }
327 RATIONAL { L _ (ITrational _) }
329 PRIMCHAR { L _ (ITprimchar _) }
330 PRIMSTRING { L _ (ITprimstring _) }
331 PRIMINTEGER { L _ (ITprimint _) }
332 PRIMWORD { L _ (ITprimword _) }
333 PRIMFLOAT { L _ (ITprimfloat _) }
334 PRIMDOUBLE { L _ (ITprimdouble _) }
336 DOCNEXT { L _ (ITdocCommentNext _) }
337 DOCPREV { L _ (ITdocCommentPrev _) }
338 DOCNAMED { L _ (ITdocCommentNamed _) }
339 DOCSECTION { L _ (ITdocSection _ _) }
342 '[|' { L _ ITopenExpQuote }
343 '[p|' { L _ ITopenPatQuote }
344 '[t|' { L _ ITopenTypQuote }
345 '[d|' { L _ ITopenDecQuote }
346 '|]' { L _ ITcloseQuote }
347 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
348 '$(' { L _ ITparenEscape } -- $( exp )
349 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
350 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
351 TH_QUASIQUOTE { L _ (ITquasiQuote _) }
353 %monad { P } { >>= } { return }
354 %lexer { lexer } { L _ ITeof }
355 %name parseModule module
356 %name parseStmt maybe_stmt
357 %name parseIdentifier identifier
358 %name parseType ctype
359 %partial parseHeader header
360 %tokentype { (Located Token) }
363 -----------------------------------------------------------------------------
364 -- Identifiers; one of the entry points
365 identifier :: { Located RdrName }
370 | '(' '->' ')' { LL $ getRdrName funTyCon }
372 -----------------------------------------------------------------------------
375 -- The place for module deprecation is really too restrictive, but if it
376 -- was allowed at its natural place just before 'module', we get an ugly
377 -- s/r conflict with the second alternative. Another solution would be the
378 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
379 -- either, and DEPRECATED is only expected to be used by people who really
380 -- know what they are doing. :-)
382 module :: { Located (HsModule RdrName) }
383 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' body
384 {% fileSrcSpan >>= \ loc -> case $1 of { (info, doc) ->
385 return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4
388 {% fileSrcSpan >>= \ loc ->
389 return (L loc (HsModule Nothing Nothing
390 (fst $1) (snd $1) Nothing emptyHaddockModInfo
393 maybedocheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
394 : moduleheader { $1 }
395 | {- empty -} { (emptyHaddockModInfo, Nothing) }
397 missing_module_keyword :: { () }
398 : {- empty -} {% pushCurrentContext }
400 maybemodwarning :: { Maybe WarningTxt }
401 : '{-# DEPRECATED' STRING '#-}' { Just (DeprecatedTxt (getSTRING $2)) }
402 | '{-# WARNING' STRING '#-}' { Just (WarningTxt (getSTRING $2)) }
403 | {- empty -} { Nothing }
405 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
407 | vocurly top close { $2 }
409 body2 :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
411 | missing_module_keyword top close { $2 }
413 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
414 : importdecls { (reverse $1,[]) }
415 | importdecls ';' cvtopdecls { (reverse $1,$3) }
416 | cvtopdecls { ([],$1) }
418 cvtopdecls :: { [LHsDecl RdrName] }
419 : topdecls { cvTopDecls $1 }
421 -----------------------------------------------------------------------------
422 -- Module declaration & imports only
424 header :: { Located (HsModule RdrName) }
425 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' header_body
426 {% fileSrcSpan >>= \ loc -> case $1 of { (info, doc) ->
427 return (L loc (HsModule (Just $3) $5 $7 [] $4
429 | missing_module_keyword importdecls
430 {% fileSrcSpan >>= \ loc ->
431 return (L loc (HsModule Nothing Nothing $2 [] Nothing
432 emptyHaddockModInfo Nothing)) }
434 header_body :: { [LImportDecl RdrName] }
435 : '{' importdecls { $2 }
436 | vocurly importdecls { $2 }
438 -----------------------------------------------------------------------------
441 maybeexports :: { Maybe [LIE RdrName] }
442 : '(' exportlist ')' { Just $2 }
443 | {- empty -} { Nothing }
445 exportlist :: { [LIE RdrName] }
446 : expdoclist ',' expdoclist { $1 ++ $3 }
449 exportlist1 :: { [LIE RdrName] }
450 : expdoclist export expdoclist ',' exportlist { $1 ++ ($2 : $3) ++ $5 }
451 | expdoclist export expdoclist { $1 ++ ($2 : $3) }
454 expdoclist :: { [LIE RdrName] }
455 : exp_doc expdoclist { $1 : $2 }
458 exp_doc :: { LIE RdrName }
459 : docsection { L1 (case (unLoc $1) of (n, doc) -> IEGroup n doc) }
460 | docnamed { L1 (IEDocNamed ((fst . unLoc) $1)) }
461 | docnext { L1 (IEDoc (unLoc $1)) }
463 -- No longer allow things like [] and (,,,) to be exported
464 -- They are built in syntax, always available
465 export :: { LIE RdrName }
466 : qvar { L1 (IEVar (unLoc $1)) }
467 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
468 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
469 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
470 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
471 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
473 qcnames :: { [RdrName] }
474 : qcnames ',' qcname_ext { unLoc $3 : $1 }
475 | qcname_ext { [unLoc $1] }
477 qcname_ext :: { Located RdrName } -- Variable or data constructor
478 -- or tagged type constructor
480 | 'type' qcon { sL (comb2 $1 $2)
481 (setRdrNameSpace (unLoc $2)
484 -- Cannot pull into qcname_ext, as qcname is also used in expression.
485 qcname :: { Located RdrName } -- Variable or data constructor
489 -----------------------------------------------------------------------------
490 -- Import Declarations
492 -- import decls can be *empty*, or even just a string of semicolons
493 -- whereas topdecls must contain at least one topdecl.
495 importdecls :: { [LImportDecl RdrName] }
496 : importdecls ';' importdecl { $3 : $1 }
497 | importdecls ';' { $1 }
498 | importdecl { [ $1 ] }
501 importdecl :: { LImportDecl RdrName }
502 : 'import' maybe_src optqualified maybe_pkg modid maybeas maybeimpspec
503 { L (comb4 $1 $5 $6 $7) (ImportDecl $5 $4 $2 $3 (unLoc $6) (unLoc $7)) }
505 maybe_src :: { IsBootInterface }
506 : '{-# SOURCE' '#-}' { True }
507 | {- empty -} { False }
509 maybe_pkg :: { Maybe FastString }
510 : STRING { Just (getSTRING $1) }
511 | {- empty -} { Nothing }
513 optqualified :: { Bool }
514 : 'qualified' { True }
515 | {- empty -} { False }
517 maybeas :: { Located (Maybe ModuleName) }
518 : 'as' modid { LL (Just (unLoc $2)) }
519 | {- empty -} { noLoc Nothing }
521 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
522 : impspec { L1 (Just (unLoc $1)) }
523 | {- empty -} { noLoc Nothing }
525 impspec :: { Located (Bool, [LIE RdrName]) }
526 : '(' exportlist ')' { LL (False, $2) }
527 | 'hiding' '(' exportlist ')' { LL (True, $3) }
529 -----------------------------------------------------------------------------
530 -- Fixity Declarations
534 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
536 infix :: { Located FixityDirection }
537 : 'infix' { L1 InfixN }
538 | 'infixl' { L1 InfixL }
539 | 'infixr' { L1 InfixR }
541 ops :: { Located [Located RdrName] }
542 : ops ',' op { LL ($3 : unLoc $1) }
545 -----------------------------------------------------------------------------
546 -- Top-Level Declarations
548 topdecls :: { OrdList (LHsDecl RdrName) }
549 : topdecls ';' topdecl { $1 `appOL` $3 }
550 | topdecls ';' { $1 }
553 topdecl :: { OrdList (LHsDecl RdrName) }
554 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
555 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
556 | 'instance' inst_type where_inst
557 { let (binds, sigs, ats, _) = cvBindsAndSigs (unLoc $3)
559 unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs ats)))}
560 | stand_alone_deriving { unitOL (LL (DerivD (unLoc $1))) }
561 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
562 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
563 | '{-# DEPRECATED' deprecations '#-}' { $2 }
564 | '{-# WARNING' warnings '#-}' { $2 }
565 | '{-# RULES' rules '#-}' { $2 }
566 | annotation { unitOL $1 }
569 -- Template Haskell Extension
570 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
571 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
572 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
577 cl_decl :: { LTyClDecl RdrName }
578 : 'class' tycl_hdr fds where_cls
579 {% do { let { (binds, sigs, ats, docs) =
580 cvBindsAndSigs (unLoc $4)
581 ; (ctxt, tc, tvs, tparms) = unLoc $2}
582 ; checkTyVars tparms -- only type vars allowed
584 ; return $ L (comb4 $1 $2 $3 $4)
585 (mkClassDecl (ctxt, tc, tvs)
586 (unLoc $3) sigs binds ats docs) } }
588 -- Type declarations (toplevel)
590 ty_decl :: { LTyClDecl RdrName }
591 -- ordinary type synonyms
592 : 'type' type '=' ctypedoc
593 -- Note ctype, not sigtype, on the right of '='
594 -- We allow an explicit for-all but we don't insert one
595 -- in type Foo a = (b,b)
596 -- Instead we just say b is out of scope
598 -- Note the use of type for the head; this allows
599 -- infix type constructors to be declared
600 {% do { (tc, tvs, _) <- checkSynHdr $2 False
601 ; return (L (comb2 $1 $4)
602 (TySynonym tc tvs Nothing $4))
605 -- type family declarations
606 | 'type' 'family' type opt_kind_sig
607 -- Note the use of type for the head; this allows
608 -- infix type constructors to be declared
610 {% do { (tc, tvs, _) <- checkSynHdr $3 False
611 ; return (L (comb3 $1 $3 $4)
612 (TyFamily TypeFamily tc tvs (unLoc $4)))
615 -- type instance declarations
616 | 'type' 'instance' type '=' ctype
617 -- Note the use of type for the head; this allows
618 -- infix type constructors and type patterns
620 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
621 ; return (L (comb2 $1 $5)
622 (TySynonym tc tvs (Just typats) $5))
625 -- ordinary data type or newtype declaration
626 | data_or_newtype tycl_hdr constrs deriving
627 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
628 ; checkTyVars tparms -- no type pattern
630 sL (comb4 $1 $2 $3 $4)
631 -- We need the location on tycl_hdr in case
632 -- constrs and deriving are both empty
633 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
634 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
636 -- ordinary GADT declaration
637 | data_or_newtype tycl_hdr opt_kind_sig
638 'where' gadt_constrlist
640 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
641 ; checkTyVars tparms -- can have type pats
643 sL (comb4 $1 $2 $4 $5)
644 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
645 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
647 -- data/newtype family
648 | 'data' 'family' tycl_hdr opt_kind_sig
649 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
650 ; checkTyVars tparms -- no type pattern
651 ; unless (null (unLoc ctxt)) $ -- and no context
652 parseError (getLoc ctxt)
653 "A family declaration cannot have a context"
656 (TyFamily DataFamily tc tvs (unLoc $4)) } }
658 -- data/newtype instance declaration
659 | data_or_newtype 'instance' tycl_hdr constrs deriving
660 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
661 -- can have type pats
663 L (comb4 $1 $3 $4 $5)
664 -- We need the location on tycl_hdr in case
665 -- constrs and deriving are both empty
666 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
667 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
669 -- GADT instance declaration
670 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
671 'where' gadt_constrlist
673 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
674 -- can have type pats
676 L (comb4 $1 $3 $6 $7)
677 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
678 (unLoc $4) (reverse (unLoc $6)) (unLoc $7)) } }
680 -- Associate type family declarations
682 -- * They have a different syntax than on the toplevel (no family special
685 -- * They also need to be separate from instances; otherwise, data family
686 -- declarations without a kind signature cause parsing conflicts with empty
687 -- data declarations.
689 at_decl_cls :: { LTyClDecl RdrName }
690 -- type family declarations
691 : 'type' type opt_kind_sig
692 -- Note the use of type for the head; this allows
693 -- infix type constructors to be declared
695 {% do { (tc, tvs, _) <- checkSynHdr $2 False
696 ; return (L (comb3 $1 $2 $3)
697 (TyFamily TypeFamily tc tvs (unLoc $3)))
700 -- default type instance
701 | 'type' type '=' ctype
702 -- Note the use of type for the head; this allows
703 -- infix type constructors and type patterns
705 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
706 ; return (L (comb2 $1 $4)
707 (TySynonym tc tvs (Just typats) $4))
710 -- data/newtype family declaration
711 | 'data' tycl_hdr opt_kind_sig
712 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
713 ; checkTyVars tparms -- no type pattern
714 ; unless (null (unLoc ctxt)) $ -- and no context
715 parseError (getLoc ctxt)
716 "A family declaration cannot have a context"
719 (TyFamily DataFamily tc tvs (unLoc $3))
722 -- Associate type instances
724 at_decl_inst :: { LTyClDecl RdrName }
725 -- type instance declarations
726 : 'type' type '=' ctype
727 -- Note the use of type for the head; this allows
728 -- infix type constructors and type patterns
730 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
731 ; return (L (comb2 $1 $4)
732 (TySynonym tc tvs (Just typats) $4))
735 -- data/newtype instance declaration
736 | data_or_newtype tycl_hdr constrs deriving
737 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
738 -- can have type pats
740 L (comb4 $1 $2 $3 $4)
741 -- We need the location on tycl_hdr in case
742 -- constrs and deriving are both empty
743 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
744 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
746 -- GADT instance declaration
747 | data_or_newtype tycl_hdr opt_kind_sig
748 'where' gadt_constrlist
750 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
751 -- can have type pats
753 L (comb4 $1 $2 $5 $6)
754 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
755 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
757 data_or_newtype :: { Located NewOrData }
758 : 'data' { L1 DataType }
759 | 'newtype' { L1 NewType }
761 opt_kind_sig :: { Located (Maybe Kind) }
763 | '::' kind { LL (Just (unLoc $2)) }
765 -- tycl_hdr parses the header of a class or data type decl,
766 -- which takes the form
769 -- (Eq a, Ord b) => T a b
770 -- T Int [a] -- for associated types
771 -- Rather a lot of inlining here, else we get reduce/reduce errors
772 tycl_hdr :: { Located (LHsContext RdrName,
774 [LHsTyVarBndr RdrName],
776 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
777 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
779 -----------------------------------------------------------------------------
780 -- Stand-alone deriving
782 -- Glasgow extension: stand-alone deriving declarations
783 stand_alone_deriving :: { LDerivDecl RdrName }
784 : 'deriving' 'instance' inst_type {% checkDerivDecl (LL (DerivDecl $3)) }
786 -----------------------------------------------------------------------------
787 -- Nested declarations
789 -- Declaration in class bodies
791 decl_cls :: { Located (OrdList (LHsDecl RdrName)) }
792 decl_cls : at_decl_cls { LL (unitOL (L1 (TyClD (unLoc $1)))) }
795 decls_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
796 : decls_cls ';' decl_cls { LL (unLoc $1 `appOL` unLoc $3) }
797 | decls_cls ';' { LL (unLoc $1) }
799 | {- empty -} { noLoc nilOL }
803 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
804 : '{' decls_cls '}' { LL (unLoc $2) }
805 | vocurly decls_cls close { $2 }
809 where_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
810 -- No implicit parameters
811 -- May have type declarations
812 : 'where' decllist_cls { LL (unLoc $2) }
813 | {- empty -} { noLoc nilOL }
815 -- Declarations in instance bodies
817 decl_inst :: { Located (OrdList (LHsDecl RdrName)) }
818 decl_inst : at_decl_inst { LL (unitOL (L1 (TyClD (unLoc $1)))) }
821 decls_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
822 : decls_inst ';' decl_inst { LL (unLoc $1 `appOL` unLoc $3) }
823 | decls_inst ';' { LL (unLoc $1) }
825 | {- empty -} { noLoc nilOL }
828 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
829 : '{' decls_inst '}' { LL (unLoc $2) }
830 | vocurly decls_inst close { $2 }
834 where_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
835 -- No implicit parameters
836 -- May have type declarations
837 : 'where' decllist_inst { LL (unLoc $2) }
838 | {- empty -} { noLoc nilOL }
840 -- Declarations in binding groups other than classes and instances
842 decls :: { Located (OrdList (LHsDecl RdrName)) }
843 : decls ';' decl { let { this = unLoc $3;
845 these = rest `appOL` this }
846 in rest `seq` this `seq` these `seq`
848 | decls ';' { LL (unLoc $1) }
850 | {- empty -} { noLoc nilOL }
852 decllist :: { Located (OrdList (LHsDecl RdrName)) }
853 : '{' decls '}' { LL (unLoc $2) }
854 | vocurly decls close { $2 }
856 -- Binding groups other than those of class and instance declarations
858 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
859 -- No type declarations
860 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
861 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
862 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
864 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
865 -- No type declarations
866 : 'where' binds { LL (unLoc $2) }
867 | {- empty -} { noLoc emptyLocalBinds }
870 -----------------------------------------------------------------------------
871 -- Transformation Rules
873 rules :: { OrdList (LHsDecl RdrName) }
874 : rules ';' rule { $1 `snocOL` $3 }
877 | {- empty -} { nilOL }
879 rule :: { LHsDecl RdrName }
880 : STRING activation rule_forall infixexp '=' exp
881 { LL $ RuleD (HsRule (getSTRING $1)
882 ($2 `orElse` AlwaysActive)
883 $3 $4 placeHolderNames $6 placeHolderNames) }
885 activation :: { Maybe Activation }
886 : {- empty -} { Nothing }
887 | explicit_activation { Just $1 }
889 explicit_activation :: { Activation } -- In brackets
890 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
891 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
893 rule_forall :: { [RuleBndr RdrName] }
894 : 'forall' rule_var_list '.' { $2 }
897 rule_var_list :: { [RuleBndr RdrName] }
899 | rule_var rule_var_list { $1 : $2 }
901 rule_var :: { RuleBndr RdrName }
902 : varid { RuleBndr $1 }
903 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
905 -----------------------------------------------------------------------------
906 -- Warnings and deprecations (c.f. rules)
908 warnings :: { OrdList (LHsDecl RdrName) }
909 : warnings ';' warning { $1 `appOL` $3 }
910 | warnings ';' { $1 }
912 | {- empty -} { nilOL }
914 -- SUP: TEMPORARY HACK, not checking for `module Foo'
915 warning :: { OrdList (LHsDecl RdrName) }
917 { toOL [ LL $ WarningD (Warning n (WarningTxt (getSTRING $2)))
920 deprecations :: { OrdList (LHsDecl RdrName) }
921 : deprecations ';' deprecation { $1 `appOL` $3 }
922 | deprecations ';' { $1 }
924 | {- empty -} { nilOL }
926 -- SUP: TEMPORARY HACK, not checking for `module Foo'
927 deprecation :: { OrdList (LHsDecl RdrName) }
929 { toOL [ LL $ WarningD (Warning n (DeprecatedTxt (getSTRING $2)))
932 -----------------------------------------------------------------------------
934 annotation :: { LHsDecl RdrName }
935 : '{-# ANN' name_var aexp '#-}' { LL (AnnD $ HsAnnotation (ValueAnnProvenance (unLoc $2)) $3) }
936 | '{-# ANN' 'type' tycon aexp '#-}' { LL (AnnD $ HsAnnotation (TypeAnnProvenance (unLoc $3)) $4) }
937 | '{-# ANN' 'module' aexp '#-}' { LL (AnnD $ HsAnnotation ModuleAnnProvenance $3) }
940 -----------------------------------------------------------------------------
941 -- Foreign import and export declarations
943 fdecl :: { LHsDecl RdrName }
944 fdecl : 'import' callconv safety fspec
945 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
946 | 'import' callconv fspec
947 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
949 | 'export' callconv fspec
950 {% mkExport $2 (unLoc $3) >>= return.LL }
952 callconv :: { CallConv }
953 : 'stdcall' { CCall StdCallConv }
954 | 'ccall' { CCall CCallConv }
955 | 'dotnet' { DNCall }
958 : 'unsafe' { PlayRisky }
959 | 'safe' { PlaySafe False }
960 | 'threadsafe' { PlaySafe True }
962 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
963 : STRING var '::' sigtypedoc { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
964 | var '::' sigtypedoc { LL (noLoc nilFS, $1, $3) }
965 -- if the entity string is missing, it defaults to the empty string;
966 -- the meaning of an empty entity string depends on the calling
969 -----------------------------------------------------------------------------
972 opt_sig :: { Maybe (LHsType RdrName) }
973 : {- empty -} { Nothing }
974 | '::' sigtype { Just $2 }
976 opt_asig :: { Maybe (LHsType RdrName) }
977 : {- empty -} { Nothing }
978 | '::' atype { Just $2 }
980 sigtypes1 :: { [LHsType RdrName] }
982 | sigtype ',' sigtypes1 { $1 : $3 }
984 sigtype :: { LHsType RdrName }
985 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
986 -- Wrap an Implicit forall if there isn't one there already
988 sigtypedoc :: { LHsType RdrName }
989 : ctypedoc { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
990 -- Wrap an Implicit forall if there isn't one there already
992 sig_vars :: { Located [Located RdrName] }
993 : sig_vars ',' var { LL ($3 : unLoc $1) }
996 -----------------------------------------------------------------------------
999 infixtype :: { LHsType RdrName }
1000 : btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
1001 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
1003 strict_mark :: { Located HsBang }
1004 : '!' { L1 HsStrict }
1005 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
1007 -- A ctype is a for-all type
1008 ctype :: { LHsType RdrName }
1009 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
1010 | context '=>' ctype { LL $ mkImplicitHsForAllTy $1 $3 }
1011 -- A type of form (context => type) is an *implicit* HsForAllTy
1012 | ipvar '::' type { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
1015 ----------------------
1016 -- Notes for 'ctypedoc'
1017 -- It would have been nice to simplify the grammar by unifying `ctype` and
1018 -- ctypedoc` into one production, allowing comments on types everywhere (and
1019 -- rejecting them after parsing, where necessary). This is however not possible
1020 -- since it leads to ambiguity. The reason is the support for comments on record
1022 -- data R = R { field :: Int -- ^ comment on the field }
1023 -- If we allow comments on types here, it's not clear if the comment applies
1024 -- to 'field' or to 'Int'. So we must use `ctype` to describe the type.
1026 ctypedoc :: { LHsType RdrName }
1027 : 'forall' tv_bndrs '.' ctypedoc { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
1028 | context '=>' ctypedoc { LL $ mkImplicitHsForAllTy $1 $3 }
1029 -- A type of form (context => type) is an *implicit* HsForAllTy
1030 | ipvar '::' type { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
1033 ----------------------
1034 -- Notes for 'context'
1035 -- We parse a context as a btype so that we don't get reduce/reduce
1036 -- errors in ctype. The basic problem is that
1038 -- looks so much like a tuple type. We can't tell until we find the =>
1040 -- We have the t1 ~ t2 form both in 'context' and in type,
1041 -- to permit an individual equational constraint without parenthesis.
1042 -- Thus for some reason we allow f :: a~b => blah
1043 -- but not f :: ?x::Int => blah
1044 context :: { LHsContext RdrName }
1045 : btype '~' btype {% checkContext
1046 (LL $ HsPredTy (HsEqualP $1 $3)) }
1047 | btype {% checkContext $1 }
1049 type :: { LHsType RdrName }
1051 | btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
1052 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
1053 | btype '->' ctype { LL $ HsFunTy $1 $3 }
1054 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1056 typedoc :: { LHsType RdrName }
1058 | btype docprev { LL $ HsDocTy $1 $2 }
1059 | btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
1060 | btype qtyconop type docprev { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
1061 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
1062 | btype tyvarop type docprev { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
1063 | btype '->' ctypedoc { LL $ HsFunTy $1 $3 }
1064 | btype docprev '->' ctypedoc { LL $ HsFunTy (L (comb2 $1 $2) (HsDocTy $1 $2)) $4 }
1065 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1067 btype :: { LHsType RdrName }
1068 : btype atype { LL $ HsAppTy $1 $2 }
1071 atype :: { LHsType RdrName }
1072 : gtycon { L1 (HsTyVar (unLoc $1)) }
1073 | tyvar { L1 (HsTyVar (unLoc $1)) }
1074 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
1075 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
1076 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
1077 | '[' ctype ']' { LL $ HsListTy $2 }
1078 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
1079 | '(' ctype ')' { LL $ HsParTy $2 }
1080 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
1082 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
1084 -- An inst_type is what occurs in the head of an instance decl
1085 -- e.g. (Foo a, Gaz b) => Wibble a b
1086 -- It's kept as a single type, with a MonoDictTy at the right
1087 -- hand corner, for convenience.
1088 inst_type :: { LHsType RdrName }
1089 : sigtype {% checkInstType $1 }
1091 inst_types1 :: { [LHsType RdrName] }
1092 : inst_type { [$1] }
1093 | inst_type ',' inst_types1 { $1 : $3 }
1095 comma_types0 :: { [LHsType RdrName] }
1096 : comma_types1 { $1 }
1097 | {- empty -} { [] }
1099 comma_types1 :: { [LHsType RdrName] }
1101 | ctype ',' comma_types1 { $1 : $3 }
1103 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1104 : tv_bndr tv_bndrs { $1 : $2 }
1105 | {- empty -} { [] }
1107 tv_bndr :: { LHsTyVarBndr RdrName }
1108 : tyvar { L1 (UserTyVar (unLoc $1)) }
1109 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
1112 fds :: { Located [Located ([RdrName], [RdrName])] }
1113 : {- empty -} { noLoc [] }
1114 | '|' fds1 { LL (reverse (unLoc $2)) }
1116 fds1 :: { Located [Located ([RdrName], [RdrName])] }
1117 : fds1 ',' fd { LL ($3 : unLoc $1) }
1120 fd :: { Located ([RdrName], [RdrName]) }
1121 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
1122 (reverse (unLoc $1), reverse (unLoc $3)) }
1124 varids0 :: { Located [RdrName] }
1125 : {- empty -} { noLoc [] }
1126 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
1128 -----------------------------------------------------------------------------
1131 kind :: { Located Kind }
1133 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1135 akind :: { Located Kind }
1136 : '*' { L1 liftedTypeKind }
1137 | '!' { L1 unliftedTypeKind }
1138 | '(' kind ')' { LL (unLoc $2) }
1141 -----------------------------------------------------------------------------
1142 -- Datatype declarations
1144 gadt_constrlist :: { Located [LConDecl RdrName] }
1145 : '{' gadt_constrs '}' { LL (unLoc $2) }
1146 | vocurly gadt_constrs close { $2 }
1148 gadt_constrs :: { Located [LConDecl RdrName] }
1149 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
1150 | gadt_constrs ';' { $1 }
1151 | gadt_constr { L1 [$1] }
1153 -- We allow the following forms:
1154 -- C :: Eq a => a -> T a
1155 -- C :: forall a. Eq a => !a -> T a
1156 -- D { x,y :: a } :: T a
1157 -- forall a. Eq a => D { x,y :: a } :: T a
1159 gadt_constr :: { LConDecl RdrName }
1161 { LL (mkGadtDecl $1 $3) }
1162 -- Syntax: Maybe merge the record stuff with the single-case above?
1163 -- (to kill the mostly harmless reduce/reduce error)
1164 -- XXX revisit audreyt
1165 | constr_stuff_record '::' sigtype
1166 { let (con,details) = unLoc $1 in
1167 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3) Nothing) }
1169 | forall context '=>' constr_stuff_record '::' sigtype
1170 { let (con,details) = unLoc $4 in
1171 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6) Nothing ) }
1172 | forall constr_stuff_record '::' sigtype
1173 { let (con,details) = unLoc $2 in
1174 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4) Nothing) }
1178 constrs :: { Located [LConDecl RdrName] }
1179 : {- empty; a GHC extension -} { noLoc [] }
1180 | maybe_docnext '=' constrs1 { L (comb2 $2 $3) (addConDocs (unLoc $3) $1) }
1182 constrs1 :: { Located [LConDecl RdrName] }
1183 : constrs1 maybe_docnext '|' maybe_docprev constr { LL (addConDoc $5 $2 : addConDocFirst (unLoc $1) $4) }
1184 | constr { L1 [$1] }
1186 constr :: { LConDecl RdrName }
1187 : maybe_docnext forall context '=>' constr_stuff maybe_docprev
1188 { let (con,details) = unLoc $5 in
1189 L (comb4 $2 $3 $4 $5) (ConDecl con Explicit (unLoc $2) $3 details ResTyH98 ($1 `mplus` $6)) }
1190 | maybe_docnext forall constr_stuff maybe_docprev
1191 { let (con,details) = unLoc $3 in
1192 L (comb2 $2 $3) (ConDecl con Explicit (unLoc $2) (noLoc []) details ResTyH98 ($1 `mplus` $4)) }
1194 forall :: { Located [LHsTyVarBndr RdrName] }
1195 : 'forall' tv_bndrs '.' { LL $2 }
1196 | {- empty -} { noLoc [] }
1198 constr_stuff :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1199 -- We parse the constructor declaration
1201 -- as a btype (treating C as a type constructor) and then convert C to be
1202 -- a data constructor. Reason: it might continue like this:
1204 -- in which case C really would be a type constructor. We can't resolve this
1205 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1206 : btype {% mkPrefixCon $1 [] >>= return.LL }
1207 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
1208 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
1209 | btype conop btype { LL ($2, InfixCon $1 $3) }
1211 constr_stuff_record :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1212 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
1213 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
1215 fielddecls :: { [([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName))] }
1216 : fielddecl maybe_docnext ',' maybe_docprev fielddecls { addFieldDoc (unLoc $1) $4 : addFieldDocs $5 $2 }
1217 | fielddecl { [unLoc $1] }
1219 fielddecl :: { Located ([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName)) }
1220 : maybe_docnext sig_vars '::' ctype maybe_docprev { L (comb3 $2 $3 $4) (reverse (unLoc $2), $4, $1 `mplus` $5) }
1222 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1223 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1224 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1225 -- We don't allow a context, but that's sorted out by the type checker.
1226 deriving :: { Located (Maybe [LHsType RdrName]) }
1227 : {- empty -} { noLoc Nothing }
1228 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1229 ; p <- checkInstType (L loc (HsTyVar tv))
1230 ; return (LL (Just [p])) } }
1231 | 'deriving' '(' ')' { LL (Just []) }
1232 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1233 -- Glasgow extension: allow partial
1234 -- applications in derivings
1236 -----------------------------------------------------------------------------
1237 -- Value definitions
1239 {- There's an awkward overlap with a type signature. Consider
1240 f :: Int -> Int = ...rhs...
1241 Then we can't tell whether it's a type signature or a value
1242 definition with a result signature until we see the '='.
1243 So we have to inline enough to postpone reductions until we know.
1247 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1248 instead of qvar, we get another shift/reduce-conflict. Consider the
1251 { (^^) :: Int->Int ; } Type signature; only var allowed
1253 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1254 qvar allowed (because of instance decls)
1256 We can't tell whether to reduce var to qvar until after we've read the signatures.
1259 docdecl :: { LHsDecl RdrName }
1260 : docdecld { L1 (DocD (unLoc $1)) }
1262 docdecld :: { LDocDecl RdrName }
1263 : docnext { L1 (DocCommentNext (unLoc $1)) }
1264 | docprev { L1 (DocCommentPrev (unLoc $1)) }
1265 | docnamed { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1266 | docsection { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1268 decl :: { Located (OrdList (LHsDecl RdrName)) }
1270 | '!' aexp rhs {% do { pat <- checkPattern $2;
1271 return (LL $ unitOL $ LL $ ValD (
1272 PatBind (LL $ BangPat pat) (unLoc $3)
1273 placeHolderType placeHolderNames)) } }
1274 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1275 let { l = comb2 $1 $> };
1276 return $! (sL l (unitOL $! (sL l $ ValD r))) } }
1277 | docdecl { LL $ unitOL $1 }
1279 rhs :: { Located (GRHSs RdrName) }
1280 : '=' exp wherebinds { sL (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1281 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1283 gdrhs :: { Located [LGRHS RdrName] }
1284 : gdrhs gdrh { LL ($2 : unLoc $1) }
1287 gdrh :: { LGRHS RdrName }
1288 : '|' guardquals '=' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1290 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1291 : infixexp '::' sigtypedoc
1292 {% do s <- checkValSig $1 $3;
1293 return (LL $ unitOL (LL $ SigD s)) }
1294 -- See the above notes for why we need infixexp here
1295 | var ',' sig_vars '::' sigtypedoc
1296 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1297 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1299 | '{-# INLINE' activation qvar '#-}'
1300 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 FunLike (getINLINE $1)))) }
1301 | '{-# INLINE_CONLIKE' activation qvar '#-}'
1302 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 ConLike (getINLINE_CONLIKE $1)))) }
1303 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1304 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1306 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1307 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 FunLike (getSPEC_INLINE $1)))
1309 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1310 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1312 -----------------------------------------------------------------------------
1315 exp :: { LHsExpr RdrName }
1316 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1317 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1318 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1319 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1320 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1323 infixexp :: { LHsExpr RdrName }
1325 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1327 exp10 :: { LHsExpr RdrName }
1328 : '\\' apat apats opt_asig '->' exp
1329 { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1332 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1333 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1334 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1335 | '-' fexp { LL $ NegApp $2 noSyntaxExpr }
1337 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1338 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1339 return (L loc (mkHsDo DoExpr stmts body)) }
1340 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1341 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1342 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1343 | scc_annot exp { LL $ if opt_SccProfilingOn
1344 then HsSCC (unLoc $1) $2
1346 | hpc_annot exp { LL $ if opt_Hpc
1347 then HsTickPragma (unLoc $1) $2
1350 | 'proc' aexp '->' exp
1351 {% checkPattern $2 >>= \ p ->
1352 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1353 placeHolderType undefined)) }
1354 -- TODO: is LL right here?
1356 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1357 -- hdaume: core annotation
1360 scc_annot :: { Located FastString }
1361 : '_scc_' STRING {% (addWarning Opt_WarnWarningsDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1362 ( do scc <- getSCC $2; return $ LL scc ) }
1363 | '{-# SCC' STRING '#-}' {% do scc <- getSCC $2; return $ LL scc }
1365 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1366 : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1367 { LL $ (getSTRING $2
1368 ,( fromInteger $ getINTEGER $3
1369 , fromInteger $ getINTEGER $5
1371 ,( fromInteger $ getINTEGER $7
1372 , fromInteger $ getINTEGER $9
1377 fexp :: { LHsExpr RdrName }
1378 : fexp aexp { LL $ HsApp $1 $2 }
1381 aexp :: { LHsExpr RdrName }
1382 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1383 | '~' aexp { LL $ ELazyPat $2 }
1386 aexp1 :: { LHsExpr RdrName }
1387 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1391 -- Here was the syntax for type applications that I was planning
1392 -- but there are difficulties (e.g. what order for type args)
1393 -- so it's not enabled yet.
1394 -- But this case *is* used for the left hand side of a generic definition,
1395 -- which is parsed as an expression before being munged into a pattern
1396 | qcname '{|' type '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1397 (sL (getLoc $3) (HsType $3)) }
1399 aexp2 :: { LHsExpr RdrName }
1400 : ipvar { L1 (HsIPVar $! unLoc $1) }
1401 | qcname { L1 (HsVar $! unLoc $1) }
1402 | literal { L1 (HsLit $! unLoc $1) }
1403 -- This will enable overloaded strings permanently. Normally the renamer turns HsString
1404 -- into HsOverLit when -foverloaded-strings is on.
1405 -- | STRING { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1406 | INTEGER { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1407 | RATIONAL { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1408 -- N.B.: sections get parsed by these next two productions.
1409 -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't correct Haskell98
1410 -- (you'd have to write '((+ 3), (4 -))')
1411 -- but the less cluttered version fell out of having texps.
1412 | '(' texp ')' { LL (HsPar $2) }
1413 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1414 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1415 | '[' list ']' { LL (unLoc $2) }
1416 | '[:' parr ':]' { LL (unLoc $2) }
1417 | '_' { L1 EWildPat }
1419 -- Template Haskell Extension
1420 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1421 (L1 $ HsVar (mkUnqual varName
1422 (getTH_ID_SPLICE $1)))) } -- $x
1423 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1425 | TH_QUASIQUOTE { let { loc = getLoc $1
1426 ; ITquasiQuote (quoter, quote, quoteSpan) = unLoc $1
1427 ; quoterId = mkUnqual varName quoter
1429 in sL loc $ HsQuasiQuoteE (mkHsQuasiQuote quoterId quoteSpan quote) }
1430 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1431 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1432 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1433 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1434 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1435 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1436 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1437 return (LL $ HsBracket (PatBr p)) }
1438 | '[d|' cvtopbody '|]' {% checkDecBrGroup $2 >>= \g ->
1439 return (LL $ HsBracket (DecBr g)) }
1441 -- arrow notation extension
1442 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1444 cmdargs :: { [LHsCmdTop RdrName] }
1445 : cmdargs acmd { $2 : $1 }
1446 | {- empty -} { [] }
1448 acmd :: { LHsCmdTop RdrName }
1449 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1451 cvtopbody :: { [LHsDecl RdrName] }
1452 : '{' cvtopdecls0 '}' { $2 }
1453 | vocurly cvtopdecls0 close { $2 }
1455 cvtopdecls0 :: { [LHsDecl RdrName] }
1456 : {- empty -} { [] }
1459 -- "texp" is short for tuple expressions:
1460 -- things that can appear unparenthesized as long as they're
1461 -- inside parens or delimitted by commas
1462 texp :: { LHsExpr RdrName }
1465 -- Note [Parsing sections]
1466 -- ~~~~~~~~~~~~~~~~~~~~~~~
1467 -- We include left and right sections here, which isn't
1468 -- technically right according to Haskell 98. For example
1469 -- (3 +, True) isn't legal
1470 -- However, we want to parse bang patterns like
1472 -- and it's convenient to do so here as a section
1473 -- Then when converting expr to pattern we unravel it again
1474 -- Meanwhile, the renamer checks that real sections appear
1476 | infixexp qop { LL $ SectionL $1 $2 }
1477 | qopm infixexp { LL $ SectionR $1 $2 }
1479 -- View patterns get parenthesized above
1480 | exp '->' exp { LL $ EViewPat $1 $3 }
1482 texps :: { [LHsExpr RdrName] }
1483 : texps ',' texp { $3 : $1 }
1487 -----------------------------------------------------------------------------
1490 -- The rules below are little bit contorted to keep lexps left-recursive while
1491 -- avoiding another shift/reduce-conflict.
1493 list :: { LHsExpr RdrName }
1494 : texp { L1 $ ExplicitList placeHolderType [$1] }
1495 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1496 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1497 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1498 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1499 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1500 | texp '|' flattenedpquals { sL (comb2 $1 $>) $ mkHsDo ListComp (unLoc $3) $1 }
1502 lexps :: { Located [LHsExpr RdrName] }
1503 : lexps ',' texp { LL (((:) $! $3) $! unLoc $1) }
1504 | texp ',' texp { LL [$3,$1] }
1506 -----------------------------------------------------------------------------
1507 -- List Comprehensions
1509 flattenedpquals :: { Located [LStmt RdrName] }
1510 : pquals { case (unLoc $1) of
1511 ParStmt [(qs, _)] -> L1 qs
1512 -- We just had one thing in our "parallel" list so
1513 -- we simply return that thing directly
1516 -- We actually found some actual parallel lists so
1517 -- we leave them into as a ParStmt
1520 pquals :: { LStmt RdrName }
1521 : pquals1 { L1 (ParStmt [(qs, undefined) | qs <- (reverse (unLoc $1))]) }
1523 pquals1 :: { Located [[LStmt RdrName]] }
1524 : pquals1 '|' squals { LL (unLoc $3 : unLoc $1) }
1525 | squals { L (getLoc $1) [unLoc $1] }
1527 squals :: { Located [LStmt RdrName] }
1528 : squals1 { L (getLoc $1) (reverse (unLoc $1)) }
1530 squals1 :: { Located [LStmt RdrName] }
1531 : transformquals1 { LL (unLoc $1) }
1533 transformquals1 :: { Located [LStmt RdrName] }
1534 : transformquals1 ',' transformqual { LL $ [LL ((unLoc $3) (unLoc $1))] }
1535 | transformquals1 ',' qual { LL ($3 : unLoc $1) }
1536 -- | transformquals1 ',' '{|' pquals '|}' { LL ($4 : unLoc $1) }
1537 | transformqual { LL $ [LL ((unLoc $1) [])] }
1539 -- | '{|' pquals '|}' { L1 [$2] }
1542 -- It is possible to enable bracketing (associating) qualifier lists by uncommenting the lines with {| |}
1543 -- above. Due to a lack of consensus on the syntax, this feature is not being used until we get user
1544 -- demand. Note that the {| |} symbols are reused from -XGenerics and hence if you want to compile
1545 -- a program that makes use of this temporary syntax you must supply that flag to GHC
1547 transformqual :: { Located ([LStmt RdrName] -> Stmt RdrName) }
1548 : 'then' exp { LL $ \leftStmts -> (mkTransformStmt (reverse leftStmts) $2) }
1549 | 'then' exp 'by' exp { LL $ \leftStmts -> (mkTransformByStmt (reverse leftStmts) $2 $4) }
1550 | 'then' 'group' 'by' exp { LL $ \leftStmts -> (mkGroupByStmt (reverse leftStmts) $4) }
1551 | 'then' 'group' 'using' exp { LL $ \leftStmts -> (mkGroupUsingStmt (reverse leftStmts) $4) }
1552 | 'then' 'group' 'by' exp 'using' exp { LL $ \leftStmts -> (mkGroupByUsingStmt (reverse leftStmts) $4 $6) }
1554 -----------------------------------------------------------------------------
1555 -- Parallel array expressions
1557 -- The rules below are little bit contorted; see the list case for details.
1558 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1559 -- Moreover, we allow explicit arrays with no element (represented by the nil
1560 -- constructor in the list case).
1562 parr :: { LHsExpr RdrName }
1563 : { noLoc (ExplicitPArr placeHolderType []) }
1564 | texp { L1 $ ExplicitPArr placeHolderType [$1] }
1565 | lexps { L1 $ ExplicitPArr placeHolderType
1566 (reverse (unLoc $1)) }
1567 | texp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1568 | texp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1569 | texp '|' flattenedpquals { LL $ mkHsDo PArrComp (unLoc $3) $1 }
1571 -- We are reusing `lexps' and `flattenedpquals' from the list case.
1573 -----------------------------------------------------------------------------
1576 guardquals :: { Located [LStmt RdrName] }
1577 : guardquals1 { L (getLoc $1) (reverse (unLoc $1)) }
1579 guardquals1 :: { Located [LStmt RdrName] }
1580 : guardquals1 ',' qual { LL ($3 : unLoc $1) }
1583 -----------------------------------------------------------------------------
1584 -- Case alternatives
1586 altslist :: { Located [LMatch RdrName] }
1587 : '{' alts '}' { LL (reverse (unLoc $2)) }
1588 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1590 alts :: { Located [LMatch RdrName] }
1591 : alts1 { L1 (unLoc $1) }
1592 | ';' alts { LL (unLoc $2) }
1594 alts1 :: { Located [LMatch RdrName] }
1595 : alts1 ';' alt { LL ($3 : unLoc $1) }
1596 | alts1 ';' { LL (unLoc $1) }
1599 alt :: { LMatch RdrName }
1600 : pat opt_sig alt_rhs { LL (Match [$1] $2 (unLoc $3)) }
1602 alt_rhs :: { Located (GRHSs RdrName) }
1603 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1605 ralt :: { Located [LGRHS RdrName] }
1606 : '->' exp { LL (unguardedRHS $2) }
1607 | gdpats { L1 (reverse (unLoc $1)) }
1609 gdpats :: { Located [LGRHS RdrName] }
1610 : gdpats gdpat { LL ($2 : unLoc $1) }
1613 gdpat :: { LGRHS RdrName }
1614 : '|' guardquals '->' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1616 -- 'pat' recognises a pattern, including one with a bang at the top
1617 -- e.g. "!x" or "!(x,y)" or "C a b" etc
1618 -- Bangs inside are parsed as infix operator applications, so that
1619 -- we parse them right when bang-patterns are off
1620 pat :: { LPat RdrName }
1621 pat : exp {% checkPattern $1 }
1622 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1624 apat :: { LPat RdrName }
1625 apat : aexp {% checkPattern $1 }
1626 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1628 apats :: { [LPat RdrName] }
1629 : apat apats { $1 : $2 }
1630 | {- empty -} { [] }
1632 -----------------------------------------------------------------------------
1633 -- Statement sequences
1635 stmtlist :: { Located [LStmt RdrName] }
1636 : '{' stmts '}' { LL (unLoc $2) }
1637 | vocurly stmts close { $2 }
1639 -- do { ;; s ; s ; ; s ;; }
1640 -- The last Stmt should be an expression, but that's hard to enforce
1641 -- here, because we need too much lookahead if we see do { e ; }
1642 -- So we use ExprStmts throughout, and switch the last one over
1643 -- in ParseUtils.checkDo instead
1644 stmts :: { Located [LStmt RdrName] }
1645 : stmt stmts_help { LL ($1 : unLoc $2) }
1646 | ';' stmts { LL (unLoc $2) }
1647 | {- empty -} { noLoc [] }
1649 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1650 : ';' stmts { LL (unLoc $2) }
1651 | {- empty -} { noLoc [] }
1653 -- For typing stmts at the GHCi prompt, where
1654 -- the input may consist of just comments.
1655 maybe_stmt :: { Maybe (LStmt RdrName) }
1657 | {- nothing -} { Nothing }
1659 stmt :: { LStmt RdrName }
1661 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1663 qual :: { LStmt RdrName }
1664 : pat '<-' exp { LL $ mkBindStmt $1 $3 }
1665 | exp { L1 $ mkExprStmt $1 }
1666 | 'let' binds { LL $ LetStmt (unLoc $2) }
1668 -----------------------------------------------------------------------------
1669 -- Record Field Update/Construction
1671 fbinds :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1673 | {- empty -} { ([], False) }
1675 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1676 : fbind ',' fbinds1 { case $3 of (flds, dd) -> ($1 : flds, dd) }
1677 | fbind { ([$1], False) }
1678 | '..' { ([], True) }
1680 fbind :: { HsRecField RdrName (LHsExpr RdrName) }
1681 : qvar '=' exp { HsRecField $1 $3 False }
1682 | qvar { HsRecField $1 (L (getLoc $1) (HsVar (unLoc $1))) True }
1683 -- Here's where we say that plain 'x'
1684 -- means exactly 'x = x'. The pun-flag boolean is
1685 -- there so we can still print it right
1687 -----------------------------------------------------------------------------
1688 -- Implicit Parameter Bindings
1690 dbinds :: { Located [LIPBind RdrName] }
1691 : dbinds ';' dbind { let { this = $3; rest = unLoc $1 }
1692 in rest `seq` this `seq` LL (this : rest) }
1693 | dbinds ';' { LL (unLoc $1) }
1694 | dbind { let this = $1 in this `seq` L1 [this] }
1695 -- | {- empty -} { [] }
1697 dbind :: { LIPBind RdrName }
1698 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1700 ipvar :: { Located (IPName RdrName) }
1701 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1703 -----------------------------------------------------------------------------
1704 -- Warnings and deprecations
1706 namelist :: { Located [RdrName] }
1707 namelist : name_var { L1 [unLoc $1] }
1708 | name_var ',' namelist { LL (unLoc $1 : unLoc $3) }
1710 name_var :: { Located RdrName }
1711 name_var : var { $1 }
1714 -----------------------------------------
1715 -- Data constructors
1716 qcon :: { Located RdrName }
1718 | '(' qconsym ')' { LL (unLoc $2) }
1719 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1720 -- The case of '[:' ':]' is part of the production `parr'
1722 con :: { Located RdrName }
1724 | '(' consym ')' { LL (unLoc $2) }
1725 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1727 sysdcon :: { Located DataCon } -- Wired in data constructors
1728 : '(' ')' { LL unitDataCon }
1729 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1730 | '(#' '#)' { LL $ unboxedSingletonDataCon }
1731 | '(#' commas '#)' { LL $ tupleCon Unboxed $2 }
1732 | '[' ']' { LL nilDataCon }
1734 conop :: { Located RdrName }
1736 | '`' conid '`' { LL (unLoc $2) }
1738 qconop :: { Located RdrName }
1740 | '`' qconid '`' { LL (unLoc $2) }
1742 -----------------------------------------------------------------------------
1743 -- Type constructors
1745 gtycon :: { Located RdrName } -- A "general" qualified tycon
1747 | '(' ')' { LL $ getRdrName unitTyCon }
1748 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1749 | '(#' '#)' { LL $ getRdrName unboxedSingletonTyCon }
1750 | '(#' commas '#)' { LL $ getRdrName (tupleTyCon Unboxed $2) }
1751 | '(' '->' ')' { LL $ getRdrName funTyCon }
1752 | '[' ']' { LL $ listTyCon_RDR }
1753 | '[:' ':]' { LL $ parrTyCon_RDR }
1755 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1757 | '(' qtyconsym ')' { LL (unLoc $2) }
1759 qtyconop :: { Located RdrName } -- Qualified or unqualified
1761 | '`' qtycon '`' { LL (unLoc $2) }
1763 qtycon :: { Located RdrName } -- Qualified or unqualified
1764 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1765 | PREFIXQCONSYM { L1 $! mkQual tcClsName (getPREFIXQCONSYM $1) }
1768 tycon :: { Located RdrName } -- Unqualified
1769 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1771 qtyconsym :: { Located RdrName }
1772 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1775 tyconsym :: { Located RdrName }
1776 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1778 -----------------------------------------------------------------------------
1781 op :: { Located RdrName } -- used in infix decls
1785 varop :: { Located RdrName }
1787 | '`' varid '`' { LL (unLoc $2) }
1789 qop :: { LHsExpr RdrName } -- used in sections
1790 : qvarop { L1 $ HsVar (unLoc $1) }
1791 | qconop { L1 $ HsVar (unLoc $1) }
1793 qopm :: { LHsExpr RdrName } -- used in sections
1794 : qvaropm { L1 $ HsVar (unLoc $1) }
1795 | qconop { L1 $ HsVar (unLoc $1) }
1797 qvarop :: { Located RdrName }
1799 | '`' qvarid '`' { LL (unLoc $2) }
1801 qvaropm :: { Located RdrName }
1802 : qvarsym_no_minus { $1 }
1803 | '`' qvarid '`' { LL (unLoc $2) }
1805 -----------------------------------------------------------------------------
1808 tyvar :: { Located RdrName }
1809 tyvar : tyvarid { $1 }
1810 | '(' tyvarsym ')' { LL (unLoc $2) }
1812 tyvarop :: { Located RdrName }
1813 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1815 | '.' {% parseErrorSDoc (getLoc $1)
1816 (vcat [ptext (sLit "Illegal symbol '.' in type"),
1817 ptext (sLit "Perhaps you intended -XRankNTypes or similar flag"),
1818 ptext (sLit "to enable explicit-forall syntax: forall <tvs>. <type>")])
1821 tyvarid :: { Located RdrName }
1822 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1823 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1824 | 'unsafe' { L1 $! mkUnqual tvName (fsLit "unsafe") }
1825 | 'safe' { L1 $! mkUnqual tvName (fsLit "safe") }
1826 | 'threadsafe' { L1 $! mkUnqual tvName (fsLit "threadsafe") }
1828 tyvarsym :: { Located RdrName }
1829 -- Does not include "!", because that is used for strictness marks
1830 -- or ".", because that separates the quantified type vars from the rest
1831 -- or "*", because that's used for kinds
1832 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1834 -----------------------------------------------------------------------------
1837 var :: { Located RdrName }
1839 | '(' varsym ')' { LL (unLoc $2) }
1841 qvar :: { Located RdrName }
1843 | '(' varsym ')' { LL (unLoc $2) }
1844 | '(' qvarsym1 ')' { LL (unLoc $2) }
1845 -- We've inlined qvarsym here so that the decision about
1846 -- whether it's a qvar or a var can be postponed until
1847 -- *after* we see the close paren.
1849 qvarid :: { Located RdrName }
1851 | QVARID { L1 $! mkQual varName (getQVARID $1) }
1852 | PREFIXQVARSYM { L1 $! mkQual varName (getPREFIXQVARSYM $1) }
1854 varid :: { Located RdrName }
1855 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1856 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1857 | 'unsafe' { L1 $! mkUnqual varName (fsLit "unsafe") }
1858 | 'safe' { L1 $! mkUnqual varName (fsLit "safe") }
1859 | 'threadsafe' { L1 $! mkUnqual varName (fsLit "threadsafe") }
1860 | 'forall' { L1 $! mkUnqual varName (fsLit "forall") }
1861 | 'family' { L1 $! mkUnqual varName (fsLit "family") }
1863 qvarsym :: { Located RdrName }
1867 qvarsym_no_minus :: { Located RdrName }
1868 : varsym_no_minus { $1 }
1871 qvarsym1 :: { Located RdrName }
1872 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1874 varsym :: { Located RdrName }
1875 : varsym_no_minus { $1 }
1876 | '-' { L1 $ mkUnqual varName (fsLit "-") }
1878 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1879 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1880 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1883 -- These special_ids are treated as keywords in various places,
1884 -- but as ordinary ids elsewhere. 'special_id' collects all these
1885 -- except 'unsafe', 'forall', and 'family' whose treatment differs
1886 -- depending on context
1887 special_id :: { Located FastString }
1889 : 'as' { L1 (fsLit "as") }
1890 | 'qualified' { L1 (fsLit "qualified") }
1891 | 'hiding' { L1 (fsLit "hiding") }
1892 | 'export' { L1 (fsLit "export") }
1893 | 'label' { L1 (fsLit "label") }
1894 | 'dynamic' { L1 (fsLit "dynamic") }
1895 | 'stdcall' { L1 (fsLit "stdcall") }
1896 | 'ccall' { L1 (fsLit "ccall") }
1898 special_sym :: { Located FastString }
1899 special_sym : '!' { L1 (fsLit "!") }
1900 | '.' { L1 (fsLit ".") }
1901 | '*' { L1 (fsLit "*") }
1903 -----------------------------------------------------------------------------
1904 -- Data constructors
1906 qconid :: { Located RdrName } -- Qualified or unqualified
1908 | QCONID { L1 $! mkQual dataName (getQCONID $1) }
1909 | PREFIXQCONSYM { L1 $! mkQual dataName (getPREFIXQCONSYM $1) }
1911 conid :: { Located RdrName }
1912 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1914 qconsym :: { Located RdrName } -- Qualified or unqualified
1916 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1918 consym :: { Located RdrName }
1919 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1921 -- ':' means only list cons
1922 | ':' { L1 $ consDataCon_RDR }
1925 -----------------------------------------------------------------------------
1928 literal :: { Located HsLit }
1929 : CHAR { L1 $ HsChar $ getCHAR $1 }
1930 | STRING { L1 $ HsString $ getSTRING $1 }
1931 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1932 | PRIMWORD { L1 $ HsWordPrim $ getPRIMWORD $1 }
1933 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1934 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1935 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1936 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1938 -----------------------------------------------------------------------------
1942 : vccurly { () } -- context popped in lexer.
1943 | error {% popContext }
1945 -----------------------------------------------------------------------------
1946 -- Miscellaneous (mostly renamings)
1948 modid :: { Located ModuleName }
1949 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1950 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1953 (unpackFS mod ++ '.':unpackFS c))
1957 : commas ',' { $1 + 1 }
1960 -----------------------------------------------------------------------------
1961 -- Documentation comments
1963 docnext :: { LHsDoc RdrName }
1964 : DOCNEXT {% case parseHaddockParagraphs (tokenise (getDOCNEXT $1)) of {
1965 MyLeft err -> parseError (getLoc $1) err;
1966 MyRight doc -> return (L1 doc) } }
1968 docprev :: { LHsDoc RdrName }
1969 : DOCPREV {% case parseHaddockParagraphs (tokenise (getDOCPREV $1)) of {
1970 MyLeft err -> parseError (getLoc $1) err;
1971 MyRight doc -> return (L1 doc) } }
1973 docnamed :: { Located (String, (HsDoc RdrName)) }
1975 let string = getDOCNAMED $1
1976 (name, rest) = break isSpace string
1977 in case parseHaddockParagraphs (tokenise rest) of {
1978 MyLeft err -> parseError (getLoc $1) err;
1979 MyRight doc -> return (L1 (name, doc)) } }
1981 docsection :: { Located (Int, HsDoc RdrName) }
1982 : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1983 case parseHaddockString (tokenise doc) of {
1984 MyLeft err -> parseError (getLoc $1) err;
1985 MyRight doc -> return (L1 (n, doc)) } }
1987 moduleheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
1988 : DOCNEXT {% let string = getDOCNEXT $1 in
1989 case parseModuleHeader string of {
1990 Right (str, info) ->
1991 case parseHaddockParagraphs (tokenise str) of {
1992 MyLeft err -> parseError (getLoc $1) err;
1993 MyRight doc -> return (info, Just doc);
1995 Left err -> parseError (getLoc $1) err
1998 maybe_docprev :: { Maybe (LHsDoc RdrName) }
1999 : docprev { Just $1 }
2000 | {- empty -} { Nothing }
2002 maybe_docnext :: { Maybe (LHsDoc RdrName) }
2003 : docnext { Just $1 }
2004 | {- empty -} { Nothing }
2008 happyError = srcParseFail
2010 getVARID (L _ (ITvarid x)) = x
2011 getCONID (L _ (ITconid x)) = x
2012 getVARSYM (L _ (ITvarsym x)) = x
2013 getCONSYM (L _ (ITconsym x)) = x
2014 getQVARID (L _ (ITqvarid x)) = x
2015 getQCONID (L _ (ITqconid x)) = x
2016 getQVARSYM (L _ (ITqvarsym x)) = x
2017 getQCONSYM (L _ (ITqconsym x)) = x
2018 getPREFIXQVARSYM (L _ (ITprefixqvarsym x)) = x
2019 getPREFIXQCONSYM (L _ (ITprefixqconsym x)) = x
2020 getIPDUPVARID (L _ (ITdupipvarid x)) = x
2021 getCHAR (L _ (ITchar x)) = x
2022 getSTRING (L _ (ITstring x)) = x
2023 getINTEGER (L _ (ITinteger x)) = x
2024 getRATIONAL (L _ (ITrational x)) = x
2025 getPRIMCHAR (L _ (ITprimchar x)) = x
2026 getPRIMSTRING (L _ (ITprimstring x)) = x
2027 getPRIMINTEGER (L _ (ITprimint x)) = x
2028 getPRIMWORD (L _ (ITprimword x)) = x
2029 getPRIMFLOAT (L _ (ITprimfloat x)) = x
2030 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
2031 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
2032 getINLINE (L _ (ITinline_prag b)) = b
2033 getINLINE_CONLIKE (L _ (ITinline_conlike_prag b)) = b
2034 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
2036 getDOCNEXT (L _ (ITdocCommentNext x)) = x
2037 getDOCPREV (L _ (ITdocCommentPrev x)) = x
2038 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
2039 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
2041 getSCC :: Located Token -> P FastString
2042 getSCC lt = do let s = getSTRING lt
2043 err = "Spaces are not allowed in SCCs"
2044 -- We probably actually want to be more restrictive than this
2045 if ' ' `elem` unpackFS s
2046 then failSpanMsgP (getLoc lt) (text err)
2049 -- Utilities for combining source spans
2050 comb2 :: Located a -> Located b -> SrcSpan
2051 comb2 a b = a `seq` b `seq` combineLocs a b
2053 comb3 :: Located a -> Located b -> Located c -> SrcSpan
2054 comb3 a b c = a `seq` b `seq` c `seq`
2055 combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
2057 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
2058 comb4 a b c d = a `seq` b `seq` c `seq` d `seq`
2059 (combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
2060 combineSrcSpans (getLoc c) (getLoc d))
2062 -- strict constructor version:
2064 sL :: SrcSpan -> a -> Located a
2065 sL span a = span `seq` a `seq` L span a
2067 -- Make a source location for the file. We're a bit lazy here and just
2068 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
2069 -- try to find the span of the whole file (ToDo).
2070 fileSrcSpan :: P SrcSpan
2073 let loc = mkSrcLoc (srcLocFile l) 1 0;
2074 return (mkSrcSpan loc loc)