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 )
35 import Type ( Kind, mkArrowKind, liftedTypeKind, unliftedTypeKind )
36 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
37 Activation(..), defaultInlineSpec )
41 import Maybes ( orElse )
47 -----------------------------------------------------------------------------
50 Conflicts: 37 shift/reduce
53 The reduce/reduce conflict is weird. It's between tyconsym and consym, and I
54 would think the two should never occur in the same context.
58 -----------------------------------------------------------------------------
59 Conflicts: 36 shift/reduce (1.25)
61 10 for abiguity in 'if x then y else z + 1' [State 178]
62 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
63 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
65 1 for ambiguity in 'if x then y else z :: T' [State 178]
66 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
68 4 for ambiguity in 'if x then y else z -< e' [State 178]
69 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
70 There are four such operators: -<, >-, -<<, >>-
73 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
74 Which of these two is intended?
76 (x::T) -> T -- Rhs is T
79 (x::T -> T) -> .. -- Rhs is ...
81 10 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
84 As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
85 Same duplication between states 11 and 253 as the previous case
87 1 for ambiguity in 'let ?x ...' [State 329]
88 the parser can't tell whether the ?x is the lhs of a normal binding or
89 an implicit binding. Fortunately resolving as shift gives it the only
90 sensible meaning, namely the lhs of an implicit binding.
92 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
93 we don't know whether the '[' starts the activation or not: it
94 might be the start of the declaration with the activation being
97 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
98 since 'forall' is a valid variable name, we don't know whether
99 to treat a forall on the input as the beginning of a quantifier
100 or the beginning of the rule itself. Resolving to shift means
101 it's always treated as a quantifier, hence the above is disallowed.
102 This saves explicitly defining a grammar for the rule lhs that
103 doesn't include 'forall'.
105 -- ---------------------------------------------------------------------------
106 -- Adding location info
108 This is done in a stylised way using the three macros below, L0, L1
109 and LL. Each of these macros can be thought of as having type
111 L0, L1, LL :: a -> Located a
113 They each add a SrcSpan to their argument.
115 L0 adds 'noSrcSpan', used for empty productions
117 L1 for a production with a single token on the lhs. Grabs the SrcSpan
120 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
121 the first and last tokens.
123 These suffice for the majority of cases. However, we must be
124 especially careful with empty productions: LL won't work if the first
125 or last token on the lhs can represent an empty span. In these cases,
126 we have to calculate the span using more of the tokens from the lhs, eg.
128 | 'newtype' tycl_hdr '=' newconstr deriving
130 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
132 We provide comb3 and comb4 functions which are useful in such cases.
134 Be careful: there's no checking that you actually got this right, the
135 only symptom will be that the SrcSpans of your syntax will be
139 * We must expand these macros *before* running Happy, which is why this file is
140 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
142 #define L0 L noSrcSpan
143 #define L1 sL (getLoc $1)
144 #define LL sL (comb2 $1 $>)
146 -- -----------------------------------------------------------------------------
151 '_' { L _ ITunderscore } -- Haskell keywords
153 'case' { L _ ITcase }
154 'class' { L _ ITclass }
155 'data' { L _ ITdata }
156 'default' { L _ ITdefault }
157 'deriving' { L _ ITderiving }
159 'else' { L _ ITelse }
160 'hiding' { L _ IThiding }
162 'import' { L _ ITimport }
164 'infix' { L _ ITinfix }
165 'infixl' { L _ ITinfixl }
166 'infixr' { L _ ITinfixr }
167 'instance' { L _ ITinstance }
169 'module' { L _ ITmodule }
170 'newtype' { L _ ITnewtype }
172 'qualified' { L _ ITqualified }
173 'then' { L _ ITthen }
174 'type' { L _ ITtype }
175 'where' { L _ ITwhere }
176 '_scc_' { L _ ITscc } -- ToDo: remove
178 'forall' { L _ ITforall } -- GHC extension keywords
179 'foreign' { L _ ITforeign }
180 'export' { L _ ITexport }
181 'label' { L _ ITlabel }
182 'dynamic' { L _ ITdynamic }
183 'safe' { L _ ITsafe }
184 'threadsafe' { L _ ITthreadsafe }
185 'unsafe' { L _ ITunsafe }
187 'stdcall' { L _ ITstdcallconv }
188 'ccall' { L _ ITccallconv }
189 'dotnet' { L _ ITdotnet }
190 'proc' { L _ ITproc } -- for arrow notation extension
191 'rec' { L _ ITrec } -- for arrow notation extension
193 '{-# INLINE' { L _ (ITinline_prag _) }
194 '{-# SPECIALISE' { L _ ITspec_prag }
195 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
196 '{-# SOURCE' { L _ ITsource_prag }
197 '{-# RULES' { L _ ITrules_prag }
198 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
199 '{-# SCC' { L _ ITscc_prag }
200 '{-# DEPRECATED' { L _ ITdeprecated_prag }
201 '{-# UNPACK' { L _ ITunpack_prag }
202 '#-}' { L _ ITclose_prag }
204 '..' { L _ ITdotdot } -- reserved symbols
206 '::' { L _ ITdcolon }
210 '<-' { L _ ITlarrow }
211 '->' { L _ ITrarrow }
214 '=>' { L _ ITdarrow }
218 '-<' { L _ ITlarrowtail } -- for arrow notation
219 '>-' { L _ ITrarrowtail } -- for arrow notation
220 '-<<' { L _ ITLarrowtail } -- for arrow notation
221 '>>-' { L _ ITRarrowtail } -- for arrow notation
224 '{' { L _ ITocurly } -- special symbols
226 '{|' { L _ ITocurlybar }
227 '|}' { L _ ITccurlybar }
228 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
229 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
232 '[:' { L _ ITopabrack }
233 ':]' { L _ ITcpabrack }
236 '(#' { L _ IToubxparen }
237 '#)' { L _ ITcubxparen }
238 '(|' { L _ IToparenbar }
239 '|)' { L _ ITcparenbar }
242 '`' { L _ ITbackquote }
244 VARID { L _ (ITvarid _) } -- identifiers
245 CONID { L _ (ITconid _) }
246 VARSYM { L _ (ITvarsym _) }
247 CONSYM { L _ (ITconsym _) }
248 QVARID { L _ (ITqvarid _) }
249 QCONID { L _ (ITqconid _) }
250 QVARSYM { L _ (ITqvarsym _) }
251 QCONSYM { L _ (ITqconsym _) }
253 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
254 IPSPLITVARID { L _ (ITsplitipvarid _) } -- GHC extension
256 CHAR { L _ (ITchar _) }
257 STRING { L _ (ITstring _) }
258 INTEGER { L _ (ITinteger _) }
259 RATIONAL { L _ (ITrational _) }
261 PRIMCHAR { L _ (ITprimchar _) }
262 PRIMSTRING { L _ (ITprimstring _) }
263 PRIMINTEGER { L _ (ITprimint _) }
264 PRIMFLOAT { L _ (ITprimfloat _) }
265 PRIMDOUBLE { L _ (ITprimdouble _) }
268 '[|' { L _ ITopenExpQuote }
269 '[p|' { L _ ITopenPatQuote }
270 '[t|' { L _ ITopenTypQuote }
271 '[d|' { L _ ITopenDecQuote }
272 '|]' { L _ ITcloseQuote }
273 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
274 '$(' { L _ ITparenEscape } -- $( exp )
275 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
276 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
278 %monad { P } { >>= } { return }
279 %lexer { lexer } { L _ ITeof }
280 %name parseModule module
281 %name parseStmt maybe_stmt
282 %name parseIdentifier identifier
283 %name parseType ctype
284 %partial parseHeader header
285 %tokentype { (Located Token) }
288 -----------------------------------------------------------------------------
289 -- Identifiers; one of the entry points
290 identifier :: { Located RdrName }
296 -----------------------------------------------------------------------------
299 -- The place for module deprecation is really too restrictive, but if it
300 -- was allowed at its natural place just before 'module', we get an ugly
301 -- s/r conflict with the second alternative. Another solution would be the
302 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
303 -- either, and DEPRECATED is only expected to be used by people who really
304 -- know what they are doing. :-)
306 module :: { Located (HsModule RdrName) }
307 : 'module' modid maybemoddeprec maybeexports 'where' body
308 {% fileSrcSpan >>= \ loc ->
309 return (L loc (HsModule (Just $2) $4 (fst $6) (snd $6) $3)) }
310 | missing_module_keyword top close
311 {% fileSrcSpan >>= \ loc ->
312 return (L loc (HsModule Nothing Nothing
313 (fst $2) (snd $2) Nothing)) }
315 missing_module_keyword :: { () }
316 : {- empty -} {% pushCurrentContext }
318 maybemoddeprec :: { Maybe DeprecTxt }
319 : '{-# DEPRECATED' STRING '#-}' { Just (getSTRING $2) }
320 | {- empty -} { Nothing }
322 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
324 | vocurly top close { $2 }
326 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
327 : importdecls { (reverse $1,[]) }
328 | importdecls ';' cvtopdecls { (reverse $1,$3) }
329 | cvtopdecls { ([],$1) }
331 cvtopdecls :: { [LHsDecl RdrName] }
332 : topdecls { cvTopDecls $1 }
334 -----------------------------------------------------------------------------
335 -- Module declaration & imports only
337 header :: { Located (HsModule RdrName) }
338 : 'module' modid maybemoddeprec maybeexports 'where' header_body
339 {% fileSrcSpan >>= \ loc ->
340 return (L loc (HsModule (Just $2) $4 $6 [] $3)) }
341 | missing_module_keyword importdecls
342 {% fileSrcSpan >>= \ loc ->
343 return (L loc (HsModule Nothing Nothing $2 [] Nothing)) }
345 header_body :: { [LImportDecl RdrName] }
346 : '{' importdecls { $2 }
347 | vocurly importdecls { $2 }
349 -----------------------------------------------------------------------------
352 maybeexports :: { Maybe [LIE RdrName] }
353 : '(' exportlist ')' { Just $2 }
354 | {- empty -} { Nothing }
356 exportlist :: { [LIE RdrName] }
360 exportlist1 :: { [LIE RdrName] }
362 | export ',' exportlist { $1 : $3 }
365 -- No longer allow things like [] and (,,,) to be exported
366 -- They are built in syntax, always available
367 export :: { LIE RdrName }
368 : qvar { L1 (IEVar (unLoc $1)) }
369 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
370 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
371 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
372 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
373 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
375 qcnames :: { [RdrName] }
376 : qcnames ',' qcname { unLoc $3 : $1 }
377 | qcname { [unLoc $1] }
379 qcname :: { Located RdrName } -- Variable or data constructor
383 -----------------------------------------------------------------------------
384 -- Import Declarations
386 -- import decls can be *empty*, or even just a string of semicolons
387 -- whereas topdecls must contain at least one topdecl.
389 importdecls :: { [LImportDecl RdrName] }
390 : importdecls ';' importdecl { $3 : $1 }
391 | importdecls ';' { $1 }
392 | importdecl { [ $1 ] }
395 importdecl :: { LImportDecl RdrName }
396 : 'import' maybe_src optqualified modid maybeas maybeimpspec
397 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
399 maybe_src :: { IsBootInterface }
400 : '{-# SOURCE' '#-}' { True }
401 | {- empty -} { False }
403 optqualified :: { Bool }
404 : 'qualified' { True }
405 | {- empty -} { False }
407 maybeas :: { Located (Maybe ModuleName) }
408 : 'as' modid { LL (Just (unLoc $2)) }
409 | {- empty -} { noLoc Nothing }
411 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
412 : impspec { L1 (Just (unLoc $1)) }
413 | {- empty -} { noLoc Nothing }
415 impspec :: { Located (Bool, [LIE RdrName]) }
416 : '(' exportlist ')' { LL (False, $2) }
417 | 'hiding' '(' exportlist ')' { LL (True, $3) }
419 -----------------------------------------------------------------------------
420 -- Fixity Declarations
424 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
426 infix :: { Located FixityDirection }
427 : 'infix' { L1 InfixN }
428 | 'infixl' { L1 InfixL }
429 | 'infixr' { L1 InfixR }
431 ops :: { Located [Located RdrName] }
432 : ops ',' op { LL ($3 : unLoc $1) }
435 -----------------------------------------------------------------------------
436 -- Top-Level Declarations
438 topdecls :: { OrdList (LHsDecl RdrName) }
439 : topdecls ';' topdecl { $1 `appOL` $3 }
440 | topdecls ';' { $1 }
443 topdecl :: { OrdList (LHsDecl RdrName) }
444 : cl_decl { unitOL (L1 (TyClD (unLoc $1))) }
445 | ty_decl {% checkTopTyClD $1 >>= return.unitOL.L1 }
446 | 'instance' inst_type where
447 { let (binds, sigs, ats) = cvBindsAndSigs (unLoc $3)
448 in unitOL (L (comb3 $1 $2 $3)
449 (InstD (InstDecl $2 binds sigs ats))) }
450 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
451 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
452 | '{-# DEPRECATED' deprecations '#-}' { $2 }
453 | '{-# RULES' rules '#-}' { $2 }
456 -- Template Haskell Extension
457 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
458 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
459 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
464 cl_decl :: { LTyClDecl RdrName }
465 : 'class' tycl_hdr fds where
466 {% do { let { (binds, sigs, ats) =
467 cvBindsAndSigs (unLoc $4)
468 ; (ctxt, tc, tvs, Just tparms) = unLoc $2}
470 ; return $ L (comb4 $1 $2 $3 $4)
471 (mkClassDecl (ctxt, tc, tvs)
472 (unLoc $3) sigs binds ats) } }
476 ty_decl :: { LTyClDecl RdrName }
477 : 'type' type '=' ctype
478 -- Note type on the left of the '='; this allows
479 -- infix type constructors to be declared
481 -- Note ctype, not sigtype, on the right
482 -- We allow an explicit for-all but we don't insert one
483 -- in type Foo a = (b,b)
484 -- Instead we just say b is out of scope
485 {% do { (tc,tvs) <- checkSynHdr $2
486 ; return (LL (TySynonym tc tvs $4)) } }
488 | data_or_newtype tycl_hdr constrs deriving
489 { L (comb4 $1 $2 $3 $4) -- We need the location on tycl_hdr
490 -- in case constrs and deriving are both empty
491 (mkTyData (unLoc $1) (unLoc $2) Nothing (reverse (unLoc $3)) (unLoc $4)) }
493 | data_or_newtype tycl_hdr opt_kind_sig
494 'where' gadt_constrlist
496 { L (comb4 $1 $2 $4 $5)
497 (mkTyData (unLoc $1) (unLoc $2) $3 (reverse (unLoc $5)) (unLoc $6)) }
499 data_or_newtype :: { Located NewOrData }
500 : 'data' { L1 DataType }
501 | 'newtype' { L1 NewType }
503 opt_kind_sig :: { Maybe Kind }
505 | '::' kind { Just $2 }
507 -- tycl_hdr parses the header of a type decl,
508 -- which takes the form
511 -- (Eq a, Ord b) => T a b
512 -- T Int [a] -- for associated types
513 -- Rather a lot of inlining here, else we get reduce/reduce errors
514 tycl_hdr :: { Located (LHsContext RdrName,
516 [LHsTyVarBndr RdrName],
517 Maybe [LHsType RdrName]) }
518 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
519 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
521 -----------------------------------------------------------------------------
522 -- Nested declarations
524 -- Type declaration or value declaration
526 tydecl :: { Located (OrdList (LHsDecl RdrName)) }
527 tydecl : ty_decl { LL (unitOL (L1 (TyClD (unLoc $1)))) }
530 tydecls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
531 : tydecls ';' tydecl { LL (unLoc $1 `appOL` unLoc $3) }
532 | tydecls ';' { LL (unLoc $1) }
534 | {- empty -} { noLoc nilOL }
538 :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
539 : '{' tydecls '}' { LL (unLoc $2) }
540 | vocurly tydecls close { $2 }
542 -- Form of the body of class and instance declarations
544 where :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
545 -- No implicit parameters
546 -- May have type declarations
547 : 'where' tydecllist { LL (unLoc $2) }
548 | {- empty -} { noLoc nilOL }
550 decls :: { Located (OrdList (LHsDecl RdrName)) }
551 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
552 | decls ';' { LL (unLoc $1) }
554 | {- empty -} { noLoc nilOL }
557 decllist :: { Located (OrdList (LHsDecl RdrName)) }
558 : '{' decls '}' { LL (unLoc $2) }
559 | vocurly decls close { $2 }
561 -- Binding groups other than those of class and instance declarations
563 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
564 -- No type declarations
565 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
566 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
567 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
569 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
570 -- No type declarations
571 : 'where' binds { LL (unLoc $2) }
572 | {- empty -} { noLoc emptyLocalBinds }
575 -----------------------------------------------------------------------------
576 -- Transformation Rules
578 rules :: { OrdList (LHsDecl RdrName) }
579 : rules ';' rule { $1 `snocOL` $3 }
582 | {- empty -} { nilOL }
584 rule :: { LHsDecl RdrName }
585 : STRING activation rule_forall infixexp '=' exp
586 { LL $ RuleD (HsRule (getSTRING $1)
587 ($2 `orElse` AlwaysActive)
588 $3 $4 placeHolderNames $6 placeHolderNames) }
590 activation :: { Maybe Activation }
591 : {- empty -} { Nothing }
592 | explicit_activation { Just $1 }
594 explicit_activation :: { Activation } -- In brackets
595 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
596 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
598 rule_forall :: { [RuleBndr RdrName] }
599 : 'forall' rule_var_list '.' { $2 }
602 rule_var_list :: { [RuleBndr RdrName] }
604 | rule_var rule_var_list { $1 : $2 }
606 rule_var :: { RuleBndr RdrName }
607 : varid { RuleBndr $1 }
608 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
610 -----------------------------------------------------------------------------
611 -- Deprecations (c.f. rules)
613 deprecations :: { OrdList (LHsDecl RdrName) }
614 : deprecations ';' deprecation { $1 `appOL` $3 }
615 | deprecations ';' { $1 }
617 | {- empty -} { nilOL }
619 -- SUP: TEMPORARY HACK, not checking for `module Foo'
620 deprecation :: { OrdList (LHsDecl RdrName) }
622 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
626 -----------------------------------------------------------------------------
627 -- Foreign import and export declarations
629 fdecl :: { LHsDecl RdrName }
630 fdecl : 'import' callconv safety fspec
631 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
632 | 'import' callconv fspec
633 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
635 | 'export' callconv fspec
636 {% mkExport $2 (unLoc $3) >>= return.LL }
638 callconv :: { CallConv }
639 : 'stdcall' { CCall StdCallConv }
640 | 'ccall' { CCall CCallConv }
641 | 'dotnet' { DNCall }
644 : 'unsafe' { PlayRisky }
645 | 'safe' { PlaySafe False }
646 | 'threadsafe' { PlaySafe True }
648 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
649 : STRING var '::' sigtype { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
650 | var '::' sigtype { LL (noLoc nilFS, $1, $3) }
651 -- if the entity string is missing, it defaults to the empty string;
652 -- the meaning of an empty entity string depends on the calling
655 -----------------------------------------------------------------------------
658 opt_sig :: { Maybe (LHsType RdrName) }
659 : {- empty -} { Nothing }
660 | '::' sigtype { Just $2 }
662 opt_asig :: { Maybe (LHsType RdrName) }
663 : {- empty -} { Nothing }
664 | '::' atype { Just $2 }
666 sigtypes1 :: { [LHsType RdrName] }
668 | sigtype ',' sigtypes1 { $1 : $3 }
670 sigtype :: { LHsType RdrName }
671 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
672 -- Wrap an Implicit forall if there isn't one there already
674 sig_vars :: { Located [Located RdrName] }
675 : sig_vars ',' var { LL ($3 : unLoc $1) }
678 -----------------------------------------------------------------------------
681 strict_mark :: { Located HsBang }
682 : '!' { L1 HsStrict }
683 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
685 -- A ctype is a for-all type
686 ctype :: { LHsType RdrName }
687 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
688 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
689 -- A type of form (context => type) is an *implicit* HsForAllTy
692 -- We parse a context as a btype so that we don't get reduce/reduce
693 -- errors in ctype. The basic problem is that
695 -- looks so much like a tuple type. We can't tell until we find the =>
696 context :: { LHsContext RdrName }
697 : btype {% checkContext $1 }
699 type :: { LHsType RdrName }
700 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
703 gentype :: { LHsType RdrName }
705 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
706 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
707 | btype '->' ctype { LL $ HsFunTy $1 $3 }
709 btype :: { LHsType RdrName }
710 : btype atype { LL $ HsAppTy $1 $2 }
713 atype :: { LHsType RdrName }
714 : gtycon { L1 (HsTyVar (unLoc $1)) }
715 | tyvar { L1 (HsTyVar (unLoc $1)) }
716 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
717 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
718 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
719 | '[' ctype ']' { LL $ HsListTy $2 }
720 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
721 | '(' ctype ')' { LL $ HsParTy $2 }
722 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 $4 }
724 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
726 -- An inst_type is what occurs in the head of an instance decl
727 -- e.g. (Foo a, Gaz b) => Wibble a b
728 -- It's kept as a single type, with a MonoDictTy at the right
729 -- hand corner, for convenience.
730 inst_type :: { LHsType RdrName }
731 : sigtype {% checkInstType $1 }
733 inst_types1 :: { [LHsType RdrName] }
735 | inst_type ',' inst_types1 { $1 : $3 }
737 comma_types0 :: { [LHsType RdrName] }
738 : comma_types1 { $1 }
741 comma_types1 :: { [LHsType RdrName] }
743 | ctype ',' comma_types1 { $1 : $3 }
745 tv_bndrs :: { [LHsTyVarBndr RdrName] }
746 : tv_bndr tv_bndrs { $1 : $2 }
749 tv_bndr :: { LHsTyVarBndr RdrName }
750 : tyvar { L1 (UserTyVar (unLoc $1)) }
751 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2) $4) }
753 fds :: { Located [Located ([RdrName], [RdrName])] }
754 : {- empty -} { noLoc [] }
755 | '|' fds1 { LL (reverse (unLoc $2)) }
757 fds1 :: { Located [Located ([RdrName], [RdrName])] }
758 : fds1 ',' fd { LL ($3 : unLoc $1) }
761 fd :: { Located ([RdrName], [RdrName]) }
762 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
763 (reverse (unLoc $1), reverse (unLoc $3)) }
765 varids0 :: { Located [RdrName] }
766 : {- empty -} { noLoc [] }
767 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
769 -----------------------------------------------------------------------------
774 | akind '->' kind { mkArrowKind $1 $3 }
777 : '*' { liftedTypeKind }
778 | '!' { unliftedTypeKind }
779 | '(' kind ')' { $2 }
782 -----------------------------------------------------------------------------
783 -- Datatype declarations
785 gadt_constrlist :: { Located [LConDecl RdrName] }
786 : '{' gadt_constrs '}' { LL (unLoc $2) }
787 | vocurly gadt_constrs close { $2 }
789 gadt_constrs :: { Located [LConDecl RdrName] }
790 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
791 | gadt_constrs ';' { $1 }
792 | gadt_constr { L1 [$1] }
794 -- We allow the following forms:
795 -- C :: Eq a => a -> T a
796 -- C :: forall a. Eq a => !a -> T a
797 -- D { x,y :: a } :: T a
798 -- forall a. Eq a => D { x,y :: a } :: T a
800 gadt_constr :: { LConDecl RdrName }
802 { LL (mkGadtDecl $1 $3) }
803 -- Syntax: Maybe merge the record stuff with the single-case above?
804 -- (to kill the mostly harmless reduce/reduce error)
805 -- XXX revisit audreyt
806 | constr_stuff_record '::' sigtype
807 { let (con,details) = unLoc $1 in
808 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
810 | forall context '=>' constr_stuff_record '::' sigtype
811 { let (con,details) = unLoc $4 in
812 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
813 | forall constr_stuff_record '::' sigtype
814 { let (con,details) = unLoc $2 in
815 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
819 constrs :: { Located [LConDecl RdrName] }
820 : {- empty; a GHC extension -} { noLoc [] }
821 | '=' constrs1 { LL (unLoc $2) }
823 constrs1 :: { Located [LConDecl RdrName] }
824 : constrs1 '|' constr { LL ($3 : unLoc $1) }
827 constr :: { LConDecl RdrName }
828 : forall context '=>' constr_stuff
829 { let (con,details) = unLoc $4 in
830 LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
831 | forall constr_stuff
832 { let (con,details) = unLoc $2 in
833 LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
835 forall :: { Located [LHsTyVarBndr RdrName] }
836 : 'forall' tv_bndrs '.' { LL $2 }
837 | {- empty -} { noLoc [] }
839 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
840 -- We parse the constructor declaration
842 -- as a btype (treating C as a type constructor) and then convert C to be
843 -- a data constructor. Reason: it might continue like this:
845 -- in which case C really would be a type constructor. We can't resolve this
846 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
847 : btype {% mkPrefixCon $1 [] >>= return.LL }
848 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
849 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
850 | btype conop btype { LL ($2, InfixCon $1 $3) }
852 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
853 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
854 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
856 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
857 : fielddecl ',' fielddecls { unLoc $1 : $3 }
858 | fielddecl { [unLoc $1] }
860 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
861 : sig_vars '::' ctype { LL (reverse (unLoc $1), $3) }
863 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
864 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
865 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
866 -- We don't allow a context, but that's sorted out by the type checker.
867 deriving :: { Located (Maybe [LHsType RdrName]) }
868 : {- empty -} { noLoc Nothing }
869 | 'deriving' qtycon {% do { let { L loc tv = $2 }
870 ; p <- checkInstType (L loc (HsTyVar tv))
871 ; return (LL (Just [p])) } }
872 | 'deriving' '(' ')' { LL (Just []) }
873 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
874 -- Glasgow extension: allow partial
875 -- applications in derivings
877 -----------------------------------------------------------------------------
880 {- There's an awkward overlap with a type signature. Consider
881 f :: Int -> Int = ...rhs...
882 Then we can't tell whether it's a type signature or a value
883 definition with a result signature until we see the '='.
884 So we have to inline enough to postpone reductions until we know.
888 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
889 instead of qvar, we get another shift/reduce-conflict. Consider the
892 { (^^) :: Int->Int ; } Type signature; only var allowed
894 { (^^) :: Int->Int = ... ; } Value defn with result signature;
895 qvar allowed (because of instance decls)
897 We can't tell whether to reduce var to qvar until after we've read the signatures.
900 decl :: { Located (OrdList (LHsDecl RdrName)) }
902 | '!' infixexp rhs {% do { pat <- checkPattern $2;
903 return (LL $ unitOL $ LL $ ValD $
904 PatBind (LL $ BangPat pat) (unLoc $3)
905 placeHolderType placeHolderNames) } }
906 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
907 return (LL $ unitOL (LL $ ValD r)) } }
909 rhs :: { Located (GRHSs RdrName) }
910 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
911 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
913 gdrhs :: { Located [LGRHS RdrName] }
914 : gdrhs gdrh { LL ($2 : unLoc $1) }
917 gdrh :: { LGRHS RdrName }
918 : '|' quals '=' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
920 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
921 : infixexp '::' sigtype
922 {% do s <- checkValSig $1 $3;
923 return (LL $ unitOL (LL $ SigD s)) }
924 -- See the above notes for why we need infixexp here
925 | var ',' sig_vars '::' sigtype
926 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
927 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
929 | '{-# INLINE' activation qvar '#-}'
930 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
931 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
932 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
934 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
935 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
937 | '{-# SPECIALISE' 'instance' inst_type '#-}'
938 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
940 -----------------------------------------------------------------------------
943 exp :: { LHsExpr RdrName }
944 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
945 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
946 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
947 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
948 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
951 infixexp :: { LHsExpr RdrName }
953 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
955 exp10 :: { LHsExpr RdrName }
956 : '\\' aexp aexps opt_asig '->' exp
957 {% checkPatterns ($2 : reverse $3) >>= \ ps ->
958 return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
959 (GRHSs (unguardedRHS $6) emptyLocalBinds
961 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
962 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
963 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
964 | '-' fexp { LL $ mkHsNegApp $2 }
966 | 'do' stmtlist {% let loc = comb2 $1 $2 in
967 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
968 return (L loc (mkHsDo DoExpr stmts body)) }
969 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
970 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
971 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
972 | scc_annot exp { LL $ if opt_SccProfilingOn
973 then HsSCC (unLoc $1) $2
976 | 'proc' aexp '->' exp
977 {% checkPattern $2 >>= \ p ->
978 return (LL $ HsProc p (LL $ HsCmdTop $4 []
979 placeHolderType undefined)) }
980 -- TODO: is LL right here?
982 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
983 -- hdaume: core annotation
986 scc_annot :: { Located FastString }
987 : '_scc_' STRING { LL $ getSTRING $2 }
988 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
990 fexp :: { LHsExpr RdrName }
991 : fexp aexp { LL $ HsApp $1 $2 }
994 aexps :: { [LHsExpr RdrName] }
995 : aexps aexp { $2 : $1 }
998 aexp :: { LHsExpr RdrName }
999 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1000 | '~' aexp { LL $ ELazyPat $2 }
1001 -- | '!' aexp { LL $ EBangPat $2 }
1004 aexp1 :: { LHsExpr RdrName }
1005 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1010 -- Here was the syntax for type applications that I was planning
1011 -- but there are difficulties (e.g. what order for type args)
1012 -- so it's not enabled yet.
1013 -- But this case *is* used for the left hand side of a generic definition,
1014 -- which is parsed as an expression before being munged into a pattern
1015 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1016 (sL (getLoc $3) (HsType $3)) }
1018 aexp2 :: { LHsExpr RdrName }
1019 : ipvar { L1 (HsIPVar $! unLoc $1) }
1020 | qcname { L1 (HsVar $! unLoc $1) }
1021 | literal { L1 (HsLit $! unLoc $1) }
1022 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1023 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1024 | '(' exp ')' { LL (HsPar $2) }
1025 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1026 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1027 | '[' list ']' { LL (unLoc $2) }
1028 | '[:' parr ':]' { LL (unLoc $2) }
1029 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1030 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1031 | '_' { L1 EWildPat }
1033 -- Template Haskell Extension
1034 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1035 (L1 $ HsVar (mkUnqual varName
1036 (getTH_ID_SPLICE $1)))) } -- $x
1037 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1039 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1040 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1041 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1042 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1043 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1044 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1045 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1046 return (LL $ HsBracket (PatBr p)) }
1047 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBr (mkGroup $2)) }
1049 -- arrow notation extension
1050 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1052 cmdargs :: { [LHsCmdTop RdrName] }
1053 : cmdargs acmd { $2 : $1 }
1054 | {- empty -} { [] }
1056 acmd :: { LHsCmdTop RdrName }
1057 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1059 cvtopbody :: { [LHsDecl RdrName] }
1060 : '{' cvtopdecls0 '}' { $2 }
1061 | vocurly cvtopdecls0 close { $2 }
1063 cvtopdecls0 :: { [LHsDecl RdrName] }
1064 : {- empty -} { [] }
1067 texp :: { LHsExpr RdrName }
1069 | qopm infixexp { LL $ SectionR $1 $2 }
1070 -- The second production is really here only for bang patterns
1073 texps :: { [LHsExpr RdrName] }
1074 : texps ',' texp { $3 : $1 }
1078 -----------------------------------------------------------------------------
1081 -- The rules below are little bit contorted to keep lexps left-recursive while
1082 -- avoiding another shift/reduce-conflict.
1084 list :: { LHsExpr RdrName }
1085 : texp { L1 $ ExplicitList placeHolderType [$1] }
1086 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1087 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1088 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1089 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1090 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1091 | texp pquals { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1093 lexps :: { Located [LHsExpr RdrName] }
1094 : lexps ',' texp { LL ($3 : unLoc $1) }
1095 | texp ',' texp { LL [$3,$1] }
1097 -----------------------------------------------------------------------------
1098 -- List Comprehensions
1100 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1101 -- or a reversed list of Stmts
1102 : pquals1 { case unLoc $1 of
1104 qss -> L1 [L1 (ParStmt stmtss)]
1106 stmtss = [ (reverse qs, undefined)
1110 pquals1 :: { Located [[LStmt RdrName]] }
1111 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1112 | '|' quals { L (getLoc $2) [unLoc $2] }
1114 quals :: { Located [LStmt RdrName] }
1115 : quals ',' qual { LL ($3 : unLoc $1) }
1118 -----------------------------------------------------------------------------
1119 -- Parallel array expressions
1121 -- The rules below are little bit contorted; see the list case for details.
1122 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1123 -- Moreover, we allow explicit arrays with no element (represented by the nil
1124 -- constructor in the list case).
1126 parr :: { LHsExpr RdrName }
1127 : { noLoc (ExplicitPArr placeHolderType []) }
1128 | exp { L1 $ ExplicitPArr placeHolderType [$1] }
1129 | lexps { L1 $ ExplicitPArr placeHolderType
1130 (reverse (unLoc $1)) }
1131 | exp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1132 | exp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1133 | exp pquals { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1135 -- We are reusing `lexps' and `pquals' from the list case.
1137 -----------------------------------------------------------------------------
1138 -- Case alternatives
1140 altslist :: { Located [LMatch RdrName] }
1141 : '{' alts '}' { LL (reverse (unLoc $2)) }
1142 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1144 alts :: { Located [LMatch RdrName] }
1145 : alts1 { L1 (unLoc $1) }
1146 | ';' alts { LL (unLoc $2) }
1148 alts1 :: { Located [LMatch RdrName] }
1149 : alts1 ';' alt { LL ($3 : unLoc $1) }
1150 | alts1 ';' { LL (unLoc $1) }
1153 alt :: { LMatch RdrName }
1154 : infixexp opt_sig alt_rhs {% checkPattern $1 >>= \p ->
1155 return (LL (Match [p] $2 (unLoc $3))) }
1156 | '!' infixexp opt_sig alt_rhs {% checkPattern $2 >>= \p ->
1157 return (LL (Match [LL $ BangPat p] $3 (unLoc $4))) }
1159 alt_rhs :: { Located (GRHSs RdrName) }
1160 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1162 ralt :: { Located [LGRHS RdrName] }
1163 : '->' exp { LL (unguardedRHS $2) }
1164 | gdpats { L1 (reverse (unLoc $1)) }
1166 gdpats :: { Located [LGRHS RdrName] }
1167 : gdpats gdpat { LL ($2 : unLoc $1) }
1170 gdpat :: { LGRHS RdrName }
1171 : '|' quals '->' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1173 -----------------------------------------------------------------------------
1174 -- Statement sequences
1176 stmtlist :: { Located [LStmt RdrName] }
1177 : '{' stmts '}' { LL (unLoc $2) }
1178 | vocurly stmts close { $2 }
1180 -- do { ;; s ; s ; ; s ;; }
1181 -- The last Stmt should be an expression, but that's hard to enforce
1182 -- here, because we need too much lookahead if we see do { e ; }
1183 -- So we use ExprStmts throughout, and switch the last one over
1184 -- in ParseUtils.checkDo instead
1185 stmts :: { Located [LStmt RdrName] }
1186 : stmt stmts_help { LL ($1 : unLoc $2) }
1187 | ';' stmts { LL (unLoc $2) }
1188 | {- empty -} { noLoc [] }
1190 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1191 : ';' stmts { LL (unLoc $2) }
1192 | {- empty -} { noLoc [] }
1194 -- For typing stmts at the GHCi prompt, where
1195 -- the input may consist of just comments.
1196 maybe_stmt :: { Maybe (LStmt RdrName) }
1198 | {- nothing -} { Nothing }
1200 stmt :: { LStmt RdrName }
1202 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1203 return (LL $ mkBindStmt p $1) }
1204 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1206 qual :: { LStmt RdrName }
1207 : exp '<-' exp {% checkPattern $1 >>= \p ->
1208 return (LL $ mkBindStmt p $3) }
1209 | exp { L1 $ mkExprStmt $1 }
1210 | 'let' binds { LL $ LetStmt (unLoc $2) }
1212 -----------------------------------------------------------------------------
1213 -- Record Field Update/Construction
1215 fbinds :: { HsRecordBinds RdrName }
1217 | {- empty -} { [] }
1219 fbinds1 :: { HsRecordBinds RdrName }
1220 : fbinds1 ',' fbind { $3 : $1 }
1223 fbind :: { (Located RdrName, LHsExpr RdrName) }
1224 : qvar '=' exp { ($1,$3) }
1226 -----------------------------------------------------------------------------
1227 -- Implicit Parameter Bindings
1229 dbinds :: { Located [LIPBind RdrName] }
1230 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1231 | dbinds ';' { LL (unLoc $1) }
1233 -- | {- empty -} { [] }
1235 dbind :: { LIPBind RdrName }
1236 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1238 ipvar :: { Located (IPName RdrName) }
1239 : IPDUPVARID { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1240 | IPSPLITVARID { L1 (Linear (mkUnqual varName (getIPSPLITVARID $1))) }
1242 -----------------------------------------------------------------------------
1245 depreclist :: { Located [RdrName] }
1246 depreclist : deprec_var { L1 [unLoc $1] }
1247 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1249 deprec_var :: { Located RdrName }
1250 deprec_var : var { $1 }
1253 -----------------------------------------
1254 -- Data constructors
1255 qcon :: { Located RdrName }
1257 | '(' qconsym ')' { LL (unLoc $2) }
1258 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1259 -- The case of '[:' ':]' is part of the production `parr'
1261 con :: { Located RdrName }
1263 | '(' consym ')' { LL (unLoc $2) }
1264 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1266 sysdcon :: { Located DataCon } -- Wired in data constructors
1267 : '(' ')' { LL unitDataCon }
1268 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1269 | '[' ']' { LL nilDataCon }
1271 conop :: { Located RdrName }
1273 | '`' conid '`' { LL (unLoc $2) }
1275 qconop :: { Located RdrName }
1277 | '`' qconid '`' { LL (unLoc $2) }
1279 -----------------------------------------------------------------------------
1280 -- Type constructors
1282 gtycon :: { Located RdrName } -- A "general" qualified tycon
1284 | '(' ')' { LL $ getRdrName unitTyCon }
1285 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1286 | '(' '->' ')' { LL $ getRdrName funTyCon }
1287 | '[' ']' { LL $ listTyCon_RDR }
1288 | '[:' ':]' { LL $ parrTyCon_RDR }
1290 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1292 | '(' qtyconsym ')' { LL (unLoc $2) }
1294 qtyconop :: { Located RdrName } -- Qualified or unqualified
1296 | '`' qtycon '`' { LL (unLoc $2) }
1298 qtycon :: { Located RdrName } -- Qualified or unqualified
1299 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1302 tycon :: { Located RdrName } -- Unqualified
1303 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1305 qtyconsym :: { Located RdrName }
1306 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1309 tyconsym :: { Located RdrName }
1310 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1312 -----------------------------------------------------------------------------
1315 op :: { Located RdrName } -- used in infix decls
1319 varop :: { Located RdrName }
1321 | '`' varid '`' { LL (unLoc $2) }
1323 qop :: { LHsExpr RdrName } -- used in sections
1324 : qvarop { L1 $ HsVar (unLoc $1) }
1325 | qconop { L1 $ HsVar (unLoc $1) }
1327 qopm :: { LHsExpr RdrName } -- used in sections
1328 : qvaropm { L1 $ HsVar (unLoc $1) }
1329 | qconop { L1 $ HsVar (unLoc $1) }
1331 qvarop :: { Located RdrName }
1333 | '`' qvarid '`' { LL (unLoc $2) }
1335 qvaropm :: { Located RdrName }
1336 : qvarsym_no_minus { $1 }
1337 | '`' qvarid '`' { LL (unLoc $2) }
1339 -----------------------------------------------------------------------------
1342 tyvar :: { Located RdrName }
1343 tyvar : tyvarid { $1 }
1344 | '(' tyvarsym ')' { LL (unLoc $2) }
1346 tyvarop :: { Located RdrName }
1347 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1350 tyvarid :: { Located RdrName }
1351 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1352 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1353 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1354 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1355 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1357 tyvarsym :: { Located RdrName }
1358 -- Does not include "!", because that is used for strictness marks
1359 -- or ".", because that separates the quantified type vars from the rest
1360 -- or "*", because that's used for kinds
1361 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1363 -----------------------------------------------------------------------------
1366 var :: { Located RdrName }
1368 | '(' varsym ')' { LL (unLoc $2) }
1370 qvar :: { Located RdrName }
1372 | '(' varsym ')' { LL (unLoc $2) }
1373 | '(' qvarsym1 ')' { LL (unLoc $2) }
1374 -- We've inlined qvarsym here so that the decision about
1375 -- whether it's a qvar or a var can be postponed until
1376 -- *after* we see the close paren.
1378 qvarid :: { Located RdrName }
1380 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1382 varid :: { Located RdrName }
1383 : varid_no_unsafe { $1 }
1384 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1385 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1386 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1388 varid_no_unsafe :: { Located RdrName }
1389 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1390 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1391 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1393 qvarsym :: { Located RdrName }
1397 qvarsym_no_minus :: { Located RdrName }
1398 : varsym_no_minus { $1 }
1401 qvarsym1 :: { Located RdrName }
1402 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1404 varsym :: { Located RdrName }
1405 : varsym_no_minus { $1 }
1406 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1408 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1409 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1410 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1413 -- These special_ids are treated as keywords in various places,
1414 -- but as ordinary ids elsewhere. 'special_id' collects all these
1415 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1416 special_id :: { Located FastString }
1418 : 'as' { L1 FSLIT("as") }
1419 | 'qualified' { L1 FSLIT("qualified") }
1420 | 'hiding' { L1 FSLIT("hiding") }
1421 | 'export' { L1 FSLIT("export") }
1422 | 'label' { L1 FSLIT("label") }
1423 | 'dynamic' { L1 FSLIT("dynamic") }
1424 | 'stdcall' { L1 FSLIT("stdcall") }
1425 | 'ccall' { L1 FSLIT("ccall") }
1426 | 'iso' { L1 FSLIT("iso") }
1428 special_sym :: { Located FastString }
1429 special_sym : '!' { L1 FSLIT("!") }
1430 | '.' { L1 FSLIT(".") }
1431 | '*' { L1 FSLIT("*") }
1433 -----------------------------------------------------------------------------
1434 -- Data constructors
1436 qconid :: { Located RdrName } -- Qualified or unqualified
1438 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1440 conid :: { Located RdrName }
1441 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1443 qconsym :: { Located RdrName } -- Qualified or unqualified
1445 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1447 consym :: { Located RdrName }
1448 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1450 -- ':' means only list cons
1451 | ':' { L1 $ consDataCon_RDR }
1454 -----------------------------------------------------------------------------
1457 literal :: { Located HsLit }
1458 : CHAR { L1 $ HsChar $ getCHAR $1 }
1459 | STRING { L1 $ HsString $ getSTRING $1 }
1460 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1461 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1462 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1463 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1464 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1466 -----------------------------------------------------------------------------
1470 : vccurly { () } -- context popped in lexer.
1471 | error {% popContext }
1473 -----------------------------------------------------------------------------
1474 -- Miscellaneous (mostly renamings)
1476 modid :: { Located ModuleName }
1477 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1478 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1481 (unpackFS mod ++ '.':unpackFS c))
1485 : commas ',' { $1 + 1 }
1488 -----------------------------------------------------------------------------
1492 happyError = srcParseFail
1494 getVARID (L _ (ITvarid x)) = x
1495 getCONID (L _ (ITconid x)) = x
1496 getVARSYM (L _ (ITvarsym x)) = x
1497 getCONSYM (L _ (ITconsym x)) = x
1498 getQVARID (L _ (ITqvarid x)) = x
1499 getQCONID (L _ (ITqconid x)) = x
1500 getQVARSYM (L _ (ITqvarsym x)) = x
1501 getQCONSYM (L _ (ITqconsym x)) = x
1502 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1503 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1504 getCHAR (L _ (ITchar x)) = x
1505 getSTRING (L _ (ITstring x)) = x
1506 getINTEGER (L _ (ITinteger x)) = x
1507 getRATIONAL (L _ (ITrational x)) = x
1508 getPRIMCHAR (L _ (ITprimchar x)) = x
1509 getPRIMSTRING (L _ (ITprimstring x)) = x
1510 getPRIMINTEGER (L _ (ITprimint x)) = x
1511 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1512 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1513 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1514 getINLINE (L _ (ITinline_prag b)) = b
1515 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1517 -- Utilities for combining source spans
1518 comb2 :: Located a -> Located b -> SrcSpan
1521 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1522 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1524 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1525 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1526 combineSrcSpans (getLoc c) (getLoc d)
1528 -- strict constructor version:
1530 sL :: SrcSpan -> a -> Located a
1531 sL span a = span `seq` L span a
1533 -- Make a source location for the file. We're a bit lazy here and just
1534 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1535 -- try to find the span of the whole file (ToDo).
1536 fileSrcSpan :: P SrcSpan
1539 let loc = mkSrcLoc (srcLocFile l) 1 0;
1540 return (mkSrcSpan loc loc)