2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.58 2001/05/01 09:16:55 qrczak 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 OccName ( UserFS, varName, tcName, dataName, tcClsName, tvName )
25 import SrcLoc ( SrcLoc )
28 import CmdLineOpts ( opt_SccProfilingOn )
29 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), NewOrData(..) )
33 import FastString ( tailFS )
36 #include "HsVersions.h"
40 -----------------------------------------------------------------------------
41 Conflicts: 14 shift/reduce
42 (note: it's currently 21 -- JRL, 31/1/2000)
44 8 for abiguity in 'if x then y else z + 1'
45 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
46 1 for ambiguity in 'if x then y else z :: T'
47 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
48 3 for ambiguity in 'case x of y :: a -> b'
49 (don't know whether to reduce 'a' as a btype or shift the '->'.
50 conclusion: bogus expression anyway, doesn't matter)
52 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
53 since 'forall' is a valid variable name, we don't know whether
54 to treat a forall on the input as the beginning of a quantifier
55 or the beginning of the rule itself. Resolving to shift means
56 it's always treated as a quantifier, hence the above is disallowed.
57 This saves explicitly defining a grammar for the rule lhs that
58 doesn't include 'forall'.
60 1 for ambiguity in 'x @ Rec{..}'.
61 Only sensible parse is 'x @ (Rec{..})', which is what resolving
64 -----------------------------------------------------------------------------
68 '_' { ITunderscore } -- Haskell keywords
73 'default' { ITdefault }
74 'deriving' { ITderiving }
84 'instance' { ITinstance }
87 'newtype' { ITnewtype }
89 'qualified' { ITqualified }
93 '_scc_' { ITscc } -- ToDo: remove
95 'forall' { ITforall } -- GHC extension keywords
96 'foreign' { ITforeign }
99 'dynamic' { ITdynamic }
100 'unsafe' { ITunsafe }
102 'stdcall' { ITstdcallconv }
103 'ccall' { ITccallconv }
104 '_ccall_' { ITccall (False, False, False) }
105 '_ccall_GC_' { ITccall (False, False, True) }
106 '_casm_' { ITccall (False, True, False) }
107 '_casm_GC_' { ITccall (False, True, True) }
109 '{-# SPECIALISE' { ITspecialise_prag }
110 '{-# SOURCE' { ITsource_prag }
111 '{-# INLINE' { ITinline_prag }
112 '{-# NOINLINE' { ITnoinline_prag }
113 '{-# RULES' { ITrules_prag }
114 '{-# SCC' { ITscc_prag }
115 '{-# DEPRECATED' { ITdeprecated_prag }
116 '#-}' { ITclose_prag }
119 '__interface' { ITinterface } -- interface keywords
120 '__export' { IT__export }
121 '__instimport' { ITinstimport }
122 '__forall' { IT__forall }
123 '__letrec' { ITletrec }
124 '__coerce' { ITcoerce }
125 '__depends' { ITdepends }
126 '__inline' { ITinline }
127 '__DEFAULT' { ITdefaultbranch }
129 '__integer' { ITinteger_lit }
130 '__float' { ITfloat_lit }
131 '__rational' { ITrational_lit }
132 '__addr' { ITaddr_lit }
133 '__label' { ITlabel_lit }
134 '__litlit' { ITlit_lit }
135 '__string' { ITstring_lit }
136 '__ccall' { ITccall $$ }
138 '__sccC' { ITsccAllCafs }
141 '__P' { ITspecialise }
143 '__U' { ITunfold $$ }
144 '__S' { ITstrict $$ }
145 '__M' { ITcprinfo $$ }
148 '..' { ITdotdot } -- reserved symbols
162 '{' { ITocurly } -- special symbols
166 vccurly { ITvccurly } -- virtual close curly (from layout)
177 VARID { ITvarid $$ } -- identifiers
179 VARSYM { ITvarsym $$ }
180 CONSYM { ITconsym $$ }
181 QVARID { ITqvarid $$ }
182 QCONID { ITqconid $$ }
183 QVARSYM { ITqvarsym $$ }
184 QCONSYM { ITqconsym $$ }
186 IPVARID { ITipvarid $$ } -- GHC extension
189 STRING { ITstring $$ }
190 INTEGER { ITinteger $$ }
191 RATIONAL { ITrational $$ }
193 PRIMCHAR { ITprimchar $$ }
194 PRIMSTRING { ITprimstring $$ }
195 PRIMINTEGER { ITprimint $$ }
196 PRIMFLOAT { ITprimfloat $$ }
197 PRIMDOUBLE { ITprimdouble $$ }
198 CLITLIT { ITlitlit $$ }
200 %monad { P } { thenP } { returnP }
201 %lexer { lexer } { ITeof }
202 %name parseModule module
203 %name parseStmt maybe_stmt
207 -----------------------------------------------------------------------------
210 -- The place for module deprecation is really too restrictive, but if it
211 -- was allowed at its natural place just before 'module', we get an ugly
212 -- s/r conflict with the second alternative. Another solution would be the
213 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
214 -- either, and DEPRECATED is only expected to be used by people who really
215 -- know what they are doing. :-)
217 module :: { RdrNameHsModule }
218 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
219 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
221 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
223 maybemoddeprec :: { Maybe DeprecTxt }
224 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
225 | {- empty -} { Nothing }
227 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
229 | layout_on top close { $2 }
231 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
232 : importdecls { (reverse $1,[]) }
233 | importdecls ';' cvtopdecls { (reverse $1,$3) }
234 | cvtopdecls { ([],$1) }
236 cvtopdecls :: { [RdrNameHsDecl] }
237 : topdecls { cvTopDecls (groupBindings $1)}
239 -----------------------------------------------------------------------------
242 maybeexports :: { Maybe [RdrNameIE] }
243 : '(' exportlist ')' { Just $2 }
244 | {- empty -} { Nothing }
246 exportlist :: { [RdrNameIE] }
247 : exportlist ',' export { $3 : $1 }
248 | exportlist ',' { $1 }
252 -- GHC extension: we allow things like [] and (,,,) to be exported
253 export :: { RdrNameIE }
255 | gtycon { IEThingAbs $1 }
256 | gtycon '(' '..' ')' { IEThingAll $1 }
257 | gtycon '(' ')' { IEThingWith $1 [] }
258 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
259 | 'module' modid { IEModuleContents $2 }
261 qcnames :: { [RdrName] }
262 : qcnames ',' qcname { $3 : $1 }
265 qcname :: { RdrName }
269 -----------------------------------------------------------------------------
270 -- Import Declarations
272 -- import decls can be *empty*, or even just a string of semicolons
273 -- whereas topdecls must contain at least one topdecl.
275 importdecls :: { [RdrNameImportDecl] }
276 : importdecls ';' importdecl { $3 : $1 }
277 | importdecls ';' { $1 }
278 | importdecl { [ $1 ] }
281 importdecl :: { RdrNameImportDecl }
282 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
283 { ImportDecl (mkModuleNameFS $5) $3 $4 $6 $7 $2 }
285 maybe_src :: { WhereFrom }
286 : '{-# SOURCE' '#-}' { ImportByUserSource }
287 | {- empty -} { ImportByUser }
289 optqualified :: { Bool }
290 : 'qualified' { True }
291 | {- empty -} { False }
293 maybeas :: { Maybe ModuleName }
294 : 'as' modid { Just $2 }
295 | {- empty -} { Nothing }
297 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
298 : impspec { Just $1 }
299 | {- empty -} { Nothing }
301 impspec :: { (Bool, [RdrNameIE]) }
302 : '(' exportlist ')' { (False, reverse $2) }
303 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
305 -----------------------------------------------------------------------------
306 -- Fixity Declarations
310 | INTEGER {% checkPrec $1 `thenP_`
311 returnP (fromInteger $1) }
313 infix :: { FixityDirection }
315 | 'infixl' { InfixL }
316 | 'infixr' { InfixR }
319 : ops ',' op { $3 : $1 }
322 -----------------------------------------------------------------------------
323 -- Top-Level Declarations
325 topdecls :: { [RdrBinding] }
326 : topdecls ';' topdecl { ($3 : $1) }
327 | topdecls ';' { $1 }
330 topdecl :: { RdrBinding }
331 : srcloc 'type' simpletype '=' ctype
332 -- Note ctype, not sigtype.
333 -- We allow an explicit for-all but we don't insert one
334 -- in type Foo a = (b,b)
335 -- Instead we just say b is out of scope
336 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
338 | srcloc 'data' ctype '=' constrs deriving
339 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
340 returnP (RdrHsDecl (TyClD
341 (mkTyData DataType cs c ts (reverse $5) (length $5) $6 $1))) }
343 | srcloc 'newtype' ctype '=' newconstr deriving
344 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
345 returnP (RdrHsDecl (TyClD
346 (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
348 | srcloc 'class' ctype fds where
349 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
351 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
353 returnP (RdrHsDecl (TyClD
354 (mkClassDecl cs c ts $4 sigs (Just binds) $1))) }
356 | srcloc 'instance' inst_type where
358 = cvMonoBindsAndSigs cvInstDeclSig
360 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
362 | srcloc 'default' '(' types0 ')'
363 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
365 | srcloc 'foreign' 'import' callconv ext_name
366 unsafe_flag varid_no_unsafe '::' sigtype
367 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
369 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
370 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
372 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
373 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
374 defaultCallConv $1)) }
376 | '{-# DEPRECATED' deprecations '#-}' { $2 }
377 | '{-# RULES' rules '#-}' { $2 }
380 decls :: { [RdrBinding] }
381 : decls ';' decl { $3 : $1 }
386 decl :: { RdrBinding }
389 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
390 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
391 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
392 { foldr1 RdrAndBindings
393 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
394 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
395 { RdrSig (SpecInstSig $4 $2) }
397 opt_phase :: { Maybe Int }
398 : INTEGER { Just (fromInteger $1) }
399 | {- empty -} { Nothing }
401 wherebinds :: { RdrNameHsBinds }
402 : where { cvBinds cvValSig (groupBindings $1) }
404 where :: { [RdrBinding] }
405 : 'where' decllist { $2 }
408 declbinds :: { RdrNameHsBinds }
409 : decllist { cvBinds cvValSig (groupBindings $1) }
411 decllist :: { [RdrBinding] }
412 : '{' decls '}' { $2 }
413 | layout_on decls close { $2 }
415 fixdecl :: { RdrBinding }
416 : srcloc infix prec ops { foldr1 RdrAndBindings
417 [ RdrSig (FixSig (FixitySig n
421 -----------------------------------------------------------------------------
422 -- Transformation Rules
424 rules :: { RdrBinding }
425 : rules ';' rule { $1 `RdrAndBindings` $3 }
428 | {- empty -} { RdrNullBind }
430 rule :: { RdrBinding }
431 : STRING rule_forall fexp '=' srcloc exp
432 { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
434 rule_forall :: { [RdrNameRuleBndr] }
435 : 'forall' rule_var_list '.' { $2 }
438 rule_var_list :: { [RdrNameRuleBndr] }
440 | rule_var rule_var_list { $1 : $2 }
442 rule_var :: { RdrNameRuleBndr }
443 : varid { RuleBndr $1 }
444 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
446 -----------------------------------------------------------------------------
449 deprecations :: { RdrBinding }
450 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
451 | deprecations ';' { $1 }
453 | {- empty -} { RdrNullBind }
455 -- SUP: TEMPORARY HACK, not checking for `module Foo'
456 deprecation :: { RdrBinding }
457 : srcloc depreclist STRING
458 { foldr RdrAndBindings RdrNullBind
459 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
461 -----------------------------------------------------------------------------
462 -- Foreign import/export
465 : 'stdcall' { stdCallConv }
466 | 'ccall' { cCallConv }
467 | {- empty -} { defaultCallConv }
469 unsafe_flag :: { Bool }
471 | {- empty -} { False }
473 ext_name :: { Maybe ExtName }
474 : 'dynamic' { Just Dynamic }
475 | STRING { Just (ExtName $1 Nothing) }
476 | STRING STRING { Just (ExtName $2 (Just $1)) }
477 | {- empty -} { Nothing }
480 -----------------------------------------------------------------------------
483 opt_sig :: { Maybe RdrNameHsType }
484 : {- empty -} { Nothing }
485 | '::' sigtype { Just $2 }
487 opt_asig :: { Maybe RdrNameHsType }
488 : {- empty -} { Nothing }
489 | '::' atype { Just $2 }
491 sigtypes :: { [RdrNameHsType] }
493 | sigtypes ',' sigtype { $3 : $1 }
495 sigtype :: { RdrNameHsType }
496 : ctype { (mkHsForAllTy Nothing [] $1) }
498 sig_vars :: { [RdrName] }
499 : sig_vars ',' var { $3 : $1 }
502 -----------------------------------------------------------------------------
505 -- A ctype is a for-all type
506 ctype :: { RdrNameHsType }
507 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
508 | context type { mkHsForAllTy Nothing $1 $2 }
509 -- A type of form (context => type) is an *implicit* HsForAllTy
512 type :: { RdrNameHsType }
513 : gentype '->' type { HsFunTy $1 $3 }
514 | ipvar '::' type { mkHsIParamTy $1 $3 }
517 gentype :: { RdrNameHsType }
520 | atype tyconop atype { HsOpTy $1 $2 $3 }
522 btype :: { RdrNameHsType }
523 : btype atype { (HsAppTy $1 $2) }
526 atype :: { RdrNameHsType }
527 : gtycon { HsTyVar $1 }
528 | tyvar { HsTyVar $1 }
529 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
530 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
531 | '[' type ']' { HsListTy $2 }
532 | '(' ctype ')' { $2 }
534 | INTEGER { HsNumTy $1 }
536 -- An inst_type is what occurs in the head of an instance decl
537 -- e.g. (Foo a, Gaz b) => Wibble a b
538 -- It's kept as a single type, with a MonoDictTy at the right
539 -- hand corner, for convenience.
540 inst_type :: { RdrNameHsType }
541 : ctype {% checkInstType $1 }
543 types0 :: { [RdrNameHsType] }
544 : types { reverse $1 }
547 types :: { [RdrNameHsType] }
549 | types ',' type { $3 : $1 }
551 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
552 : tycon tyvars { ($1, reverse $2) }
554 tyvars :: { [RdrNameHsTyVar] }
555 : tyvars tyvar { UserTyVar $2 : $1 }
558 fds :: { [([RdrName], [RdrName])] }
560 | '|' fds1 { reverse $2 }
562 fds1 :: { [([RdrName], [RdrName])] }
563 : fds1 ',' fd { $3 : $1 }
566 fd :: { ([RdrName], [RdrName]) }
567 : varids0 '->' varids0 { (reverse $1, reverse $3) }
569 varids0 :: { [RdrName] }
571 | varids0 tyvar { $2 : $1 }
573 -----------------------------------------------------------------------------
574 -- Datatype declarations
576 newconstr :: { RdrNameConDecl }
577 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [Unbanged $3]) $1 }
578 | srcloc conid '{' var '::' type '}'
579 { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $1 }
581 constrs :: { [RdrNameConDecl] }
582 : constrs '|' constr { $3 : $1 }
585 constr :: { RdrNameConDecl }
586 : srcloc forall context constr_stuff
587 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
588 | srcloc forall constr_stuff
589 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
591 forall :: { [RdrNameHsTyVar] }
592 : 'forall' tyvars '.' { $2 }
595 context :: { RdrNameContext }
596 : btype '=>' {% checkContext $1 }
598 constr_stuff :: { (RdrName, RdrNameConDetails) }
599 : btype {% mkVanillaCon $1 [] }
600 | btype '!' atype satypes {% mkVanillaCon $1 (Banged $3 : $4) }
601 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
602 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
604 satypes :: { [RdrNameBangType] }
605 : atype satypes { Unbanged $1 : $2 }
606 | '!' atype satypes { Banged $2 : $3 }
609 sbtype :: { RdrNameBangType }
610 : btype { Unbanged $1 }
611 | '!' atype { Banged $2 }
613 fielddecls :: { [([RdrName],RdrNameBangType)] }
614 : fielddecl ',' fielddecls { $1 : $3 }
617 fielddecl :: { ([RdrName],RdrNameBangType) }
618 : sig_vars '::' stype { (reverse $1, $3) }
620 stype :: { RdrNameBangType }
621 : ctype { Unbanged $1 }
622 | '!' atype { Banged $2 }
624 deriving :: { Maybe [RdrName] }
625 : {- empty -} { Nothing }
626 | 'deriving' qtycls { Just [$2] }
627 | 'deriving' '(' ')' { Just [] }
628 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
630 dclasses :: { [RdrName] }
631 : dclasses ',' qtycls { $3 : $1 }
634 -----------------------------------------------------------------------------
637 {- There's an awkward overlap with a type signature. Consider
638 f :: Int -> Int = ...rhs...
639 Then we can't tell whether it's a type signature or a value
640 definition with a result signature until we see the '='.
641 So we have to inline enough to postpone reductions until we know.
645 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
646 instead of qvar, we get another shift/reduce-conflict. Consider the
649 { (^^) :: Int->Int ; } Type signature; only var allowed
651 { (^^) :: Int->Int = ... ; } Value defn with result signature;
652 qvar allowed (because of instance decls)
654 We can't tell whether to reduce var to qvar until after we've read the signatures.
657 valdef :: { RdrBinding }
658 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
659 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
660 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
661 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
665 rhs :: { RdrNameGRHSs }
666 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2)
668 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
670 gdrhs :: { [RdrNameGRHS] }
671 : gdrhs gdrh { $2 : $1 }
674 gdrh :: { RdrNameGRHS }
675 : '|' srcloc quals '=' exp { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
677 -----------------------------------------------------------------------------
680 exp :: { RdrNameHsExpr }
681 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
682 | infixexp 'with' dbinding { HsWith $1 $3 }
685 infixexp :: { RdrNameHsExpr }
687 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
688 (panic "fixity") $3 )}
690 exp10 :: { RdrNameHsExpr }
691 : '\\' aexp aexps opt_asig '->' srcloc exp
692 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
693 returnP (HsLam (Match [] ps $4
694 (GRHSs (unguardedRHS $7 $6)
695 EmptyBinds Nothing))) }
696 | 'let' declbinds 'in' exp { HsLet $2 $4 }
697 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
698 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
699 | '-' fexp { mkHsNegApp $2 }
700 | srcloc 'do' stmtlist { HsDo DoExpr $3 $1 }
702 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 False False cbot }
703 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 True False cbot }
704 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 False True cbot }
705 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 True True cbot }
707 | scc_annot exp { if opt_SccProfilingOn
713 scc_annot :: { FAST_STRING }
714 : '_scc_' STRING { $2 }
715 | '{-# SCC' STRING '#-}' { $2 }
717 ccallid :: { FAST_STRING }
721 fexp :: { RdrNameHsExpr }
722 : fexp aexp { (HsApp $1 $2) }
725 aexps0 :: { [RdrNameHsExpr] }
726 : aexps { (reverse $1) }
728 aexps :: { [RdrNameHsExpr] }
729 : aexps aexp { $2 : $1 }
732 aexp :: { RdrNameHsExpr }
733 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
734 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
738 var_or_con :: { RdrNameHsExpr }
742 aexp1 :: { RdrNameHsExpr }
743 : ipvar { HsIPVar $1 }
745 | literal { HsLit $1 }
746 | INTEGER { HsOverLit (HsIntegral $1) }
747 | RATIONAL { HsOverLit (HsFractional $1) }
748 | '(' exp ')' { HsPar $2 }
749 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
750 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
751 | '[' list ']' { $2 }
752 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
753 | '(' qopm infixexp ')' { (SectionR $2 $3) }
754 | qvar '@' aexp { EAsPat $1 $3 }
756 | '~' aexp1 { ELazyPat $2 }
758 texps :: { [RdrNameHsExpr] }
759 : texps ',' exp { $3 : $1 }
763 -----------------------------------------------------------------------------
766 -- The rules below are little bit contorted to keep lexps left-recursive while
767 -- avoiding another shift/reduce-conflict.
769 list :: { RdrNameHsExpr }
770 : exp { ExplicitList [$1] }
771 | lexps { ExplicitList (reverse $1) }
772 | exp '..' { ArithSeqIn (From $1) }
773 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
774 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
775 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
776 | exp srcloc pquals {% let { body [qs] = qs;
777 body qss = [ParStmt (map reverse qss)] }
779 returnP ( HsDo ListComp
780 (reverse (ExprStmt $1 $2 : body $3))
785 lexps :: { [RdrNameHsExpr] }
786 : lexps ',' exp { $3 : $1 }
787 | exp ',' exp { [$3,$1] }
789 -----------------------------------------------------------------------------
790 -- List Comprehensions
792 pquals :: { [[RdrNameStmt]] }
793 : pquals '|' quals { $3 : $1 }
796 quals :: { [RdrNameStmt] }
797 : quals ',' stmt { $3 : $1 }
800 -----------------------------------------------------------------------------
803 altslist :: { [RdrNameMatch] }
804 : '{' alts '}' { reverse $2 }
805 | layout_on alts close { reverse $2 }
807 alts :: { [RdrNameMatch] }
811 alts1 :: { [RdrNameMatch] }
812 : alts1 ';' alt { $3 : $1 }
816 alt :: { RdrNameMatch }
817 : infixexp opt_sig ralt wherebinds
818 {% (checkPattern $1 `thenP` \p ->
819 returnP (Match [] [p] $2
820 (GRHSs $3 $4 Nothing)) )}
822 ralt :: { [RdrNameGRHS] }
823 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
824 | gdpats { (reverse $1) }
826 gdpats :: { [RdrNameGRHS] }
827 : gdpats gdpat { $2 : $1 }
830 gdpat :: { RdrNameGRHS }
831 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
833 -----------------------------------------------------------------------------
834 -- Statement sequences
836 stmtlist :: { [RdrNameStmt] }
837 : '{' stmts '}' { reverse $2 }
838 | layout_on_for_do stmts close { reverse $2 }
840 -- Stmt list should really end in an expression, but it's not
841 -- convenient to enforce this here, so we throw out erroneous
842 -- statement sequences in the renamer instead.
844 stmts :: { [RdrNameStmt] }
848 stmts1 :: { [RdrNameStmt] }
849 : stmts1 ';' stmt { $3 : $1 }
853 -- for typing stmts at the GHCi prompt, where the input may consist of
855 maybe_stmt :: { Maybe RdrNameStmt }
857 | {- nothing -} { Nothing }
859 stmt :: { RdrNameStmt }
860 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
861 returnP (BindStmt p $4 $1) }
862 | srcloc exp { ExprStmt $2 $1 }
863 | srcloc 'let' declbinds { LetStmt $3 }
865 -----------------------------------------------------------------------------
866 -- Record Field Update/Construction
868 fbinds :: { RdrNameHsRecordBinds }
869 : fbinds ',' fbind { $3 : $1 }
874 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
875 : qvar '=' exp { ($1,$3,False) }
877 -----------------------------------------------------------------------------
878 -- Implicit Parameter Bindings
880 dbinding :: { [(RdrName, RdrNameHsExpr)] }
881 : '{' dbinds '}' { $2 }
882 | layout_on dbinds close { $2 }
884 dbinds :: { [(RdrName, RdrNameHsExpr)] }
885 : dbinds ';' dbind { $3 : $1 }
890 dbind :: { (RdrName, RdrNameHsExpr) }
891 dbind : ipvar '=' exp { ($1, $3) }
893 -----------------------------------------------------------------------------
894 -- Variables, Constructors and Operators.
896 depreclist :: { [RdrName] }
897 depreclist : deprec_var { [$1] }
898 | deprec_var ',' depreclist { $1 : $3 }
900 deprec_var :: { RdrName }
901 deprec_var : var { $1 }
904 gtycon :: { RdrName }
906 | '(' qtyconop ')' { $2 }
907 | '(' ')' { unitTyCon_RDR }
908 | '(' '->' ')' { funTyCon_RDR }
909 | '[' ']' { listTyCon_RDR }
910 | '(' commas ')' { tupleTyCon_RDR $2 }
913 : '(' ')' { unitCon_RDR }
914 | '[' ']' { nilCon_RDR }
915 | '(' commas ')' { tupleCon_RDR $2 }
920 | '(' varsym ')' { $2 }
924 | '(' varsym ')' { $2 }
925 | '(' qvarsym1 ')' { $2 }
926 -- We've inlined qvarsym here so that the decision about
927 -- whether it's a qvar or a var can be postponed until
928 -- *after* we see the close paren.
931 : IPVARID { (mkUnqual varName (tailFS $1)) }
935 | '(' qconsym ')' { $2 }
939 | '`' varid '`' { $2 }
941 qvarop :: { RdrName }
943 | '`' qvarid '`' { $2 }
945 qvaropm :: { RdrName }
946 : qvarsym_no_minus { $1 }
947 | '`' qvarid '`' { $2 }
951 | '`' conid '`' { $2 }
953 qconop :: { RdrName }
955 | '`' qconid '`' { $2 }
957 -----------------------------------------------------------------------------
960 op :: { RdrName } -- used in infix decls
964 qop :: { RdrName {-HsExpr-} } -- used in sections
968 qopm :: { RdrNameHsExpr } -- used in sections
969 : qvaropm { HsVar $1 }
970 | qconop { HsVar $1 }
972 -----------------------------------------------------------------------------
975 qvarid :: { RdrName }
977 | QVARID { mkQual varName $1 }
980 : varid_no_unsafe { $1 }
981 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
983 varid_no_unsafe :: { RdrName }
984 : VARID { mkUnqual varName $1 }
985 | special_id { mkUnqual varName $1 }
986 | 'forall' { mkUnqual varName SLIT("forall") }
989 : VARID { mkUnqual tvName $1 }
990 | special_id { mkUnqual tvName $1 }
991 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
993 -- These special_ids are treated as keywords in various places,
994 -- but as ordinary ids elsewhere. A special_id collects all thsee
995 -- except 'unsafe' and 'forall' whose treatment differs depending on context
996 special_id :: { UserFS }
998 : 'as' { SLIT("as") }
999 | 'qualified' { SLIT("qualified") }
1000 | 'hiding' { SLIT("hiding") }
1001 | 'export' { SLIT("export") }
1002 | 'label' { SLIT("label") }
1003 | 'dynamic' { SLIT("dynamic") }
1004 | 'stdcall' { SLIT("stdcall") }
1005 | 'ccall' { SLIT("ccall") }
1007 -----------------------------------------------------------------------------
1010 qconid :: { RdrName }
1012 | QCONID { mkQual dataName $1 }
1014 conid :: { RdrName }
1015 : CONID { mkUnqual dataName $1 }
1017 -----------------------------------------------------------------------------
1020 qconsym :: { RdrName }
1022 | QCONSYM { mkQual dataName $1 }
1024 consym :: { RdrName }
1025 : CONSYM { mkUnqual dataName $1 }
1027 -----------------------------------------------------------------------------
1030 qvarsym :: { RdrName }
1034 qvarsym_no_minus :: { RdrName }
1035 : varsym_no_minus { $1 }
1038 qvarsym1 :: { RdrName }
1039 qvarsym1 : QVARSYM { mkQual varName $1 }
1041 varsym :: { RdrName }
1042 : varsym_no_minus { $1 }
1043 | '-' { mkUnqual varName SLIT("-") }
1045 varsym_no_minus :: { RdrName } -- varsym not including '-'
1046 : VARSYM { mkUnqual varName $1 }
1047 | special_sym { mkUnqual varName $1 }
1050 -- See comments with special_id
1051 special_sym :: { UserFS }
1052 special_sym : '!' { SLIT("!") }
1055 -----------------------------------------------------------------------------
1058 literal :: { HsLit }
1059 : CHAR { HsChar $1 }
1060 | STRING { HsString $1 }
1061 | PRIMINTEGER { HsIntPrim $1 }
1062 | PRIMCHAR { HsCharPrim $1 }
1063 | PRIMSTRING { HsStringPrim $1 }
1064 | PRIMFLOAT { HsFloatPrim $1 }
1065 | PRIMDOUBLE { HsDoublePrim $1 }
1066 | CLITLIT { HsLitLit $1 (error "Parser.y: CLITLIT") }
1068 srcloc :: { SrcLoc } : {% getSrcLocP }
1070 -----------------------------------------------------------------------------
1074 : vccurly { () } -- context popped in lexer.
1075 | error {% popContext }
1077 layout_on :: { () } : {% layoutOn True{-strict-} }
1078 layout_on_for_do :: { () } : {% layoutOn False }
1080 -----------------------------------------------------------------------------
1081 -- Miscellaneous (mostly renamings)
1083 modid :: { ModuleName }
1084 : CONID { mkModuleNameFS $1 }
1086 tycon :: { RdrName }
1087 : CONID { mkUnqual tcClsName $1 }
1089 tyconop :: { RdrName }
1090 : CONSYM { mkUnqual tcClsName $1 }
1092 qtycon :: { RdrName }
1094 | QCONID { mkQual tcClsName $1 }
1096 qtyconop :: { RdrName }
1098 | QCONSYM { mkQual tcClsName $1 }
1100 qtycls :: { RdrName }
1104 : commas ',' { $1 + 1 }
1107 -----------------------------------------------------------------------------
1111 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)