2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.86 2002/02/11 15:16:26 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(..), CCallSpec(..),
25 CCallConv(..), CCallTarget(..), defaultCCallConv,
27 import OccName ( UserFS, varName, tcName, dataName, tcClsName, tvName )
28 import SrcLoc ( SrcLoc )
30 import CmdLineOpts ( opt_SccProfilingOn )
31 import Type ( Kind, mkArrowKind, liftedTypeKind )
32 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
33 NewOrData(..), StrictnessMark(..), Activation(..) )
37 import CStrings ( CLabelString )
39 import Maybes ( orElse )
42 #include "HsVersions.h"
46 -----------------------------------------------------------------------------
47 Conflicts: 21 shift/reduce, -=chak[4Feb2]
49 9 for abiguity in 'if x then y else z + 1'
50 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
51 8 because op might be: - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
52 1 for ambiguity in 'if x then y else z :: T'
53 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
54 1 for ambiguity in 'if x then y else z with ?x=3'
55 (shift parses as 'if x then y else (z with ?x=3)'
57 3 for ambiguity in 'case x of y :: a -> b'
58 (don't know whether to reduce 'a' as a btype or shift the '->'.
59 conclusion: bogus expression anyway, doesn't matter)
61 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
62 since 'forall' is a valid variable name, we don't know whether
63 to treat a forall on the input as the beginning of a quantifier
64 or the beginning of the rule itself. Resolving to shift means
65 it's always treated as a quantifier, hence the above is disallowed.
66 This saves explicitly defining a grammar for the rule lhs that
67 doesn't include 'forall'.
69 1 for ambiguity in 'x @ Rec{..}'.
70 Only sensible parse is 'x @ (Rec{..})', which is what resolving
73 6 for conflicts between `fdecl' and `fdeclDEPRECATED', which are resolved
74 correctly, and moreover, should go away when `fdeclDEPRECATED' is removed.
76 -----------------------------------------------------------------------------
80 '_' { ITunderscore } -- Haskell keywords
85 'default' { ITdefault }
86 'deriving' { ITderiving }
96 'instance' { ITinstance }
99 'newtype' { ITnewtype }
101 'qualified' { ITqualified }
105 '_scc_' { ITscc } -- ToDo: remove
107 'forall' { ITforall } -- GHC extension keywords
108 'foreign' { ITforeign }
109 'export' { ITexport }
111 'dynamic' { ITdynamic }
113 'unsafe' { ITunsafe }
115 'stdcall' { ITstdcallconv }
116 'ccall' { ITccallconv }
117 'dotnet' { ITdotnet }
118 '_ccall_' { ITccall (False, False, PlayRisky) }
119 '_ccall_GC_' { ITccall (False, False, PlaySafe) }
120 '_casm_' { ITccall (False, True, PlayRisky) }
121 '_casm_GC_' { ITccall (False, True, PlaySafe) }
123 '{-# SPECIALISE' { ITspecialise_prag }
124 '{-# SOURCE' { ITsource_prag }
125 '{-# INLINE' { ITinline_prag }
126 '{-# NOINLINE' { ITnoinline_prag }
127 '{-# RULES' { ITrules_prag }
128 '{-# SCC' { ITscc_prag }
129 '{-# DEPRECATED' { ITdeprecated_prag }
130 '#-}' { ITclose_prag }
133 '__interface' { ITinterface } -- interface keywords
134 '__export' { IT__export }
135 '__instimport' { ITinstimport }
136 '__forall' { IT__forall }
137 '__letrec' { ITletrec }
138 '__coerce' { ITcoerce }
139 '__depends' { ITdepends }
140 '__inline' { ITinline }
141 '__DEFAULT' { ITdefaultbranch }
143 '__integer' { ITinteger_lit }
144 '__float' { ITfloat_lit }
145 '__rational' { ITrational_lit }
146 '__addr' { ITaddr_lit }
147 '__label' { ITlabel_lit }
148 '__litlit' { ITlit_lit }
149 '__string' { ITstring_lit }
150 '__ccall' { ITccall $$ }
152 '__sccC' { ITsccAllCafs }
155 '__P' { ITspecialise }
158 '__S' { ITstrict $$ }
159 '__M' { ITcprinfo $$ }
162 '..' { ITdotdot } -- reserved symbols
177 '{' { ITocurly } -- special symbols
181 vccurly { ITvccurly } -- virtual close curly (from layout)
194 VARID { ITvarid $$ } -- identifiers
196 VARSYM { ITvarsym $$ }
197 CONSYM { ITconsym $$ }
198 QVARID { ITqvarid $$ }
199 QCONID { ITqconid $$ }
200 QVARSYM { ITqvarsym $$ }
201 QCONSYM { ITqconsym $$ }
203 IPDUPVARID { ITdupipvarid $$ } -- GHC extension
204 IPSPLITVARID { ITsplitipvarid $$ } -- GHC extension
207 STRING { ITstring $$ }
208 INTEGER { ITinteger $$ }
209 RATIONAL { ITrational $$ }
211 PRIMCHAR { ITprimchar $$ }
212 PRIMSTRING { ITprimstring $$ }
213 PRIMINTEGER { ITprimint $$ }
214 PRIMFLOAT { ITprimfloat $$ }
215 PRIMDOUBLE { ITprimdouble $$ }
216 CLITLIT { ITlitlit $$ }
218 %monad { P } { thenP } { returnP }
219 %lexer { lexer } { ITeof }
220 %name parseModule module
221 %name parseStmt maybe_stmt
222 %name parseIdentifier identifier
226 -----------------------------------------------------------------------------
229 -- The place for module deprecation is really too restrictive, but if it
230 -- was allowed at its natural place just before 'module', we get an ugly
231 -- s/r conflict with the second alternative. Another solution would be the
232 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
233 -- either, and DEPRECATED is only expected to be used by people who really
234 -- know what they are doing. :-)
236 module :: { RdrNameHsModule }
237 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
238 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
240 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
242 maybemoddeprec :: { Maybe DeprecTxt }
243 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
244 | {- empty -} { Nothing }
246 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
248 | layout_on top close { $2 }
250 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
251 : importdecls { (reverse $1,[]) }
252 | importdecls ';' cvtopdecls { (reverse $1,$3) }
253 | cvtopdecls { ([],$1) }
255 cvtopdecls :: { [RdrNameHsDecl] }
256 : topdecls { cvTopDecls (groupBindings $1)}
258 -----------------------------------------------------------------------------
261 maybeexports :: { Maybe [RdrNameIE] }
262 : '(' exportlist ')' { Just $2 }
263 | {- empty -} { Nothing }
265 exportlist :: { [RdrNameIE] }
266 : exportlist ',' export { $3 : $1 }
267 | exportlist ',' { $1 }
271 -- GHC extension: we allow things like [] and (,,,) to be exported
272 export :: { RdrNameIE }
274 | gtycon { IEThingAbs $1 }
275 | gtycon '(' '..' ')' { IEThingAll $1 }
276 | gtycon '(' ')' { IEThingWith $1 [] }
277 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
278 | 'module' modid { IEModuleContents $2 }
280 qcnames :: { [RdrName] }
281 : qcnames ',' qcname { $3 : $1 }
284 qcname :: { RdrName }
288 -----------------------------------------------------------------------------
289 -- Import Declarations
291 -- import decls can be *empty*, or even just a string of semicolons
292 -- whereas topdecls must contain at least one topdecl.
294 importdecls :: { [RdrNameImportDecl] }
295 : importdecls ';' importdecl { $3 : $1 }
296 | importdecls ';' { $1 }
297 | importdecl { [ $1 ] }
300 importdecl :: { RdrNameImportDecl }
301 : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec
302 { ImportDecl $5 $3 $4 $6 $7 $2 }
304 maybe_src :: { WhereFrom }
305 : '{-# SOURCE' '#-}' { ImportByUserSource }
306 | {- empty -} { ImportByUser }
308 optqualified :: { Bool }
309 : 'qualified' { True }
310 | {- empty -} { False }
312 maybeas :: { Maybe ModuleName }
313 : 'as' modid { Just $2 }
314 | {- empty -} { Nothing }
316 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
317 : impspec { Just $1 }
318 | {- empty -} { Nothing }
320 impspec :: { (Bool, [RdrNameIE]) }
321 : '(' exportlist ')' { (False, reverse $2) }
322 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
324 -----------------------------------------------------------------------------
325 -- Fixity Declarations
329 | INTEGER {% checkPrec $1 `thenP_`
330 returnP (fromInteger $1) }
332 infix :: { FixityDirection }
334 | 'infixl' { InfixL }
335 | 'infixr' { InfixR }
338 : ops ',' op { $3 : $1 }
341 -----------------------------------------------------------------------------
342 -- Top-Level Declarations
344 topdecls :: { [RdrBinding] }
345 : topdecls ';' topdecl { ($3 : $1) }
346 | topdecls ';' { $1 }
349 topdecl :: { RdrBinding }
350 : srcloc 'type' tycon tv_bndrs '=' ctype
351 -- Note ctype, not sigtype.
352 -- We allow an explicit for-all but we don't insert one
353 -- in type Foo a = (b,b)
354 -- Instead we just say b is out of scope
355 { RdrHsDecl (TyClD (TySynonym $3 $4 $6 $1)) }
358 | srcloc 'data' tycl_hdr constrs deriving
359 {% returnP (RdrHsDecl (TyClD
360 (mkTyData DataType $3 (reverse $4) (length $4) $5 $1))) }
362 | srcloc 'newtype' tycl_hdr '=' newconstr deriving
363 {% returnP (RdrHsDecl (TyClD
364 (mkTyData NewType $3 [$5] 1 $6 $1))) }
366 | srcloc 'class' tycl_hdr fds where
368 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
370 returnP (RdrHsDecl (TyClD
371 (mkClassDecl $3 $4 sigs (Just binds) $1))) }
373 | srcloc 'instance' inst_type where
375 = cvMonoBindsAndSigs cvInstDeclSig
377 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
379 | srcloc 'default' '(' comma_types0 ')' { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
380 | 'foreign' fdecl { RdrHsDecl $2 }
381 | '{-# DEPRECATED' deprecations '#-}' { $2 }
382 | '{-# RULES' rules '#-}' { $2 }
385 -- tycl_hdr parses the header of a type or class decl,
386 -- which takes the form
389 -- (Eq a, Ord b) => T a b
390 -- Rather a lot of inlining here, else we get reduce/reduce errors
391 tycl_hdr :: { (RdrNameContext, RdrName, [RdrNameHsTyVar]) }
392 : '(' comma_types1 ')' '=>' tycon tv_bndrs {% mapP checkPred $2 `thenP` \ cxt ->
393 returnP (cxt, $5, $6) }
394 | qtycon atypes1 '=>' tycon atypes0 {% checkTyVars $5 `thenP` \ tvs ->
395 returnP ([HsClassP $1 $2], $4, tvs) }
396 | qtycon atypes0 {% checkTyVars $2 `thenP` \ tvs ->
397 returnP ([], $1, tvs) }
398 -- We have to have qtycon in this production to avoid s/r conflicts
399 -- with the previous one. The renamer will complain if we use
400 -- a qualified tycon.
402 decls :: { [RdrBinding] }
403 : decls ';' decl { $3 : $1 }
408 decl :: { RdrBinding }
411 | '{-# INLINE' srcloc activation qvar '#-}' { RdrSig (InlineSig True $4 $3 $2) }
412 | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
413 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
414 { foldr1 RdrAndBindings
415 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
416 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
417 { RdrSig (SpecInstSig $4 $2) }
419 wherebinds :: { RdrNameHsBinds }
420 : where { cvBinds cvValSig (groupBindings $1) }
422 where :: { [RdrBinding] }
423 : 'where' decllist { $2 }
426 declbinds :: { RdrNameHsBinds }
427 : decllist { cvBinds cvValSig (groupBindings $1) }
429 decllist :: { [RdrBinding] }
430 : '{' decls '}' { $2 }
431 | layout_on decls close { $2 }
433 fixdecl :: { RdrBinding }
434 : srcloc infix prec ops { foldr1 RdrAndBindings
435 [ RdrSig (FixSig (FixitySig n
439 -----------------------------------------------------------------------------
440 -- Transformation Rules
442 rules :: { RdrBinding }
443 : rules ';' rule { $1 `RdrAndBindings` $3 }
446 | {- empty -} { RdrNullBind }
448 rule :: { RdrBinding }
449 : STRING activation rule_forall infixexp '=' srcloc exp
450 { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
452 activation :: { Activation } -- Omitted means AlwaysActive
453 : {- empty -} { AlwaysActive }
454 | explicit_activation { $1 }
456 inverse_activation :: { Activation } -- Omitted means NeverActive
457 : {- empty -} { NeverActive }
458 | explicit_activation { $1 }
460 explicit_activation :: { Activation } -- In brackets
461 : '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
462 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger $3) }
464 rule_forall :: { [RdrNameRuleBndr] }
465 : 'forall' rule_var_list '.' { $2 }
468 rule_var_list :: { [RdrNameRuleBndr] }
470 | rule_var rule_var_list { $1 : $2 }
472 rule_var :: { RdrNameRuleBndr }
473 : varid { RuleBndr $1 }
474 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
476 -----------------------------------------------------------------------------
479 deprecations :: { RdrBinding }
480 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
481 | deprecations ';' { $1 }
483 | {- empty -} { RdrNullBind }
485 -- SUP: TEMPORARY HACK, not checking for `module Foo'
486 deprecation :: { RdrBinding }
487 : srcloc depreclist STRING
488 { foldr RdrAndBindings RdrNullBind
489 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
492 -----------------------------------------------------------------------------
493 -- Foreign import and export declarations
495 -- for the time being, the following accepts foreign declarations conforming
496 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
498 -- * a flag indicates whether pre-standard declarations have been used and
499 -- triggers a deprecation warning further down the road
501 -- NB: The first two rules could be combined into one by replacing `safety1'
502 -- with `safety'. However, the combined rule conflicts with the
505 fdecl :: { RdrNameHsDecl }
506 fdecl : srcloc 'import' callconv safety1 fspec {% mkImport $3 $4 $5 $1 }
507 | srcloc 'import' callconv fspec {% mkImport $3 PlaySafe $4 $1 }
508 | srcloc 'export' callconv fspec {% mkExport $3 $4 $1 }
509 -- the following syntax is DEPRECATED
510 | srcloc fdecl1DEPRECATED { ForD ($2 True $1) }
511 | srcloc fdecl2DEPRECATED { $2 $1 }
513 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
515 ----------- DEPRECATED label decls ------------
516 : 'label' ext_name varid '::' sigtype
517 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
518 (CLabel ($2 `orElse` mkExtName $3))) }
520 ----------- DEPRECATED ccall/stdcall decls ------------
522 -- NB: This business with the case expression below may seem overly
523 -- complicated, but it is necessary to avoid some conflicts.
525 -- DEPRECATED variant #1: lack of a calling convention specification
527 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
529 target = StaticTarget ($2 `orElse` mkExtName $4)
531 ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
532 (CFunction target)) }
534 -- DEPRECATED variant #2: external name consists of two separate strings
535 -- (module name and function name) (import)
536 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
538 DNCall -> parseError "Illegal format of .NET foreign import"
539 CCall cconv -> returnP $
541 imp = CFunction (StaticTarget $4)
543 ForeignImport $6 $8 (CImport cconv $5 _NIL_ _NIL_ imp) }
545 -- DEPRECATED variant #3: `unsafe' after entity
546 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
548 DNCall -> parseError "Illegal format of .NET foreign import"
549 CCall cconv -> returnP $
551 imp = CFunction (StaticTarget $3)
553 ForeignImport $5 $7 (CImport cconv PlayRisky _NIL_ _NIL_ imp) }
555 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
556 -- an explicit calling convention (import)
557 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
558 { ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
559 (CFunction DynamicTarget)) }
561 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
562 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
564 DNCall -> parseError "Illegal format of .NET foreign import"
565 CCall cconv -> returnP $
566 ForeignImport $5 $7 (CImport cconv $4 _NIL_ _NIL_
567 (CFunction DynamicTarget)) }
569 -- DEPRECATED variant #6: lack of a calling convention specification
571 | 'export' {-no callconv-} ext_name varid '::' sigtype
572 { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3)
575 -- DEPRECATED variant #7: external name consists of two separate strings
576 -- (module name and function name) (export)
577 | 'export' callconv STRING STRING varid '::' sigtype
579 DNCall -> parseError "Illegal format of .NET foreign import"
580 CCall cconv -> returnP $
582 (CExport (CExportStatic $4 cconv)) }
584 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
585 -- an explicit calling convention (export)
586 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
587 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
590 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
591 | 'export' callconv 'dynamic' varid '::' sigtype
593 DNCall -> parseError "Illegal format of .NET foreign import"
594 CCall cconv -> returnP $
595 ForeignImport $4 $6 (CImport cconv PlaySafe _NIL_ _NIL_ CWrapper) }
597 ----------- DEPRECATED .NET decls ------------
598 -- NB: removed the .NET call declaration, as it is entirely subsumed
599 -- by the new standard FFI declarations
601 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
603 : 'import' 'dotnet' 'type' ext_name tycon
604 { \loc -> TyClD (ForeignType $5 $4 DNType loc) }
605 -- left this one unchanged for the moment as type imports are not
606 -- covered currently by the FFI standard -=chak
609 callconv :: { CallConv }
610 : 'stdcall' { CCall StdCallConv }
611 | 'ccall' { CCall CCallConv }
612 | 'dotnet' { DNCall }
615 : 'unsafe' { PlayRisky }
616 | 'safe' { PlaySafe }
617 | {- empty -} { PlaySafe }
619 safety1 :: { Safety }
620 : 'unsafe' { PlayRisky }
621 | 'safe' { PlaySafe }
622 -- only needed to avoid conflicts with the DEPRECATED rules
624 fspec :: { (FAST_STRING, RdrName, RdrNameHsType) }
625 : STRING varid '::' sigtype { ($1 , $2, $4) }
626 | varid '::' sigtype { (SLIT(""), $1, $3) }
627 -- if the entity string is missing, it defaults to the empty string;
628 -- the meaning of an empty entity string depends on the calling
632 ext_name :: { Maybe CLabelString }
634 | STRING STRING { Just $2 } -- Ignore "module name" for now
635 | {- empty -} { Nothing }
638 -----------------------------------------------------------------------------
641 opt_sig :: { Maybe RdrNameHsType }
642 : {- empty -} { Nothing }
643 | '::' sigtype { Just $2 }
645 opt_asig :: { Maybe RdrNameHsType }
646 : {- empty -} { Nothing }
647 | '::' atype { Just $2 }
649 sigtypes :: { [RdrNameHsType] }
651 | sigtypes ',' sigtype { $3 : $1 }
653 sigtype :: { RdrNameHsType }
654 : ctype { mkHsForAllTy Nothing [] $1 }
656 sig_vars :: { [RdrName] }
657 : sig_vars ',' var { $3 : $1 }
660 -----------------------------------------------------------------------------
663 -- A ctype is a for-all type
664 ctype :: { RdrNameHsType }
665 : 'forall' tv_bndrs '.' ctype { mkHsForAllTy (Just $2) [] $4 }
666 | context '=>' type { mkHsForAllTy Nothing $1 $3 }
667 -- A type of form (context => type) is an *implicit* HsForAllTy
670 -- We parse a context as a btype so that we don't get reduce/reduce
671 -- errors in ctype. The basic problem is that
673 -- looks so much like a tuple type. We can't tell until we find the =>
674 context :: { RdrNameContext }
675 : btype {% checkContext $1 }
677 type :: { RdrNameHsType }
678 : gentype '->' type { HsFunTy $1 $3 }
679 | ipvar '::' type { mkHsIParamTy $1 $3 }
682 gentype :: { RdrNameHsType }
685 | atype tyconop atype { HsOpTy $1 $2 $3 }
687 btype :: { RdrNameHsType }
688 : btype atype { HsAppTy $1 $2 }
691 atype :: { RdrNameHsType }
692 : gtycon { HsTyVar $1 }
693 | tyvar { HsTyVar $1 }
694 | '(' type ',' comma_types1 ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2:$4) }
695 | '(#' comma_types1 '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
696 | '[' type ']' { HsListTy $2 }
697 | '[:' type ':]' { HsPArrTy $2 }
698 | '(' ctype ')' { $2 }
699 | '(' ctype '::' kind ')' { HsKindSig $2 $4 }
701 | INTEGER { HsNumTy $1 }
703 -- An inst_type is what occurs in the head of an instance decl
704 -- e.g. (Foo a, Gaz b) => Wibble a b
705 -- It's kept as a single type, with a MonoDictTy at the right
706 -- hand corner, for convenience.
707 inst_type :: { RdrNameHsType }
708 : ctype {% checkInstType $1 }
710 comma_types0 :: { [RdrNameHsType] }
711 : comma_types1 { $1 }
714 comma_types1 :: { [RdrNameHsType] }
716 | type ',' comma_types1 { $1 : $3 }
718 atypes0 :: { [RdrNameHsType] }
722 atypes1 :: { [RdrNameHsType] }
724 | atype atypes1 { $1 : $2 }
726 tv_bndrs :: { [RdrNameHsTyVar] }
727 : tv_bndr tv_bndrs { $1 : $2 }
730 tv_bndr :: { RdrNameHsTyVar }
731 : tyvar { UserTyVar $1 }
732 | '(' tyvar '::' kind ')' { IfaceTyVar $2 $4 }
734 fds :: { [([RdrName], [RdrName])] }
736 | '|' fds1 { reverse $2 }
738 fds1 :: { [([RdrName], [RdrName])] }
739 : fds1 ',' fd { $3 : $1 }
742 fd :: { ([RdrName], [RdrName]) }
743 : varids0 '->' varids0 { (reverse $1, reverse $3) }
745 varids0 :: { [RdrName] }
747 | varids0 tyvar { $2 : $1 }
749 -----------------------------------------------------------------------------
754 | akind '->' kind { mkArrowKind $1 $3 }
757 : '*' { liftedTypeKind }
758 | '(' kind ')' { $2 }
761 -----------------------------------------------------------------------------
762 -- Datatype declarations
764 newconstr :: { RdrNameConDecl }
765 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
766 | srcloc conid '{' var '::' ctype '}'
767 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
769 constrs :: { [RdrNameConDecl] }
770 : {- empty; a GHC extension -} { [] }
771 | '=' constrs1 { $2 }
773 constrs1 :: { [RdrNameConDecl] }
774 : constrs1 '|' constr { $3 : $1 }
777 constr :: { RdrNameConDecl }
778 : srcloc forall context '=>' constr_stuff
779 { mkConDecl (fst $5) $2 $3 (snd $5) $1 }
780 | srcloc forall constr_stuff
781 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
783 forall :: { [RdrNameHsTyVar] }
784 : 'forall' tv_bndrs '.' { $2 }
787 constr_stuff :: { (RdrName, RdrNameConDetails) }
788 : btype {% mkVanillaCon $1 [] }
789 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
790 | gtycon '{' '}' {% mkRecCon $1 [] }
791 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
792 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
794 satypes :: { [RdrNameBangType] }
795 : atype satypes { unbangedType $1 : $2 }
796 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
799 sbtype :: { RdrNameBangType }
800 : btype { unbangedType $1 }
801 | '!' atype { BangType MarkedUserStrict $2 }
803 fielddecls :: { [([RdrName],RdrNameBangType)] }
804 : fielddecl ',' fielddecls { $1 : $3 }
807 fielddecl :: { ([RdrName],RdrNameBangType) }
808 : sig_vars '::' stype { (reverse $1, $3) }
810 stype :: { RdrNameBangType }
811 : ctype { unbangedType $1 }
812 | '!' atype { BangType MarkedUserStrict $2 }
814 deriving :: { Maybe RdrNameContext }
815 : {- empty -} { Nothing }
816 | 'deriving' context { Just $2 }
817 -- Glasgow extension: allow partial
818 -- applications in derivings
820 -----------------------------------------------------------------------------
823 {- There's an awkward overlap with a type signature. Consider
824 f :: Int -> Int = ...rhs...
825 Then we can't tell whether it's a type signature or a value
826 definition with a result signature until we see the '='.
827 So we have to inline enough to postpone reductions until we know.
831 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
832 instead of qvar, we get another shift/reduce-conflict. Consider the
835 { (^^) :: Int->Int ; } Type signature; only var allowed
837 { (^^) :: Int->Int = ... ; } Value defn with result signature;
838 qvar allowed (because of instance decls)
840 We can't tell whether to reduce var to qvar until after we've read the signatures.
843 valdef :: { RdrBinding }
844 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
845 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
846 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
847 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
851 rhs :: { RdrNameGRHSs }
852 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
853 | gdrhs wherebinds { GRHSs (reverse $1) $2 placeHolderType }
855 gdrhs :: { [RdrNameGRHS] }
856 : gdrhs gdrh { $2 : $1 }
859 gdrh :: { RdrNameGRHS }
860 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
862 -----------------------------------------------------------------------------
865 exp :: { RdrNameHsExpr }
866 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
867 | infixexp 'with' dbinding { HsWith $1 $3 }
870 infixexp :: { RdrNameHsExpr }
872 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
873 (panic "fixity") $3 )}
875 exp10 :: { RdrNameHsExpr }
876 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
877 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
878 returnP (HsLam (Match ps $5
879 (GRHSs (unguardedRHS $8 $7)
880 EmptyBinds placeHolderType))) }
881 | 'let' declbinds 'in' exp { HsLet $2 $4 }
882 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
883 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
884 | '-' fexp { mkHsNegApp $2 }
885 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
886 returnP (HsDo DoExpr stmts $1) }
888 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False placeHolderType }
889 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 PlaySafe False placeHolderType }
890 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True placeHolderType }
891 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 PlaySafe True placeHolderType }
893 | scc_annot exp { if opt_SccProfilingOn
899 scc_annot :: { FAST_STRING }
900 : '_scc_' STRING { $2 }
901 | '{-# SCC' STRING '#-}' { $2 }
903 ccallid :: { FAST_STRING }
907 fexp :: { RdrNameHsExpr }
908 : fexp aexp { (HsApp $1 $2) }
911 aexps0 :: { [RdrNameHsExpr] }
912 : aexps { reverse $1 }
914 aexps :: { [RdrNameHsExpr] }
915 : aexps aexp { $2 : $1 }
918 aexp :: { RdrNameHsExpr }
919 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
920 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
924 var_or_con :: { RdrNameHsExpr }
928 aexp1 :: { RdrNameHsExpr }
929 : ipvar { HsIPVar $1 }
931 | literal { HsLit $1 }
932 | INTEGER { HsOverLit (mkHsIntegral $1) }
933 | RATIONAL { HsOverLit (mkHsFractional $1) }
934 | '(' exp ')' { HsPar $2 }
935 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
936 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
937 | '[' list ']' { $2 }
938 | '[:' parr ':]' { $2 }
939 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
940 | '(' qopm infixexp ')' { (SectionR $2 $3) }
941 | qvar '@' aexp { EAsPat $1 $3 }
943 | '~' aexp1 { ELazyPat $2 }
945 texps :: { [RdrNameHsExpr] }
946 : texps ',' exp { $3 : $1 }
950 -----------------------------------------------------------------------------
953 -- The rules below are little bit contorted to keep lexps left-recursive while
954 -- avoiding another shift/reduce-conflict.
956 list :: { RdrNameHsExpr }
957 : exp { ExplicitList placeHolderType [$1] }
958 | lexps { ExplicitList placeHolderType (reverse $1) }
959 | exp '..' { ArithSeqIn (From $1) }
960 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
961 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
962 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
963 | exp srcloc pquals {% let { body [qs] = qs;
964 body qss = [ParStmt (map reverse qss)] }
966 returnP ( HsDo ListComp
967 (reverse (ResultStmt $1 $2 : body $3))
972 lexps :: { [RdrNameHsExpr] }
973 : lexps ',' exp { $3 : $1 }
974 | exp ',' exp { [$3,$1] }
976 -----------------------------------------------------------------------------
977 -- List Comprehensions
979 pquals :: { [[RdrNameStmt]] }
980 : pquals '|' quals { $3 : $1 }
983 quals :: { [RdrNameStmt] }
984 : quals ',' stmt { $3 : $1 }
987 -----------------------------------------------------------------------------
988 -- Parallel array expressions
990 -- The rules below are little bit contorted; see the list case for details.
991 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
992 -- Moreover, we allow explicit arrays with no element (represented by the nil
993 -- constructor in the list case).
995 parr :: { RdrNameHsExpr }
996 : { ExplicitPArr placeHolderType [] }
997 | exp { ExplicitPArr placeHolderType [$1] }
998 | lexps { ExplicitPArr placeHolderType
1000 | exp '..' exp { PArrSeqIn (FromTo $1 $3) }
1001 | exp ',' exp '..' exp { PArrSeqIn (FromThenTo $1 $3 $5) }
1002 | exp srcloc pquals {% let {
1009 (reverse (ResultStmt $1 $2
1014 -- We are reusing `lexps' and `pquals' from the list case.
1016 -----------------------------------------------------------------------------
1017 -- Case alternatives
1019 altslist :: { [RdrNameMatch] }
1020 : '{' alts '}' { reverse $2 }
1021 | layout_on alts close { reverse $2 }
1023 alts :: { [RdrNameMatch] }
1027 alts1 :: { [RdrNameMatch] }
1028 : alts1 ';' alt { $3 : $1 }
1032 alt :: { RdrNameMatch }
1033 : srcloc infixexp opt_sig ralt wherebinds
1034 {% (checkPattern $1 $2 `thenP` \p ->
1035 returnP (Match [p] $3
1036 (GRHSs $4 $5 placeHolderType)) )}
1038 ralt :: { [RdrNameGRHS] }
1039 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
1040 | gdpats { reverse $1 }
1042 gdpats :: { [RdrNameGRHS] }
1043 : gdpats gdpat { $2 : $1 }
1046 gdpat :: { RdrNameGRHS }
1047 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
1049 -----------------------------------------------------------------------------
1050 -- Statement sequences
1052 stmtlist :: { [RdrNameStmt] }
1053 : '{' stmts '}' { $2 }
1054 | layout_on_for_do stmts close { $2 }
1056 -- do { ;; s ; s ; ; s ;; }
1057 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1058 -- here, because we need too much lookahead if we see do { e ; }
1059 -- So we use ExprStmts throughout, and switch the last one over
1060 -- in ParseUtils.checkDo instead
1061 stmts :: { [RdrNameStmt] }
1062 : stmt stmts_help { $1 : $2 }
1064 | {- empty -} { [] }
1066 stmts_help :: { [RdrNameStmt] }
1068 | {- empty -} { [] }
1070 -- For typing stmts at the GHCi prompt, where
1071 -- the input may consist of just comments.
1072 maybe_stmt :: { Maybe RdrNameStmt }
1074 | {- nothing -} { Nothing }
1076 stmt :: { RdrNameStmt }
1077 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
1078 returnP (BindStmt p $4 $1) }
1079 | srcloc exp { ExprStmt $2 placeHolderType $1 }
1080 | srcloc 'let' declbinds { LetStmt $3 }
1082 -----------------------------------------------------------------------------
1083 -- Record Field Update/Construction
1085 fbinds :: { RdrNameHsRecordBinds }
1086 : fbinds ',' fbind { $3 : $1 }
1089 | {- empty -} { [] }
1091 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
1092 : qvar '=' exp { ($1,$3,False) }
1094 -----------------------------------------------------------------------------
1095 -- Implicit Parameter Bindings
1097 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1098 : '{' dbinds '}' { $2 }
1099 | layout_on dbinds close { $2 }
1101 dbinds :: { [(IPName RdrName, RdrNameHsExpr)] }
1102 : dbinds ';' dbind { $3 : $1 }
1105 | {- empty -} { [] }
1107 dbind :: { (IPName RdrName, RdrNameHsExpr) }
1108 dbind : ipvar '=' exp { ($1, $3) }
1110 -----------------------------------------------------------------------------
1111 -- Variables, Constructors and Operators.
1113 identifier :: { RdrName }
1118 depreclist :: { [RdrName] }
1119 depreclist : deprec_var { [$1] }
1120 | deprec_var ',' depreclist { $1 : $3 }
1122 deprec_var :: { RdrName }
1123 deprec_var : var { $1 }
1126 gtycon :: { RdrName }
1128 | '(' qtyconop ')' { $2 }
1129 | '(' ')' { unitTyCon_RDR }
1130 | '(' '->' ')' { funTyCon_RDR }
1131 | '[' ']' { listTyCon_RDR }
1132 | '[:' ':]' { parrTyCon_RDR }
1133 | '(' commas ')' { tupleTyCon_RDR $2 }
1135 gcon :: { RdrName } -- Data constructor namespace
1136 : '(' ')' { unitCon_RDR }
1137 | '[' ']' { nilCon_RDR }
1138 | '(' commas ')' { tupleCon_RDR $2 }
1140 -- the case of '[:' ':]' is part of the production `parr'
1144 | '(' varsym ')' { $2 }
1148 | '(' varsym ')' { $2 }
1149 | '(' qvarsym1 ')' { $2 }
1150 -- We've inlined qvarsym here so that the decision about
1151 -- whether it's a qvar or a var can be postponed until
1152 -- *after* we see the close paren.
1154 ipvar :: { IPName RdrName }
1155 : IPDUPVARID { Dupable (mkUnqual varName $1) }
1156 | IPSPLITVARID { Linear (mkUnqual varName $1) }
1160 | '(' qconsym ')' { $2 }
1162 varop :: { RdrName }
1164 | '`' varid '`' { $2 }
1166 qvarop :: { RdrName }
1168 | '`' qvarid '`' { $2 }
1170 qvaropm :: { RdrName }
1171 : qvarsym_no_minus { $1 }
1172 | '`' qvarid '`' { $2 }
1174 conop :: { RdrName }
1176 | '`' conid '`' { $2 }
1178 qconop :: { RdrName }
1180 | '`' qconid '`' { $2 }
1182 -----------------------------------------------------------------------------
1185 op :: { RdrName } -- used in infix decls
1189 qop :: { RdrName {-HsExpr-} } -- used in sections
1193 qopm :: { RdrNameHsExpr } -- used in sections
1194 : qvaropm { HsVar $1 }
1195 | qconop { HsVar $1 }
1197 -----------------------------------------------------------------------------
1200 qvarid :: { RdrName }
1202 | QVARID { mkQual varName $1 }
1204 varid :: { RdrName }
1205 : varid_no_unsafe { $1 }
1206 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1208 varid_no_unsafe :: { RdrName }
1209 : VARID { mkUnqual varName $1 }
1210 | special_id { mkUnqual varName $1 }
1211 | 'forall' { mkUnqual varName SLIT("forall") }
1213 tyvar :: { RdrName }
1214 : VARID { mkUnqual tvName $1 }
1215 | special_id { mkUnqual tvName $1 }
1216 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1218 -- These special_ids are treated as keywords in various places,
1219 -- but as ordinary ids elsewhere. A special_id collects all thsee
1220 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1221 special_id :: { UserFS }
1223 : 'as' { SLIT("as") }
1224 | 'qualified' { SLIT("qualified") }
1225 | 'hiding' { SLIT("hiding") }
1226 | 'export' { SLIT("export") }
1227 | 'label' { SLIT("label") }
1228 | 'dynamic' { SLIT("dynamic") }
1229 | 'stdcall' { SLIT("stdcall") }
1230 | 'ccall' { SLIT("ccall") }
1232 -----------------------------------------------------------------------------
1235 qconid :: { RdrName } -- Qualified or unqualifiedb
1237 | QCONID { mkQual dataName $1 }
1239 conid :: { RdrName }
1240 : CONID { mkUnqual dataName $1 }
1242 -----------------------------------------------------------------------------
1245 qconsym :: { RdrName } -- Qualified or unqualifiedb
1247 | QCONSYM { mkQual dataName $1 }
1249 consym :: { RdrName }
1250 : CONSYM { mkUnqual dataName $1 }
1252 -----------------------------------------------------------------------------
1255 qvarsym :: { RdrName }
1259 qvarsym_no_minus :: { RdrName }
1260 : varsym_no_minus { $1 }
1263 qvarsym1 :: { RdrName }
1264 qvarsym1 : QVARSYM { mkQual varName $1 }
1266 varsym :: { RdrName }
1267 : varsym_no_minus { $1 }
1268 | '-' { mkUnqual varName SLIT("-") }
1270 varsym_no_minus :: { RdrName } -- varsym not including '-'
1271 : VARSYM { mkUnqual varName $1 }
1272 | special_sym { mkUnqual varName $1 }
1275 -- See comments with special_id
1276 special_sym :: { UserFS }
1277 special_sym : '!' { SLIT("!") }
1281 -----------------------------------------------------------------------------
1284 literal :: { HsLit }
1285 : CHAR { HsChar $1 }
1286 | STRING { HsString $1 }
1287 | PRIMINTEGER { HsIntPrim $1 }
1288 | PRIMCHAR { HsCharPrim $1 }
1289 | PRIMSTRING { HsStringPrim $1 }
1290 | PRIMFLOAT { HsFloatPrim $1 }
1291 | PRIMDOUBLE { HsDoublePrim $1 }
1292 | CLITLIT { HsLitLit $1 placeHolderType }
1294 srcloc :: { SrcLoc } : {% getSrcLocP }
1296 -----------------------------------------------------------------------------
1300 : vccurly { () } -- context popped in lexer.
1301 | error {% popContext }
1303 layout_on :: { () } : {% layoutOn True{-strict-} }
1304 layout_on_for_do :: { () } : {% layoutOn False }
1306 -----------------------------------------------------------------------------
1307 -- Miscellaneous (mostly renamings)
1309 modid :: { ModuleName }
1310 : CONID { mkModuleNameFS $1 }
1311 | QCONID { mkModuleNameFS
1313 (unpackFS (fst $1) ++
1314 '.':unpackFS (snd $1)))
1317 tycon :: { RdrName }
1318 : CONID { mkUnqual tcClsName $1 }
1320 tyconop :: { RdrName }
1321 : CONSYM { mkUnqual tcClsName $1 }
1323 qtycon :: { RdrName } -- Qualified or unqualified
1324 : QCONID { mkQual tcClsName $1 }
1327 qtyconop :: { RdrName } -- Qualified or unqualified
1328 : QCONSYM { mkQual tcClsName $1 }
1332 : commas ',' { $1 + 1 }
1335 -----------------------------------------------------------------------------
1339 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)