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 Class ( FunDep )
50 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
51 Activation(..), RuleMatchInfo(..), defaultInlineSpec )
55 import {-# SOURCE #-} HaddockLex hiding ( Token )
59 import Maybes ( orElse )
62 import Control.Monad ( unless )
65 import Control.Monad ( mplus )
69 -----------------------------------------------------------------------------
72 Conflicts: 33 shift/reduce
75 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
76 would think the two should never occur in the same context.
80 -----------------------------------------------------------------------------
83 Conflicts: 34 shift/reduce
86 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
87 would think the two should never occur in the same context.
91 -----------------------------------------------------------------------------
94 Conflicts: 32 shift/reduce
97 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
98 would think the two should never occur in the same context.
102 -----------------------------------------------------------------------------
105 Conflicts: 37 shift/reduce
108 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
109 would think the two should never occur in the same context.
113 -----------------------------------------------------------------------------
114 Conflicts: 38 shift/reduce (1.25)
116 10 for abiguity in 'if x then y else z + 1' [State 178]
117 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
118 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
120 1 for ambiguity in 'if x then y else z :: T' [State 178]
121 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
123 4 for ambiguity in 'if x then y else z -< e' [State 178]
124 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
125 There are four such operators: -<, >-, -<<, >>-
128 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
129 Which of these two is intended?
131 (x::T) -> T -- Rhs is T
134 (x::T -> T) -> .. -- Rhs is ...
136 10 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
139 As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
140 Same duplication between states 11 and 253 as the previous case
142 1 for ambiguity in 'let ?x ...' [State 329]
143 the parser can't tell whether the ?x is the lhs of a normal binding or
144 an implicit binding. Fortunately resolving as shift gives it the only
145 sensible meaning, namely the lhs of an implicit binding.
147 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
148 we don't know whether the '[' starts the activation or not: it
149 might be the start of the declaration with the activation being
150 empty. --SDM 1/4/2002
152 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
153 since 'forall' is a valid variable name, we don't know whether
154 to treat a forall on the input as the beginning of a quantifier
155 or the beginning of the rule itself. Resolving to shift means
156 it's always treated as a quantifier, hence the above is disallowed.
157 This saves explicitly defining a grammar for the rule lhs that
158 doesn't include 'forall'.
160 1 for ambiguity when the source file starts with "-- | doc". We need another
161 token of lookahead to determine if a top declaration or the 'module' keyword
162 follows. Shift parses as if the 'module' keyword follows.
164 -- ---------------------------------------------------------------------------
165 -- Adding location info
167 This is done in a stylised way using the three macros below, L0, L1
168 and LL. Each of these macros can be thought of as having type
170 L0, L1, LL :: a -> Located a
172 They each add a SrcSpan to their argument.
174 L0 adds 'noSrcSpan', used for empty productions
175 -- This doesn't seem to work anymore -=chak
177 L1 for a production with a single token on the lhs. Grabs the SrcSpan
180 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
181 the first and last tokens.
183 These suffice for the majority of cases. However, we must be
184 especially careful with empty productions: LL won't work if the first
185 or last token on the lhs can represent an empty span. In these cases,
186 we have to calculate the span using more of the tokens from the lhs, eg.
188 | 'newtype' tycl_hdr '=' newconstr deriving
190 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
192 We provide comb3 and comb4 functions which are useful in such cases.
194 Be careful: there's no checking that you actually got this right, the
195 only symptom will be that the SrcSpans of your syntax will be
199 * We must expand these macros *before* running Happy, which is why this file is
200 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
202 #define L0 L noSrcSpan
203 #define L1 sL (getLoc $1)
204 #define LL sL (comb2 $1 $>)
206 -- -----------------------------------------------------------------------------
211 '_' { L _ ITunderscore } -- Haskell keywords
213 'case' { L _ ITcase }
214 'class' { L _ ITclass }
215 'data' { L _ ITdata }
216 'default' { L _ ITdefault }
217 'deriving' { L _ ITderiving }
219 'else' { L _ ITelse }
220 'hiding' { L _ IThiding }
222 'import' { L _ ITimport }
224 'infix' { L _ ITinfix }
225 'infixl' { L _ ITinfixl }
226 'infixr' { L _ ITinfixr }
227 'instance' { L _ ITinstance }
229 'module' { L _ ITmodule }
230 'newtype' { L _ ITnewtype }
232 'qualified' { L _ ITqualified }
233 'then' { L _ ITthen }
234 'type' { L _ ITtype }
235 'where' { L _ ITwhere }
236 '_scc_' { L _ ITscc } -- ToDo: remove
238 'forall' { L _ ITforall } -- GHC extension keywords
239 'foreign' { L _ ITforeign }
240 'export' { L _ ITexport }
241 'label' { L _ ITlabel }
242 'dynamic' { L _ ITdynamic }
243 'safe' { L _ ITsafe }
244 'threadsafe' { L _ ITthreadsafe } -- ToDo: remove deprecated alias
245 'unsafe' { L _ ITunsafe }
247 'family' { L _ ITfamily }
248 'stdcall' { L _ ITstdcallconv }
249 'ccall' { L _ ITccallconv }
250 'prim' { L _ ITprimcallconv }
251 'dotnet' { L _ ITdotnet }
252 'proc' { L _ ITproc } -- for arrow notation extension
253 'rec' { L _ ITrec } -- for arrow notation extension
254 'group' { L _ ITgroup } -- for list transform extension
255 'by' { L _ ITby } -- for list transform extension
256 'using' { L _ ITusing } -- for list transform extension
258 '{-# INLINE' { L _ (ITinline_prag _) }
259 '{-# INLINE_CONLIKE' { L _ (ITinline_conlike_prag _) }
260 '{-# SPECIALISE' { L _ ITspec_prag }
261 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
262 '{-# SOURCE' { L _ ITsource_prag }
263 '{-# RULES' { L _ ITrules_prag }
264 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
265 '{-# SCC' { L _ ITscc_prag }
266 '{-# GENERATED' { L _ ITgenerated_prag }
267 '{-# DEPRECATED' { L _ ITdeprecated_prag }
268 '{-# WARNING' { L _ ITwarning_prag }
269 '{-# UNPACK' { L _ ITunpack_prag }
270 '{-# ANN' { L _ ITann_prag }
271 '#-}' { L _ ITclose_prag }
273 '..' { L _ ITdotdot } -- reserved symbols
275 '::' { L _ ITdcolon }
279 '<-' { L _ ITlarrow }
280 '->' { L _ ITrarrow }
283 '=>' { L _ ITdarrow }
287 '-<' { L _ ITlarrowtail } -- for arrow notation
288 '>-' { L _ ITrarrowtail } -- for arrow notation
289 '-<<' { L _ ITLarrowtail } -- for arrow notation
290 '>>-' { L _ ITRarrowtail } -- for arrow notation
293 '{' { L _ ITocurly } -- special symbols
295 '{|' { L _ ITocurlybar }
296 '|}' { L _ ITccurlybar }
297 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
298 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
301 '[:' { L _ ITopabrack }
302 ':]' { L _ ITcpabrack }
305 '(#' { L _ IToubxparen }
306 '#)' { L _ ITcubxparen }
307 '(|' { L _ IToparenbar }
308 '|)' { L _ ITcparenbar }
311 '`' { L _ ITbackquote }
313 VARID { L _ (ITvarid _) } -- identifiers
314 CONID { L _ (ITconid _) }
315 VARSYM { L _ (ITvarsym _) }
316 CONSYM { L _ (ITconsym _) }
317 QVARID { L _ (ITqvarid _) }
318 QCONID { L _ (ITqconid _) }
319 QVARSYM { L _ (ITqvarsym _) }
320 QCONSYM { L _ (ITqconsym _) }
321 PREFIXQVARSYM { L _ (ITprefixqvarsym _) }
322 PREFIXQCONSYM { L _ (ITprefixqconsym _) }
324 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
326 CHAR { L _ (ITchar _) }
327 STRING { L _ (ITstring _) }
328 INTEGER { L _ (ITinteger _) }
329 RATIONAL { L _ (ITrational _) }
331 PRIMCHAR { L _ (ITprimchar _) }
332 PRIMSTRING { L _ (ITprimstring _) }
333 PRIMINTEGER { L _ (ITprimint _) }
334 PRIMWORD { L _ (ITprimword _) }
335 PRIMFLOAT { L _ (ITprimfloat _) }
336 PRIMDOUBLE { L _ (ITprimdouble _) }
338 DOCNEXT { L _ (ITdocCommentNext _) }
339 DOCPREV { L _ (ITdocCommentPrev _) }
340 DOCNAMED { L _ (ITdocCommentNamed _) }
341 DOCSECTION { L _ (ITdocSection _ _) }
344 '[|' { L _ ITopenExpQuote }
345 '[p|' { L _ ITopenPatQuote }
346 '[t|' { L _ ITopenTypQuote }
347 '[d|' { L _ ITopenDecQuote }
348 '|]' { L _ ITcloseQuote }
349 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
350 '$(' { L _ ITparenEscape } -- $( exp )
351 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
352 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
353 TH_QUASIQUOTE { L _ (ITquasiQuote _) }
355 %monad { P } { >>= } { return }
356 %lexer { lexer } { L _ ITeof }
357 %name parseModule module
358 %name parseStmt maybe_stmt
359 %name parseIdentifier identifier
360 %name parseType ctype
361 %partial parseHeader header
362 %tokentype { (Located Token) }
365 -----------------------------------------------------------------------------
366 -- Identifiers; one of the entry points
367 identifier :: { Located RdrName }
372 | '(' '->' ')' { LL $ getRdrName funTyCon }
374 -----------------------------------------------------------------------------
377 -- The place for module deprecation is really too restrictive, but if it
378 -- was allowed at its natural place just before 'module', we get an ugly
379 -- s/r conflict with the second alternative. Another solution would be the
380 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
381 -- either, and DEPRECATED is only expected to be used by people who really
382 -- know what they are doing. :-)
384 module :: { Located (HsModule RdrName) }
385 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' body
386 {% fileSrcSpan >>= \ loc -> case $1 of { (info, doc) ->
387 return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4
390 {% fileSrcSpan >>= \ loc ->
391 return (L loc (HsModule Nothing Nothing
392 (fst $1) (snd $1) Nothing emptyHaddockModInfo
395 maybedocheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
396 : moduleheader { $1 }
397 | {- empty -} { (emptyHaddockModInfo, Nothing) }
399 missing_module_keyword :: { () }
400 : {- empty -} {% pushCurrentContext }
402 maybemodwarning :: { Maybe WarningTxt }
403 : '{-# DEPRECATED' STRING '#-}' { Just (DeprecatedTxt (getSTRING $2)) }
404 | '{-# WARNING' STRING '#-}' { Just (WarningTxt (getSTRING $2)) }
405 | {- empty -} { Nothing }
407 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
409 | vocurly top close { $2 }
411 body2 :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
413 | missing_module_keyword top close { $2 }
415 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
416 : importdecls { (reverse $1,[]) }
417 | importdecls ';' cvtopdecls { (reverse $1,$3) }
418 | cvtopdecls { ([],$1) }
420 cvtopdecls :: { [LHsDecl RdrName] }
421 : topdecls { cvTopDecls $1 }
423 -----------------------------------------------------------------------------
424 -- Module declaration & imports only
426 header :: { Located (HsModule RdrName) }
427 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' header_body
428 {% fileSrcSpan >>= \ loc -> case $1 of { (info, doc) ->
429 return (L loc (HsModule (Just $3) $5 $7 [] $4
431 | missing_module_keyword importdecls
432 {% fileSrcSpan >>= \ loc ->
433 return (L loc (HsModule Nothing Nothing $2 [] Nothing
434 emptyHaddockModInfo Nothing)) }
436 header_body :: { [LImportDecl RdrName] }
437 : '{' importdecls { $2 }
438 | vocurly importdecls { $2 }
440 -----------------------------------------------------------------------------
443 maybeexports :: { Maybe [LIE RdrName] }
444 : '(' exportlist ')' { Just $2 }
445 | {- empty -} { Nothing }
447 exportlist :: { [LIE RdrName] }
448 : expdoclist ',' expdoclist { $1 ++ $3 }
451 exportlist1 :: { [LIE RdrName] }
452 : expdoclist export expdoclist ',' exportlist { $1 ++ ($2 : $3) ++ $5 }
453 | expdoclist export expdoclist { $1 ++ ($2 : $3) }
456 expdoclist :: { [LIE RdrName] }
457 : exp_doc expdoclist { $1 : $2 }
460 exp_doc :: { LIE RdrName }
461 : docsection { L1 (case (unLoc $1) of (n, doc) -> IEGroup n doc) }
462 | docnamed { L1 (IEDocNamed ((fst . unLoc) $1)) }
463 | docnext { L1 (IEDoc (unLoc $1)) }
465 -- No longer allow things like [] and (,,,) to be exported
466 -- They are built in syntax, always available
467 export :: { LIE RdrName }
468 : qvar { L1 (IEVar (unLoc $1)) }
469 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
470 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
471 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
472 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
473 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
475 qcnames :: { [RdrName] }
476 : qcnames ',' qcname_ext { unLoc $3 : $1 }
477 | qcname_ext { [unLoc $1] }
479 qcname_ext :: { Located RdrName } -- Variable or data constructor
480 -- or tagged type constructor
482 | 'type' qcon { sL (comb2 $1 $2)
483 (setRdrNameSpace (unLoc $2)
486 -- Cannot pull into qcname_ext, as qcname is also used in expression.
487 qcname :: { Located RdrName } -- Variable or data constructor
491 -----------------------------------------------------------------------------
492 -- Import Declarations
494 -- import decls can be *empty*, or even just a string of semicolons
495 -- whereas topdecls must contain at least one topdecl.
497 importdecls :: { [LImportDecl RdrName] }
498 : importdecls ';' importdecl { $3 : $1 }
499 | importdecls ';' { $1 }
500 | importdecl { [ $1 ] }
503 importdecl :: { LImportDecl RdrName }
504 : 'import' maybe_src optqualified maybe_pkg modid maybeas maybeimpspec
505 { L (comb4 $1 $5 $6 $7) (ImportDecl $5 $4 $2 $3 (unLoc $6) (unLoc $7)) }
507 maybe_src :: { IsBootInterface }
508 : '{-# SOURCE' '#-}' { True }
509 | {- empty -} { False }
511 maybe_pkg :: { Maybe FastString }
512 : STRING { Just (getSTRING $1) }
513 | {- empty -} { Nothing }
515 optqualified :: { Bool }
516 : 'qualified' { True }
517 | {- empty -} { False }
519 maybeas :: { Located (Maybe ModuleName) }
520 : 'as' modid { LL (Just (unLoc $2)) }
521 | {- empty -} { noLoc Nothing }
523 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
524 : impspec { L1 (Just (unLoc $1)) }
525 | {- empty -} { noLoc Nothing }
527 impspec :: { Located (Bool, [LIE RdrName]) }
528 : '(' exportlist ')' { LL (False, $2) }
529 | 'hiding' '(' exportlist ')' { LL (True, $3) }
531 -----------------------------------------------------------------------------
532 -- Fixity Declarations
536 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
538 infix :: { Located FixityDirection }
539 : 'infix' { L1 InfixN }
540 | 'infixl' { L1 InfixL }
541 | 'infixr' { L1 InfixR }
543 ops :: { Located [Located RdrName] }
544 : ops ',' op { LL ($3 : unLoc $1) }
547 -----------------------------------------------------------------------------
548 -- Top-Level Declarations
550 topdecls :: { OrdList (LHsDecl RdrName) }
551 : topdecls ';' topdecl { $1 `appOL` $3 }
552 | topdecls ';' { $1 }
555 topdecl :: { OrdList (LHsDecl RdrName) }
556 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
557 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
558 | 'instance' inst_type where_inst
559 { let (binds, sigs, ats, _) = cvBindsAndSigs (unLoc $3)
561 unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs ats)))}
562 | stand_alone_deriving { unitOL (LL (DerivD (unLoc $1))) }
563 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
564 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
565 | '{-# DEPRECATED' deprecations '#-}' { $2 }
566 | '{-# WARNING' warnings '#-}' { $2 }
567 | '{-# RULES' rules '#-}' { $2 }
568 | annotation { unitOL $1 }
571 -- Template Haskell Extension
572 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
573 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
574 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
579 cl_decl :: { LTyClDecl RdrName }
580 : 'class' tycl_hdr fds where_cls {% mkClassDecl (comb4 $1 $2 $3 $4) $2 $3 $4 }
582 -- Type declarations (toplevel)
584 ty_decl :: { LTyClDecl RdrName }
585 -- ordinary type synonyms
586 : 'type' type '=' ctypedoc
587 -- Note ctype, not sigtype, on the right of '='
588 -- We allow an explicit for-all but we don't insert one
589 -- in type Foo a = (b,b)
590 -- Instead we just say b is out of scope
592 -- Note the use of type for the head; this allows
593 -- infix type constructors to be declared
594 {% mkTySynonym (comb2 $1 $4) False $2 $4 }
596 -- type family declarations
597 | 'type' 'family' type opt_kind_sig
598 -- Note the use of type for the head; this allows
599 -- infix type constructors to be declared
600 {% mkTyFamily (comb3 $1 $3 $4) TypeFamily $3 (unLoc $4) }
602 -- type instance declarations
603 | 'type' 'instance' type '=' ctype
604 -- Note the use of type for the head; this allows
605 -- infix type constructors and type patterns
606 {% mkTySynonym (comb2 $1 $5) True $3 $5 }
608 -- ordinary data type or newtype declaration
609 | data_or_newtype tycl_hdr constrs deriving
610 {% mkTyData (comb4 $1 $2 $3 $4) (unLoc $1) False $2
611 Nothing (reverse (unLoc $3)) (unLoc $4) }
612 -- We need the location on tycl_hdr in case
613 -- constrs and deriving are both empty
615 -- ordinary GADT declaration
616 | data_or_newtype tycl_hdr opt_kind_sig
617 'where' gadt_constrlist
619 {% mkTyData (comb4 $1 $2 $4 $5) (unLoc $1) False $2
620 (unLoc $3) (reverse (unLoc $5)) (unLoc $6) }
621 -- We need the location on tycl_hdr in case
622 -- constrs and deriving are both empty
624 -- data/newtype family
625 | 'data' 'family' type opt_kind_sig
626 {% mkTyFamily (comb3 $1 $2 $4) DataFamily $3 (unLoc $4) }
628 -- data/newtype instance declaration
629 | data_or_newtype 'instance' tycl_hdr constrs deriving
630 {% mkTyData (comb4 $1 $3 $4 $5) (unLoc $1) True $3
631 Nothing (reverse (unLoc $4)) (unLoc $5) }
633 -- GADT instance declaration
634 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
635 'where' gadt_constrlist
637 {% mkTyData (comb4 $1 $3 $6 $7) (unLoc $1) True $3
638 (unLoc $4) (reverse (unLoc $6)) (unLoc $7) }
640 -- Associated type family declarations
642 -- * They have a different syntax than on the toplevel (no family special
645 -- * They also need to be separate from instances; otherwise, data family
646 -- declarations without a kind signature cause parsing conflicts with empty
647 -- data declarations.
649 at_decl_cls :: { LTyClDecl RdrName }
650 -- type family declarations
651 : 'type' type opt_kind_sig
652 -- Note the use of type for the head; this allows
653 -- infix type constructors to be declared
654 {% mkTyFamily (comb3 $1 $2 $3) TypeFamily $2 (unLoc $3) }
656 -- default type instance
657 | 'type' type '=' ctype
658 -- Note the use of type for the head; this allows
659 -- infix type constructors and type patterns
660 {% mkTySynonym (comb2 $1 $4) True $2 $4 }
662 -- data/newtype family declaration
663 | 'data' type opt_kind_sig
664 {% mkTyFamily (comb3 $1 $2 $3) DataFamily $2 (unLoc $3) }
666 -- Associated type instances
668 at_decl_inst :: { LTyClDecl RdrName }
669 -- type instance declarations
670 : 'type' type '=' ctype
671 -- Note the use of type for the head; this allows
672 -- infix type constructors and type patterns
673 {% mkTySynonym (comb2 $1 $4) True $2 $4 }
675 -- data/newtype instance declaration
676 | data_or_newtype tycl_hdr constrs deriving
677 {% mkTyData (comb4 $1 $2 $3 $4) (unLoc $1) True $2
678 Nothing (reverse (unLoc $3)) (unLoc $4) }
680 -- GADT instance declaration
681 | data_or_newtype tycl_hdr opt_kind_sig
682 'where' gadt_constrlist
684 {% mkTyData (comb4 $1 $2 $5 $6) (unLoc $1) True $2
685 (unLoc $3) (reverse (unLoc $5)) (unLoc $6) }
687 data_or_newtype :: { Located NewOrData }
688 : 'data' { L1 DataType }
689 | 'newtype' { L1 NewType }
691 opt_kind_sig :: { Located (Maybe Kind) }
693 | '::' kind { LL (Just (unLoc $2)) }
695 -- tycl_hdr parses the header of a class or data type decl,
696 -- which takes the form
699 -- (Eq a, Ord b) => T a b
700 -- T Int [a] -- for associated types
701 -- Rather a lot of inlining here, else we get reduce/reduce errors
702 tycl_hdr :: { Located (LHsContext RdrName, LHsType RdrName) }
703 : context '=>' type { LL ($1, $3) }
704 | type { L1 (noLoc [], $1) }
706 -----------------------------------------------------------------------------
707 -- Stand-alone deriving
709 -- Glasgow extension: stand-alone deriving declarations
710 stand_alone_deriving :: { LDerivDecl RdrName }
711 : 'deriving' 'instance' inst_type {% checkDerivDecl (LL (DerivDecl $3)) }
713 -----------------------------------------------------------------------------
714 -- Nested declarations
716 -- Declaration in class bodies
718 decl_cls :: { Located (OrdList (LHsDecl RdrName)) }
719 decl_cls : at_decl_cls { LL (unitOL (L1 (TyClD (unLoc $1)))) }
722 decls_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
723 : decls_cls ';' decl_cls { LL (unLoc $1 `appOL` unLoc $3) }
724 | decls_cls ';' { LL (unLoc $1) }
726 | {- empty -} { noLoc nilOL }
730 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
731 : '{' decls_cls '}' { LL (unLoc $2) }
732 | vocurly decls_cls close { $2 }
736 where_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
737 -- No implicit parameters
738 -- May have type declarations
739 : 'where' decllist_cls { LL (unLoc $2) }
740 | {- empty -} { noLoc nilOL }
742 -- Declarations in instance bodies
744 decl_inst :: { Located (OrdList (LHsDecl RdrName)) }
745 decl_inst : at_decl_inst { LL (unitOL (L1 (TyClD (unLoc $1)))) }
748 decls_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
749 : decls_inst ';' decl_inst { LL (unLoc $1 `appOL` unLoc $3) }
750 | decls_inst ';' { LL (unLoc $1) }
752 | {- empty -} { noLoc nilOL }
755 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
756 : '{' decls_inst '}' { LL (unLoc $2) }
757 | vocurly decls_inst close { $2 }
761 where_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
762 -- No implicit parameters
763 -- May have type declarations
764 : 'where' decllist_inst { LL (unLoc $2) }
765 | {- empty -} { noLoc nilOL }
767 -- Declarations in binding groups other than classes and instances
769 decls :: { Located (OrdList (LHsDecl RdrName)) }
770 : decls ';' decl { let { this = unLoc $3;
772 these = rest `appOL` this }
773 in rest `seq` this `seq` these `seq`
775 | decls ';' { LL (unLoc $1) }
777 | {- empty -} { noLoc nilOL }
779 decllist :: { Located (OrdList (LHsDecl RdrName)) }
780 : '{' decls '}' { LL (unLoc $2) }
781 | vocurly decls close { $2 }
783 -- Binding groups other than those of class and instance declarations
785 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
786 -- No type declarations
787 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
788 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
789 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
791 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
792 -- No type declarations
793 : 'where' binds { LL (unLoc $2) }
794 | {- empty -} { noLoc emptyLocalBinds }
797 -----------------------------------------------------------------------------
798 -- Transformation Rules
800 rules :: { OrdList (LHsDecl RdrName) }
801 : rules ';' rule { $1 `snocOL` $3 }
804 | {- empty -} { nilOL }
806 rule :: { LHsDecl RdrName }
807 : STRING activation rule_forall infixexp '=' exp
808 { LL $ RuleD (HsRule (getSTRING $1)
809 ($2 `orElse` AlwaysActive)
810 $3 $4 placeHolderNames $6 placeHolderNames) }
812 activation :: { Maybe Activation }
813 : {- empty -} { Nothing }
814 | explicit_activation { Just $1 }
816 explicit_activation :: { Activation } -- In brackets
817 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
818 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
820 rule_forall :: { [RuleBndr RdrName] }
821 : 'forall' rule_var_list '.' { $2 }
824 rule_var_list :: { [RuleBndr RdrName] }
826 | rule_var rule_var_list { $1 : $2 }
828 rule_var :: { RuleBndr RdrName }
829 : varid { RuleBndr $1 }
830 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
832 -----------------------------------------------------------------------------
833 -- Warnings and deprecations (c.f. rules)
835 warnings :: { OrdList (LHsDecl RdrName) }
836 : warnings ';' warning { $1 `appOL` $3 }
837 | warnings ';' { $1 }
839 | {- empty -} { nilOL }
841 -- SUP: TEMPORARY HACK, not checking for `module Foo'
842 warning :: { OrdList (LHsDecl RdrName) }
844 { toOL [ LL $ WarningD (Warning n (WarningTxt (getSTRING $2)))
847 deprecations :: { OrdList (LHsDecl RdrName) }
848 : deprecations ';' deprecation { $1 `appOL` $3 }
849 | deprecations ';' { $1 }
851 | {- empty -} { nilOL }
853 -- SUP: TEMPORARY HACK, not checking for `module Foo'
854 deprecation :: { OrdList (LHsDecl RdrName) }
856 { toOL [ LL $ WarningD (Warning n (DeprecatedTxt (getSTRING $2)))
859 -----------------------------------------------------------------------------
861 annotation :: { LHsDecl RdrName }
862 : '{-# ANN' name_var aexp '#-}' { LL (AnnD $ HsAnnotation (ValueAnnProvenance (unLoc $2)) $3) }
863 | '{-# ANN' 'type' tycon aexp '#-}' { LL (AnnD $ HsAnnotation (TypeAnnProvenance (unLoc $3)) $4) }
864 | '{-# ANN' 'module' aexp '#-}' { LL (AnnD $ HsAnnotation ModuleAnnProvenance $3) }
867 -----------------------------------------------------------------------------
868 -- Foreign import and export declarations
870 fdecl :: { LHsDecl RdrName }
871 fdecl : 'import' callconv safety fspec
872 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
873 | 'import' callconv fspec
874 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
876 | 'export' callconv fspec
877 {% mkExport $2 (unLoc $3) >>= return.LL }
879 callconv :: { CallConv }
880 : 'stdcall' { CCall StdCallConv }
881 | 'ccall' { CCall CCallConv }
882 | 'prim' { CCall PrimCallConv}
883 | 'dotnet' { DNCall }
886 : 'unsafe' { PlayRisky }
887 | 'safe' { PlaySafe False }
888 | 'threadsafe' { PlaySafe True } -- deprecated alias
890 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
891 : STRING var '::' sigtypedoc { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
892 | var '::' sigtypedoc { LL (noLoc nilFS, $1, $3) }
893 -- if the entity string is missing, it defaults to the empty string;
894 -- the meaning of an empty entity string depends on the calling
897 -----------------------------------------------------------------------------
900 opt_sig :: { Maybe (LHsType RdrName) }
901 : {- empty -} { Nothing }
902 | '::' sigtype { Just $2 }
904 opt_asig :: { Maybe (LHsType RdrName) }
905 : {- empty -} { Nothing }
906 | '::' atype { Just $2 }
908 sigtype :: { LHsType RdrName } -- Always a HsForAllTy,
909 -- to tell the renamer where to generalise
910 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
911 -- Wrap an Implicit forall if there isn't one there already
913 sigtypedoc :: { LHsType RdrName } -- Always a HsForAllTy
914 : ctypedoc { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
915 -- Wrap an Implicit forall if there isn't one there already
917 sig_vars :: { Located [Located RdrName] }
918 : sig_vars ',' var { LL ($3 : unLoc $1) }
921 sigtypes1 :: { [LHsType RdrName] } -- Always HsForAllTys
923 | sigtype ',' sigtypes1 { $1 : $3 }
925 -----------------------------------------------------------------------------
928 infixtype :: { LHsType RdrName }
929 : btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
930 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
932 strict_mark :: { Located HsBang }
933 : '!' { L1 HsStrict }
934 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
936 -- A ctype is a for-all type
937 ctype :: { LHsType RdrName }
938 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
939 | context '=>' ctype { LL $ mkImplicitHsForAllTy $1 $3 }
940 -- A type of form (context => type) is an *implicit* HsForAllTy
941 | ipvar '::' type { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
944 ----------------------
945 -- Notes for 'ctypedoc'
946 -- It would have been nice to simplify the grammar by unifying `ctype` and
947 -- ctypedoc` into one production, allowing comments on types everywhere (and
948 -- rejecting them after parsing, where necessary). This is however not possible
949 -- since it leads to ambiguity. The reason is the support for comments on record
951 -- data R = R { field :: Int -- ^ comment on the field }
952 -- If we allow comments on types here, it's not clear if the comment applies
953 -- to 'field' or to 'Int'. So we must use `ctype` to describe the type.
955 ctypedoc :: { LHsType RdrName }
956 : 'forall' tv_bndrs '.' ctypedoc { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
957 | context '=>' ctypedoc { LL $ mkImplicitHsForAllTy $1 $3 }
958 -- A type of form (context => type) is an *implicit* HsForAllTy
959 | ipvar '::' type { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
962 ----------------------
963 -- Notes for 'context'
964 -- We parse a context as a btype so that we don't get reduce/reduce
965 -- errors in ctype. The basic problem is that
967 -- looks so much like a tuple type. We can't tell until we find the =>
969 -- We have the t1 ~ t2 form both in 'context' and in type,
970 -- to permit an individual equational constraint without parenthesis.
971 -- Thus for some reason we allow f :: a~b => blah
972 -- but not f :: ?x::Int => blah
973 context :: { LHsContext RdrName }
974 : btype '~' btype {% checkContext
975 (LL $ HsPredTy (HsEqualP $1 $3)) }
976 | btype {% checkContext $1 }
978 type :: { LHsType RdrName }
980 | btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
981 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
982 | btype '->' ctype { LL $ HsFunTy $1 $3 }
983 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
985 typedoc :: { LHsType RdrName }
987 | btype docprev { LL $ HsDocTy $1 $2 }
988 | btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
989 | btype qtyconop type docprev { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
990 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
991 | btype tyvarop type docprev { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
992 | btype '->' ctypedoc { LL $ HsFunTy $1 $3 }
993 | btype docprev '->' ctypedoc { LL $ HsFunTy (L (comb2 $1 $2) (HsDocTy $1 $2)) $4 }
994 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
996 btype :: { LHsType RdrName }
997 : btype atype { LL $ HsAppTy $1 $2 }
1000 atype :: { LHsType RdrName }
1001 : gtycon { L1 (HsTyVar (unLoc $1)) }
1002 | tyvar { L1 (HsTyVar (unLoc $1)) }
1003 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) } -- Constructor sigs only
1004 | '{' fielddecls '}' { LL $ HsRecTy $2 } -- Constructor sigs only
1005 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
1006 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
1007 | '[' ctype ']' { LL $ HsListTy $2 }
1008 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
1009 | '(' ctype ')' { LL $ HsParTy $2 }
1010 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
1011 | '$(' exp ')' { LL $ HsSpliceTy (mkHsSplice $2 ) }
1012 | TH_ID_SPLICE { LL $ HsSpliceTy (mkHsSplice
1013 (L1 $ HsVar (mkUnqual varName
1014 (getTH_ID_SPLICE $1)))) } -- $x
1016 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
1018 -- An inst_type is what occurs in the head of an instance decl
1019 -- e.g. (Foo a, Gaz b) => Wibble a b
1020 -- It's kept as a single type, with a MonoDictTy at the right
1021 -- hand corner, for convenience.
1022 inst_type :: { LHsType RdrName }
1023 : sigtype {% checkInstType $1 }
1025 inst_types1 :: { [LHsType RdrName] }
1026 : inst_type { [$1] }
1027 | inst_type ',' inst_types1 { $1 : $3 }
1029 comma_types0 :: { [LHsType RdrName] }
1030 : comma_types1 { $1 }
1031 | {- empty -} { [] }
1033 comma_types1 :: { [LHsType RdrName] }
1035 | ctype ',' comma_types1 { $1 : $3 }
1037 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1038 : tv_bndr tv_bndrs { $1 : $2 }
1039 | {- empty -} { [] }
1041 tv_bndr :: { LHsTyVarBndr RdrName }
1042 : tyvar { L1 (UserTyVar (unLoc $1)) }
1043 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
1046 fds :: { Located [Located (FunDep RdrName)] }
1047 : {- empty -} { noLoc [] }
1048 | '|' fds1 { LL (reverse (unLoc $2)) }
1050 fds1 :: { Located [Located (FunDep RdrName)] }
1051 : fds1 ',' fd { LL ($3 : unLoc $1) }
1054 fd :: { Located (FunDep RdrName) }
1055 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
1056 (reverse (unLoc $1), reverse (unLoc $3)) }
1058 varids0 :: { Located [RdrName] }
1059 : {- empty -} { noLoc [] }
1060 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
1062 -----------------------------------------------------------------------------
1065 kind :: { Located Kind }
1067 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1069 akind :: { Located Kind }
1070 : '*' { L1 liftedTypeKind }
1071 | '!' { L1 unliftedTypeKind }
1072 | '(' kind ')' { LL (unLoc $2) }
1075 -----------------------------------------------------------------------------
1076 -- Datatype declarations
1078 gadt_constrlist :: { Located [LConDecl RdrName] }
1079 : '{' gadt_constrs '}' { LL (unLoc $2) }
1080 | vocurly gadt_constrs close { $2 }
1082 gadt_constrs :: { Located [LConDecl RdrName] }
1083 : gadt_constrs ';' gadt_constr { sL (comb2 $1 (head $3)) ($3 ++ unLoc $1) }
1084 | gadt_constrs ';' { $1 }
1085 | gadt_constr { sL (getLoc (head $1)) $1 }
1087 -- We allow the following forms:
1088 -- C :: Eq a => a -> T a
1089 -- C :: forall a. Eq a => !a -> T a
1090 -- D { x,y :: a } :: T a
1091 -- forall a. Eq a => D { x,y :: a } :: T a
1093 gadt_constr :: { [LConDecl RdrName] }
1094 : con_list '::' sigtype
1095 { map (sL (comb2 $1 $3)) (mkGadtDecl (unLoc $1) $3) }
1097 -- Deprecated syntax for GADT record declarations
1098 | oqtycon '{' fielddecls '}' '::' sigtype
1099 {% do { cd <- mkDeprecatedGadtRecordDecl (comb2 $1 $6) $1 $3 $6
1102 constrs :: { Located [LConDecl RdrName] }
1103 : {- empty; a GHC extension -} { noLoc [] }
1104 | maybe_docnext '=' constrs1 { L (comb2 $2 $3) (addConDocs (unLoc $3) $1) }
1106 constrs1 :: { Located [LConDecl RdrName] }
1107 : constrs1 maybe_docnext '|' maybe_docprev constr { LL (addConDoc $5 $2 : addConDocFirst (unLoc $1) $4) }
1108 | constr { L1 [$1] }
1110 constr :: { LConDecl RdrName }
1111 : maybe_docnext forall context '=>' constr_stuff maybe_docprev
1112 { let (con,details) = unLoc $5 in
1113 addConDoc (L (comb4 $2 $3 $4 $5) (mkSimpleConDecl con (unLoc $2) $3 details))
1115 | maybe_docnext forall constr_stuff maybe_docprev
1116 { let (con,details) = unLoc $3 in
1117 addConDoc (L (comb2 $2 $3) (mkSimpleConDecl con (unLoc $2) (noLoc []) details))
1120 forall :: { Located [LHsTyVarBndr RdrName] }
1121 : 'forall' tv_bndrs '.' { LL $2 }
1122 | {- empty -} { noLoc [] }
1124 constr_stuff :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1125 -- We parse the constructor declaration
1127 -- as a btype (treating C as a type constructor) and then convert C to be
1128 -- a data constructor. Reason: it might continue like this:
1130 -- in which case C really would be a type constructor. We can't resolve this
1131 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1132 : btype {% splitCon $1 >>= return.LL }
1133 | btype conop btype { LL ($2, InfixCon $1 $3) }
1135 fielddecls :: { [ConDeclField RdrName] }
1136 : {- empty -} { [] }
1137 | fielddecls1 { $1 }
1139 fielddecls1 :: { [ConDeclField RdrName] }
1140 : fielddecl maybe_docnext ',' maybe_docprev fielddecls1
1141 { [ addFieldDoc f $4 | f <- $1 ] ++ addFieldDocs $5 $2 }
1142 -- This adds the doc $4 to each field separately
1145 fielddecl :: { [ConDeclField RdrName] } -- A list because of f,g :: Int
1146 : maybe_docnext sig_vars '::' ctype maybe_docprev { [ ConDeclField fld $4 ($1 `mplus` $5)
1147 | fld <- reverse (unLoc $2) ] }
1149 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1150 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1151 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1152 -- We don't allow a context, but that's sorted out by the type checker.
1153 deriving :: { Located (Maybe [LHsType RdrName]) }
1154 : {- empty -} { noLoc Nothing }
1155 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1156 ; p <- checkInstType (L loc (HsTyVar tv))
1157 ; return (LL (Just [p])) } }
1158 | 'deriving' '(' ')' { LL (Just []) }
1159 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1160 -- Glasgow extension: allow partial
1161 -- applications in derivings
1163 -----------------------------------------------------------------------------
1164 -- Value definitions
1166 {- There's an awkward overlap with a type signature. Consider
1167 f :: Int -> Int = ...rhs...
1168 Then we can't tell whether it's a type signature or a value
1169 definition with a result signature until we see the '='.
1170 So we have to inline enough to postpone reductions until we know.
1174 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1175 instead of qvar, we get another shift/reduce-conflict. Consider the
1178 { (^^) :: Int->Int ; } Type signature; only var allowed
1180 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1181 qvar allowed (because of instance decls)
1183 We can't tell whether to reduce var to qvar until after we've read the signatures.
1186 docdecl :: { LHsDecl RdrName }
1187 : docdecld { L1 (DocD (unLoc $1)) }
1189 docdecld :: { LDocDecl RdrName }
1190 : docnext { L1 (DocCommentNext (unLoc $1)) }
1191 | docprev { L1 (DocCommentPrev (unLoc $1)) }
1192 | docnamed { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1193 | docsection { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1195 decl :: { Located (OrdList (LHsDecl RdrName)) }
1197 | '!' aexp rhs {% do { pat <- checkPattern $2;
1198 return (LL $ unitOL $ LL $ ValD (
1199 PatBind (LL $ BangPat pat) (unLoc $3)
1200 placeHolderType placeHolderNames)) } }
1201 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1202 let { l = comb2 $1 $> };
1203 return $! (sL l (unitOL $! (sL l $ ValD r))) } }
1204 | docdecl { LL $ unitOL $1 }
1206 rhs :: { Located (GRHSs RdrName) }
1207 : '=' exp wherebinds { sL (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1208 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1210 gdrhs :: { Located [LGRHS RdrName] }
1211 : gdrhs gdrh { LL ($2 : unLoc $1) }
1214 gdrh :: { LGRHS RdrName }
1215 : '|' guardquals '=' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1217 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1218 : infixexp '::' sigtypedoc
1219 {% do s <- checkValSig $1 $3;
1220 return (LL $ unitOL (LL $ SigD s)) }
1221 -- See the above notes for why we need infixexp here
1222 | var ',' sig_vars '::' sigtypedoc
1223 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1224 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1226 | '{-# INLINE' activation qvar '#-}'
1227 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 FunLike (getINLINE $1)))) }
1228 | '{-# INLINE_CONLIKE' activation qvar '#-}'
1229 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 ConLike (getINLINE_CONLIKE $1)))) }
1230 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1231 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1233 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1234 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 FunLike (getSPEC_INLINE $1)))
1236 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1237 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1239 -----------------------------------------------------------------------------
1242 exp :: { LHsExpr RdrName }
1243 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1244 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1245 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1246 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1247 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1250 infixexp :: { LHsExpr RdrName }
1252 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1254 exp10 :: { LHsExpr RdrName }
1255 : '\\' apat apats opt_asig '->' exp
1256 { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1259 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1260 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1261 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1262 | '-' fexp { LL $ NegApp $2 noSyntaxExpr }
1264 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1265 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1266 return (L loc (mkHsDo DoExpr stmts body)) }
1267 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1268 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1269 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1270 | scc_annot exp { LL $ if opt_SccProfilingOn
1271 then HsSCC (unLoc $1) $2
1273 | hpc_annot exp { LL $ if opt_Hpc
1274 then HsTickPragma (unLoc $1) $2
1277 | 'proc' aexp '->' exp
1278 {% checkPattern $2 >>= \ p ->
1279 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1280 placeHolderType undefined)) }
1281 -- TODO: is LL right here?
1283 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1284 -- hdaume: core annotation
1287 scc_annot :: { Located FastString }
1288 : '_scc_' STRING {% (addWarning Opt_WarnWarningsDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1289 ( do scc <- getSCC $2; return $ LL scc ) }
1290 | '{-# SCC' STRING '#-}' {% do scc <- getSCC $2; return $ LL scc }
1292 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1293 : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1294 { LL $ (getSTRING $2
1295 ,( fromInteger $ getINTEGER $3
1296 , fromInteger $ getINTEGER $5
1298 ,( fromInteger $ getINTEGER $7
1299 , fromInteger $ getINTEGER $9
1304 fexp :: { LHsExpr RdrName }
1305 : fexp aexp { LL $ HsApp $1 $2 }
1308 aexp :: { LHsExpr RdrName }
1309 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1310 | '~' aexp { LL $ ELazyPat $2 }
1313 aexp1 :: { LHsExpr RdrName }
1314 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1318 -- Here was the syntax for type applications that I was planning
1319 -- but there are difficulties (e.g. what order for type args)
1320 -- so it's not enabled yet.
1321 -- But this case *is* used for the left hand side of a generic definition,
1322 -- which is parsed as an expression before being munged into a pattern
1323 | qcname '{|' type '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1324 (sL (getLoc $3) (HsType $3)) }
1326 aexp2 :: { LHsExpr RdrName }
1327 : ipvar { L1 (HsIPVar $! unLoc $1) }
1328 | qcname { L1 (HsVar $! unLoc $1) }
1329 | literal { L1 (HsLit $! unLoc $1) }
1330 -- This will enable overloaded strings permanently. Normally the renamer turns HsString
1331 -- into HsOverLit when -foverloaded-strings is on.
1332 -- | STRING { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1333 | INTEGER { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1334 | RATIONAL { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1336 -- N.B.: sections get parsed by these next two productions.
1337 -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't correct Haskell98
1338 -- (you'd have to write '((+ 3), (4 -))')
1339 -- but the less cluttered version fell out of having texps.
1340 | '(' texp ')' { LL (HsPar $2) }
1341 | '(' tup_exprs ')' { LL (ExplicitTuple $2 Boxed) }
1343 | '(#' texp '#)' { LL (ExplicitTuple [Present $2] Unboxed) }
1344 | '(#' tup_exprs '#)' { LL (ExplicitTuple $2 Unboxed) }
1346 | '[' list ']' { LL (unLoc $2) }
1347 | '[:' parr ':]' { LL (unLoc $2) }
1348 | '_' { L1 EWildPat }
1350 -- Template Haskell Extension
1351 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1352 (L1 $ HsVar (mkUnqual varName
1353 (getTH_ID_SPLICE $1)))) } -- $x
1354 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1356 | TH_QUASIQUOTE { let { loc = getLoc $1
1357 ; ITquasiQuote (quoter, quote, quoteSpan) = unLoc $1
1358 ; quoterId = mkUnqual varName quoter
1360 in sL loc $ HsQuasiQuoteE (mkHsQuasiQuote quoterId quoteSpan quote) }
1361 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1362 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1363 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1364 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1365 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1366 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1367 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1368 return (LL $ HsBracket (PatBr p)) }
1369 | '[d|' cvtopbody '|]' {% checkDecBrGroup $2 >>= \g ->
1370 return (LL $ HsBracket (DecBr g)) }
1372 -- arrow notation extension
1373 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1375 cmdargs :: { [LHsCmdTop RdrName] }
1376 : cmdargs acmd { $2 : $1 }
1377 | {- empty -} { [] }
1379 acmd :: { LHsCmdTop RdrName }
1380 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1382 cvtopbody :: { [LHsDecl RdrName] }
1383 : '{' cvtopdecls0 '}' { $2 }
1384 | vocurly cvtopdecls0 close { $2 }
1386 cvtopdecls0 :: { [LHsDecl RdrName] }
1387 : {- empty -} { [] }
1390 -----------------------------------------------------------------------------
1391 -- Tuple expressions
1393 -- "texp" is short for tuple expressions:
1394 -- things that can appear unparenthesized as long as they're
1395 -- inside parens or delimitted by commas
1396 texp :: { LHsExpr RdrName }
1399 -- Note [Parsing sections]
1400 -- ~~~~~~~~~~~~~~~~~~~~~~~
1401 -- We include left and right sections here, which isn't
1402 -- technically right according to Haskell 98. For example
1403 -- (3 +, True) isn't legal
1404 -- However, we want to parse bang patterns like
1406 -- and it's convenient to do so here as a section
1407 -- Then when converting expr to pattern we unravel it again
1408 -- Meanwhile, the renamer checks that real sections appear
1410 | infixexp qop { LL $ SectionL $1 $2 }
1411 | qopm infixexp { LL $ SectionR $1 $2 }
1413 -- View patterns get parenthesized above
1414 | exp '->' exp { LL $ EViewPat $1 $3 }
1416 -- Always at least one comma
1417 tup_exprs :: { [HsTupArg RdrName] }
1418 : texp commas_tup_tail { Present $1 : $2 }
1419 | commas tup_tail { replicate $1 missingTupArg ++ $2 }
1421 -- Always starts with commas; always follows an expr
1422 commas_tup_tail :: { [HsTupArg RdrName] }
1423 commas_tup_tail : commas tup_tail { replicate ($1-1) missingTupArg ++ $2 }
1425 -- Always follows a comma
1426 tup_tail :: { [HsTupArg RdrName] }
1427 : texp commas_tup_tail { Present $1 : $2 }
1428 | texp { [Present $1] }
1429 | {- empty -} { [missingTupArg] }
1431 -----------------------------------------------------------------------------
1434 -- The rules below are little bit contorted to keep lexps left-recursive while
1435 -- avoiding another shift/reduce-conflict.
1437 list :: { LHsExpr RdrName }
1438 : texp { L1 $ ExplicitList placeHolderType [$1] }
1439 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1440 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1441 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1442 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1443 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1444 | texp '|' flattenedpquals { sL (comb2 $1 $>) $ mkHsDo ListComp (unLoc $3) $1 }
1446 lexps :: { Located [LHsExpr RdrName] }
1447 : lexps ',' texp { LL (((:) $! $3) $! unLoc $1) }
1448 | texp ',' texp { LL [$3,$1] }
1450 -----------------------------------------------------------------------------
1451 -- List Comprehensions
1453 flattenedpquals :: { Located [LStmt RdrName] }
1454 : pquals { case (unLoc $1) of
1455 ParStmt [(qs, _)] -> L1 qs
1456 -- We just had one thing in our "parallel" list so
1457 -- we simply return that thing directly
1460 -- We actually found some actual parallel lists so
1461 -- we leave them into as a ParStmt
1464 pquals :: { LStmt RdrName }
1465 : pquals1 { L1 (ParStmt [(qs, undefined) | qs <- (reverse (unLoc $1))]) }
1467 pquals1 :: { Located [[LStmt RdrName]] }
1468 : pquals1 '|' squals { LL (unLoc $3 : unLoc $1) }
1469 | squals { L (getLoc $1) [unLoc $1] }
1471 squals :: { Located [LStmt RdrName] }
1472 : squals1 { L (getLoc $1) (reverse (unLoc $1)) }
1474 squals1 :: { Located [LStmt RdrName] }
1475 : transformquals1 { LL (unLoc $1) }
1477 transformquals1 :: { Located [LStmt RdrName] }
1478 : transformquals1 ',' transformqual { LL $ [LL ((unLoc $3) (unLoc $1))] }
1479 | transformquals1 ',' qual { LL ($3 : unLoc $1) }
1480 -- | transformquals1 ',' '{|' pquals '|}' { LL ($4 : unLoc $1) }
1481 | transformqual { LL $ [LL ((unLoc $1) [])] }
1483 -- | '{|' pquals '|}' { L1 [$2] }
1486 -- It is possible to enable bracketing (associating) qualifier lists by uncommenting the lines with {| |}
1487 -- above. Due to a lack of consensus on the syntax, this feature is not being used until we get user
1488 -- demand. Note that the {| |} symbols are reused from -XGenerics and hence if you want to compile
1489 -- a program that makes use of this temporary syntax you must supply that flag to GHC
1491 transformqual :: { Located ([LStmt RdrName] -> Stmt RdrName) }
1492 : 'then' exp { LL $ \leftStmts -> (mkTransformStmt (reverse leftStmts) $2) }
1493 | 'then' exp 'by' exp { LL $ \leftStmts -> (mkTransformByStmt (reverse leftStmts) $2 $4) }
1494 | 'then' 'group' 'by' exp { LL $ \leftStmts -> (mkGroupByStmt (reverse leftStmts) $4) }
1495 | 'then' 'group' 'using' exp { LL $ \leftStmts -> (mkGroupUsingStmt (reverse leftStmts) $4) }
1496 | 'then' 'group' 'by' exp 'using' exp { LL $ \leftStmts -> (mkGroupByUsingStmt (reverse leftStmts) $4 $6) }
1498 -----------------------------------------------------------------------------
1499 -- Parallel array expressions
1501 -- The rules below are little bit contorted; see the list case for details.
1502 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1503 -- Moreover, we allow explicit arrays with no element (represented by the nil
1504 -- constructor in the list case).
1506 parr :: { LHsExpr RdrName }
1507 : { noLoc (ExplicitPArr placeHolderType []) }
1508 | texp { L1 $ ExplicitPArr placeHolderType [$1] }
1509 | lexps { L1 $ ExplicitPArr placeHolderType
1510 (reverse (unLoc $1)) }
1511 | texp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1512 | texp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1513 | texp '|' flattenedpquals { LL $ mkHsDo PArrComp (unLoc $3) $1 }
1515 -- We are reusing `lexps' and `flattenedpquals' from the list case.
1517 -----------------------------------------------------------------------------
1520 guardquals :: { Located [LStmt RdrName] }
1521 : guardquals1 { L (getLoc $1) (reverse (unLoc $1)) }
1523 guardquals1 :: { Located [LStmt RdrName] }
1524 : guardquals1 ',' qual { LL ($3 : unLoc $1) }
1527 -----------------------------------------------------------------------------
1528 -- Case alternatives
1530 altslist :: { Located [LMatch RdrName] }
1531 : '{' alts '}' { LL (reverse (unLoc $2)) }
1532 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1534 alts :: { Located [LMatch RdrName] }
1535 : alts1 { L1 (unLoc $1) }
1536 | ';' alts { LL (unLoc $2) }
1538 alts1 :: { Located [LMatch RdrName] }
1539 : alts1 ';' alt { LL ($3 : unLoc $1) }
1540 | alts1 ';' { LL (unLoc $1) }
1543 alt :: { LMatch RdrName }
1544 : pat opt_sig alt_rhs { LL (Match [$1] $2 (unLoc $3)) }
1546 alt_rhs :: { Located (GRHSs RdrName) }
1547 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1549 ralt :: { Located [LGRHS RdrName] }
1550 : '->' exp { LL (unguardedRHS $2) }
1551 | gdpats { L1 (reverse (unLoc $1)) }
1553 gdpats :: { Located [LGRHS RdrName] }
1554 : gdpats gdpat { LL ($2 : unLoc $1) }
1557 gdpat :: { LGRHS RdrName }
1558 : '|' guardquals '->' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1560 -- 'pat' recognises a pattern, including one with a bang at the top
1561 -- e.g. "!x" or "!(x,y)" or "C a b" etc
1562 -- Bangs inside are parsed as infix operator applications, so that
1563 -- we parse them right when bang-patterns are off
1564 pat :: { LPat RdrName }
1565 pat : exp {% checkPattern $1 }
1566 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1568 apat :: { LPat RdrName }
1569 apat : aexp {% checkPattern $1 }
1570 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1572 apats :: { [LPat RdrName] }
1573 : apat apats { $1 : $2 }
1574 | {- empty -} { [] }
1576 -----------------------------------------------------------------------------
1577 -- Statement sequences
1579 stmtlist :: { Located [LStmt RdrName] }
1580 : '{' stmts '}' { LL (unLoc $2) }
1581 | vocurly stmts close { $2 }
1583 -- do { ;; s ; s ; ; s ;; }
1584 -- The last Stmt should be an expression, but that's hard to enforce
1585 -- here, because we need too much lookahead if we see do { e ; }
1586 -- So we use ExprStmts throughout, and switch the last one over
1587 -- in ParseUtils.checkDo instead
1588 stmts :: { Located [LStmt RdrName] }
1589 : stmt stmts_help { LL ($1 : unLoc $2) }
1590 | ';' stmts { LL (unLoc $2) }
1591 | {- empty -} { noLoc [] }
1593 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1594 : ';' stmts { LL (unLoc $2) }
1595 | {- empty -} { noLoc [] }
1597 -- For typing stmts at the GHCi prompt, where
1598 -- the input may consist of just comments.
1599 maybe_stmt :: { Maybe (LStmt RdrName) }
1601 | {- nothing -} { Nothing }
1603 stmt :: { LStmt RdrName }
1605 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1607 qual :: { LStmt RdrName }
1608 : pat '<-' exp { LL $ mkBindStmt $1 $3 }
1609 | exp { L1 $ mkExprStmt $1 }
1610 | 'let' binds { LL $ LetStmt (unLoc $2) }
1612 -----------------------------------------------------------------------------
1613 -- Record Field Update/Construction
1615 fbinds :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1617 | {- empty -} { ([], False) }
1619 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1620 : fbind ',' fbinds1 { case $3 of (flds, dd) -> ($1 : flds, dd) }
1621 | fbind { ([$1], False) }
1622 | '..' { ([], True) }
1624 fbind :: { HsRecField RdrName (LHsExpr RdrName) }
1625 : qvar '=' exp { HsRecField $1 $3 False }
1626 | qvar { HsRecField $1 (L (getLoc $1) (HsVar (unLoc $1))) True }
1627 -- Here's where we say that plain 'x'
1628 -- means exactly 'x = x'. The pun-flag boolean is
1629 -- there so we can still print it right
1631 -----------------------------------------------------------------------------
1632 -- Implicit Parameter Bindings
1634 dbinds :: { Located [LIPBind RdrName] }
1635 : dbinds ';' dbind { let { this = $3; rest = unLoc $1 }
1636 in rest `seq` this `seq` LL (this : rest) }
1637 | dbinds ';' { LL (unLoc $1) }
1638 | dbind { let this = $1 in this `seq` L1 [this] }
1639 -- | {- empty -} { [] }
1641 dbind :: { LIPBind RdrName }
1642 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1644 ipvar :: { Located (IPName RdrName) }
1645 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1647 -----------------------------------------------------------------------------
1648 -- Warnings and deprecations
1650 namelist :: { Located [RdrName] }
1651 namelist : name_var { L1 [unLoc $1] }
1652 | name_var ',' namelist { LL (unLoc $1 : unLoc $3) }
1654 name_var :: { Located RdrName }
1655 name_var : var { $1 }
1658 -----------------------------------------
1659 -- Data constructors
1660 qcon :: { Located RdrName }
1662 | '(' qconsym ')' { LL (unLoc $2) }
1663 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1664 -- The case of '[:' ':]' is part of the production `parr'
1666 con :: { Located RdrName }
1668 | '(' consym ')' { LL (unLoc $2) }
1669 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1671 con_list :: { Located [Located RdrName] }
1672 con_list : con { L1 [$1] }
1673 | con ',' con_list { LL ($1 : unLoc $3) }
1675 sysdcon :: { Located DataCon } -- Wired in data constructors
1676 : '(' ')' { LL unitDataCon }
1677 | '(' commas ')' { LL $ tupleCon Boxed ($2 + 1) }
1678 | '(#' '#)' { LL $ unboxedSingletonDataCon }
1679 | '(#' commas '#)' { LL $ tupleCon Unboxed ($2 + 1) }
1680 | '[' ']' { LL nilDataCon }
1682 conop :: { Located RdrName }
1684 | '`' conid '`' { LL (unLoc $2) }
1686 qconop :: { Located RdrName }
1688 | '`' qconid '`' { LL (unLoc $2) }
1690 -----------------------------------------------------------------------------
1691 -- Type constructors
1693 gtycon :: { Located RdrName } -- A "general" qualified tycon
1695 | '(' ')' { LL $ getRdrName unitTyCon }
1696 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed ($2 + 1)) }
1697 | '(#' '#)' { LL $ getRdrName unboxedSingletonTyCon }
1698 | '(#' commas '#)' { LL $ getRdrName (tupleTyCon Unboxed ($2 + 1)) }
1699 | '(' '->' ')' { LL $ getRdrName funTyCon }
1700 | '[' ']' { LL $ listTyCon_RDR }
1701 | '[:' ':]' { LL $ parrTyCon_RDR }
1703 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1705 | '(' qtyconsym ')' { LL (unLoc $2) }
1707 qtyconop :: { Located RdrName } -- Qualified or unqualified
1709 | '`' qtycon '`' { LL (unLoc $2) }
1711 qtycon :: { Located RdrName } -- Qualified or unqualified
1712 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1713 | PREFIXQCONSYM { L1 $! mkQual tcClsName (getPREFIXQCONSYM $1) }
1716 tycon :: { Located RdrName } -- Unqualified
1717 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1719 qtyconsym :: { Located RdrName }
1720 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1723 tyconsym :: { Located RdrName }
1724 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1726 -----------------------------------------------------------------------------
1729 op :: { Located RdrName } -- used in infix decls
1733 varop :: { Located RdrName }
1735 | '`' varid '`' { LL (unLoc $2) }
1737 qop :: { LHsExpr RdrName } -- used in sections
1738 : qvarop { L1 $ HsVar (unLoc $1) }
1739 | qconop { L1 $ HsVar (unLoc $1) }
1741 qopm :: { LHsExpr RdrName } -- used in sections
1742 : qvaropm { L1 $ HsVar (unLoc $1) }
1743 | qconop { L1 $ HsVar (unLoc $1) }
1745 qvarop :: { Located RdrName }
1747 | '`' qvarid '`' { LL (unLoc $2) }
1749 qvaropm :: { Located RdrName }
1750 : qvarsym_no_minus { $1 }
1751 | '`' qvarid '`' { LL (unLoc $2) }
1753 -----------------------------------------------------------------------------
1756 tyvar :: { Located RdrName }
1757 tyvar : tyvarid { $1 }
1758 | '(' tyvarsym ')' { LL (unLoc $2) }
1760 tyvarop :: { Located RdrName }
1761 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1763 | '.' {% parseErrorSDoc (getLoc $1)
1764 (vcat [ptext (sLit "Illegal symbol '.' in type"),
1765 ptext (sLit "Perhaps you intended -XRankNTypes or similar flag"),
1766 ptext (sLit "to enable explicit-forall syntax: forall <tvs>. <type>")])
1769 tyvarid :: { Located RdrName }
1770 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1771 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1772 | 'unsafe' { L1 $! mkUnqual tvName (fsLit "unsafe") }
1773 | 'safe' { L1 $! mkUnqual tvName (fsLit "safe") }
1774 | 'threadsafe' { L1 $! mkUnqual tvName (fsLit "threadsafe") }
1776 tyvarsym :: { Located RdrName }
1777 -- Does not include "!", because that is used for strictness marks
1778 -- or ".", because that separates the quantified type vars from the rest
1779 -- or "*", because that's used for kinds
1780 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1782 -----------------------------------------------------------------------------
1785 var :: { Located RdrName }
1787 | '(' varsym ')' { LL (unLoc $2) }
1789 qvar :: { Located RdrName }
1791 | '(' varsym ')' { LL (unLoc $2) }
1792 | '(' qvarsym1 ')' { LL (unLoc $2) }
1793 -- We've inlined qvarsym here so that the decision about
1794 -- whether it's a qvar or a var can be postponed until
1795 -- *after* we see the close paren.
1797 qvarid :: { Located RdrName }
1799 | QVARID { L1 $! mkQual varName (getQVARID $1) }
1800 | PREFIXQVARSYM { L1 $! mkQual varName (getPREFIXQVARSYM $1) }
1802 varid :: { Located RdrName }
1803 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1804 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1805 | 'unsafe' { L1 $! mkUnqual varName (fsLit "unsafe") }
1806 | 'safe' { L1 $! mkUnqual varName (fsLit "safe") }
1807 | 'threadsafe' { L1 $! mkUnqual varName (fsLit "threadsafe") }
1808 | 'forall' { L1 $! mkUnqual varName (fsLit "forall") }
1809 | 'family' { L1 $! mkUnqual varName (fsLit "family") }
1811 qvarsym :: { Located RdrName }
1815 qvarsym_no_minus :: { Located RdrName }
1816 : varsym_no_minus { $1 }
1819 qvarsym1 :: { Located RdrName }
1820 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1822 varsym :: { Located RdrName }
1823 : varsym_no_minus { $1 }
1824 | '-' { L1 $ mkUnqual varName (fsLit "-") }
1826 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1827 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1828 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1831 -- These special_ids are treated as keywords in various places,
1832 -- but as ordinary ids elsewhere. 'special_id' collects all these
1833 -- except 'unsafe', 'forall', and 'family' whose treatment differs
1834 -- depending on context
1835 special_id :: { Located FastString }
1837 : 'as' { L1 (fsLit "as") }
1838 | 'qualified' { L1 (fsLit "qualified") }
1839 | 'hiding' { L1 (fsLit "hiding") }
1840 | 'export' { L1 (fsLit "export") }
1841 | 'label' { L1 (fsLit "label") }
1842 | 'dynamic' { L1 (fsLit "dynamic") }
1843 | 'stdcall' { L1 (fsLit "stdcall") }
1844 | 'ccall' { L1 (fsLit "ccall") }
1845 | 'prim' { L1 (fsLit "prim") }
1847 special_sym :: { Located FastString }
1848 special_sym : '!' { L1 (fsLit "!") }
1849 | '.' { L1 (fsLit ".") }
1850 | '*' { L1 (fsLit "*") }
1852 -----------------------------------------------------------------------------
1853 -- Data constructors
1855 qconid :: { Located RdrName } -- Qualified or unqualified
1857 | QCONID { L1 $! mkQual dataName (getQCONID $1) }
1858 | PREFIXQCONSYM { L1 $! mkQual dataName (getPREFIXQCONSYM $1) }
1860 conid :: { Located RdrName }
1861 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1863 qconsym :: { Located RdrName } -- Qualified or unqualified
1865 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1867 consym :: { Located RdrName }
1868 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1870 -- ':' means only list cons
1871 | ':' { L1 $ consDataCon_RDR }
1874 -----------------------------------------------------------------------------
1877 literal :: { Located HsLit }
1878 : CHAR { L1 $ HsChar $ getCHAR $1 }
1879 | STRING { L1 $ HsString $ getSTRING $1 }
1880 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1881 | PRIMWORD { L1 $ HsWordPrim $ getPRIMWORD $1 }
1882 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1883 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1884 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1885 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1887 -----------------------------------------------------------------------------
1891 : vccurly { () } -- context popped in lexer.
1892 | error {% popContext }
1894 -----------------------------------------------------------------------------
1895 -- Miscellaneous (mostly renamings)
1897 modid :: { Located ModuleName }
1898 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1899 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1902 (unpackFS mod ++ '.':unpackFS c))
1906 : commas ',' { $1 + 1 }
1909 -----------------------------------------------------------------------------
1910 -- Documentation comments
1912 docnext :: { LHsDoc RdrName }
1913 : DOCNEXT {% case parseHaddockParagraphs (tokenise (getDOCNEXT $1)) of {
1914 MyLeft err -> parseError (getLoc $1) err;
1915 MyRight doc -> return (L1 doc) } }
1917 docprev :: { LHsDoc RdrName }
1918 : DOCPREV {% case parseHaddockParagraphs (tokenise (getDOCPREV $1)) of {
1919 MyLeft err -> parseError (getLoc $1) err;
1920 MyRight doc -> return (L1 doc) } }
1922 docnamed :: { Located (String, (HsDoc RdrName)) }
1924 let string = getDOCNAMED $1
1925 (name, rest) = break isSpace string
1926 in case parseHaddockParagraphs (tokenise rest) of {
1927 MyLeft err -> parseError (getLoc $1) err;
1928 MyRight doc -> return (L1 (name, doc)) } }
1930 docsection :: { Located (Int, HsDoc RdrName) }
1931 : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1932 case parseHaddockString (tokenise doc) of {
1933 MyLeft err -> parseError (getLoc $1) err;
1934 MyRight doc -> return (L1 (n, doc)) } }
1936 moduleheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
1937 : DOCNEXT {% let string = getDOCNEXT $1 in
1938 case parseModuleHeader string of {
1939 Right (str, info) ->
1940 case parseHaddockParagraphs (tokenise str) of {
1941 MyLeft err -> parseError (getLoc $1) err;
1942 MyRight doc -> return (info, Just doc);
1944 Left err -> parseError (getLoc $1) err
1947 maybe_docprev :: { Maybe (LHsDoc RdrName) }
1948 : docprev { Just $1 }
1949 | {- empty -} { Nothing }
1951 maybe_docnext :: { Maybe (LHsDoc RdrName) }
1952 : docnext { Just $1 }
1953 | {- empty -} { Nothing }
1957 happyError = srcParseFail
1959 getVARID (L _ (ITvarid x)) = x
1960 getCONID (L _ (ITconid x)) = x
1961 getVARSYM (L _ (ITvarsym x)) = x
1962 getCONSYM (L _ (ITconsym x)) = x
1963 getQVARID (L _ (ITqvarid x)) = x
1964 getQCONID (L _ (ITqconid x)) = x
1965 getQVARSYM (L _ (ITqvarsym x)) = x
1966 getQCONSYM (L _ (ITqconsym x)) = x
1967 getPREFIXQVARSYM (L _ (ITprefixqvarsym x)) = x
1968 getPREFIXQCONSYM (L _ (ITprefixqconsym x)) = x
1969 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1970 getCHAR (L _ (ITchar x)) = x
1971 getSTRING (L _ (ITstring x)) = x
1972 getINTEGER (L _ (ITinteger x)) = x
1973 getRATIONAL (L _ (ITrational x)) = x
1974 getPRIMCHAR (L _ (ITprimchar x)) = x
1975 getPRIMSTRING (L _ (ITprimstring x)) = x
1976 getPRIMINTEGER (L _ (ITprimint x)) = x
1977 getPRIMWORD (L _ (ITprimword x)) = x
1978 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1979 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1980 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1981 getINLINE (L _ (ITinline_prag b)) = b
1982 getINLINE_CONLIKE (L _ (ITinline_conlike_prag b)) = b
1983 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1985 getDOCNEXT (L _ (ITdocCommentNext x)) = x
1986 getDOCPREV (L _ (ITdocCommentPrev x)) = x
1987 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
1988 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
1990 getSCC :: Located Token -> P FastString
1991 getSCC lt = do let s = getSTRING lt
1992 err = "Spaces are not allowed in SCCs"
1993 -- We probably actually want to be more restrictive than this
1994 if ' ' `elem` unpackFS s
1995 then failSpanMsgP (getLoc lt) (text err)
1998 -- Utilities for combining source spans
1999 comb2 :: Located a -> Located b -> SrcSpan
2000 comb2 a b = a `seq` b `seq` combineLocs a b
2002 comb3 :: Located a -> Located b -> Located c -> SrcSpan
2003 comb3 a b c = a `seq` b `seq` c `seq`
2004 combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
2006 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
2007 comb4 a b c d = a `seq` b `seq` c `seq` d `seq`
2008 (combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
2009 combineSrcSpans (getLoc c) (getLoc d))
2011 -- strict constructor version:
2013 sL :: SrcSpan -> a -> Located a
2014 sL span a = span `seq` a `seq` L span a
2016 -- Make a source location for the file. We're a bit lazy here and just
2017 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
2018 -- try to find the span of the whole file (ToDo).
2019 fileSrcSpan :: P SrcSpan
2022 let loc = mkSrcLoc (srcLocFile l) 1 0;
2023 return (mkSrcSpan loc loc)