2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.75 2001/10/22 09:37:24 simonpj Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parseModule, parseStmt, parseIdentifier ) where
15 import HsTypes ( mkHsTupCon )
21 import PrelNames ( mAIN_Name, unitTyCon_RDR, funTyCon_RDR, 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(..),
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 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
210 %name parseIdentifier identifier
214 -----------------------------------------------------------------------------
217 -- The place for module deprecation is really too restrictive, but if it
218 -- was allowed at its natural place just before 'module', we get an ugly
219 -- s/r conflict with the second alternative. Another solution would be the
220 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
221 -- either, and DEPRECATED is only expected to be used by people who really
222 -- know what they are doing. :-)
224 module :: { RdrNameHsModule }
225 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
226 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
228 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
230 maybemoddeprec :: { Maybe DeprecTxt }
231 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
232 | {- empty -} { Nothing }
234 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
236 | layout_on top close { $2 }
238 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
239 : importdecls { (reverse $1,[]) }
240 | importdecls ';' cvtopdecls { (reverse $1,$3) }
241 | cvtopdecls { ([],$1) }
243 cvtopdecls :: { [RdrNameHsDecl] }
244 : topdecls { cvTopDecls (groupBindings $1)}
246 -----------------------------------------------------------------------------
249 maybeexports :: { Maybe [RdrNameIE] }
250 : '(' exportlist ')' { Just $2 }
251 | {- empty -} { Nothing }
253 exportlist :: { [RdrNameIE] }
254 : exportlist ',' export { $3 : $1 }
255 | exportlist ',' { $1 }
259 -- GHC extension: we allow things like [] and (,,,) to be exported
260 export :: { RdrNameIE }
262 | gtycon { IEThingAbs $1 }
263 | gtycon '(' '..' ')' { IEThingAll $1 }
264 | gtycon '(' ')' { IEThingWith $1 [] }
265 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
266 | 'module' modid { IEModuleContents $2 }
268 qcnames :: { [RdrName] }
269 : qcnames ',' qcname { $3 : $1 }
272 qcname :: { RdrName }
276 -----------------------------------------------------------------------------
277 -- Import Declarations
279 -- import decls can be *empty*, or even just a string of semicolons
280 -- whereas topdecls must contain at least one topdecl.
282 importdecls :: { [RdrNameImportDecl] }
283 : importdecls ';' importdecl { $3 : $1 }
284 | importdecls ';' { $1 }
285 | importdecl { [ $1 ] }
288 importdecl :: { RdrNameImportDecl }
289 : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec
290 { ImportDecl $5 $3 $4 $6 $7 $2 }
292 maybe_src :: { WhereFrom }
293 : '{-# SOURCE' '#-}' { ImportByUserSource }
294 | {- empty -} { ImportByUser }
296 optqualified :: { Bool }
297 : 'qualified' { True }
298 | {- empty -} { False }
300 maybeas :: { Maybe ModuleName }
301 : 'as' modid { Just $2 }
302 | {- empty -} { Nothing }
304 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
305 : impspec { Just $1 }
306 | {- empty -} { Nothing }
308 impspec :: { (Bool, [RdrNameIE]) }
309 : '(' exportlist ')' { (False, reverse $2) }
310 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
312 -----------------------------------------------------------------------------
313 -- Fixity Declarations
317 | INTEGER {% checkPrec $1 `thenP_`
318 returnP (fromInteger $1) }
320 infix :: { FixityDirection }
322 | 'infixl' { InfixL }
323 | 'infixr' { InfixR }
326 : ops ',' op { $3 : $1 }
329 -----------------------------------------------------------------------------
330 -- Top-Level Declarations
332 topdecls :: { [RdrBinding] }
333 : topdecls ';' topdecl { ($3 : $1) }
334 | topdecls ';' { $1 }
337 topdecl :: { RdrBinding }
338 : srcloc 'type' simpletype '=' ctype
339 -- Note ctype, not sigtype.
340 -- We allow an explicit for-all but we don't insert one
341 -- in type Foo a = (b,b)
342 -- Instead we just say b is out of scope
343 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
345 | srcloc 'data' ctype constrs deriving
346 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
347 returnP (RdrHsDecl (TyClD
348 (mkTyData DataType cs c ts (reverse $4) (length $4) $5 $1))) }
350 | srcloc 'newtype' ctype '=' newconstr deriving
351 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
352 returnP (RdrHsDecl (TyClD
353 (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
355 | srcloc 'class' ctype fds where
356 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
358 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
360 returnP (RdrHsDecl (TyClD
361 (mkClassDecl cs c ts $4 sigs (Just binds) $1))) }
363 | srcloc 'instance' inst_type where
365 = cvMonoBindsAndSigs cvInstDeclSig
367 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
369 | srcloc 'default' '(' types0 ')' { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
370 | 'foreign' fordecl { RdrHsDecl $2 }
371 | '{-# DEPRECATED' deprecations '#-}' { $2 }
372 | '{-# RULES' rules '#-}' { $2 }
375 fordecl :: { RdrNameHsDecl }
376 fordecl : srcloc 'label' ext_name varid '::' sigtype
377 { ForD (ForeignImport $4 $6 (LblImport ($3 `orElse` mkExtName $4)) $1) }
380 ----------- ccall/stdcall decls ------------
381 | srcloc 'import' ccallconv ext_name unsafe_flag varid_no_unsafe '::' sigtype
383 call_spec = CCallSpec (StaticTarget ($4 `orElse` mkExtName $6)) $3 $5
385 ForD (ForeignImport $6 $8 (CImport call_spec) $1)
388 | srcloc 'import' ccallconv 'dynamic' unsafe_flag varid_no_unsafe '::' sigtype
390 call_spec = CCallSpec DynamicTarget $3 $5
392 ForD (ForeignImport $6 $8 (CImport call_spec) $1)
395 | srcloc 'export' ccallconv ext_name varid '::' sigtype
396 { ForD (ForeignExport $5 $7 (CExport (CExportStatic ($4 `orElse` mkExtName $5) $3)) $1) }
398 | srcloc 'export' ccallconv 'dynamic' varid '::' sigtype
399 { ForD (ForeignImport $5 $7 (CDynImport $3) $1) }
402 ----------- .NET decls ------------
403 | srcloc 'import' 'dotnet' ext_name varid '::' sigtype
404 { ForD (ForeignImport $5 $7 (DNImport (DNCallSpec ($4 `orElse` mkExtName $5))) $1) }
406 | srcloc 'import' 'dotnet' 'type' ext_name tycon
407 { TyClD (ForeignType $6 $5 DNType $1) }
409 decls :: { [RdrBinding] }
410 : decls ';' decl { $3 : $1 }
415 decl :: { RdrBinding }
418 | '{-# INLINE' srcloc activation qvar '#-}' { RdrSig (InlineSig True $4 $3 $2) }
419 | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
420 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
421 { foldr1 RdrAndBindings
422 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
423 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
424 { RdrSig (SpecInstSig $4 $2) }
426 wherebinds :: { RdrNameHsBinds }
427 : where { cvBinds cvValSig (groupBindings $1) }
429 where :: { [RdrBinding] }
430 : 'where' decllist { $2 }
433 declbinds :: { RdrNameHsBinds }
434 : decllist { cvBinds cvValSig (groupBindings $1) }
436 decllist :: { [RdrBinding] }
437 : '{' decls '}' { $2 }
438 | layout_on decls close { $2 }
440 fixdecl :: { RdrBinding }
441 : srcloc infix prec ops { foldr1 RdrAndBindings
442 [ RdrSig (FixSig (FixitySig n
446 -----------------------------------------------------------------------------
447 -- Transformation Rules
449 rules :: { RdrBinding }
450 : rules ';' rule { $1 `RdrAndBindings` $3 }
453 | {- empty -} { RdrNullBind }
455 rule :: { RdrBinding }
456 : STRING activation rule_forall infixexp '=' srcloc exp
457 { RdrHsDecl (RuleD (HsRule $1 $2 [] $3 $4 $7 $6)) }
459 activation :: { Activation } -- Omitted means AlwaysActive
460 : {- empty -} { AlwaysActive }
461 | '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
463 inverse_activation :: { Activation } -- Omitted means NeverActive
464 : {- empty -} { NeverActive }
465 | '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
467 rule_forall :: { [RdrNameRuleBndr] }
468 : 'forall' rule_var_list '.' { $2 }
471 rule_var_list :: { [RdrNameRuleBndr] }
473 | rule_var rule_var_list { $1 : $2 }
475 rule_var :: { RdrNameRuleBndr }
476 : varid { RuleBndr $1 }
477 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
479 -----------------------------------------------------------------------------
482 deprecations :: { RdrBinding }
483 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
484 | deprecations ';' { $1 }
486 | {- empty -} { RdrNullBind }
488 -- SUP: TEMPORARY HACK, not checking for `module Foo'
489 deprecation :: { RdrBinding }
490 : srcloc depreclist STRING
491 { foldr RdrAndBindings RdrNullBind
492 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
494 -----------------------------------------------------------------------------
495 -- Foreign import/export
497 ccallconv :: { CCallConv }
498 : 'stdcall' { StdCallConv }
499 | 'ccall' { CCallConv }
500 | {- empty -} { defaultCCallConv }
502 unsafe_flag :: { Safety }
503 : 'unsafe' { PlayRisky }
504 | {- empty -} { PlaySafe }
506 ext_name :: { Maybe CLabelString }
508 | STRING STRING { Just $2 } -- Ignore "module name" for now
509 | {- empty -} { Nothing }
512 -----------------------------------------------------------------------------
515 opt_sig :: { Maybe RdrNameHsType }
516 : {- empty -} { Nothing }
517 | '::' sigtype { Just $2 }
519 opt_asig :: { Maybe RdrNameHsType }
520 : {- empty -} { Nothing }
521 | '::' atype { Just $2 }
523 sigtypes :: { [RdrNameHsType] }
525 | sigtypes ',' sigtype { $3 : $1 }
527 sigtype :: { RdrNameHsType }
528 : ctype { (mkHsForAllTy Nothing [] $1) }
530 sig_vars :: { [RdrName] }
531 : sig_vars ',' var { $3 : $1 }
534 -----------------------------------------------------------------------------
537 -- A ctype is a for-all type
538 ctype :: { RdrNameHsType }
539 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
540 | context type { mkHsForAllTy Nothing $1 $2 }
541 -- A type of form (context => type) is an *implicit* HsForAllTy
544 type :: { RdrNameHsType }
545 : gentype '->' type { HsFunTy $1 $3 }
546 | ipvar '::' type { mkHsIParamTy $1 $3 }
549 gentype :: { RdrNameHsType }
552 | atype tyconop atype { HsOpTy $1 $2 $3 }
554 btype :: { RdrNameHsType }
555 : btype atype { (HsAppTy $1 $2) }
558 atype :: { RdrNameHsType }
559 : gtycon { HsTyVar $1 }
560 | tyvar { HsTyVar $1 }
561 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
562 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
563 | '[' type ']' { HsListTy $2 }
564 | '(' ctype ')' { $2 }
566 | INTEGER { HsNumTy $1 }
568 -- An inst_type is what occurs in the head of an instance decl
569 -- e.g. (Foo a, Gaz b) => Wibble a b
570 -- It's kept as a single type, with a MonoDictTy at the right
571 -- hand corner, for convenience.
572 inst_type :: { RdrNameHsType }
573 : ctype {% checkInstType $1 }
575 types0 :: { [RdrNameHsType] }
576 : types { reverse $1 }
579 types :: { [RdrNameHsType] }
581 | types ',' type { $3 : $1 }
583 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
584 : tycon tyvars { ($1, reverse $2) }
586 tyvars :: { [RdrNameHsTyVar] }
587 : tyvars tyvar { UserTyVar $2 : $1 }
590 fds :: { [([RdrName], [RdrName])] }
592 | '|' fds1 { reverse $2 }
594 fds1 :: { [([RdrName], [RdrName])] }
595 : fds1 ',' fd { $3 : $1 }
598 fd :: { ([RdrName], [RdrName]) }
599 : varids0 '->' varids0 { (reverse $1, reverse $3) }
601 varids0 :: { [RdrName] }
603 | varids0 tyvar { $2 : $1 }
605 -----------------------------------------------------------------------------
606 -- Datatype declarations
608 newconstr :: { RdrNameConDecl }
609 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
610 | srcloc conid '{' var '::' ctype '}'
611 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
613 constrs :: { [RdrNameConDecl] }
614 : {- empty; a GHC extension -} { [] }
615 | '=' constrs1 { $2 }
617 constrs1 :: { [RdrNameConDecl] }
618 : constrs1 '|' constr { $3 : $1 }
621 constr :: { RdrNameConDecl }
622 : srcloc forall context constr_stuff
623 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
624 | srcloc forall constr_stuff
625 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
627 forall :: { [RdrNameHsTyVar] }
628 : 'forall' tyvars '.' { $2 }
631 context :: { RdrNameContext }
632 : btype '=>' {% checkContext $1 }
634 constr_stuff :: { (RdrName, RdrNameConDetails) }
635 : btype {% mkVanillaCon $1 [] }
636 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
637 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
638 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
640 satypes :: { [RdrNameBangType] }
641 : atype satypes { unbangedType $1 : $2 }
642 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
645 sbtype :: { RdrNameBangType }
646 : btype { unbangedType $1 }
647 | '!' atype { BangType MarkedUserStrict $2 }
649 fielddecls :: { [([RdrName],RdrNameBangType)] }
650 : fielddecl ',' fielddecls { $1 : $3 }
653 fielddecl :: { ([RdrName],RdrNameBangType) }
654 : sig_vars '::' stype { (reverse $1, $3) }
656 stype :: { RdrNameBangType }
657 : ctype { unbangedType $1 }
658 | '!' atype { BangType MarkedUserStrict $2 }
660 deriving :: { Maybe [RdrName] }
661 : {- empty -} { Nothing }
662 | 'deriving' qtycls { Just [$2] }
663 | 'deriving' '(' ')' { Just [] }
664 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
666 dclasses :: { [RdrName] }
667 : dclasses ',' qtycls { $3 : $1 }
670 -----------------------------------------------------------------------------
673 {- There's an awkward overlap with a type signature. Consider
674 f :: Int -> Int = ...rhs...
675 Then we can't tell whether it's a type signature or a value
676 definition with a result signature until we see the '='.
677 So we have to inline enough to postpone reductions until we know.
681 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
682 instead of qvar, we get another shift/reduce-conflict. Consider the
685 { (^^) :: Int->Int ; } Type signature; only var allowed
687 { (^^) :: Int->Int = ... ; } Value defn with result signature;
688 qvar allowed (because of instance decls)
690 We can't tell whether to reduce var to qvar until after we've read the signatures.
693 valdef :: { RdrBinding }
694 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
695 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
696 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
697 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
701 rhs :: { RdrNameGRHSs }
702 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
703 | gdrhs wherebinds { GRHSs (reverse $1) $2 placeHolderType }
705 gdrhs :: { [RdrNameGRHS] }
706 : gdrhs gdrh { $2 : $1 }
709 gdrh :: { RdrNameGRHS }
710 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
712 -----------------------------------------------------------------------------
715 exp :: { RdrNameHsExpr }
716 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
717 | infixexp 'with' dbinding { HsWith $1 $3 }
720 infixexp :: { RdrNameHsExpr }
722 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
723 (panic "fixity") $3 )}
725 exp10 :: { RdrNameHsExpr }
726 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
727 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
728 returnP (HsLam (Match [] ps $5
729 (GRHSs (unguardedRHS $8 $7)
730 EmptyBinds placeHolderType))) }
731 | 'let' declbinds 'in' exp { HsLet $2 $4 }
732 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
733 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
734 | '-' fexp { mkHsNegApp $2 }
735 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
736 returnP (HsDo DoExpr stmts $1) }
738 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False placeHolderType }
739 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 PlaySafe False placeHolderType }
740 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True placeHolderType }
741 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 PlaySafe True placeHolderType }
743 | scc_annot exp { if opt_SccProfilingOn
749 scc_annot :: { FAST_STRING }
750 : '_scc_' STRING { $2 }
751 | '{-# SCC' STRING '#-}' { $2 }
753 ccallid :: { FAST_STRING }
757 fexp :: { RdrNameHsExpr }
758 : fexp aexp { (HsApp $1 $2) }
761 aexps0 :: { [RdrNameHsExpr] }
762 : aexps { (reverse $1) }
764 aexps :: { [RdrNameHsExpr] }
765 : aexps aexp { $2 : $1 }
768 aexp :: { RdrNameHsExpr }
769 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
770 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
774 var_or_con :: { RdrNameHsExpr }
778 aexp1 :: { RdrNameHsExpr }
779 : ipvar { HsIPVar $1 }
781 | literal { HsLit $1 }
782 | INTEGER { HsOverLit (mkHsIntegral $1) }
783 | RATIONAL { HsOverLit (mkHsFractional $1) }
784 | '(' exp ')' { HsPar $2 }
785 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
786 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
787 | '[' list ']' { $2 }
788 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
789 | '(' qopm infixexp ')' { (SectionR $2 $3) }
790 | qvar '@' aexp { EAsPat $1 $3 }
792 | '~' aexp1 { ELazyPat $2 }
794 texps :: { [RdrNameHsExpr] }
795 : texps ',' exp { $3 : $1 }
799 -----------------------------------------------------------------------------
802 -- The rules below are little bit contorted to keep lexps left-recursive while
803 -- avoiding another shift/reduce-conflict.
805 list :: { RdrNameHsExpr }
806 : exp { ExplicitList placeHolderType [$1] }
807 | lexps { ExplicitList placeHolderType (reverse $1) }
808 | exp '..' { ArithSeqIn (From $1) }
809 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
810 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
811 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
812 | exp srcloc pquals {% let { body [qs] = qs;
813 body qss = [ParStmt (map reverse qss)] }
815 returnP ( HsDo ListComp
816 (reverse (ResultStmt $1 $2 : body $3))
821 lexps :: { [RdrNameHsExpr] }
822 : lexps ',' exp { $3 : $1 }
823 | exp ',' exp { [$3,$1] }
825 -----------------------------------------------------------------------------
826 -- List Comprehensions
828 pquals :: { [[RdrNameStmt]] }
829 : pquals '|' quals { $3 : $1 }
832 quals :: { [RdrNameStmt] }
833 : quals ',' stmt { $3 : $1 }
836 -----------------------------------------------------------------------------
839 altslist :: { [RdrNameMatch] }
840 : '{' alts '}' { reverse $2 }
841 | layout_on alts close { reverse $2 }
843 alts :: { [RdrNameMatch] }
847 alts1 :: { [RdrNameMatch] }
848 : alts1 ';' alt { $3 : $1 }
852 alt :: { RdrNameMatch }
853 : srcloc infixexp opt_sig ralt wherebinds
854 {% (checkPattern $1 $2 `thenP` \p ->
855 returnP (Match [] [p] $3
856 (GRHSs $4 $5 placeHolderType)) )}
858 ralt :: { [RdrNameGRHS] }
859 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
860 | gdpats { (reverse $1) }
862 gdpats :: { [RdrNameGRHS] }
863 : gdpats gdpat { $2 : $1 }
866 gdpat :: { RdrNameGRHS }
867 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
869 -----------------------------------------------------------------------------
870 -- Statement sequences
872 stmtlist :: { [RdrNameStmt] }
873 : '{' stmts '}' { $2 }
874 | layout_on_for_do stmts close { $2 }
876 -- do { ;; s ; s ; ; s ;; }
877 -- The last Stmt should be a ResultStmt, but that's hard to enforce
878 -- here, because we need too much lookahead if we see do { e ; }
879 -- So we use ExprStmts throughout, and switch the last one over
880 -- in ParseUtils.checkDo instead
881 stmts :: { [RdrNameStmt] }
882 : stmt stmts_help { $1 : $2 }
886 stmts_help :: { [RdrNameStmt] }
890 -- For typing stmts at the GHCi prompt, where
891 -- the input may consist of just comments.
892 maybe_stmt :: { Maybe RdrNameStmt }
894 | {- nothing -} { Nothing }
896 stmt :: { RdrNameStmt }
897 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
898 returnP (BindStmt p $4 $1) }
899 | srcloc exp { ExprStmt $2 placeHolderType $1 }
900 | srcloc 'let' declbinds { LetStmt $3 }
902 -----------------------------------------------------------------------------
903 -- Record Field Update/Construction
905 fbinds :: { RdrNameHsRecordBinds }
906 : fbinds ',' fbind { $3 : $1 }
911 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
912 : qvar '=' exp { ($1,$3,False) }
914 -----------------------------------------------------------------------------
915 -- Implicit Parameter Bindings
917 dbinding :: { [(RdrName, RdrNameHsExpr)] }
918 : '{' dbinds '}' { $2 }
919 | layout_on dbinds close { $2 }
921 dbinds :: { [(RdrName, RdrNameHsExpr)] }
922 : dbinds ';' dbind { $3 : $1 }
927 dbind :: { (RdrName, RdrNameHsExpr) }
928 dbind : ipvar '=' exp { ($1, $3) }
930 -----------------------------------------------------------------------------
931 -- Variables, Constructors and Operators.
933 identifier :: { RdrName }
938 depreclist :: { [RdrName] }
939 depreclist : deprec_var { [$1] }
940 | deprec_var ',' depreclist { $1 : $3 }
942 deprec_var :: { RdrName }
943 deprec_var : var { $1 }
946 gtycon :: { RdrName }
948 | '(' qtyconop ')' { $2 }
949 | '(' ')' { unitTyCon_RDR }
950 | '(' '->' ')' { funTyCon_RDR }
951 | '[' ']' { listTyCon_RDR }
952 | '(' commas ')' { tupleTyCon_RDR $2 }
955 : '(' ')' { unitCon_RDR }
956 | '[' ']' { nilCon_RDR }
957 | '(' commas ')' { tupleCon_RDR $2 }
962 | '(' varsym ')' { $2 }
966 | '(' varsym ')' { $2 }
967 | '(' qvarsym1 ')' { $2 }
968 -- We've inlined qvarsym here so that the decision about
969 -- whether it's a qvar or a var can be postponed until
970 -- *after* we see the close paren.
973 : IPVARID { (mkUnqual varName (tailFS $1)) }
977 | '(' qconsym ')' { $2 }
981 | '`' varid '`' { $2 }
983 qvarop :: { RdrName }
985 | '`' qvarid '`' { $2 }
987 qvaropm :: { RdrName }
988 : qvarsym_no_minus { $1 }
989 | '`' qvarid '`' { $2 }
993 | '`' conid '`' { $2 }
995 qconop :: { RdrName }
997 | '`' qconid '`' { $2 }
999 -----------------------------------------------------------------------------
1002 op :: { RdrName } -- used in infix decls
1006 qop :: { RdrName {-HsExpr-} } -- used in sections
1010 qopm :: { RdrNameHsExpr } -- used in sections
1011 : qvaropm { HsVar $1 }
1012 | qconop { HsVar $1 }
1014 -----------------------------------------------------------------------------
1017 qvarid :: { RdrName }
1019 | QVARID { mkQual varName $1 }
1021 varid :: { RdrName }
1022 : varid_no_unsafe { $1 }
1023 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1025 varid_no_unsafe :: { RdrName }
1026 : VARID { mkUnqual varName $1 }
1027 | special_id { mkUnqual varName $1 }
1028 | 'forall' { mkUnqual varName SLIT("forall") }
1030 tyvar :: { RdrName }
1031 : VARID { mkUnqual tvName $1 }
1032 | special_id { mkUnqual tvName $1 }
1033 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1035 -- These special_ids are treated as keywords in various places,
1036 -- but as ordinary ids elsewhere. A special_id collects all thsee
1037 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1038 special_id :: { UserFS }
1040 : 'as' { SLIT("as") }
1041 | 'qualified' { SLIT("qualified") }
1042 | 'hiding' { SLIT("hiding") }
1043 | 'export' { SLIT("export") }
1044 | 'label' { SLIT("label") }
1045 | 'dynamic' { SLIT("dynamic") }
1046 | 'stdcall' { SLIT("stdcall") }
1047 | 'ccall' { SLIT("ccall") }
1049 -----------------------------------------------------------------------------
1052 qconid :: { RdrName }
1054 | QCONID { mkQual dataName $1 }
1056 conid :: { RdrName }
1057 : CONID { mkUnqual dataName $1 }
1059 -----------------------------------------------------------------------------
1062 qconsym :: { RdrName }
1064 | QCONSYM { mkQual dataName $1 }
1066 consym :: { RdrName }
1067 : CONSYM { mkUnqual dataName $1 }
1069 -----------------------------------------------------------------------------
1072 qvarsym :: { RdrName }
1076 qvarsym_no_minus :: { RdrName }
1077 : varsym_no_minus { $1 }
1080 qvarsym1 :: { RdrName }
1081 qvarsym1 : QVARSYM { mkQual varName $1 }
1083 varsym :: { RdrName }
1084 : varsym_no_minus { $1 }
1085 | '-' { mkUnqual varName SLIT("-") }
1087 varsym_no_minus :: { RdrName } -- varsym not including '-'
1088 : VARSYM { mkUnqual varName $1 }
1089 | special_sym { mkUnqual varName $1 }
1092 -- See comments with special_id
1093 special_sym :: { UserFS }
1094 special_sym : '!' { SLIT("!") }
1097 -----------------------------------------------------------------------------
1100 literal :: { HsLit }
1101 : CHAR { HsChar $1 }
1102 | STRING { HsString $1 }
1103 | PRIMINTEGER { HsIntPrim $1 }
1104 | PRIMCHAR { HsCharPrim $1 }
1105 | PRIMSTRING { HsStringPrim $1 }
1106 | PRIMFLOAT { HsFloatPrim $1 }
1107 | PRIMDOUBLE { HsDoublePrim $1 }
1108 | CLITLIT { HsLitLit $1 placeHolderType }
1110 srcloc :: { SrcLoc } : {% getSrcLocP }
1112 -----------------------------------------------------------------------------
1116 : vccurly { () } -- context popped in lexer.
1117 | error {% popContext }
1119 layout_on :: { () } : {% layoutOn True{-strict-} }
1120 layout_on_for_do :: { () } : {% layoutOn False }
1122 -----------------------------------------------------------------------------
1123 -- Miscellaneous (mostly renamings)
1125 modid :: { ModuleName }
1126 : CONID { mkModuleNameFS $1 }
1127 | QCONID { mkModuleNameFS
1129 (unpackFS (fst $1) ++
1130 '.':unpackFS (snd $1)))
1133 tycon :: { RdrName }
1134 : CONID { mkUnqual tcClsName $1 }
1136 tyconop :: { RdrName }
1137 : CONSYM { mkUnqual tcClsName $1 }
1139 qtycon :: { RdrName }
1141 | QCONID { mkQual tcClsName $1 }
1143 qtyconop :: { RdrName }
1145 | QCONSYM { mkQual tcClsName $1 }
1147 qtycls :: { RdrName }
1151 : commas ',' { $1 + 1 }
1154 -----------------------------------------------------------------------------
1158 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)