2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.83 2002/02/04 03:40:32 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, listTyCon_RDR,
22 tupleTyCon_RDR, 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)
186 VARID { ITvarid $$ } -- identifiers
188 VARSYM { ITvarsym $$ }
189 CONSYM { ITconsym $$ }
190 QVARID { ITqvarid $$ }
191 QCONID { ITqconid $$ }
192 QVARSYM { ITqvarsym $$ }
193 QCONSYM { ITqconsym $$ }
195 IPDUPVARID { ITdupipvarid $$ } -- GHC extension
196 IPSPLITVARID { ITsplitipvarid $$ } -- GHC extension
199 STRING { ITstring $$ }
200 INTEGER { ITinteger $$ }
201 RATIONAL { ITrational $$ }
203 PRIMCHAR { ITprimchar $$ }
204 PRIMSTRING { ITprimstring $$ }
205 PRIMINTEGER { ITprimint $$ }
206 PRIMFLOAT { ITprimfloat $$ }
207 PRIMDOUBLE { ITprimdouble $$ }
208 CLITLIT { ITlitlit $$ }
210 %monad { P } { thenP } { returnP }
211 %lexer { lexer } { ITeof }
212 %name parseModule module
213 %name parseStmt maybe_stmt
214 %name parseIdentifier identifier
218 -----------------------------------------------------------------------------
221 -- The place for module deprecation is really too restrictive, but if it
222 -- was allowed at its natural place just before 'module', we get an ugly
223 -- s/r conflict with the second alternative. Another solution would be the
224 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
225 -- either, and DEPRECATED is only expected to be used by people who really
226 -- know what they are doing. :-)
228 module :: { RdrNameHsModule }
229 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
230 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
232 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
234 maybemoddeprec :: { Maybe DeprecTxt }
235 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
236 | {- empty -} { Nothing }
238 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
240 | layout_on top close { $2 }
242 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
243 : importdecls { (reverse $1,[]) }
244 | importdecls ';' cvtopdecls { (reverse $1,$3) }
245 | cvtopdecls { ([],$1) }
247 cvtopdecls :: { [RdrNameHsDecl] }
248 : topdecls { cvTopDecls (groupBindings $1)}
250 -----------------------------------------------------------------------------
253 maybeexports :: { Maybe [RdrNameIE] }
254 : '(' exportlist ')' { Just $2 }
255 | {- empty -} { Nothing }
257 exportlist :: { [RdrNameIE] }
258 : exportlist ',' export { $3 : $1 }
259 | exportlist ',' { $1 }
263 -- GHC extension: we allow things like [] and (,,,) to be exported
264 export :: { RdrNameIE }
266 | gtycon { IEThingAbs $1 }
267 | gtycon '(' '..' ')' { IEThingAll $1 }
268 | gtycon '(' ')' { IEThingWith $1 [] }
269 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
270 | 'module' modid { IEModuleContents $2 }
272 qcnames :: { [RdrName] }
273 : qcnames ',' qcname { $3 : $1 }
276 qcname :: { RdrName }
280 -----------------------------------------------------------------------------
281 -- Import Declarations
283 -- import decls can be *empty*, or even just a string of semicolons
284 -- whereas topdecls must contain at least one topdecl.
286 importdecls :: { [RdrNameImportDecl] }
287 : importdecls ';' importdecl { $3 : $1 }
288 | importdecls ';' { $1 }
289 | importdecl { [ $1 ] }
292 importdecl :: { RdrNameImportDecl }
293 : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec
294 { ImportDecl $5 $3 $4 $6 $7 $2 }
296 maybe_src :: { WhereFrom }
297 : '{-# SOURCE' '#-}' { ImportByUserSource }
298 | {- empty -} { ImportByUser }
300 optqualified :: { Bool }
301 : 'qualified' { True }
302 | {- empty -} { False }
304 maybeas :: { Maybe ModuleName }
305 : 'as' modid { Just $2 }
306 | {- empty -} { Nothing }
308 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
309 : impspec { Just $1 }
310 | {- empty -} { Nothing }
312 impspec :: { (Bool, [RdrNameIE]) }
313 : '(' exportlist ')' { (False, reverse $2) }
314 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
316 -----------------------------------------------------------------------------
317 -- Fixity Declarations
321 | INTEGER {% checkPrec $1 `thenP_`
322 returnP (fromInteger $1) }
324 infix :: { FixityDirection }
326 | 'infixl' { InfixL }
327 | 'infixr' { InfixR }
330 : ops ',' op { $3 : $1 }
333 -----------------------------------------------------------------------------
334 -- Top-Level Declarations
336 topdecls :: { [RdrBinding] }
337 : topdecls ';' topdecl { ($3 : $1) }
338 | topdecls ';' { $1 }
341 topdecl :: { RdrBinding }
342 : srcloc 'type' simpletype '=' ctype
343 -- Note ctype, not sigtype.
344 -- We allow an explicit for-all but we don't insert one
345 -- in type Foo a = (b,b)
346 -- Instead we just say b is out of scope
347 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
349 | srcloc 'data' ctype constrs deriving
350 {% checkDataHeader "data" $3 `thenP` \(cs,c,ts) ->
351 returnP (RdrHsDecl (TyClD
352 (mkTyData DataType cs c ts (reverse $4) (length $4) $5 $1))) }
354 | srcloc 'newtype' ctype '=' newconstr deriving
355 {% checkDataHeader "newtype" $3 `thenP` \(cs,c,ts) ->
356 returnP (RdrHsDecl (TyClD
357 (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
359 | srcloc 'class' ctype fds where
360 {% checkDataHeader "class" $3 `thenP` \(cs,c,ts) ->
362 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
364 returnP (RdrHsDecl (TyClD
365 (mkClassDecl cs c ts $4 sigs (Just binds) $1))) }
367 | srcloc 'instance' inst_type where
369 = cvMonoBindsAndSigs cvInstDeclSig
371 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
373 | srcloc 'default' '(' types0 ')' { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
374 | 'foreign' fdecl { RdrHsDecl $2 }
375 | '{-# DEPRECATED' deprecations '#-}' { $2 }
376 | '{-# RULES' rules '#-}' { $2 }
379 -- for the time being, the following accepts foreign declarations conforming
380 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
382 -- * a flag indicates whether pre-standard declarations have been used and
383 -- triggers a deprecation warning further down the road
385 -- NB: The first two rules could be combined into one by replacing `safety1'
386 -- with `safety'. However, the combined rule conflicts with the
389 fdecl :: { RdrNameHsDecl }
390 fdecl : srcloc 'import' callconv safety1 fspec {% mkImport $3 $4 $5 $1 }
391 | srcloc 'import' callconv fspec {% mkImport $3 PlaySafe $4 $1 }
392 | srcloc 'export' callconv fspec {% mkExport $3 $4 $1 }
393 -- the following syntax is DEPRECATED
394 | srcloc fdecl1DEPRECATED { ForD ($2 True $1) }
395 | srcloc fdecl2DEPRECATED { $2 $1 }
397 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
399 ----------- DEPRECATED label decls ------------
400 : 'label' ext_name varid '::' sigtype
401 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
402 (CLabel ($2 `orElse` mkExtName $3))) }
404 ----------- DEPRECATED ccall/stdcall decls ------------
406 -- NB: This business with the case expression below may seem overly
407 -- complicated, but it is necessary to avoid some conflicts.
409 -- DEPRECATED variant #1: lack of a calling convention specification
411 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
413 target = StaticTarget ($2 `orElse` mkExtName $4)
415 ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
416 (CFunction target)) }
418 -- DEPRECATED variant #2: external name consists of two separate strings
419 -- (module name and function name) (import)
420 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
422 DNCall -> parseError "Illegal format of .NET foreign import"
423 CCall cconv -> returnP $
425 imp = CFunction (StaticTarget $4)
427 ForeignImport $6 $8 (CImport cconv $5 _NIL_ _NIL_ imp) }
429 -- DEPRECATED variant #3: `unsafe' after entity
430 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
432 DNCall -> parseError "Illegal format of .NET foreign import"
433 CCall cconv -> returnP $
435 imp = CFunction (StaticTarget $3)
437 ForeignImport $5 $7 (CImport cconv PlayRisky _NIL_ _NIL_ imp) }
439 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
440 -- an explicit calling convention (import)
441 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
442 { ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
443 (CFunction DynamicTarget)) }
445 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
446 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
448 DNCall -> parseError "Illegal format of .NET foreign import"
449 CCall cconv -> returnP $
450 ForeignImport $5 $7 (CImport cconv $4 _NIL_ _NIL_
451 (CFunction DynamicTarget)) }
453 -- DEPRECATED variant #6: lack of a calling convention specification
455 | 'export' {-no callconv-} ext_name varid '::' sigtype
456 { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3)
459 -- DEPRECATED variant #7: external name consists of two separate strings
460 -- (module name and function name) (export)
461 | 'export' callconv STRING STRING varid '::' sigtype
463 DNCall -> parseError "Illegal format of .NET foreign import"
464 CCall cconv -> returnP $
466 (CExport (CExportStatic $4 cconv)) }
468 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
469 -- an explicit calling convention (export)
470 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
471 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
474 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
475 | 'export' callconv 'dynamic' varid '::' sigtype
477 DNCall -> parseError "Illegal format of .NET foreign import"
478 CCall cconv -> returnP $
479 ForeignImport $4 $6 (CImport cconv PlaySafe _NIL_ _NIL_ CWrapper) }
481 ----------- DEPRECATED .NET decls ------------
482 -- NB: removed the .NET call declaration, as it is entirely subsumed
483 -- by the new standard FFI declarations
485 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
487 : 'import' 'dotnet' 'type' ext_name tycon
488 { \loc -> TyClD (ForeignType $5 $4 DNType loc) }
489 -- left this one unchanged for the moment as type imports are not
490 -- covered currently by the FFI standard -=chak
492 decls :: { [RdrBinding] }
493 : decls ';' decl { $3 : $1 }
498 decl :: { RdrBinding }
501 | '{-# INLINE' srcloc activation qvar '#-}' { RdrSig (InlineSig True $4 $3 $2) }
502 | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
503 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
504 { foldr1 RdrAndBindings
505 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
506 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
507 { RdrSig (SpecInstSig $4 $2) }
509 wherebinds :: { RdrNameHsBinds }
510 : where { cvBinds cvValSig (groupBindings $1) }
512 where :: { [RdrBinding] }
513 : 'where' decllist { $2 }
516 declbinds :: { RdrNameHsBinds }
517 : decllist { cvBinds cvValSig (groupBindings $1) }
519 decllist :: { [RdrBinding] }
520 : '{' decls '}' { $2 }
521 | layout_on decls close { $2 }
523 fixdecl :: { RdrBinding }
524 : srcloc infix prec ops { foldr1 RdrAndBindings
525 [ RdrSig (FixSig (FixitySig n
529 -----------------------------------------------------------------------------
530 -- Transformation Rules
532 rules :: { RdrBinding }
533 : rules ';' rule { $1 `RdrAndBindings` $3 }
536 | {- empty -} { RdrNullBind }
538 rule :: { RdrBinding }
539 : STRING activation rule_forall infixexp '=' srcloc exp
540 { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
542 activation :: { Activation } -- Omitted means AlwaysActive
543 : {- empty -} { AlwaysActive }
544 | explicit_activation { $1 }
546 inverse_activation :: { Activation } -- Omitted means NeverActive
547 : {- empty -} { NeverActive }
548 | explicit_activation { $1 }
550 explicit_activation :: { Activation } -- In brackets
551 : '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
552 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger $3) }
554 rule_forall :: { [RdrNameRuleBndr] }
555 : 'forall' rule_var_list '.' { $2 }
558 rule_var_list :: { [RdrNameRuleBndr] }
560 | rule_var rule_var_list { $1 : $2 }
562 rule_var :: { RdrNameRuleBndr }
563 : varid { RuleBndr $1 }
564 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
566 -----------------------------------------------------------------------------
569 deprecations :: { RdrBinding }
570 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
571 | deprecations ';' { $1 }
573 | {- empty -} { RdrNullBind }
575 -- SUP: TEMPORARY HACK, not checking for `module Foo'
576 deprecation :: { RdrBinding }
577 : srcloc depreclist STRING
578 { foldr RdrAndBindings RdrNullBind
579 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
581 -----------------------------------------------------------------------------
582 -- Foreign declarations
584 callconv :: { CallConv }
585 : 'stdcall' { CCall StdCallConv }
586 | 'ccall' { CCall CCallConv }
587 | 'dotnet' { DNCall }
590 : 'unsafe' { PlayRisky }
591 | 'safe' { PlaySafe }
592 | {- empty -} { PlaySafe }
594 safety1 :: { Safety }
595 : 'unsafe' { PlayRisky }
596 | 'safe' { PlaySafe }
597 -- only needed to avoid conflicts with the DEPRECATED rules
599 fspec :: { (FAST_STRING, RdrName, RdrNameHsType) }
600 : STRING varid '::' sigtype { ($1 , $2, $4) }
601 | varid '::' sigtype { (SLIT(""), $1, $3) }
602 -- if the entity string is missing, it defaults to the empty string;
603 -- the meaning of an empty entity string depends on the calling
607 ext_name :: { Maybe CLabelString }
609 | STRING STRING { Just $2 } -- Ignore "module name" for now
610 | {- empty -} { Nothing }
613 -----------------------------------------------------------------------------
616 opt_sig :: { Maybe RdrNameHsType }
617 : {- empty -} { Nothing }
618 | '::' sigtype { Just $2 }
620 opt_asig :: { Maybe RdrNameHsType }
621 : {- empty -} { Nothing }
622 | '::' atype { Just $2 }
624 sigtypes :: { [RdrNameHsType] }
626 | sigtypes ',' sigtype { $3 : $1 }
628 sigtype :: { RdrNameHsType }
629 : ctype { (mkHsForAllTy Nothing [] $1) }
631 sig_vars :: { [RdrName] }
632 : sig_vars ',' var { $3 : $1 }
635 -----------------------------------------------------------------------------
638 -- A ctype is a for-all type
639 ctype :: { RdrNameHsType }
640 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
641 | context '=>' type { mkHsForAllTy Nothing $1 $3 }
642 -- A type of form (context => type) is an *implicit* HsForAllTy
645 type :: { RdrNameHsType }
646 : gentype '->' type { HsFunTy $1 $3 }
647 | ipvar '::' type { mkHsIParamTy $1 $3 }
650 gentype :: { RdrNameHsType }
653 | atype tyconop atype { HsOpTy $1 $2 $3 }
655 btype :: { RdrNameHsType }
656 : btype atype { (HsAppTy $1 $2) }
659 atype :: { RdrNameHsType }
660 : gtycon { HsTyVar $1 }
661 | tyvar { HsTyVar $1 }
662 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
663 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
664 | '[' type ']' { HsListTy $2 }
665 | '(' ctype ')' { $2 }
667 | INTEGER { HsNumTy $1 }
669 -- An inst_type is what occurs in the head of an instance decl
670 -- e.g. (Foo a, Gaz b) => Wibble a b
671 -- It's kept as a single type, with a MonoDictTy at the right
672 -- hand corner, for convenience.
673 inst_type :: { RdrNameHsType }
674 : ctype {% checkInstType $1 }
676 types0 :: { [RdrNameHsType] }
677 : types { reverse $1 }
680 types :: { [RdrNameHsType] }
682 | types ',' type { $3 : $1 }
684 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
685 : tycon tyvars { ($1, reverse $2) }
687 tyvars :: { [RdrNameHsTyVar] }
688 : tyvars tyvar { UserTyVar $2 : $1 }
691 fds :: { [([RdrName], [RdrName])] }
693 | '|' fds1 { reverse $2 }
695 fds1 :: { [([RdrName], [RdrName])] }
696 : fds1 ',' fd { $3 : $1 }
699 fd :: { ([RdrName], [RdrName]) }
700 : varids0 '->' varids0 { (reverse $1, reverse $3) }
702 varids0 :: { [RdrName] }
704 | varids0 tyvar { $2 : $1 }
706 -----------------------------------------------------------------------------
707 -- Datatype declarations
709 newconstr :: { RdrNameConDecl }
710 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
711 | srcloc conid '{' var '::' ctype '}'
712 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
714 constrs :: { [RdrNameConDecl] }
715 : {- empty; a GHC extension -} { [] }
716 | '=' constrs1 { $2 }
718 constrs1 :: { [RdrNameConDecl] }
719 : constrs1 '|' constr { $3 : $1 }
722 constr :: { RdrNameConDecl }
723 : srcloc forall context '=>' constr_stuff
724 { mkConDecl (fst $5) $2 $3 (snd $5) $1 }
725 | srcloc forall constr_stuff
726 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
728 forall :: { [RdrNameHsTyVar] }
729 : 'forall' tyvars '.' { $2 }
732 context :: { RdrNameContext }
733 : btype {% checkContext $1 }
735 constr_stuff :: { (RdrName, RdrNameConDetails) }
736 : btype {% mkVanillaCon $1 [] }
737 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
738 | gtycon '{' '}' {% mkRecCon $1 [] }
739 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
740 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
742 satypes :: { [RdrNameBangType] }
743 : atype satypes { unbangedType $1 : $2 }
744 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
747 sbtype :: { RdrNameBangType }
748 : btype { unbangedType $1 }
749 | '!' atype { BangType MarkedUserStrict $2 }
751 fielddecls :: { [([RdrName],RdrNameBangType)] }
752 : fielddecl ',' fielddecls { $1 : $3 }
755 fielddecl :: { ([RdrName],RdrNameBangType) }
756 : sig_vars '::' stype { (reverse $1, $3) }
758 stype :: { RdrNameBangType }
759 : ctype { unbangedType $1 }
760 | '!' atype { BangType MarkedUserStrict $2 }
762 deriving :: { Maybe RdrNameContext }
763 : {- empty -} { Nothing }
764 | 'deriving' context { Just $2 }
765 -- Glasgow extension: allow partial
766 -- applications in derivings
768 -----------------------------------------------------------------------------
771 {- There's an awkward overlap with a type signature. Consider
772 f :: Int -> Int = ...rhs...
773 Then we can't tell whether it's a type signature or a value
774 definition with a result signature until we see the '='.
775 So we have to inline enough to postpone reductions until we know.
779 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
780 instead of qvar, we get another shift/reduce-conflict. Consider the
783 { (^^) :: Int->Int ; } Type signature; only var allowed
785 { (^^) :: Int->Int = ... ; } Value defn with result signature;
786 qvar allowed (because of instance decls)
788 We can't tell whether to reduce var to qvar until after we've read the signatures.
791 valdef :: { RdrBinding }
792 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
793 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
794 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
795 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
799 rhs :: { RdrNameGRHSs }
800 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
801 | gdrhs wherebinds { GRHSs (reverse $1) $2 placeHolderType }
803 gdrhs :: { [RdrNameGRHS] }
804 : gdrhs gdrh { $2 : $1 }
807 gdrh :: { RdrNameGRHS }
808 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
810 -----------------------------------------------------------------------------
813 exp :: { RdrNameHsExpr }
814 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
815 | infixexp 'with' dbinding { HsWith $1 $3 }
818 infixexp :: { RdrNameHsExpr }
820 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
821 (panic "fixity") $3 )}
823 exp10 :: { RdrNameHsExpr }
824 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
825 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
826 returnP (HsLam (Match ps $5
827 (GRHSs (unguardedRHS $8 $7)
828 EmptyBinds placeHolderType))) }
829 | 'let' declbinds 'in' exp { HsLet $2 $4 }
830 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
831 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
832 | '-' fexp { mkHsNegApp $2 }
833 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
834 returnP (HsDo DoExpr stmts $1) }
836 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False placeHolderType }
837 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 PlaySafe False placeHolderType }
838 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True placeHolderType }
839 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 PlaySafe True placeHolderType }
841 | scc_annot exp { if opt_SccProfilingOn
847 scc_annot :: { FAST_STRING }
848 : '_scc_' STRING { $2 }
849 | '{-# SCC' STRING '#-}' { $2 }
851 ccallid :: { FAST_STRING }
855 fexp :: { RdrNameHsExpr }
856 : fexp aexp { (HsApp $1 $2) }
859 aexps0 :: { [RdrNameHsExpr] }
860 : aexps { (reverse $1) }
862 aexps :: { [RdrNameHsExpr] }
863 : aexps aexp { $2 : $1 }
866 aexp :: { RdrNameHsExpr }
867 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
868 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
872 var_or_con :: { RdrNameHsExpr }
876 aexp1 :: { RdrNameHsExpr }
877 : ipvar { HsIPVar $1 }
879 | literal { HsLit $1 }
880 | INTEGER { HsOverLit (mkHsIntegral $1) }
881 | RATIONAL { HsOverLit (mkHsFractional $1) }
882 | '(' exp ')' { HsPar $2 }
883 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
884 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
885 | '[' list ']' { $2 }
886 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
887 | '(' qopm infixexp ')' { (SectionR $2 $3) }
888 | qvar '@' aexp { EAsPat $1 $3 }
890 | '~' aexp1 { ELazyPat $2 }
892 texps :: { [RdrNameHsExpr] }
893 : texps ',' exp { $3 : $1 }
897 -----------------------------------------------------------------------------
900 -- The rules below are little bit contorted to keep lexps left-recursive while
901 -- avoiding another shift/reduce-conflict.
903 list :: { RdrNameHsExpr }
904 : exp { ExplicitList placeHolderType [$1] }
905 | lexps { ExplicitList placeHolderType (reverse $1) }
906 | exp '..' { ArithSeqIn (From $1) }
907 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
908 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
909 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
910 | exp srcloc pquals {% let { body [qs] = qs;
911 body qss = [ParStmt (map reverse qss)] }
913 returnP ( HsDo ListComp
914 (reverse (ResultStmt $1 $2 : body $3))
919 lexps :: { [RdrNameHsExpr] }
920 : lexps ',' exp { $3 : $1 }
921 | exp ',' exp { [$3,$1] }
923 -----------------------------------------------------------------------------
924 -- List Comprehensions
926 pquals :: { [[RdrNameStmt]] }
927 : pquals '|' quals { $3 : $1 }
930 quals :: { [RdrNameStmt] }
931 : quals ',' stmt { $3 : $1 }
934 -----------------------------------------------------------------------------
937 altslist :: { [RdrNameMatch] }
938 : '{' alts '}' { reverse $2 }
939 | layout_on alts close { reverse $2 }
941 alts :: { [RdrNameMatch] }
945 alts1 :: { [RdrNameMatch] }
946 : alts1 ';' alt { $3 : $1 }
950 alt :: { RdrNameMatch }
951 : srcloc infixexp opt_sig ralt wherebinds
952 {% (checkPattern $1 $2 `thenP` \p ->
953 returnP (Match [p] $3
954 (GRHSs $4 $5 placeHolderType)) )}
956 ralt :: { [RdrNameGRHS] }
957 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
958 | gdpats { (reverse $1) }
960 gdpats :: { [RdrNameGRHS] }
961 : gdpats gdpat { $2 : $1 }
964 gdpat :: { RdrNameGRHS }
965 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
967 -----------------------------------------------------------------------------
968 -- Statement sequences
970 stmtlist :: { [RdrNameStmt] }
971 : '{' stmts '}' { $2 }
972 | layout_on_for_do stmts close { $2 }
974 -- do { ;; s ; s ; ; s ;; }
975 -- The last Stmt should be a ResultStmt, but that's hard to enforce
976 -- here, because we need too much lookahead if we see do { e ; }
977 -- So we use ExprStmts throughout, and switch the last one over
978 -- in ParseUtils.checkDo instead
979 stmts :: { [RdrNameStmt] }
980 : stmt stmts_help { $1 : $2 }
984 stmts_help :: { [RdrNameStmt] }
988 -- For typing stmts at the GHCi prompt, where
989 -- the input may consist of just comments.
990 maybe_stmt :: { Maybe RdrNameStmt }
992 | {- nothing -} { Nothing }
994 stmt :: { RdrNameStmt }
995 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
996 returnP (BindStmt p $4 $1) }
997 | srcloc exp { ExprStmt $2 placeHolderType $1 }
998 | srcloc 'let' declbinds { LetStmt $3 }
1000 -----------------------------------------------------------------------------
1001 -- Record Field Update/Construction
1003 fbinds :: { RdrNameHsRecordBinds }
1004 : fbinds ',' fbind { $3 : $1 }
1007 | {- empty -} { [] }
1009 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
1010 : qvar '=' exp { ($1,$3,False) }
1012 -----------------------------------------------------------------------------
1013 -- Implicit Parameter Bindings
1015 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1016 : '{' dbinds '}' { $2 }
1017 | layout_on dbinds close { $2 }
1019 dbinds :: { [(IPName RdrName, RdrNameHsExpr)] }
1020 : dbinds ';' dbind { $3 : $1 }
1023 | {- empty -} { [] }
1025 dbind :: { (IPName RdrName, RdrNameHsExpr) }
1026 dbind : ipvar '=' exp { ($1, $3) }
1028 -----------------------------------------------------------------------------
1029 -- Variables, Constructors and Operators.
1031 identifier :: { RdrName }
1036 depreclist :: { [RdrName] }
1037 depreclist : deprec_var { [$1] }
1038 | deprec_var ',' depreclist { $1 : $3 }
1040 deprec_var :: { RdrName }
1041 deprec_var : var { $1 }
1044 gtycon :: { RdrName }
1046 | '(' qtyconop ')' { $2 }
1047 | '(' ')' { unitTyCon_RDR }
1048 | '(' '->' ')' { funTyCon_RDR }
1049 | '[' ']' { listTyCon_RDR }
1050 | '(' commas ')' { tupleTyCon_RDR $2 }
1053 : '(' ')' { unitCon_RDR }
1054 | '[' ']' { nilCon_RDR }
1055 | '(' commas ')' { tupleCon_RDR $2 }
1060 | '(' varsym ')' { $2 }
1064 | '(' varsym ')' { $2 }
1065 | '(' qvarsym1 ')' { $2 }
1066 -- We've inlined qvarsym here so that the decision about
1067 -- whether it's a qvar or a var can be postponed until
1068 -- *after* we see the close paren.
1070 ipvar :: { IPName RdrName }
1071 : IPDUPVARID { Dupable (mkUnqual varName $1) }
1072 | IPSPLITVARID { Linear (mkUnqual varName $1) }
1076 | '(' qconsym ')' { $2 }
1078 varop :: { RdrName }
1080 | '`' varid '`' { $2 }
1082 qvarop :: { RdrName }
1084 | '`' qvarid '`' { $2 }
1086 qvaropm :: { RdrName }
1087 : qvarsym_no_minus { $1 }
1088 | '`' qvarid '`' { $2 }
1090 conop :: { RdrName }
1092 | '`' conid '`' { $2 }
1094 qconop :: { RdrName }
1096 | '`' qconid '`' { $2 }
1098 -----------------------------------------------------------------------------
1101 op :: { RdrName } -- used in infix decls
1105 qop :: { RdrName {-HsExpr-} } -- used in sections
1109 qopm :: { RdrNameHsExpr } -- used in sections
1110 : qvaropm { HsVar $1 }
1111 | qconop { HsVar $1 }
1113 -----------------------------------------------------------------------------
1116 qvarid :: { RdrName }
1118 | QVARID { mkQual varName $1 }
1120 varid :: { RdrName }
1121 : varid_no_unsafe { $1 }
1122 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1124 varid_no_unsafe :: { RdrName }
1125 : VARID { mkUnqual varName $1 }
1126 | special_id { mkUnqual varName $1 }
1127 | 'forall' { mkUnqual varName SLIT("forall") }
1129 tyvar :: { RdrName }
1130 : VARID { mkUnqual tvName $1 }
1131 | special_id { mkUnqual tvName $1 }
1132 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1134 -- These special_ids are treated as keywords in various places,
1135 -- but as ordinary ids elsewhere. A special_id collects all thsee
1136 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1137 special_id :: { UserFS }
1139 : 'as' { SLIT("as") }
1140 | 'qualified' { SLIT("qualified") }
1141 | 'hiding' { SLIT("hiding") }
1142 | 'export' { SLIT("export") }
1143 | 'label' { SLIT("label") }
1144 | 'dynamic' { SLIT("dynamic") }
1145 | 'stdcall' { SLIT("stdcall") }
1146 | 'ccall' { SLIT("ccall") }
1148 -----------------------------------------------------------------------------
1151 qconid :: { RdrName }
1153 | QCONID { mkQual dataName $1 }
1155 conid :: { RdrName }
1156 : CONID { mkUnqual dataName $1 }
1158 -----------------------------------------------------------------------------
1161 qconsym :: { RdrName }
1163 | QCONSYM { mkQual dataName $1 }
1165 consym :: { RdrName }
1166 : CONSYM { mkUnqual dataName $1 }
1168 -----------------------------------------------------------------------------
1171 qvarsym :: { RdrName }
1175 qvarsym_no_minus :: { RdrName }
1176 : varsym_no_minus { $1 }
1179 qvarsym1 :: { RdrName }
1180 qvarsym1 : QVARSYM { mkQual varName $1 }
1182 varsym :: { RdrName }
1183 : varsym_no_minus { $1 }
1184 | '-' { mkUnqual varName SLIT("-") }
1186 varsym_no_minus :: { RdrName } -- varsym not including '-'
1187 : VARSYM { mkUnqual varName $1 }
1188 | special_sym { mkUnqual varName $1 }
1191 -- See comments with special_id
1192 special_sym :: { UserFS }
1193 special_sym : '!' { SLIT("!") }
1196 -----------------------------------------------------------------------------
1199 literal :: { HsLit }
1200 : CHAR { HsChar $1 }
1201 | STRING { HsString $1 }
1202 | PRIMINTEGER { HsIntPrim $1 }
1203 | PRIMCHAR { HsCharPrim $1 }
1204 | PRIMSTRING { HsStringPrim $1 }
1205 | PRIMFLOAT { HsFloatPrim $1 }
1206 | PRIMDOUBLE { HsDoublePrim $1 }
1207 | CLITLIT { HsLitLit $1 placeHolderType }
1209 srcloc :: { SrcLoc } : {% getSrcLocP }
1211 -----------------------------------------------------------------------------
1215 : vccurly { () } -- context popped in lexer.
1216 | error {% popContext }
1218 layout_on :: { () } : {% layoutOn True{-strict-} }
1219 layout_on_for_do :: { () } : {% layoutOn False }
1221 -----------------------------------------------------------------------------
1222 -- Miscellaneous (mostly renamings)
1224 modid :: { ModuleName }
1225 : CONID { mkModuleNameFS $1 }
1226 | QCONID { mkModuleNameFS
1228 (unpackFS (fst $1) ++
1229 '.':unpackFS (snd $1)))
1232 tycon :: { RdrName }
1233 : CONID { mkUnqual tcClsName $1 }
1235 tyconop :: { RdrName }
1236 : CONSYM { mkUnqual tcClsName $1 }
1238 qtycon :: { RdrName }
1240 | QCONID { mkQual tcClsName $1 }
1242 qtyconop :: { RdrName }
1244 | QCONSYM { mkQual tcClsName $1 }
1247 : commas ',' { $1 + 1 }
1250 -----------------------------------------------------------------------------
1254 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)