2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.43 2000/10/24 08:40:10 simonpj Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parse ) where
16 import HsTypes ( mkHsTupCon )
17 import HsPat ( InPat(..) )
23 import PrelInfo ( mAIN_Name )
24 import OccName ( UserFS, varName, ipName, 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 }
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 '{-# DEPRECATED' { ITdeprecated_prag }
115 '#-}' { ITclose_prag }
118 '__interface' { ITinterface } -- interface keywords
119 '__export' { IT__export }
120 '__instimport' { ITinstimport }
121 '__forall' { IT__forall }
122 '__letrec' { ITletrec }
123 '__coerce' { ITcoerce }
124 '__depends' { ITdepends }
125 '__inline' { ITinline }
126 '__DEFAULT' { ITdefaultbranch }
128 '__integer' { ITinteger_lit }
129 '__float' { ITfloat_lit }
130 '__rational' { ITrational_lit }
131 '__addr' { ITaddr_lit }
132 '__label' { ITlabel_lit }
133 '__litlit' { ITlit_lit }
134 '__string' { ITstring_lit }
135 '__ccall' { ITccall $$ }
137 '__sccC' { ITsccAllCafs }
140 '__P' { ITspecialise }
142 '__U' { ITunfold $$ }
143 '__S' { ITstrict $$ }
144 '__M' { ITcprinfo $$ }
147 '..' { ITdotdot } -- reserved symbols
161 '{' { ITocurly } -- special symbols
165 vccurly { ITvccurly } -- virtual close curly (from layout)
176 VARID { ITvarid $$ } -- identifiers
178 VARSYM { ITvarsym $$ }
179 CONSYM { ITconsym $$ }
180 QVARID { ITqvarid $$ }
181 QCONID { ITqconid $$ }
182 QVARSYM { ITqvarsym $$ }
183 QCONSYM { ITqconsym $$ }
185 IPVARID { ITipvarid $$ } -- GHC extension
188 STRING { ITstring $$ }
189 INTEGER { ITinteger $$ }
190 RATIONAL { ITrational $$ }
192 PRIMCHAR { ITprimchar $$ }
193 PRIMSTRING { ITprimstring $$ }
194 PRIMINTEGER { ITprimint $$ }
195 PRIMFLOAT { ITprimfloat $$ }
196 PRIMDOUBLE { ITprimdouble $$ }
197 CLITLIT { ITlitlit $$ }
199 %monad { P } { thenP } { returnP }
200 %lexer { lexer } { ITeof }
205 -----------------------------------------------------------------------------
208 -- The place for module deprecation is really too restrictive, but if it
209 -- was allowed at its natural place just before 'module', we get an ugly
210 -- s/r conflict with the second alternative. Another solution would be the
211 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
212 -- either, and DEPRECATED is only expected to be used by people who really
213 -- know what they are doing. :-)
215 module :: { RdrNameHsModule }
216 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
217 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
219 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
221 maybemoddeprec :: { Maybe DeprecTxt }
222 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
223 | {- empty -} { Nothing }
225 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
227 | layout_on top close { $2 }
229 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
230 : importdecls { (reverse $1,[]) }
231 | importdecls ';' cvtopdecls { (reverse $1,$3) }
232 | cvtopdecls { ([],$1) }
234 cvtopdecls :: { [RdrNameHsDecl] }
235 : topdecls { cvTopDecls (groupBindings $1)}
237 -----------------------------------------------------------------------------
240 maybeexports :: { Maybe [RdrNameIE] }
241 : '(' exportlist ')' { Just $2 }
242 | {- empty -} { Nothing }
244 exportlist :: { [RdrNameIE] }
245 : exportlist ',' export { $3 : $1 }
246 | exportlist ',' { $1 }
250 -- GHC extension: we allow things like [] and (,,,) to be exported
251 export :: { RdrNameIE }
253 | gtycon { IEThingAbs $1 }
254 | gtycon '(' '..' ')' { IEThingAll $1 }
255 | gtycon '(' ')' { IEThingWith $1 [] }
256 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
257 | 'module' modid { IEModuleContents $2 }
259 qcnames :: { [RdrName] }
260 : qcnames ',' qcname { $3 : $1 }
263 qcname :: { RdrName }
267 -----------------------------------------------------------------------------
268 -- Import Declarations
270 -- import decls can be *empty*, or even just a string of semicolons
271 -- whereas topdecls must contain at least one topdecl.
273 importdecls :: { [RdrNameImportDecl] }
274 : importdecls ';' importdecl { $3 : $1 }
275 | importdecls ';' { $1 }
276 | importdecl { [ $1 ] }
279 importdecl :: { RdrNameImportDecl }
280 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
281 { ImportDecl (mkSrcModuleFS $5) $3 $4 $6 $7 $2 }
283 maybe_src :: { WhereFrom }
284 : '{-# SOURCE' '#-}' { ImportByUserSource }
285 | {- empty -} { ImportByUser }
287 optqualified :: { Bool }
288 : 'qualified' { True }
289 | {- empty -} { False }
291 maybeas :: { Maybe ModuleName }
292 : 'as' modid { Just $2 }
293 | {- empty -} { Nothing }
295 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
296 : impspec { Just $1 }
297 | {- empty -} { Nothing }
299 impspec :: { (Bool, [RdrNameIE]) }
300 : '(' exportlist ')' { (False, reverse $2) }
301 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
303 -----------------------------------------------------------------------------
304 -- Fixity Declarations
308 | INTEGER {% checkPrec $1 `thenP_`
309 returnP (fromInteger $1) }
311 infix :: { FixityDirection }
313 | 'infixl' { InfixL }
314 | 'infixr' { InfixR }
317 : ops ',' op { $3 : $1 }
320 -----------------------------------------------------------------------------
321 -- Top-Level Declarations
323 topdecls :: { [RdrBinding] }
324 : topdecls ';' topdecl { ($3 : $1) }
325 | topdecls ';' { $1 }
328 topdecl :: { RdrBinding }
329 : srcloc 'type' simpletype '=' sigtype
330 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
332 | srcloc 'data' ctype '=' constrs deriving
333 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
334 returnP (RdrHsDecl (TyClD
335 (mkTyData DataType cs c ts (reverse $5) (length $5) $6 $1))) }
337 | srcloc 'newtype' ctype '=' newconstr deriving
338 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
339 returnP (RdrHsDecl (TyClD
340 (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
342 | srcloc 'class' ctype fds where
343 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
345 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
347 returnP (RdrHsDecl (TyClD
348 (mkClassDecl cs c ts $4 sigs binds $1))) }
350 | srcloc 'instance' inst_type where
352 = cvMonoBindsAndSigs cvInstDeclSig
354 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
356 | srcloc 'default' '(' types0 ')'
357 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
359 | srcloc 'foreign' 'import' callconv ext_name
360 unsafe_flag varid_no_unsafe '::' sigtype
361 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
363 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
364 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
366 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
367 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
368 defaultCallConv $1)) }
370 | '{-# DEPRECATED' deprecations '#-}' { $2 }
371 | '{-# RULES' rules '#-}' { $2 }
374 decls :: { [RdrBinding] }
375 : decls ';' decl { $3 : $1 }
380 decl :: { RdrBinding }
383 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
384 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
385 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
386 { foldr1 RdrAndBindings
387 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
388 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
389 { RdrSig (SpecInstSig $4 $2) }
391 opt_phase :: { Maybe Int }
392 : INTEGER { Just (fromInteger $1) }
393 | {- empty -} { Nothing }
395 wherebinds :: { RdrNameHsBinds }
396 : where { cvBinds cvValSig (groupBindings $1) }
398 where :: { [RdrBinding] }
399 : 'where' decllist { $2 }
402 declbinds :: { RdrNameHsBinds }
403 : decllist { cvBinds cvValSig (groupBindings $1) }
405 decllist :: { [RdrBinding] }
406 : '{' decls '}' { $2 }
407 | layout_on decls close { $2 }
409 fixdecl :: { RdrBinding }
410 : srcloc infix prec ops { foldr1 RdrAndBindings
411 [ RdrSig (FixSig (FixitySig n
415 -----------------------------------------------------------------------------
416 -- Transformation Rules
418 rules :: { RdrBinding }
419 : rules ';' rule { $1 `RdrAndBindings` $3 }
422 | {- empty -} { RdrNullBind }
424 rule :: { RdrBinding }
425 : STRING rule_forall fexp '=' srcloc exp
426 { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
428 rule_forall :: { [RdrNameRuleBndr] }
429 : 'forall' rule_var_list '.' { $2 }
432 rule_var_list :: { [RdrNameRuleBndr] }
434 | rule_var rule_var_list { $1 : $2 }
436 rule_var :: { RdrNameRuleBndr }
437 : varid { RuleBndr $1 }
438 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
440 -----------------------------------------------------------------------------
443 deprecations :: { RdrBinding }
444 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
445 | deprecations ';' { $1 }
447 | {- empty -} { RdrNullBind }
449 -- SUP: TEMPORARY HACK, not checking for `module Foo'
450 deprecation :: { RdrBinding }
451 : srcloc depreclist STRING
452 { foldr RdrAndBindings RdrNullBind
453 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
455 -----------------------------------------------------------------------------
456 -- Foreign import/export
459 : 'stdcall' { stdCallConv }
460 | 'ccall' { cCallConv }
461 | {- empty -} { defaultCallConv }
463 unsafe_flag :: { Bool }
465 | {- empty -} { False }
467 ext_name :: { Maybe ExtName }
468 : 'dynamic' { Just Dynamic }
469 | STRING { Just (ExtName $1 Nothing) }
470 | STRING STRING { Just (ExtName $2 (Just $1)) }
471 | {- empty -} { Nothing }
474 -----------------------------------------------------------------------------
477 opt_sig :: { Maybe RdrNameHsType }
478 : {- empty -} { Nothing }
479 | '::' sigtype { Just $2 }
481 opt_asig :: { Maybe RdrNameHsType }
482 : {- empty -} { Nothing }
483 | '::' atype { Just $2 }
485 sigtypes :: { [RdrNameHsType] }
487 | sigtypes ',' sigtype { $3 : $1 }
489 sigtype :: { RdrNameHsType }
490 : ctype { (mkHsForAllTy Nothing [] $1) }
492 sig_vars :: { [RdrName] }
493 : sig_vars ',' var { $3 : $1 }
496 -----------------------------------------------------------------------------
499 -- A ctype is a for-all type
500 ctype :: { RdrNameHsType }
501 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
502 | context type { mkHsForAllTy Nothing $1 $2 }
503 -- A type of form (context => type) is an *implicit* HsForAllTy
506 type :: { RdrNameHsType }
507 : gentype '->' type { HsFunTy $1 $3 }
508 | ipvar '::' type { mkHsIParamTy $1 $3 }
511 gentype :: { RdrNameHsType }
514 | atype tyconop atype { HsOpTy $1 $2 $3 }
516 btype :: { RdrNameHsType }
517 : btype atype { (HsAppTy $1 $2) }
520 atype :: { RdrNameHsType }
521 : gtycon { HsTyVar $1 }
522 | tyvar { HsTyVar $1 }
523 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
524 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
525 | '[' type ']' { HsListTy $2 }
526 | '(' ctype ')' { $2 }
528 | INTEGER { HsNumTy $1 }
530 -- An inst_type is what occurs in the head of an instance decl
531 -- e.g. (Foo a, Gaz b) => Wibble a b
532 -- It's kept as a single type, with a MonoDictTy at the right
533 -- hand corner, for convenience.
534 inst_type :: { RdrNameHsType }
535 : ctype {% checkInstType $1 }
537 types0 :: { [RdrNameHsType] }
541 types :: { [RdrNameHsType] }
543 | types ',' type { $3 : $1 }
545 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
546 : tycon tyvars { ($1, reverse $2) }
548 tyvars :: { [RdrNameHsTyVar] }
549 : tyvars tyvar { UserTyVar $2 : $1 }
552 fds :: { [([RdrName], [RdrName])] }
554 | '|' fds1 { reverse $2 }
556 fds1 :: { [([RdrName], [RdrName])] }
557 : fds1 ',' fd { $3 : $1 }
560 fd :: { ([RdrName], [RdrName]) }
561 : varids0 '->' varids0 { (reverse $1, reverse $3) }
563 varids0 :: { [RdrName] }
565 | varids0 tyvar { $2 : $1 }
567 -----------------------------------------------------------------------------
568 -- Datatype declarations
570 newconstr :: { RdrNameConDecl }
571 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [Unbanged $3]) $1 }
572 | srcloc conid '{' var '::' type '}'
573 { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $1 }
575 constrs :: { [RdrNameConDecl] }
576 : constrs '|' constr { $3 : $1 }
579 constr :: { RdrNameConDecl }
580 : srcloc forall context constr_stuff
581 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
582 | srcloc forall constr_stuff
583 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
585 forall :: { [RdrNameHsTyVar] }
586 : 'forall' tyvars '.' { $2 }
589 context :: { RdrNameContext }
590 : btype '=>' {% checkContext $1 }
592 constr_stuff :: { (RdrName, RdrNameConDetails) }
593 : btype {% mkVanillaCon $1 [] }
594 | btype '!' atype satypes {% mkVanillaCon $1 (Banged $3 : $4) }
595 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
596 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
598 satypes :: { [RdrNameBangType] }
599 : atype satypes { Unbanged $1 : $2 }
600 | '!' atype satypes { Banged $2 : $3 }
603 sbtype :: { RdrNameBangType }
604 : btype { Unbanged $1 }
605 | '!' atype { Banged $2 }
607 fielddecls :: { [([RdrName],RdrNameBangType)] }
608 : fielddecl ',' fielddecls { $1 : $3 }
611 fielddecl :: { ([RdrName],RdrNameBangType) }
612 : sig_vars '::' stype { (reverse $1, $3) }
614 stype :: { RdrNameBangType }
615 : ctype { Unbanged $1 }
616 | '!' atype { Banged $2 }
618 deriving :: { Maybe [RdrName] }
619 : {- empty -} { Nothing }
620 | 'deriving' qtycls { Just [$2] }
621 | 'deriving' '(' ')' { Just [] }
622 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
624 dclasses :: { [RdrName] }
625 : dclasses ',' qtycls { $3 : $1 }
628 -----------------------------------------------------------------------------
631 {- There's an awkward overlap with a type signature. Consider
632 f :: Int -> Int = ...rhs...
633 Then we can't tell whether it's a type signature or a value
634 definition with a result signature until we see the '='.
635 So we have to inline enough to postpone reductions until we know.
639 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
640 instead of qvar, we get another shift/reduce-conflict. Consider the
643 { (^^) :: Int->Int ; } Type signature; only var allowed
645 { (^^) :: Int->Int = ... ; } Value defn with result signature;
646 qvar allowed (because of instance decls)
648 We can't tell whether to reduce var to qvar until after we've read the signatures.
651 valdef :: { RdrBinding }
652 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
653 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
654 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
655 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
659 rhs :: { RdrNameGRHSs }
660 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2)
662 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
664 gdrhs :: { [RdrNameGRHS] }
665 : gdrhs gdrh { $2 : $1 }
668 gdrh :: { RdrNameGRHS }
669 : '|' srcloc quals '=' exp { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
671 -----------------------------------------------------------------------------
674 exp :: { RdrNameHsExpr }
675 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
676 | infixexp 'with' dbinding { HsWith $1 $3 }
679 infixexp :: { RdrNameHsExpr }
681 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
682 (panic "fixity") $3 )}
684 exp10 :: { RdrNameHsExpr }
685 : '\\' aexp aexps opt_asig '->' srcloc exp
686 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
687 returnP (HsLam (Match [] ps $4
688 (GRHSs (unguardedRHS $7 $6)
689 EmptyBinds Nothing))) }
690 | 'let' declbinds 'in' exp { HsLet $2 $4 }
691 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
692 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
693 | '-' fexp { mkHsNegApp $2 }
694 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
696 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 False False cbot }
697 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 True False cbot }
698 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 False True cbot }
699 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 True True cbot }
701 | '_scc_' STRING exp { if opt_SccProfilingOn
707 ccallid :: { FAST_STRING }
711 fexp :: { RdrNameHsExpr }
712 : fexp aexp { (HsApp $1 $2) }
715 aexps0 :: { [RdrNameHsExpr] }
716 : aexps { (reverse $1) }
718 aexps :: { [RdrNameHsExpr] }
719 : aexps aexp { $2 : $1 }
722 aexp :: { RdrNameHsExpr }
723 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
724 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
728 var_or_con :: { RdrNameHsExpr }
732 aexp1 :: { RdrNameHsExpr }
733 : ipvar { HsIPVar $1 }
735 | literal { HsLit $1 }
736 | INTEGER { HsOverLit (mkHsIntegralLit $1) }
737 | RATIONAL { HsOverLit (mkHsFractionalLit $1) }
738 | '(' exp ')' { HsPar $2 }
739 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
740 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
741 | '[' list ']' { $2 }
742 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
743 | '(' qopm infixexp ')' { (SectionR $2 $3) }
744 | qvar '@' aexp { EAsPat $1 $3 }
746 | '~' aexp1 { ELazyPat $2 }
748 texps :: { [RdrNameHsExpr] }
749 : texps ',' exp { $3 : $1 }
753 -----------------------------------------------------------------------------
756 -- The rules below are little bit contorted to keep lexps left-recursive while
757 -- avoiding another shift/reduce-conflict.
759 list :: { RdrNameHsExpr }
760 : exp { ExplicitList [$1] }
761 | lexps { ExplicitList (reverse $1) }
762 | exp '..' { ArithSeqIn (From $1) }
763 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
764 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
765 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
766 | exp srcloc '|' quals { HsDo ListComp (reverse
767 (ReturnStmt $1 : $4)) $2 }
769 lexps :: { [RdrNameHsExpr] }
770 : lexps ',' exp { $3 : $1 }
771 | exp ',' exp { [$3,$1] }
773 -----------------------------------------------------------------------------
774 -- List Comprehensions
776 quals :: { [RdrNameStmt] }
777 : quals ',' qual { $3 : $1 }
780 qual :: { RdrNameStmt }
781 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
782 returnP (BindStmt p $4 $1) }
783 | srcloc exp { GuardStmt $2 $1 }
784 | srcloc 'let' declbinds { LetStmt $3 }
786 -----------------------------------------------------------------------------
789 altslist :: { [RdrNameMatch] }
790 : '{' alts '}' { reverse $2 }
791 | layout_on alts close { reverse $2 }
793 alts :: { [RdrNameMatch] }
797 alts1 :: { [RdrNameMatch] }
798 : alts1 ';' alt { $3 : $1 }
802 alt :: { RdrNameMatch }
803 : infixexp opt_sig ralt wherebinds
804 {% (checkPattern $1 `thenP` \p ->
805 returnP (Match [] [p] $2
806 (GRHSs $3 $4 Nothing)) )}
808 ralt :: { [RdrNameGRHS] }
809 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
810 | gdpats { (reverse $1) }
812 gdpats :: { [RdrNameGRHS] }
813 : gdpats gdpat { $2 : $1 }
816 gdpat :: { RdrNameGRHS }
817 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
819 -----------------------------------------------------------------------------
820 -- Statement sequences
822 stmtlist :: { [RdrNameStmt] }
823 : '{' stmts '}' { reverse $2 }
824 | layout_on_for_do stmts close { reverse $2 }
826 -- Stmt list should really end in an expression, but it's not
827 -- convenient to enforce this here, so we throw out erroneous
828 -- statement sequences in the renamer instead.
830 stmts :: { [RdrNameStmt] }
834 stmts1 :: { [RdrNameStmt] }
835 : stmts1 ';' stmt { $3 : $1 }
839 stmt :: { RdrNameStmt }
840 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
841 returnP (BindStmt p $4 $1) }
842 | srcloc exp { ExprStmt $2 $1 }
843 | srcloc 'let' declbinds { LetStmt $3 }
845 -----------------------------------------------------------------------------
846 -- Record Field Update/Construction
848 fbinds :: { RdrNameHsRecordBinds }
849 : fbinds ',' fbind { $3 : $1 }
854 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
855 : qvar '=' exp { ($1,$3,False) }
857 -----------------------------------------------------------------------------
858 -- Implicit Parameter Bindings
860 dbinding :: { [(RdrName, RdrNameHsExpr)] }
861 : '{' dbinds '}' { $2 }
862 | layout_on dbinds close { $2 }
864 dbinds :: { [(RdrName, RdrNameHsExpr)] }
865 : dbinds ';' dbind { $3 : $1 }
870 dbind :: { (RdrName, RdrNameHsExpr) }
871 dbind : ipvar '=' exp { ($1, $3) }
873 -----------------------------------------------------------------------------
874 -- Variables, Constructors and Operators.
876 depreclist :: { [RdrName] }
877 depreclist : deprec_var { [$1] }
878 | deprec_var ',' depreclist { $1 : $2 }
880 deprec_var :: { RdrName }
881 deprec_var : var { $1 }
884 gtycon :: { RdrName }
886 | '(' qtyconop ')' { $2 }
887 | '(' ')' { unitTyCon_RDR }
888 | '(' '->' ')' { funTyCon_RDR }
889 | '[' ']' { listTyCon_RDR }
890 | '(' commas ')' { tupleTyCon_RDR $2 }
893 : '(' ')' { unitCon_RDR }
894 | '[' ']' { nilCon_RDR }
895 | '(' commas ')' { tupleCon_RDR $2 }
900 | '(' varsym ')' { $2 }
904 | '(' varsym ')' { $2 }
905 | '(' qvarsym1 ')' { $2 }
906 -- We've inlined qvarsym here so that the decision about
907 -- whether it's a qvar or a var can be postponed until
908 -- *after* we see the close paren.
911 : IPVARID { (mkUnqual ipName (tailFS $1)) }
915 | '(' qconsym ')' { $2 }
919 | '`' varid '`' { $2 }
921 qvarop :: { RdrName }
923 | '`' qvarid '`' { $2 }
925 qvaropm :: { RdrName }
926 : qvarsym_no_minus { $1 }
927 | '`' qvarid '`' { $2 }
931 | '`' conid '`' { $2 }
933 qconop :: { RdrName }
935 | '`' qconid '`' { $2 }
937 -----------------------------------------------------------------------------
940 op :: { RdrName } -- used in infix decls
944 qop :: { RdrName {-HsExpr-} } -- used in sections
948 qopm :: { RdrNameHsExpr } -- used in sections
949 : qvaropm { HsVar $1 }
950 | qconop { HsVar $1 }
952 -----------------------------------------------------------------------------
955 qvarid :: { RdrName }
957 | QVARID { mkQual varName $1 }
960 : varid_no_unsafe { $1 }
961 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
963 varid_no_unsafe :: { RdrName }
964 : VARID { mkUnqual varName $1 }
965 | special_id { mkUnqual varName $1 }
966 | 'forall' { mkUnqual varName SLIT("forall") }
969 : VARID { mkUnqual tvName $1 }
970 | special_id { mkUnqual tvName $1 }
971 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
973 -- These special_ids are treated as keywords in various places,
974 -- but as ordinary ids elsewhere. A special_id collects all thsee
975 -- except 'unsafe' and 'forall' whose treatment differs depending on context
976 special_id :: { UserFS }
978 : 'as' { SLIT("as") }
979 | 'qualified' { SLIT("qualified") }
980 | 'hiding' { SLIT("hiding") }
981 | 'export' { SLIT("export") }
982 | 'label' { SLIT("label") }
983 | 'dynamic' { SLIT("dynamic") }
984 | 'stdcall' { SLIT("stdcall") }
985 | 'ccall' { SLIT("ccall") }
987 -----------------------------------------------------------------------------
990 qconid :: { RdrName }
992 | QCONID { mkQual dataName $1 }
995 : CONID { mkUnqual dataName $1 }
997 -----------------------------------------------------------------------------
1000 qconsym :: { RdrName }
1002 | QCONSYM { mkQual dataName $1 }
1004 consym :: { RdrName }
1005 : CONSYM { mkUnqual dataName $1 }
1007 -----------------------------------------------------------------------------
1010 qvarsym :: { RdrName }
1014 qvarsym_no_minus :: { RdrName }
1015 : varsym_no_minus { $1 }
1018 qvarsym1 :: { RdrName }
1019 qvarsym1 : QVARSYM { mkQual varName $1 }
1021 varsym :: { RdrName }
1022 : varsym_no_minus { $1 }
1023 | '-' { mkUnqual varName SLIT("-") }
1025 varsym_no_minus :: { RdrName } -- varsym not including '-'
1026 : VARSYM { mkUnqual varName $1 }
1027 | special_sym { mkUnqual varName $1 }
1030 -- See comments with special_id
1031 special_sym :: { UserFS }
1032 special_sym : '!' { SLIT("!") }
1035 -----------------------------------------------------------------------------
1038 literal :: { HsLit }
1039 : CHAR { HsChar $1 }
1040 | STRING { HsString $1 }
1041 | PRIMINTEGER { HsIntPrim $1 }
1042 | PRIMCHAR { HsCharPrim $1 }
1043 | PRIMSTRING { HsStringPrim $1 }
1044 | PRIMFLOAT { HsFloatPrim $1 }
1045 | PRIMDOUBLE { HsDoublePrim $1 }
1046 | CLITLIT { HsLitLit $1 (error "Parser.y: CLITLIT") }
1048 srcloc :: { SrcLoc } : {% getSrcLocP }
1050 -----------------------------------------------------------------------------
1054 : vccurly { () } -- context popped in lexer.
1055 | error {% popContext }
1057 layout_on :: { () } : {% layoutOn True{-strict-} }
1058 layout_on_for_do :: { () } : {% layoutOn False }
1060 -----------------------------------------------------------------------------
1061 -- Miscellaneous (mostly renamings)
1063 modid :: { ModuleName }
1064 : CONID { mkSrcModuleFS $1 }
1066 tycon :: { RdrName }
1067 : CONID { mkUnqual tcClsName $1 }
1069 tyconop :: { RdrName }
1070 : CONSYM { mkUnqual tcClsName $1 }
1072 qtycon :: { RdrName }
1074 | QCONID { mkQual tcClsName $1 }
1076 qtyconop :: { RdrName }
1078 | QCONSYM { mkQual tcClsName $1 }
1080 qtycls :: { RdrName }
1084 : commas ',' { $1 + 1 }
1087 -----------------------------------------------------------------------------
1091 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)