2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.81 2001/12/21 10:24:24 simonmar 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: 14 shift/reduce
47 (note: it's currently 21 -- JRL, 31/1/2000)
49 8 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 1 for ambiguity in 'if x then y else z :: T'
52 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
53 3 for ambiguity in 'case x of y :: a -> b'
54 (don't know whether to reduce 'a' as a btype or shift the '->'.
55 conclusion: bogus expression anyway, doesn't matter)
57 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
58 since 'forall' is a valid variable name, we don't know whether
59 to treat a forall on the input as the beginning of a quantifier
60 or the beginning of the rule itself. Resolving to shift means
61 it's always treated as a quantifier, hence the above is disallowed.
62 This saves explicitly defining a grammar for the rule lhs that
63 doesn't include 'forall'.
65 1 for ambiguity in 'x @ Rec{..}'.
66 Only sensible parse is 'x @ (Rec{..})', which is what resolving
69 -----------------------------------------------------------------------------
73 '_' { ITunderscore } -- Haskell keywords
78 'default' { ITdefault }
79 'deriving' { ITderiving }
89 'instance' { ITinstance }
92 'newtype' { ITnewtype }
94 'qualified' { ITqualified }
98 '_scc_' { ITscc } -- ToDo: remove
100 'forall' { ITforall } -- GHC extension keywords
101 'foreign' { ITforeign }
102 'export' { ITexport }
104 'dynamic' { ITdynamic }
105 'unsafe' { ITunsafe }
107 'stdcall' { ITstdcallconv }
108 'ccall' { ITccallconv }
109 'dotnet' { ITdotnet }
110 '_ccall_' { ITccall (False, False, PlayRisky) }
111 '_ccall_GC_' { ITccall (False, False, PlaySafe) }
112 '_casm_' { ITccall (False, True, PlayRisky) }
113 '_casm_GC_' { ITccall (False, True, PlaySafe) }
115 '{-# SPECIALISE' { ITspecialise_prag }
116 '{-# SOURCE' { ITsource_prag }
117 '{-# INLINE' { ITinline_prag }
118 '{-# NOINLINE' { ITnoinline_prag }
119 '{-# RULES' { ITrules_prag }
120 '{-# SCC' { ITscc_prag }
121 '{-# DEPRECATED' { ITdeprecated_prag }
122 '#-}' { ITclose_prag }
125 '__interface' { ITinterface } -- interface keywords
126 '__export' { IT__export }
127 '__instimport' { ITinstimport }
128 '__forall' { IT__forall }
129 '__letrec' { ITletrec }
130 '__coerce' { ITcoerce }
131 '__depends' { ITdepends }
132 '__inline' { ITinline }
133 '__DEFAULT' { ITdefaultbranch }
135 '__integer' { ITinteger_lit }
136 '__float' { ITfloat_lit }
137 '__rational' { ITrational_lit }
138 '__addr' { ITaddr_lit }
139 '__label' { ITlabel_lit }
140 '__litlit' { ITlit_lit }
141 '__string' { ITstring_lit }
142 '__ccall' { ITccall $$ }
144 '__sccC' { ITsccAllCafs }
147 '__P' { ITspecialise }
150 '__S' { ITstrict $$ }
151 '__M' { ITcprinfo $$ }
154 '..' { ITdotdot } -- reserved symbols
168 '{' { ITocurly } -- special symbols
172 vccurly { ITvccurly } -- virtual close curly (from layout)
183 VARID { ITvarid $$ } -- identifiers
185 VARSYM { ITvarsym $$ }
186 CONSYM { ITconsym $$ }
187 QVARID { ITqvarid $$ }
188 QCONID { ITqconid $$ }
189 QVARSYM { ITqvarsym $$ }
190 QCONSYM { ITqconsym $$ }
192 IPDUPVARID { ITdupipvarid $$ } -- GHC extension
193 IPSPLITVARID { ITsplitipvarid $$ } -- GHC extension
196 STRING { ITstring $$ }
197 INTEGER { ITinteger $$ }
198 RATIONAL { ITrational $$ }
200 PRIMCHAR { ITprimchar $$ }
201 PRIMSTRING { ITprimstring $$ }
202 PRIMINTEGER { ITprimint $$ }
203 PRIMFLOAT { ITprimfloat $$ }
204 PRIMDOUBLE { ITprimdouble $$ }
205 CLITLIT { ITlitlit $$ }
207 %monad { P } { thenP } { returnP }
208 %lexer { lexer } { ITeof }
209 %name parseModule module
210 %name parseStmt maybe_stmt
211 %name parseIdentifier identifier
215 -----------------------------------------------------------------------------
218 -- The place for module deprecation is really too restrictive, but if it
219 -- was allowed at its natural place just before 'module', we get an ugly
220 -- s/r conflict with the second alternative. Another solution would be the
221 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
222 -- either, and DEPRECATED is only expected to be used by people who really
223 -- know what they are doing. :-)
225 module :: { RdrNameHsModule }
226 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
227 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
229 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
231 maybemoddeprec :: { Maybe DeprecTxt }
232 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
233 | {- empty -} { Nothing }
235 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
237 | layout_on top close { $2 }
239 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
240 : importdecls { (reverse $1,[]) }
241 | importdecls ';' cvtopdecls { (reverse $1,$3) }
242 | cvtopdecls { ([],$1) }
244 cvtopdecls :: { [RdrNameHsDecl] }
245 : topdecls { cvTopDecls (groupBindings $1)}
247 -----------------------------------------------------------------------------
250 maybeexports :: { Maybe [RdrNameIE] }
251 : '(' exportlist ')' { Just $2 }
252 | {- empty -} { Nothing }
254 exportlist :: { [RdrNameIE] }
255 : exportlist ',' export { $3 : $1 }
256 | exportlist ',' { $1 }
260 -- GHC extension: we allow things like [] and (,,,) to be exported
261 export :: { RdrNameIE }
263 | gtycon { IEThingAbs $1 }
264 | gtycon '(' '..' ')' { IEThingAll $1 }
265 | gtycon '(' ')' { IEThingWith $1 [] }
266 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
267 | 'module' modid { IEModuleContents $2 }
269 qcnames :: { [RdrName] }
270 : qcnames ',' qcname { $3 : $1 }
273 qcname :: { RdrName }
277 -----------------------------------------------------------------------------
278 -- Import Declarations
280 -- import decls can be *empty*, or even just a string of semicolons
281 -- whereas topdecls must contain at least one topdecl.
283 importdecls :: { [RdrNameImportDecl] }
284 : importdecls ';' importdecl { $3 : $1 }
285 | importdecls ';' { $1 }
286 | importdecl { [ $1 ] }
289 importdecl :: { RdrNameImportDecl }
290 : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec
291 { ImportDecl $5 $3 $4 $6 $7 $2 }
293 maybe_src :: { WhereFrom }
294 : '{-# SOURCE' '#-}' { ImportByUserSource }
295 | {- empty -} { ImportByUser }
297 optqualified :: { Bool }
298 : 'qualified' { True }
299 | {- empty -} { False }
301 maybeas :: { Maybe ModuleName }
302 : 'as' modid { Just $2 }
303 | {- empty -} { Nothing }
305 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
306 : impspec { Just $1 }
307 | {- empty -} { Nothing }
309 impspec :: { (Bool, [RdrNameIE]) }
310 : '(' exportlist ')' { (False, reverse $2) }
311 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
313 -----------------------------------------------------------------------------
314 -- Fixity Declarations
318 | INTEGER {% checkPrec $1 `thenP_`
319 returnP (fromInteger $1) }
321 infix :: { FixityDirection }
323 | 'infixl' { InfixL }
324 | 'infixr' { InfixR }
327 : ops ',' op { $3 : $1 }
330 -----------------------------------------------------------------------------
331 -- Top-Level Declarations
333 topdecls :: { [RdrBinding] }
334 : topdecls ';' topdecl { ($3 : $1) }
335 | topdecls ';' { $1 }
338 topdecl :: { RdrBinding }
339 : srcloc 'type' simpletype '=' ctype
340 -- Note ctype, not sigtype.
341 -- We allow an explicit for-all but we don't insert one
342 -- in type Foo a = (b,b)
343 -- Instead we just say b is out of scope
344 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
346 | srcloc 'data' ctype constrs deriving
347 {% checkDataHeader "data" $3 `thenP` \(cs,c,ts) ->
348 returnP (RdrHsDecl (TyClD
349 (mkTyData DataType cs c ts (reverse $4) (length $4) $5 $1))) }
351 | srcloc 'newtype' ctype '=' newconstr deriving
352 {% checkDataHeader "newtype" $3 `thenP` \(cs,c,ts) ->
353 returnP (RdrHsDecl (TyClD
354 (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
356 | srcloc 'class' ctype fds where
357 {% checkDataHeader "class" $3 `thenP` \(cs,c,ts) ->
359 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
361 returnP (RdrHsDecl (TyClD
362 (mkClassDecl cs c ts $4 sigs (Just binds) $1))) }
364 | srcloc 'instance' inst_type where
366 = cvMonoBindsAndSigs cvInstDeclSig
368 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
370 | srcloc 'default' '(' types0 ')' { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
371 | 'foreign' fordecl { RdrHsDecl $2 }
372 | '{-# DEPRECATED' deprecations '#-}' { $2 }
373 | '{-# RULES' rules '#-}' { $2 }
376 fordecl :: { RdrNameHsDecl }
377 fordecl : srcloc 'label' ext_name varid '::' sigtype
378 { ForD (ForeignImport $4 $6 (LblImport ($3 `orElse` mkExtName $4)) $1) }
381 ----------- ccall/stdcall decls ------------
382 | srcloc 'import' ccallconv ext_name unsafe_flag varid_no_unsafe '::' sigtype
384 call_spec = CCallSpec (StaticTarget ($4 `orElse` mkExtName $6)) $3 $5
386 ForD (ForeignImport $6 $8 (CImport call_spec) $1)
389 | srcloc 'import' ccallconv 'dynamic' unsafe_flag varid_no_unsafe '::' sigtype
391 call_spec = CCallSpec DynamicTarget $3 $5
393 ForD (ForeignImport $6 $8 (CImport call_spec) $1)
396 | srcloc 'export' ccallconv ext_name varid '::' sigtype
397 { ForD (ForeignExport $5 $7 (CExport (CExportStatic ($4 `orElse` mkExtName $5) $3)) $1) }
399 | srcloc 'export' ccallconv 'dynamic' varid '::' sigtype
400 { ForD (ForeignImport $5 $7 (CDynImport $3) $1) }
403 ----------- .NET decls ------------
404 | srcloc 'import' 'dotnet' ext_name varid '::' sigtype
405 { ForD (ForeignImport $5 $7 (DNImport (DNCallSpec ($4 `orElse` mkExtName $5))) $1) }
407 | srcloc 'import' 'dotnet' 'type' ext_name tycon
408 { TyClD (ForeignType $6 $5 DNType $1) }
410 decls :: { [RdrBinding] }
411 : decls ';' decl { $3 : $1 }
416 decl :: { RdrBinding }
419 | '{-# INLINE' srcloc activation qvar '#-}' { RdrSig (InlineSig True $4 $3 $2) }
420 | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
421 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
422 { foldr1 RdrAndBindings
423 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
424 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
425 { RdrSig (SpecInstSig $4 $2) }
427 wherebinds :: { RdrNameHsBinds }
428 : where { cvBinds cvValSig (groupBindings $1) }
430 where :: { [RdrBinding] }
431 : 'where' decllist { $2 }
434 declbinds :: { RdrNameHsBinds }
435 : decllist { cvBinds cvValSig (groupBindings $1) }
437 decllist :: { [RdrBinding] }
438 : '{' decls '}' { $2 }
439 | layout_on decls close { $2 }
441 fixdecl :: { RdrBinding }
442 : srcloc infix prec ops { foldr1 RdrAndBindings
443 [ RdrSig (FixSig (FixitySig n
447 -----------------------------------------------------------------------------
448 -- Transformation Rules
450 rules :: { RdrBinding }
451 : rules ';' rule { $1 `RdrAndBindings` $3 }
454 | {- empty -} { RdrNullBind }
456 rule :: { RdrBinding }
457 : STRING activation rule_forall infixexp '=' srcloc exp
458 { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
460 activation :: { Activation } -- Omitted means AlwaysActive
461 : {- empty -} { AlwaysActive }
462 | '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
464 inverse_activation :: { Activation } -- Omitted means NeverActive
465 : {- empty -} { NeverActive }
466 | '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
468 rule_forall :: { [RdrNameRuleBndr] }
469 : 'forall' rule_var_list '.' { $2 }
472 rule_var_list :: { [RdrNameRuleBndr] }
474 | rule_var rule_var_list { $1 : $2 }
476 rule_var :: { RdrNameRuleBndr }
477 : varid { RuleBndr $1 }
478 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
480 -----------------------------------------------------------------------------
483 deprecations :: { RdrBinding }
484 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
485 | deprecations ';' { $1 }
487 | {- empty -} { RdrNullBind }
489 -- SUP: TEMPORARY HACK, not checking for `module Foo'
490 deprecation :: { RdrBinding }
491 : srcloc depreclist STRING
492 { foldr RdrAndBindings RdrNullBind
493 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
495 -----------------------------------------------------------------------------
496 -- Foreign import/export
498 ccallconv :: { CCallConv }
499 : 'stdcall' { StdCallConv }
500 | 'ccall' { CCallConv }
501 | {- empty -} { defaultCCallConv }
503 unsafe_flag :: { Safety }
504 : 'unsafe' { PlayRisky }
505 | {- empty -} { PlaySafe }
507 ext_name :: { Maybe CLabelString }
509 | STRING STRING { Just $2 } -- Ignore "module name" for now
510 | {- empty -} { Nothing }
513 -----------------------------------------------------------------------------
516 opt_sig :: { Maybe RdrNameHsType }
517 : {- empty -} { Nothing }
518 | '::' sigtype { Just $2 }
520 opt_asig :: { Maybe RdrNameHsType }
521 : {- empty -} { Nothing }
522 | '::' atype { Just $2 }
524 sigtypes :: { [RdrNameHsType] }
526 | sigtypes ',' sigtype { $3 : $1 }
528 sigtype :: { RdrNameHsType }
529 : ctype { (mkHsForAllTy Nothing [] $1) }
531 sig_vars :: { [RdrName] }
532 : sig_vars ',' var { $3 : $1 }
535 -----------------------------------------------------------------------------
538 -- A ctype is a for-all type
539 ctype :: { RdrNameHsType }
540 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
541 | context '=>' type { mkHsForAllTy Nothing $1 $3 }
542 -- A type of form (context => type) is an *implicit* HsForAllTy
545 type :: { RdrNameHsType }
546 : gentype '->' type { HsFunTy $1 $3 }
547 | ipvar '::' type { mkHsIParamTy $1 $3 }
550 gentype :: { RdrNameHsType }
553 | atype tyconop atype { HsOpTy $1 $2 $3 }
555 btype :: { RdrNameHsType }
556 : btype atype { (HsAppTy $1 $2) }
559 atype :: { RdrNameHsType }
560 : gtycon { HsTyVar $1 }
561 | tyvar { HsTyVar $1 }
562 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
563 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
564 | '[' type ']' { HsListTy $2 }
565 | '(' ctype ')' { $2 }
567 | INTEGER { HsNumTy $1 }
569 -- An inst_type is what occurs in the head of an instance decl
570 -- e.g. (Foo a, Gaz b) => Wibble a b
571 -- It's kept as a single type, with a MonoDictTy at the right
572 -- hand corner, for convenience.
573 inst_type :: { RdrNameHsType }
574 : ctype {% checkInstType $1 }
576 types0 :: { [RdrNameHsType] }
577 : types { reverse $1 }
580 types :: { [RdrNameHsType] }
582 | types ',' type { $3 : $1 }
584 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
585 : tycon tyvars { ($1, reverse $2) }
587 tyvars :: { [RdrNameHsTyVar] }
588 : tyvars tyvar { UserTyVar $2 : $1 }
591 fds :: { [([RdrName], [RdrName])] }
593 | '|' fds1 { reverse $2 }
595 fds1 :: { [([RdrName], [RdrName])] }
596 : fds1 ',' fd { $3 : $1 }
599 fd :: { ([RdrName], [RdrName]) }
600 : varids0 '->' varids0 { (reverse $1, reverse $3) }
602 varids0 :: { [RdrName] }
604 | varids0 tyvar { $2 : $1 }
606 -----------------------------------------------------------------------------
607 -- Datatype declarations
609 newconstr :: { RdrNameConDecl }
610 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
611 | srcloc conid '{' var '::' ctype '}'
612 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
614 constrs :: { [RdrNameConDecl] }
615 : {- empty; a GHC extension -} { [] }
616 | '=' constrs1 { $2 }
618 constrs1 :: { [RdrNameConDecl] }
619 : constrs1 '|' constr { $3 : $1 }
622 constr :: { RdrNameConDecl }
623 : srcloc forall context '=>' constr_stuff
624 { mkConDecl (fst $5) $2 $3 (snd $5) $1 }
625 | srcloc forall constr_stuff
626 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
628 forall :: { [RdrNameHsTyVar] }
629 : 'forall' tyvars '.' { $2 }
632 context :: { RdrNameContext }
633 : btype {% checkContext $1 }
635 constr_stuff :: { (RdrName, RdrNameConDetails) }
636 : btype {% mkVanillaCon $1 [] }
637 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
638 | gtycon '{' '}' {% mkRecCon $1 [] }
639 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
640 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
642 satypes :: { [RdrNameBangType] }
643 : atype satypes { unbangedType $1 : $2 }
644 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
647 sbtype :: { RdrNameBangType }
648 : btype { unbangedType $1 }
649 | '!' atype { BangType MarkedUserStrict $2 }
651 fielddecls :: { [([RdrName],RdrNameBangType)] }
652 : fielddecl ',' fielddecls { $1 : $3 }
655 fielddecl :: { ([RdrName],RdrNameBangType) }
656 : sig_vars '::' stype { (reverse $1, $3) }
658 stype :: { RdrNameBangType }
659 : ctype { unbangedType $1 }
660 | '!' atype { BangType MarkedUserStrict $2 }
662 deriving :: { Maybe RdrNameContext }
663 : {- empty -} { Nothing }
664 | 'deriving' context { Just $2 }
665 -- Glasgow extension: allow partial
666 -- applications in derivings
668 -----------------------------------------------------------------------------
671 {- There's an awkward overlap with a type signature. Consider
672 f :: Int -> Int = ...rhs...
673 Then we can't tell whether it's a type signature or a value
674 definition with a result signature until we see the '='.
675 So we have to inline enough to postpone reductions until we know.
679 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
680 instead of qvar, we get another shift/reduce-conflict. Consider the
683 { (^^) :: Int->Int ; } Type signature; only var allowed
685 { (^^) :: Int->Int = ... ; } Value defn with result signature;
686 qvar allowed (because of instance decls)
688 We can't tell whether to reduce var to qvar until after we've read the signatures.
691 valdef :: { RdrBinding }
692 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
693 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
694 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
695 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
699 rhs :: { RdrNameGRHSs }
700 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
701 | gdrhs wherebinds { GRHSs (reverse $1) $2 placeHolderType }
703 gdrhs :: { [RdrNameGRHS] }
704 : gdrhs gdrh { $2 : $1 }
707 gdrh :: { RdrNameGRHS }
708 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
710 -----------------------------------------------------------------------------
713 exp :: { RdrNameHsExpr }
714 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
715 | infixexp 'with' dbinding { HsWith $1 $3 }
718 infixexp :: { RdrNameHsExpr }
720 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
721 (panic "fixity") $3 )}
723 exp10 :: { RdrNameHsExpr }
724 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
725 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
726 returnP (HsLam (Match ps $5
727 (GRHSs (unguardedRHS $8 $7)
728 EmptyBinds placeHolderType))) }
729 | 'let' declbinds 'in' exp { HsLet $2 $4 }
730 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
731 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
732 | '-' fexp { mkHsNegApp $2 }
733 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
734 returnP (HsDo DoExpr stmts $1) }
736 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False placeHolderType }
737 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 PlaySafe False placeHolderType }
738 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True placeHolderType }
739 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 PlaySafe True placeHolderType }
741 | scc_annot exp { if opt_SccProfilingOn
747 scc_annot :: { FAST_STRING }
748 : '_scc_' STRING { $2 }
749 | '{-# SCC' STRING '#-}' { $2 }
751 ccallid :: { FAST_STRING }
755 fexp :: { RdrNameHsExpr }
756 : fexp aexp { (HsApp $1 $2) }
759 aexps0 :: { [RdrNameHsExpr] }
760 : aexps { (reverse $1) }
762 aexps :: { [RdrNameHsExpr] }
763 : aexps aexp { $2 : $1 }
766 aexp :: { RdrNameHsExpr }
767 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
768 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
772 var_or_con :: { RdrNameHsExpr }
776 aexp1 :: { RdrNameHsExpr }
777 : ipvar { HsIPVar $1 }
779 | literal { HsLit $1 }
780 | INTEGER { HsOverLit (mkHsIntegral $1) }
781 | RATIONAL { HsOverLit (mkHsFractional $1) }
782 | '(' exp ')' { HsPar $2 }
783 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
784 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
785 | '[' list ']' { $2 }
786 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
787 | '(' qopm infixexp ')' { (SectionR $2 $3) }
788 | qvar '@' aexp { EAsPat $1 $3 }
790 | '~' aexp1 { ELazyPat $2 }
792 texps :: { [RdrNameHsExpr] }
793 : texps ',' exp { $3 : $1 }
797 -----------------------------------------------------------------------------
800 -- The rules below are little bit contorted to keep lexps left-recursive while
801 -- avoiding another shift/reduce-conflict.
803 list :: { RdrNameHsExpr }
804 : exp { ExplicitList placeHolderType [$1] }
805 | lexps { ExplicitList placeHolderType (reverse $1) }
806 | exp '..' { ArithSeqIn (From $1) }
807 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
808 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
809 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
810 | exp srcloc pquals {% let { body [qs] = qs;
811 body qss = [ParStmt (map reverse qss)] }
813 returnP ( HsDo ListComp
814 (reverse (ResultStmt $1 $2 : body $3))
819 lexps :: { [RdrNameHsExpr] }
820 : lexps ',' exp { $3 : $1 }
821 | exp ',' exp { [$3,$1] }
823 -----------------------------------------------------------------------------
824 -- List Comprehensions
826 pquals :: { [[RdrNameStmt]] }
827 : pquals '|' quals { $3 : $1 }
830 quals :: { [RdrNameStmt] }
831 : quals ',' stmt { $3 : $1 }
834 -----------------------------------------------------------------------------
837 altslist :: { [RdrNameMatch] }
838 : '{' alts '}' { reverse $2 }
839 | layout_on alts close { reverse $2 }
841 alts :: { [RdrNameMatch] }
845 alts1 :: { [RdrNameMatch] }
846 : alts1 ';' alt { $3 : $1 }
850 alt :: { RdrNameMatch }
851 : srcloc infixexp opt_sig ralt wherebinds
852 {% (checkPattern $1 $2 `thenP` \p ->
853 returnP (Match [p] $3
854 (GRHSs $4 $5 placeHolderType)) )}
856 ralt :: { [RdrNameGRHS] }
857 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
858 | gdpats { (reverse $1) }
860 gdpats :: { [RdrNameGRHS] }
861 : gdpats gdpat { $2 : $1 }
864 gdpat :: { RdrNameGRHS }
865 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
867 -----------------------------------------------------------------------------
868 -- Statement sequences
870 stmtlist :: { [RdrNameStmt] }
871 : '{' stmts '}' { $2 }
872 | layout_on_for_do stmts close { $2 }
874 -- do { ;; s ; s ; ; s ;; }
875 -- The last Stmt should be a ResultStmt, but that's hard to enforce
876 -- here, because we need too much lookahead if we see do { e ; }
877 -- So we use ExprStmts throughout, and switch the last one over
878 -- in ParseUtils.checkDo instead
879 stmts :: { [RdrNameStmt] }
880 : stmt stmts_help { $1 : $2 }
884 stmts_help :: { [RdrNameStmt] }
888 -- For typing stmts at the GHCi prompt, where
889 -- the input may consist of just comments.
890 maybe_stmt :: { Maybe RdrNameStmt }
892 | {- nothing -} { Nothing }
894 stmt :: { RdrNameStmt }
895 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
896 returnP (BindStmt p $4 $1) }
897 | srcloc exp { ExprStmt $2 placeHolderType $1 }
898 | srcloc 'let' declbinds { LetStmt $3 }
900 -----------------------------------------------------------------------------
901 -- Record Field Update/Construction
903 fbinds :: { RdrNameHsRecordBinds }
904 : fbinds ',' fbind { $3 : $1 }
909 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
910 : qvar '=' exp { ($1,$3,False) }
912 -----------------------------------------------------------------------------
913 -- Implicit Parameter Bindings
915 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
916 : '{' dbinds '}' { $2 }
917 | layout_on dbinds close { $2 }
919 dbinds :: { [(IPName RdrName, RdrNameHsExpr)] }
920 : dbinds ';' dbind { $3 : $1 }
925 dbind :: { (IPName RdrName, RdrNameHsExpr) }
926 dbind : ipvar '=' exp { ($1, $3) }
928 -----------------------------------------------------------------------------
929 -- Variables, Constructors and Operators.
931 identifier :: { RdrName }
936 depreclist :: { [RdrName] }
937 depreclist : deprec_var { [$1] }
938 | deprec_var ',' depreclist { $1 : $3 }
940 deprec_var :: { RdrName }
941 deprec_var : var { $1 }
944 gtycon :: { RdrName }
946 | '(' qtyconop ')' { $2 }
947 | '(' ')' { unitTyCon_RDR }
948 | '(' '->' ')' { funTyCon_RDR }
949 | '[' ']' { listTyCon_RDR }
950 | '(' commas ')' { tupleTyCon_RDR $2 }
953 : '(' ')' { unitCon_RDR }
954 | '[' ']' { nilCon_RDR }
955 | '(' commas ')' { tupleCon_RDR $2 }
960 | '(' varsym ')' { $2 }
964 | '(' varsym ')' { $2 }
965 | '(' qvarsym1 ')' { $2 }
966 -- We've inlined qvarsym here so that the decision about
967 -- whether it's a qvar or a var can be postponed until
968 -- *after* we see the close paren.
970 ipvar :: { IPName RdrName }
971 : IPDUPVARID { Dupable (mkUnqual varName $1) }
972 | IPSPLITVARID { Linear (mkUnqual varName $1) }
976 | '(' qconsym ')' { $2 }
980 | '`' varid '`' { $2 }
982 qvarop :: { RdrName }
984 | '`' qvarid '`' { $2 }
986 qvaropm :: { RdrName }
987 : qvarsym_no_minus { $1 }
988 | '`' qvarid '`' { $2 }
992 | '`' conid '`' { $2 }
994 qconop :: { RdrName }
996 | '`' qconid '`' { $2 }
998 -----------------------------------------------------------------------------
1001 op :: { RdrName } -- used in infix decls
1005 qop :: { RdrName {-HsExpr-} } -- used in sections
1009 qopm :: { RdrNameHsExpr } -- used in sections
1010 : qvaropm { HsVar $1 }
1011 | qconop { HsVar $1 }
1013 -----------------------------------------------------------------------------
1016 qvarid :: { RdrName }
1018 | QVARID { mkQual varName $1 }
1020 varid :: { RdrName }
1021 : varid_no_unsafe { $1 }
1022 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1024 varid_no_unsafe :: { RdrName }
1025 : VARID { mkUnqual varName $1 }
1026 | special_id { mkUnqual varName $1 }
1027 | 'forall' { mkUnqual varName SLIT("forall") }
1029 tyvar :: { RdrName }
1030 : VARID { mkUnqual tvName $1 }
1031 | special_id { mkUnqual tvName $1 }
1032 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1034 -- These special_ids are treated as keywords in various places,
1035 -- but as ordinary ids elsewhere. A special_id collects all thsee
1036 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1037 special_id :: { UserFS }
1039 : 'as' { SLIT("as") }
1040 | 'qualified' { SLIT("qualified") }
1041 | 'hiding' { SLIT("hiding") }
1042 | 'export' { SLIT("export") }
1043 | 'label' { SLIT("label") }
1044 | 'dynamic' { SLIT("dynamic") }
1045 | 'stdcall' { SLIT("stdcall") }
1046 | 'ccall' { SLIT("ccall") }
1048 -----------------------------------------------------------------------------
1051 qconid :: { RdrName }
1053 | QCONID { mkQual dataName $1 }
1055 conid :: { RdrName }
1056 : CONID { mkUnqual dataName $1 }
1058 -----------------------------------------------------------------------------
1061 qconsym :: { RdrName }
1063 | QCONSYM { mkQual dataName $1 }
1065 consym :: { RdrName }
1066 : CONSYM { mkUnqual dataName $1 }
1068 -----------------------------------------------------------------------------
1071 qvarsym :: { RdrName }
1075 qvarsym_no_minus :: { RdrName }
1076 : varsym_no_minus { $1 }
1079 qvarsym1 :: { RdrName }
1080 qvarsym1 : QVARSYM { mkQual varName $1 }
1082 varsym :: { RdrName }
1083 : varsym_no_minus { $1 }
1084 | '-' { mkUnqual varName SLIT("-") }
1086 varsym_no_minus :: { RdrName } -- varsym not including '-'
1087 : VARSYM { mkUnqual varName $1 }
1088 | special_sym { mkUnqual varName $1 }
1091 -- See comments with special_id
1092 special_sym :: { UserFS }
1093 special_sym : '!' { SLIT("!") }
1096 -----------------------------------------------------------------------------
1099 literal :: { HsLit }
1100 : CHAR { HsChar $1 }
1101 | STRING { HsString $1 }
1102 | PRIMINTEGER { HsIntPrim $1 }
1103 | PRIMCHAR { HsCharPrim $1 }
1104 | PRIMSTRING { HsStringPrim $1 }
1105 | PRIMFLOAT { HsFloatPrim $1 }
1106 | PRIMDOUBLE { HsDoublePrim $1 }
1107 | CLITLIT { HsLitLit $1 placeHolderType }
1109 srcloc :: { SrcLoc } : {% getSrcLocP }
1111 -----------------------------------------------------------------------------
1115 : vccurly { () } -- context popped in lexer.
1116 | error {% popContext }
1118 layout_on :: { () } : {% layoutOn True{-strict-} }
1119 layout_on_for_do :: { () } : {% layoutOn False }
1121 -----------------------------------------------------------------------------
1122 -- Miscellaneous (mostly renamings)
1124 modid :: { ModuleName }
1125 : CONID { mkModuleNameFS $1 }
1126 | QCONID { mkModuleNameFS
1128 (unpackFS (fst $1) ++
1129 '.':unpackFS (snd $1)))
1132 tycon :: { RdrName }
1133 : CONID { mkUnqual tcClsName $1 }
1135 tyconop :: { RdrName }
1136 : CONSYM { mkUnqual tcClsName $1 }
1138 qtycon :: { RdrName }
1140 | QCONID { mkQual tcClsName $1 }
1142 qtyconop :: { RdrName }
1144 | QCONSYM { mkQual tcClsName $1 }
1146 qtycls :: { RdrName }
1150 : commas ',' { $1 + 1 }
1153 -----------------------------------------------------------------------------
1157 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)