2 -- ---------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1997-2003
7 -- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -- ---------------------------------------------------------------------------
11 module Parser ( parseModule, parseStmt, parseIdentifier, parseType,
14 #define INCLUDE #include
15 INCLUDE "HsVersions.h"
19 import HscTypes ( IsBootInterface, DeprecTxt )
22 import TysWiredIn ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
23 listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
24 import Type ( funTyCon )
25 import ForeignCall ( Safety(..), CExportSpec(..), CLabelString,
26 CCallConv(..), CCallTarget(..), defaultCCallConv
28 import OccName ( varName, dataName, tcClsName, tvName )
29 import DataCon ( DataCon, dataConName )
30 import SrcLoc ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
31 SrcSpan, combineLocs, srcLocFile,
34 import StaticFlags ( opt_SccProfilingOn, opt_Hpc )
35 import Type ( Kind, mkArrowKind, liftedTypeKind, unliftedTypeKind )
36 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
37 Activation(..), defaultInlineSpec )
40 import {-# SOURCE #-} HaddockLex hiding ( Token )
44 import Maybes ( orElse )
47 import Control.Monad ( unless )
50 import Control.Monad ( mplus )
54 -----------------------------------------------------------------------------
57 Conflicts: 33 shift/reduce
60 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
61 would think the two should never occur in the same context.
65 -----------------------------------------------------------------------------
68 Conflicts: 34 shift/reduce
71 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
72 would think the two should never occur in the same context.
76 -----------------------------------------------------------------------------
79 Conflicts: 32 shift/reduce
82 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
83 would think the two should never occur in the same context.
87 -----------------------------------------------------------------------------
90 Conflicts: 37 shift/reduce
93 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
94 would think the two should never occur in the same context.
98 -----------------------------------------------------------------------------
99 Conflicts: 38 shift/reduce (1.25)
101 10 for abiguity in 'if x then y else z + 1' [State 178]
102 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
103 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
105 1 for ambiguity in 'if x then y else z :: T' [State 178]
106 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
108 4 for ambiguity in 'if x then y else z -< e' [State 178]
109 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
110 There are four such operators: -<, >-, -<<, >>-
113 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
114 Which of these two is intended?
116 (x::T) -> T -- Rhs is T
119 (x::T -> T) -> .. -- Rhs is ...
121 10 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
124 As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
125 Same duplication between states 11 and 253 as the previous case
127 1 for ambiguity in 'let ?x ...' [State 329]
128 the parser can't tell whether the ?x is the lhs of a normal binding or
129 an implicit binding. Fortunately resolving as shift gives it the only
130 sensible meaning, namely the lhs of an implicit binding.
132 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
133 we don't know whether the '[' starts the activation or not: it
134 might be the start of the declaration with the activation being
135 empty. --SDM 1/4/2002
137 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
138 since 'forall' is a valid variable name, we don't know whether
139 to treat a forall on the input as the beginning of a quantifier
140 or the beginning of the rule itself. Resolving to shift means
141 it's always treated as a quantifier, hence the above is disallowed.
142 This saves explicitly defining a grammar for the rule lhs that
143 doesn't include 'forall'.
145 1 for ambiguity when the source file starts with "-- | doc". We need another
146 token of lookahead to determine if a top declaration or the 'module' keyword
147 follows. Shift parses as if the 'module' keyword follows.
149 -- ---------------------------------------------------------------------------
150 -- Adding location info
152 This is done in a stylised way using the three macros below, L0, L1
153 and LL. Each of these macros can be thought of as having type
155 L0, L1, LL :: a -> Located a
157 They each add a SrcSpan to their argument.
159 L0 adds 'noSrcSpan', used for empty productions
160 -- This doesn't seem to work anymore -=chak
162 L1 for a production with a single token on the lhs. Grabs the SrcSpan
165 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
166 the first and last tokens.
168 These suffice for the majority of cases. However, we must be
169 especially careful with empty productions: LL won't work if the first
170 or last token on the lhs can represent an empty span. In these cases,
171 we have to calculate the span using more of the tokens from the lhs, eg.
173 | 'newtype' tycl_hdr '=' newconstr deriving
175 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
177 We provide comb3 and comb4 functions which are useful in such cases.
179 Be careful: there's no checking that you actually got this right, the
180 only symptom will be that the SrcSpans of your syntax will be
184 * We must expand these macros *before* running Happy, which is why this file is
185 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
187 #define L0 L noSrcSpan
188 #define L1 sL (getLoc $1)
189 #define LL sL (comb2 $1 $>)
191 -- -----------------------------------------------------------------------------
196 '_' { L _ ITunderscore } -- Haskell keywords
198 'case' { L _ ITcase }
199 'class' { L _ ITclass }
200 'data' { L _ ITdata }
201 'default' { L _ ITdefault }
202 'deriving' { L _ ITderiving }
203 'derive' { L _ ITderive }
205 'else' { L _ ITelse }
206 'hiding' { L _ IThiding }
208 'import' { L _ ITimport }
210 'infix' { L _ ITinfix }
211 'infixl' { L _ ITinfixl }
212 'infixr' { L _ ITinfixr }
213 'instance' { L _ ITinstance }
215 'module' { L _ ITmodule }
216 'newtype' { L _ ITnewtype }
218 'qualified' { L _ ITqualified }
219 'then' { L _ ITthen }
220 'type' { L _ ITtype }
221 'where' { L _ ITwhere }
222 '_scc_' { L _ ITscc } -- ToDo: remove
224 'forall' { L _ ITforall } -- GHC extension keywords
225 'foreign' { L _ ITforeign }
226 'export' { L _ ITexport }
227 'label' { L _ ITlabel }
228 'dynamic' { L _ ITdynamic }
229 'safe' { L _ ITsafe }
230 'threadsafe' { L _ ITthreadsafe }
231 'unsafe' { L _ ITunsafe }
233 'family' { L _ ITfamily }
234 'stdcall' { L _ ITstdcallconv }
235 'ccall' { L _ ITccallconv }
236 'dotnet' { L _ ITdotnet }
237 'proc' { L _ ITproc } -- for arrow notation extension
238 'rec' { L _ ITrec } -- for arrow notation extension
240 '{-# INLINE' { L _ (ITinline_prag _) }
241 '{-# SPECIALISE' { L _ ITspec_prag }
242 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
243 '{-# SOURCE' { L _ ITsource_prag }
244 '{-# RULES' { L _ ITrules_prag }
245 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
246 '{-# SCC' { L _ ITscc_prag }
247 '{-# GENERATED' { L _ ITgenerated_prag }
248 '{-# DEPRECATED' { L _ ITdeprecated_prag }
249 '{-# UNPACK' { L _ ITunpack_prag }
250 '#-}' { L _ ITclose_prag }
252 '..' { L _ ITdotdot } -- reserved symbols
254 '::' { L _ ITdcolon }
258 '<-' { L _ ITlarrow }
259 '->' { L _ ITrarrow }
262 '=>' { L _ ITdarrow }
266 '-<' { L _ ITlarrowtail } -- for arrow notation
267 '>-' { L _ ITrarrowtail } -- for arrow notation
268 '-<<' { L _ ITLarrowtail } -- for arrow notation
269 '>>-' { L _ ITRarrowtail } -- for arrow notation
272 '{' { L _ ITocurly } -- special symbols
274 '{|' { L _ ITocurlybar }
275 '|}' { L _ ITccurlybar }
276 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
277 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
280 '[:' { L _ ITopabrack }
281 ':]' { L _ ITcpabrack }
284 '(#' { L _ IToubxparen }
285 '#)' { L _ ITcubxparen }
286 '(|' { L _ IToparenbar }
287 '|)' { L _ ITcparenbar }
290 '`' { L _ ITbackquote }
292 VARID { L _ (ITvarid _) } -- identifiers
293 CONID { L _ (ITconid _) }
294 VARSYM { L _ (ITvarsym _) }
295 CONSYM { L _ (ITconsym _) }
296 QVARID { L _ (ITqvarid _) }
297 QCONID { L _ (ITqconid _) }
298 QVARSYM { L _ (ITqvarsym _) }
299 QCONSYM { L _ (ITqconsym _) }
301 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
303 CHAR { L _ (ITchar _) }
304 STRING { L _ (ITstring _) }
305 INTEGER { L _ (ITinteger _) }
306 RATIONAL { L _ (ITrational _) }
308 PRIMCHAR { L _ (ITprimchar _) }
309 PRIMSTRING { L _ (ITprimstring _) }
310 PRIMINTEGER { L _ (ITprimint _) }
311 PRIMFLOAT { L _ (ITprimfloat _) }
312 PRIMDOUBLE { L _ (ITprimdouble _) }
314 DOCNEXT { L _ (ITdocCommentNext _) }
315 DOCPREV { L _ (ITdocCommentPrev _) }
316 DOCNAMED { L _ (ITdocCommentNamed _) }
317 DOCSECTION { L _ (ITdocSection _ _) }
318 DOCOPTIONS { L _ (ITdocOptions _) }
321 '[|' { L _ ITopenExpQuote }
322 '[p|' { L _ ITopenPatQuote }
323 '[t|' { L _ ITopenTypQuote }
324 '[d|' { L _ ITopenDecQuote }
325 '|]' { L _ ITcloseQuote }
326 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
327 '$(' { L _ ITparenEscape } -- $( exp )
328 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
329 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
331 %monad { P } { >>= } { return }
332 %lexer { lexer } { L _ ITeof }
333 %name parseModule module
334 %name parseStmt maybe_stmt
335 %name parseIdentifier identifier
336 %name parseType ctype
337 %partial parseHeader header
338 %tokentype { (Located Token) }
341 -----------------------------------------------------------------------------
342 -- Identifiers; one of the entry points
343 identifier :: { Located RdrName }
349 -----------------------------------------------------------------------------
352 -- The place for module deprecation is really too restrictive, but if it
353 -- was allowed at its natural place just before 'module', we get an ugly
354 -- s/r conflict with the second alternative. Another solution would be the
355 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
356 -- either, and DEPRECATED is only expected to be used by people who really
357 -- know what they are doing. :-)
359 module :: { Located (HsModule RdrName) }
360 : optdoc 'module' modid maybemoddeprec maybeexports 'where' body
361 {% fileSrcSpan >>= \ loc -> case $1 of { (opt, info, doc) ->
362 return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4
365 {% fileSrcSpan >>= \ loc ->
366 return (L loc (HsModule Nothing Nothing
367 (fst $1) (snd $1) Nothing Nothing emptyHaddockModInfo
370 optdoc :: { (Maybe String, HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
371 : moduleheader { (Nothing, fst $1, snd $1) }
372 | docoptions { (Just $1, emptyHaddockModInfo, Nothing)}
373 | docoptions moduleheader { (Just $1, fst $2, snd $2) }
374 | moduleheader docoptions { (Just $2, fst $1, snd $1) }
375 | {- empty -} { (Nothing, emptyHaddockModInfo, Nothing) }
377 missing_module_keyword :: { () }
378 : {- empty -} {% pushCurrentContext }
380 maybemoddeprec :: { Maybe DeprecTxt }
381 : '{-# DEPRECATED' STRING '#-}' { Just (getSTRING $2) }
382 | {- empty -} { Nothing }
384 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
386 | vocurly top close { $2 }
388 body2 :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
390 | missing_module_keyword top close { $2 }
392 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
393 : importdecls { (reverse $1,[]) }
394 | importdecls ';' cvtopdecls { (reverse $1,$3) }
395 | cvtopdecls { ([],$1) }
397 cvtopdecls :: { [LHsDecl RdrName] }
398 : topdecls { cvTopDecls $1 }
400 -----------------------------------------------------------------------------
401 -- Module declaration & imports only
403 header :: { Located (HsModule RdrName) }
404 : optdoc 'module' modid maybemoddeprec maybeexports 'where' header_body
405 {% fileSrcSpan >>= \ loc -> case $1 of { (opt, info, doc) ->
406 return (L loc (HsModule (Just $3) $5 $7 [] $4
408 | missing_module_keyword importdecls
409 {% fileSrcSpan >>= \ loc ->
410 return (L loc (HsModule Nothing Nothing $2 [] Nothing
411 Nothing emptyHaddockModInfo Nothing)) }
413 header_body :: { [LImportDecl RdrName] }
414 : '{' importdecls { $2 }
415 | vocurly importdecls { $2 }
417 -----------------------------------------------------------------------------
420 maybeexports :: { Maybe [LIE RdrName] }
421 : '(' exportlist ')' { Just $2 }
422 | {- empty -} { Nothing }
424 exportlist :: { [LIE RdrName] }
425 : expdoclist ',' expdoclist { $1 ++ $3 }
428 exportlist1 :: { [LIE RdrName] }
429 : expdoclist export expdoclist ',' exportlist { $1 ++ ($2 : $3) ++ $5 }
430 | expdoclist export expdoclist { $1 ++ ($2 : $3) }
433 expdoclist :: { [LIE RdrName] }
434 : exp_doc expdoclist { $1 : $2 }
437 exp_doc :: { LIE RdrName }
438 : docsection { L1 (case (unLoc $1) of (n, doc) -> IEGroup n doc) }
439 | docnamed { L1 (IEDocNamed ((fst . unLoc) $1)) }
440 | docnext { L1 (IEDoc (unLoc $1)) }
442 -- No longer allow things like [] and (,,,) to be exported
443 -- They are built in syntax, always available
444 export :: { LIE RdrName }
445 : qvar { L1 (IEVar (unLoc $1)) }
446 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
447 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
448 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
449 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
450 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
452 qcnames :: { [RdrName] }
453 : qcnames ',' qcname_ext { unLoc $3 : $1 }
454 | qcname_ext { [unLoc $1] }
456 qcname_ext :: { Located RdrName } -- Variable or data constructor
457 -- or tagged type constructor
459 | 'type' qcon { sL (comb2 $1 $2)
460 (setRdrNameSpace (unLoc $2)
463 -- Cannot pull into qcname_ext, as qcname is also used in expression.
464 qcname :: { Located RdrName } -- Variable or data constructor
468 -----------------------------------------------------------------------------
469 -- Import Declarations
471 -- import decls can be *empty*, or even just a string of semicolons
472 -- whereas topdecls must contain at least one topdecl.
474 importdecls :: { [LImportDecl RdrName] }
475 : importdecls ';' importdecl { $3 : $1 }
476 | importdecls ';' { $1 }
477 | importdecl { [ $1 ] }
480 importdecl :: { LImportDecl RdrName }
481 : 'import' maybe_src optqualified modid maybeas maybeimpspec
482 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
484 maybe_src :: { IsBootInterface }
485 : '{-# SOURCE' '#-}' { True }
486 | {- empty -} { False }
488 optqualified :: { Bool }
489 : 'qualified' { True }
490 | {- empty -} { False }
492 maybeas :: { Located (Maybe ModuleName) }
493 : 'as' modid { LL (Just (unLoc $2)) }
494 | {- empty -} { noLoc Nothing }
496 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
497 : impspec { L1 (Just (unLoc $1)) }
498 | {- empty -} { noLoc Nothing }
500 impspec :: { Located (Bool, [LIE RdrName]) }
501 : '(' exportlist ')' { LL (False, $2) }
502 | 'hiding' '(' exportlist ')' { LL (True, $3) }
504 -----------------------------------------------------------------------------
505 -- Fixity Declarations
509 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
511 infix :: { Located FixityDirection }
512 : 'infix' { L1 InfixN }
513 | 'infixl' { L1 InfixL }
514 | 'infixr' { L1 InfixR }
516 ops :: { Located [Located RdrName] }
517 : ops ',' op { LL ($3 : unLoc $1) }
520 -----------------------------------------------------------------------------
521 -- Top-Level Declarations
523 topdecls :: { OrdList (LHsDecl RdrName) }
524 : topdecls ';' topdecl { $1 `appOL` $3 }
525 | topdecls ';' { $1 }
528 topdecl :: { OrdList (LHsDecl RdrName) }
529 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
530 | ty_decl { unitOL (L1 (TyClD (unLoc $1))) }
531 | 'instance' inst_type where_inst
532 { let (binds, sigs, ats, _) = cvBindsAndSigs (unLoc $3)
534 unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs ats)))}
535 | stand_alone_deriving { unitOL (LL (DerivD (unLoc $1))) }
536 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
537 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
538 | '{-# DEPRECATED' deprecations '#-}' { $2 }
539 | '{-# RULES' rules '#-}' { $2 }
542 -- Template Haskell Extension
543 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
544 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
545 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
550 cl_decl :: { LTyClDecl RdrName }
551 : 'class' tycl_hdr fds where_cls
552 {% do { let { (binds, sigs, ats, docs) =
553 cvBindsAndSigs (unLoc $4)
554 ; (ctxt, tc, tvs, tparms) = unLoc $2}
555 ; checkTyVars tparms -- only type vars allowed
557 ; return $ L (comb4 $1 $2 $3 $4)
558 (mkClassDecl (ctxt, tc, tvs)
559 (unLoc $3) sigs binds ats docs) } }
561 -- Type declarations (toplevel)
563 ty_decl :: { LTyClDecl RdrName }
564 -- ordinary type synonyms
565 : 'type' type '=' ctype
566 -- Note ctype, not sigtype, on the right of '='
567 -- We allow an explicit for-all but we don't insert one
568 -- in type Foo a = (b,b)
569 -- Instead we just say b is out of scope
571 -- Note the use of type for the head; this allows
572 -- infix type constructors to be declared
573 {% do { (tc, tvs, _) <- checkSynHdr $2 False
574 ; return (L (comb2 $1 $4)
575 (TySynonym tc tvs Nothing $4))
578 -- type family declarations
579 | 'type' 'family' type opt_kind_sig
580 -- Note the use of type for the head; this allows
581 -- infix type constructors to be declared
583 {% do { (tc, tvs, _) <- checkSynHdr $3 False
584 ; return (L (comb3 $1 $3 $4)
585 (TyFamily TypeFamily tc tvs (unLoc $4)))
588 -- type instance declarations
589 | 'type' 'instance' type '=' ctype
590 -- Note the use of type for the head; this allows
591 -- infix type constructors and type patterns
593 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
594 ; return (L (comb2 $1 $5)
595 (TySynonym tc tvs (Just typats) $5))
598 -- ordinary data type or newtype declaration
599 | data_or_newtype tycl_hdr constrs deriving
600 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
601 ; checkTyVars tparms -- no type pattern
603 L (comb4 $1 $2 $3 $4)
604 -- We need the location on tycl_hdr in case
605 -- constrs and deriving are both empty
606 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
607 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
609 -- ordinary GADT declaration
610 | data_or_newtype tycl_hdr opt_kind_sig
611 'where' gadt_constrlist
613 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
614 ; checkTyVars tparms -- can have type pats
616 L (comb4 $1 $2 $4 $5)
617 (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing)
618 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
620 -- data/newtype family
621 | data_or_newtype 'family' tycl_hdr opt_kind_sig
622 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
623 ; checkTyVars tparms -- no type pattern
624 ; unless (null (unLoc ctxt)) $ -- and no context
625 parseError (getLoc ctxt)
626 "A family declaration cannot have a context"
629 (TyFamily (DataFamily (unLoc $1)) tc tvs
632 -- data/newtype instance declaration
633 | data_or_newtype 'instance' tycl_hdr constrs deriving
634 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
635 -- can have type pats
637 L (comb4 $1 $3 $4 $5)
638 -- We need the location on tycl_hdr in case
639 -- constrs and deriving are both empty
640 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
641 Nothing (reverse (unLoc $4)) (unLoc $5)) } }
643 -- GADT instance declaration
644 | data_or_newtype 'instance' tycl_hdr opt_kind_sig
645 'where' gadt_constrlist
647 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
648 -- can have type pats
650 L (comb4 $1 $3 $6 $7)
651 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
652 (unLoc $4) (reverse (unLoc $6)) (unLoc $7)) } }
654 -- Associate type family declarations
656 -- * They have a different syntax than on the toplevel (no family special
659 -- * They also need to be separate from instances; otherwise, data family
660 -- declarations without a kind signature cause parsing conflicts with empty
661 -- data declarations.
663 at_decl_cls :: { LTyClDecl RdrName }
664 -- type family declarations
665 : 'type' type opt_kind_sig
666 -- Note the use of type for the head; this allows
667 -- infix type constructors to be declared
669 {% do { (tc, tvs, _) <- checkSynHdr $2 False
670 ; return (L (comb3 $1 $2 $3)
671 (TyFamily TypeFamily tc tvs (unLoc $3)))
674 -- default type instance
675 | 'type' type '=' ctype
676 -- Note the use of type for the head; this allows
677 -- infix type constructors and type patterns
679 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
680 ; return (L (comb2 $1 $4)
681 (TySynonym tc tvs (Just typats) $4))
684 -- data/newtype family declaration
685 | data_or_newtype tycl_hdr opt_kind_sig
686 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
687 ; checkTyVars tparms -- no type pattern
688 ; unless (null (unLoc ctxt)) $ -- and no context
689 parseError (getLoc ctxt)
690 "A family declaration cannot have a context"
693 (TyFamily (DataFamily (unLoc $1)) tc tvs
697 -- Associate type instances
699 at_decl_inst :: { LTyClDecl RdrName }
700 -- type instance declarations
701 : 'type' type '=' ctype
702 -- Note the use of type for the head; this allows
703 -- infix type constructors and type patterns
705 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
706 ; return (L (comb2 $1 $4)
707 (TySynonym tc tvs (Just typats) $4))
710 -- data/newtype instance declaration
711 | data_or_newtype tycl_hdr constrs deriving
712 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
713 -- can have type pats
715 L (comb4 $1 $2 $3 $4)
716 -- We need the location on tycl_hdr in case
717 -- constrs and deriving are both empty
718 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
719 Nothing (reverse (unLoc $3)) (unLoc $4)) } }
721 -- GADT instance declaration
722 | data_or_newtype tycl_hdr opt_kind_sig
723 'where' gadt_constrlist
725 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
726 -- can have type pats
728 L (comb4 $1 $2 $5 $6)
729 (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms)
730 (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
732 data_or_newtype :: { Located NewOrData }
733 : 'data' { L1 DataType }
734 | 'newtype' { L1 NewType }
736 opt_kind_sig :: { Located (Maybe Kind) }
738 | '::' kind { LL (Just (unLoc $2)) }
740 -- tycl_hdr parses the header of a class or data type decl,
741 -- which takes the form
744 -- (Eq a, Ord b) => T a b
745 -- T Int [a] -- for associated types
746 -- Rather a lot of inlining here, else we get reduce/reduce errors
747 tycl_hdr :: { Located (LHsContext RdrName,
749 [LHsTyVarBndr RdrName],
751 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
752 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
754 -----------------------------------------------------------------------------
755 -- Stand-alone deriving
757 -- Glasgow extension: stand-alone deriving declarations
758 stand_alone_deriving :: { LDerivDecl RdrName }
759 : 'derive' 'instance' inst_type {% checkDerivDecl (LL (DerivDecl $3)) }
761 -----------------------------------------------------------------------------
762 -- Nested declarations
764 -- Declaration in class bodies
766 decl_cls :: { Located (OrdList (LHsDecl RdrName)) }
767 decl_cls : at_decl_cls { LL (unitOL (L1 (TyClD (unLoc $1)))) }
770 decls_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
771 : decls_cls ';' decl_cls { LL (unLoc $1 `appOL` unLoc $3) }
772 | decls_cls ';' { LL (unLoc $1) }
774 | {- empty -} { noLoc nilOL }
778 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
779 : '{' decls_cls '}' { LL (unLoc $2) }
780 | vocurly decls_cls close { $2 }
784 where_cls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
785 -- No implicit parameters
786 -- May have type declarations
787 : 'where' decllist_cls { LL (unLoc $2) }
788 | {- empty -} { noLoc nilOL }
790 -- Declarations in instance bodies
792 decl_inst :: { Located (OrdList (LHsDecl RdrName)) }
793 decl_inst : at_decl_inst { LL (unitOL (L1 (TyClD (unLoc $1)))) }
796 decls_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
797 : decls_inst ';' decl_inst { LL (unLoc $1 `appOL` unLoc $3) }
798 | decls_inst ';' { LL (unLoc $1) }
800 | {- empty -} { noLoc nilOL }
803 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
804 : '{' decls_inst '}' { LL (unLoc $2) }
805 | vocurly decls_inst close { $2 }
809 where_inst :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
810 -- No implicit parameters
811 -- May have type declarations
812 : 'where' decllist_inst { LL (unLoc $2) }
813 | {- empty -} { noLoc nilOL }
815 -- Declarations in binding groups other than classes and instances
817 decls :: { Located (OrdList (LHsDecl RdrName)) }
818 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
819 | decls ';' { LL (unLoc $1) }
821 | {- empty -} { noLoc nilOL }
823 decllist :: { Located (OrdList (LHsDecl RdrName)) }
824 : '{' decls '}' { LL (unLoc $2) }
825 | vocurly decls close { $2 }
827 -- Binding groups other than those of class and instance declarations
829 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
830 -- No type declarations
831 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
832 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
833 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
835 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
836 -- No type declarations
837 : 'where' binds { LL (unLoc $2) }
838 | {- empty -} { noLoc emptyLocalBinds }
841 -----------------------------------------------------------------------------
842 -- Transformation Rules
844 rules :: { OrdList (LHsDecl RdrName) }
845 : rules ';' rule { $1 `snocOL` $3 }
848 | {- empty -} { nilOL }
850 rule :: { LHsDecl RdrName }
851 : STRING activation rule_forall infixexp '=' exp
852 { LL $ RuleD (HsRule (getSTRING $1)
853 ($2 `orElse` AlwaysActive)
854 $3 $4 placeHolderNames $6 placeHolderNames) }
856 activation :: { Maybe Activation }
857 : {- empty -} { Nothing }
858 | explicit_activation { Just $1 }
860 explicit_activation :: { Activation } -- In brackets
861 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
862 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
864 rule_forall :: { [RuleBndr RdrName] }
865 : 'forall' rule_var_list '.' { $2 }
868 rule_var_list :: { [RuleBndr RdrName] }
870 | rule_var rule_var_list { $1 : $2 }
872 rule_var :: { RuleBndr RdrName }
873 : varid { RuleBndr $1 }
874 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
876 -----------------------------------------------------------------------------
877 -- Deprecations (c.f. rules)
879 deprecations :: { OrdList (LHsDecl RdrName) }
880 : deprecations ';' deprecation { $1 `appOL` $3 }
881 | deprecations ';' { $1 }
883 | {- empty -} { nilOL }
885 -- SUP: TEMPORARY HACK, not checking for `module Foo'
886 deprecation :: { OrdList (LHsDecl RdrName) }
888 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
892 -----------------------------------------------------------------------------
893 -- Foreign import and export declarations
895 fdecl :: { LHsDecl RdrName }
896 fdecl : 'import' callconv safety fspec
897 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
898 | 'import' callconv fspec
899 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
901 | 'export' callconv fspec
902 {% mkExport $2 (unLoc $3) >>= return.LL }
904 callconv :: { CallConv }
905 : 'stdcall' { CCall StdCallConv }
906 | 'ccall' { CCall CCallConv }
907 | 'dotnet' { DNCall }
910 : 'unsafe' { PlayRisky }
911 | 'safe' { PlaySafe False }
912 | 'threadsafe' { PlaySafe True }
914 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
915 : STRING var '::' sigtypedoc { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
916 | var '::' sigtypedoc { LL (noLoc nilFS, $1, $3) }
917 -- if the entity string is missing, it defaults to the empty string;
918 -- the meaning of an empty entity string depends on the calling
921 -----------------------------------------------------------------------------
924 opt_sig :: { Maybe (LHsType RdrName) }
925 : {- empty -} { Nothing }
926 | '::' sigtype { Just $2 }
928 opt_asig :: { Maybe (LHsType RdrName) }
929 : {- empty -} { Nothing }
930 | '::' atype { Just $2 }
932 sigtypes1 :: { [LHsType RdrName] }
934 | sigtype ',' sigtypes1 { $1 : $3 }
936 sigtype :: { LHsType RdrName }
937 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
938 -- Wrap an Implicit forall if there isn't one there already
940 sigtypedoc :: { LHsType RdrName }
941 : ctypedoc { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
942 -- Wrap an Implicit forall if there isn't one there already
944 sig_vars :: { Located [Located RdrName] }
945 : sig_vars ',' var { LL ($3 : unLoc $1) }
948 -----------------------------------------------------------------------------
951 infixtype :: { LHsType RdrName }
952 : btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
953 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
955 infixtypedoc :: { LHsType RdrName }
957 | infixtype docprev { LL $ HsDocTy $1 $2 }
959 gentypedoc :: { LHsType RdrName }
962 | infixtypedoc { $1 }
963 | btype '->' ctypedoc { LL $ HsFunTy $1 $3 }
964 | btypedoc '->' ctypedoc { LL $ HsFunTy $1 $3 }
966 ctypedoc :: { LHsType RdrName }
967 : 'forall' tv_bndrs '.' ctypedoc { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
968 | context '=>' gentypedoc { LL $ mkImplicitHsForAllTy $1 $3 }
969 -- A type of form (context => type) is an *implicit* HsForAllTy
972 strict_mark :: { Located HsBang }
973 : '!' { L1 HsStrict }
974 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
976 -- A ctype is a for-all type
977 ctype :: { LHsType RdrName }
978 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
979 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
980 -- A type of form (context => type) is an *implicit* HsForAllTy
983 -- We parse a context as a btype so that we don't get reduce/reduce
984 -- errors in ctype. The basic problem is that
986 -- looks so much like a tuple type. We can't tell until we find the =>
988 -- We have the t1 ~ t2 form here and in gentype, to permit an individual
989 -- equational constraint without parenthesis.
990 context :: { LHsContext RdrName }
991 : btype '~' btype {% checkContext
992 (LL $ HsPredTy (HsEqualP $1 $3)) }
993 | btype {% checkContext $1 }
995 type :: { LHsType RdrName }
996 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
999 gentype :: { LHsType RdrName }
1001 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
1002 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
1003 | btype '->' ctype { LL $ HsFunTy $1 $3 }
1004 | btype '~' btype { LL $ HsPredTy (HsEqualP $1 $3) }
1006 btype :: { LHsType RdrName }
1007 : btype atype { LL $ HsAppTy $1 $2 }
1010 btypedoc :: { LHsType RdrName }
1011 : btype atype docprev { LL $ HsDocTy (L (comb2 $1 $2) (HsAppTy $1 $2)) $3 }
1012 | atype docprev { LL $ HsDocTy $1 $2 }
1014 atype :: { LHsType RdrName }
1015 : gtycon { L1 (HsTyVar (unLoc $1)) }
1016 | tyvar { L1 (HsTyVar (unLoc $1)) }
1017 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
1018 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
1019 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
1020 | '[' ctype ']' { LL $ HsListTy $2 }
1021 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
1022 | '(' ctype ')' { LL $ HsParTy $2 }
1023 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 (unLoc $4) }
1025 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
1027 -- An inst_type is what occurs in the head of an instance decl
1028 -- e.g. (Foo a, Gaz b) => Wibble a b
1029 -- It's kept as a single type, with a MonoDictTy at the right
1030 -- hand corner, for convenience.
1031 inst_type :: { LHsType RdrName }
1032 : sigtype {% checkInstType $1 }
1034 inst_types1 :: { [LHsType RdrName] }
1035 : inst_type { [$1] }
1036 | inst_type ',' inst_types1 { $1 : $3 }
1038 comma_types0 :: { [LHsType RdrName] }
1039 : comma_types1 { $1 }
1040 | {- empty -} { [] }
1042 comma_types1 :: { [LHsType RdrName] }
1044 | ctype ',' comma_types1 { $1 : $3 }
1046 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1047 : tv_bndr tv_bndrs { $1 : $2 }
1048 | {- empty -} { [] }
1050 tv_bndr :: { LHsTyVarBndr RdrName }
1051 : tyvar { L1 (UserTyVar (unLoc $1)) }
1052 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2)
1055 fds :: { Located [Located ([RdrName], [RdrName])] }
1056 : {- empty -} { noLoc [] }
1057 | '|' fds1 { LL (reverse (unLoc $2)) }
1059 fds1 :: { Located [Located ([RdrName], [RdrName])] }
1060 : fds1 ',' fd { LL ($3 : unLoc $1) }
1063 fd :: { Located ([RdrName], [RdrName]) }
1064 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
1065 (reverse (unLoc $1), reverse (unLoc $3)) }
1067 varids0 :: { Located [RdrName] }
1068 : {- empty -} { noLoc [] }
1069 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
1071 -----------------------------------------------------------------------------
1074 kind :: { Located Kind }
1076 | akind '->' kind { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1078 akind :: { Located Kind }
1079 : '*' { L1 liftedTypeKind }
1080 | '!' { L1 unliftedTypeKind }
1081 | '(' kind ')' { LL (unLoc $2) }
1084 -----------------------------------------------------------------------------
1085 -- Datatype declarations
1087 gadt_constrlist :: { Located [LConDecl RdrName] }
1088 : '{' gadt_constrs '}' { LL (unLoc $2) }
1089 | vocurly gadt_constrs close { $2 }
1091 gadt_constrs :: { Located [LConDecl RdrName] }
1092 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
1093 | gadt_constrs ';' { $1 }
1094 | gadt_constr { L1 [$1] }
1096 -- We allow the following forms:
1097 -- C :: Eq a => a -> T a
1098 -- C :: forall a. Eq a => !a -> T a
1099 -- D { x,y :: a } :: T a
1100 -- forall a. Eq a => D { x,y :: a } :: T a
1102 gadt_constr :: { LConDecl RdrName }
1104 { LL (mkGadtDecl $1 $3) }
1105 -- Syntax: Maybe merge the record stuff with the single-case above?
1106 -- (to kill the mostly harmless reduce/reduce error)
1107 -- XXX revisit audreyt
1108 | constr_stuff_record '::' sigtype
1109 { let (con,details) = unLoc $1 in
1110 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3) Nothing) }
1112 | forall context '=>' constr_stuff_record '::' sigtype
1113 { let (con,details) = unLoc $4 in
1114 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6) Nothing ) }
1115 | forall constr_stuff_record '::' sigtype
1116 { let (con,details) = unLoc $2 in
1117 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4) Nothing) }
1121 constrs :: { Located [LConDecl RdrName] }
1122 : {- empty; a GHC extension -} { noLoc [] }
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 L (comb4 $2 $3 $4 $5) (ConDecl con Explicit (unLoc $2) $3 details ResTyH98 ($1 `mplus` $6)) }
1133 | maybe_docnext forall constr_stuff maybe_docprev
1134 { let (con,details) = unLoc $3 in
1135 L (comb2 $2 $3) (ConDecl con Explicit (unLoc $2) (noLoc []) details ResTyH98 ($1 `mplus` $4)) }
1137 forall :: { Located [LHsTyVarBndr RdrName] }
1138 : 'forall' tv_bndrs '.' { LL $2 }
1139 | {- empty -} { noLoc [] }
1141 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
1142 -- We parse the constructor declaration
1144 -- as a btype (treating C as a type constructor) and then convert C to be
1145 -- a data constructor. Reason: it might continue like this:
1147 -- in which case C really would be a type constructor. We can't resolve this
1148 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1149 : btype {% mkPrefixCon $1 [] >>= return.LL }
1150 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
1151 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
1152 | btype conop btype { LL ($2, InfixCon $1 $3) }
1154 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
1155 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
1156 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
1158 fielddecls :: { [([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName))] }
1159 : fielddecl maybe_docnext ',' maybe_docprev fielddecls { addFieldDoc (unLoc $1) $4 : addFieldDocs $5 $2 }
1160 | fielddecl { [unLoc $1] }
1162 fielddecl :: { Located ([Located RdrName], LBangType RdrName, Maybe (LHsDoc RdrName)) }
1163 : maybe_docnext sig_vars '::' ctype maybe_docprev { L (comb3 $2 $3 $4) (reverse (unLoc $2), $4, $1 `mplus` $5) }
1165 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1166 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1167 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1168 -- We don't allow a context, but that's sorted out by the type checker.
1169 deriving :: { Located (Maybe [LHsType RdrName]) }
1170 : {- empty -} { noLoc Nothing }
1171 | 'deriving' qtycon {% do { let { L loc tv = $2 }
1172 ; p <- checkInstType (L loc (HsTyVar tv))
1173 ; return (LL (Just [p])) } }
1174 | 'deriving' '(' ')' { LL (Just []) }
1175 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
1176 -- Glasgow extension: allow partial
1177 -- applications in derivings
1179 -----------------------------------------------------------------------------
1180 -- Value definitions
1182 {- There's an awkward overlap with a type signature. Consider
1183 f :: Int -> Int = ...rhs...
1184 Then we can't tell whether it's a type signature or a value
1185 definition with a result signature until we see the '='.
1186 So we have to inline enough to postpone reductions until we know.
1190 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1191 instead of qvar, we get another shift/reduce-conflict. Consider the
1194 { (^^) :: Int->Int ; } Type signature; only var allowed
1196 { (^^) :: Int->Int = ... ; } Value defn with result signature;
1197 qvar allowed (because of instance decls)
1199 We can't tell whether to reduce var to qvar until after we've read the signatures.
1202 docdecl :: { LHsDecl RdrName }
1203 : docdecld { L1 (DocD (unLoc $1)) }
1205 docdecld :: { LDocDecl RdrName }
1206 : docnext { L1 (DocCommentNext (unLoc $1)) }
1207 | docprev { L1 (DocCommentPrev (unLoc $1)) }
1208 | docnamed { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1209 | docsection { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1211 decl :: { Located (OrdList (LHsDecl RdrName)) }
1213 | '!' aexp rhs {% do { pat <- checkPattern $2;
1214 return (LL $ unitOL $ LL $ ValD (
1215 PatBind (LL $ BangPat pat) (unLoc $3)
1216 placeHolderType placeHolderNames)) } }
1217 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
1218 return (LL $ unitOL (LL $ ValD r)) } }
1219 | docdecl { LL $ unitOL $1 }
1221 rhs :: { Located (GRHSs RdrName) }
1222 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1223 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1225 gdrhs :: { Located [LGRHS RdrName] }
1226 : gdrhs gdrh { LL ($2 : unLoc $1) }
1229 gdrh :: { LGRHS RdrName }
1230 : '|' quals '=' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1232 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1233 : infixexp '::' sigtypedoc
1234 {% do s <- checkValSig $1 $3;
1235 return (LL $ unitOL (LL $ SigD s)) }
1236 -- See the above notes for why we need infixexp here
1237 | var ',' sig_vars '::' sigtypedoc
1238 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1239 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1241 | '{-# INLINE' activation qvar '#-}'
1242 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1243 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1244 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1246 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1247 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1249 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1250 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1252 -----------------------------------------------------------------------------
1255 exp :: { LHsExpr RdrName }
1256 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1257 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1258 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1259 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1260 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1263 infixexp :: { LHsExpr RdrName }
1265 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1267 exp10 :: { LHsExpr RdrName }
1268 : '\\' apat apats opt_asig '->' exp
1269 { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1272 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1273 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1274 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1275 | '-' fexp { LL $ mkHsNegApp $2 }
1277 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1278 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1279 return (L loc (mkHsDo DoExpr stmts body)) }
1280 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1281 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1282 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1283 | scc_annot exp { LL $ if opt_SccProfilingOn
1284 then HsSCC (unLoc $1) $2
1286 | hpc_annot exp { LL $ if opt_Hpc
1287 then HsTickPragma (unLoc $1) $2
1290 | 'proc' aexp '->' exp
1291 {% checkPattern $2 >>= \ p ->
1292 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1293 placeHolderType undefined)) }
1294 -- TODO: is LL right here?
1296 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1297 -- hdaume: core annotation
1300 scc_annot :: { Located FastString }
1301 : '_scc_' STRING { LL $ getSTRING $2 }
1302 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
1304 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1305 : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1306 { LL $ (getSTRING $2
1307 ,( fromInteger $ getINTEGER $3
1308 , fromInteger $ getINTEGER $5
1310 ,( fromInteger $ getINTEGER $7
1311 , fromInteger $ getINTEGER $9
1316 fexp :: { LHsExpr RdrName }
1317 : fexp aexp { LL $ HsApp $1 $2 }
1320 aexp :: { LHsExpr RdrName }
1321 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1322 | '~' aexp { LL $ ELazyPat $2 }
1325 aexp1 :: { LHsExpr RdrName }
1326 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1331 -- Here was the syntax for type applications that I was planning
1332 -- but there are difficulties (e.g. what order for type args)
1333 -- so it's not enabled yet.
1334 -- But this case *is* used for the left hand side of a generic definition,
1335 -- which is parsed as an expression before being munged into a pattern
1336 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1337 (sL (getLoc $3) (HsType $3)) }
1339 aexp2 :: { LHsExpr RdrName }
1340 : ipvar { L1 (HsIPVar $! unLoc $1) }
1341 | qcname { L1 (HsVar $! unLoc $1) }
1342 | literal { L1 (HsLit $! unLoc $1) }
1343 -- This will enable overloaded strings permanently. Normally the renamer turns HsString
1344 -- into HsOverLit when -foverloaded-strings is on.
1345 -- | STRING { L1 (HsOverLit $! mkHsIsString (getSTRING $1)) }
1346 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1347 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1348 | '(' exp ')' { LL (HsPar $2) }
1349 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1350 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1351 | '[' list ']' { LL (unLoc $2) }
1352 | '[:' parr ':]' { LL (unLoc $2) }
1353 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1354 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1355 | '_' { L1 EWildPat }
1357 -- Template Haskell Extension
1358 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1359 (L1 $ HsVar (mkUnqual varName
1360 (getTH_ID_SPLICE $1)))) } -- $x
1361 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1363 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1364 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1365 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1366 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1367 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1368 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1369 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1370 return (LL $ HsBracket (PatBr p)) }
1371 | '[d|' cvtopbody '|]' {% checkDecBrGroup $2 >>= \g ->
1372 return (LL $ HsBracket (DecBr g)) }
1374 -- arrow notation extension
1375 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1377 cmdargs :: { [LHsCmdTop RdrName] }
1378 : cmdargs acmd { $2 : $1 }
1379 | {- empty -} { [] }
1381 acmd :: { LHsCmdTop RdrName }
1382 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1384 cvtopbody :: { [LHsDecl RdrName] }
1385 : '{' cvtopdecls0 '}' { $2 }
1386 | vocurly cvtopdecls0 close { $2 }
1388 cvtopdecls0 :: { [LHsDecl RdrName] }
1389 : {- empty -} { [] }
1392 texp :: { LHsExpr RdrName }
1394 | qopm infixexp { LL $ SectionR $1 $2 }
1395 -- The second production is really here only for bang patterns
1398 texps :: { [LHsExpr RdrName] }
1399 : texps ',' texp { $3 : $1 }
1403 -----------------------------------------------------------------------------
1406 -- The rules below are little bit contorted to keep lexps left-recursive while
1407 -- avoiding another shift/reduce-conflict.
1409 list :: { LHsExpr RdrName }
1410 : texp { L1 $ ExplicitList placeHolderType [$1] }
1411 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1412 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1413 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1414 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1415 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1416 | texp pquals { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1418 lexps :: { Located [LHsExpr RdrName] }
1419 : lexps ',' texp { LL ($3 : unLoc $1) }
1420 | texp ',' texp { LL [$3,$1] }
1422 -----------------------------------------------------------------------------
1423 -- List Comprehensions
1425 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1426 -- or a reversed list of Stmts
1427 : pquals1 { case unLoc $1 of
1429 qss -> L1 [L1 (ParStmt stmtss)]
1431 stmtss = [ (reverse qs, undefined)
1435 pquals1 :: { Located [[LStmt RdrName]] }
1436 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1437 | '|' quals { L (getLoc $2) [unLoc $2] }
1439 quals :: { Located [LStmt RdrName] }
1440 : quals ',' qual { LL ($3 : unLoc $1) }
1443 -----------------------------------------------------------------------------
1444 -- Parallel array expressions
1446 -- The rules below are little bit contorted; see the list case for details.
1447 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1448 -- Moreover, we allow explicit arrays with no element (represented by the nil
1449 -- constructor in the list case).
1451 parr :: { LHsExpr RdrName }
1452 : { noLoc (ExplicitPArr placeHolderType []) }
1453 | texp { L1 $ ExplicitPArr placeHolderType [$1] }
1454 | lexps { L1 $ ExplicitPArr placeHolderType
1455 (reverse (unLoc $1)) }
1456 | texp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1457 | texp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1458 | texp pquals { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1460 -- We are reusing `lexps' and `pquals' from the list case.
1462 -----------------------------------------------------------------------------
1463 -- Case alternatives
1465 altslist :: { Located [LMatch RdrName] }
1466 : '{' alts '}' { LL (reverse (unLoc $2)) }
1467 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1469 alts :: { Located [LMatch RdrName] }
1470 : alts1 { L1 (unLoc $1) }
1471 | ';' alts { LL (unLoc $2) }
1473 alts1 :: { Located [LMatch RdrName] }
1474 : alts1 ';' alt { LL ($3 : unLoc $1) }
1475 | alts1 ';' { LL (unLoc $1) }
1478 alt :: { LMatch RdrName }
1479 : pat opt_sig alt_rhs { LL (Match [$1] $2 (unLoc $3)) }
1481 alt_rhs :: { Located (GRHSs RdrName) }
1482 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1484 ralt :: { Located [LGRHS RdrName] }
1485 : '->' exp { LL (unguardedRHS $2) }
1486 | gdpats { L1 (reverse (unLoc $1)) }
1488 gdpats :: { Located [LGRHS RdrName] }
1489 : gdpats gdpat { LL ($2 : unLoc $1) }
1492 gdpat :: { LGRHS RdrName }
1493 : '|' quals '->' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1495 -- 'pat' recognises a pattern, including one with a bang at the top
1496 -- e.g. "!x" or "!(x,y)" or "C a b" etc
1497 -- Bangs inside are parsed as infix operator applications, so that
1498 -- we parse them right when bang-patterns are off
1499 pat :: { LPat RdrName }
1500 pat : infixexp {% checkPattern $1 }
1501 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1503 apat :: { LPat RdrName }
1504 apat : aexp {% checkPattern $1 }
1505 | '!' aexp {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1507 apats :: { [LPat RdrName] }
1508 : apat apats { $1 : $2 }
1509 | {- empty -} { [] }
1511 -----------------------------------------------------------------------------
1512 -- Statement sequences
1514 stmtlist :: { Located [LStmt RdrName] }
1515 : '{' stmts '}' { LL (unLoc $2) }
1516 | vocurly stmts close { $2 }
1518 -- do { ;; s ; s ; ; s ;; }
1519 -- The last Stmt should be an expression, but that's hard to enforce
1520 -- here, because we need too much lookahead if we see do { e ; }
1521 -- So we use ExprStmts throughout, and switch the last one over
1522 -- in ParseUtils.checkDo instead
1523 stmts :: { Located [LStmt RdrName] }
1524 : stmt stmts_help { LL ($1 : unLoc $2) }
1525 | ';' stmts { LL (unLoc $2) }
1526 | {- empty -} { noLoc [] }
1528 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1529 : ';' stmts { LL (unLoc $2) }
1530 | {- empty -} { noLoc [] }
1532 -- For typing stmts at the GHCi prompt, where
1533 -- the input may consist of just comments.
1534 maybe_stmt :: { Maybe (LStmt RdrName) }
1536 | {- nothing -} { Nothing }
1538 stmt :: { LStmt RdrName }
1540 -- What is this next production doing? I have no clue! SLPJ Dec06
1541 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1542 return (LL $ mkBindStmt p $1) }
1543 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1545 qual :: { LStmt RdrName }
1546 : pat '<-' exp { LL $ mkBindStmt $1 $3 }
1547 | exp { L1 $ mkExprStmt $1 }
1548 | 'let' binds { LL $ LetStmt (unLoc $2) }
1550 -----------------------------------------------------------------------------
1551 -- Record Field Update/Construction
1553 fbinds :: { HsRecordBinds RdrName }
1554 : fbinds1 { HsRecordBinds (reverse $1) }
1555 | {- empty -} { HsRecordBinds [] }
1557 fbinds1 :: { [(Located id, LHsExpr id)] }
1558 : fbinds1 ',' fbind { $3 : $1 }
1561 fbind :: { (Located RdrName, LHsExpr RdrName) }
1562 : qvar '=' exp { ($1,$3) }
1564 -----------------------------------------------------------------------------
1565 -- Implicit Parameter Bindings
1567 dbinds :: { Located [LIPBind RdrName] }
1568 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1569 | dbinds ';' { LL (unLoc $1) }
1571 -- | {- empty -} { [] }
1573 dbind :: { LIPBind RdrName }
1574 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1576 ipvar :: { Located (IPName RdrName) }
1577 : IPDUPVARID { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1579 -----------------------------------------------------------------------------
1582 depreclist :: { Located [RdrName] }
1583 depreclist : deprec_var { L1 [unLoc $1] }
1584 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1586 deprec_var :: { Located RdrName }
1587 deprec_var : var { $1 }
1590 -----------------------------------------
1591 -- Data constructors
1592 qcon :: { Located RdrName }
1594 | '(' qconsym ')' { LL (unLoc $2) }
1595 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1596 -- The case of '[:' ':]' is part of the production `parr'
1598 con :: { Located RdrName }
1600 | '(' consym ')' { LL (unLoc $2) }
1601 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1603 sysdcon :: { Located DataCon } -- Wired in data constructors
1604 : '(' ')' { LL unitDataCon }
1605 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1606 | '[' ']' { LL nilDataCon }
1608 conop :: { Located RdrName }
1610 | '`' conid '`' { LL (unLoc $2) }
1612 qconop :: { Located RdrName }
1614 | '`' qconid '`' { LL (unLoc $2) }
1616 -----------------------------------------------------------------------------
1617 -- Type constructors
1619 gtycon :: { Located RdrName } -- A "general" qualified tycon
1621 | '(' ')' { LL $ getRdrName unitTyCon }
1622 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1623 | '(' '->' ')' { LL $ getRdrName funTyCon }
1624 | '[' ']' { LL $ listTyCon_RDR }
1625 | '[:' ':]' { LL $ parrTyCon_RDR }
1627 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1629 | '(' qtyconsym ')' { LL (unLoc $2) }
1631 qtyconop :: { Located RdrName } -- Qualified or unqualified
1633 | '`' qtycon '`' { LL (unLoc $2) }
1635 qtycon :: { Located RdrName } -- Qualified or unqualified
1636 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1639 tycon :: { Located RdrName } -- Unqualified
1640 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1642 qtyconsym :: { Located RdrName }
1643 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1646 tyconsym :: { Located RdrName }
1647 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1649 -----------------------------------------------------------------------------
1652 op :: { Located RdrName } -- used in infix decls
1656 varop :: { Located RdrName }
1658 | '`' varid '`' { LL (unLoc $2) }
1660 qop :: { LHsExpr RdrName } -- used in sections
1661 : qvarop { L1 $ HsVar (unLoc $1) }
1662 | qconop { L1 $ HsVar (unLoc $1) }
1664 qopm :: { LHsExpr RdrName } -- used in sections
1665 : qvaropm { L1 $ HsVar (unLoc $1) }
1666 | qconop { L1 $ HsVar (unLoc $1) }
1668 qvarop :: { Located RdrName }
1670 | '`' qvarid '`' { LL (unLoc $2) }
1672 qvaropm :: { Located RdrName }
1673 : qvarsym_no_minus { $1 }
1674 | '`' qvarid '`' { LL (unLoc $2) }
1676 -----------------------------------------------------------------------------
1679 tyvar :: { Located RdrName }
1680 tyvar : tyvarid { $1 }
1681 | '(' tyvarsym ')' { LL (unLoc $2) }
1683 tyvarop :: { Located RdrName }
1684 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1687 tyvarid :: { Located RdrName }
1688 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1689 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1690 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1691 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1692 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1694 tyvarsym :: { Located RdrName }
1695 -- Does not include "!", because that is used for strictness marks
1696 -- or ".", because that separates the quantified type vars from the rest
1697 -- or "*", because that's used for kinds
1698 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1700 -----------------------------------------------------------------------------
1703 var :: { Located RdrName }
1705 | '(' varsym ')' { LL (unLoc $2) }
1707 qvar :: { Located RdrName }
1709 | '(' varsym ')' { LL (unLoc $2) }
1710 | '(' qvarsym1 ')' { LL (unLoc $2) }
1711 -- We've inlined qvarsym here so that the decision about
1712 -- whether it's a qvar or a var can be postponed until
1713 -- *after* we see the close paren.
1715 qvarid :: { Located RdrName }
1717 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1719 varid :: { Located RdrName }
1720 : varid_no_unsafe { $1 }
1721 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1722 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1723 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1725 varid_no_unsafe :: { Located RdrName }
1726 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1727 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1728 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1729 | 'family' { L1 $! mkUnqual varName FSLIT("family") }
1731 qvarsym :: { Located RdrName }
1735 qvarsym_no_minus :: { Located RdrName }
1736 : varsym_no_minus { $1 }
1739 qvarsym1 :: { Located RdrName }
1740 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1742 varsym :: { Located RdrName }
1743 : varsym_no_minus { $1 }
1744 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1746 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1747 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1748 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1751 -- These special_ids are treated as keywords in various places,
1752 -- but as ordinary ids elsewhere. 'special_id' collects all these
1753 -- except 'unsafe', 'forall', and 'family' whose treatment differs
1754 -- depending on context
1755 special_id :: { Located FastString }
1757 : 'as' { L1 FSLIT("as") }
1758 | 'qualified' { L1 FSLIT("qualified") }
1759 | 'hiding' { L1 FSLIT("hiding") }
1760 | 'derive' { L1 FSLIT("derive") }
1761 | 'export' { L1 FSLIT("export") }
1762 | 'label' { L1 FSLIT("label") }
1763 | 'dynamic' { L1 FSLIT("dynamic") }
1764 | 'stdcall' { L1 FSLIT("stdcall") }
1765 | 'ccall' { L1 FSLIT("ccall") }
1767 special_sym :: { Located FastString }
1768 special_sym : '!' { L1 FSLIT("!") }
1769 | '.' { L1 FSLIT(".") }
1770 | '*' { L1 FSLIT("*") }
1772 -----------------------------------------------------------------------------
1773 -- Data constructors
1775 qconid :: { Located RdrName } -- Qualified or unqualified
1777 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1779 conid :: { Located RdrName }
1780 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1782 qconsym :: { Located RdrName } -- Qualified or unqualified
1784 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1786 consym :: { Located RdrName }
1787 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1789 -- ':' means only list cons
1790 | ':' { L1 $ consDataCon_RDR }
1793 -----------------------------------------------------------------------------
1796 literal :: { Located HsLit }
1797 : CHAR { L1 $ HsChar $ getCHAR $1 }
1798 | STRING { L1 $ HsString $ getSTRING $1 }
1799 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1800 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1801 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1802 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1803 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1805 -----------------------------------------------------------------------------
1809 : vccurly { () } -- context popped in lexer.
1810 | error {% popContext }
1812 -----------------------------------------------------------------------------
1813 -- Miscellaneous (mostly renamings)
1815 modid :: { Located ModuleName }
1816 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1817 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1820 (unpackFS mod ++ '.':unpackFS c))
1824 : commas ',' { $1 + 1 }
1827 -----------------------------------------------------------------------------
1828 -- Documentation comments
1830 docnext :: { LHsDoc RdrName }
1831 : DOCNEXT {% case parseHaddockParagraphs (tokenise (getDOCNEXT $1)) of {
1832 Left err -> parseError (getLoc $1) err;
1833 Right doc -> return (L1 doc) } }
1835 docprev :: { LHsDoc RdrName }
1836 : DOCPREV {% case parseHaddockParagraphs (tokenise (getDOCPREV $1)) of {
1837 Left err -> parseError (getLoc $1) err;
1838 Right doc -> return (L1 doc) } }
1840 docnamed :: { Located (String, (HsDoc RdrName)) }
1842 let string = getDOCNAMED $1
1843 (name, rest) = break isSpace string
1844 in case parseHaddockParagraphs (tokenise rest) of {
1845 Left err -> parseError (getLoc $1) err;
1846 Right doc -> return (L1 (name, doc)) } }
1848 docsection :: { Located (n, HsDoc RdrName) }
1849 : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1850 case parseHaddockString (tokenise doc) of {
1851 Left err -> parseError (getLoc $1) err;
1852 Right doc -> return (L1 (n, doc)) } }
1854 docoptions :: { String }
1855 : DOCOPTIONS { getDOCOPTIONS $1 }
1857 moduleheader :: { (HaddockModInfo RdrName, Maybe (HsDoc RdrName)) }
1858 : DOCNEXT {% let string = getDOCNEXT $1 in
1859 case parseModuleHeader string of {
1860 Right (str, info) ->
1861 case parseHaddockParagraphs (tokenise str) of {
1862 Left err -> parseError (getLoc $1) err;
1863 Right doc -> return (info, Just doc);
1865 Left err -> parseError (getLoc $1) err
1868 maybe_docprev :: { Maybe (LHsDoc RdrName) }
1869 : docprev { Just $1 }
1870 | {- empty -} { Nothing }
1872 maybe_docnext :: { Maybe (LHsDoc RdrName) }
1873 : docnext { Just $1 }
1874 | {- empty -} { Nothing }
1878 happyError = srcParseFail
1880 getVARID (L _ (ITvarid x)) = x
1881 getCONID (L _ (ITconid x)) = x
1882 getVARSYM (L _ (ITvarsym x)) = x
1883 getCONSYM (L _ (ITconsym x)) = x
1884 getQVARID (L _ (ITqvarid x)) = x
1885 getQCONID (L _ (ITqconid x)) = x
1886 getQVARSYM (L _ (ITqvarsym x)) = x
1887 getQCONSYM (L _ (ITqconsym x)) = x
1888 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1889 getCHAR (L _ (ITchar x)) = x
1890 getSTRING (L _ (ITstring x)) = x
1891 getINTEGER (L _ (ITinteger x)) = x
1892 getRATIONAL (L _ (ITrational x)) = x
1893 getPRIMCHAR (L _ (ITprimchar x)) = x
1894 getPRIMSTRING (L _ (ITprimstring x)) = x
1895 getPRIMINTEGER (L _ (ITprimint x)) = x
1896 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1897 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1898 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1899 getINLINE (L _ (ITinline_prag b)) = b
1900 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1902 getDOCNEXT (L _ (ITdocCommentNext x)) = x
1903 getDOCPREV (L _ (ITdocCommentPrev x)) = x
1904 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
1905 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
1906 getDOCOPTIONS (L _ (ITdocOptions x)) = x
1908 -- Utilities for combining source spans
1909 comb2 :: Located a -> Located b -> SrcSpan
1912 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1913 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1915 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1916 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1917 combineSrcSpans (getLoc c) (getLoc d)
1919 -- strict constructor version:
1921 sL :: SrcSpan -> a -> Located a
1922 sL span a = span `seq` L span a
1924 -- Make a source location for the file. We're a bit lazy here and just
1925 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1926 -- try to find the span of the whole file (ToDo).
1927 fileSrcSpan :: P SrcSpan
1930 let loc = mkSrcLoc (srcLocFile l) 1 0;
1931 return (mkSrcSpan loc loc)