2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.67 2001/06/11 12:21:17 simonpj Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parseModule, parseStmt ) 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 Demand ( StrictnessMark(..) )
31 import CmdLineOpts ( opt_SccProfilingOn )
32 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), NewOrData(..) )
36 import CStrings ( CLabelString )
37 import FastString ( tailFS )
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 }
149 '__U' { ITunfold $$ }
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 IPVARID { ITipvarid $$ } -- GHC extension
195 STRING { ITstring $$ }
196 INTEGER { ITinteger $$ }
197 RATIONAL { ITrational $$ }
199 PRIMCHAR { ITprimchar $$ }
200 PRIMSTRING { ITprimstring $$ }
201 PRIMINTEGER { ITprimint $$ }
202 PRIMFLOAT { ITprimfloat $$ }
203 PRIMDOUBLE { ITprimdouble $$ }
204 CLITLIT { ITlitlit $$ }
206 %monad { P } { thenP } { returnP }
207 %lexer { lexer } { ITeof }
208 %name parseModule module
209 %name parseStmt maybe_stmt
213 -----------------------------------------------------------------------------
216 -- The place for module deprecation is really too restrictive, but if it
217 -- was allowed at its natural place just before 'module', we get an ugly
218 -- s/r conflict with the second alternative. Another solution would be the
219 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
220 -- either, and DEPRECATED is only expected to be used by people who really
221 -- know what they are doing. :-)
223 module :: { RdrNameHsModule }
224 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
225 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
227 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
229 maybemoddeprec :: { Maybe DeprecTxt }
230 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
231 | {- empty -} { Nothing }
233 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
235 | layout_on top close { $2 }
237 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
238 : importdecls { (reverse $1,[]) }
239 | importdecls ';' cvtopdecls { (reverse $1,$3) }
240 | cvtopdecls { ([],$1) }
242 cvtopdecls :: { [RdrNameHsDecl] }
243 : topdecls { cvTopDecls (groupBindings $1)}
245 -----------------------------------------------------------------------------
248 maybeexports :: { Maybe [RdrNameIE] }
249 : '(' exportlist ')' { Just $2 }
250 | {- empty -} { Nothing }
252 exportlist :: { [RdrNameIE] }
253 : exportlist ',' export { $3 : $1 }
254 | exportlist ',' { $1 }
258 -- GHC extension: we allow things like [] and (,,,) to be exported
259 export :: { RdrNameIE }
261 | gtycon { IEThingAbs $1 }
262 | gtycon '(' '..' ')' { IEThingAll $1 }
263 | gtycon '(' ')' { IEThingWith $1 [] }
264 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
265 | 'module' modid { IEModuleContents $2 }
267 qcnames :: { [RdrName] }
268 : qcnames ',' qcname { $3 : $1 }
271 qcname :: { RdrName }
275 -----------------------------------------------------------------------------
276 -- Import Declarations
278 -- import decls can be *empty*, or even just a string of semicolons
279 -- whereas topdecls must contain at least one topdecl.
281 importdecls :: { [RdrNameImportDecl] }
282 : importdecls ';' importdecl { $3 : $1 }
283 | importdecls ';' { $1 }
284 | importdecl { [ $1 ] }
287 importdecl :: { RdrNameImportDecl }
288 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
289 { ImportDecl (mkModuleNameFS $5) $3 $4 $6 $7 $2 }
291 maybe_src :: { WhereFrom }
292 : '{-# SOURCE' '#-}' { ImportByUserSource }
293 | {- empty -} { ImportByUser }
295 optqualified :: { Bool }
296 : 'qualified' { True }
297 | {- empty -} { False }
299 maybeas :: { Maybe ModuleName }
300 : 'as' modid { Just $2 }
301 | {- empty -} { Nothing }
303 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
304 : impspec { Just $1 }
305 | {- empty -} { Nothing }
307 impspec :: { (Bool, [RdrNameIE]) }
308 : '(' exportlist ')' { (False, reverse $2) }
309 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
311 -----------------------------------------------------------------------------
312 -- Fixity Declarations
316 | INTEGER {% checkPrec $1 `thenP_`
317 returnP (fromInteger $1) }
319 infix :: { FixityDirection }
321 | 'infixl' { InfixL }
322 | 'infixr' { InfixR }
325 : ops ',' op { $3 : $1 }
328 -----------------------------------------------------------------------------
329 -- Top-Level Declarations
331 topdecls :: { [RdrBinding] }
332 : topdecls ';' topdecl { ($3 : $1) }
333 | topdecls ';' { $1 }
336 topdecl :: { RdrBinding }
337 : srcloc 'type' simpletype '=' ctype
338 -- Note ctype, not sigtype.
339 -- We allow an explicit for-all but we don't insert one
340 -- in type Foo a = (b,b)
341 -- Instead we just say b is out of scope
342 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
344 | srcloc 'data' ctype constrs deriving
345 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
346 returnP (RdrHsDecl (TyClD
347 (mkTyData DataType cs c ts (reverse $4) (length $4) $5 $1))) }
349 | srcloc 'newtype' ctype '=' newconstr deriving
350 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
351 returnP (RdrHsDecl (TyClD
352 (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
354 | srcloc 'class' ctype fds where
355 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
357 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
359 returnP (RdrHsDecl (TyClD
360 (mkClassDecl cs c ts $4 sigs (Just binds) $1))) }
362 | srcloc 'instance' inst_type where
364 = cvMonoBindsAndSigs cvInstDeclSig
366 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
368 | srcloc 'default' '(' types0 ')' { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
369 | 'foreign' fordecl { RdrHsDecl $2 }
370 | '{-# DEPRECATED' deprecations '#-}' { $2 }
371 | '{-# RULES' rules '#-}' { $2 }
374 fordecl :: { RdrNameHsDecl }
375 fordecl : srcloc 'label' ext_name varid '::' sigtype
376 { ForD (ForeignImport $4 $6 (LblImport ($3 `orElse` mkExtName $4)) $1) }
379 ----------- ccall/stdcall decls ------------
380 | srcloc 'import' ccallconv ext_name unsafe_flag varid_no_unsafe '::' sigtype
382 call_spec = CCallSpec (StaticTarget ($4 `orElse` mkExtName $6)) $3 $5
384 ForD (ForeignImport $6 $8 (CImport call_spec) $1)
387 | srcloc 'import' ccallconv 'dynamic' unsafe_flag varid_no_unsafe '::' sigtype
389 call_spec = CCallSpec DynamicTarget $3 $5
391 ForD (ForeignImport $6 $8 (CImport call_spec) $1)
394 | srcloc 'export' ccallconv ext_name varid '::' sigtype
395 { ForD (ForeignExport $5 $7 (CExport (CExportStatic ($4 `orElse` mkExtName $5) $3)) $1) }
397 | srcloc 'export' ccallconv 'dynamic' varid '::' sigtype
398 { ForD (ForeignImport $5 $7 (CDynImport $3) $1) }
401 ----------- .NET decls ------------
402 | srcloc 'import' 'dotnet' ext_name varid '::' sigtype
403 { ForD (ForeignImport $5 $7 (DNImport (DNCallSpec ($4 `orElse` mkExtName $5))) $1) }
405 | srcloc 'import' 'dotnet' 'type' tycon
406 { TyClD (ForeignType $5 DNType $1) }
408 decls :: { [RdrBinding] }
409 : decls ';' decl { $3 : $1 }
414 decl :: { RdrBinding }
417 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
418 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
419 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
420 { foldr1 RdrAndBindings
421 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
422 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
423 { RdrSig (SpecInstSig $4 $2) }
425 opt_phase :: { Maybe Int }
426 : INTEGER { Just (fromInteger $1) }
427 | {- empty -} { Nothing }
429 wherebinds :: { RdrNameHsBinds }
430 : where { cvBinds cvValSig (groupBindings $1) }
432 where :: { [RdrBinding] }
433 : 'where' decllist { $2 }
436 declbinds :: { RdrNameHsBinds }
437 : decllist { cvBinds cvValSig (groupBindings $1) }
439 decllist :: { [RdrBinding] }
440 : '{' decls '}' { $2 }
441 | layout_on decls close { $2 }
443 fixdecl :: { RdrBinding }
444 : srcloc infix prec ops { foldr1 RdrAndBindings
445 [ RdrSig (FixSig (FixitySig n
449 -----------------------------------------------------------------------------
450 -- Transformation Rules
452 rules :: { RdrBinding }
453 : rules ';' rule { $1 `RdrAndBindings` $3 }
456 | {- empty -} { RdrNullBind }
458 rule :: { RdrBinding }
459 : STRING rule_forall infixexp '=' srcloc exp
460 { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
462 rule_forall :: { [RdrNameRuleBndr] }
463 : 'forall' rule_var_list '.' { $2 }
466 rule_var_list :: { [RdrNameRuleBndr] }
468 | rule_var rule_var_list { $1 : $2 }
470 rule_var :: { RdrNameRuleBndr }
471 : varid { RuleBndr $1 }
472 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
474 -----------------------------------------------------------------------------
477 deprecations :: { RdrBinding }
478 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
479 | deprecations ';' { $1 }
481 | {- empty -} { RdrNullBind }
483 -- SUP: TEMPORARY HACK, not checking for `module Foo'
484 deprecation :: { RdrBinding }
485 : srcloc depreclist STRING
486 { foldr RdrAndBindings RdrNullBind
487 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
489 -----------------------------------------------------------------------------
490 -- Foreign import/export
492 ccallconv :: { CCallConv }
493 : 'stdcall' { StdCallConv }
494 | 'ccall' { CCallConv }
495 | {- empty -} { defaultCCallConv }
497 unsafe_flag :: { Safety }
498 : 'unsafe' { PlayRisky }
499 | {- empty -} { PlaySafe }
501 ext_name :: { Maybe CLabelString }
503 | {- empty -} { Nothing }
506 -----------------------------------------------------------------------------
509 opt_sig :: { Maybe RdrNameHsType }
510 : {- empty -} { Nothing }
511 | '::' sigtype { Just $2 }
513 opt_asig :: { Maybe RdrNameHsType }
514 : {- empty -} { Nothing }
515 | '::' atype { Just $2 }
517 sigtypes :: { [RdrNameHsType] }
519 | sigtypes ',' sigtype { $3 : $1 }
521 sigtype :: { RdrNameHsType }
522 : ctype { (mkHsForAllTy Nothing [] $1) }
524 sig_vars :: { [RdrName] }
525 : sig_vars ',' var { $3 : $1 }
528 -----------------------------------------------------------------------------
531 -- A ctype is a for-all type
532 ctype :: { RdrNameHsType }
533 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
534 | context type { mkHsForAllTy Nothing $1 $2 }
535 -- A type of form (context => type) is an *implicit* HsForAllTy
538 type :: { RdrNameHsType }
539 : gentype '->' type { HsFunTy $1 $3 }
540 | ipvar '::' type { mkHsIParamTy $1 $3 }
543 gentype :: { RdrNameHsType }
546 | atype tyconop atype { HsOpTy $1 $2 $3 }
548 btype :: { RdrNameHsType }
549 : btype atype { (HsAppTy $1 $2) }
552 atype :: { RdrNameHsType }
553 : gtycon { HsTyVar $1 }
554 | tyvar { HsTyVar $1 }
555 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
556 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
557 | '[' type ']' { HsListTy $2 }
558 | '(' ctype ')' { $2 }
560 | INTEGER { HsNumTy $1 }
562 -- An inst_type is what occurs in the head of an instance decl
563 -- e.g. (Foo a, Gaz b) => Wibble a b
564 -- It's kept as a single type, with a MonoDictTy at the right
565 -- hand corner, for convenience.
566 inst_type :: { RdrNameHsType }
567 : ctype {% checkInstType $1 }
569 types0 :: { [RdrNameHsType] }
570 : types { reverse $1 }
573 types :: { [RdrNameHsType] }
575 | types ',' type { $3 : $1 }
577 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
578 : tycon tyvars { ($1, reverse $2) }
580 tyvars :: { [RdrNameHsTyVar] }
581 : tyvars tyvar { UserTyVar $2 : $1 }
584 fds :: { [([RdrName], [RdrName])] }
586 | '|' fds1 { reverse $2 }
588 fds1 :: { [([RdrName], [RdrName])] }
589 : fds1 ',' fd { $3 : $1 }
592 fd :: { ([RdrName], [RdrName]) }
593 : varids0 '->' varids0 { (reverse $1, reverse $3) }
595 varids0 :: { [RdrName] }
597 | varids0 tyvar { $2 : $1 }
599 -----------------------------------------------------------------------------
600 -- Datatype declarations
602 newconstr :: { RdrNameConDecl }
603 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
604 | srcloc conid '{' var '::' type '}'
605 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
607 constrs :: { [RdrNameConDecl] }
608 : {- empty; a GHC extension -} { [] }
609 | '=' constrs1 { $2 }
611 constrs1 :: { [RdrNameConDecl] }
612 : constrs1 '|' constr { $3 : $1 }
615 constr :: { RdrNameConDecl }
616 : srcloc forall context constr_stuff
617 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
618 | srcloc forall constr_stuff
619 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
621 forall :: { [RdrNameHsTyVar] }
622 : 'forall' tyvars '.' { $2 }
625 context :: { RdrNameContext }
626 : btype '=>' {% checkContext $1 }
628 constr_stuff :: { (RdrName, RdrNameConDetails) }
629 : btype {% mkVanillaCon $1 [] }
630 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
631 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
632 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
634 satypes :: { [RdrNameBangType] }
635 : atype satypes { unbangedType $1 : $2 }
636 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
639 sbtype :: { RdrNameBangType }
640 : btype { unbangedType $1 }
641 | '!' atype { BangType MarkedUserStrict $2 }
643 fielddecls :: { [([RdrName],RdrNameBangType)] }
644 : fielddecl ',' fielddecls { $1 : $3 }
647 fielddecl :: { ([RdrName],RdrNameBangType) }
648 : sig_vars '::' stype { (reverse $1, $3) }
650 stype :: { RdrNameBangType }
651 : ctype { unbangedType $1 }
652 | '!' atype { BangType MarkedUserStrict $2 }
654 deriving :: { Maybe [RdrName] }
655 : {- empty -} { Nothing }
656 | 'deriving' qtycls { Just [$2] }
657 | 'deriving' '(' ')' { Just [] }
658 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
660 dclasses :: { [RdrName] }
661 : dclasses ',' qtycls { $3 : $1 }
664 -----------------------------------------------------------------------------
667 {- There's an awkward overlap with a type signature. Consider
668 f :: Int -> Int = ...rhs...
669 Then we can't tell whether it's a type signature or a value
670 definition with a result signature until we see the '='.
671 So we have to inline enough to postpone reductions until we know.
675 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
676 instead of qvar, we get another shift/reduce-conflict. Consider the
679 { (^^) :: Int->Int ; } Type signature; only var allowed
681 { (^^) :: Int->Int = ... ; } Value defn with result signature;
682 qvar allowed (because of instance decls)
684 We can't tell whether to reduce var to qvar until after we've read the signatures.
687 valdef :: { RdrBinding }
688 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
689 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
690 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
691 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
695 rhs :: { RdrNameGRHSs }
696 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2)
698 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
700 gdrhs :: { [RdrNameGRHS] }
701 : gdrhs gdrh { $2 : $1 }
704 gdrh :: { RdrNameGRHS }
705 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
707 -----------------------------------------------------------------------------
710 exp :: { RdrNameHsExpr }
711 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
712 | infixexp 'with' dbinding { HsWith $1 $3 }
715 infixexp :: { RdrNameHsExpr }
717 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
718 (panic "fixity") $3 )}
720 exp10 :: { RdrNameHsExpr }
721 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
722 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
723 returnP (HsLam (Match [] ps $5
724 (GRHSs (unguardedRHS $8 $7)
725 EmptyBinds Nothing))) }
726 | 'let' declbinds 'in' exp { HsLet $2 $4 }
727 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
728 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
729 | '-' fexp { mkHsNegApp $2 }
730 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
731 returnP (HsDo DoExpr stmts $1) }
733 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False cbot }
734 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 PlaySafe False cbot }
735 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True cbot }
736 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 PlaySafe True cbot }
738 | scc_annot exp { if opt_SccProfilingOn
744 scc_annot :: { FAST_STRING }
745 : '_scc_' STRING { $2 }
746 | '{-# SCC' STRING '#-}' { $2 }
748 ccallid :: { FAST_STRING }
752 fexp :: { RdrNameHsExpr }
753 : fexp aexp { (HsApp $1 $2) }
756 aexps0 :: { [RdrNameHsExpr] }
757 : aexps { (reverse $1) }
759 aexps :: { [RdrNameHsExpr] }
760 : aexps aexp { $2 : $1 }
763 aexp :: { RdrNameHsExpr }
764 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
765 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
769 var_or_con :: { RdrNameHsExpr }
773 aexp1 :: { RdrNameHsExpr }
774 : ipvar { HsIPVar $1 }
776 | literal { HsLit $1 }
777 | INTEGER { HsOverLit (HsIntegral $1) }
778 | RATIONAL { HsOverLit (HsFractional $1) }
779 | '(' exp ')' { HsPar $2 }
780 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
781 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
782 | '[' list ']' { $2 }
783 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
784 | '(' qopm infixexp ')' { (SectionR $2 $3) }
785 | qvar '@' aexp { EAsPat $1 $3 }
787 | '~' aexp1 { ELazyPat $2 }
789 texps :: { [RdrNameHsExpr] }
790 : texps ',' exp { $3 : $1 }
794 -----------------------------------------------------------------------------
797 -- The rules below are little bit contorted to keep lexps left-recursive while
798 -- avoiding another shift/reduce-conflict.
800 list :: { RdrNameHsExpr }
801 : exp { ExplicitList [$1] }
802 | lexps { ExplicitList (reverse $1) }
803 | exp '..' { ArithSeqIn (From $1) }
804 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
805 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
806 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
807 | exp srcloc pquals {% let { body [qs] = qs;
808 body qss = [ParStmt (map reverse qss)] }
810 returnP ( HsDo ListComp
811 (reverse (ResultStmt $1 $2 : body $3))
816 lexps :: { [RdrNameHsExpr] }
817 : lexps ',' exp { $3 : $1 }
818 | exp ',' exp { [$3,$1] }
820 -----------------------------------------------------------------------------
821 -- List Comprehensions
823 pquals :: { [[RdrNameStmt]] }
824 : pquals '|' quals { $3 : $1 }
827 quals :: { [RdrNameStmt] }
828 : quals ',' stmt { $3 : $1 }
831 -----------------------------------------------------------------------------
834 altslist :: { [RdrNameMatch] }
835 : '{' alts '}' { reverse $2 }
836 | layout_on alts close { reverse $2 }
838 alts :: { [RdrNameMatch] }
842 alts1 :: { [RdrNameMatch] }
843 : alts1 ';' alt { $3 : $1 }
847 alt :: { RdrNameMatch }
848 : srcloc infixexp opt_sig ralt wherebinds
849 {% (checkPattern $1 $2 `thenP` \p ->
850 returnP (Match [] [p] $3
851 (GRHSs $4 $5 Nothing)) )}
853 ralt :: { [RdrNameGRHS] }
854 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
855 | gdpats { (reverse $1) }
857 gdpats :: { [RdrNameGRHS] }
858 : gdpats gdpat { $2 : $1 }
861 gdpat :: { RdrNameGRHS }
862 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
864 -----------------------------------------------------------------------------
865 -- Statement sequences
867 stmtlist :: { [RdrNameStmt] }
868 : '{' stmts '}' { $2 }
869 | layout_on_for_do stmts close { $2 }
871 -- do { ;; s ; s ; ; s ;; }
872 -- The last Stmt should be a ResultStmt, but that's hard to enforce
873 -- here, because we need too much lookahead if we see do { e ; }
874 -- So we use ExprStmts throughout, and switch the last one over
875 -- in ParseUtils.checkDo instead
876 stmts :: { [RdrNameStmt] }
877 : stmt stmts_help { $1 : $2 }
881 stmts_help :: { [RdrNameStmt] }
885 -- For typing stmts at the GHCi prompt, where
886 -- the input may consist of just comments.
887 maybe_stmt :: { Maybe RdrNameStmt }
889 | {- nothing -} { Nothing }
891 stmt :: { RdrNameStmt }
892 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
893 returnP (BindStmt p $4 $1) }
894 | srcloc exp { ExprStmt $2 $1 }
895 | srcloc 'let' declbinds { LetStmt $3 }
897 -----------------------------------------------------------------------------
898 -- Record Field Update/Construction
900 fbinds :: { RdrNameHsRecordBinds }
901 : fbinds ',' fbind { $3 : $1 }
906 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
907 : qvar '=' exp { ($1,$3,False) }
909 -----------------------------------------------------------------------------
910 -- Implicit Parameter Bindings
912 dbinding :: { [(RdrName, RdrNameHsExpr)] }
913 : '{' dbinds '}' { $2 }
914 | layout_on dbinds close { $2 }
916 dbinds :: { [(RdrName, RdrNameHsExpr)] }
917 : dbinds ';' dbind { $3 : $1 }
922 dbind :: { (RdrName, RdrNameHsExpr) }
923 dbind : ipvar '=' exp { ($1, $3) }
925 -----------------------------------------------------------------------------
926 -- Variables, Constructors and Operators.
928 depreclist :: { [RdrName] }
929 depreclist : deprec_var { [$1] }
930 | deprec_var ',' depreclist { $1 : $3 }
932 deprec_var :: { RdrName }
933 deprec_var : var { $1 }
936 gtycon :: { RdrName }
938 | '(' qtyconop ')' { $2 }
939 | '(' ')' { unitTyCon_RDR }
940 | '(' '->' ')' { funTyCon_RDR }
941 | '[' ']' { listTyCon_RDR }
942 | '(' commas ')' { tupleTyCon_RDR $2 }
945 : '(' ')' { unitCon_RDR }
946 | '[' ']' { nilCon_RDR }
947 | '(' commas ')' { tupleCon_RDR $2 }
952 | '(' varsym ')' { $2 }
956 | '(' varsym ')' { $2 }
957 | '(' qvarsym1 ')' { $2 }
958 -- We've inlined qvarsym here so that the decision about
959 -- whether it's a qvar or a var can be postponed until
960 -- *after* we see the close paren.
963 : IPVARID { (mkUnqual varName (tailFS $1)) }
967 | '(' qconsym ')' { $2 }
971 | '`' varid '`' { $2 }
973 qvarop :: { RdrName }
975 | '`' qvarid '`' { $2 }
977 qvaropm :: { RdrName }
978 : qvarsym_no_minus { $1 }
979 | '`' qvarid '`' { $2 }
983 | '`' conid '`' { $2 }
985 qconop :: { RdrName }
987 | '`' qconid '`' { $2 }
989 -----------------------------------------------------------------------------
992 op :: { RdrName } -- used in infix decls
996 qop :: { RdrName {-HsExpr-} } -- used in sections
1000 qopm :: { RdrNameHsExpr } -- used in sections
1001 : qvaropm { HsVar $1 }
1002 | qconop { HsVar $1 }
1004 -----------------------------------------------------------------------------
1007 qvarid :: { RdrName }
1009 | QVARID { mkQual varName $1 }
1011 varid :: { RdrName }
1012 : varid_no_unsafe { $1 }
1013 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1015 varid_no_unsafe :: { RdrName }
1016 : VARID { mkUnqual varName $1 }
1017 | special_id { mkUnqual varName $1 }
1018 | 'forall' { mkUnqual varName SLIT("forall") }
1020 tyvar :: { RdrName }
1021 : VARID { mkUnqual tvName $1 }
1022 | special_id { mkUnqual tvName $1 }
1023 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1025 -- These special_ids are treated as keywords in various places,
1026 -- but as ordinary ids elsewhere. A special_id collects all thsee
1027 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1028 special_id :: { UserFS }
1030 : 'as' { SLIT("as") }
1031 | 'qualified' { SLIT("qualified") }
1032 | 'hiding' { SLIT("hiding") }
1033 | 'export' { SLIT("export") }
1034 | 'label' { SLIT("label") }
1035 | 'dynamic' { SLIT("dynamic") }
1036 | 'stdcall' { SLIT("stdcall") }
1037 | 'ccall' { SLIT("ccall") }
1039 -----------------------------------------------------------------------------
1042 qconid :: { RdrName }
1044 | QCONID { mkQual dataName $1 }
1046 conid :: { RdrName }
1047 : CONID { mkUnqual dataName $1 }
1049 -----------------------------------------------------------------------------
1052 qconsym :: { RdrName }
1054 | QCONSYM { mkQual dataName $1 }
1056 consym :: { RdrName }
1057 : CONSYM { mkUnqual dataName $1 }
1059 -----------------------------------------------------------------------------
1062 qvarsym :: { RdrName }
1066 qvarsym_no_minus :: { RdrName }
1067 : varsym_no_minus { $1 }
1070 qvarsym1 :: { RdrName }
1071 qvarsym1 : QVARSYM { mkQual varName $1 }
1073 varsym :: { RdrName }
1074 : varsym_no_minus { $1 }
1075 | '-' { mkUnqual varName SLIT("-") }
1077 varsym_no_minus :: { RdrName } -- varsym not including '-'
1078 : VARSYM { mkUnqual varName $1 }
1079 | special_sym { mkUnqual varName $1 }
1082 -- See comments with special_id
1083 special_sym :: { UserFS }
1084 special_sym : '!' { SLIT("!") }
1087 -----------------------------------------------------------------------------
1090 literal :: { HsLit }
1091 : CHAR { HsChar $1 }
1092 | STRING { HsString $1 }
1093 | PRIMINTEGER { HsIntPrim $1 }
1094 | PRIMCHAR { HsCharPrim $1 }
1095 | PRIMSTRING { HsStringPrim $1 }
1096 | PRIMFLOAT { HsFloatPrim $1 }
1097 | PRIMDOUBLE { HsDoublePrim $1 }
1098 | CLITLIT { HsLitLit $1 (error "Parser.y: CLITLIT") }
1100 srcloc :: { SrcLoc } : {% getSrcLocP }
1102 -----------------------------------------------------------------------------
1106 : vccurly { () } -- context popped in lexer.
1107 | error {% popContext }
1109 layout_on :: { () } : {% layoutOn True{-strict-} }
1110 layout_on_for_do :: { () } : {% layoutOn False }
1112 -----------------------------------------------------------------------------
1113 -- Miscellaneous (mostly renamings)
1115 modid :: { ModuleName }
1116 : CONID { mkModuleNameFS $1 }
1118 tycon :: { RdrName }
1119 : CONID { mkUnqual tcClsName $1 }
1121 tyconop :: { RdrName }
1122 : CONSYM { mkUnqual tcClsName $1 }
1124 qtycon :: { RdrName }
1126 | QCONID { mkQual tcClsName $1 }
1128 qtyconop :: { RdrName }
1130 | QCONSYM { mkQual tcClsName $1 }
1132 qtycls :: { RdrName }
1136 : commas ',' { $1 + 1 }
1139 -----------------------------------------------------------------------------
1143 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)