2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.84 2002/02/11 08:20:44 chak 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 BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
32 NewOrData(..), StrictnessMark(..), Activation(..) )
36 import CStrings ( CLabelString )
38 import Maybes ( orElse )
41 #include "HsVersions.h"
45 -----------------------------------------------------------------------------
46 Conflicts: 21 shift/reduce, -=chak[4Feb2]
48 8 for abiguity in 'if x then y else z + 1'
49 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
50 1 for ambiguity in 'if x then y else z :: T'
51 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
52 3 for ambiguity in 'case x of y :: a -> b'
53 (don't know whether to reduce 'a' as a btype or shift the '->'.
54 conclusion: bogus expression anyway, doesn't matter)
56 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
57 since 'forall' is a valid variable name, we don't know whether
58 to treat a forall on the input as the beginning of a quantifier
59 or the beginning of the rule itself. Resolving to shift means
60 it's always treated as a quantifier, hence the above is disallowed.
61 This saves explicitly defining a grammar for the rule lhs that
62 doesn't include 'forall'.
64 1 for ambiguity in 'x @ Rec{..}'.
65 Only sensible parse is 'x @ (Rec{..})', which is what resolving
68 6 for conflicts between `fdecl' and `fdeclDEPRECATED', which are resolved
69 correctly, and moreover, should go away when `fdeclDEPRECATED' is removed.
71 -----------------------------------------------------------------------------
75 '_' { ITunderscore } -- Haskell keywords
80 'default' { ITdefault }
81 'deriving' { ITderiving }
91 'instance' { ITinstance }
94 'newtype' { ITnewtype }
96 'qualified' { ITqualified }
100 '_scc_' { ITscc } -- ToDo: remove
102 'forall' { ITforall } -- GHC extension keywords
103 'foreign' { ITforeign }
104 'export' { ITexport }
106 'dynamic' { ITdynamic }
108 'unsafe' { ITunsafe }
110 'stdcall' { ITstdcallconv }
111 'ccall' { ITccallconv }
112 'dotnet' { ITdotnet }
113 '_ccall_' { ITccall (False, False, PlayRisky) }
114 '_ccall_GC_' { ITccall (False, False, PlaySafe) }
115 '_casm_' { ITccall (False, True, PlayRisky) }
116 '_casm_GC_' { ITccall (False, True, PlaySafe) }
118 '{-# SPECIALISE' { ITspecialise_prag }
119 '{-# SOURCE' { ITsource_prag }
120 '{-# INLINE' { ITinline_prag }
121 '{-# NOINLINE' { ITnoinline_prag }
122 '{-# RULES' { ITrules_prag }
123 '{-# SCC' { ITscc_prag }
124 '{-# DEPRECATED' { ITdeprecated_prag }
125 '#-}' { ITclose_prag }
128 '__interface' { ITinterface } -- interface keywords
129 '__export' { IT__export }
130 '__instimport' { ITinstimport }
131 '__forall' { IT__forall }
132 '__letrec' { ITletrec }
133 '__coerce' { ITcoerce }
134 '__depends' { ITdepends }
135 '__inline' { ITinline }
136 '__DEFAULT' { ITdefaultbranch }
138 '__integer' { ITinteger_lit }
139 '__float' { ITfloat_lit }
140 '__rational' { ITrational_lit }
141 '__addr' { ITaddr_lit }
142 '__label' { ITlabel_lit }
143 '__litlit' { ITlit_lit }
144 '__string' { ITstring_lit }
145 '__ccall' { ITccall $$ }
147 '__sccC' { ITsccAllCafs }
150 '__P' { ITspecialise }
153 '__S' { ITstrict $$ }
154 '__M' { ITcprinfo $$ }
157 '..' { ITdotdot } -- reserved symbols
171 '{' { ITocurly } -- special symbols
175 vccurly { ITvccurly } -- virtual close curly (from layout)
188 VARID { ITvarid $$ } -- identifiers
190 VARSYM { ITvarsym $$ }
191 CONSYM { ITconsym $$ }
192 QVARID { ITqvarid $$ }
193 QCONID { ITqconid $$ }
194 QVARSYM { ITqvarsym $$ }
195 QCONSYM { ITqconsym $$ }
197 IPDUPVARID { ITdupipvarid $$ } -- GHC extension
198 IPSPLITVARID { ITsplitipvarid $$ } -- GHC extension
201 STRING { ITstring $$ }
202 INTEGER { ITinteger $$ }
203 RATIONAL { ITrational $$ }
205 PRIMCHAR { ITprimchar $$ }
206 PRIMSTRING { ITprimstring $$ }
207 PRIMINTEGER { ITprimint $$ }
208 PRIMFLOAT { ITprimfloat $$ }
209 PRIMDOUBLE { ITprimdouble $$ }
210 CLITLIT { ITlitlit $$ }
212 %monad { P } { thenP } { returnP }
213 %lexer { lexer } { ITeof }
214 %name parseModule module
215 %name parseStmt maybe_stmt
216 %name parseIdentifier identifier
220 -----------------------------------------------------------------------------
223 -- The place for module deprecation is really too restrictive, but if it
224 -- was allowed at its natural place just before 'module', we get an ugly
225 -- s/r conflict with the second alternative. Another solution would be the
226 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
227 -- either, and DEPRECATED is only expected to be used by people who really
228 -- know what they are doing. :-)
230 module :: { RdrNameHsModule }
231 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
232 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
234 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
236 maybemoddeprec :: { Maybe DeprecTxt }
237 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
238 | {- empty -} { Nothing }
240 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
242 | layout_on top close { $2 }
244 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
245 : importdecls { (reverse $1,[]) }
246 | importdecls ';' cvtopdecls { (reverse $1,$3) }
247 | cvtopdecls { ([],$1) }
249 cvtopdecls :: { [RdrNameHsDecl] }
250 : topdecls { cvTopDecls (groupBindings $1)}
252 -----------------------------------------------------------------------------
255 maybeexports :: { Maybe [RdrNameIE] }
256 : '(' exportlist ')' { Just $2 }
257 | {- empty -} { Nothing }
259 exportlist :: { [RdrNameIE] }
260 : exportlist ',' export { $3 : $1 }
261 | exportlist ',' { $1 }
265 -- GHC extension: we allow things like [] and (,,,) to be exported
266 export :: { RdrNameIE }
268 | gtycon { IEThingAbs $1 }
269 | gtycon '(' '..' ')' { IEThingAll $1 }
270 | gtycon '(' ')' { IEThingWith $1 [] }
271 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
272 | 'module' modid { IEModuleContents $2 }
274 qcnames :: { [RdrName] }
275 : qcnames ',' qcname { $3 : $1 }
278 qcname :: { RdrName }
282 -----------------------------------------------------------------------------
283 -- Import Declarations
285 -- import decls can be *empty*, or even just a string of semicolons
286 -- whereas topdecls must contain at least one topdecl.
288 importdecls :: { [RdrNameImportDecl] }
289 : importdecls ';' importdecl { $3 : $1 }
290 | importdecls ';' { $1 }
291 | importdecl { [ $1 ] }
294 importdecl :: { RdrNameImportDecl }
295 : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec
296 { ImportDecl $5 $3 $4 $6 $7 $2 }
298 maybe_src :: { WhereFrom }
299 : '{-# SOURCE' '#-}' { ImportByUserSource }
300 | {- empty -} { ImportByUser }
302 optqualified :: { Bool }
303 : 'qualified' { True }
304 | {- empty -} { False }
306 maybeas :: { Maybe ModuleName }
307 : 'as' modid { Just $2 }
308 | {- empty -} { Nothing }
310 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
311 : impspec { Just $1 }
312 | {- empty -} { Nothing }
314 impspec :: { (Bool, [RdrNameIE]) }
315 : '(' exportlist ')' { (False, reverse $2) }
316 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
318 -----------------------------------------------------------------------------
319 -- Fixity Declarations
323 | INTEGER {% checkPrec $1 `thenP_`
324 returnP (fromInteger $1) }
326 infix :: { FixityDirection }
328 | 'infixl' { InfixL }
329 | 'infixr' { InfixR }
332 : ops ',' op { $3 : $1 }
335 -----------------------------------------------------------------------------
336 -- Top-Level Declarations
338 topdecls :: { [RdrBinding] }
339 : topdecls ';' topdecl { ($3 : $1) }
340 | topdecls ';' { $1 }
343 topdecl :: { RdrBinding }
344 : srcloc 'type' simpletype '=' ctype
345 -- Note ctype, not sigtype.
346 -- We allow an explicit for-all but we don't insert one
347 -- in type Foo a = (b,b)
348 -- Instead we just say b is out of scope
349 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
351 | srcloc 'data' ctype constrs deriving
352 {% checkDataHeader "data" $3 `thenP` \(cs,c,ts) ->
353 returnP (RdrHsDecl (TyClD
354 (mkTyData DataType cs c ts (reverse $4) (length $4) $5 $1))) }
356 | srcloc 'newtype' ctype '=' newconstr deriving
357 {% checkDataHeader "newtype" $3 `thenP` \(cs,c,ts) ->
358 returnP (RdrHsDecl (TyClD
359 (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
361 | srcloc 'class' ctype fds where
362 {% checkDataHeader "class" $3 `thenP` \(cs,c,ts) ->
364 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
366 returnP (RdrHsDecl (TyClD
367 (mkClassDecl cs c ts $4 sigs (Just binds) $1))) }
369 | srcloc 'instance' inst_type where
371 = cvMonoBindsAndSigs cvInstDeclSig
373 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
375 | srcloc 'default' '(' types0 ')' { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
376 | 'foreign' fdecl { RdrHsDecl $2 }
377 | '{-# DEPRECATED' deprecations '#-}' { $2 }
378 | '{-# RULES' rules '#-}' { $2 }
381 -- for the time being, the following accepts foreign declarations conforming
382 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
384 -- * a flag indicates whether pre-standard declarations have been used and
385 -- triggers a deprecation warning further down the road
387 -- NB: The first two rules could be combined into one by replacing `safety1'
388 -- with `safety'. However, the combined rule conflicts with the
391 fdecl :: { RdrNameHsDecl }
392 fdecl : srcloc 'import' callconv safety1 fspec {% mkImport $3 $4 $5 $1 }
393 | srcloc 'import' callconv fspec {% mkImport $3 PlaySafe $4 $1 }
394 | srcloc 'export' callconv fspec {% mkExport $3 $4 $1 }
395 -- the following syntax is DEPRECATED
396 | srcloc fdecl1DEPRECATED { ForD ($2 True $1) }
397 | srcloc fdecl2DEPRECATED { $2 $1 }
399 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
401 ----------- DEPRECATED label decls ------------
402 : 'label' ext_name varid '::' sigtype
403 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
404 (CLabel ($2 `orElse` mkExtName $3))) }
406 ----------- DEPRECATED ccall/stdcall decls ------------
408 -- NB: This business with the case expression below may seem overly
409 -- complicated, but it is necessary to avoid some conflicts.
411 -- DEPRECATED variant #1: lack of a calling convention specification
413 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
415 target = StaticTarget ($2 `orElse` mkExtName $4)
417 ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
418 (CFunction target)) }
420 -- DEPRECATED variant #2: external name consists of two separate strings
421 -- (module name and function name) (import)
422 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
424 DNCall -> parseError "Illegal format of .NET foreign import"
425 CCall cconv -> returnP $
427 imp = CFunction (StaticTarget $4)
429 ForeignImport $6 $8 (CImport cconv $5 _NIL_ _NIL_ imp) }
431 -- DEPRECATED variant #3: `unsafe' after entity
432 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
434 DNCall -> parseError "Illegal format of .NET foreign import"
435 CCall cconv -> returnP $
437 imp = CFunction (StaticTarget $3)
439 ForeignImport $5 $7 (CImport cconv PlayRisky _NIL_ _NIL_ imp) }
441 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
442 -- an explicit calling convention (import)
443 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
444 { ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
445 (CFunction DynamicTarget)) }
447 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
448 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
450 DNCall -> parseError "Illegal format of .NET foreign import"
451 CCall cconv -> returnP $
452 ForeignImport $5 $7 (CImport cconv $4 _NIL_ _NIL_
453 (CFunction DynamicTarget)) }
455 -- DEPRECATED variant #6: lack of a calling convention specification
457 | 'export' {-no callconv-} ext_name varid '::' sigtype
458 { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3)
461 -- DEPRECATED variant #7: external name consists of two separate strings
462 -- (module name and function name) (export)
463 | 'export' callconv STRING STRING varid '::' sigtype
465 DNCall -> parseError "Illegal format of .NET foreign import"
466 CCall cconv -> returnP $
468 (CExport (CExportStatic $4 cconv)) }
470 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
471 -- an explicit calling convention (export)
472 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
473 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
476 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
477 | 'export' callconv 'dynamic' varid '::' sigtype
479 DNCall -> parseError "Illegal format of .NET foreign import"
480 CCall cconv -> returnP $
481 ForeignImport $4 $6 (CImport cconv PlaySafe _NIL_ _NIL_ CWrapper) }
483 ----------- DEPRECATED .NET decls ------------
484 -- NB: removed the .NET call declaration, as it is entirely subsumed
485 -- by the new standard FFI declarations
487 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
489 : 'import' 'dotnet' 'type' ext_name tycon
490 { \loc -> TyClD (ForeignType $5 $4 DNType loc) }
491 -- left this one unchanged for the moment as type imports are not
492 -- covered currently by the FFI standard -=chak
494 decls :: { [RdrBinding] }
495 : decls ';' decl { $3 : $1 }
500 decl :: { RdrBinding }
503 | '{-# INLINE' srcloc activation qvar '#-}' { RdrSig (InlineSig True $4 $3 $2) }
504 | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
505 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
506 { foldr1 RdrAndBindings
507 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
508 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
509 { RdrSig (SpecInstSig $4 $2) }
511 wherebinds :: { RdrNameHsBinds }
512 : where { cvBinds cvValSig (groupBindings $1) }
514 where :: { [RdrBinding] }
515 : 'where' decllist { $2 }
518 declbinds :: { RdrNameHsBinds }
519 : decllist { cvBinds cvValSig (groupBindings $1) }
521 decllist :: { [RdrBinding] }
522 : '{' decls '}' { $2 }
523 | layout_on decls close { $2 }
525 fixdecl :: { RdrBinding }
526 : srcloc infix prec ops { foldr1 RdrAndBindings
527 [ RdrSig (FixSig (FixitySig n
531 -----------------------------------------------------------------------------
532 -- Transformation Rules
534 rules :: { RdrBinding }
535 : rules ';' rule { $1 `RdrAndBindings` $3 }
538 | {- empty -} { RdrNullBind }
540 rule :: { RdrBinding }
541 : STRING activation rule_forall infixexp '=' srcloc exp
542 { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
544 activation :: { Activation } -- Omitted means AlwaysActive
545 : {- empty -} { AlwaysActive }
546 | explicit_activation { $1 }
548 inverse_activation :: { Activation } -- Omitted means NeverActive
549 : {- empty -} { NeverActive }
550 | explicit_activation { $1 }
552 explicit_activation :: { Activation } -- In brackets
553 : '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
554 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger $3) }
556 rule_forall :: { [RdrNameRuleBndr] }
557 : 'forall' rule_var_list '.' { $2 }
560 rule_var_list :: { [RdrNameRuleBndr] }
562 | rule_var rule_var_list { $1 : $2 }
564 rule_var :: { RdrNameRuleBndr }
565 : varid { RuleBndr $1 }
566 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
568 -----------------------------------------------------------------------------
571 deprecations :: { RdrBinding }
572 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
573 | deprecations ';' { $1 }
575 | {- empty -} { RdrNullBind }
577 -- SUP: TEMPORARY HACK, not checking for `module Foo'
578 deprecation :: { RdrBinding }
579 : srcloc depreclist STRING
580 { foldr RdrAndBindings RdrNullBind
581 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
583 -----------------------------------------------------------------------------
584 -- Foreign declarations
586 callconv :: { CallConv }
587 : 'stdcall' { CCall StdCallConv }
588 | 'ccall' { CCall CCallConv }
589 | 'dotnet' { DNCall }
592 : 'unsafe' { PlayRisky }
593 | 'safe' { PlaySafe }
594 | {- empty -} { PlaySafe }
596 safety1 :: { Safety }
597 : 'unsafe' { PlayRisky }
598 | 'safe' { PlaySafe }
599 -- only needed to avoid conflicts with the DEPRECATED rules
601 fspec :: { (FAST_STRING, RdrName, RdrNameHsType) }
602 : STRING varid '::' sigtype { ($1 , $2, $4) }
603 | varid '::' sigtype { (SLIT(""), $1, $3) }
604 -- if the entity string is missing, it defaults to the empty string;
605 -- the meaning of an empty entity string depends on the calling
609 ext_name :: { Maybe CLabelString }
611 | STRING STRING { Just $2 } -- Ignore "module name" for now
612 | {- empty -} { Nothing }
615 -----------------------------------------------------------------------------
618 opt_sig :: { Maybe RdrNameHsType }
619 : {- empty -} { Nothing }
620 | '::' sigtype { Just $2 }
622 opt_asig :: { Maybe RdrNameHsType }
623 : {- empty -} { Nothing }
624 | '::' atype { Just $2 }
626 sigtypes :: { [RdrNameHsType] }
628 | sigtypes ',' sigtype { $3 : $1 }
630 sigtype :: { RdrNameHsType }
631 : ctype { (mkHsForAllTy Nothing [] $1) }
633 sig_vars :: { [RdrName] }
634 : sig_vars ',' var { $3 : $1 }
637 -----------------------------------------------------------------------------
640 -- A ctype is a for-all type
641 ctype :: { RdrNameHsType }
642 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
643 | context '=>' type { mkHsForAllTy Nothing $1 $3 }
644 -- A type of form (context => type) is an *implicit* HsForAllTy
647 type :: { RdrNameHsType }
648 : gentype '->' type { HsFunTy $1 $3 }
649 | ipvar '::' type { mkHsIParamTy $1 $3 }
652 gentype :: { RdrNameHsType }
655 | atype tyconop atype { HsOpTy $1 $2 $3 }
657 btype :: { RdrNameHsType }
658 : btype atype { (HsAppTy $1 $2) }
661 atype :: { RdrNameHsType }
662 : gtycon { HsTyVar $1 }
663 | tyvar { HsTyVar $1 }
664 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
665 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
666 | '[' type ']' { HsListTy $2 }
667 | '[:' type ':]' { HsPArrTy $2 }
668 | '(' ctype ')' { $2 }
670 | INTEGER { HsNumTy $1 }
672 -- An inst_type is what occurs in the head of an instance decl
673 -- e.g. (Foo a, Gaz b) => Wibble a b
674 -- It's kept as a single type, with a MonoDictTy at the right
675 -- hand corner, for convenience.
676 inst_type :: { RdrNameHsType }
677 : ctype {% checkInstType $1 }
679 types0 :: { [RdrNameHsType] }
680 : types { reverse $1 }
683 types :: { [RdrNameHsType] }
685 | types ',' type { $3 : $1 }
687 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
688 : tycon tyvars { ($1, reverse $2) }
690 tyvars :: { [RdrNameHsTyVar] }
691 : tyvars tyvar { UserTyVar $2 : $1 }
694 fds :: { [([RdrName], [RdrName])] }
696 | '|' fds1 { reverse $2 }
698 fds1 :: { [([RdrName], [RdrName])] }
699 : fds1 ',' fd { $3 : $1 }
702 fd :: { ([RdrName], [RdrName]) }
703 : varids0 '->' varids0 { (reverse $1, reverse $3) }
705 varids0 :: { [RdrName] }
707 | varids0 tyvar { $2 : $1 }
709 -----------------------------------------------------------------------------
710 -- Datatype declarations
712 newconstr :: { RdrNameConDecl }
713 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
714 | srcloc conid '{' var '::' ctype '}'
715 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
717 constrs :: { [RdrNameConDecl] }
718 : {- empty; a GHC extension -} { [] }
719 | '=' constrs1 { $2 }
721 constrs1 :: { [RdrNameConDecl] }
722 : constrs1 '|' constr { $3 : $1 }
725 constr :: { RdrNameConDecl }
726 : srcloc forall context '=>' constr_stuff
727 { mkConDecl (fst $5) $2 $3 (snd $5) $1 }
728 | srcloc forall constr_stuff
729 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
731 forall :: { [RdrNameHsTyVar] }
732 : 'forall' tyvars '.' { $2 }
735 context :: { RdrNameContext }
736 : btype {% checkContext $1 }
738 constr_stuff :: { (RdrName, RdrNameConDetails) }
739 : btype {% mkVanillaCon $1 [] }
740 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
741 | gtycon '{' '}' {% mkRecCon $1 [] }
742 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
743 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
745 satypes :: { [RdrNameBangType] }
746 : atype satypes { unbangedType $1 : $2 }
747 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
750 sbtype :: { RdrNameBangType }
751 : btype { unbangedType $1 }
752 | '!' atype { BangType MarkedUserStrict $2 }
754 fielddecls :: { [([RdrName],RdrNameBangType)] }
755 : fielddecl ',' fielddecls { $1 : $3 }
758 fielddecl :: { ([RdrName],RdrNameBangType) }
759 : sig_vars '::' stype { (reverse $1, $3) }
761 stype :: { RdrNameBangType }
762 : ctype { unbangedType $1 }
763 | '!' atype { BangType MarkedUserStrict $2 }
765 deriving :: { Maybe RdrNameContext }
766 : {- empty -} { Nothing }
767 | 'deriving' context { Just $2 }
768 -- Glasgow extension: allow partial
769 -- applications in derivings
771 -----------------------------------------------------------------------------
774 {- There's an awkward overlap with a type signature. Consider
775 f :: Int -> Int = ...rhs...
776 Then we can't tell whether it's a type signature or a value
777 definition with a result signature until we see the '='.
778 So we have to inline enough to postpone reductions until we know.
782 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
783 instead of qvar, we get another shift/reduce-conflict. Consider the
786 { (^^) :: Int->Int ; } Type signature; only var allowed
788 { (^^) :: Int->Int = ... ; } Value defn with result signature;
789 qvar allowed (because of instance decls)
791 We can't tell whether to reduce var to qvar until after we've read the signatures.
794 valdef :: { RdrBinding }
795 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
796 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
797 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
798 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
802 rhs :: { RdrNameGRHSs }
803 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
804 | gdrhs wherebinds { GRHSs (reverse $1) $2 placeHolderType }
806 gdrhs :: { [RdrNameGRHS] }
807 : gdrhs gdrh { $2 : $1 }
810 gdrh :: { RdrNameGRHS }
811 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
813 -----------------------------------------------------------------------------
816 exp :: { RdrNameHsExpr }
817 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
818 | infixexp 'with' dbinding { HsWith $1 $3 }
821 infixexp :: { RdrNameHsExpr }
823 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
824 (panic "fixity") $3 )}
826 exp10 :: { RdrNameHsExpr }
827 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
828 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
829 returnP (HsLam (Match ps $5
830 (GRHSs (unguardedRHS $8 $7)
831 EmptyBinds placeHolderType))) }
832 | 'let' declbinds 'in' exp { HsLet $2 $4 }
833 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
834 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
835 | '-' fexp { mkHsNegApp $2 }
836 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
837 returnP (HsDo DoExpr stmts $1) }
839 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False placeHolderType }
840 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 PlaySafe False placeHolderType }
841 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True placeHolderType }
842 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 PlaySafe True placeHolderType }
844 | scc_annot exp { if opt_SccProfilingOn
850 scc_annot :: { FAST_STRING }
851 : '_scc_' STRING { $2 }
852 | '{-# SCC' STRING '#-}' { $2 }
854 ccallid :: { FAST_STRING }
858 fexp :: { RdrNameHsExpr }
859 : fexp aexp { (HsApp $1 $2) }
862 aexps0 :: { [RdrNameHsExpr] }
863 : aexps { (reverse $1) }
865 aexps :: { [RdrNameHsExpr] }
866 : aexps aexp { $2 : $1 }
869 aexp :: { RdrNameHsExpr }
870 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
871 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
875 var_or_con :: { RdrNameHsExpr }
879 aexp1 :: { RdrNameHsExpr }
880 : ipvar { HsIPVar $1 }
882 | literal { HsLit $1 }
883 | INTEGER { HsOverLit (mkHsIntegral $1) }
884 | RATIONAL { HsOverLit (mkHsFractional $1) }
885 | '(' exp ')' { HsPar $2 }
886 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
887 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
888 | '[' list ']' { $2 }
889 | '[:' parr ':]' { $2 }
890 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
891 | '(' qopm infixexp ')' { (SectionR $2 $3) }
892 | qvar '@' aexp { EAsPat $1 $3 }
894 | '~' aexp1 { ELazyPat $2 }
896 texps :: { [RdrNameHsExpr] }
897 : texps ',' exp { $3 : $1 }
901 -----------------------------------------------------------------------------
904 -- The rules below are little bit contorted to keep lexps left-recursive while
905 -- avoiding another shift/reduce-conflict.
907 list :: { RdrNameHsExpr }
908 : exp { ExplicitList placeHolderType [$1] }
909 | lexps { ExplicitList placeHolderType (reverse $1) }
910 | exp '..' { ArithSeqIn (From $1) }
911 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
912 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
913 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
914 | exp srcloc pquals {% let { body [qs] = qs;
915 body qss = [ParStmt (map reverse qss)] }
917 returnP ( HsDo ListComp
918 (reverse (ResultStmt $1 $2 : body $3))
923 lexps :: { [RdrNameHsExpr] }
924 : lexps ',' exp { $3 : $1 }
925 | exp ',' exp { [$3,$1] }
927 -----------------------------------------------------------------------------
928 -- List Comprehensions
930 pquals :: { [[RdrNameStmt]] }
931 : pquals '|' quals { $3 : $1 }
934 quals :: { [RdrNameStmt] }
935 : quals ',' stmt { $3 : $1 }
938 -----------------------------------------------------------------------------
939 -- Parallel array expressions
941 -- The rules below are little bit contorted; see the list case for details.
942 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
943 -- Moreover, we allow explicit arrays with no element (represented by the nil
944 -- constructor in the list case).
946 parr :: { RdrNameHsExpr }
947 : { ExplicitPArr placeHolderType [] }
948 | exp { ExplicitPArr placeHolderType [$1] }
949 | lexps { ExplicitPArr placeHolderType
951 | exp '..' exp { PArrSeqIn (FromTo $1 $3) }
952 | exp ',' exp '..' exp { PArrSeqIn (FromThenTo $1 $3 $5) }
953 | exp srcloc pquals {% let {
960 (reverse (ResultStmt $1 $2
965 -- We are reusing `lexps' and `pquals' from the list case.
967 -----------------------------------------------------------------------------
970 altslist :: { [RdrNameMatch] }
971 : '{' alts '}' { reverse $2 }
972 | layout_on alts close { reverse $2 }
974 alts :: { [RdrNameMatch] }
978 alts1 :: { [RdrNameMatch] }
979 : alts1 ';' alt { $3 : $1 }
983 alt :: { RdrNameMatch }
984 : srcloc infixexp opt_sig ralt wherebinds
985 {% (checkPattern $1 $2 `thenP` \p ->
986 returnP (Match [p] $3
987 (GRHSs $4 $5 placeHolderType)) )}
989 ralt :: { [RdrNameGRHS] }
990 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
991 | gdpats { (reverse $1) }
993 gdpats :: { [RdrNameGRHS] }
994 : gdpats gdpat { $2 : $1 }
997 gdpat :: { RdrNameGRHS }
998 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
1000 -----------------------------------------------------------------------------
1001 -- Statement sequences
1003 stmtlist :: { [RdrNameStmt] }
1004 : '{' stmts '}' { $2 }
1005 | layout_on_for_do stmts close { $2 }
1007 -- do { ;; s ; s ; ; s ;; }
1008 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1009 -- here, because we need too much lookahead if we see do { e ; }
1010 -- So we use ExprStmts throughout, and switch the last one over
1011 -- in ParseUtils.checkDo instead
1012 stmts :: { [RdrNameStmt] }
1013 : stmt stmts_help { $1 : $2 }
1015 | {- empty -} { [] }
1017 stmts_help :: { [RdrNameStmt] }
1019 | {- empty -} { [] }
1021 -- For typing stmts at the GHCi prompt, where
1022 -- the input may consist of just comments.
1023 maybe_stmt :: { Maybe RdrNameStmt }
1025 | {- nothing -} { Nothing }
1027 stmt :: { RdrNameStmt }
1028 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
1029 returnP (BindStmt p $4 $1) }
1030 | srcloc exp { ExprStmt $2 placeHolderType $1 }
1031 | srcloc 'let' declbinds { LetStmt $3 }
1033 -----------------------------------------------------------------------------
1034 -- Record Field Update/Construction
1036 fbinds :: { RdrNameHsRecordBinds }
1037 : fbinds ',' fbind { $3 : $1 }
1040 | {- empty -} { [] }
1042 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
1043 : qvar '=' exp { ($1,$3,False) }
1045 -----------------------------------------------------------------------------
1046 -- Implicit Parameter Bindings
1048 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1049 : '{' dbinds '}' { $2 }
1050 | layout_on dbinds close { $2 }
1052 dbinds :: { [(IPName RdrName, RdrNameHsExpr)] }
1053 : dbinds ';' dbind { $3 : $1 }
1056 | {- empty -} { [] }
1058 dbind :: { (IPName RdrName, RdrNameHsExpr) }
1059 dbind : ipvar '=' exp { ($1, $3) }
1061 -----------------------------------------------------------------------------
1062 -- Variables, Constructors and Operators.
1064 identifier :: { RdrName }
1069 depreclist :: { [RdrName] }
1070 depreclist : deprec_var { [$1] }
1071 | deprec_var ',' depreclist { $1 : $3 }
1073 deprec_var :: { RdrName }
1074 deprec_var : var { $1 }
1077 gtycon :: { RdrName }
1079 | '(' qtyconop ')' { $2 }
1080 | '(' ')' { unitTyCon_RDR }
1081 | '(' '->' ')' { funTyCon_RDR }
1082 | '[' ']' { listTyCon_RDR }
1083 | '[:' ':]' { parrTyCon_RDR }
1084 | '(' commas ')' { tupleTyCon_RDR $2 }
1087 : '(' ')' { unitCon_RDR }
1088 | '[' ']' { nilCon_RDR }
1089 | '(' commas ')' { tupleCon_RDR $2 }
1091 -- the case of '[:' ':]' is part of the production `parr'
1095 | '(' varsym ')' { $2 }
1099 | '(' varsym ')' { $2 }
1100 | '(' qvarsym1 ')' { $2 }
1101 -- We've inlined qvarsym here so that the decision about
1102 -- whether it's a qvar or a var can be postponed until
1103 -- *after* we see the close paren.
1105 ipvar :: { IPName RdrName }
1106 : IPDUPVARID { Dupable (mkUnqual varName $1) }
1107 | IPSPLITVARID { Linear (mkUnqual varName $1) }
1111 | '(' qconsym ')' { $2 }
1113 varop :: { RdrName }
1115 | '`' varid '`' { $2 }
1117 qvarop :: { RdrName }
1119 | '`' qvarid '`' { $2 }
1121 qvaropm :: { RdrName }
1122 : qvarsym_no_minus { $1 }
1123 | '`' qvarid '`' { $2 }
1125 conop :: { RdrName }
1127 | '`' conid '`' { $2 }
1129 qconop :: { RdrName }
1131 | '`' qconid '`' { $2 }
1133 -----------------------------------------------------------------------------
1136 op :: { RdrName } -- used in infix decls
1140 qop :: { RdrName {-HsExpr-} } -- used in sections
1144 qopm :: { RdrNameHsExpr } -- used in sections
1145 : qvaropm { HsVar $1 }
1146 | qconop { HsVar $1 }
1148 -----------------------------------------------------------------------------
1151 qvarid :: { RdrName }
1153 | QVARID { mkQual varName $1 }
1155 varid :: { RdrName }
1156 : varid_no_unsafe { $1 }
1157 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1159 varid_no_unsafe :: { RdrName }
1160 : VARID { mkUnqual varName $1 }
1161 | special_id { mkUnqual varName $1 }
1162 | 'forall' { mkUnqual varName SLIT("forall") }
1164 tyvar :: { RdrName }
1165 : VARID { mkUnqual tvName $1 }
1166 | special_id { mkUnqual tvName $1 }
1167 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1169 -- These special_ids are treated as keywords in various places,
1170 -- but as ordinary ids elsewhere. A special_id collects all thsee
1171 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1172 special_id :: { UserFS }
1174 : 'as' { SLIT("as") }
1175 | 'qualified' { SLIT("qualified") }
1176 | 'hiding' { SLIT("hiding") }
1177 | 'export' { SLIT("export") }
1178 | 'label' { SLIT("label") }
1179 | 'dynamic' { SLIT("dynamic") }
1180 | 'stdcall' { SLIT("stdcall") }
1181 | 'ccall' { SLIT("ccall") }
1183 -----------------------------------------------------------------------------
1186 qconid :: { RdrName }
1188 | QCONID { mkQual dataName $1 }
1190 conid :: { RdrName }
1191 : CONID { mkUnqual dataName $1 }
1193 -----------------------------------------------------------------------------
1196 qconsym :: { RdrName }
1198 | QCONSYM { mkQual dataName $1 }
1200 consym :: { RdrName }
1201 : CONSYM { mkUnqual dataName $1 }
1203 -----------------------------------------------------------------------------
1206 qvarsym :: { RdrName }
1210 qvarsym_no_minus :: { RdrName }
1211 : varsym_no_minus { $1 }
1214 qvarsym1 :: { RdrName }
1215 qvarsym1 : QVARSYM { mkQual varName $1 }
1217 varsym :: { RdrName }
1218 : varsym_no_minus { $1 }
1219 | '-' { mkUnqual varName SLIT("-") }
1221 varsym_no_minus :: { RdrName } -- varsym not including '-'
1222 : VARSYM { mkUnqual varName $1 }
1223 | special_sym { mkUnqual varName $1 }
1226 -- See comments with special_id
1227 special_sym :: { UserFS }
1228 special_sym : '!' { SLIT("!") }
1231 -----------------------------------------------------------------------------
1234 literal :: { HsLit }
1235 : CHAR { HsChar $1 }
1236 | STRING { HsString $1 }
1237 | PRIMINTEGER { HsIntPrim $1 }
1238 | PRIMCHAR { HsCharPrim $1 }
1239 | PRIMSTRING { HsStringPrim $1 }
1240 | PRIMFLOAT { HsFloatPrim $1 }
1241 | PRIMDOUBLE { HsDoublePrim $1 }
1242 | CLITLIT { HsLitLit $1 placeHolderType }
1244 srcloc :: { SrcLoc } : {% getSrcLocP }
1246 -----------------------------------------------------------------------------
1250 : vccurly { () } -- context popped in lexer.
1251 | error {% popContext }
1253 layout_on :: { () } : {% layoutOn True{-strict-} }
1254 layout_on_for_do :: { () } : {% layoutOn False }
1256 -----------------------------------------------------------------------------
1257 -- Miscellaneous (mostly renamings)
1259 modid :: { ModuleName }
1260 : CONID { mkModuleNameFS $1 }
1261 | QCONID { mkModuleNameFS
1263 (unpackFS (fst $1) ++
1264 '.':unpackFS (snd $1)))
1267 tycon :: { RdrName }
1268 : CONID { mkUnqual tcClsName $1 }
1270 tyconop :: { RdrName }
1271 : CONSYM { mkUnqual tcClsName $1 }
1273 qtycon :: { RdrName }
1275 | QCONID { mkQual tcClsName $1 }
1277 qtyconop :: { RdrName }
1279 | QCONSYM { mkQual tcClsName $1 }
1282 : commas ',' { $1 + 1 }
1285 -----------------------------------------------------------------------------
1289 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)