2 -- ---------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1997-2003
7 -- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -- ---------------------------------------------------------------------------
12 -- The above warning supression flag is a temporary kludge.
13 -- While working on this module you are encouraged to remove it and fix
14 -- any warnings in the module. See
15 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
18 module Parser ( parseModule, parseStmt, parseIdentifier, parseType,
21 #define INCLUDE #include
22 INCLUDE "HsVersions.h"
26 import HscTypes ( IsBootInterface, DeprecTxt )
29 import TysWiredIn ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
30 listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
31 import Type ( funTyCon )
32 import ForeignCall ( Safety(..), CExportSpec(..), CLabelString,
33 CCallConv(..), CCallTarget(..), defaultCCallConv
35 import OccName ( varName, dataName, tcClsName, tvName )
36 import DataCon ( DataCon, dataConName )
37 import SrcLoc ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
38 SrcSpan, combineLocs, srcLocFile,
41 import StaticFlags ( opt_SccProfilingOn, opt_Hpc )
42 import Type ( Kind, mkArrowKind, liftedTypeKind, unliftedTypeKind )
43 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
44 Activation(..), defaultInlineSpec )
48 import {-# SOURCE #-} HaddockLex hiding ( Token )
52 import Maybes ( orElse )
55 import Control.Monad ( unless )
58 import Control.Monad ( mplus )
62 -----------------------------------------------------------------------------
65 Conflicts: 33 shift/reduce
68 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
69 would think the two should never occur in the same context.
73 -----------------------------------------------------------------------------
76 Conflicts: 34 shift/reduce
79 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
80 would think the two should never occur in the same context.
84 -----------------------------------------------------------------------------
87 Conflicts: 32 shift/reduce
90 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
91 would think the two should never occur in the same context.
95 -----------------------------------------------------------------------------
98 Conflicts: 37 shift/reduce
101 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
102 would think the two should never occur in the same context.
106 -----------------------------------------------------------------------------
107 Conflicts: 38 shift/reduce (1.25)
109 10 for abiguity in 'if x then y else z + 1' [State 178]
110 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
111 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
113 1 for ambiguity in 'if x then y else z :: T' [State 178]
114 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
116 4 for ambiguity in 'if x then y else z -< e' [State 178]
117 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
118 There are four such operators: -<, >-, -<<, >>-
121 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
122 Which of these two is intended?
124 (x::T) -> T -- Rhs is T
127 (x::T -> T) -> .. -- Rhs is ...
129 10 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
132 As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
133 Same duplication between states 11 and 253 as the previous case
135 1 for ambiguity in 'let ?x ...' [State 329]
136 the parser can't tell whether the ?x is the lhs of a normal binding or
137 an implicit binding. Fortunately resolving as shift gives it the only
138 sensible meaning, namely the lhs of an implicit binding.
140 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
141 we don't know whether the '[' starts the activation or not: it
142 might be the start of the declaration with the activation being
143 empty. --SDM 1/4/2002
145 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
146 since 'forall' is a valid variable name, we don't know whether
147 to treat a forall on the input as the beginning of a quantifier
148 or the beginning of the rule itself. Resolving to shift means
149 it's always treated as a quantifier, hence the above is disallowed.
150 This saves explicitly defining a grammar for the rule lhs that
151 doesn't include 'forall'.
153 1 for ambiguity when the source file starts with "-- | doc". We need another
154 token of lookahead to determine if a top declaration or the 'module' keyword
155 follows. Shift parses as if the 'module' keyword follows.
157 -- ---------------------------------------------------------------------------
158 -- Adding location info
160 This is done in a stylised way using the three macros below, L0, L1
161 and LL. Each of these macros can be thought of as having type
163 L0, L1, LL :: a -> Located a
165 They each add a SrcSpan to their argument.
167 L0 adds 'noSrcSpan', used for empty productions
168 -- This doesn't seem to work anymore -=chak
170 L1 for a production with a single token on the lhs. Grabs the SrcSpan
173 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
174 the first and last tokens.
176 These suffice for the majority of cases. However, we must be
177 especially careful with empty productions: LL won't work if the first
178 or last token on the lhs can represent an empty span. In these cases,
179 we have to calculate the span using more of the tokens from the lhs, eg.
181 | 'newtype' tycl_hdr '=' newconstr deriving
183 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
185 We provide comb3 and comb4 functions which are useful in such cases.
187 Be careful: there's no checking that you actually got this right, the
188 only symptom will be that the SrcSpans of your syntax will be
192 * We must expand these macros *before* running Happy, which is why this file is
193 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
195 #define L0 L noSrcSpan
196 #define L1 sL (getLoc $1)
197 #define LL sL (comb2 $1 $>)
199 -- -----------------------------------------------------------------------------
204 '_' { L _ ITunderscore } -- Haskell keywords
206 'case' { L _ ITcase }
207 'class' { L _ ITclass }
208 'data' { L _ ITdata }
209 'default' { L _ ITdefault }
210 'deriving' { L _ ITderiving }
212 'else' { L _ ITelse }
213 'hiding' { L _ IThiding }
215 'import' { L _ ITimport }
217 'infix' { L _ ITinfix }
218 'infixl' { L _ ITinfixl }
219 'infixr' { L _ ITinfixr }
220 'instance' { L _ ITinstance }
222 'module' { L _ ITmodule }
223 'newtype' { L _ ITnewtype }
225 'qualified' { L _ ITqualified }
226 'then' { L _ ITthen }
227 'type' { L _ ITtype }
228 'where' { L _ ITwhere }
229 '_scc_' { L _ ITscc } -- ToDo: remove
231 'forall' { L _ ITforall } -- GHC extension keywords
232 'foreign' { L _ ITforeign }
233 'export' { L _ ITexport }
234 'label' { L _ ITlabel }
235 'dynamic' { L _ ITdynamic }
236 'safe' { L _ ITsafe }
237 'threadsafe' { L _ ITthreadsafe }
238 'unsafe' { L _ ITunsafe }
240 'family' { L _ ITfamily }
241 'stdcall' { L _ ITstdcallconv }
242 'ccall' { L _ ITccallconv }
243 'dotnet' { L _ ITdotnet }
244 'proc' { L _ ITproc } -- for arrow notation extension
245 'rec' { L _ ITrec } -- for arrow notation extension
247 '{-# INLINE' { L _ (ITinline_prag _) }
248 '{-# SPECIALISE' { L _ ITspec_prag }
249 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
250 '{-# SOURCE' { L _ ITsource_prag }
251 '{-# RULES' { L _ ITrules_prag }
252 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
253 '{-# SCC' { L _ ITscc_prag }
254 '{-# GENERATED' { L _ ITgenerated_prag }
255 '{-# DEPRECATED' { L _ ITdeprecated_prag }
256 '{-# UNPACK' { L _ ITunpack_prag }
257 '#-}' { L _ ITclose_prag }
259 '..' { L _ ITdotdot } -- reserved symbols
261 '::' { L _ ITdcolon }
265 '<-' { L _ ITlarrow }
266 '->' { L _ ITrarrow }
269 '=>' { L _ ITdarrow }
273 '-<' { L _ ITlarrowtail } -- for arrow notation
274 '>-' { L _ ITrarrowtail } -- for arrow notation
275 '-<<' { L _ ITLarrowtail } -- for arrow notation
276 '>>-' { L _ ITRarrowtail } -- for arrow notation
279 '{' { L _ ITocurly } -- special symbols
281 '{|' { L _ ITocurlybar }
282 '|}' { L _ ITccurlybar }
283 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
284 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
287 '[:' { L _ ITopabrack }
288 ':]' { L _ ITcpabrack }
291 '(#' { L _ IToubxparen }
292 '#)' { L _ ITcubxparen }
293 '(|' { L _ IToparenbar }
294 '|)' { L _ ITcparenbar }
297 '`' { L _ ITbackquote }
299 VARID { L _ (ITvarid _) } -- identifiers
300 CONID { L _ (ITconid _) }
301 VARSYM { L _ (ITvarsym _) }
302 CONSYM { L _ (ITconsym _) }
303 QVARID { L _ (ITqvarid _) }
304 QCONID { L _ (ITqconid _) }
305 QVARSYM { L _ (ITqvarsym _) }
306 QCONSYM { L _ (ITqconsym _) }
308 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
310 CHAR { L _ (ITchar _) }
311 STRING { L _ (ITstring _) }
312 INTEGER { L _ (ITinteger _) }
313 RATIONAL { L _ (ITrational _) }
315 PRIMCHAR { L _ (ITprimchar _) }
316 PRIMSTRING { L _ (ITprimstring _) }
317 PRIMINTEGER { L _ (ITprimint _) }
318 PRIMFLOAT { L _ (ITprimfloat _) }
319 PRIMDOUBLE { L _ (ITprimdouble _) }
321 DOCNEXT { L _ (ITdocCommentNext _) }
322 DOCPREV { L _ (ITdocCommentPrev _) }
323 DOCNAMED { L _ (ITdocCommentNamed _) }
324 DOCSECTION { L _ (ITdocSection _ _) }
327 '[|' { L _ ITopenExpQuote }
328 '[p|' { L _ ITopenPatQuote }
329 '[t|' { L _ ITopenTypQuote }
330 '[d|' { L _ ITopenDecQuote }
331 '|]' { L _ ITcloseQuote }
332 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
333 '$(' { L _ ITparenEscape } -- $( exp )
334 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
335 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
337 %monad { P } { >>= } { return }
338 %lexer { lexer } { L _ ITeof }
339 %name parseModule module
340 %name parseStmt maybe_stmt
341 %name parseIdentifier identifier
342 %name parseType ctype
343 %partial parseHeader header
344 %tokentype { (Located Token) }
347 -----------------------------------------------------------------------------
348 -- Identifiers; one of the entry points
349 identifier :: { Located RdrName }
354 | '(' '->' ')' { LL $ getRdrName funTyCon }
356 -----------------------------------------------------------------------------
359 -- The place for module deprecation is really too restrictive, but if it
360 -- was allowed at its natural place just before 'module', we get an ugly
361 -- s/r conflict with the second alternative. Another solution would be the
362 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
363 -- either, and DEPRECATED is only expected to be used by people who really
364 -- know what they are doing. :-)
366 module :: { Located (HsModule RdrName) }
367 : maybedocheader 'module' modid maybemoddeprec maybeexports 'where' body
368 {% fileSrcSpan >>= \ loc -> case $1 of { (info, doc) ->
369 return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4
372 {% fileSrcSpan >>= \ loc ->
373 return (L loc (HsModule Nothing Nothing
374 (fst $1) (snd $1) Nothing emptyHaddockModInfo
377 maybedocheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
378 : moduleheader { (fst $1, snd $1) }
379 | {- empty -} { (emptyHaddockModInfo, Nothing) }
381 missing_module_keyword :: { () }
382 : {- empty -} {% pushCurrentContext }
384 maybemoddeprec :: { Maybe DeprecTxt }
385 : '{-# DEPRECATED' STRING '#-}' { Just (getSTRING $2) }
386 | {- empty -} { Nothing }
388 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
390 | vocurly top close { $2 }
392 body2 :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
394 | missing_module_keyword top close { $2 }
396 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
397 : importdecls { (reverse $1,[]) }
398 | importdecls ';' cvtopdecls { (reverse $1,$3) }
399 | cvtopdecls { ([],$1) }
401 cvtopdecls :: { [LHsDecl RdrName] }
402 : topdecls { cvTopDecls $1 }
404 -----------------------------------------------------------------------------
405 -- Module declaration & imports only
407 header :: { Located (HsModule RdrName) }
408 : maybedocheader 'module' modid maybemoddeprec maybeexports 'where' header_body
409 {% fileSrcSpan >>= \ loc -> case $1 of { (info, doc) ->
410 return (L loc (HsModule (Just $3) $5 $7 [] $4
412 | missing_module_keyword importdecls
413 {% fileSrcSpan >>= \ loc ->
414 return (L loc (HsModule Nothing Nothing $2 [] Nothing
415 emptyHaddockModInfo Nothing)) }
417 header_body :: { [LImportDecl RdrName] }
418 : '{' importdecls { $2 }
419 | vocurly importdecls { $2 }
421 -----------------------------------------------------------------------------
424 maybeexports :: { Maybe [LIE RdrName] }
425 : '(' exportlist ')' { Just $2 }
426 | {- empty -} { Nothing }
428 exportlist :: { [LIE RdrName] }
429 : expdoclist ',' expdoclist { $1 ++ $3 }
432 exportlist1 :: { [LIE RdrName] }
433 : expdoclist export expdoclist ',' exportlist { $1 ++ ($2 : $3) ++ $5 }
434 | expdoclist export expdoclist { $1 ++ ($2 : $3) }
437 expdoclist :: { [LIE RdrName] }
438 : exp_doc expdoclist { $1 : $2 }
441 exp_doc :: { LIE RdrName }
442 : docsection { L1 (case (unLoc $1) of (n, doc) -> IEGroup n doc) }
443 | docnamed { L1 (IEDocNamed ((fst . unLoc) $1)) }
444 | docnext { L1 (IEDoc (unLoc $1)) }
446 -- No longer allow things like [] and (,,,) to be exported
447 -- They are built in syntax, always available
448 export :: { LIE RdrName }
449 : qvar { L1 (IEVar (unLoc $1)) }
450 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
451 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
452 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
453 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
454 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
456 qcnames :: { [RdrName] }
457 : qcnames ',' qcname_ext { unLoc $3 : $1 }
458 | qcname_ext { [unLoc $1] }
460 qcname_ext :: { Located RdrName } -- Variable or data constructor
461 -- or tagged type constructor
463 | 'type' qcon { sL (comb2 $1 $2)
464 (setRdrNameSpace (unLoc $2)
467 -- Cannot pull into qcname_ext, as qcname is also used in expression.
468 qcname :: { Located RdrName } -- Variable or data constructor
472 -----------------------------------------------------------------------------
473 -- Import Declarations
475 -- import decls can be *empty*, or even just a string of semicolons
476 -- whereas topdecls must contain at least one topdecl.
478 importdecls :: { [LImportDecl RdrName] }
479 : importdecls ';' importdecl { $3 : $1 }
480 | importdecls ';' { $1 }
481 | importdecl { [ $1 ] }
484 importdecl :: { LImportDecl RdrName }
485 : 'import' maybe_src optqualified modid maybeas maybeimpspec
486 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
488 maybe_src :: { IsBootInterface }
489 : '{-# SOURCE' '#-}' { True }
490 | {- empty -} { False }
492 optqualified :: { Bool }
493 : 'qualified' { True }
494 | {- empty -} { False }
496 maybeas :: { Located (Maybe ModuleName) }
497 : 'as' modid { LL (Just (unLoc $2)) }
498 | {- empty -} { noLoc Nothing }
500 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
501 : impspec { L1 (Just (unLoc $1)) }
502 | {- empty -} { noLoc Nothing }
504 impspec :: { Located (Bool, [LIE RdrName]) }
505 : '(' exportlist ')' { LL (False, $2) }
506 | 'hiding' '(' exportlist ')' { LL (True, $3) }
508 -----------------------------------------------------------------------------
509 -- Fixity Declarations
513 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
515 infix :: { Located FixityDirection }
516 : 'infix' { L1 InfixN }
517 | 'infixl' { L1 InfixL }
518 | 'infixr' { L1 InfixR }
520 ops :: { Located [Located RdrName] }
521 : ops ',' op { LL ($3 : unLoc $1) }
524 -----------------------------------------------------------------------------
525 -- Top-Level Declarations
527 topdecls :: { OrdList (LHsDecl RdrName) }
528 : topdecls ';' topdecl { $1 `appOL` $3 }
529 | topdecls ';' { $1 }
532 topdecl :: { OrdList (LHsDecl RdrName) }
533 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
534 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
535 | 'instance' inst_type where_inst
536 { let (binds, sigs, ats, _) = cvBindsAndSigs (unLoc $3)
538 unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs ats)))}
539 | stand_alone_deriving { unitOL (LL (DerivD (unLoc $1))) }
540 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
541 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
542 | '{-# DEPRECATED' deprecations '#-}' { $2 }
543 | '{-# RULES' rules '#-}' { $2 }
546 -- Template Haskell Extension
547 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
548 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
549 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
554 cl_decl :: { LTyClDecl RdrName }
555 : 'class' tycl_hdr fds where_cls
556 {% do { let { (binds, sigs, ats, docs) =
557 cvBindsAndSigs (unLoc $4)
558 ; (ctxt, tc, tvs, tparms) = unLoc $2}
559 ; checkTyVars tparms -- only type vars allowed
561 ; return $ L (comb4 $1 $2 $3 $4)
562 (mkClassDecl (ctxt, tc, tvs)
563 (unLoc $3) sigs binds ats docs) } }
565 -- Type declarations (toplevel)
567 ty_decl :: { LTyClDecl RdrName }
568 -- ordinary type synonyms
569 : 'type' type '=' ctype
570 -- Note ctype, not sigtype, on the right of '='
571 -- We allow an explicit for-all but we don't insert one
572 -- in type Foo a = (b,b)
573 -- Instead we just say b is out of scope
575 -- Note the use of type for the head; this allows
576 -- infix type constructors to be declared
577 {% do { (tc, tvs, _) <- checkSynHdr $2 False
578 ; return (L (comb2 $1 $4)
579 (TySynonym tc tvs Nothing $4))
582 -- type family declarations
583 | 'type' 'family' type opt_kind_sig
584 -- Note the use of type for the head; this allows
585 -- infix type constructors to be declared
587 {% do { (tc, tvs, _) <- checkSynHdr $3 False
588 ; return (L (comb3 $1 $3 $4)
589 (TyFamily TypeFamily tc tvs (unLoc $4)))
592 -- type instance declarations
593 | 'type' 'instance' type '=' ctype
594 -- Note the use of type for the head; this allows
595 -- infix type constructors and type patterns
597 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
598 ; return (L (comb2 $1 $5)
599 (TySynonym tc tvs (Just typats) $5))
602 -- ordinary data type or newtype declaration
603 | data_or_newtype tycl_hdr constrs deriving
604 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
605 ; checkTyVars tparms -- no type pattern
607 L (comb4 $1 $2 $3 $4)
608 -- We need the location on tycl_hdr in case
609 -- constrs and deriving are both empty
610 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
611 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
613 -- ordinary GADT declaration
614 | data_or_newtype tycl_hdr opt_kind_sig
615 'where' gadt_constrlist
617 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
618 ; checkTyVars tparms -- can have type pats
620 L (comb4 $1 $2 $4 $5)
621 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
622 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
624 -- data/newtype family
625 | 'data' 'family' tycl_hdr opt_kind_sig
626 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
627 ; checkTyVars tparms -- no type pattern
628 ; unless (null (unLoc ctxt)) $ -- and no context
629 parseError (getLoc ctxt)
630 "A family declaration cannot have a context"
633 (TyFamily DataFamily tc tvs (unLoc $4)) } }
635 -- data/newtype instance declaration
636 | data_or_newtype 'instance' tycl_hdr constrs deriving
637 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
638 -- can have type pats
640 L (comb4 $1 $3 $4 $5)
641 -- We need the location on tycl_hdr in case
642 -- constrs and deriving are both empty
643 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
644 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
646 -- GADT instance declaration
647 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
648 'where' gadt_constrlist
650 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
651 -- can have type pats
653 L (comb4 $1 $3 $6 $7)
654 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
655 (unLoc $4) (reverse (unLoc $6)) (unLoc $7)) } }
657 -- Associate type family declarations
659 -- * They have a different syntax than on the toplevel (no family special
662 -- * They also need to be separate from instances; otherwise, data family
663 -- declarations without a kind signature cause parsing conflicts with empty
664 -- data declarations.
666 at_decl_cls :: { LTyClDecl RdrName }
667 -- type family declarations
668 : 'type' type opt_kind_sig
669 -- Note the use of type for the head; this allows
670 -- infix type constructors to be declared
672 {% do { (tc, tvs, _) <- checkSynHdr $2 False
673 ; return (L (comb3 $1 $2 $3)
674 (TyFamily TypeFamily tc tvs (unLoc $3)))
677 -- default type instance
678 | 'type' type '=' ctype
679 -- Note the use of type for the head; this allows
680 -- infix type constructors and type patterns
682 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
683 ; return (L (comb2 $1 $4)
684 (TySynonym tc tvs (Just typats) $4))
687 -- data/newtype family declaration
688 | 'data' tycl_hdr opt_kind_sig
689 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
690 ; checkTyVars tparms -- no type pattern
691 ; unless (null (unLoc ctxt)) $ -- and no context
692 parseError (getLoc ctxt)
693 "A family declaration cannot have a context"
696 (TyFamily DataFamily tc tvs (unLoc $3))
699 -- Associate type instances
701 at_decl_inst :: { LTyClDecl RdrName }
702 -- type instance declarations
703 : 'type' type '=' ctype
704 -- Note the use of type for the head; this allows
705 -- infix type constructors and type patterns
707 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
708 ; return (L (comb2 $1 $4)
709 (TySynonym tc tvs (Just typats) $4))
712 -- data/newtype instance declaration
713 | data_or_newtype tycl_hdr constrs deriving
714 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
715 -- can have type pats
717 L (comb4 $1 $2 $3 $4)
718 -- We need the location on tycl_hdr in case
719 -- constrs and deriving are both empty
720 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
721 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
723 -- GADT instance declaration
724 | data_or_newtype tycl_hdr opt_kind_sig
725 'where' gadt_constrlist
727 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
728 -- can have type pats
730 L (comb4 $1 $2 $5 $6)
731 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
732 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
734 data_or_newtype :: { Located NewOrData }
735 : 'data' { L1 DataType }
736 | 'newtype' { L1 NewType }
738 opt_kind_sig :: { Located (Maybe Kind) }
740 | '::' kind { LL (Just (unLoc $2)) }
742 -- tycl_hdr parses the header of a class or data type decl,
743 -- which takes the form
746 -- (Eq a, Ord b) => T a b
747 -- T Int [a] -- for associated types
748 -- Rather a lot of inlining here, else we get reduce/reduce errors
749 tycl_hdr :: { Located (LHsContext RdrName,
751 [LHsTyVarBndr RdrName],
753 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
754 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
756 -----------------------------------------------------------------------------
757 -- Stand-alone deriving
759 -- Glasgow extension: stand-alone deriving declarations
760 stand_alone_deriving :: { LDerivDecl RdrName }
761 : 'deriving' 'instance' inst_type {% checkDerivDecl (LL (DerivDecl $3)) }
763 -----------------------------------------------------------------------------
764 -- Nested declarations
766 -- Declaration in class bodies
768 decl_cls :: { Located (OrdList (LHsDecl RdrName)) }
769 decl_cls : at_decl_cls { LL (unitOL (L1 (TyClD (unLoc $1)))) }
772 decls_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
773 : decls_cls ';' decl_cls { LL (unLoc $1 `appOL` unLoc $3) }
774 | decls_cls ';' { LL (unLoc $1) }
776 | {- empty -} { noLoc nilOL }
780 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
781 : '{' decls_cls '}' { LL (unLoc $2) }
782 | vocurly decls_cls close { $2 }
786 where_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
787 -- No implicit parameters
788 -- May have type declarations
789 : 'where' decllist_cls { LL (unLoc $2) }
790 | {- empty -} { noLoc nilOL }
792 -- Declarations in instance bodies
794 decl_inst :: { Located (OrdList (LHsDecl RdrName)) }
795 decl_inst : at_decl_inst { LL (unitOL (L1 (TyClD (unLoc $1)))) }
798 decls_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
799 : decls_inst ';' decl_inst { LL (unLoc $1 `appOL` unLoc $3) }
800 | decls_inst ';' { LL (unLoc $1) }
802 | {- empty -} { noLoc nilOL }
805 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
806 : '{' decls_inst '}' { LL (unLoc $2) }
807 | vocurly decls_inst close { $2 }
811 where_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
812 -- No implicit parameters
813 -- May have type declarations
814 : 'where' decllist_inst { LL (unLoc $2) }
815 | {- empty -} { noLoc nilOL }
817 -- Declarations in binding groups other than classes and instances
819 decls :: { Located (OrdList (LHsDecl RdrName)) }
820 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
821 | decls ';' { LL (unLoc $1) }
823 | {- empty -} { noLoc nilOL }
825 decllist :: { Located (OrdList (LHsDecl RdrName)) }
826 : '{' decls '}' { LL (unLoc $2) }
827 | vocurly decls close { $2 }
829 -- Binding groups other than those of class and instance declarations
831 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
832 -- No type declarations
833 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
834 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
835 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
837 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
838 -- No type declarations
839 : 'where' binds { LL (unLoc $2) }
840 | {- empty -} { noLoc emptyLocalBinds }
843 -----------------------------------------------------------------------------
844 -- Transformation Rules
846 rules :: { OrdList (LHsDecl RdrName) }
847 : rules ';' rule { $1 `snocOL` $3 }
850 | {- empty -} { nilOL }
852 rule :: { LHsDecl RdrName }
853 : STRING activation rule_forall infixexp '=' exp
854 { LL $ RuleD (HsRule (getSTRING $1)
855 ($2 `orElse` AlwaysActive)
856 $3 $4 placeHolderNames $6 placeHolderNames) }
858 activation :: { Maybe Activation }
859 : {- empty -} { Nothing }
860 | explicit_activation { Just $1 }
862 explicit_activation :: { Activation } -- In brackets
863 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
864 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
866 rule_forall :: { [RuleBndr RdrName] }
867 : 'forall' rule_var_list '.' { $2 }
870 rule_var_list :: { [RuleBndr RdrName] }
872 | rule_var rule_var_list { $1 : $2 }
874 rule_var :: { RuleBndr RdrName }
875 : varid { RuleBndr $1 }
876 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
878 -----------------------------------------------------------------------------
879 -- Deprecations (c.f. rules)
881 deprecations :: { OrdList (LHsDecl RdrName) }
882 : deprecations ';' deprecation { $1 `appOL` $3 }
883 | deprecations ';' { $1 }
885 | {- empty -} { nilOL }
887 -- SUP: TEMPORARY HACK, not checking for `module Foo'
888 deprecation :: { OrdList (LHsDecl RdrName) }
890 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
894 -----------------------------------------------------------------------------
895 -- Foreign import and export declarations
897 fdecl :: { LHsDecl RdrName }
898 fdecl : 'import' callconv safety fspec
899 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
900 | 'import' callconv fspec
901 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
903 | 'export' callconv fspec
904 {% mkExport $2 (unLoc $3) >>= return.LL }
906 callconv :: { CallConv }
907 : 'stdcall' { CCall StdCallConv }
908 | 'ccall' { CCall CCallConv }
909 | 'dotnet' { DNCall }
912 : 'unsafe' { PlayRisky }
913 | 'safe' { PlaySafe False }
914 | 'threadsafe' { PlaySafe True }
916 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
917 : STRING var '::' sigtypedoc { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
918 | var '::' sigtypedoc { LL (noLoc nilFS, $1, $3) }
919 -- if the entity string is missing, it defaults to the empty string;
920 -- the meaning of an empty entity string depends on the calling
923 -----------------------------------------------------------------------------
926 opt_sig :: { Maybe (LHsType RdrName) }
927 : {- empty -} { Nothing }
928 | '::' sigtype { Just $2 }
930 opt_asig :: { Maybe (LHsType RdrName) }
931 : {- empty -} { Nothing }
932 | '::' atype { Just $2 }
934 sigtypes1 :: { [LHsType RdrName] }
936 | sigtype ',' sigtypes1 { $1 : $3 }
938 sigtype :: { LHsType RdrName }
939 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
940 -- Wrap an Implicit forall if there isn't one there already
942 sigtypedoc :: { LHsType RdrName }
943 : ctypedoc { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
944 -- Wrap an Implicit forall if there isn't one there already
946 sig_vars :: { Located [Located RdrName] }
947 : sig_vars ',' var { LL ($3 : unLoc $1) }
950 -----------------------------------------------------------------------------
953 infixtype :: { LHsType RdrName }
954 : btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
955 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
957 infixtypedoc :: { LHsType RdrName }
959 | infixtype docprev { LL $ HsDocTy $1 $2 }
961 gentypedoc :: { LHsType RdrName }
964 | infixtypedoc { $1 }
965 | btype '->' ctypedoc { LL $ HsFunTy $1 $3 }
966 | btypedoc '->' ctypedoc { LL $ HsFunTy $1 $3 }
968 ctypedoc :: { LHsType RdrName }
969 : 'forall' tv_bndrs '.' ctypedoc { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
970 | context '=>' ctypedoc { LL $ mkImplicitHsForAllTy $1 $3 }
971 -- A type of form (context => type) is an *implicit* HsForAllTy
974 strict_mark :: { Located HsBang }
975 : '!' { L1 HsStrict }
976 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
978 -- A ctype is a for-all type
979 ctype :: { LHsType RdrName }
980 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
981 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
982 -- A type of form (context => type) is an *implicit* HsForAllTy
985 -- We parse a context as a btype so that we don't get reduce/reduce
986 -- errors in ctype. The basic problem is that
988 -- looks so much like a tuple type. We can't tell until we find the =>
990 -- We have the t1 ~ t2 form here and in gentype, to permit an individual
991 -- equational constraint without parenthesis.
992 context :: { LHsContext RdrName }
993 : btype '~' btype {% checkContext
994 (LL $ HsPredTy (HsEqualP $1 $3)) }
995 | btype {% checkContext $1 }
997 type :: { LHsType RdrName }
998 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
1001 gentype :: { LHsType RdrName }
1003 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
1004 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
1005 | btype '->' ctype { LL $ HsFunTy $1 $3 }
1006 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1008 btype :: { LHsType RdrName }
1009 : btype atype { LL $ HsAppTy $1 $2 }
1012 btypedoc :: { LHsType RdrName }
1013 : btype atype docprev { LL $ HsDocTy (L (comb2 $1 $2) (HsAppTy $1 $2)) $3 }
1014 | atype docprev { LL $ HsDocTy $1 $2 }
1016 atype :: { LHsType RdrName }
1017 : gtycon { L1 (HsTyVar (unLoc $1)) }
1018 | tyvar { L1 (HsTyVar (unLoc $1)) }
1019 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
1020 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
1021 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
1022 | '[' ctype ']' { LL $ HsListTy $2 }
1023 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
1024 | '(' ctype ')' { LL $ HsParTy $2 }
1025 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
1027 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
1029 -- An inst_type is what occurs in the head of an instance decl
1030 -- e.g. (Foo a, Gaz b) => Wibble a b
1031 -- It's kept as a single type, with a MonoDictTy at the right
1032 -- hand corner, for convenience.
1033 inst_type :: { LHsType RdrName }
1034 : sigtype {% checkInstType $1 }
1036 inst_types1 :: { [LHsType RdrName] }
1037 : inst_type { [$1] }
1038 | inst_type ',' inst_types1 { $1 : $3 }
1040 comma_types0 :: { [LHsType RdrName] }
1041 : comma_types1 { $1 }
1042 | {- empty -} { [] }
1044 comma_types1 :: { [LHsType RdrName] }
1046 | ctype ',' comma_types1 { $1 : $3 }
1048 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1049 : tv_bndr tv_bndrs { $1 : $2 }
1050 | {- empty -} { [] }
1052 tv_bndr :: { LHsTyVarBndr RdrName }
1053 : tyvar { L1 (UserTyVar (unLoc $1)) }
1054 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
1057 fds :: { Located [Located ([RdrName], [RdrName])] }
1058 : {- empty -} { noLoc [] }
1059 | '|' fds1 { LL (reverse (unLoc $2)) }
1061 fds1 :: { Located [Located ([RdrName], [RdrName])] }
1062 : fds1 ',' fd { LL ($3 : unLoc $1) }
1065 fd :: { Located ([RdrName], [RdrName]) }
1066 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
1067 (reverse (unLoc $1), reverse (unLoc $3)) }
1069 varids0 :: { Located [RdrName] }
1070 : {- empty -} { noLoc [] }
1071 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
1073 -----------------------------------------------------------------------------
1076 kind :: { Located Kind }
1078 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1080 akind :: { Located Kind }
1081 : '*' { L1 liftedTypeKind }
1082 | '!' { L1 unliftedTypeKind }
1083 | '(' kind ')' { LL (unLoc $2) }
1086 -----------------------------------------------------------------------------
1087 -- Datatype declarations
1089 gadt_constrlist :: { Located [LConDecl RdrName] }
1090 : '{' gadt_constrs '}' { LL (unLoc $2) }
1091 | vocurly gadt_constrs close { $2 }
1093 gadt_constrs :: { Located [LConDecl RdrName] }
1094 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
1095 | gadt_constrs ';' { $1 }
1096 | gadt_constr { L1 [$1] }
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 }
1106 { LL (mkGadtDecl $1 $3) }
1107 -- Syntax: Maybe merge the record stuff with the single-case above?
1108 -- (to kill the mostly harmless reduce/reduce error)
1109 -- XXX revisit audreyt
1110 | constr_stuff_record '::' sigtype
1111 { let (con,details) = unLoc $1 in
1112 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3) Nothing) }
1114 | forall context '=>' constr_stuff_record '::' sigtype
1115 { let (con,details) = unLoc $4 in
1116 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6) Nothing ) }
1117 | forall constr_stuff_record '::' sigtype
1118 { let (con,details) = unLoc $2 in
1119 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4) Nothing) }
1123 constrs :: { Located [LConDecl RdrName] }
1124 : {- empty; a GHC extension -} { noLoc [] }
1125 | maybe_docnext '=' constrs1 { L (comb2 $2 $3) (addConDocs (unLoc $3) $1) }
1127 constrs1 :: { Located [LConDecl RdrName] }
1128 : constrs1 maybe_docnext '|' maybe_docprev constr { LL (addConDoc $5 $2 : addConDocFirst (unLoc $1) $4) }
1129 | constr { L1 [$1] }
1131 constr :: { LConDecl RdrName }
1132 : maybe_docnext forall context '=>' constr_stuff maybe_docprev
1133 { let (con,details) = unLoc $5 in
1134 L (comb4 $2 $3 $4 $5) (ConDecl con Explicit (unLoc $2) $3 details ResTyH98 ($1 `mplus` $6)) }
1135 | maybe_docnext forall constr_stuff maybe_docprev
1136 { let (con,details) = unLoc $3 in
1137 L (comb2 $2 $3) (ConDecl con Explicit (unLoc $2) (noLoc []) details ResTyH98 ($1 `mplus` $4)) }
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 {% mkPrefixCon $1 [] >>= return.LL }
1152 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
1153 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
1154 | btype conop btype { LL ($2, InfixCon $1 $3) }
1156 constr_stuff_record :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1157 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
1158 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
1160 fielddecls :: { [([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName))] }
1161 : fielddecl maybe_docnext ',' maybe_docprev fielddecls { addFieldDoc (unLoc $1) $4 : addFieldDocs $5 $2 }
1162 | fielddecl { [unLoc $1] }
1164 fielddecl :: { Located ([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName)) }
1165 : maybe_docnext sig_vars '::' ctype maybe_docprev { L (comb3 $2 $3 $4) (reverse (unLoc $2), $4, $1 `mplus` $5) }
1167 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1168 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1169 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1170 -- We don't allow a context, but that's sorted out by the type checker.
1171 deriving :: { Located (Maybe [LHsType RdrName]) }
1172 : {- empty -} { noLoc Nothing }
1173 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1174 ; p <- checkInstType (L loc (HsTyVar tv))
1175 ; return (LL (Just [p])) } }
1176 | 'deriving' '(' ')' { LL (Just []) }
1177 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1178 -- Glasgow extension: allow partial
1179 -- applications in derivings
1181 -----------------------------------------------------------------------------
1182 -- Value definitions
1184 {- There's an awkward overlap with a type signature. Consider
1185 f :: Int -> Int = ...rhs...
1186 Then we can't tell whether it's a type signature or a value
1187 definition with a result signature until we see the '='.
1188 So we have to inline enough to postpone reductions until we know.
1192 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1193 instead of qvar, we get another shift/reduce-conflict. Consider the
1196 { (^^) :: Int->Int ; } Type signature; only var allowed
1198 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1199 qvar allowed (because of instance decls)
1201 We can't tell whether to reduce var to qvar until after we've read the signatures.
1204 docdecl :: { LHsDecl RdrName }
1205 : docdecld { L1 (DocD (unLoc $1)) }
1207 docdecld :: { LDocDecl RdrName }
1208 : docnext { L1 (DocCommentNext (unLoc $1)) }
1209 | docprev { L1 (DocCommentPrev (unLoc $1)) }
1210 | docnamed { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1211 | docsection { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1213 decl :: { Located (OrdList (LHsDecl RdrName)) }
1215 | '!' aexp rhs {% do { pat <- checkPattern $2;
1216 return (LL $ unitOL $ LL $ ValD (
1217 PatBind (LL $ BangPat pat) (unLoc $3)
1218 placeHolderType placeHolderNames)) } }
1219 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1220 return (LL $ unitOL (LL $ ValD r)) } }
1221 | docdecl { LL $ unitOL $1 }
1223 rhs :: { Located (GRHSs RdrName) }
1224 : '=' exp wherebinds { L (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 : '|' quals '=' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1234 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1235 : infixexp '::' sigtypedoc
1236 {% do s <- checkValSig $1 $3;
1237 return (LL $ unitOL (LL $ SigD s)) }
1238 -- See the above notes for why we need infixexp here
1239 | var ',' sig_vars '::' sigtypedoc
1240 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1241 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1243 | '{-# INLINE' activation qvar '#-}'
1244 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1245 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1246 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1248 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1249 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1251 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1252 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1254 -----------------------------------------------------------------------------
1257 exp :: { LHsExpr RdrName }
1258 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1259 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1260 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1261 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1262 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1265 infixexp :: { LHsExpr RdrName }
1267 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1269 exp10 :: { LHsExpr RdrName }
1270 : '\\' apat apats opt_asig '->' exp
1271 { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1274 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1275 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1276 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1277 | '-' fexp { LL $ NegApp $2 noSyntaxExpr }
1279 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1280 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1281 return (L loc (mkHsDo DoExpr stmts body)) }
1282 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1283 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1284 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1285 | scc_annot exp { LL $ if opt_SccProfilingOn
1286 then HsSCC (unLoc $1) $2
1288 | hpc_annot exp { LL $ if opt_Hpc
1289 then HsTickPragma (unLoc $1) $2
1292 | 'proc' aexp '->' exp
1293 {% checkPattern $2 >>= \ p ->
1294 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1295 placeHolderType undefined)) }
1296 -- TODO: is LL right here?
1298 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1299 -- hdaume: core annotation
1302 scc_annot :: { Located FastString }
1303 : '_scc_' STRING {% (addWarning Opt_WarnDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1304 (return $ LL $ getSTRING $2) }
1305 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
1307 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1308 : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1309 { LL $ (getSTRING $2
1310 ,( fromInteger $ getINTEGER $3
1311 , fromInteger $ getINTEGER $5
1313 ,( fromInteger $ getINTEGER $7
1314 , fromInteger $ getINTEGER $9
1319 fexp :: { LHsExpr RdrName }
1320 : fexp aexp { LL $ HsApp $1 $2 }
1323 aexp :: { LHsExpr RdrName }
1324 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1325 | '~' aexp { LL $ ELazyPat $2 }
1328 aexp1 :: { LHsExpr RdrName }
1329 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1333 -- Here was the syntax for type applications that I was planning
1334 -- but there are difficulties (e.g. what order for type args)
1335 -- so it's not enabled yet.
1336 -- But this case *is* used for the left hand side of a generic definition,
1337 -- which is parsed as an expression before being munged into a pattern
1338 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1339 (sL (getLoc $3) (HsType $3)) }
1341 aexp2 :: { LHsExpr RdrName }
1342 : ipvar { L1 (HsIPVar $! unLoc $1) }
1343 | qcname { L1 (HsVar $! unLoc $1) }
1344 | literal { L1 (HsLit $! unLoc $1) }
1345 -- This will enable overloaded strings permanently. Normally the renamer turns HsString
1346 -- into HsOverLit when -foverloaded-strings is on.
1347 -- | STRING { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1348 | INTEGER { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1349 | RATIONAL { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1350 -- N.B.: sections get parsed by these next two productions.
1351 -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't correct Haskell98
1352 -- (you'd have to write '((+ 3), (4 -))')
1353 -- but the less cluttered version fell out of having texps.
1354 | '(' texp ')' { LL (HsPar $2) }
1355 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1356 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1357 | '[' list ']' { LL (unLoc $2) }
1358 | '[:' parr ':]' { LL (unLoc $2) }
1359 | '_' { L1 EWildPat }
1361 -- Template Haskell Extension
1362 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1363 (L1 $ HsVar (mkUnqual varName
1364 (getTH_ID_SPLICE $1)))) } -- $x
1365 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1367 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1368 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1369 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1370 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1371 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1372 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1373 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1374 return (LL $ HsBracket (PatBr p)) }
1375 | '[d|' cvtopbody '|]' {% checkDecBrGroup $2 >>= \g ->
1376 return (LL $ HsBracket (DecBr g)) }
1378 -- arrow notation extension
1379 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1381 cmdargs :: { [LHsCmdTop RdrName] }
1382 : cmdargs acmd { $2 : $1 }
1383 | {- empty -} { [] }
1385 acmd :: { LHsCmdTop RdrName }
1386 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1388 cvtopbody :: { [LHsDecl RdrName] }
1389 : '{' cvtopdecls0 '}' { $2 }
1390 | vocurly cvtopdecls0 close { $2 }
1392 cvtopdecls0 :: { [LHsDecl RdrName] }
1393 : {- empty -} { [] }
1396 -- tuple expressions: things that can appear unparenthesized as long as they're
1397 -- inside parens or delimitted by commas
1398 texp :: { LHsExpr RdrName }
1400 -- Technically, this should only be used for bang patterns,
1401 -- but we can be a little more liberal here and avoid parens
1403 | infixexp qop { LL $ SectionL $1 $2 }
1404 | qopm infixexp { LL $ SectionR $1 $2 }
1405 -- view patterns get parenthesized above
1406 | exp '->' exp { LL $ EViewPat $1 $3 }
1408 texps :: { [LHsExpr RdrName] }
1409 : texps ',' texp { $3 : $1 }
1413 -----------------------------------------------------------------------------
1416 -- The rules below are little bit contorted to keep lexps left-recursive while
1417 -- avoiding another shift/reduce-conflict.
1419 list :: { LHsExpr RdrName }
1420 : texp { L1 $ ExplicitList placeHolderType [$1] }
1421 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1422 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1423 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1424 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1425 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1426 | texp pquals { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1428 lexps :: { Located [LHsExpr RdrName] }
1429 : lexps ',' texp { LL ($3 : unLoc $1) }
1430 | texp ',' texp { LL [$3,$1] }
1432 -----------------------------------------------------------------------------
1433 -- List Comprehensions
1435 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1436 -- or a reversed list of Stmts
1437 : pquals1 { case unLoc $1 of
1439 qss -> L1 [L1 (ParStmt stmtss)]
1441 stmtss = [ (reverse qs, undefined)
1445 pquals1 :: { Located [[LStmt RdrName]] }
1446 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1447 | '|' quals { L (getLoc $2) [unLoc $2] }
1449 quals :: { Located [LStmt RdrName] }
1450 : quals ',' qual { LL ($3 : unLoc $1) }
1453 -----------------------------------------------------------------------------
1454 -- Parallel array expressions
1456 -- The rules below are little bit contorted; see the list case for details.
1457 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1458 -- Moreover, we allow explicit arrays with no element (represented by the nil
1459 -- constructor in the list case).
1461 parr :: { LHsExpr RdrName }
1462 : { noLoc (ExplicitPArr placeHolderType []) }
1463 | texp { L1 $ ExplicitPArr placeHolderType [$1] }
1464 | lexps { L1 $ ExplicitPArr placeHolderType
1465 (reverse (unLoc $1)) }
1466 | texp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1467 | texp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1468 | texp pquals { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1470 -- We are reusing `lexps' and `pquals' from the list case.
1472 -----------------------------------------------------------------------------
1473 -- Case alternatives
1475 altslist :: { Located [LMatch RdrName] }
1476 : '{' alts '}' { LL (reverse (unLoc $2)) }
1477 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1479 alts :: { Located [LMatch RdrName] }
1480 : alts1 { L1 (unLoc $1) }
1481 | ';' alts { LL (unLoc $2) }
1483 alts1 :: { Located [LMatch RdrName] }
1484 : alts1 ';' alt { LL ($3 : unLoc $1) }
1485 | alts1 ';' { LL (unLoc $1) }
1488 alt :: { LMatch RdrName }
1489 : pat opt_sig alt_rhs { LL (Match [$1] $2 (unLoc $3)) }
1491 alt_rhs :: { Located (GRHSs RdrName) }
1492 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1494 ralt :: { Located [LGRHS RdrName] }
1495 : '->' exp { LL (unguardedRHS $2) }
1496 | gdpats { L1 (reverse (unLoc $1)) }
1498 gdpats :: { Located [LGRHS RdrName] }
1499 : gdpats gdpat { LL ($2 : unLoc $1) }
1502 gdpat :: { LGRHS RdrName }
1503 : '|' quals '->' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1505 -- 'pat' recognises a pattern, including one with a bang at the top
1506 -- e.g. "!x" or "!(x,y)" or "C a b" etc
1507 -- Bangs inside are parsed as infix operator applications, so that
1508 -- we parse them right when bang-patterns are off
1509 pat :: { LPat RdrName }
1510 pat : exp {% checkPattern $1 }
1511 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1513 apat :: { LPat RdrName }
1514 apat : aexp {% checkPattern $1 }
1515 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1517 apats :: { [LPat RdrName] }
1518 : apat apats { $1 : $2 }
1519 | {- empty -} { [] }
1521 -----------------------------------------------------------------------------
1522 -- Statement sequences
1524 stmtlist :: { Located [LStmt RdrName] }
1525 : '{' stmts '}' { LL (unLoc $2) }
1526 | vocurly stmts close { $2 }
1528 -- do { ;; s ; s ; ; s ;; }
1529 -- The last Stmt should be an expression, but that's hard to enforce
1530 -- here, because we need too much lookahead if we see do { e ; }
1531 -- So we use ExprStmts throughout, and switch the last one over
1532 -- in ParseUtils.checkDo instead
1533 stmts :: { Located [LStmt RdrName] }
1534 : stmt stmts_help { LL ($1 : unLoc $2) }
1535 | ';' stmts { LL (unLoc $2) }
1536 | {- empty -} { noLoc [] }
1538 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1539 : ';' stmts { LL (unLoc $2) }
1540 | {- empty -} { noLoc [] }
1542 -- For typing stmts at the GHCi prompt, where
1543 -- the input may consist of just comments.
1544 maybe_stmt :: { Maybe (LStmt RdrName) }
1546 | {- nothing -} { Nothing }
1548 stmt :: { LStmt RdrName }
1550 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1552 qual :: { LStmt RdrName }
1553 : pat '<-' exp { LL $ mkBindStmt $1 $3 }
1554 | exp { L1 $ mkExprStmt $1 }
1555 | 'let' binds { LL $ LetStmt (unLoc $2) }
1557 -----------------------------------------------------------------------------
1558 -- Record Field Update/Construction
1560 fbinds :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1562 | {- empty -} { ([], False) }
1564 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1565 : fbind ',' fbinds1 { case $3 of (flds, dd) -> ($1 : flds, dd) }
1566 | fbind { ([$1], False) }
1567 | '..' { ([], True) }
1569 fbind :: { HsRecField RdrName (LHsExpr RdrName) }
1570 : qvar '=' exp { HsRecField $1 $3 False }
1571 | qvar { HsRecField $1 (L (getLoc $1) (HsVar (unLoc $1))) True }
1572 -- Here's where we say that plain 'x'
1573 -- means exactly 'x = x'. The pun-flag boolean is
1574 -- there so we can still print it right
1576 -----------------------------------------------------------------------------
1577 -- Implicit Parameter Bindings
1579 dbinds :: { Located [LIPBind RdrName] }
1580 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1581 | dbinds ';' { LL (unLoc $1) }
1583 -- | {- empty -} { [] }
1585 dbind :: { LIPBind RdrName }
1586 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1588 ipvar :: { Located (IPName RdrName) }
1589 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1591 -----------------------------------------------------------------------------
1594 depreclist :: { Located [RdrName] }
1595 depreclist : deprec_var { L1 [unLoc $1] }
1596 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1598 deprec_var :: { Located RdrName }
1599 deprec_var : var { $1 }
1602 -----------------------------------------
1603 -- Data constructors
1604 qcon :: { Located RdrName }
1606 | '(' qconsym ')' { LL (unLoc $2) }
1607 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1608 -- The case of '[:' ':]' is part of the production `parr'
1610 con :: { Located RdrName }
1612 | '(' consym ')' { LL (unLoc $2) }
1613 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1615 sysdcon :: { Located DataCon } -- Wired in data constructors
1616 : '(' ')' { LL unitDataCon }
1617 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1618 | '[' ']' { LL nilDataCon }
1620 conop :: { Located RdrName }
1622 | '`' conid '`' { LL (unLoc $2) }
1624 qconop :: { Located RdrName }
1626 | '`' qconid '`' { LL (unLoc $2) }
1628 -----------------------------------------------------------------------------
1629 -- Type constructors
1631 gtycon :: { Located RdrName } -- A "general" qualified tycon
1633 | '(' ')' { LL $ getRdrName unitTyCon }
1634 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1635 | '(' '->' ')' { LL $ getRdrName funTyCon }
1636 | '[' ']' { LL $ listTyCon_RDR }
1637 | '[:' ':]' { LL $ parrTyCon_RDR }
1639 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1641 | '(' qtyconsym ')' { LL (unLoc $2) }
1643 qtyconop :: { Located RdrName } -- Qualified or unqualified
1645 | '`' qtycon '`' { LL (unLoc $2) }
1647 qtycon :: { Located RdrName } -- Qualified or unqualified
1648 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1651 tycon :: { Located RdrName } -- Unqualified
1652 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1654 qtyconsym :: { Located RdrName }
1655 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1658 tyconsym :: { Located RdrName }
1659 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1661 -----------------------------------------------------------------------------
1664 op :: { Located RdrName } -- used in infix decls
1668 varop :: { Located RdrName }
1670 | '`' varid '`' { LL (unLoc $2) }
1672 qop :: { LHsExpr RdrName } -- used in sections
1673 : qvarop { L1 $ HsVar (unLoc $1) }
1674 | qconop { L1 $ HsVar (unLoc $1) }
1676 qopm :: { LHsExpr RdrName } -- used in sections
1677 : qvaropm { L1 $ HsVar (unLoc $1) }
1678 | qconop { L1 $ HsVar (unLoc $1) }
1680 qvarop :: { Located RdrName }
1682 | '`' qvarid '`' { LL (unLoc $2) }
1684 qvaropm :: { Located RdrName }
1685 : qvarsym_no_minus { $1 }
1686 | '`' qvarid '`' { LL (unLoc $2) }
1688 -----------------------------------------------------------------------------
1691 tyvar :: { Located RdrName }
1692 tyvar : tyvarid { $1 }
1693 | '(' tyvarsym ')' { LL (unLoc $2) }
1695 tyvarop :: { Located RdrName }
1696 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1699 tyvarid :: { Located RdrName }
1700 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1701 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1702 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1703 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1704 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1706 tyvarsym :: { Located RdrName }
1707 -- Does not include "!", because that is used for strictness marks
1708 -- or ".", because that separates the quantified type vars from the rest
1709 -- or "*", because that's used for kinds
1710 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1712 -----------------------------------------------------------------------------
1715 var :: { Located RdrName }
1717 | '(' varsym ')' { LL (unLoc $2) }
1719 qvar :: { Located RdrName }
1721 | '(' varsym ')' { LL (unLoc $2) }
1722 | '(' qvarsym1 ')' { LL (unLoc $2) }
1723 -- We've inlined qvarsym here so that the decision about
1724 -- whether it's a qvar or a var can be postponed until
1725 -- *after* we see the close paren.
1727 qvarid :: { Located RdrName }
1729 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1731 varid :: { Located RdrName }
1732 : varid_no_unsafe { $1 }
1733 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1734 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1735 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1737 varid_no_unsafe :: { Located RdrName }
1738 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1739 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1740 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1741 | 'family' { L1 $! mkUnqual varName FSLIT("family") }
1743 qvarsym :: { Located RdrName }
1747 qvarsym_no_minus :: { Located RdrName }
1748 : varsym_no_minus { $1 }
1751 qvarsym1 :: { Located RdrName }
1752 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1754 varsym :: { Located RdrName }
1755 : varsym_no_minus { $1 }
1756 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1758 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1759 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1760 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1763 -- These special_ids are treated as keywords in various places,
1764 -- but as ordinary ids elsewhere. 'special_id' collects all these
1765 -- except 'unsafe', 'forall', and 'family' whose treatment differs
1766 -- depending on context
1767 special_id :: { Located FastString }
1769 : 'as' { L1 FSLIT("as") }
1770 | 'qualified' { L1 FSLIT("qualified") }
1771 | 'hiding' { L1 FSLIT("hiding") }
1772 | 'export' { L1 FSLIT("export") }
1773 | 'label' { L1 FSLIT("label") }
1774 | 'dynamic' { L1 FSLIT("dynamic") }
1775 | 'stdcall' { L1 FSLIT("stdcall") }
1776 | 'ccall' { L1 FSLIT("ccall") }
1778 special_sym :: { Located FastString }
1779 special_sym : '!' { L1 FSLIT("!") }
1780 | '.' { L1 FSLIT(".") }
1781 | '*' { L1 FSLIT("*") }
1783 -----------------------------------------------------------------------------
1784 -- Data constructors
1786 qconid :: { Located RdrName } -- Qualified or unqualified
1788 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1790 conid :: { Located RdrName }
1791 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1793 qconsym :: { Located RdrName } -- Qualified or unqualified
1795 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1797 consym :: { Located RdrName }
1798 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1800 -- ':' means only list cons
1801 | ':' { L1 $ consDataCon_RDR }
1804 -----------------------------------------------------------------------------
1807 literal :: { Located HsLit }
1808 : CHAR { L1 $ HsChar $ getCHAR $1 }
1809 | STRING { L1 $ HsString $ getSTRING $1 }
1810 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1811 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1812 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1813 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1814 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1816 -----------------------------------------------------------------------------
1820 : vccurly { () } -- context popped in lexer.
1821 | error {% popContext }
1823 -----------------------------------------------------------------------------
1824 -- Miscellaneous (mostly renamings)
1826 modid :: { Located ModuleName }
1827 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1828 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1831 (unpackFS mod ++ '.':unpackFS c))
1835 : commas ',' { $1 + 1 }
1838 -----------------------------------------------------------------------------
1839 -- Documentation comments
1841 docnext :: { LHsDoc RdrName }
1842 : DOCNEXT {% case parseHaddockParagraphs (tokenise (getDOCNEXT $1)) of {
1843 Left err -> parseError (getLoc $1) err;
1844 Right doc -> return (L1 doc) } }
1846 docprev :: { LHsDoc RdrName }
1847 : DOCPREV {% case parseHaddockParagraphs (tokenise (getDOCPREV $1)) of {
1848 Left err -> parseError (getLoc $1) err;
1849 Right doc -> return (L1 doc) } }
1851 docnamed :: { Located (String, (HsDoc RdrName)) }
1853 let string = getDOCNAMED $1
1854 (name, rest) = break isSpace string
1855 in case parseHaddockParagraphs (tokenise rest) of {
1856 Left err -> parseError (getLoc $1) err;
1857 Right doc -> return (L1 (name, doc)) } }
1859 docsection :: { Located (n, HsDoc RdrName) }
1860 : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1861 case parseHaddockString (tokenise doc) of {
1862 Left err -> parseError (getLoc $1) err;
1863 Right doc -> return (L1 (n, doc)) } }
1865 moduleheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
1866 : DOCNEXT {% let string = getDOCNEXT $1 in
1867 case parseModuleHeader string of {
1868 Right (str, info) ->
1869 case parseHaddockParagraphs (tokenise str) of {
1870 Left err -> parseError (getLoc $1) err;
1871 Right doc -> return (info, Just doc);
1873 Left err -> parseError (getLoc $1) err
1876 maybe_docprev :: { Maybe (LHsDoc RdrName) }
1877 : docprev { Just $1 }
1878 | {- empty -} { Nothing }
1880 maybe_docnext :: { Maybe (LHsDoc RdrName) }
1881 : docnext { Just $1 }
1882 | {- empty -} { Nothing }
1886 happyError = srcParseFail
1888 getVARID (L _ (ITvarid x)) = x
1889 getCONID (L _ (ITconid x)) = x
1890 getVARSYM (L _ (ITvarsym x)) = x
1891 getCONSYM (L _ (ITconsym x)) = x
1892 getQVARID (L _ (ITqvarid x)) = x
1893 getQCONID (L _ (ITqconid x)) = x
1894 getQVARSYM (L _ (ITqvarsym x)) = x
1895 getQCONSYM (L _ (ITqconsym x)) = x
1896 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1897 getCHAR (L _ (ITchar x)) = x
1898 getSTRING (L _ (ITstring x)) = x
1899 getINTEGER (L _ (ITinteger x)) = x
1900 getRATIONAL (L _ (ITrational x)) = x
1901 getPRIMCHAR (L _ (ITprimchar x)) = x
1902 getPRIMSTRING (L _ (ITprimstring x)) = x
1903 getPRIMINTEGER (L _ (ITprimint x)) = x
1904 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1905 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1906 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1907 getINLINE (L _ (ITinline_prag b)) = b
1908 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1910 getDOCNEXT (L _ (ITdocCommentNext x)) = x
1911 getDOCPREV (L _ (ITdocCommentPrev x)) = x
1912 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
1913 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
1915 -- Utilities for combining source spans
1916 comb2 :: Located a -> Located b -> SrcSpan
1919 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1920 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1922 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1923 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1924 combineSrcSpans (getLoc c) (getLoc d)
1926 -- strict constructor version:
1928 sL :: SrcSpan -> a -> Located a
1929 sL span a = span `seq` L span a
1931 -- Make a source location for the file. We're a bit lazy here and just
1932 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1933 -- try to find the span of the whole file (ToDo).
1934 fileSrcSpan :: P SrcSpan
1937 let loc = mkSrcLoc (srcLocFile l) 1 0;
1938 return (mkSrcSpan loc loc)