2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.66 2001/05/24 13:59:11 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 $5) (length $5) $6 $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 : constrs '|' constr { $3 : $1 }
611 constr :: { RdrNameConDecl }
612 : srcloc forall context constr_stuff
613 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
614 | srcloc forall constr_stuff
615 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
617 forall :: { [RdrNameHsTyVar] }
618 : 'forall' tyvars '.' { $2 }
621 context :: { RdrNameContext }
622 : btype '=>' {% checkContext $1 }
624 constr_stuff :: { (RdrName, RdrNameConDetails) }
625 : btype {% mkVanillaCon $1 [] }
626 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
627 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
628 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
630 satypes :: { [RdrNameBangType] }
631 : atype satypes { unbangedType $1 : $2 }
632 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
635 sbtype :: { RdrNameBangType }
636 : btype { unbangedType $1 }
637 | '!' atype { BangType MarkedUserStrict $2 }
639 fielddecls :: { [([RdrName],RdrNameBangType)] }
640 : fielddecl ',' fielddecls { $1 : $3 }
643 fielddecl :: { ([RdrName],RdrNameBangType) }
644 : sig_vars '::' stype { (reverse $1, $3) }
646 stype :: { RdrNameBangType }
647 : ctype { unbangedType $1 }
648 | '!' atype { BangType MarkedUserStrict $2 }
650 deriving :: { Maybe [RdrName] }
651 : {- empty -} { Nothing }
652 | 'deriving' qtycls { Just [$2] }
653 | 'deriving' '(' ')' { Just [] }
654 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
656 dclasses :: { [RdrName] }
657 : dclasses ',' qtycls { $3 : $1 }
660 -----------------------------------------------------------------------------
663 {- There's an awkward overlap with a type signature. Consider
664 f :: Int -> Int = ...rhs...
665 Then we can't tell whether it's a type signature or a value
666 definition with a result signature until we see the '='.
667 So we have to inline enough to postpone reductions until we know.
671 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
672 instead of qvar, we get another shift/reduce-conflict. Consider the
675 { (^^) :: Int->Int ; } Type signature; only var allowed
677 { (^^) :: Int->Int = ... ; } Value defn with result signature;
678 qvar allowed (because of instance decls)
680 We can't tell whether to reduce var to qvar until after we've read the signatures.
683 valdef :: { RdrBinding }
684 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
685 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
686 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
687 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
691 rhs :: { RdrNameGRHSs }
692 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2)
694 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
696 gdrhs :: { [RdrNameGRHS] }
697 : gdrhs gdrh { $2 : $1 }
700 gdrh :: { RdrNameGRHS }
701 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
703 -----------------------------------------------------------------------------
706 exp :: { RdrNameHsExpr }
707 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
708 | infixexp 'with' dbinding { HsWith $1 $3 }
711 infixexp :: { RdrNameHsExpr }
713 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
714 (panic "fixity") $3 )}
716 exp10 :: { RdrNameHsExpr }
717 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
718 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
719 returnP (HsLam (Match [] ps $5
720 (GRHSs (unguardedRHS $8 $7)
721 EmptyBinds Nothing))) }
722 | 'let' declbinds 'in' exp { HsLet $2 $4 }
723 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
724 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
725 | '-' fexp { mkHsNegApp $2 }
726 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
727 returnP (HsDo DoExpr stmts $1) }
729 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False cbot }
730 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 PlaySafe False cbot }
731 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True cbot }
732 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 PlaySafe True cbot }
734 | scc_annot exp { if opt_SccProfilingOn
740 scc_annot :: { FAST_STRING }
741 : '_scc_' STRING { $2 }
742 | '{-# SCC' STRING '#-}' { $2 }
744 ccallid :: { FAST_STRING }
748 fexp :: { RdrNameHsExpr }
749 : fexp aexp { (HsApp $1 $2) }
752 aexps0 :: { [RdrNameHsExpr] }
753 : aexps { (reverse $1) }
755 aexps :: { [RdrNameHsExpr] }
756 : aexps aexp { $2 : $1 }
759 aexp :: { RdrNameHsExpr }
760 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
761 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
765 var_or_con :: { RdrNameHsExpr }
769 aexp1 :: { RdrNameHsExpr }
770 : ipvar { HsIPVar $1 }
772 | literal { HsLit $1 }
773 | INTEGER { HsOverLit (HsIntegral $1) }
774 | RATIONAL { HsOverLit (HsFractional $1) }
775 | '(' exp ')' { HsPar $2 }
776 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
777 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
778 | '[' list ']' { $2 }
779 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
780 | '(' qopm infixexp ')' { (SectionR $2 $3) }
781 | qvar '@' aexp { EAsPat $1 $3 }
783 | '~' aexp1 { ELazyPat $2 }
785 texps :: { [RdrNameHsExpr] }
786 : texps ',' exp { $3 : $1 }
790 -----------------------------------------------------------------------------
793 -- The rules below are little bit contorted to keep lexps left-recursive while
794 -- avoiding another shift/reduce-conflict.
796 list :: { RdrNameHsExpr }
797 : exp { ExplicitList [$1] }
798 | lexps { ExplicitList (reverse $1) }
799 | exp '..' { ArithSeqIn (From $1) }
800 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
801 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
802 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
803 | exp srcloc pquals {% let { body [qs] = qs;
804 body qss = [ParStmt (map reverse qss)] }
806 returnP ( HsDo ListComp
807 (reverse (ResultStmt $1 $2 : body $3))
812 lexps :: { [RdrNameHsExpr] }
813 : lexps ',' exp { $3 : $1 }
814 | exp ',' exp { [$3,$1] }
816 -----------------------------------------------------------------------------
817 -- List Comprehensions
819 pquals :: { [[RdrNameStmt]] }
820 : pquals '|' quals { $3 : $1 }
823 quals :: { [RdrNameStmt] }
824 : quals ',' stmt { $3 : $1 }
827 -----------------------------------------------------------------------------
830 altslist :: { [RdrNameMatch] }
831 : '{' alts '}' { reverse $2 }
832 | layout_on alts close { reverse $2 }
834 alts :: { [RdrNameMatch] }
838 alts1 :: { [RdrNameMatch] }
839 : alts1 ';' alt { $3 : $1 }
843 alt :: { RdrNameMatch }
844 : srcloc infixexp opt_sig ralt wherebinds
845 {% (checkPattern $1 $2 `thenP` \p ->
846 returnP (Match [] [p] $3
847 (GRHSs $4 $5 Nothing)) )}
849 ralt :: { [RdrNameGRHS] }
850 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
851 | gdpats { (reverse $1) }
853 gdpats :: { [RdrNameGRHS] }
854 : gdpats gdpat { $2 : $1 }
857 gdpat :: { RdrNameGRHS }
858 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
860 -----------------------------------------------------------------------------
861 -- Statement sequences
863 stmtlist :: { [RdrNameStmt] }
864 : '{' stmts '}' { $2 }
865 | layout_on_for_do stmts close { $2 }
867 -- do { ;; s ; s ; ; s ;; }
868 -- The last Stmt should be a ResultStmt, but that's hard to enforce
869 -- here, because we need too much lookahead if we see do { e ; }
870 -- So we use ExprStmts throughout, and switch the last one over
871 -- in ParseUtils.checkDo instead
872 stmts :: { [RdrNameStmt] }
873 : stmt stmts_help { $1 : $2 }
877 stmts_help :: { [RdrNameStmt] }
881 -- For typing stmts at the GHCi prompt, where
882 -- the input may consist of just comments.
883 maybe_stmt :: { Maybe RdrNameStmt }
885 | {- nothing -} { Nothing }
887 stmt :: { RdrNameStmt }
888 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
889 returnP (BindStmt p $4 $1) }
890 | srcloc exp { ExprStmt $2 $1 }
891 | srcloc 'let' declbinds { LetStmt $3 }
893 -----------------------------------------------------------------------------
894 -- Record Field Update/Construction
896 fbinds :: { RdrNameHsRecordBinds }
897 : fbinds ',' fbind { $3 : $1 }
902 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
903 : qvar '=' exp { ($1,$3,False) }
905 -----------------------------------------------------------------------------
906 -- Implicit Parameter Bindings
908 dbinding :: { [(RdrName, RdrNameHsExpr)] }
909 : '{' dbinds '}' { $2 }
910 | layout_on dbinds close { $2 }
912 dbinds :: { [(RdrName, RdrNameHsExpr)] }
913 : dbinds ';' dbind { $3 : $1 }
918 dbind :: { (RdrName, RdrNameHsExpr) }
919 dbind : ipvar '=' exp { ($1, $3) }
921 -----------------------------------------------------------------------------
922 -- Variables, Constructors and Operators.
924 depreclist :: { [RdrName] }
925 depreclist : deprec_var { [$1] }
926 | deprec_var ',' depreclist { $1 : $3 }
928 deprec_var :: { RdrName }
929 deprec_var : var { $1 }
932 gtycon :: { RdrName }
934 | '(' qtyconop ')' { $2 }
935 | '(' ')' { unitTyCon_RDR }
936 | '(' '->' ')' { funTyCon_RDR }
937 | '[' ']' { listTyCon_RDR }
938 | '(' commas ')' { tupleTyCon_RDR $2 }
941 : '(' ')' { unitCon_RDR }
942 | '[' ']' { nilCon_RDR }
943 | '(' commas ')' { tupleCon_RDR $2 }
948 | '(' varsym ')' { $2 }
952 | '(' varsym ')' { $2 }
953 | '(' qvarsym1 ')' { $2 }
954 -- We've inlined qvarsym here so that the decision about
955 -- whether it's a qvar or a var can be postponed until
956 -- *after* we see the close paren.
959 : IPVARID { (mkUnqual varName (tailFS $1)) }
963 | '(' qconsym ')' { $2 }
967 | '`' varid '`' { $2 }
969 qvarop :: { RdrName }
971 | '`' qvarid '`' { $2 }
973 qvaropm :: { RdrName }
974 : qvarsym_no_minus { $1 }
975 | '`' qvarid '`' { $2 }
979 | '`' conid '`' { $2 }
981 qconop :: { RdrName }
983 | '`' qconid '`' { $2 }
985 -----------------------------------------------------------------------------
988 op :: { RdrName } -- used in infix decls
992 qop :: { RdrName {-HsExpr-} } -- used in sections
996 qopm :: { RdrNameHsExpr } -- used in sections
997 : qvaropm { HsVar $1 }
998 | qconop { HsVar $1 }
1000 -----------------------------------------------------------------------------
1003 qvarid :: { RdrName }
1005 | QVARID { mkQual varName $1 }
1007 varid :: { RdrName }
1008 : varid_no_unsafe { $1 }
1009 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1011 varid_no_unsafe :: { RdrName }
1012 : VARID { mkUnqual varName $1 }
1013 | special_id { mkUnqual varName $1 }
1014 | 'forall' { mkUnqual varName SLIT("forall") }
1016 tyvar :: { RdrName }
1017 : VARID { mkUnqual tvName $1 }
1018 | special_id { mkUnqual tvName $1 }
1019 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1021 -- These special_ids are treated as keywords in various places,
1022 -- but as ordinary ids elsewhere. A special_id collects all thsee
1023 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1024 special_id :: { UserFS }
1026 : 'as' { SLIT("as") }
1027 | 'qualified' { SLIT("qualified") }
1028 | 'hiding' { SLIT("hiding") }
1029 | 'export' { SLIT("export") }
1030 | 'label' { SLIT("label") }
1031 | 'dynamic' { SLIT("dynamic") }
1032 | 'stdcall' { SLIT("stdcall") }
1033 | 'ccall' { SLIT("ccall") }
1035 -----------------------------------------------------------------------------
1038 qconid :: { RdrName }
1040 | QCONID { mkQual dataName $1 }
1042 conid :: { RdrName }
1043 : CONID { mkUnqual dataName $1 }
1045 -----------------------------------------------------------------------------
1048 qconsym :: { RdrName }
1050 | QCONSYM { mkQual dataName $1 }
1052 consym :: { RdrName }
1053 : CONSYM { mkUnqual dataName $1 }
1055 -----------------------------------------------------------------------------
1058 qvarsym :: { RdrName }
1062 qvarsym_no_minus :: { RdrName }
1063 : varsym_no_minus { $1 }
1066 qvarsym1 :: { RdrName }
1067 qvarsym1 : QVARSYM { mkQual varName $1 }
1069 varsym :: { RdrName }
1070 : varsym_no_minus { $1 }
1071 | '-' { mkUnqual varName SLIT("-") }
1073 varsym_no_minus :: { RdrName } -- varsym not including '-'
1074 : VARSYM { mkUnqual varName $1 }
1075 | special_sym { mkUnqual varName $1 }
1078 -- See comments with special_id
1079 special_sym :: { UserFS }
1080 special_sym : '!' { SLIT("!") }
1083 -----------------------------------------------------------------------------
1086 literal :: { HsLit }
1087 : CHAR { HsChar $1 }
1088 | STRING { HsString $1 }
1089 | PRIMINTEGER { HsIntPrim $1 }
1090 | PRIMCHAR { HsCharPrim $1 }
1091 | PRIMSTRING { HsStringPrim $1 }
1092 | PRIMFLOAT { HsFloatPrim $1 }
1093 | PRIMDOUBLE { HsDoublePrim $1 }
1094 | CLITLIT { HsLitLit $1 (error "Parser.y: CLITLIT") }
1096 srcloc :: { SrcLoc } : {% getSrcLocP }
1098 -----------------------------------------------------------------------------
1102 : vccurly { () } -- context popped in lexer.
1103 | error {% popContext }
1105 layout_on :: { () } : {% layoutOn True{-strict-} }
1106 layout_on_for_do :: { () } : {% layoutOn False }
1108 -----------------------------------------------------------------------------
1109 -- Miscellaneous (mostly renamings)
1111 modid :: { ModuleName }
1112 : CONID { mkModuleNameFS $1 }
1114 tycon :: { RdrName }
1115 : CONID { mkUnqual tcClsName $1 }
1117 tyconop :: { RdrName }
1118 : CONSYM { mkUnqual tcClsName $1 }
1120 qtycon :: { RdrName }
1122 | QCONID { mkQual tcClsName $1 }
1124 qtyconop :: { RdrName }
1126 | QCONSYM { mkQual tcClsName $1 }
1128 qtycls :: { RdrName }
1132 : commas ',' { $1 + 1 }
1135 -----------------------------------------------------------------------------
1139 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)