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, varNameDepth, dataName, tcClsName, tvName )
43 import DataCon ( DataCon, dataConName )
46 import StaticFlags ( opt_SccProfilingOn, opt_Hpc )
47 import Type ( Kind, liftedTypeKind, unliftedTypeKind )
48 import Coercion ( mkArrowKind )
49 import Class ( FunDep )
56 import Maybes ( orElse )
59 import Control.Monad ( unless )
62 import Control.Monad ( mplus )
66 -----------------------------------------------------------------------------
69 Conflicts: 33 shift/reduce
72 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
73 would think the two should never occur in the same context.
77 -----------------------------------------------------------------------------
80 Conflicts: 34 shift/reduce
83 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
84 would think the two should never occur in the same context.
88 -----------------------------------------------------------------------------
91 Conflicts: 32 shift/reduce
94 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
95 would think the two should never occur in the same context.
99 -----------------------------------------------------------------------------
102 Conflicts: 37 shift/reduce
105 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
106 would think the two should never occur in the same context.
110 -----------------------------------------------------------------------------
111 Conflicts: 38 shift/reduce (1.25)
113 10 for abiguity in 'if x then y else z + 1' [State 178]
114 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
115 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
117 1 for ambiguity in 'if x then y else z :: T' [State 178]
118 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
120 4 for ambiguity in 'if x then y else z -< e' [State 178]
121 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
122 There are four such operators: -<, >-, -<<, >>-
125 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
126 Which of these two is intended?
128 (x::T) -> T -- Rhs is T
131 (x::T -> T) -> .. -- Rhs is ...
133 10 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
136 As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
137 Same duplication between states 11 and 253 as the previous case
139 1 for ambiguity in 'let ?x ...' [State 329]
140 the parser can't tell whether the ?x is the lhs of a normal binding or
141 an implicit binding. Fortunately resolving as shift gives it the only
142 sensible meaning, namely the lhs of an implicit binding.
144 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
145 we don't know whether the '[' starts the activation or not: it
146 might be the start of the declaration with the activation being
147 empty. --SDM 1/4/2002
149 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
150 since 'forall' is a valid variable name, we don't know whether
151 to treat a forall on the input as the beginning of a quantifier
152 or the beginning of the rule itself. Resolving to shift means
153 it's always treated as a quantifier, hence the above is disallowed.
154 This saves explicitly defining a grammar for the rule lhs that
155 doesn't include 'forall'.
157 1 for ambiguity when the source file starts with "-- | doc". We need another
158 token of lookahead to determine if a top declaration or the 'module' keyword
159 follows. Shift parses as if the 'module' keyword follows.
161 -- ---------------------------------------------------------------------------
162 -- Adding location info
164 This is done in a stylised way using the three macros below, L0, L1
165 and LL. Each of these macros can be thought of as having type
167 L0, L1, LL :: a -> Located a
169 They each add a SrcSpan to their argument.
171 L0 adds 'noSrcSpan', used for empty productions
172 -- This doesn't seem to work anymore -=chak
174 L1 for a production with a single token on the lhs. Grabs the SrcSpan
177 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
178 the first and last tokens.
180 These suffice for the majority of cases. However, we must be
181 especially careful with empty productions: LL won't work if the first
182 or last token on the lhs can represent an empty span. In these cases,
183 we have to calculate the span using more of the tokens from the lhs, eg.
185 | 'newtype' tycl_hdr '=' newconstr deriving
187 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
189 We provide comb3 and comb4 functions which are useful in such cases.
191 Be careful: there's no checking that you actually got this right, the
192 only symptom will be that the SrcSpans of your syntax will be
196 * We must expand these macros *before* running Happy, which is why this file is
197 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
199 #define L0 L noSrcSpan
200 #define L1 sL (getLoc $1)
201 #define LL sL (comb2 $1 $>)
203 -- -----------------------------------------------------------------------------
208 '_' { L _ ITunderscore } -- Haskell keywords
210 'case' { L _ ITcase }
211 'class' { L _ ITclass }
212 'data' { L _ ITdata }
213 'default' { L _ ITdefault }
214 'deriving' { L _ ITderiving }
216 'else' { L _ ITelse }
217 'hiding' { L _ IThiding }
219 'import' { L _ ITimport }
221 'infix' { L _ ITinfix }
222 'infixl' { L _ ITinfixl }
223 'infixr' { L _ ITinfixr }
224 'instance' { L _ ITinstance }
226 'module' { L _ ITmodule }
227 'newtype' { L _ ITnewtype }
229 'qualified' { L _ ITqualified }
230 'then' { L _ ITthen }
231 'type' { L _ ITtype }
232 'where' { L _ ITwhere }
233 '_scc_' { L _ ITscc } -- ToDo: remove
235 'forall' { L _ ITforall } -- GHC extension keywords
236 'foreign' { L _ ITforeign }
237 'export' { L _ ITexport }
238 'label' { L _ ITlabel }
239 'dynamic' { L _ ITdynamic }
240 'safe' { L _ ITsafe }
241 'threadsafe' { L _ ITthreadsafe } -- ToDo: remove deprecated alias
242 'interruptible' { L _ ITinterruptible }
243 'unsafe' { L _ ITunsafe }
245 'family' { L _ ITfamily }
246 'stdcall' { L _ ITstdcallconv }
247 'ccall' { L _ ITccallconv }
248 'prim' { L _ ITprimcallconv }
249 'proc' { L _ ITproc } -- for arrow notation extension
250 'rec' { L _ ITrec } -- for arrow notation extension
251 'group' { L _ ITgroup } -- for list transform extension
252 'by' { L _ ITby } -- for list transform extension
253 'using' { L _ ITusing } -- for list transform extension
255 '{-# INLINE' { L _ (ITinline_prag _ _) }
256 '{-# SPECIALISE' { L _ ITspec_prag }
257 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
258 '{-# SOURCE' { L _ ITsource_prag }
259 '{-# RULES' { L _ ITrules_prag }
260 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
261 '{-# SCC' { L _ ITscc_prag }
262 '{-# GENERATED' { L _ ITgenerated_prag }
263 '{-# DEPRECATED' { L _ ITdeprecated_prag }
264 '{-# WARNING' { L _ ITwarning_prag }
265 '{-# UNPACK' { L _ ITunpack_prag }
266 '{-# ANN' { L _ ITann_prag }
267 '{-# VECTORISE' { L _ ITvect_prag }
268 '{-# VECTORISE_SCALAR' { L _ ITvect_scalar_prag }
269 '{-# NOVECTORISE' { L _ ITnovect_prag }
270 '#-}' { L _ ITclose_prag }
272 '..' { L _ ITdotdot } -- reserved symbols
274 '::' { L _ ITdcolon }
278 '<-' { L _ ITlarrow }
279 '->' { L _ ITrarrow }
282 '=>' { L _ ITdarrow }
286 '-<' { L _ ITlarrowtail } -- for arrow notation
287 '>-' { L _ ITrarrowtail } -- for arrow notation
288 '-<<' { L _ ITLarrowtail } -- for arrow notation
289 '>>-' { L _ ITRarrowtail } -- for arrow notation
292 '{' { L _ ITocurly } -- special symbols
294 '{|' { L _ ITocurlybar }
295 '|}' { L _ ITccurlybar }
296 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
297 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
300 '[:' { L _ ITopabrack }
301 ':]' { L _ ITcpabrack }
304 '(#' { L _ IToubxparen }
305 '#)' { L _ ITcubxparen }
306 '(|' { L _ IToparenbar }
307 '|)' { L _ ITcparenbar }
308 '<[' { L _ ITopenBrak }
309 ']>' { L _ ITcloseBrak }
310 '~~' { L _ ITescape }
311 '~~$' { L _ ITescapeDollar }
312 '%%' { L _ ITdoublePercent }
315 '`' { L _ ITbackquote }
317 VARID { L _ (ITvarid _) } -- identifiers
318 CONID { L _ (ITconid _) }
319 VARSYM { L _ (ITvarsym _) }
320 CONSYM { L _ (ITconsym _) }
321 QVARID { L _ (ITqvarid _) }
322 QCONID { L _ (ITqconid _) }
323 QVARSYM { L _ (ITqvarsym _) }
324 QCONSYM { L _ (ITqconsym _) }
325 PREFIXQVARSYM { L _ (ITprefixqvarsym _) }
326 PREFIXQCONSYM { L _ (ITprefixqconsym _) }
328 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
330 CHAR { L _ (ITchar _) }
331 STRING { L _ (ITstring _) }
332 INTEGER { L _ (ITinteger _) }
333 RATIONAL { L _ (ITrational _) }
335 PRIMCHAR { L _ (ITprimchar _) }
336 PRIMSTRING { L _ (ITprimstring _) }
337 PRIMINTEGER { L _ (ITprimint _) }
338 PRIMWORD { L _ (ITprimword _) }
339 PRIMFLOAT { L _ (ITprimfloat _) }
340 PRIMDOUBLE { L _ (ITprimdouble _) }
342 DOCNEXT { L _ (ITdocCommentNext _) }
343 DOCPREV { L _ (ITdocCommentPrev _) }
344 DOCNAMED { L _ (ITdocCommentNamed _) }
345 DOCSECTION { L _ (ITdocSection _ _) }
348 '[|' { L _ ITopenExpQuote }
349 '[p|' { L _ ITopenPatQuote }
350 '[t|' { L _ ITopenTypQuote }
351 '[d|' { L _ ITopenDecQuote }
352 '|]' { L _ ITcloseQuote }
353 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
354 '$(' { L _ ITparenEscape } -- $( exp )
355 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
356 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
357 TH_QUASIQUOTE { L _ (ITquasiQuote _) }
359 %monad { P } { >>= } { return }
360 %lexer { lexer } { L _ ITeof }
361 %name parseModule module
362 %name parseStmt maybe_stmt
363 %name parseIdentifier identifier
364 %name parseType ctype
365 %partial parseHeader header
366 %tokentype { (Located Token) }
369 -----------------------------------------------------------------------------
370 -- Identifiers; one of the entry points
371 identifier :: { Located RdrName }
376 | '(' '->' ')' { LL $ getRdrName funTyCon }
378 -----------------------------------------------------------------------------
381 -- The place for module deprecation is really too restrictive, but if it
382 -- was allowed at its natural place just before 'module', we get an ugly
383 -- s/r conflict with the second alternative. Another solution would be the
384 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
385 -- either, and DEPRECATED is only expected to be used by people who really
386 -- know what they are doing. :-)
388 module :: { Located (HsModule RdrName) }
389 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' body
390 {% fileSrcSpan >>= \ loc ->
391 return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4 $1
394 {% fileSrcSpan >>= \ loc ->
395 return (L loc (HsModule Nothing Nothing
396 (fst $1) (snd $1) Nothing Nothing
399 maybedocheader :: { Maybe LHsDocString }
400 : moduleheader { $1 }
401 | {- empty -} { Nothing }
403 missing_module_keyword :: { () }
404 : {- empty -} {% pushCurrentContext }
406 maybemodwarning :: { Maybe WarningTxt }
407 : '{-# DEPRECATED' strings '#-}' { Just (DeprecatedTxt $ unLoc $2) }
408 | '{-# WARNING' strings '#-}' { Just (WarningTxt $ unLoc $2) }
409 | {- empty -} { Nothing }
411 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
413 | vocurly top close { $2 }
415 body2 :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
417 | missing_module_keyword top close { $2 }
419 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
420 : importdecls { (reverse $1,[]) }
421 | importdecls ';' cvtopdecls { (reverse $1,$3) }
422 | cvtopdecls { ([],$1) }
424 cvtopdecls :: { [LHsDecl RdrName] }
425 : topdecls { cvTopDecls $1 }
427 -----------------------------------------------------------------------------
428 -- Module declaration & imports only
430 header :: { Located (HsModule RdrName) }
431 : maybedocheader 'module' modid maybemodwarning maybeexports 'where' header_body
432 {% fileSrcSpan >>= \ loc ->
433 return (L loc (HsModule (Just $3) $5 $7 [] $4 $1
435 | missing_module_keyword importdecls
436 {% fileSrcSpan >>= \ loc ->
437 return (L loc (HsModule Nothing Nothing $2 [] Nothing
440 header_body :: { [LImportDecl RdrName] }
441 : '{' importdecls { $2 }
442 | vocurly importdecls { $2 }
444 -----------------------------------------------------------------------------
447 maybeexports :: { Maybe [LIE RdrName] }
448 : '(' exportlist ')' { Just $2 }
449 | {- empty -} { Nothing }
451 exportlist :: { [LIE RdrName] }
452 : expdoclist ',' expdoclist { $1 ++ $3 }
455 exportlist1 :: { [LIE RdrName] }
456 : expdoclist export expdoclist ',' exportlist { $1 ++ ($2 : $3) ++ $5 }
457 | expdoclist export expdoclist { $1 ++ ($2 : $3) }
460 expdoclist :: { [LIE RdrName] }
461 : exp_doc expdoclist { $1 : $2 }
464 exp_doc :: { LIE RdrName }
465 : docsection { L1 (case (unLoc $1) of (n, doc) -> IEGroup n doc) }
466 | docnamed { L1 (IEDocNamed ((fst . unLoc) $1)) }
467 | docnext { L1 (IEDoc (unLoc $1)) }
469 -- No longer allow things like [] and (,,,) to be exported
470 -- They are built in syntax, always available
471 export :: { LIE RdrName }
472 : qvar { L1 (IEVar (unLoc $1)) }
473 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
474 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
475 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
476 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
477 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
478 | '<[' incdepth export decdepth ']>' { $3 }
479 qcnames :: { [RdrName] }
480 : qcnames ',' qcname_ext { unLoc $3 : $1 }
481 | qcname_ext { [unLoc $1] }
483 qcname_ext :: { Located RdrName } -- Variable or data constructor
484 -- or tagged type constructor
486 | 'type' qcon { sL (comb2 $1 $2)
487 (setRdrNameSpace (unLoc $2)
490 -- Cannot pull into qcname_ext, as qcname is also used in expression.
491 qcname :: { Located RdrName } -- Variable or data constructor
495 -----------------------------------------------------------------------------
496 -- Import Declarations
498 -- import decls can be *empty*, or even just a string of semicolons
499 -- whereas topdecls must contain at least one topdecl.
501 importdecls :: { [LImportDecl RdrName] }
502 : importdecls ';' importdecl { $3 : $1 }
503 | importdecls ';' { $1 }
504 | importdecl { [ $1 ] }
507 importdecl :: { LImportDecl RdrName }
508 : 'import' maybe_src optqualified maybe_pkg modid maybeas maybeimpspec
509 { L (comb4 $1 $5 $6 $7) (ImportDecl $5 $4 $2 $3 (unLoc $6) (unLoc $7)) }
511 maybe_src :: { IsBootInterface }
512 : '{-# SOURCE' '#-}' { True }
513 | {- empty -} { False }
515 maybe_pkg :: { Maybe FastString }
516 : STRING { Just (getSTRING $1) }
517 | {- empty -} { Nothing }
519 optqualified :: { Bool }
520 : 'qualified' { True }
521 | {- empty -} { False }
523 maybeas :: { Located (Maybe ModuleName) }
524 : 'as' modid { LL (Just (unLoc $2)) }
525 | {- empty -} { noLoc Nothing }
527 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
528 : impspec { L1 (Just (unLoc $1)) }
529 | {- empty -} { noLoc Nothing }
531 impspec :: { Located (Bool, [LIE RdrName]) }
532 : '(' exportlist ')' { LL (False, $2) }
533 | 'hiding' '(' exportlist ')' { LL (True, $3) }
535 -----------------------------------------------------------------------------
536 -- Fixity Declarations
540 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
542 infix :: { Located FixityDirection }
543 : 'infix' { L1 InfixN }
544 | 'infixl' { L1 InfixL }
545 | 'infixr' { L1 InfixR }
547 ops :: { Located [Located RdrName] }
548 : ops ',' op { LL ($3 : unLoc $1) }
551 -----------------------------------------------------------------------------
552 -- Top-Level Declarations
554 topdecls :: { OrdList (LHsDecl RdrName) }
555 : topdecls ';' topdecl { $1 `appOL` $3 }
556 | topdecls ';' { $1 }
559 topdecl :: { OrdList (LHsDecl RdrName) }
560 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
561 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
562 | 'instance' inst_type where_inst
563 { let (binds, sigs, ats, _) = cvBindsAndSigs (unLoc $3)
565 unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs ats)))}
566 | stand_alone_deriving { unitOL (LL (DerivD (unLoc $1))) }
567 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
568 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
569 | '{-# DEPRECATED' deprecations '#-}' { $2 }
570 | '{-# WARNING' warnings '#-}' { $2 }
571 | '{-# RULES' rules '#-}' { $2 }
572 | '{-# VECTORISE_SCALAR' qvar '#-}' { unitOL $ LL $ VectD (HsVect $2 Nothing) }
573 | '{-# VECTORISE' qvar '=' exp '#-}' { unitOL $ LL $ VectD (HsVect $2 (Just $4)) }
574 | '{-# NOVECTORISE' qvar '#-}' { unitOL $ LL $ VectD (HsNoVect $2) }
575 | annotation { unitOL $1 }
578 -- Template Haskell Extension
579 -- The $(..) form is one possible form of infixexp
580 -- but we treat an arbitrary expression just as if
581 -- it had a $(..) wrapped around it
582 | infixexp { unitOL (LL $ mkTopSpliceDecl $1) }
586 cl_decl :: { LTyClDecl RdrName }
587 : 'class' tycl_hdr fds where_cls {% mkClassDecl (comb4 $1 $2 $3 $4) $2 $3 $4 }
589 -- Type declarations (toplevel)
591 ty_decl :: { LTyClDecl RdrName }
592 -- ordinary type synonyms
593 : 'type' type '=' ctypedoc
594 -- Note ctype, not sigtype, on the right of '='
595 -- We allow an explicit for-all but we don't insert one
596 -- in type Foo a = (b,b)
597 -- Instead we just say b is out of scope
599 -- Note the use of type for the head; this allows
600 -- infix type constructors to be declared
601 {% mkTySynonym (comb2 $1 $4) False $2 $4 }
603 -- type family declarations
604 | 'type' 'family' type opt_kind_sig
605 -- Note the use of type for the head; this allows
606 -- infix type constructors to be declared
607 {% mkTyFamily (comb3 $1 $3 $4) TypeFamily $3 (unLoc $4) }
609 -- type instance declarations
610 | 'type' 'instance' type '=' ctype
611 -- Note the use of type for the head; this allows
612 -- infix type constructors and type patterns
613 {% mkTySynonym (comb2 $1 $5) True $3 $5 }
615 -- ordinary data type or newtype declaration
616 | data_or_newtype tycl_hdr constrs deriving
617 {% mkTyData (comb4 $1 $2 $3 $4) (unLoc $1) False $2
618 Nothing (reverse (unLoc $3)) (unLoc $4) }
619 -- We need the location on tycl_hdr in case
620 -- constrs and deriving are both empty
622 -- ordinary GADT declaration
623 | data_or_newtype tycl_hdr opt_kind_sig
626 {% mkTyData (comb4 $1 $2 $4 $5) (unLoc $1) False $2
627 (unLoc $3) (unLoc $4) (unLoc $5) }
628 -- We need the location on tycl_hdr in case
629 -- constrs and deriving are both empty
631 -- data/newtype family
632 | 'data' 'family' type opt_kind_sig
633 {% mkTyFamily (comb3 $1 $2 $4) DataFamily $3 (unLoc $4) }
635 -- data/newtype instance declaration
636 | data_or_newtype 'instance' tycl_hdr constrs deriving
637 {% mkTyData (comb4 $1 $3 $4 $5) (unLoc $1) True $3
638 Nothing (reverse (unLoc $4)) (unLoc $5) }
640 -- GADT instance declaration
641 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
644 {% mkTyData (comb4 $1 $3 $5 $6) (unLoc $1) True $3
645 (unLoc $4) (unLoc $5) (unLoc $6) }
647 -- Associated type family declarations
649 -- * They have a different syntax than on the toplevel (no family special
652 -- * They also need to be separate from instances; otherwise, data family
653 -- declarations without a kind signature cause parsing conflicts with empty
654 -- data declarations.
656 at_decl_cls :: { LTyClDecl RdrName }
657 -- type family declarations
658 : 'type' type opt_kind_sig
659 -- Note the use of type for the head; this allows
660 -- infix type constructors to be declared
661 {% mkTyFamily (comb3 $1 $2 $3) TypeFamily $2 (unLoc $3) }
663 -- default type instance
664 | 'type' type '=' ctype
665 -- Note the use of type for the head; this allows
666 -- infix type constructors and type patterns
667 {% mkTySynonym (comb2 $1 $4) True $2 $4 }
669 -- data/newtype family declaration
670 | 'data' type opt_kind_sig
671 {% mkTyFamily (comb3 $1 $2 $3) DataFamily $2 (unLoc $3) }
673 -- Associated type instances
675 at_decl_inst :: { LTyClDecl RdrName }
676 -- type instance declarations
677 : 'type' type '=' ctype
678 -- Note the use of type for the head; this allows
679 -- infix type constructors and type patterns
680 {% mkTySynonym (comb2 $1 $4) True $2 $4 }
682 -- data/newtype instance declaration
683 | data_or_newtype tycl_hdr constrs deriving
684 {% mkTyData (comb4 $1 $2 $3 $4) (unLoc $1) True $2
685 Nothing (reverse (unLoc $3)) (unLoc $4) }
687 -- GADT instance declaration
688 | data_or_newtype tycl_hdr opt_kind_sig
691 {% mkTyData (comb4 $1 $2 $4 $5) (unLoc $1) True $2
692 (unLoc $3) (unLoc $4) (unLoc $5) }
694 data_or_newtype :: { Located NewOrData }
695 : 'data' { L1 DataType }
696 | 'newtype' { L1 NewType }
698 opt_kind_sig :: { Located (Maybe Kind) }
700 | '::' kind { LL (Just (unLoc $2)) }
702 -- tycl_hdr parses the header of a class or data type decl,
703 -- which takes the form
706 -- (Eq a, Ord b) => T a b
707 -- T Int [a] -- for associated types
708 -- Rather a lot of inlining here, else we get reduce/reduce errors
709 tycl_hdr :: { Located (Maybe (LHsContext RdrName), LHsType RdrName) }
710 : context '=>' type { LL (Just $1, $3) }
711 | type { L1 (Nothing, $1) }
713 -----------------------------------------------------------------------------
714 -- Stand-alone deriving
716 -- Glasgow extension: stand-alone deriving declarations
717 stand_alone_deriving :: { LDerivDecl RdrName }
718 : 'deriving' 'instance' inst_type { LL (DerivDecl $3) }
720 -----------------------------------------------------------------------------
721 -- Nested declarations
723 -- Declaration in class bodies
725 decl_cls :: { Located (OrdList (LHsDecl RdrName)) }
726 decl_cls : at_decl_cls { LL (unitOL (L1 (TyClD (unLoc $1)))) }
729 -- A 'default' signature used with the generic-programming extension
730 | 'default' infixexp '::' sigtypedoc
731 {% do { (TypeSig l ty) <- checkValSig $2 $4
732 ; return (LL $ unitOL (LL $ SigD (GenericSig l ty))) } }
734 decls_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
735 : decls_cls ';' decl_cls { LL (unLoc $1 `appOL` unLoc $3) }
736 | decls_cls ';' { LL (unLoc $1) }
738 | {- empty -} { noLoc nilOL }
742 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
743 : '{' decls_cls '}' { LL (unLoc $2) }
744 | vocurly decls_cls close { $2 }
748 where_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
749 -- No implicit parameters
750 -- May have type declarations
751 : 'where' decllist_cls { LL (unLoc $2) }
752 | {- empty -} { noLoc nilOL }
754 -- Declarations in instance bodies
756 decl_inst :: { Located (OrdList (LHsDecl RdrName)) }
757 decl_inst : at_decl_inst { LL (unitOL (L1 (TyClD (unLoc $1)))) }
760 decls_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
761 : decls_inst ';' decl_inst { LL (unLoc $1 `appOL` unLoc $3) }
762 | decls_inst ';' { LL (unLoc $1) }
764 | {- empty -} { noLoc nilOL }
767 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
768 : '{' decls_inst '}' { LL (unLoc $2) }
769 | vocurly decls_inst close { $2 }
773 where_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
774 -- No implicit parameters
775 -- May have type declarations
776 : 'where' decllist_inst { LL (unLoc $2) }
777 | {- empty -} { noLoc nilOL }
779 -- Declarations in binding groups other than classes and instances
781 decls :: { Located (OrdList (LHsDecl RdrName)) }
782 : decls ';' decl { let { this = unLoc $3;
784 these = rest `appOL` this }
785 in rest `seq` this `seq` these `seq`
787 | decls ';' { LL (unLoc $1) }
789 | {- empty -} { noLoc nilOL }
791 decllist :: { Located (OrdList (LHsDecl RdrName)) }
792 : '{' decls '}' { LL (unLoc $2) }
793 | vocurly decls close { $2 }
795 -- Binding groups other than those of class and instance declarations
797 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
798 -- No type declarations
799 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
800 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyTcEvBinds)) }
801 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyTcEvBinds)) }
803 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
804 -- No type declarations
805 : 'where' binds { LL (unLoc $2) }
806 | {- empty -} { noLoc emptyLocalBinds }
809 -----------------------------------------------------------------------------
810 -- Transformation Rules
812 rules :: { OrdList (LHsDecl RdrName) }
813 : rules ';' rule { $1 `snocOL` $3 }
816 | {- empty -} { nilOL }
818 rule :: { LHsDecl RdrName }
819 : STRING activation rule_forall infixexp '=' exp
820 { LL $ RuleD (HsRule (getSTRING $1)
821 ($2 `orElse` AlwaysActive)
822 $3 $4 placeHolderNames $6 placeHolderNames) }
824 activation :: { Maybe Activation }
825 : {- empty -} { Nothing }
826 | explicit_activation { Just $1 }
828 explicit_activation :: { Activation } -- In brackets
829 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
830 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
832 rule_forall :: { [RuleBndr RdrName] }
833 : 'forall' rule_var_list '.' { $2 }
836 rule_var_list :: { [RuleBndr RdrName] }
838 | rule_var rule_var_list { $1 : $2 }
840 rule_var :: { RuleBndr RdrName }
841 : varid { RuleBndr $1 }
842 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
844 -----------------------------------------------------------------------------
845 -- Warnings and deprecations (c.f. rules)
847 warnings :: { OrdList (LHsDecl RdrName) }
848 : warnings ';' warning { $1 `appOL` $3 }
849 | warnings ';' { $1 }
851 | {- empty -} { nilOL }
853 -- SUP: TEMPORARY HACK, not checking for `module Foo'
854 warning :: { OrdList (LHsDecl RdrName) }
856 { toOL [ LL $ WarningD (Warning n (WarningTxt $ unLoc $2))
859 deprecations :: { OrdList (LHsDecl RdrName) }
860 : deprecations ';' deprecation { $1 `appOL` $3 }
861 | deprecations ';' { $1 }
863 | {- empty -} { nilOL }
865 -- SUP: TEMPORARY HACK, not checking for `module Foo'
866 deprecation :: { OrdList (LHsDecl RdrName) }
868 { toOL [ LL $ WarningD (Warning n (DeprecatedTxt $ unLoc $2))
871 strings :: { Located [FastString] }
872 : STRING { L1 [getSTRING $1] }
873 | '[' stringlist ']' { LL $ fromOL (unLoc $2) }
875 stringlist :: { Located (OrdList FastString) }
876 : stringlist ',' STRING { LL (unLoc $1 `snocOL` getSTRING $3) }
877 | STRING { LL (unitOL (getSTRING $1)) }
879 -----------------------------------------------------------------------------
881 annotation :: { LHsDecl RdrName }
882 : '{-# ANN' name_var aexp '#-}' { LL (AnnD $ HsAnnotation (ValueAnnProvenance (unLoc $2)) $3) }
883 | '{-# ANN' 'type' tycon aexp '#-}' { LL (AnnD $ HsAnnotation (TypeAnnProvenance (unLoc $3)) $4) }
884 | '{-# ANN' 'module' aexp '#-}' { LL (AnnD $ HsAnnotation ModuleAnnProvenance $3) }
887 -----------------------------------------------------------------------------
888 -- Foreign import and export declarations
890 fdecl :: { LHsDecl RdrName }
891 fdecl : 'import' callconv safety fspec
892 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
893 | 'import' callconv fspec
894 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
896 | 'export' callconv fspec
897 {% mkExport $2 (unLoc $3) >>= return.LL }
899 callconv :: { CCallConv }
900 : 'stdcall' { StdCallConv }
901 | 'ccall' { CCallConv }
902 | 'prim' { PrimCallConv}
905 : 'unsafe' { PlayRisky }
906 | 'safe' { PlaySafe False }
907 | 'interruptible' { PlayInterruptible }
908 | 'threadsafe' { PlaySafe True } -- deprecated alias
910 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
911 : STRING var '::' sigtypedoc { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
912 | var '::' sigtypedoc { LL (noLoc nilFS, $1, $3) }
913 -- if the entity string is missing, it defaults to the empty string;
914 -- the meaning of an empty entity string depends on the calling
917 -----------------------------------------------------------------------------
920 opt_sig :: { Maybe (LHsType RdrName) }
921 : {- empty -} { Nothing }
922 | '::' sigtype { Just $2 }
924 opt_asig :: { Maybe (LHsType RdrName) }
925 : {- empty -} { Nothing }
926 | '::' atype { Just $2 }
928 sigtype :: { LHsType RdrName } -- Always a HsForAllTy,
929 -- to tell the renamer where to generalise
930 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
931 -- Wrap an Implicit forall if there isn't one there already
933 sigtypedoc :: { LHsType RdrName } -- Always a HsForAllTy
934 : ctypedoc { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
935 -- Wrap an Implicit forall if there isn't one there already
937 sig_vars :: { Located [Located RdrName] }
938 : sig_vars ',' var { LL ($3 : unLoc $1) }
941 sigtypes1 :: { [LHsType RdrName] } -- Always HsForAllTys
943 | sigtype ',' sigtypes1 { $1 : $3 }
945 -----------------------------------------------------------------------------
948 infixtype :: { LHsType RdrName }
949 : btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
950 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
952 strict_mark :: { Located HsBang }
953 : '!' { L1 HsStrict }
954 | '{-# UNPACK' '#-}' '!' { LL HsUnpack }
956 -- A ctype is a for-all type
957 ctype :: { LHsType RdrName }
958 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
959 | context '=>' ctype { LL $ mkImplicitHsForAllTy $1 $3 }
960 -- A type of form (context => type) is an *implicit* HsForAllTy
961 | ipvar '::' type { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
964 ----------------------
965 -- Notes for 'ctypedoc'
966 -- It would have been nice to simplify the grammar by unifying `ctype` and
967 -- ctypedoc` into one production, allowing comments on types everywhere (and
968 -- rejecting them after parsing, where necessary). This is however not possible
969 -- since it leads to ambiguity. The reason is the support for comments on record
971 -- data R = R { field :: Int -- ^ comment on the field }
972 -- If we allow comments on types here, it's not clear if the comment applies
973 -- to 'field' or to 'Int'. So we must use `ctype` to describe the type.
975 ctypedoc :: { LHsType RdrName }
976 : 'forall' tv_bndrs '.' ctypedoc { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
977 | context '=>' ctypedoc { LL $ mkImplicitHsForAllTy $1 $3 }
978 -- A type of form (context => type) is an *implicit* HsForAllTy
979 | ipvar '::' type { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
982 ----------------------
983 -- Notes for 'context'
984 -- We parse a context as a btype so that we don't get reduce/reduce
985 -- errors in ctype. The basic problem is that
987 -- looks so much like a tuple type. We can't tell until we find the =>
989 -- We have the t1 ~ t2 form both in 'context' and in type,
990 -- to permit an individual equational constraint without parenthesis.
991 -- Thus for some reason we allow f :: a~b => blah
992 -- but not f :: ?x::Int => blah
993 context :: { LHsContext RdrName }
994 : btype '~' btype {% checkContext
995 (LL $ HsPredTy (HsEqualP $1 $3)) }
996 | btype {% checkContext $1 }
998 type :: { LHsType RdrName }
1000 | btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
1001 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
1002 | btype '->' ctype { LL $ HsFunTy $1 $3 }
1003 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1005 typedoc :: { LHsType RdrName }
1007 | btype docprev { LL $ HsDocTy $1 $2 }
1008 | btype qtyconop type { LL $ HsOpTy $1 $2 $3 }
1009 | btype qtyconop type docprev { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
1010 | btype tyvarop type { LL $ HsOpTy $1 $2 $3 }
1011 | btype tyvarop type docprev { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
1012 | btype '->' ctypedoc { LL $ HsFunTy $1 $3 }
1013 | btype docprev '->' ctypedoc { LL $ HsFunTy (L (comb2 $1 $2) (HsDocTy $1 $2)) $4 }
1014 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1016 btype :: { LHsType RdrName }
1017 : btype atype { LL $ HsAppTy $1 $2 }
1020 atype :: { LHsType RdrName }
1021 : gtycon { L1 (HsTyVar (unLoc $1)) }
1022 | tyvar { L1 (HsTyVar (unLoc $1)) }
1023 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) } -- Constructor sigs only
1024 | '{' fielddecls '}' { LL $ HsRecTy $2 } -- Constructor sigs only
1025 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
1026 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
1027 | '[' ctype ']' { LL $ HsListTy $2 }
1028 | '<[' ctype ']>' '@' tyvar { LL $ HsModalBoxType (unLoc $5) $2 }
1029 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
1030 | '(' ctype ')' { LL $ HsParTy $2 }
1031 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
1032 | quasiquote { L1 (HsQuasiQuoteTy (unLoc $1)) }
1033 | '$(' exp ')' { LL $ mkHsSpliceTy $2 }
1034 | TH_ID_SPLICE { LL $ mkHsSpliceTy $ L1 $ HsVar $
1035 mkUnqual varName (getTH_ID_SPLICE $1) }
1037 -- An inst_type is what occurs in the head of an instance decl
1038 -- e.g. (Foo a, Gaz b) => Wibble a b
1039 -- It's kept as a single type, with a MonoDictTy at the right
1040 -- hand corner, for convenience.
1041 inst_type :: { LHsType RdrName }
1042 : sigtype {% checkInstType $1 }
1044 inst_types1 :: { [LHsType RdrName] }
1045 : inst_type { [$1] }
1046 | inst_type ',' inst_types1 { $1 : $3 }
1048 comma_types0 :: { [LHsType RdrName] }
1049 : comma_types1 { $1 }
1050 | {- empty -} { [] }
1052 comma_types1 :: { [LHsType RdrName] }
1054 | ctype ',' comma_types1 { $1 : $3 }
1056 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1057 : tv_bndr tv_bndrs { $1 : $2 }
1058 | {- empty -} { [] }
1060 tv_bndr :: { LHsTyVarBndr RdrName }
1061 : tyvar { L1 (UserTyVar (unLoc $1) placeHolderKind) }
1062 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
1065 fds :: { Located [Located (FunDep RdrName)] }
1066 : {- empty -} { noLoc [] }
1067 | '|' fds1 { LL (reverse (unLoc $2)) }
1069 fds1 :: { Located [Located (FunDep RdrName)] }
1070 : fds1 ',' fd { LL ($3 : unLoc $1) }
1073 fd :: { Located (FunDep RdrName) }
1074 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
1075 (reverse (unLoc $1), reverse (unLoc $3)) }
1077 varids0 :: { Located [RdrName] }
1078 : {- empty -} { noLoc [] }
1079 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
1081 -----------------------------------------------------------------------------
1084 kind :: { Located Kind }
1086 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1088 akind :: { Located Kind }
1089 : '*' { L1 liftedTypeKind }
1090 | '!' { L1 unliftedTypeKind }
1091 | '(' kind ')' { LL (unLoc $2) }
1094 -----------------------------------------------------------------------------
1095 -- Datatype declarations
1097 gadt_constrlist :: { Located [LConDecl RdrName] } -- Returned in order
1098 : 'where' '{' gadt_constrs '}' { L (comb2 $1 $3) (unLoc $3) }
1099 | 'where' vocurly gadt_constrs close { L (comb2 $1 $3) (unLoc $3) }
1100 | {- empty -} { noLoc [] }
1102 gadt_constrs :: { Located [LConDecl RdrName] }
1103 : gadt_constr ';' gadt_constrs { L (comb2 (head $1) $3) ($1 ++ unLoc $3) }
1104 | gadt_constr { L (getLoc (head $1)) $1 }
1105 | {- empty -} { noLoc [] }
1107 -- We allow the following forms:
1108 -- C :: Eq a => a -> T a
1109 -- C :: forall a. Eq a => !a -> T a
1110 -- D { x,y :: a } :: T a
1111 -- forall a. Eq a => D { x,y :: a } :: T a
1113 gadt_constr :: { [LConDecl RdrName] } -- Returns a list because of: C,D :: ty
1114 : con_list '::' sigtype
1115 { map (sL (comb2 $1 $3)) (mkGadtDecl (unLoc $1) $3) }
1117 -- Deprecated syntax for GADT record declarations
1118 | oqtycon '{' fielddecls '}' '::' sigtype
1119 {% do { cd <- mkDeprecatedGadtRecordDecl (comb2 $1 $6) $1 $3 $6
1122 constrs :: { Located [LConDecl RdrName] }
1123 : maybe_docnext '=' constrs1 { L (comb2 $2 $3) (addConDocs (unLoc $3) $1) }
1125 constrs1 :: { Located [LConDecl RdrName] }
1126 : constrs1 maybe_docnext '|' maybe_docprev constr { LL (addConDoc $5 $2 : addConDocFirst (unLoc $1) $4) }
1127 | constr { L1 [$1] }
1129 constr :: { LConDecl RdrName }
1130 : maybe_docnext forall context '=>' constr_stuff maybe_docprev
1131 { let (con,details) = unLoc $5 in
1132 addConDoc (L (comb4 $2 $3 $4 $5) (mkSimpleConDecl con (unLoc $2) $3 details))
1134 | maybe_docnext forall constr_stuff maybe_docprev
1135 { let (con,details) = unLoc $3 in
1136 addConDoc (L (comb2 $2 $3) (mkSimpleConDecl con (unLoc $2) (noLoc []) details))
1139 forall :: { Located [LHsTyVarBndr RdrName] }
1140 : 'forall' tv_bndrs '.' { LL $2 }
1141 | {- empty -} { noLoc [] }
1143 constr_stuff :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1144 -- We parse the constructor declaration
1146 -- as a btype (treating C as a type constructor) and then convert C to be
1147 -- a data constructor. Reason: it might continue like this:
1149 -- in which case C really would be a type constructor. We can't resolve this
1150 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1151 : btype {% splitCon $1 >>= return.LL }
1152 | btype conop btype { LL ($2, InfixCon $1 $3) }
1154 fielddecls :: { [ConDeclField RdrName] }
1155 : {- empty -} { [] }
1156 | fielddecls1 { $1 }
1158 fielddecls1 :: { [ConDeclField RdrName] }
1159 : fielddecl maybe_docnext ',' maybe_docprev fielddecls1
1160 { [ addFieldDoc f $4 | f <- $1 ] ++ addFieldDocs $5 $2 }
1161 -- This adds the doc $4 to each field separately
1164 fielddecl :: { [ConDeclField RdrName] } -- A list because of f,g :: Int
1165 : maybe_docnext sig_vars '::' ctype maybe_docprev { [ ConDeclField fld $4 ($1 `mplus` $5)
1166 | fld <- reverse (unLoc $2) ] }
1168 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1169 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1170 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1171 -- We don't allow a context, but that's sorted out by the type checker.
1172 deriving :: { Located (Maybe [LHsType RdrName]) }
1173 : {- empty -} { noLoc Nothing }
1174 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1175 ; p <- checkInstType (L loc (HsTyVar tv))
1176 ; return (LL (Just [p])) } }
1177 | 'deriving' '(' ')' { LL (Just []) }
1178 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1179 -- Glasgow extension: allow partial
1180 -- applications in derivings
1182 -----------------------------------------------------------------------------
1183 -- Value definitions
1185 {- Note [Declaration/signature overlap]
1186 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1187 There's an awkward overlap with a type signature. Consider
1188 f :: Int -> Int = ...rhs...
1189 Then we can't tell whether it's a type signature or a value
1190 definition with a result signature until we see the '='.
1191 So we have to inline enough to postpone reductions until we know.
1195 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1196 instead of qvar, we get another shift/reduce-conflict. Consider the
1199 { (^^) :: Int->Int ; } Type signature; only var allowed
1201 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1202 qvar allowed (because of instance decls)
1204 We can't tell whether to reduce var to qvar until after we've read the signatures.
1207 docdecl :: { LHsDecl RdrName }
1208 : docdecld { L1 (DocD (unLoc $1)) }
1210 docdecld :: { LDocDecl }
1211 : docnext { L1 (DocCommentNext (unLoc $1)) }
1212 | docprev { L1 (DocCommentPrev (unLoc $1)) }
1213 | docnamed { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1214 | docsection { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1216 decl :: { Located (OrdList (LHsDecl RdrName)) }
1219 | '!' aexp rhs {% do { let { e = LL (SectionR (LL (HsVar bang_RDR)) $2) };
1220 pat <- checkPattern e;
1221 return $ LL $ unitOL $ LL $ ValD $
1222 PatBind pat (unLoc $3)
1223 placeHolderType placeHolderNames } }
1224 -- Turn it all into an expression so that
1225 -- checkPattern can check that bangs are enabled
1227 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1228 let { l = comb2 $1 $> };
1229 return $! (sL l (unitOL $! (sL l $ ValD r))) } }
1231 | docdecl { LL $ unitOL $1 }
1233 rhs :: { Located (GRHSs RdrName) }
1234 : '=' exp wherebinds { sL (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1235 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1237 gdrhs :: { Located [LGRHS RdrName] }
1238 : gdrhs gdrh { LL ($2 : unLoc $1) }
1241 gdrh :: { LGRHS RdrName }
1242 : '|' guardquals '=' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1244 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1246 -- See Note [Declaration/signature overlap] for why we need infixexp here
1247 infixexp '::' sigtypedoc
1248 {% do s <- checkValSig $1 $3
1249 ; return (LL $ unitOL (LL $ SigD s)) }
1250 | var ',' sig_vars '::' sigtypedoc
1251 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1252 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1254 | '{-# INLINE' activation qvar '#-}'
1255 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlinePragma (getINLINE $1) $2))) }
1256 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1257 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlinePragma)
1259 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1260 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlinePragma (getSPEC_INLINE $1) $2))
1262 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1263 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1265 -----------------------------------------------------------------------------
1268 quasiquote :: { Located (HsQuasiQuote RdrName) }
1269 : TH_QUASIQUOTE { let { loc = getLoc $1
1270 ; ITquasiQuote (quoter, quote, quoteSpan) = unLoc $1
1271 ; quoterId = mkUnqual varName quoter }
1272 in L1 (mkHsQuasiQuote quoterId (RealSrcSpan quoteSpan) quote) }
1274 incdepth :: { Located () } : {% do { incrBracketDepth ; return $ noLoc () } }
1275 decdepth :: { Located () } : {% do { decrBracketDepth ; return $ noLoc () } }
1278 exp :: { LHsExpr RdrName }
1279 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1280 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1281 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1282 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1283 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1285 | '~~$' decdepth exp incdepth { sL (comb2 $3 $>) (HsHetMetEsc placeHolderType placeHolderType $3) }
1287 infixexp :: { LHsExpr RdrName }
1289 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1291 exp10 :: { LHsExpr RdrName }
1292 : '\\' apat apats opt_asig '->' exp
1293 { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1296 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1297 | 'if' exp optSemi 'then' exp optSemi 'else' exp
1298 {% checkDoAndIfThenElse $2 $3 $5 $6 $8 >>
1299 return (LL $ mkHsIf $2 $5 $8) }
1300 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1301 | '-' fexp { LL $ NegApp $2 noSyntaxExpr }
1303 | 'do' stmtlist { L (comb2 $1 $2) (mkHsDo DoExpr (unLoc $2)) }
1304 | 'mdo' stmtlist { L (comb2 $1 $2) (mkHsDo MDoExpr (unLoc $2)) }
1306 | scc_annot exp { LL $ if opt_SccProfilingOn
1307 then HsSCC (unLoc $1) $2
1309 | hpc_annot exp { LL $ if opt_Hpc
1310 then HsTickPragma (unLoc $1) $2
1313 | 'proc' aexp '->' exp
1314 {% checkPattern $2 >>= \ p ->
1315 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1316 placeHolderType undefined)) }
1317 -- TODO: is LL right here?
1319 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1320 -- hdaume: core annotation
1325 | {- empty -} { False }
1327 scc_annot :: { Located FastString }
1328 : '_scc_' STRING {% (addWarning Opt_WarnWarningsDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1329 ( do scc <- getSCC $2; return $ LL scc ) }
1330 | '{-# SCC' STRING '#-}' {% do scc <- getSCC $2; return $ LL scc }
1332 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1333 : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1334 { LL $ (getSTRING $2
1335 ,( fromInteger $ getINTEGER $3
1336 , fromInteger $ getINTEGER $5
1338 ,( fromInteger $ getINTEGER $7
1339 , fromInteger $ getINTEGER $9
1344 fexp :: { LHsExpr RdrName }
1345 : fexp aexp { LL $ HsApp $1 $2 }
1348 aexp :: { LHsExpr RdrName }
1349 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1350 | '~' aexp { LL $ ELazyPat $2 }
1353 aexp1 :: { LHsExpr RdrName }
1354 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1358 -- Here was the syntax for type applications that I was planning
1359 -- but there are difficulties (e.g. what order for type args)
1360 -- so it's not enabled yet.
1361 -- But this case *is* used for the left hand side of a generic definition,
1362 -- which is parsed as an expression before being munged into a pattern
1363 | qcname '{|' type '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1364 (sL (getLoc $3) (HsType $3)) }
1366 aexp2 :: { LHsExpr RdrName }
1367 : ipvar { L1 (HsIPVar $! unLoc $1) }
1368 | qcname { L1 (HsVar $! unLoc $1) }
1369 | literal { L1 (HsLit $! unLoc $1) }
1370 -- This will enable overloaded strings permanently. Normally the renamer turns HsString
1371 -- into HsOverLit when -foverloaded-strings is on.
1372 -- | STRING { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1373 | INTEGER { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1374 | RATIONAL { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1376 -- N.B.: sections get parsed by these next two productions.
1377 -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't
1378 -- correct Haskell (you'd have to write '((+ 3), (4 -))')
1379 -- but the less cluttered version fell out of having texps.
1380 | '(' texp ')' { LL (HsPar $2) }
1381 | '(' tup_exprs ')' { LL (ExplicitTuple $2 Boxed) }
1383 | '(#' texp '#)' { LL (ExplicitTuple [Present $2] Unboxed) }
1384 | '(#' tup_exprs '#)' { LL (ExplicitTuple $2 Unboxed) }
1386 | '[' list ']' { LL (unLoc $2) }
1387 | '[:' parr ':]' { LL (unLoc $2) }
1388 | '_' { L1 EWildPat }
1390 -- Template Haskell Extension
1391 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1392 (L1 $ HsVar (mkUnqual varName
1393 (getTH_ID_SPLICE $1)))) }
1394 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) }
1397 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1398 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1399 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1400 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1401 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1402 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1403 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1404 return (LL $ HsBracket (PatBr p)) }
1405 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBrL $2) }
1406 | quasiquote { L1 (HsQuasiQuoteE (unLoc $1)) }
1408 -- arrow notation extension
1409 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1411 -- code type notation extension
1412 | '<[' incdepth exp decdepth ']>' { sL (comb2 $3 $>) (HsHetMetBrak placeHolderType $3) }
1413 | '~~' decdepth aexp incdepth { sL (comb2 $3 $>) (HsHetMetEsc placeHolderType placeHolderType $3) }
1414 | '%%' decdepth aexp incdepth { sL (comb2 $3 $>) (HsHetMetCSP placeHolderType $3) }
1416 cmdargs :: { [LHsCmdTop RdrName] }
1417 : cmdargs acmd { $2 : $1 }
1418 | {- empty -} { [] }
1420 acmd :: { LHsCmdTop RdrName }
1421 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1423 cvtopbody :: { [LHsDecl RdrName] }
1424 : '{' cvtopdecls0 '}' { $2 }
1425 | vocurly cvtopdecls0 close { $2 }
1427 cvtopdecls0 :: { [LHsDecl RdrName] }
1428 : {- empty -} { [] }
1431 -----------------------------------------------------------------------------
1432 -- Tuple expressions
1434 -- "texp" is short for tuple expressions:
1435 -- things that can appear unparenthesized as long as they're
1436 -- inside parens or delimitted by commas
1437 texp :: { LHsExpr RdrName }
1440 -- Note [Parsing sections]
1441 -- ~~~~~~~~~~~~~~~~~~~~~~~
1442 -- We include left and right sections here, which isn't
1443 -- technically right according to the Haskell standard.
1444 -- For example (3 +, True) isn't legal.
1445 -- However, we want to parse bang patterns like
1447 -- and it's convenient to do so here as a section
1448 -- Then when converting expr to pattern we unravel it again
1449 -- Meanwhile, the renamer checks that real sections appear
1451 | infixexp qop { LL $ SectionL $1 $2 }
1452 | qopm infixexp { LL $ SectionR $1 $2 }
1454 -- View patterns get parenthesized above
1455 | exp '->' texp { LL $ EViewPat $1 $3 }
1457 -- Always at least one comma
1458 tup_exprs :: { [HsTupArg RdrName] }
1459 : texp commas_tup_tail { Present $1 : $2 }
1460 | commas tup_tail { replicate $1 missingTupArg ++ $2 }
1462 -- Always starts with commas; always follows an expr
1463 commas_tup_tail :: { [HsTupArg RdrName] }
1464 commas_tup_tail : commas tup_tail { replicate ($1-1) missingTupArg ++ $2 }
1466 -- Always follows a comma
1467 tup_tail :: { [HsTupArg RdrName] }
1468 : texp commas_tup_tail { Present $1 : $2 }
1469 | texp { [Present $1] }
1470 | {- empty -} { [missingTupArg] }
1472 -----------------------------------------------------------------------------
1475 -- The rules below are little bit contorted to keep lexps left-recursive while
1476 -- avoiding another shift/reduce-conflict.
1478 list :: { LHsExpr RdrName }
1479 : texp { L1 $ ExplicitList placeHolderType [$1] }
1480 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1481 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1482 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1483 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1484 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1485 | texp '|' flattenedpquals
1486 {% checkMonadComp >>= \ ctxt ->
1487 return (sL (comb2 $1 $>) $
1488 mkHsComp ctxt (unLoc $3) $1) }
1490 lexps :: { Located [LHsExpr RdrName] }
1491 : lexps ',' texp { LL (((:) $! $3) $! unLoc $1) }
1492 | texp ',' texp { LL [$3,$1] }
1494 -----------------------------------------------------------------------------
1495 -- List Comprehensions
1497 flattenedpquals :: { Located [LStmt RdrName] }
1498 : pquals { case (unLoc $1) of
1500 -- We just had one thing in our "parallel" list so
1501 -- we simply return that thing directly
1503 qss -> L1 [L1 $ ParStmt [(qs, undefined) | qs <- qss] noSyntaxExpr noSyntaxExpr noSyntaxExpr]
1504 -- We actually found some actual parallel lists so
1505 -- we wrap them into as a ParStmt
1508 pquals :: { Located [[LStmt RdrName]] }
1509 : squals '|' pquals { L (getLoc $2) (reverse (unLoc $1) : unLoc $3) }
1510 | squals { L (getLoc $1) [reverse (unLoc $1)] }
1512 squals :: { Located [LStmt RdrName] } -- In reverse order, because the last
1513 -- one can "grab" the earlier ones
1514 : squals ',' transformqual { LL [L (getLoc $3) ((unLoc $3) (reverse (unLoc $1)))] }
1515 | squals ',' qual { LL ($3 : unLoc $1) }
1516 | transformqual { LL [L (getLoc $1) ((unLoc $1) [])] }
1518 -- | transformquals1 ',' '{|' pquals '|}' { LL ($4 : unLoc $1) }
1519 -- | '{|' pquals '|}' { L1 [$2] }
1522 -- It is possible to enable bracketing (associating) qualifier lists by uncommenting the lines with {| |}
1523 -- above. Due to a lack of consensus on the syntax, this feature is not being used until we get user
1526 transformqual :: { Located ([LStmt RdrName] -> Stmt RdrName) }
1527 -- Function is applied to a list of stmts *in order*
1528 : 'then' exp { LL $ \leftStmts -> (mkTransformStmt leftStmts $2) }
1530 | 'then' exp 'by' exp { LL $ \leftStmts -> (mkTransformByStmt leftStmts $2 $4) }
1531 | 'then' 'group' 'by' exp { LL $ \leftStmts -> (mkGroupByStmt leftStmts $4) }
1533 -- These two productions deliberately have a shift-reduce conflict. I have made 'group' into a special_id,
1534 -- which means you can enable TransformListComp while still using Data.List.group. However, this makes the two
1535 -- productions ambiguous. I've set things up so that Happy chooses to resolve the conflict in that case by
1536 -- choosing the "group by" variant, which is what we want.
1538 -- This is rather dubious: the user might be confused as to how to parse this statement. However, it is a good
1539 -- practical choice. NB: Data.List.group :: [a] -> [[a]], so using the first production would not even type check
1540 -- if /that/ is the group function we conflict with.
1541 | 'then' 'group' 'using' exp { LL $ \leftStmts -> (mkGroupUsingStmt leftStmts $4) }
1542 | 'then' 'group' 'by' exp 'using' exp { LL $ \leftStmts -> (mkGroupByUsingStmt leftStmts $4 $6) }
1544 -----------------------------------------------------------------------------
1545 -- Parallel array expressions
1547 -- The rules below are little bit contorted; see the list case for details.
1548 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1549 -- Moreover, we allow explicit arrays with no element (represented by the nil
1550 -- constructor in the list case).
1552 parr :: { LHsExpr RdrName }
1553 : { noLoc (ExplicitPArr placeHolderType []) }
1554 | texp { L1 $ ExplicitPArr placeHolderType [$1] }
1555 | lexps { L1 $ ExplicitPArr placeHolderType
1556 (reverse (unLoc $1)) }
1557 | texp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1558 | texp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1559 | texp '|' flattenedpquals { LL $ mkHsComp PArrComp (unLoc $3) $1 }
1561 -- We are reusing `lexps' and `flattenedpquals' from the list case.
1563 -----------------------------------------------------------------------------
1566 guardquals :: { Located [LStmt RdrName] }
1567 : guardquals1 { L (getLoc $1) (reverse (unLoc $1)) }
1569 guardquals1 :: { Located [LStmt RdrName] }
1570 : guardquals1 ',' qual { LL ($3 : unLoc $1) }
1573 -----------------------------------------------------------------------------
1574 -- Case alternatives
1576 altslist :: { Located [LMatch RdrName] }
1577 : '{' alts '}' { LL (reverse (unLoc $2)) }
1578 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1580 alts :: { Located [LMatch RdrName] }
1581 : alts1 { L1 (unLoc $1) }
1582 | ';' alts { LL (unLoc $2) }
1584 alts1 :: { Located [LMatch RdrName] }
1585 : alts1 ';' alt { LL ($3 : unLoc $1) }
1586 | alts1 ';' { LL (unLoc $1) }
1589 alt :: { LMatch RdrName }
1590 : pat opt_sig alt_rhs { LL (Match [$1] $2 (unLoc $3)) }
1592 alt_rhs :: { Located (GRHSs RdrName) }
1593 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1595 ralt :: { Located [LGRHS RdrName] }
1596 : '->' exp { LL (unguardedRHS $2) }
1597 | gdpats { L1 (reverse (unLoc $1)) }
1599 gdpats :: { Located [LGRHS RdrName] }
1600 : gdpats gdpat { LL ($2 : unLoc $1) }
1603 gdpat :: { LGRHS RdrName }
1604 : '|' guardquals '->' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1606 -- 'pat' recognises a pattern, including one with a bang at the top
1607 -- e.g. "!x" or "!(x,y)" or "C a b" etc
1608 -- Bangs inside are parsed as infix operator applications, so that
1609 -- we parse them right when bang-patterns are off
1610 pat :: { LPat RdrName }
1611 pat : exp {% checkPattern $1 }
1612 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1614 apat :: { LPat RdrName }
1615 apat : aexp {% checkPattern $1 }
1616 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1618 apats :: { [LPat RdrName] }
1619 : apat apats { $1 : $2 }
1620 | {- empty -} { [] }
1622 -----------------------------------------------------------------------------
1623 -- Statement sequences
1625 stmtlist :: { Located [LStmt RdrName] }
1626 : '{' stmts '}' { LL (unLoc $2) }
1627 | vocurly stmts close { $2 }
1629 -- do { ;; s ; s ; ; s ;; }
1630 -- The last Stmt should be an expression, but that's hard to enforce
1631 -- here, because we need too much lookahead if we see do { e ; }
1632 -- So we use ExprStmts throughout, and switch the last one over
1633 -- in ParseUtils.checkDo instead
1634 stmts :: { Located [LStmt RdrName] }
1635 : stmt stmts_help { LL ($1 : unLoc $2) }
1636 | ';' stmts { LL (unLoc $2) }
1637 | {- empty -} { noLoc [] }
1639 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1640 : ';' stmts { LL (unLoc $2) }
1641 | {- empty -} { noLoc [] }
1643 -- For typing stmts at the GHCi prompt, where
1644 -- the input may consist of just comments.
1645 maybe_stmt :: { Maybe (LStmt RdrName) }
1647 | {- nothing -} { Nothing }
1649 stmt :: { LStmt RdrName }
1651 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1653 qual :: { LStmt RdrName }
1654 : pat '<-' exp { LL $ mkBindStmt $1 $3 }
1655 | exp { L1 $ mkExprStmt $1 }
1656 | 'let' binds { LL $ LetStmt (unLoc $2) }
1658 -----------------------------------------------------------------------------
1659 -- Record Field Update/Construction
1661 fbinds :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1663 | {- empty -} { ([], False) }
1665 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1666 : fbind ',' fbinds1 { case $3 of (flds, dd) -> ($1 : flds, dd) }
1667 | fbind { ([$1], False) }
1668 | '..' { ([], True) }
1670 fbind :: { HsRecField RdrName (LHsExpr RdrName) }
1671 : qvar '=' exp { HsRecField $1 $3 False }
1672 | qvar { HsRecField $1 placeHolderPunRhs True }
1673 -- In the punning case, use a place-holder
1674 -- The renamer fills in the final value
1676 -----------------------------------------------------------------------------
1677 -- Implicit Parameter Bindings
1679 dbinds :: { Located [LIPBind RdrName] }
1680 : dbinds ';' dbind { let { this = $3; rest = unLoc $1 }
1681 in rest `seq` this `seq` LL (this : rest) }
1682 | dbinds ';' { LL (unLoc $1) }
1683 | dbind { let this = $1 in this `seq` L1 [this] }
1684 -- | {- empty -} { [] }
1686 dbind :: { LIPBind RdrName }
1687 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1689 ipvar :: { Located (IPName RdrName) }
1690 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1692 -----------------------------------------------------------------------------
1693 -- Warnings and deprecations
1695 namelist :: { Located [RdrName] }
1696 namelist : name_var { L1 [unLoc $1] }
1697 | name_var ',' namelist { LL (unLoc $1 : unLoc $3) }
1699 name_var :: { Located RdrName }
1700 name_var : var { $1 }
1703 -----------------------------------------
1704 -- Data constructors
1705 qcon :: { Located RdrName }
1707 | '(' qconsym ')' { LL (unLoc $2) }
1708 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1709 -- The case of '[:' ':]' is part of the production `parr'
1711 con :: { Located RdrName }
1713 | '(' consym ')' { LL (unLoc $2) }
1714 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1716 con_list :: { Located [Located RdrName] }
1717 con_list : con { L1 [$1] }
1718 | con ',' con_list { LL ($1 : unLoc $3) }
1720 sysdcon :: { Located DataCon } -- Wired in data constructors
1721 : '(' ')' { LL unitDataCon }
1722 | '(' commas ')' { LL $ tupleCon Boxed ($2 + 1) }
1723 | '(#' '#)' { LL $ unboxedSingletonDataCon }
1724 | '(#' commas '#)' { LL $ tupleCon Unboxed ($2 + 1) }
1725 | '[' ']' { LL nilDataCon }
1727 conop :: { Located RdrName }
1729 | '`' conid '`' { LL (unLoc $2) }
1731 qconop :: { Located RdrName }
1733 | '`' qconid '`' { LL (unLoc $2) }
1735 -----------------------------------------------------------------------------
1736 -- Type constructors
1738 gtycon :: { Located RdrName } -- A "general" qualified tycon
1740 | '(' ')' { LL $ getRdrName unitTyCon }
1741 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed ($2 + 1)) }
1742 | '(#' '#)' { LL $ getRdrName unboxedSingletonTyCon }
1743 | '(#' commas '#)' { LL $ getRdrName (tupleTyCon Unboxed ($2 + 1)) }
1744 | '(' '->' ')' { LL $ getRdrName funTyCon }
1745 | '[' ']' { LL $ listTyCon_RDR }
1746 | '[:' ':]' { LL $ parrTyCon_RDR }
1748 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1750 | '(' qtyconsym ')' { LL (unLoc $2) }
1752 qtyconop :: { Located RdrName } -- Qualified or unqualified
1754 | '`' qtycon '`' { LL (unLoc $2) }
1756 qtycon :: { Located RdrName } -- Qualified or unqualified
1757 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1758 | PREFIXQCONSYM { L1 $! mkQual tcClsName (getPREFIXQCONSYM $1) }
1761 tycon :: { Located RdrName } -- Unqualified
1762 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1764 qtyconsym :: { Located RdrName }
1765 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1768 tyconsym :: { Located RdrName }
1769 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1771 -----------------------------------------------------------------------------
1774 op :: { Located RdrName } -- used in infix decls
1778 varop :: { Located RdrName }
1780 | '`' varid '`' { LL (unLoc $2) }
1782 qop :: { LHsExpr RdrName } -- used in sections
1783 : qvarop { L1 $ HsVar (unLoc $1) }
1784 | qconop { L1 $ HsVar (unLoc $1) }
1786 qopm :: { LHsExpr RdrName } -- used in sections
1787 : qvaropm { L1 $ HsVar (unLoc $1) }
1788 | qconop { L1 $ HsVar (unLoc $1) }
1790 qvarop :: { Located RdrName }
1792 | '`' qvarid '`' { LL (unLoc $2) }
1794 qvaropm :: { Located RdrName }
1795 : qvarsym_no_minus { $1 }
1796 | '`' qvarid '`' { LL (unLoc $2) }
1798 -----------------------------------------------------------------------------
1801 tyvar :: { Located RdrName }
1802 tyvar : tyvarid { $1 }
1803 | '(' tyvarsym ')' { LL (unLoc $2) }
1805 tyvarop :: { Located RdrName }
1806 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1808 | '.' {% parseErrorSDoc (getLoc $1)
1809 (vcat [ptext (sLit "Illegal symbol '.' in type"),
1810 ptext (sLit "Perhaps you intended -XRankNTypes or similar flag"),
1811 ptext (sLit "to enable explicit-forall syntax: forall <tvs>. <type>")])
1814 tyvarid :: { Located RdrName }
1815 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1816 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1817 | 'unsafe' { L1 $! mkUnqual tvName (fsLit "unsafe") }
1818 | 'safe' { L1 $! mkUnqual tvName (fsLit "safe") }
1819 | 'interruptible' { L1 $! mkUnqual tvName (fsLit "interruptible") }
1820 | 'threadsafe' { L1 $! mkUnqual tvName (fsLit "threadsafe") }
1822 tyvarsym :: { Located RdrName }
1823 -- Does not include "!", because that is used for strictness marks
1824 -- or ".", because that separates the quantified type vars from the rest
1825 -- or "*", because that's used for kinds
1826 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1828 -----------------------------------------------------------------------------
1831 var :: { Located RdrName }
1833 | '(' varsym ')' { LL (unLoc $2) }
1835 qvar :: { Located RdrName }
1837 | '(' varsym ')' { LL (unLoc $2) }
1838 | '(' qvarsym1 ')' { LL (unLoc $2) }
1839 -- We've inlined qvarsym here so that the decision about
1840 -- whether it's a qvar or a var can be postponed until
1841 -- *after* we see the close paren.
1843 qvarid :: { Located RdrName }
1845 | QVARID { L1 $! mkQual varName (getQVARID $1) }
1846 | PREFIXQVARSYM { L1 $! mkQual varName (getPREFIXQVARSYM $1) }
1848 varid :: { Located RdrName }
1849 : VARID {% do { depth <- getParserBrakDepth ; return (L1 $! mkUnqual (varNameDepth depth) (getVARID $1)) } }
1850 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1851 | 'unsafe' { L1 $! mkUnqual varName (fsLit "unsafe") }
1852 | 'safe' { L1 $! mkUnqual varName (fsLit "safe") }
1853 | 'interruptible' { L1 $! mkUnqual varName (fsLit "interruptible") }
1854 | 'threadsafe' { L1 $! mkUnqual varName (fsLit "threadsafe") }
1855 | 'forall' { L1 $! mkUnqual varName (fsLit "forall") }
1856 | 'family' { L1 $! mkUnqual varName (fsLit "family") }
1858 qvarsym :: { Located RdrName }
1862 qvarsym_no_minus :: { Located RdrName }
1863 : varsym_no_minus { $1 }
1866 qvarsym1 :: { Located RdrName }
1867 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1869 varsym :: { Located RdrName }
1870 : varsym_no_minus { $1 }
1871 | '-' { L1 $ mkUnqual varName (fsLit "-") }
1873 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1874 : VARSYM {% do { depth <- getParserBrakDepth
1875 ; return (L1 $! mkUnqual (varNameDepth depth) (getVARSYM $1)) } }
1876 | special_sym {% do { depth <- getParserBrakDepth
1877 ; return (L1 $! mkUnqual (varNameDepth depth) (unLoc $1)) } }
1879 -- These special_ids are treated as keywords in various places,
1880 -- but as ordinary ids elsewhere. 'special_id' collects all these
1881 -- except 'unsafe', 'interruptible', 'forall', and 'family' whose treatment differs
1882 -- depending on context
1883 special_id :: { Located FastString }
1885 : 'as' { L1 (fsLit "as") }
1886 | 'qualified' { L1 (fsLit "qualified") }
1887 | 'hiding' { L1 (fsLit "hiding") }
1888 | 'export' { L1 (fsLit "export") }
1889 | 'label' { L1 (fsLit "label") }
1890 | 'dynamic' { L1 (fsLit "dynamic") }
1891 | 'stdcall' { L1 (fsLit "stdcall") }
1892 | 'ccall' { L1 (fsLit "ccall") }
1893 | 'prim' { L1 (fsLit "prim") }
1894 | 'group' { L1 (fsLit "group") }
1896 special_sym :: { Located FastString }
1897 special_sym : '!' { L1 (fsLit "!") }
1898 | '.' { L1 (fsLit ".") }
1899 | '*' { L1 (fsLit "*") }
1901 -----------------------------------------------------------------------------
1902 -- Data constructors
1904 qconid :: { Located RdrName } -- Qualified or unqualified
1906 | QCONID { L1 $! mkQual dataName (getQCONID $1) }
1907 | PREFIXQCONSYM { L1 $! mkQual dataName (getPREFIXQCONSYM $1) }
1909 conid :: { Located RdrName }
1910 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1912 qconsym :: { Located RdrName } -- Qualified or unqualified
1914 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1916 consym :: { Located RdrName }
1917 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1919 -- ':' means only list cons
1920 | ':' { L1 $ consDataCon_RDR }
1923 -----------------------------------------------------------------------------
1926 literal :: { Located HsLit }
1927 : CHAR { L1 $ HsChar $ getCHAR $1 }
1928 | STRING { L1 $ HsString $ getSTRING $1 }
1929 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1930 | PRIMWORD { L1 $ HsWordPrim $ getPRIMWORD $1 }
1931 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1932 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1933 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1934 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1936 -----------------------------------------------------------------------------
1940 : vccurly { () } -- context popped in lexer.
1941 | error {% popContext }
1943 -----------------------------------------------------------------------------
1944 -- Miscellaneous (mostly renamings)
1946 modid :: { Located ModuleName }
1947 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1948 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1951 (unpackFS mod ++ '.':unpackFS c))
1955 : commas ',' { $1 + 1 }
1958 -----------------------------------------------------------------------------
1959 -- Documentation comments
1961 docnext :: { LHsDocString }
1962 : DOCNEXT {% return (L1 (HsDocString (mkFastString (getDOCNEXT $1)))) }
1964 docprev :: { LHsDocString }
1965 : DOCPREV {% return (L1 (HsDocString (mkFastString (getDOCPREV $1)))) }
1967 docnamed :: { Located (String, HsDocString) }
1969 let string = getDOCNAMED $1
1970 (name, rest) = break isSpace string
1971 in return (L1 (name, HsDocString (mkFastString rest))) }
1973 docsection :: { Located (Int, HsDocString) }
1974 : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1975 return (L1 (n, HsDocString (mkFastString doc))) }
1977 moduleheader :: { Maybe LHsDocString }
1978 : DOCNEXT {% let string = getDOCNEXT $1 in
1979 return (Just (L1 (HsDocString (mkFastString string)))) }
1981 maybe_docprev :: { Maybe LHsDocString }
1982 : docprev { Just $1 }
1983 | {- empty -} { Nothing }
1985 maybe_docnext :: { Maybe LHsDocString }
1986 : docnext { Just $1 }
1987 | {- empty -} { Nothing }
1991 happyError = srcParseFail
1993 getVARID (L _ (ITvarid x)) = x
1994 getCONID (L _ (ITconid x)) = x
1995 getVARSYM (L _ (ITvarsym x)) = x
1996 getCONSYM (L _ (ITconsym x)) = x
1997 getQVARID (L _ (ITqvarid x)) = x
1998 getQCONID (L _ (ITqconid x)) = x
1999 getQVARSYM (L _ (ITqvarsym x)) = x
2000 getQCONSYM (L _ (ITqconsym x)) = x
2001 getPREFIXQVARSYM (L _ (ITprefixqvarsym x)) = x
2002 getPREFIXQCONSYM (L _ (ITprefixqconsym x)) = x
2003 getIPDUPVARID (L _ (ITdupipvarid x)) = x
2004 getCHAR (L _ (ITchar x)) = x
2005 getSTRING (L _ (ITstring x)) = x
2006 getINTEGER (L _ (ITinteger x)) = x
2007 getRATIONAL (L _ (ITrational x)) = x
2008 getPRIMCHAR (L _ (ITprimchar x)) = x
2009 getPRIMSTRING (L _ (ITprimstring x)) = x
2010 getPRIMINTEGER (L _ (ITprimint x)) = x
2011 getPRIMWORD (L _ (ITprimword x)) = x
2012 getPRIMFLOAT (L _ (ITprimfloat x)) = x
2013 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
2014 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
2015 getINLINE (L _ (ITinline_prag inl conl)) = (inl,conl)
2016 getSPEC_INLINE (L _ (ITspec_inline_prag True)) = (Inline, FunLike)
2017 getSPEC_INLINE (L _ (ITspec_inline_prag False)) = (NoInline,FunLike)
2019 getDOCNEXT (L _ (ITdocCommentNext x)) = x
2020 getDOCPREV (L _ (ITdocCommentPrev x)) = x
2021 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
2022 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
2024 getSCC :: Located Token -> P FastString
2025 getSCC lt = do let s = getSTRING lt
2026 err = "Spaces are not allowed in SCCs"
2027 -- We probably actually want to be more restrictive than this
2028 if ' ' `elem` unpackFS s
2029 then failSpanMsgP (getLoc lt) (text err)
2032 -- Utilities for combining source spans
2033 comb2 :: Located a -> Located b -> SrcSpan
2034 comb2 a b = a `seq` b `seq` combineLocs a b
2036 comb3 :: Located a -> Located b -> Located c -> SrcSpan
2037 comb3 a b c = a `seq` b `seq` c `seq`
2038 combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
2040 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
2041 comb4 a b c d = a `seq` b `seq` c `seq` d `seq`
2042 (combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
2043 combineSrcSpans (getLoc c) (getLoc d))
2045 -- strict constructor version:
2047 sL :: SrcSpan -> a -> Located a
2048 sL span a = span `seq` a `seq` L span a
2050 -- Make a source location for the file. We're a bit lazy here and just
2051 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
2052 -- try to find the span of the whole file (ToDo).
2053 fileSrcSpan :: P SrcSpan
2056 let loc = mkSrcLoc (srcLocFile l) 1 1;
2057 return (mkSrcSpan loc loc)