2 -- ---------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1997-2003
7 -- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -- ---------------------------------------------------------------------------
11 {-# LANGUAGE BangPatterns #-} -- required for versions of Happy before 1.18.6
12 {-# OPTIONS -Wwarn -w #-}
13 -- The above warning supression flag is a temporary kludge.
14 -- While working on this module you are encouraged to remove it and fix
15 -- any warnings in the module. See
16 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
19 {-# OPTIONS_GHC -O0 -fno-ignore-interface-pragmas #-}
21 Careful optimisation of the parser: we don't want to throw everything
22 at it, because that takes too long and doesn't buy much, but we do want
23 to inline certain key external functions, so we instruct GHC not to
24 throw away inlinings as it would normally do in -O0 mode.
27 module Parser ( parseModule, parseStmt, parseIdentifier, parseType,
32 import HscTypes ( IsBootInterface, WarningTxt(..) )
35 import TysWiredIn ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
36 unboxedSingletonTyCon, unboxedSingletonDataCon,
37 listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
38 import Type ( funTyCon )
39 import ForeignCall ( Safety(..), CExportSpec(..), CLabelString,
40 CCallConv(..), CCallTarget(..), defaultCCallConv
42 import OccName ( varName, dataName, tcClsName, tvName )
43 import DataCon ( DataCon, dataConName )
44 import SrcLoc ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
45 SrcSpan, combineLocs, srcLocFile,
48 import StaticFlags ( opt_SccProfilingOn, opt_Hpc )
49 import Type ( Kind, liftedTypeKind, unliftedTypeKind )
50 import Coercion ( mkArrowKind )
51 import Class ( FunDep )
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 } -- ToDo: remove deprecated alias
244 'interruptible' { L _ ITinterruptible }
245 'unsafe' { L _ ITunsafe }
247 'family' { L _ ITfamily }
248 'stdcall' { L _ ITstdcallconv }
249 'ccall' { L _ ITccallconv }
250 'prim' { L _ ITprimcallconv }
251 'proc' { L _ ITproc } -- for arrow notation extension
252 'rec' { L _ ITrec } -- for arrow notation extension
253 'group' { L _ ITgroup } -- for list transform extension
254 'by' { L _ ITby } -- for list transform extension
255 'using' { L _ ITusing } -- for list transform extension
257 '{-# INLINE' { L _ (ITinline_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 '{-# VECTORISE' { L _ ITvect_prag }
270 '{-# VECTORISE_SCALAR' { L _ ITvect_scalar_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 ->
387 return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4 $1
390 {% fileSrcSpan >>= \ loc ->
391 return (L loc (HsModule Nothing Nothing
392 (fst $1) (snd $1) Nothing Nothing
395 maybedocheader :: { Maybe LHsDocString }
396 : moduleheader { $1 }
397 | {- empty -} { Nothing }
399 missing_module_keyword :: { () }
400 : {- empty -} {% pushCurrentContext }
402 maybemodwarning :: { Maybe WarningTxt }
403 : '{-# DEPRECATED' strings '#-}' { Just (DeprecatedTxt $ unLoc $2) }
404 | '{-# WARNING' strings '#-}' { Just (WarningTxt $ unLoc $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 ->
429 return (L loc (HsModule (Just $3) $5 $7 [] $4 $1
431 | missing_module_keyword importdecls
432 {% fileSrcSpan >>= \ loc ->
433 return (L loc (HsModule Nothing Nothing $2 [] 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 | '{-# VECTORISE_SCALAR' qvar '#-}' { unitOL $ LL $ VectD (HsVect $2 Nothing) }
569 | '{-# VECTORISE' qvar '=' exp '#-}' { unitOL $ LL $ VectD (HsVect $2 (Just $4)) }
570 | annotation { unitOL $1 }
573 -- Template Haskell Extension
574 -- The $(..) form is one possible form of infixexp
575 -- but we treat an arbitrary expression just as if
576 -- it had a $(..) wrapped around it
577 | infixexp { unitOL (LL $ mkTopSpliceDecl $1) }
581 cl_decl :: { LTyClDecl RdrName }
582 : 'class' tycl_hdr fds where_cls {% mkClassDecl (comb4 $1 $2 $3 $4) $2 $3 $4 }
584 -- Type declarations (toplevel)
586 ty_decl :: { LTyClDecl RdrName }
587 -- ordinary type synonyms
588 : 'type' type '=' ctypedoc
589 -- Note ctype, not sigtype, on the right of '='
590 -- We allow an explicit for-all but we don't insert one
591 -- in type Foo a = (b,b)
592 -- Instead we just say b is out of scope
594 -- Note the use of type for the head; this allows
595 -- infix type constructors to be declared
596 {% mkTySynonym (comb2 $1 $4) False $2 $4 }
598 -- type family declarations
599 | 'type' 'family' type opt_kind_sig
600 -- Note the use of type for the head; this allows
601 -- infix type constructors to be declared
602 {% mkTyFamily (comb3 $1 $3 $4) TypeFamily $3 (unLoc $4) }
604 -- type instance declarations
605 | 'type' 'instance' type '=' ctype
606 -- Note the use of type for the head; this allows
607 -- infix type constructors and type patterns
608 {% mkTySynonym (comb2 $1 $5) True $3 $5 }
610 -- ordinary data type or newtype declaration
611 | data_or_newtype tycl_hdr constrs deriving
612 {% mkTyData (comb4 $1 $2 $3 $4) (unLoc $1) False $2
613 Nothing (reverse (unLoc $3)) (unLoc $4) }
614 -- We need the location on tycl_hdr in case
615 -- constrs and deriving are both empty
617 -- ordinary GADT declaration
618 | data_or_newtype tycl_hdr opt_kind_sig
621 {% mkTyData (comb4 $1 $2 $4 $5) (unLoc $1) False $2
622 (unLoc $3) (unLoc $4) (unLoc $5) }
623 -- We need the location on tycl_hdr in case
624 -- constrs and deriving are both empty
626 -- data/newtype family
627 | 'data' 'family' type opt_kind_sig
628 {% mkTyFamily (comb3 $1 $2 $4) DataFamily $3 (unLoc $4) }
630 -- data/newtype instance declaration
631 | data_or_newtype 'instance' tycl_hdr constrs deriving
632 {% mkTyData (comb4 $1 $3 $4 $5) (unLoc $1) True $3
633 Nothing (reverse (unLoc $4)) (unLoc $5) }
635 -- GADT instance declaration
636 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
639 {% mkTyData (comb4 $1 $3 $5 $6) (unLoc $1) True $3
640 (unLoc $4) (unLoc $5) (unLoc $6) }
642 -- Associated type family declarations
644 -- * They have a different syntax than on the toplevel (no family special
647 -- * They also need to be separate from instances; otherwise, data family
648 -- declarations without a kind signature cause parsing conflicts with empty
649 -- data declarations.
651 at_decl_cls :: { LTyClDecl RdrName }
652 -- type family declarations
653 : 'type' type opt_kind_sig
654 -- Note the use of type for the head; this allows
655 -- infix type constructors to be declared
656 {% mkTyFamily (comb3 $1 $2 $3) TypeFamily $2 (unLoc $3) }
658 -- default type instance
659 | 'type' type '=' ctype
660 -- Note the use of type for the head; this allows
661 -- infix type constructors and type patterns
662 {% mkTySynonym (comb2 $1 $4) True $2 $4 }
664 -- data/newtype family declaration
665 | 'data' type opt_kind_sig
666 {% mkTyFamily (comb3 $1 $2 $3) DataFamily $2 (unLoc $3) }
668 -- Associated type instances
670 at_decl_inst :: { LTyClDecl RdrName }
671 -- type instance declarations
672 : 'type' type '=' ctype
673 -- Note the use of type for the head; this allows
674 -- infix type constructors and type patterns
675 {% mkTySynonym (comb2 $1 $4) True $2 $4 }
677 -- data/newtype instance declaration
678 | data_or_newtype tycl_hdr constrs deriving
679 {% mkTyData (comb4 $1 $2 $3 $4) (unLoc $1) True $2
680 Nothing (reverse (unLoc $3)) (unLoc $4) }
682 -- GADT instance declaration
683 | data_or_newtype tycl_hdr opt_kind_sig
686 {% mkTyData (comb4 $1 $2 $4 $5) (unLoc $1) True $2
687 (unLoc $3) (unLoc $4) (unLoc $5) }
689 data_or_newtype :: { Located NewOrData }
690 : 'data' { L1 DataType }
691 | 'newtype' { L1 NewType }
693 opt_kind_sig :: { Located (Maybe Kind) }
695 | '::' kind { LL (Just (unLoc $2)) }
697 -- tycl_hdr parses the header of a class or data type decl,
698 -- which takes the form
701 -- (Eq a, Ord b) => T a b
702 -- T Int [a] -- for associated types
703 -- Rather a lot of inlining here, else we get reduce/reduce errors
704 tycl_hdr :: { Located (Maybe (LHsContext RdrName), LHsType RdrName) }
705 : context '=>' type { LL (Just $1, $3) }
706 | type { L1 (Nothing, $1) }
708 -----------------------------------------------------------------------------
709 -- Stand-alone deriving
711 -- Glasgow extension: stand-alone deriving declarations
712 stand_alone_deriving :: { LDerivDecl RdrName }
713 : 'deriving' 'instance' inst_type { LL (DerivDecl $3) }
715 -----------------------------------------------------------------------------
716 -- Nested declarations
718 -- Declaration in class bodies
720 decl_cls :: { Located (OrdList (LHsDecl RdrName)) }
721 decl_cls : at_decl_cls { LL (unitOL (L1 (TyClD (unLoc $1)))) }
724 decls_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
725 : decls_cls ';' decl_cls { LL (unLoc $1 `appOL` unLoc $3) }
726 | decls_cls ';' { LL (unLoc $1) }
728 | {- empty -} { noLoc nilOL }
732 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
733 : '{' decls_cls '}' { LL (unLoc $2) }
734 | vocurly decls_cls close { $2 }
738 where_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
739 -- No implicit parameters
740 -- May have type declarations
741 : 'where' decllist_cls { LL (unLoc $2) }
742 | {- empty -} { noLoc nilOL }
744 -- Declarations in instance bodies
746 decl_inst :: { Located (OrdList (LHsDecl RdrName)) }
747 decl_inst : at_decl_inst { LL (unitOL (L1 (TyClD (unLoc $1)))) }
750 decls_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
751 : decls_inst ';' decl_inst { LL (unLoc $1 `appOL` unLoc $3) }
752 | decls_inst ';' { LL (unLoc $1) }
754 | {- empty -} { noLoc nilOL }
757 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
758 : '{' decls_inst '}' { LL (unLoc $2) }
759 | vocurly decls_inst close { $2 }
763 where_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
764 -- No implicit parameters
765 -- May have type declarations
766 : 'where' decllist_inst { LL (unLoc $2) }
767 | {- empty -} { noLoc nilOL }
769 -- Declarations in binding groups other than classes and instances
771 decls :: { Located (OrdList (LHsDecl RdrName)) }
772 : decls ';' decl { let { this = unLoc $3;
774 these = rest `appOL` this }
775 in rest `seq` this `seq` these `seq`
777 | decls ';' { LL (unLoc $1) }
779 | {- empty -} { noLoc nilOL }
781 decllist :: { Located (OrdList (LHsDecl RdrName)) }
782 : '{' decls '}' { LL (unLoc $2) }
783 | vocurly decls close { $2 }
785 -- Binding groups other than those of class and instance declarations
787 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
788 -- No type declarations
789 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
790 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyTcEvBinds)) }
791 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyTcEvBinds)) }
793 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
794 -- No type declarations
795 : 'where' binds { LL (unLoc $2) }
796 | {- empty -} { noLoc emptyLocalBinds }
799 -----------------------------------------------------------------------------
800 -- Transformation Rules
802 rules :: { OrdList (LHsDecl RdrName) }
803 : rules ';' rule { $1 `snocOL` $3 }
806 | {- empty -} { nilOL }
808 rule :: { LHsDecl RdrName }
809 : STRING activation rule_forall infixexp '=' exp
810 { LL $ RuleD (HsRule (getSTRING $1)
811 ($2 `orElse` AlwaysActive)
812 $3 $4 placeHolderNames $6 placeHolderNames) }
814 activation :: { Maybe Activation }
815 : {- empty -} { Nothing }
816 | explicit_activation { Just $1 }
818 explicit_activation :: { Activation } -- In brackets
819 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
820 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
822 rule_forall :: { [RuleBndr RdrName] }
823 : 'forall' rule_var_list '.' { $2 }
826 rule_var_list :: { [RuleBndr RdrName] }
828 | rule_var rule_var_list { $1 : $2 }
830 rule_var :: { RuleBndr RdrName }
831 : varid { RuleBndr $1 }
832 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
834 -----------------------------------------------------------------------------
835 -- Warnings and deprecations (c.f. rules)
837 warnings :: { OrdList (LHsDecl RdrName) }
838 : warnings ';' warning { $1 `appOL` $3 }
839 | warnings ';' { $1 }
841 | {- empty -} { nilOL }
843 -- SUP: TEMPORARY HACK, not checking for `module Foo'
844 warning :: { OrdList (LHsDecl RdrName) }
846 { toOL [ LL $ WarningD (Warning n (WarningTxt $ unLoc $2))
849 deprecations :: { OrdList (LHsDecl RdrName) }
850 : deprecations ';' deprecation { $1 `appOL` $3 }
851 | deprecations ';' { $1 }
853 | {- empty -} { nilOL }
855 -- SUP: TEMPORARY HACK, not checking for `module Foo'
856 deprecation :: { OrdList (LHsDecl RdrName) }
858 { toOL [ LL $ WarningD (Warning n (DeprecatedTxt $ unLoc $2))
861 strings :: { Located [FastString] }
862 : STRING { L1 [getSTRING $1] }
863 | '[' stringlist ']' { LL $ fromOL (unLoc $2) }
865 stringlist :: { Located (OrdList FastString) }
866 : stringlist ',' STRING { LL (unLoc $1 `snocOL` getSTRING $3) }
867 | STRING { LL (unitOL (getSTRING $1)) }
869 -----------------------------------------------------------------------------
871 annotation :: { LHsDecl RdrName }
872 : '{-# ANN' name_var aexp '#-}' { LL (AnnD $ HsAnnotation (ValueAnnProvenance (unLoc $2)) $3) }
873 | '{-# ANN' 'type' tycon aexp '#-}' { LL (AnnD $ HsAnnotation (TypeAnnProvenance (unLoc $3)) $4) }
874 | '{-# ANN' 'module' aexp '#-}' { LL (AnnD $ HsAnnotation ModuleAnnProvenance $3) }
877 -----------------------------------------------------------------------------
878 -- Foreign import and export declarations
880 fdecl :: { LHsDecl RdrName }
881 fdecl : 'import' callconv safety fspec
882 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
883 | 'import' callconv fspec
884 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
886 | 'export' callconv fspec
887 {% mkExport $2 (unLoc $3) >>= return.LL }
889 callconv :: { CCallConv }
890 : 'stdcall' { StdCallConv }
891 | 'ccall' { CCallConv }
892 | 'prim' { PrimCallConv}
895 : 'unsafe' { PlayRisky }
896 | 'safe' { PlaySafe False }
897 | 'interruptible' { PlayInterruptible }
898 | 'threadsafe' { PlaySafe True } -- deprecated alias
900 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
901 : STRING var '::' sigtypedoc { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
902 | var '::' sigtypedoc { LL (noLoc nilFS, $1, $3) }
903 -- if the entity string is missing, it defaults to the empty string;
904 -- the meaning of an empty entity string depends on the calling
907 -----------------------------------------------------------------------------
910 opt_sig :: { Maybe (LHsType RdrName) }
911 : {- empty -} { Nothing }
912 | '::' sigtype { Just $2 }
914 opt_asig :: { Maybe (LHsType RdrName) }
915 : {- empty -} { Nothing }
916 | '::' atype { Just $2 }
918 sigtype :: { LHsType RdrName } -- Always a HsForAllTy,
919 -- to tell the renamer where to generalise
920 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
921 -- Wrap an Implicit forall if there isn't one there already
923 sigtypedoc :: { LHsType RdrName } -- Always a HsForAllTy
924 : ctypedoc { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
925 -- Wrap an Implicit forall if there isn't one there already
927 sig_vars :: { Located [Located RdrName] }
928 : sig_vars ',' var { LL ($3 : unLoc $1) }
931 sigtypes1 :: { [LHsType RdrName] } -- Always HsForAllTys
933 | sigtype ',' sigtypes1 { $1 : $3 }
935 -----------------------------------------------------------------------------
938 infixtype :: { LHsType RdrName }
939 : btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
940 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
942 strict_mark :: { Located HsBang }
943 : '!' { L1 HsStrict }
944 | '{-# UNPACK' '#-}' '!' { LL HsUnpack }
946 -- A ctype is a for-all type
947 ctype :: { LHsType RdrName }
948 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
949 | context '=>' ctype { LL $ mkImplicitHsForAllTy $1 $3 }
950 -- A type of form (context => type) is an *implicit* HsForAllTy
951 | ipvar '::' type { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
954 ----------------------
955 -- Notes for 'ctypedoc'
956 -- It would have been nice to simplify the grammar by unifying `ctype` and
957 -- ctypedoc` into one production, allowing comments on types everywhere (and
958 -- rejecting them after parsing, where necessary). This is however not possible
959 -- since it leads to ambiguity. The reason is the support for comments on record
961 -- data R = R { field :: Int -- ^ comment on the field }
962 -- If we allow comments on types here, it's not clear if the comment applies
963 -- to 'field' or to 'Int'. So we must use `ctype` to describe the type.
965 ctypedoc :: { LHsType RdrName }
966 : 'forall' tv_bndrs '.' ctypedoc { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
967 | context '=>' ctypedoc { LL $ mkImplicitHsForAllTy $1 $3 }
968 -- A type of form (context => type) is an *implicit* HsForAllTy
969 | ipvar '::' type { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
972 ----------------------
973 -- Notes for 'context'
974 -- We parse a context as a btype so that we don't get reduce/reduce
975 -- errors in ctype. The basic problem is that
977 -- looks so much like a tuple type. We can't tell until we find the =>
979 -- We have the t1 ~ t2 form both in 'context' and in type,
980 -- to permit an individual equational constraint without parenthesis.
981 -- Thus for some reason we allow f :: a~b => blah
982 -- but not f :: ?x::Int => blah
983 context :: { LHsContext RdrName }
984 : btype '~' btype {% checkContext
985 (LL $ HsPredTy (HsEqualP $1 $3)) }
986 | btype {% checkContext $1 }
988 type :: { LHsType RdrName }
990 | btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
991 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
992 | btype '->' ctype { LL $ HsFunTy $1 $3 }
993 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
995 typedoc :: { LHsType RdrName }
997 | btype docprev { LL $ HsDocTy $1 $2 }
998 | btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
999 | btype qtyconop type docprev { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
1000 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
1001 | btype tyvarop type docprev { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
1002 | btype '->' ctypedoc { LL $ HsFunTy $1 $3 }
1003 | btype docprev '->' ctypedoc { LL $ HsFunTy (L (comb2 $1 $2) (HsDocTy $1 $2)) $4 }
1004 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1006 btype :: { LHsType RdrName }
1007 : btype atype { LL $ HsAppTy $1 $2 }
1010 atype :: { LHsType RdrName }
1011 : gtycon { L1 (HsTyVar (unLoc $1)) }
1012 | tyvar { L1 (HsTyVar (unLoc $1)) }
1013 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) } -- Constructor sigs only
1014 | '{' fielddecls '}' { LL $ HsRecTy $2 } -- Constructor sigs only
1015 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
1016 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
1017 | '[' ctype ']' { LL $ HsListTy $2 }
1018 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
1019 | '(' ctype ')' { LL $ HsParTy $2 }
1020 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
1021 | quasiquote { L1 (HsQuasiQuoteTy (unLoc $1)) }
1022 | '$(' exp ')' { LL $ mkHsSpliceTy $2 }
1023 | TH_ID_SPLICE { LL $ mkHsSpliceTy $ L1 $ HsVar $
1024 mkUnqual varName (getTH_ID_SPLICE $1) }
1026 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
1028 -- An inst_type is what occurs in the head of an instance decl
1029 -- e.g. (Foo a, Gaz b) => Wibble a b
1030 -- It's kept as a single type, with a MonoDictTy at the right
1031 -- hand corner, for convenience.
1032 inst_type :: { LHsType RdrName }
1033 : sigtype {% checkInstType $1 }
1035 inst_types1 :: { [LHsType RdrName] }
1036 : inst_type { [$1] }
1037 | inst_type ',' inst_types1 { $1 : $3 }
1039 comma_types0 :: { [LHsType RdrName] }
1040 : comma_types1 { $1 }
1041 | {- empty -} { [] }
1043 comma_types1 :: { [LHsType RdrName] }
1045 | ctype ',' comma_types1 { $1 : $3 }
1047 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1048 : tv_bndr tv_bndrs { $1 : $2 }
1049 | {- empty -} { [] }
1051 tv_bndr :: { LHsTyVarBndr RdrName }
1052 : tyvar { L1 (UserTyVar (unLoc $1) placeHolderKind) }
1053 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
1056 fds :: { Located [Located (FunDep RdrName)] }
1057 : {- empty -} { noLoc [] }
1058 | '|' fds1 { LL (reverse (unLoc $2)) }
1060 fds1 :: { Located [Located (FunDep RdrName)] }
1061 : fds1 ',' fd { LL ($3 : unLoc $1) }
1064 fd :: { Located (FunDep RdrName) }
1065 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
1066 (reverse (unLoc $1), reverse (unLoc $3)) }
1068 varids0 :: { Located [RdrName] }
1069 : {- empty -} { noLoc [] }
1070 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
1072 -----------------------------------------------------------------------------
1075 kind :: { Located Kind }
1077 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1079 akind :: { Located Kind }
1080 : '*' { L1 liftedTypeKind }
1081 | '!' { L1 unliftedTypeKind }
1082 | '(' kind ')' { LL (unLoc $2) }
1085 -----------------------------------------------------------------------------
1086 -- Datatype declarations
1088 gadt_constrlist :: { Located [LConDecl RdrName] } -- Returned in order
1089 : 'where' '{' gadt_constrs '}' { L (comb2 $1 $3) (unLoc $3) }
1090 | 'where' vocurly gadt_constrs close { L (comb2 $1 $3) (unLoc $3) }
1091 | {- empty -} { noLoc [] }
1093 gadt_constrs :: { Located [LConDecl RdrName] }
1094 : gadt_constr ';' gadt_constrs { L (comb2 (head $1) $3) ($1 ++ unLoc $3) }
1095 | gadt_constr { L (getLoc (head $1)) $1 }
1096 | {- empty -} { noLoc [] }
1098 -- We allow the following forms:
1099 -- C :: Eq a => a -> T a
1100 -- C :: forall a. Eq a => !a -> T a
1101 -- D { x,y :: a } :: T a
1102 -- forall a. Eq a => D { x,y :: a } :: T a
1104 gadt_constr :: { [LConDecl RdrName] } -- Returns a list because of: C,D :: ty
1105 : con_list '::' sigtype
1106 { map (sL (comb2 $1 $3)) (mkGadtDecl (unLoc $1) $3) }
1108 -- Deprecated syntax for GADT record declarations
1109 | oqtycon '{' fielddecls '}' '::' sigtype
1110 {% do { cd <- mkDeprecatedGadtRecordDecl (comb2 $1 $6) $1 $3 $6
1113 constrs :: { Located [LConDecl RdrName] }
1114 : maybe_docnext '=' constrs1 { L (comb2 $2 $3) (addConDocs (unLoc $3) $1) }
1116 constrs1 :: { Located [LConDecl RdrName] }
1117 : constrs1 maybe_docnext '|' maybe_docprev constr { LL (addConDoc $5 $2 : addConDocFirst (unLoc $1) $4) }
1118 | constr { L1 [$1] }
1120 constr :: { LConDecl RdrName }
1121 : maybe_docnext forall context '=>' constr_stuff maybe_docprev
1122 { let (con,details) = unLoc $5 in
1123 addConDoc (L (comb4 $2 $3 $4 $5) (mkSimpleConDecl con (unLoc $2) $3 details))
1125 | maybe_docnext forall constr_stuff maybe_docprev
1126 { let (con,details) = unLoc $3 in
1127 addConDoc (L (comb2 $2 $3) (mkSimpleConDecl con (unLoc $2) (noLoc []) details))
1130 forall :: { Located [LHsTyVarBndr RdrName] }
1131 : 'forall' tv_bndrs '.' { LL $2 }
1132 | {- empty -} { noLoc [] }
1134 constr_stuff :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1135 -- We parse the constructor declaration
1137 -- as a btype (treating C as a type constructor) and then convert C to be
1138 -- a data constructor. Reason: it might continue like this:
1140 -- in which case C really would be a type constructor. We can't resolve this
1141 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1142 : btype {% splitCon $1 >>= return.LL }
1143 | btype conop btype { LL ($2, InfixCon $1 $3) }
1145 fielddecls :: { [ConDeclField RdrName] }
1146 : {- empty -} { [] }
1147 | fielddecls1 { $1 }
1149 fielddecls1 :: { [ConDeclField RdrName] }
1150 : fielddecl maybe_docnext ',' maybe_docprev fielddecls1
1151 { [ addFieldDoc f $4 | f <- $1 ] ++ addFieldDocs $5 $2 }
1152 -- This adds the doc $4 to each field separately
1155 fielddecl :: { [ConDeclField RdrName] } -- A list because of f,g :: Int
1156 : maybe_docnext sig_vars '::' ctype maybe_docprev { [ ConDeclField fld $4 ($1 `mplus` $5)
1157 | fld <- reverse (unLoc $2) ] }
1159 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1160 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1161 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1162 -- We don't allow a context, but that's sorted out by the type checker.
1163 deriving :: { Located (Maybe [LHsType RdrName]) }
1164 : {- empty -} { noLoc Nothing }
1165 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1166 ; p <- checkInstType (L loc (HsTyVar tv))
1167 ; return (LL (Just [p])) } }
1168 | 'deriving' '(' ')' { LL (Just []) }
1169 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1170 -- Glasgow extension: allow partial
1171 -- applications in derivings
1173 -----------------------------------------------------------------------------
1174 -- Value definitions
1176 {- Note [Declaration/signature overlap]
1177 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1178 There's an awkward overlap with a type signature. Consider
1179 f :: Int -> Int = ...rhs...
1180 Then we can't tell whether it's a type signature or a value
1181 definition with a result signature until we see the '='.
1182 So we have to inline enough to postpone reductions until we know.
1186 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1187 instead of qvar, we get another shift/reduce-conflict. Consider the
1190 { (^^) :: Int->Int ; } Type signature; only var allowed
1192 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1193 qvar allowed (because of instance decls)
1195 We can't tell whether to reduce var to qvar until after we've read the signatures.
1198 docdecl :: { LHsDecl RdrName }
1199 : docdecld { L1 (DocD (unLoc $1)) }
1201 docdecld :: { LDocDecl }
1202 : docnext { L1 (DocCommentNext (unLoc $1)) }
1203 | docprev { L1 (DocCommentPrev (unLoc $1)) }
1204 | docnamed { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1205 | docsection { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1207 decl :: { Located (OrdList (LHsDecl RdrName)) }
1210 | '!' aexp rhs {% do { let { e = LL (SectionR (LL (HsVar bang_RDR)) $2) };
1211 pat <- checkPattern e;
1212 return $ LL $ unitOL $ LL $ ValD $
1213 PatBind pat (unLoc $3)
1214 placeHolderType placeHolderNames } }
1215 -- Turn it all into an expression so that
1216 -- checkPattern can check that bangs are enabled
1218 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1219 let { l = comb2 $1 $> };
1220 return $! (sL l (unitOL $! (sL l $ ValD r))) } }
1221 | docdecl { LL $ unitOL $1 }
1223 rhs :: { Located (GRHSs RdrName) }
1224 : '=' exp wherebinds { sL (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1225 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1227 gdrhs :: { Located [LGRHS RdrName] }
1228 : gdrhs gdrh { LL ($2 : unLoc $1) }
1231 gdrh :: { LGRHS RdrName }
1232 : '|' guardquals '=' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1234 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1235 : infixexp '::' sigtypedoc {% do s <- checkValSig $1 $3
1236 ; return (LL $ unitOL (LL $ SigD s)) }
1237 -- See Note [Declaration/signature overlap] for why we need infixexp here
1238 | var ',' sig_vars '::' sigtypedoc
1239 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1240 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1242 | '{-# INLINE' activation qvar '#-}'
1243 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlinePragma (getINLINE $1) $2))) }
1244 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1245 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlinePragma)
1247 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1248 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlinePragma (getSPEC_INLINE $1) $2))
1250 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1251 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1253 -----------------------------------------------------------------------------
1256 quasiquote :: { Located (HsQuasiQuote RdrName) }
1257 : TH_QUASIQUOTE { let { loc = getLoc $1
1258 ; ITquasiQuote (quoter, quote, quoteSpan) = unLoc $1
1259 ; quoterId = mkUnqual varName quoter }
1260 in L1 (mkHsQuasiQuote quoterId quoteSpan quote) }
1262 exp :: { LHsExpr RdrName }
1263 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1264 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1265 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1266 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1267 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1270 infixexp :: { LHsExpr RdrName }
1272 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1274 exp10 :: { LHsExpr RdrName }
1275 : '\\' apat apats opt_asig '->' exp
1276 { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1279 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1280 | 'if' exp optSemi 'then' exp optSemi 'else' exp
1281 {% checkDoAndIfThenElse $2 $3 $5 $6 $8 >>
1282 return (LL $ mkHsIf $2 $5 $8) }
1283 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1284 | '-' fexp { LL $ NegApp $2 noSyntaxExpr }
1286 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1287 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1288 return (L loc (mkHsDo DoExpr stmts body)) }
1289 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1290 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1291 return (L loc (mkHsDo MDoExpr
1292 [L loc (mkRecStmt stmts)]
1294 | scc_annot exp { LL $ if opt_SccProfilingOn
1295 then HsSCC (unLoc $1) $2
1297 | hpc_annot exp { LL $ if opt_Hpc
1298 then HsTickPragma (unLoc $1) $2
1301 | 'proc' aexp '->' exp
1302 {% checkPattern $2 >>= \ p ->
1303 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1304 placeHolderType undefined)) }
1305 -- TODO: is LL right here?
1307 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1308 -- hdaume: core annotation
1313 | {- empty -} { False }
1315 scc_annot :: { Located FastString }
1316 : '_scc_' STRING {% (addWarning Opt_WarnWarningsDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1317 ( do scc <- getSCC $2; return $ LL scc ) }
1318 | '{-# SCC' STRING '#-}' {% do scc <- getSCC $2; return $ LL scc }
1320 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1321 : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1322 { LL $ (getSTRING $2
1323 ,( fromInteger $ getINTEGER $3
1324 , fromInteger $ getINTEGER $5
1326 ,( fromInteger $ getINTEGER $7
1327 , fromInteger $ getINTEGER $9
1332 fexp :: { LHsExpr RdrName }
1333 : fexp aexp { LL $ HsApp $1 $2 }
1336 aexp :: { LHsExpr RdrName }
1337 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1338 | '~' aexp { LL $ ELazyPat $2 }
1341 aexp1 :: { LHsExpr RdrName }
1342 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1346 -- Here was the syntax for type applications that I was planning
1347 -- but there are difficulties (e.g. what order for type args)
1348 -- so it's not enabled yet.
1349 -- But this case *is* used for the left hand side of a generic definition,
1350 -- which is parsed as an expression before being munged into a pattern
1351 | qcname '{|' type '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1352 (sL (getLoc $3) (HsType $3)) }
1354 aexp2 :: { LHsExpr RdrName }
1355 : ipvar { L1 (HsIPVar $! unLoc $1) }
1356 | qcname { L1 (HsVar $! unLoc $1) }
1357 | literal { L1 (HsLit $! unLoc $1) }
1358 -- This will enable overloaded strings permanently. Normally the renamer turns HsString
1359 -- into HsOverLit when -foverloaded-strings is on.
1360 -- | STRING { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1361 | INTEGER { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1362 | RATIONAL { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1364 -- N.B.: sections get parsed by these next two productions.
1365 -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't
1366 -- correct Haskell (you'd have to write '((+ 3), (4 -))')
1367 -- but the less cluttered version fell out of having texps.
1368 | '(' texp ')' { LL (HsPar $2) }
1369 | '(' tup_exprs ')' { LL (ExplicitTuple $2 Boxed) }
1371 | '(#' texp '#)' { LL (ExplicitTuple [Present $2] Unboxed) }
1372 | '(#' tup_exprs '#)' { LL (ExplicitTuple $2 Unboxed) }
1374 | '[' list ']' { LL (unLoc $2) }
1375 | '[:' parr ':]' { LL (unLoc $2) }
1376 | '_' { L1 EWildPat }
1378 -- Template Haskell Extension
1379 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1380 (L1 $ HsVar (mkUnqual varName
1381 (getTH_ID_SPLICE $1)))) }
1382 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) }
1385 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1386 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1387 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1388 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1389 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1390 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1391 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1392 return (LL $ HsBracket (PatBr p)) }
1393 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBrL $2) }
1394 | quasiquote { L1 (HsQuasiQuoteE (unLoc $1)) }
1396 -- arrow notation extension
1397 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1399 cmdargs :: { [LHsCmdTop RdrName] }
1400 : cmdargs acmd { $2 : $1 }
1401 | {- empty -} { [] }
1403 acmd :: { LHsCmdTop RdrName }
1404 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1406 cvtopbody :: { [LHsDecl RdrName] }
1407 : '{' cvtopdecls0 '}' { $2 }
1408 | vocurly cvtopdecls0 close { $2 }
1410 cvtopdecls0 :: { [LHsDecl RdrName] }
1411 : {- empty -} { [] }
1414 -----------------------------------------------------------------------------
1415 -- Tuple expressions
1417 -- "texp" is short for tuple expressions:
1418 -- things that can appear unparenthesized as long as they're
1419 -- inside parens or delimitted by commas
1420 texp :: { LHsExpr RdrName }
1423 -- Note [Parsing sections]
1424 -- ~~~~~~~~~~~~~~~~~~~~~~~
1425 -- We include left and right sections here, which isn't
1426 -- technically right according to the Haskell standard.
1427 -- For example (3 +, True) isn't legal.
1428 -- However, we want to parse bang patterns like
1430 -- and it's convenient to do so here as a section
1431 -- Then when converting expr to pattern we unravel it again
1432 -- Meanwhile, the renamer checks that real sections appear
1434 | infixexp qop { LL $ SectionL $1 $2 }
1435 | qopm infixexp { LL $ SectionR $1 $2 }
1437 -- View patterns get parenthesized above
1438 | exp '->' texp { LL $ EViewPat $1 $3 }
1440 -- Always at least one comma
1441 tup_exprs :: { [HsTupArg RdrName] }
1442 : texp commas_tup_tail { Present $1 : $2 }
1443 | commas tup_tail { replicate $1 missingTupArg ++ $2 }
1445 -- Always starts with commas; always follows an expr
1446 commas_tup_tail :: { [HsTupArg RdrName] }
1447 commas_tup_tail : commas tup_tail { replicate ($1-1) missingTupArg ++ $2 }
1449 -- Always follows a comma
1450 tup_tail :: { [HsTupArg RdrName] }
1451 : texp commas_tup_tail { Present $1 : $2 }
1452 | texp { [Present $1] }
1453 | {- empty -} { [missingTupArg] }
1455 -----------------------------------------------------------------------------
1458 -- The rules below are little bit contorted to keep lexps left-recursive while
1459 -- avoiding another shift/reduce-conflict.
1461 list :: { LHsExpr RdrName }
1462 : texp { L1 $ ExplicitList placeHolderType [$1] }
1463 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1464 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1465 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1466 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1467 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1468 | texp '|' flattenedpquals { sL (comb2 $1 $>) $ mkHsDo ListComp (unLoc $3) $1 }
1470 lexps :: { Located [LHsExpr RdrName] }
1471 : lexps ',' texp { LL (((:) $! $3) $! unLoc $1) }
1472 | texp ',' texp { LL [$3,$1] }
1474 -----------------------------------------------------------------------------
1475 -- List Comprehensions
1477 flattenedpquals :: { Located [LStmt RdrName] }
1478 : pquals { case (unLoc $1) of
1480 -- We just had one thing in our "parallel" list so
1481 -- we simply return that thing directly
1483 qss -> L1 [L1 $ ParStmt [(qs, undefined) | qs <- qss]]
1484 -- We actually found some actual parallel lists so
1485 -- we wrap them into as a ParStmt
1488 pquals :: { Located [[LStmt RdrName]] }
1489 : squals '|' pquals { L (getLoc $2) (reverse (unLoc $1) : unLoc $3) }
1490 | squals { L (getLoc $1) [reverse (unLoc $1)] }
1492 squals :: { Located [LStmt RdrName] } -- In reverse order, because the last
1493 -- one can "grab" the earlier ones
1494 : squals ',' transformqual { LL [L (getLoc $3) ((unLoc $3) (reverse (unLoc $1)))] }
1495 | squals ',' qual { LL ($3 : unLoc $1) }
1496 | transformqual { LL [L (getLoc $1) ((unLoc $1) [])] }
1498 -- | transformquals1 ',' '{|' pquals '|}' { LL ($4 : unLoc $1) }
1499 -- | '{|' pquals '|}' { L1 [$2] }
1502 -- It is possible to enable bracketing (associating) qualifier lists by uncommenting the lines with {| |}
1503 -- above. Due to a lack of consensus on the syntax, this feature is not being used until we get user
1504 -- demand. Note that the {| |} symbols are reused from -XGenerics and hence if you want to compile
1505 -- a program that makes use of this temporary syntax you must supply that flag to GHC
1507 transformqual :: { Located ([LStmt RdrName] -> Stmt RdrName) }
1508 -- Function is applied to a list of stmts *in order*
1509 : 'then' exp { LL $ \leftStmts -> (mkTransformStmt leftStmts $2) }
1511 | 'then' exp 'by' exp { LL $ \leftStmts -> (mkTransformByStmt leftStmts $2 $4) }
1512 | 'then' 'group' 'by' exp { LL $ \leftStmts -> (mkGroupByStmt leftStmts $4) }
1514 -- These two productions deliberately have a shift-reduce conflict. I have made 'group' into a special_id,
1515 -- which means you can enable TransformListComp while still using Data.List.group. However, this makes the two
1516 -- productions ambiguous. I've set things up so that Happy chooses to resolve the conflict in that case by
1517 -- choosing the "group by" variant, which is what we want.
1519 -- This is rather dubious: the user might be confused as to how to parse this statement. However, it is a good
1520 -- practical choice. NB: Data.List.group :: [a] -> [[a]], so using the first production would not even type check
1521 -- if /that/ is the group function we conflict with.
1522 | 'then' 'group' 'using' exp { LL $ \leftStmts -> (mkGroupUsingStmt leftStmts $4) }
1523 | 'then' 'group' 'by' exp 'using' exp { LL $ \leftStmts -> (mkGroupByUsingStmt leftStmts $4 $6) }
1525 -----------------------------------------------------------------------------
1526 -- Parallel array expressions
1528 -- The rules below are little bit contorted; see the list case for details.
1529 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1530 -- Moreover, we allow explicit arrays with no element (represented by the nil
1531 -- constructor in the list case).
1533 parr :: { LHsExpr RdrName }
1534 : { noLoc (ExplicitPArr placeHolderType []) }
1535 | texp { L1 $ ExplicitPArr placeHolderType [$1] }
1536 | lexps { L1 $ ExplicitPArr placeHolderType
1537 (reverse (unLoc $1)) }
1538 | texp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1539 | texp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1540 | texp '|' flattenedpquals { LL $ mkHsDo PArrComp (unLoc $3) $1 }
1542 -- We are reusing `lexps' and `flattenedpquals' from the list case.
1544 -----------------------------------------------------------------------------
1547 guardquals :: { Located [LStmt RdrName] }
1548 : guardquals1 { L (getLoc $1) (reverse (unLoc $1)) }
1550 guardquals1 :: { Located [LStmt RdrName] }
1551 : guardquals1 ',' qual { LL ($3 : unLoc $1) }
1554 -----------------------------------------------------------------------------
1555 -- Case alternatives
1557 altslist :: { Located [LMatch RdrName] }
1558 : '{' alts '}' { LL (reverse (unLoc $2)) }
1559 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1561 alts :: { Located [LMatch RdrName] }
1562 : alts1 { L1 (unLoc $1) }
1563 | ';' alts { LL (unLoc $2) }
1565 alts1 :: { Located [LMatch RdrName] }
1566 : alts1 ';' alt { LL ($3 : unLoc $1) }
1567 | alts1 ';' { LL (unLoc $1) }
1570 alt :: { LMatch RdrName }
1571 : pat opt_sig alt_rhs { LL (Match [$1] $2 (unLoc $3)) }
1573 alt_rhs :: { Located (GRHSs RdrName) }
1574 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1576 ralt :: { Located [LGRHS RdrName] }
1577 : '->' exp { LL (unguardedRHS $2) }
1578 | gdpats { L1 (reverse (unLoc $1)) }
1580 gdpats :: { Located [LGRHS RdrName] }
1581 : gdpats gdpat { LL ($2 : unLoc $1) }
1584 gdpat :: { LGRHS RdrName }
1585 : '|' guardquals '->' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1587 -- 'pat' recognises a pattern, including one with a bang at the top
1588 -- e.g. "!x" or "!(x,y)" or "C a b" etc
1589 -- Bangs inside are parsed as infix operator applications, so that
1590 -- we parse them right when bang-patterns are off
1591 pat :: { LPat RdrName }
1592 pat : exp {% checkPattern $1 }
1593 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1595 apat :: { LPat RdrName }
1596 apat : aexp {% checkPattern $1 }
1597 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1599 apats :: { [LPat RdrName] }
1600 : apat apats { $1 : $2 }
1601 | {- empty -} { [] }
1603 -----------------------------------------------------------------------------
1604 -- Statement sequences
1606 stmtlist :: { Located [LStmt RdrName] }
1607 : '{' stmts '}' { LL (unLoc $2) }
1608 | vocurly stmts close { $2 }
1610 -- do { ;; s ; s ; ; s ;; }
1611 -- The last Stmt should be an expression, but that's hard to enforce
1612 -- here, because we need too much lookahead if we see do { e ; }
1613 -- So we use ExprStmts throughout, and switch the last one over
1614 -- in ParseUtils.checkDo instead
1615 stmts :: { Located [LStmt RdrName] }
1616 : stmt stmts_help { LL ($1 : unLoc $2) }
1617 | ';' stmts { LL (unLoc $2) }
1618 | {- empty -} { noLoc [] }
1620 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1621 : ';' stmts { LL (unLoc $2) }
1622 | {- empty -} { noLoc [] }
1624 -- For typing stmts at the GHCi prompt, where
1625 -- the input may consist of just comments.
1626 maybe_stmt :: { Maybe (LStmt RdrName) }
1628 | {- nothing -} { Nothing }
1630 stmt :: { LStmt RdrName }
1632 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1634 qual :: { LStmt RdrName }
1635 : pat '<-' exp { LL $ mkBindStmt $1 $3 }
1636 | exp { L1 $ mkExprStmt $1 }
1637 | 'let' binds { LL $ LetStmt (unLoc $2) }
1639 -----------------------------------------------------------------------------
1640 -- Record Field Update/Construction
1642 fbinds :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1644 | {- empty -} { ([], False) }
1646 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1647 : fbind ',' fbinds1 { case $3 of (flds, dd) -> ($1 : flds, dd) }
1648 | fbind { ([$1], False) }
1649 | '..' { ([], True) }
1651 fbind :: { HsRecField RdrName (LHsExpr RdrName) }
1652 : qvar '=' exp { HsRecField $1 $3 False }
1653 | qvar { HsRecField $1 placeHolderPunRhs True }
1654 -- In the punning case, use a place-holder
1655 -- The renamer fills in the final value
1657 -----------------------------------------------------------------------------
1658 -- Implicit Parameter Bindings
1660 dbinds :: { Located [LIPBind RdrName] }
1661 : dbinds ';' dbind { let { this = $3; rest = unLoc $1 }
1662 in rest `seq` this `seq` LL (this : rest) }
1663 | dbinds ';' { LL (unLoc $1) }
1664 | dbind { let this = $1 in this `seq` L1 [this] }
1665 -- | {- empty -} { [] }
1667 dbind :: { LIPBind RdrName }
1668 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1670 ipvar :: { Located (IPName RdrName) }
1671 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1673 -----------------------------------------------------------------------------
1674 -- Warnings and deprecations
1676 namelist :: { Located [RdrName] }
1677 namelist : name_var { L1 [unLoc $1] }
1678 | name_var ',' namelist { LL (unLoc $1 : unLoc $3) }
1680 name_var :: { Located RdrName }
1681 name_var : var { $1 }
1684 -----------------------------------------
1685 -- Data constructors
1686 qcon :: { Located RdrName }
1688 | '(' qconsym ')' { LL (unLoc $2) }
1689 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1690 -- The case of '[:' ':]' is part of the production `parr'
1692 con :: { Located RdrName }
1694 | '(' consym ')' { LL (unLoc $2) }
1695 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1697 con_list :: { Located [Located RdrName] }
1698 con_list : con { L1 [$1] }
1699 | con ',' con_list { LL ($1 : unLoc $3) }
1701 sysdcon :: { Located DataCon } -- Wired in data constructors
1702 : '(' ')' { LL unitDataCon }
1703 | '(' commas ')' { LL $ tupleCon Boxed ($2 + 1) }
1704 | '(#' '#)' { LL $ unboxedSingletonDataCon }
1705 | '(#' commas '#)' { LL $ tupleCon Unboxed ($2 + 1) }
1706 | '[' ']' { LL nilDataCon }
1708 conop :: { Located RdrName }
1710 | '`' conid '`' { LL (unLoc $2) }
1712 qconop :: { Located RdrName }
1714 | '`' qconid '`' { LL (unLoc $2) }
1716 -----------------------------------------------------------------------------
1717 -- Type constructors
1719 gtycon :: { Located RdrName } -- A "general" qualified tycon
1721 | '(' ')' { LL $ getRdrName unitTyCon }
1722 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed ($2 + 1)) }
1723 | '(#' '#)' { LL $ getRdrName unboxedSingletonTyCon }
1724 | '(#' commas '#)' { LL $ getRdrName (tupleTyCon Unboxed ($2 + 1)) }
1725 | '(' '->' ')' { LL $ getRdrName funTyCon }
1726 | '[' ']' { LL $ listTyCon_RDR }
1727 | '[:' ':]' { LL $ parrTyCon_RDR }
1729 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1731 | '(' qtyconsym ')' { LL (unLoc $2) }
1733 qtyconop :: { Located RdrName } -- Qualified or unqualified
1735 | '`' qtycon '`' { LL (unLoc $2) }
1737 qtycon :: { Located RdrName } -- Qualified or unqualified
1738 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1739 | PREFIXQCONSYM { L1 $! mkQual tcClsName (getPREFIXQCONSYM $1) }
1742 tycon :: { Located RdrName } -- Unqualified
1743 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1745 qtyconsym :: { Located RdrName }
1746 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1749 tyconsym :: { Located RdrName }
1750 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1752 -----------------------------------------------------------------------------
1755 op :: { Located RdrName } -- used in infix decls
1759 varop :: { Located RdrName }
1761 | '`' varid '`' { LL (unLoc $2) }
1763 qop :: { LHsExpr RdrName } -- used in sections
1764 : qvarop { L1 $ HsVar (unLoc $1) }
1765 | qconop { L1 $ HsVar (unLoc $1) }
1767 qopm :: { LHsExpr RdrName } -- used in sections
1768 : qvaropm { L1 $ HsVar (unLoc $1) }
1769 | qconop { L1 $ HsVar (unLoc $1) }
1771 qvarop :: { Located RdrName }
1773 | '`' qvarid '`' { LL (unLoc $2) }
1775 qvaropm :: { Located RdrName }
1776 : qvarsym_no_minus { $1 }
1777 | '`' qvarid '`' { LL (unLoc $2) }
1779 -----------------------------------------------------------------------------
1782 tyvar :: { Located RdrName }
1783 tyvar : tyvarid { $1 }
1784 | '(' tyvarsym ')' { LL (unLoc $2) }
1786 tyvarop :: { Located RdrName }
1787 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1789 | '.' {% parseErrorSDoc (getLoc $1)
1790 (vcat [ptext (sLit "Illegal symbol '.' in type"),
1791 ptext (sLit "Perhaps you intended -XRankNTypes or similar flag"),
1792 ptext (sLit "to enable explicit-forall syntax: forall <tvs>. <type>")])
1795 tyvarid :: { Located RdrName }
1796 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1797 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1798 | 'unsafe' { L1 $! mkUnqual tvName (fsLit "unsafe") }
1799 | 'safe' { L1 $! mkUnqual tvName (fsLit "safe") }
1800 | 'interruptible' { L1 $! mkUnqual tvName (fsLit "interruptible") }
1801 | 'threadsafe' { L1 $! mkUnqual tvName (fsLit "threadsafe") }
1803 tyvarsym :: { Located RdrName }
1804 -- Does not include "!", because that is used for strictness marks
1805 -- or ".", because that separates the quantified type vars from the rest
1806 -- or "*", because that's used for kinds
1807 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1809 -----------------------------------------------------------------------------
1812 var :: { Located RdrName }
1814 | '(' varsym ')' { LL (unLoc $2) }
1816 qvar :: { Located RdrName }
1818 | '(' varsym ')' { LL (unLoc $2) }
1819 | '(' qvarsym1 ')' { LL (unLoc $2) }
1820 -- We've inlined qvarsym here so that the decision about
1821 -- whether it's a qvar or a var can be postponed until
1822 -- *after* we see the close paren.
1824 qvarid :: { Located RdrName }
1826 | QVARID { L1 $! mkQual varName (getQVARID $1) }
1827 | PREFIXQVARSYM { L1 $! mkQual varName (getPREFIXQVARSYM $1) }
1829 varid :: { Located RdrName }
1830 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1831 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1832 | 'unsafe' { L1 $! mkUnqual varName (fsLit "unsafe") }
1833 | 'safe' { L1 $! mkUnqual varName (fsLit "safe") }
1834 | 'interruptible' { L1 $! mkUnqual varName (fsLit "interruptible") }
1835 | 'threadsafe' { L1 $! mkUnqual varName (fsLit "threadsafe") }
1836 | 'forall' { L1 $! mkUnqual varName (fsLit "forall") }
1837 | 'family' { L1 $! mkUnqual varName (fsLit "family") }
1839 qvarsym :: { Located RdrName }
1843 qvarsym_no_minus :: { Located RdrName }
1844 : varsym_no_minus { $1 }
1847 qvarsym1 :: { Located RdrName }
1848 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1850 varsym :: { Located RdrName }
1851 : varsym_no_minus { $1 }
1852 | '-' { L1 $ mkUnqual varName (fsLit "-") }
1854 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1855 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1856 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1859 -- These special_ids are treated as keywords in various places,
1860 -- but as ordinary ids elsewhere. 'special_id' collects all these
1861 -- except 'unsafe', 'interruptible', 'forall', and 'family' whose treatment differs
1862 -- depending on context
1863 special_id :: { Located FastString }
1865 : 'as' { L1 (fsLit "as") }
1866 | 'qualified' { L1 (fsLit "qualified") }
1867 | 'hiding' { L1 (fsLit "hiding") }
1868 | 'export' { L1 (fsLit "export") }
1869 | 'label' { L1 (fsLit "label") }
1870 | 'dynamic' { L1 (fsLit "dynamic") }
1871 | 'stdcall' { L1 (fsLit "stdcall") }
1872 | 'ccall' { L1 (fsLit "ccall") }
1873 | 'prim' { L1 (fsLit "prim") }
1874 | 'group' { L1 (fsLit "group") }
1876 special_sym :: { Located FastString }
1877 special_sym : '!' { L1 (fsLit "!") }
1878 | '.' { L1 (fsLit ".") }
1879 | '*' { L1 (fsLit "*") }
1881 -----------------------------------------------------------------------------
1882 -- Data constructors
1884 qconid :: { Located RdrName } -- Qualified or unqualified
1886 | QCONID { L1 $! mkQual dataName (getQCONID $1) }
1887 | PREFIXQCONSYM { L1 $! mkQual dataName (getPREFIXQCONSYM $1) }
1889 conid :: { Located RdrName }
1890 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1892 qconsym :: { Located RdrName } -- Qualified or unqualified
1894 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1896 consym :: { Located RdrName }
1897 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1899 -- ':' means only list cons
1900 | ':' { L1 $ consDataCon_RDR }
1903 -----------------------------------------------------------------------------
1906 literal :: { Located HsLit }
1907 : CHAR { L1 $ HsChar $ getCHAR $1 }
1908 | STRING { L1 $ HsString $ getSTRING $1 }
1909 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1910 | PRIMWORD { L1 $ HsWordPrim $ getPRIMWORD $1 }
1911 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1912 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1913 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1914 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1916 -----------------------------------------------------------------------------
1920 : vccurly { () } -- context popped in lexer.
1921 | error {% popContext }
1923 -----------------------------------------------------------------------------
1924 -- Miscellaneous (mostly renamings)
1926 modid :: { Located ModuleName }
1927 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1928 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1931 (unpackFS mod ++ '.':unpackFS c))
1935 : commas ',' { $1 + 1 }
1938 -----------------------------------------------------------------------------
1939 -- Documentation comments
1941 docnext :: { LHsDocString }
1942 : DOCNEXT {% return (L1 (HsDocString (mkFastString (getDOCNEXT $1)))) }
1944 docprev :: { LHsDocString }
1945 : DOCPREV {% return (L1 (HsDocString (mkFastString (getDOCPREV $1)))) }
1947 docnamed :: { Located (String, HsDocString) }
1949 let string = getDOCNAMED $1
1950 (name, rest) = break isSpace string
1951 in return (L1 (name, HsDocString (mkFastString rest))) }
1953 docsection :: { Located (Int, HsDocString) }
1954 : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1955 return (L1 (n, HsDocString (mkFastString doc))) }
1957 moduleheader :: { Maybe LHsDocString }
1958 : DOCNEXT {% let string = getDOCNEXT $1 in
1959 return (Just (L1 (HsDocString (mkFastString string)))) }
1961 maybe_docprev :: { Maybe LHsDocString }
1962 : docprev { Just $1 }
1963 | {- empty -} { Nothing }
1965 maybe_docnext :: { Maybe LHsDocString }
1966 : docnext { Just $1 }
1967 | {- empty -} { Nothing }
1971 happyError = srcParseFail
1973 getVARID (L _ (ITvarid x)) = x
1974 getCONID (L _ (ITconid x)) = x
1975 getVARSYM (L _ (ITvarsym x)) = x
1976 getCONSYM (L _ (ITconsym x)) = x
1977 getQVARID (L _ (ITqvarid x)) = x
1978 getQCONID (L _ (ITqconid x)) = x
1979 getQVARSYM (L _ (ITqvarsym x)) = x
1980 getQCONSYM (L _ (ITqconsym x)) = x
1981 getPREFIXQVARSYM (L _ (ITprefixqvarsym x)) = x
1982 getPREFIXQCONSYM (L _ (ITprefixqconsym x)) = x
1983 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1984 getCHAR (L _ (ITchar x)) = x
1985 getSTRING (L _ (ITstring x)) = x
1986 getINTEGER (L _ (ITinteger x)) = x
1987 getRATIONAL (L _ (ITrational x)) = x
1988 getPRIMCHAR (L _ (ITprimchar x)) = x
1989 getPRIMSTRING (L _ (ITprimstring x)) = x
1990 getPRIMINTEGER (L _ (ITprimint x)) = x
1991 getPRIMWORD (L _ (ITprimword x)) = x
1992 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1993 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1994 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1995 getINLINE (L _ (ITinline_prag inl conl)) = (inl,conl)
1996 getSPEC_INLINE (L _ (ITspec_inline_prag True)) = (Inline, FunLike)
1997 getSPEC_INLINE (L _ (ITspec_inline_prag False)) = (NoInline,FunLike)
1999 getDOCNEXT (L _ (ITdocCommentNext x)) = x
2000 getDOCPREV (L _ (ITdocCommentPrev x)) = x
2001 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
2002 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
2004 getSCC :: Located Token -> P FastString
2005 getSCC lt = do let s = getSTRING lt
2006 err = "Spaces are not allowed in SCCs"
2007 -- We probably actually want to be more restrictive than this
2008 if ' ' `elem` unpackFS s
2009 then failSpanMsgP (getLoc lt) (text err)
2012 -- Utilities for combining source spans
2013 comb2 :: Located a -> Located b -> SrcSpan
2014 comb2 a b = a `seq` b `seq` combineLocs a b
2016 comb3 :: Located a -> Located b -> Located c -> SrcSpan
2017 comb3 a b c = a `seq` b `seq` c `seq`
2018 combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
2020 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
2021 comb4 a b c d = a `seq` b `seq` c `seq` d `seq`
2022 (combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
2023 combineSrcSpans (getLoc c) (getLoc d))
2025 -- strict constructor version:
2027 sL :: SrcSpan -> a -> Located a
2028 sL span a = span `seq` a `seq` L span a
2030 -- Make a source location for the file. We're a bit lazy here and just
2031 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
2032 -- try to find the span of the whole file (ToDo).
2033 fileSrcSpan :: P SrcSpan
2036 let loc = mkSrcLoc (srcLocFile l) 1 1;
2037 return (mkSrcSpan loc loc)