2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.89 2002/02/13 15:19:19 simonpj Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parseModule, parseStmt, parseIdentifier ) where
15 import HsTypes ( mkHsTupCon )
21 import PrelNames ( mAIN_Name, unitTyCon_RDR, funTyCon_RDR,
22 listTyCon_RDR, parrTyCon_RDR, tupleTyCon_RDR,
23 unitCon_RDR, nilCon_RDR, tupleCon_RDR )
24 import ForeignCall ( Safety(..), CExportSpec(..),
25 CCallConv(..), CCallTarget(..), defaultCCallConv,
27 import OccName ( UserFS, varName, tcName, dataName, tcClsName, tvName )
28 import TyCon ( DataConDetails(..) )
29 import SrcLoc ( SrcLoc )
31 import CmdLineOpts ( opt_SccProfilingOn )
32 import Type ( Kind, mkArrowKind, liftedTypeKind )
33 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
34 NewOrData(..), StrictnessMark(..), Activation(..) )
38 import CStrings ( CLabelString )
40 import Maybes ( orElse )
43 #include "HsVersions.h"
47 -----------------------------------------------------------------------------
48 Conflicts: 21 shift/reduce, -=chak[4Feb2]
50 9 for abiguity in 'if x then y else z + 1'
51 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
52 8 because op might be: - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
53 1 for ambiguity in 'if x then y else z :: T'
54 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
55 1 for ambiguity in 'if x then y else z with ?x=3'
56 (shift parses as 'if x then y else (z with ?x=3)'
58 3 for ambiguity in 'case x of y :: a -> b'
59 (don't know whether to reduce 'a' as a btype or shift the '->'.
60 conclusion: bogus expression anyway, doesn't matter)
62 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
63 since 'forall' is a valid variable name, we don't know whether
64 to treat a forall on the input as the beginning of a quantifier
65 or the beginning of the rule itself. Resolving to shift means
66 it's always treated as a quantifier, hence the above is disallowed.
67 This saves explicitly defining a grammar for the rule lhs that
68 doesn't include 'forall'.
70 1 for ambiguity in 'x @ Rec{..}'.
71 Only sensible parse is 'x @ (Rec{..})', which is what resolving
74 6 for conflicts between `fdecl' and `fdeclDEPRECATED', which are resolved
75 correctly, and moreover, should go away when `fdeclDEPRECATED' is removed.
77 -----------------------------------------------------------------------------
81 '_' { ITunderscore } -- Haskell keywords
86 'default' { ITdefault }
87 'deriving' { ITderiving }
97 'instance' { ITinstance }
100 'newtype' { ITnewtype }
102 'qualified' { ITqualified }
106 '_scc_' { ITscc } -- ToDo: remove
108 'forall' { ITforall } -- GHC extension keywords
109 'foreign' { ITforeign }
110 'export' { ITexport }
112 'dynamic' { ITdynamic }
114 'unsafe' { ITunsafe }
116 'stdcall' { ITstdcallconv }
117 'ccall' { ITccallconv }
118 'dotnet' { ITdotnet }
119 '_ccall_' { ITccall (False, False, PlayRisky) }
120 '_ccall_GC_' { ITccall (False, False, PlaySafe) }
121 '_casm_' { ITccall (False, True, PlayRisky) }
122 '_casm_GC_' { ITccall (False, True, PlaySafe) }
124 '{-# SPECIALISE' { ITspecialise_prag }
125 '{-# SOURCE' { ITsource_prag }
126 '{-# INLINE' { ITinline_prag }
127 '{-# NOINLINE' { ITnoinline_prag }
128 '{-# RULES' { ITrules_prag }
129 '{-# SCC' { ITscc_prag }
130 '{-# DEPRECATED' { ITdeprecated_prag }
131 '#-}' { ITclose_prag }
134 '__interface' { ITinterface } -- interface keywords
135 '__export' { IT__export }
136 '__instimport' { ITinstimport }
137 '__forall' { IT__forall }
138 '__letrec' { ITletrec }
139 '__coerce' { ITcoerce }
140 '__depends' { ITdepends }
141 '__inline' { ITinline }
142 '__DEFAULT' { ITdefaultbranch }
144 '__integer' { ITinteger_lit }
145 '__float' { ITfloat_lit }
146 '__rational' { ITrational_lit }
147 '__addr' { ITaddr_lit }
148 '__label' { ITlabel_lit }
149 '__litlit' { ITlit_lit }
150 '__string' { ITstring_lit }
151 '__ccall' { ITccall $$ }
153 '__sccC' { ITsccAllCafs }
156 '__P' { ITspecialise }
159 '__S' { ITstrict $$ }
160 '__M' { ITcprinfo $$ }
163 '..' { ITdotdot } -- reserved symbols
178 '{' { ITocurly } -- special symbols
182 vccurly { ITvccurly } -- virtual close curly (from layout)
195 VARID { ITvarid $$ } -- identifiers
197 VARSYM { ITvarsym $$ }
198 CONSYM { ITconsym $$ }
199 QVARID { ITqvarid $$ }
200 QCONID { ITqconid $$ }
201 QVARSYM { ITqvarsym $$ }
202 QCONSYM { ITqconsym $$ }
204 IPDUPVARID { ITdupipvarid $$ } -- GHC extension
205 IPSPLITVARID { ITsplitipvarid $$ } -- GHC extension
208 STRING { ITstring $$ }
209 INTEGER { ITinteger $$ }
210 RATIONAL { ITrational $$ }
212 PRIMCHAR { ITprimchar $$ }
213 PRIMSTRING { ITprimstring $$ }
214 PRIMINTEGER { ITprimint $$ }
215 PRIMFLOAT { ITprimfloat $$ }
216 PRIMDOUBLE { ITprimdouble $$ }
217 CLITLIT { ITlitlit $$ }
219 %monad { P } { thenP } { returnP }
220 %lexer { lexer } { ITeof }
221 %name parseModule module
222 %name parseStmt maybe_stmt
223 %name parseIdentifier identifier
227 -----------------------------------------------------------------------------
230 -- The place for module deprecation is really too restrictive, but if it
231 -- was allowed at its natural place just before 'module', we get an ugly
232 -- s/r conflict with the second alternative. Another solution would be the
233 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
234 -- either, and DEPRECATED is only expected to be used by people who really
235 -- know what they are doing. :-)
237 module :: { RdrNameHsModule }
238 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
239 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
241 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
243 maybemoddeprec :: { Maybe DeprecTxt }
244 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
245 | {- empty -} { Nothing }
247 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
249 | layout_on top close { $2 }
251 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
252 : importdecls { (reverse $1,[]) }
253 | importdecls ';' cvtopdecls { (reverse $1,$3) }
254 | cvtopdecls { ([],$1) }
256 cvtopdecls :: { [RdrNameHsDecl] }
257 : topdecls { cvTopDecls (groupBindings $1)}
259 -----------------------------------------------------------------------------
262 maybeexports :: { Maybe [RdrNameIE] }
263 : '(' exportlist ')' { Just $2 }
264 | {- empty -} { Nothing }
266 exportlist :: { [RdrNameIE] }
267 : exportlist ',' export { $3 : $1 }
268 | exportlist ',' { $1 }
272 -- GHC extension: we allow things like [] and (,,,) to be exported
273 export :: { RdrNameIE }
275 | gtycon { IEThingAbs $1 }
276 | gtycon '(' '..' ')' { IEThingAll $1 }
277 | gtycon '(' ')' { IEThingWith $1 [] }
278 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
279 | 'module' modid { IEModuleContents $2 }
281 qcnames :: { [RdrName] }
282 : qcnames ',' qcname { $3 : $1 }
285 qcname :: { RdrName }
289 -----------------------------------------------------------------------------
290 -- Import Declarations
292 -- import decls can be *empty*, or even just a string of semicolons
293 -- whereas topdecls must contain at least one topdecl.
295 importdecls :: { [RdrNameImportDecl] }
296 : importdecls ';' importdecl { $3 : $1 }
297 | importdecls ';' { $1 }
298 | importdecl { [ $1 ] }
301 importdecl :: { RdrNameImportDecl }
302 : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec
303 { ImportDecl $5 $3 $4 $6 $7 $2 }
305 maybe_src :: { WhereFrom }
306 : '{-# SOURCE' '#-}' { ImportByUserSource }
307 | {- empty -} { ImportByUser }
309 optqualified :: { Bool }
310 : 'qualified' { True }
311 | {- empty -} { False }
313 maybeas :: { Maybe ModuleName }
314 : 'as' modid { Just $2 }
315 | {- empty -} { Nothing }
317 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
318 : impspec { Just $1 }
319 | {- empty -} { Nothing }
321 impspec :: { (Bool, [RdrNameIE]) }
322 : '(' exportlist ')' { (False, reverse $2) }
323 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
325 -----------------------------------------------------------------------------
326 -- Fixity Declarations
330 | INTEGER {% checkPrec $1 `thenP_`
331 returnP (fromInteger $1) }
333 infix :: { FixityDirection }
335 | 'infixl' { InfixL }
336 | 'infixr' { InfixR }
339 : ops ',' op { $3 : $1 }
342 -----------------------------------------------------------------------------
343 -- Top-Level Declarations
345 topdecls :: { [RdrBinding] }
346 : topdecls ';' topdecl { ($3 : $1) }
347 | topdecls ';' { $1 }
350 topdecl :: { RdrBinding }
351 : srcloc 'type' tycon tv_bndrs '=' ctype
352 -- Note ctype, not sigtype.
353 -- We allow an explicit for-all but we don't insert one
354 -- in type Foo a = (b,b)
355 -- Instead we just say b is out of scope
356 { RdrHsDecl (TyClD (TySynonym $3 $4 $6 $1)) }
359 | srcloc 'data' tycl_hdr constrs deriving
360 {% returnP (RdrHsDecl (TyClD
361 (mkTyData DataType $3 (DataCons (reverse $4)) $5 $1))) }
363 | srcloc 'newtype' tycl_hdr '=' newconstr deriving
364 {% returnP (RdrHsDecl (TyClD
365 (mkTyData NewType $3 (DataCons [$5]) $6 $1))) }
367 | srcloc 'class' tycl_hdr fds where
369 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
371 returnP (RdrHsDecl (TyClD
372 (mkClassDecl $3 $4 sigs (Just binds) $1))) }
374 | srcloc 'instance' inst_type where
376 = cvMonoBindsAndSigs cvInstDeclSig
378 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
380 | srcloc 'default' '(' comma_types0 ')' { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
381 | 'foreign' fdecl { RdrHsDecl $2 }
382 | '{-# DEPRECATED' deprecations '#-}' { $2 }
383 | '{-# RULES' rules '#-}' { $2 }
386 -- tycl_hdr parses the header of a type or class decl,
387 -- which takes the form
390 -- (Eq a, Ord b) => T a b
391 -- Rather a lot of inlining here, else we get reduce/reduce errors
392 tycl_hdr :: { (RdrNameContext, RdrName, [RdrNameHsTyVar]) }
393 : '(' comma_types1 ')' '=>' gtycon tv_bndrs {% mapP checkPred $2 `thenP` \ cxt ->
394 returnP (cxt, $5, $6) }
395 -- qtycon for the class below name would lead to many s/r conflicts
396 -- FIXME: does the renamer pick up all wrong forms and raise an
398 | gtycon atypes1 '=>' gtycon atypes0 {% checkTyVars $5 `thenP` \ tvs ->
399 returnP ([HsClassP $1 $2], $4, tvs) }
400 | gtycon atypes0 {% checkTyVars $2 `thenP` \ tvs ->
401 returnP ([], $1, tvs) }
402 -- We have to have qtycon in this production to avoid s/r
403 -- conflicts with the previous one. The renamer will complain
404 -- if we use a qualified tycon.
406 -- Using a `gtycon' throughout. This enables special syntax,
407 -- such as "[]" for tycons as well as tycon ops in
408 -- parentheses. This is beyond H98, but used repeatedly in
409 -- the Prelude modules. (So, it would be a good idea to raise
410 -- an error in the renamer if some non-H98 form is used and
411 -- -fglasgow-exts is not given.) -=chak
413 decls :: { [RdrBinding] }
414 : decls ';' decl { $3 : $1 }
419 decl :: { RdrBinding }
422 | '{-# INLINE' srcloc activation qvar '#-}' { RdrSig (InlineSig True $4 $3 $2) }
423 | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
424 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
425 { foldr1 RdrAndBindings
426 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
427 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
428 { RdrSig (SpecInstSig $4 $2) }
430 wherebinds :: { RdrNameHsBinds }
431 : where { cvBinds cvValSig (groupBindings $1) }
433 where :: { [RdrBinding] }
434 : 'where' decllist { $2 }
437 declbinds :: { RdrNameHsBinds }
438 : decllist { cvBinds cvValSig (groupBindings $1) }
440 decllist :: { [RdrBinding] }
441 : '{' decls '}' { $2 }
442 | layout_on decls close { $2 }
444 fixdecl :: { RdrBinding }
445 : srcloc infix prec ops { foldr1 RdrAndBindings
446 [ RdrSig (FixSig (FixitySig n
450 -----------------------------------------------------------------------------
451 -- Transformation Rules
453 rules :: { RdrBinding }
454 : rules ';' rule { $1 `RdrAndBindings` $3 }
457 | {- empty -} { RdrNullBind }
459 rule :: { RdrBinding }
460 : STRING activation rule_forall infixexp '=' srcloc exp
461 { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
463 activation :: { Activation } -- Omitted means AlwaysActive
464 : {- empty -} { AlwaysActive }
465 | explicit_activation { $1 }
467 inverse_activation :: { Activation } -- Omitted means NeverActive
468 : {- empty -} { NeverActive }
469 | explicit_activation { $1 }
471 explicit_activation :: { Activation } -- In brackets
472 : '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
473 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger $3) }
475 rule_forall :: { [RdrNameRuleBndr] }
476 : 'forall' rule_var_list '.' { $2 }
479 rule_var_list :: { [RdrNameRuleBndr] }
481 | rule_var rule_var_list { $1 : $2 }
483 rule_var :: { RdrNameRuleBndr }
484 : varid { RuleBndr $1 }
485 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
487 -----------------------------------------------------------------------------
490 deprecations :: { RdrBinding }
491 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
492 | deprecations ';' { $1 }
494 | {- empty -} { RdrNullBind }
496 -- SUP: TEMPORARY HACK, not checking for `module Foo'
497 deprecation :: { RdrBinding }
498 : srcloc depreclist STRING
499 { foldr RdrAndBindings RdrNullBind
500 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
503 -----------------------------------------------------------------------------
504 -- Foreign import and export declarations
506 -- for the time being, the following accepts foreign declarations conforming
507 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
509 -- * a flag indicates whether pre-standard declarations have been used and
510 -- triggers a deprecation warning further down the road
512 -- NB: The first two rules could be combined into one by replacing `safety1'
513 -- with `safety'. However, the combined rule conflicts with the
516 fdecl :: { RdrNameHsDecl }
517 fdecl : srcloc 'import' callconv safety1 fspec {% mkImport $3 $4 $5 $1 }
518 | srcloc 'import' callconv fspec {% mkImport $3 PlaySafe $4 $1 }
519 | srcloc 'export' callconv fspec {% mkExport $3 $4 $1 }
520 -- the following syntax is DEPRECATED
521 | srcloc fdecl1DEPRECATED { ForD ($2 True $1) }
522 | srcloc fdecl2DEPRECATED { $2 $1 }
524 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
526 ----------- DEPRECATED label decls ------------
527 : 'label' ext_name varid '::' sigtype
528 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
529 (CLabel ($2 `orElse` mkExtName $3))) }
531 ----------- DEPRECATED ccall/stdcall decls ------------
533 -- NB: This business with the case expression below may seem overly
534 -- complicated, but it is necessary to avoid some conflicts.
536 -- DEPRECATED variant #1: lack of a calling convention specification
538 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
540 target = StaticTarget ($2 `orElse` mkExtName $4)
542 ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
543 (CFunction target)) }
545 -- DEPRECATED variant #2: external name consists of two separate strings
546 -- (module name and function name) (import)
547 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
549 DNCall -> parseError "Illegal format of .NET foreign import"
550 CCall cconv -> returnP $
552 imp = CFunction (StaticTarget $4)
554 ForeignImport $6 $8 (CImport cconv $5 _NIL_ _NIL_ imp) }
556 -- DEPRECATED variant #3: `unsafe' after entity
557 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
559 DNCall -> parseError "Illegal format of .NET foreign import"
560 CCall cconv -> returnP $
562 imp = CFunction (StaticTarget $3)
564 ForeignImport $5 $7 (CImport cconv PlayRisky _NIL_ _NIL_ imp) }
566 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
567 -- an explicit calling convention (import)
568 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
569 { ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
570 (CFunction DynamicTarget)) }
572 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
573 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
575 DNCall -> parseError "Illegal format of .NET foreign import"
576 CCall cconv -> returnP $
577 ForeignImport $5 $7 (CImport cconv $4 _NIL_ _NIL_
578 (CFunction DynamicTarget)) }
580 -- DEPRECATED variant #6: lack of a calling convention specification
582 | 'export' {-no callconv-} ext_name varid '::' sigtype
583 { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3)
586 -- DEPRECATED variant #7: external name consists of two separate strings
587 -- (module name and function name) (export)
588 | 'export' callconv STRING STRING varid '::' sigtype
590 DNCall -> parseError "Illegal format of .NET foreign import"
591 CCall cconv -> returnP $
593 (CExport (CExportStatic $4 cconv)) }
595 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
596 -- an explicit calling convention (export)
597 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
598 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
601 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
602 | 'export' callconv 'dynamic' varid '::' sigtype
604 DNCall -> parseError "Illegal format of .NET foreign import"
605 CCall cconv -> returnP $
606 ForeignImport $4 $6 (CImport cconv PlaySafe _NIL_ _NIL_ CWrapper) }
608 ----------- DEPRECATED .NET decls ------------
609 -- NB: removed the .NET call declaration, as it is entirely subsumed
610 -- by the new standard FFI declarations
612 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
614 : 'import' 'dotnet' 'type' ext_name tycon
615 { \loc -> TyClD (ForeignType $5 $4 DNType loc) }
616 -- left this one unchanged for the moment as type imports are not
617 -- covered currently by the FFI standard -=chak
620 callconv :: { CallConv }
621 : 'stdcall' { CCall StdCallConv }
622 | 'ccall' { CCall CCallConv }
623 | 'dotnet' { DNCall }
626 : 'unsafe' { PlayRisky }
627 | 'safe' { PlaySafe }
628 | {- empty -} { PlaySafe }
630 safety1 :: { Safety }
631 : 'unsafe' { PlayRisky }
632 | 'safe' { PlaySafe }
633 -- only needed to avoid conflicts with the DEPRECATED rules
635 fspec :: { (FAST_STRING, RdrName, RdrNameHsType) }
636 : STRING varid '::' sigtype { ($1 , $2, $4) }
637 | varid '::' sigtype { (SLIT(""), $1, $3) }
638 -- if the entity string is missing, it defaults to the empty string;
639 -- the meaning of an empty entity string depends on the calling
643 ext_name :: { Maybe CLabelString }
645 | STRING STRING { Just $2 } -- Ignore "module name" for now
646 | {- empty -} { Nothing }
649 -----------------------------------------------------------------------------
652 opt_sig :: { Maybe RdrNameHsType }
653 : {- empty -} { Nothing }
654 | '::' sigtype { Just $2 }
656 opt_asig :: { Maybe RdrNameHsType }
657 : {- empty -} { Nothing }
658 | '::' atype { Just $2 }
660 sigtypes :: { [RdrNameHsType] }
662 | sigtypes ',' sigtype { $3 : $1 }
664 sigtype :: { RdrNameHsType }
665 : ctype { mkHsForAllTy Nothing [] $1 }
667 sig_vars :: { [RdrName] }
668 : sig_vars ',' var { $3 : $1 }
671 -----------------------------------------------------------------------------
674 -- A ctype is a for-all type
675 ctype :: { RdrNameHsType }
676 : 'forall' tv_bndrs '.' ctype { mkHsForAllTy (Just $2) [] $4 }
677 | context '=>' type { mkHsForAllTy Nothing $1 $3 }
678 -- A type of form (context => type) is an *implicit* HsForAllTy
681 -- We parse a context as a btype so that we don't get reduce/reduce
682 -- errors in ctype. The basic problem is that
684 -- looks so much like a tuple type. We can't tell until we find the =>
685 context :: { RdrNameContext }
686 : btype {% checkContext $1 }
688 type :: { RdrNameHsType }
689 : gentype '->' type { HsFunTy $1 $3 }
690 | ipvar '::' type { mkHsIParamTy $1 $3 }
693 gentype :: { RdrNameHsType }
696 | atype tyconop atype { HsOpTy $1 $2 $3 }
698 btype :: { RdrNameHsType }
699 : btype atype { HsAppTy $1 $2 }
702 atype :: { RdrNameHsType }
703 : gtycon { HsTyVar $1 }
704 | tyvar { HsTyVar $1 }
705 | '(' type ',' comma_types1 ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2:$4) }
706 | '(#' comma_types1 '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
707 | '[' type ']' { HsListTy $2 }
708 | '[:' type ':]' { HsPArrTy $2 }
709 | '(' ctype ')' { $2 }
710 | '(' ctype '::' kind ')' { HsKindSig $2 $4 }
712 | INTEGER { HsNumTy $1 }
714 -- An inst_type is what occurs in the head of an instance decl
715 -- e.g. (Foo a, Gaz b) => Wibble a b
716 -- It's kept as a single type, with a MonoDictTy at the right
717 -- hand corner, for convenience.
718 inst_type :: { RdrNameHsType }
719 : ctype {% checkInstType $1 }
721 comma_types0 :: { [RdrNameHsType] }
722 : comma_types1 { $1 }
725 comma_types1 :: { [RdrNameHsType] }
727 | type ',' comma_types1 { $1 : $3 }
729 atypes0 :: { [RdrNameHsType] }
733 atypes1 :: { [RdrNameHsType] }
735 | atype atypes1 { $1 : $2 }
737 tv_bndrs :: { [RdrNameHsTyVar] }
738 : tv_bndr tv_bndrs { $1 : $2 }
741 tv_bndr :: { RdrNameHsTyVar }
742 : tyvar { UserTyVar $1 }
743 | '(' tyvar '::' kind ')' { IfaceTyVar $2 $4 }
745 fds :: { [([RdrName], [RdrName])] }
747 | '|' fds1 { reverse $2 }
749 fds1 :: { [([RdrName], [RdrName])] }
750 : fds1 ',' fd { $3 : $1 }
753 fd :: { ([RdrName], [RdrName]) }
754 : varids0 '->' varids0 { (reverse $1, reverse $3) }
756 varids0 :: { [RdrName] }
758 | varids0 tyvar { $2 : $1 }
760 -----------------------------------------------------------------------------
765 | akind '->' kind { mkArrowKind $1 $3 }
768 : '*' { liftedTypeKind }
769 | '(' kind ')' { $2 }
772 -----------------------------------------------------------------------------
773 -- Datatype declarations
775 newconstr :: { RdrNameConDecl }
776 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
777 | srcloc conid '{' var '::' ctype '}'
778 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
780 constrs :: { [RdrNameConDecl] }
781 : {- empty; a GHC extension -} { [] }
782 | '=' constrs1 { $2 }
784 constrs1 :: { [RdrNameConDecl] }
785 : constrs1 '|' constr { $3 : $1 }
788 constr :: { RdrNameConDecl }
789 : srcloc forall context '=>' constr_stuff
790 { mkConDecl (fst $5) $2 $3 (snd $5) $1 }
791 | srcloc forall constr_stuff
792 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
794 forall :: { [RdrNameHsTyVar] }
795 : 'forall' tv_bndrs '.' { $2 }
798 constr_stuff :: { (RdrName, RdrNameConDetails) }
799 : btype {% mkVanillaCon $1 [] }
800 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
801 | gtycon '{' '}' {% mkRecCon $1 [] }
802 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
803 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
805 satypes :: { [RdrNameBangType] }
806 : atype satypes { unbangedType $1 : $2 }
807 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
810 sbtype :: { RdrNameBangType }
811 : btype { unbangedType $1 }
812 | '!' atype { BangType MarkedUserStrict $2 }
814 fielddecls :: { [([RdrName],RdrNameBangType)] }
815 : fielddecl ',' fielddecls { $1 : $3 }
818 fielddecl :: { ([RdrName],RdrNameBangType) }
819 : sig_vars '::' stype { (reverse $1, $3) }
821 stype :: { RdrNameBangType }
822 : ctype { unbangedType $1 }
823 | '!' atype { BangType MarkedUserStrict $2 }
825 deriving :: { Maybe RdrNameContext }
826 : {- empty -} { Nothing }
827 | 'deriving' context { Just $2 }
828 -- Glasgow extension: allow partial
829 -- applications in derivings
831 -----------------------------------------------------------------------------
834 {- There's an awkward overlap with a type signature. Consider
835 f :: Int -> Int = ...rhs...
836 Then we can't tell whether it's a type signature or a value
837 definition with a result signature until we see the '='.
838 So we have to inline enough to postpone reductions until we know.
842 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
843 instead of qvar, we get another shift/reduce-conflict. Consider the
846 { (^^) :: Int->Int ; } Type signature; only var allowed
848 { (^^) :: Int->Int = ... ; } Value defn with result signature;
849 qvar allowed (because of instance decls)
851 We can't tell whether to reduce var to qvar until after we've read the signatures.
854 valdef :: { RdrBinding }
855 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
856 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
857 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
858 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
862 rhs :: { RdrNameGRHSs }
863 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
864 | gdrhs wherebinds { GRHSs (reverse $1) $2 placeHolderType }
866 gdrhs :: { [RdrNameGRHS] }
867 : gdrhs gdrh { $2 : $1 }
870 gdrh :: { RdrNameGRHS }
871 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
873 -----------------------------------------------------------------------------
876 exp :: { RdrNameHsExpr }
877 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
878 | infixexp 'with' dbinding { HsWith $1 $3 }
881 infixexp :: { RdrNameHsExpr }
883 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
884 (panic "fixity") $3 )}
886 exp10 :: { RdrNameHsExpr }
887 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
888 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
889 returnP (HsLam (Match ps $5
890 (GRHSs (unguardedRHS $8 $7)
891 EmptyBinds placeHolderType))) }
892 | 'let' declbinds 'in' exp { HsLet $2 $4 }
893 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
894 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
895 | '-' fexp { mkHsNegApp $2 }
896 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
897 returnP (HsDo DoExpr stmts $1) }
899 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False placeHolderType }
900 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 PlaySafe False placeHolderType }
901 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True placeHolderType }
902 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 PlaySafe True placeHolderType }
904 | scc_annot exp { if opt_SccProfilingOn
910 scc_annot :: { FAST_STRING }
911 : '_scc_' STRING { $2 }
912 | '{-# SCC' STRING '#-}' { $2 }
914 ccallid :: { FAST_STRING }
918 fexp :: { RdrNameHsExpr }
919 : fexp aexp { (HsApp $1 $2) }
922 aexps0 :: { [RdrNameHsExpr] }
923 : aexps { reverse $1 }
925 aexps :: { [RdrNameHsExpr] }
926 : aexps aexp { $2 : $1 }
929 aexp :: { RdrNameHsExpr }
930 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
931 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
935 var_or_con :: { RdrNameHsExpr }
939 aexp1 :: { RdrNameHsExpr }
940 : ipvar { HsIPVar $1 }
942 | literal { HsLit $1 }
943 | INTEGER { HsOverLit (mkHsIntegral $1) }
944 | RATIONAL { HsOverLit (mkHsFractional $1) }
945 | '(' exp ')' { HsPar $2 }
946 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
947 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
948 | '[' list ']' { $2 }
949 | '[:' parr ':]' { $2 }
950 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
951 | '(' qopm infixexp ')' { (SectionR $2 $3) }
952 | qvar '@' aexp { EAsPat $1 $3 }
954 | '~' aexp1 { ELazyPat $2 }
956 texps :: { [RdrNameHsExpr] }
957 : texps ',' exp { $3 : $1 }
961 -----------------------------------------------------------------------------
964 -- The rules below are little bit contorted to keep lexps left-recursive while
965 -- avoiding another shift/reduce-conflict.
967 list :: { RdrNameHsExpr }
968 : exp { ExplicitList placeHolderType [$1] }
969 | lexps { ExplicitList placeHolderType (reverse $1) }
970 | exp '..' { ArithSeqIn (From $1) }
971 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
972 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
973 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
974 | exp srcloc pquals {% let { body [qs] = qs;
975 body qss = [ParStmt (map reverse qss)] }
977 returnP ( HsDo ListComp
978 (reverse (ResultStmt $1 $2 : body $3))
983 lexps :: { [RdrNameHsExpr] }
984 : lexps ',' exp { $3 : $1 }
985 | exp ',' exp { [$3,$1] }
987 -----------------------------------------------------------------------------
988 -- List Comprehensions
990 pquals :: { [[RdrNameStmt]] }
991 : pquals '|' quals { $3 : $1 }
994 quals :: { [RdrNameStmt] }
995 : quals ',' stmt { $3 : $1 }
998 -----------------------------------------------------------------------------
999 -- Parallel array expressions
1001 -- The rules below are little bit contorted; see the list case for details.
1002 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1003 -- Moreover, we allow explicit arrays with no element (represented by the nil
1004 -- constructor in the list case).
1006 parr :: { RdrNameHsExpr }
1007 : { ExplicitPArr placeHolderType [] }
1008 | exp { ExplicitPArr placeHolderType [$1] }
1009 | lexps { ExplicitPArr placeHolderType
1011 | exp '..' exp { PArrSeqIn (FromTo $1 $3) }
1012 | exp ',' exp '..' exp { PArrSeqIn (FromThenTo $1 $3 $5) }
1013 | exp srcloc pquals {% let {
1020 (reverse (ResultStmt $1 $2
1025 -- We are reusing `lexps' and `pquals' from the list case.
1027 -----------------------------------------------------------------------------
1028 -- Case alternatives
1030 altslist :: { [RdrNameMatch] }
1031 : '{' alts '}' { reverse $2 }
1032 | layout_on alts close { reverse $2 }
1034 alts :: { [RdrNameMatch] }
1038 alts1 :: { [RdrNameMatch] }
1039 : alts1 ';' alt { $3 : $1 }
1043 alt :: { RdrNameMatch }
1044 : srcloc infixexp opt_sig ralt wherebinds
1045 {% (checkPattern $1 $2 `thenP` \p ->
1046 returnP (Match [p] $3
1047 (GRHSs $4 $5 placeHolderType)) )}
1049 ralt :: { [RdrNameGRHS] }
1050 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
1051 | gdpats { reverse $1 }
1053 gdpats :: { [RdrNameGRHS] }
1054 : gdpats gdpat { $2 : $1 }
1057 gdpat :: { RdrNameGRHS }
1058 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
1060 -----------------------------------------------------------------------------
1061 -- Statement sequences
1063 stmtlist :: { [RdrNameStmt] }
1064 : '{' stmts '}' { $2 }
1065 | layout_on_for_do stmts close { $2 }
1067 -- do { ;; s ; s ; ; s ;; }
1068 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1069 -- here, because we need too much lookahead if we see do { e ; }
1070 -- So we use ExprStmts throughout, and switch the last one over
1071 -- in ParseUtils.checkDo instead
1072 stmts :: { [RdrNameStmt] }
1073 : stmt stmts_help { $1 : $2 }
1075 | {- empty -} { [] }
1077 stmts_help :: { [RdrNameStmt] }
1079 | {- empty -} { [] }
1081 -- For typing stmts at the GHCi prompt, where
1082 -- the input may consist of just comments.
1083 maybe_stmt :: { Maybe RdrNameStmt }
1085 | {- nothing -} { Nothing }
1087 stmt :: { RdrNameStmt }
1088 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
1089 returnP (BindStmt p $4 $1) }
1090 | srcloc exp { ExprStmt $2 placeHolderType $1 }
1091 | srcloc 'let' declbinds { LetStmt $3 }
1093 -----------------------------------------------------------------------------
1094 -- Record Field Update/Construction
1096 fbinds :: { RdrNameHsRecordBinds }
1097 : fbinds ',' fbind { $3 : $1 }
1100 | {- empty -} { [] }
1102 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
1103 : qvar '=' exp { ($1,$3,False) }
1105 -----------------------------------------------------------------------------
1106 -- Implicit Parameter Bindings
1108 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1109 : '{' dbinds '}' { $2 }
1110 | layout_on dbinds close { $2 }
1112 dbinds :: { [(IPName RdrName, RdrNameHsExpr)] }
1113 : dbinds ';' dbind { $3 : $1 }
1116 | {- empty -} { [] }
1118 dbind :: { (IPName RdrName, RdrNameHsExpr) }
1119 dbind : ipvar '=' exp { ($1, $3) }
1121 -----------------------------------------------------------------------------
1122 -- Variables, Constructors and Operators.
1124 identifier :: { RdrName }
1129 depreclist :: { [RdrName] }
1130 depreclist : deprec_var { [$1] }
1131 | deprec_var ',' depreclist { $1 : $3 }
1133 deprec_var :: { RdrName }
1134 deprec_var : var { $1 }
1137 gtycon :: { RdrName }
1139 | '(' qtyconop ')' { $2 }
1140 | '(' ')' { unitTyCon_RDR }
1141 | '(' '->' ')' { funTyCon_RDR }
1142 | '[' ']' { listTyCon_RDR }
1143 | '[:' ':]' { parrTyCon_RDR }
1144 | '(' commas ')' { tupleTyCon_RDR $2 }
1146 gcon :: { RdrName } -- Data constructor namespace
1147 : '(' ')' { unitCon_RDR }
1148 | '[' ']' { nilCon_RDR }
1149 | '(' commas ')' { tupleCon_RDR $2 }
1151 -- the case of '[:' ':]' is part of the production `parr'
1155 | '(' varsym ')' { $2 }
1159 | '(' varsym ')' { $2 }
1160 | '(' qvarsym1 ')' { $2 }
1161 -- We've inlined qvarsym here so that the decision about
1162 -- whether it's a qvar or a var can be postponed until
1163 -- *after* we see the close paren.
1165 ipvar :: { IPName RdrName }
1166 : IPDUPVARID { Dupable (mkUnqual varName $1) }
1167 | IPSPLITVARID { Linear (mkUnqual varName $1) }
1171 | '(' qconsym ')' { $2 }
1173 varop :: { RdrName }
1175 | '`' varid '`' { $2 }
1177 qvarop :: { RdrName }
1179 | '`' qvarid '`' { $2 }
1181 qvaropm :: { RdrName }
1182 : qvarsym_no_minus { $1 }
1183 | '`' qvarid '`' { $2 }
1185 conop :: { RdrName }
1187 | '`' conid '`' { $2 }
1189 qconop :: { RdrName }
1191 | '`' qconid '`' { $2 }
1193 -----------------------------------------------------------------------------
1196 op :: { RdrName } -- used in infix decls
1200 qop :: { RdrName {-HsExpr-} } -- used in sections
1204 qopm :: { RdrNameHsExpr } -- used in sections
1205 : qvaropm { HsVar $1 }
1206 | qconop { HsVar $1 }
1208 -----------------------------------------------------------------------------
1211 qvarid :: { RdrName }
1213 | QVARID { mkQual varName $1 }
1215 varid :: { RdrName }
1216 : varid_no_unsafe { $1 }
1217 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1219 varid_no_unsafe :: { RdrName }
1220 : VARID { mkUnqual varName $1 }
1221 | special_id { mkUnqual varName $1 }
1222 | 'forall' { mkUnqual varName SLIT("forall") }
1224 tyvar :: { RdrName }
1225 : VARID { mkUnqual tvName $1 }
1226 | special_id { mkUnqual tvName $1 }
1227 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1229 -- These special_ids are treated as keywords in various places,
1230 -- but as ordinary ids elsewhere. A special_id collects all thsee
1231 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1232 special_id :: { UserFS }
1234 : 'as' { SLIT("as") }
1235 | 'qualified' { SLIT("qualified") }
1236 | 'hiding' { SLIT("hiding") }
1237 | 'export' { SLIT("export") }
1238 | 'label' { SLIT("label") }
1239 | 'dynamic' { SLIT("dynamic") }
1240 | 'stdcall' { SLIT("stdcall") }
1241 | 'ccall' { SLIT("ccall") }
1243 -----------------------------------------------------------------------------
1246 qconid :: { RdrName } -- Qualified or unqualifiedb
1248 | QCONID { mkQual dataName $1 }
1250 conid :: { RdrName }
1251 : CONID { mkUnqual dataName $1 }
1253 -----------------------------------------------------------------------------
1256 qconsym :: { RdrName } -- Qualified or unqualifiedb
1258 | QCONSYM { mkQual dataName $1 }
1260 consym :: { RdrName }
1261 : CONSYM { mkUnqual dataName $1 }
1263 -----------------------------------------------------------------------------
1266 qvarsym :: { RdrName }
1270 qvarsym_no_minus :: { RdrName }
1271 : varsym_no_minus { $1 }
1274 qvarsym1 :: { RdrName }
1275 qvarsym1 : QVARSYM { mkQual varName $1 }
1277 varsym :: { RdrName }
1278 : varsym_no_minus { $1 }
1279 | '-' { mkUnqual varName SLIT("-") }
1281 varsym_no_minus :: { RdrName } -- varsym not including '-'
1282 : VARSYM { mkUnqual varName $1 }
1283 | special_sym { mkUnqual varName $1 }
1286 -- See comments with special_id
1287 special_sym :: { UserFS }
1288 special_sym : '!' { SLIT("!") }
1292 -----------------------------------------------------------------------------
1295 literal :: { HsLit }
1296 : CHAR { HsChar $1 }
1297 | STRING { HsString $1 }
1298 | PRIMINTEGER { HsIntPrim $1 }
1299 | PRIMCHAR { HsCharPrim $1 }
1300 | PRIMSTRING { HsStringPrim $1 }
1301 | PRIMFLOAT { HsFloatPrim $1 }
1302 | PRIMDOUBLE { HsDoublePrim $1 }
1303 | CLITLIT { HsLitLit $1 placeHolderType }
1305 srcloc :: { SrcLoc } : {% getSrcLocP }
1307 -----------------------------------------------------------------------------
1311 : vccurly { () } -- context popped in lexer.
1312 | error {% popContext }
1314 layout_on :: { () } : {% layoutOn True{-strict-} }
1315 layout_on_for_do :: { () } : {% layoutOn False }
1317 -----------------------------------------------------------------------------
1318 -- Miscellaneous (mostly renamings)
1320 modid :: { ModuleName }
1321 : CONID { mkModuleNameFS $1 }
1322 | QCONID { mkModuleNameFS
1324 (unpackFS (fst $1) ++
1325 '.':unpackFS (snd $1)))
1328 tycon :: { RdrName }
1329 : CONID { mkUnqual tcClsName $1 }
1331 tyconop :: { RdrName }
1332 : CONSYM { mkUnqual tcClsName $1 }
1334 qtycon :: { RdrName } -- Qualified or unqualified
1335 : QCONID { mkQual tcClsName $1 }
1338 qtyconop :: { RdrName } -- Qualified or unqualified
1339 : QCONSYM { mkQual tcClsName $1 }
1343 : commas ',' { $1 + 1 }
1346 -----------------------------------------------------------------------------
1350 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)