2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.47 2000/11/07 15:21:40 simonmar Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parse ) where
15 import HsTypes ( mkHsTupCon )
16 import HsPat ( InPat(..) )
23 import OccName ( UserFS, varName, ipName, tcName, dataName, tcClsName, tvName )
24 import SrcLoc ( SrcLoc )
27 import CmdLineOpts ( opt_SccProfilingOn )
28 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), NewOrData(..) )
32 import FastString ( tailFS )
35 #include "HsVersions.h"
39 -----------------------------------------------------------------------------
40 Conflicts: 14 shift/reduce
41 (note: it's currently 21 -- JRL, 31/1/2000)
43 8 for abiguity in 'if x then y else z + 1'
44 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
45 1 for ambiguity in 'if x then y else z :: T'
46 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
47 3 for ambiguity in 'case x of y :: a -> b'
48 (don't know whether to reduce 'a' as a btype or shift the '->'.
49 conclusion: bogus expression anyway, doesn't matter)
51 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
52 since 'forall' is a valid variable name, we don't know whether
53 to treat a forall on the input as the beginning of a quantifier
54 or the beginning of the rule itself. Resolving to shift means
55 it's always treated as a quantifier, hence the above is disallowed.
56 This saves explicitly defining a grammar for the rule lhs that
57 doesn't include 'forall'.
59 1 for ambiguity in 'x @ Rec{..}'.
60 Only sensible parse is 'x @ (Rec{..})', which is what resolving
63 -----------------------------------------------------------------------------
67 '_' { ITunderscore } -- Haskell keywords
72 'default' { ITdefault }
73 'deriving' { ITderiving }
83 'instance' { ITinstance }
86 'newtype' { ITnewtype }
88 'qualified' { ITqualified }
94 'forall' { ITforall } -- GHC extension keywords
95 'foreign' { ITforeign }
98 'dynamic' { ITdynamic }
101 'stdcall' { ITstdcallconv }
102 'ccall' { ITccallconv }
103 '_ccall_' { ITccall (False, False, False) }
104 '_ccall_GC_' { ITccall (False, False, True) }
105 '_casm_' { ITccall (False, True, False) }
106 '_casm_GC_' { ITccall (False, True, True) }
108 '{-# SPECIALISE' { ITspecialise_prag }
109 '{-# SOURCE' { ITsource_prag }
110 '{-# INLINE' { ITinline_prag }
111 '{-# NOINLINE' { ITnoinline_prag }
112 '{-# RULES' { ITrules_prag }
113 '{-# DEPRECATED' { ITdeprecated_prag }
114 '#-}' { ITclose_prag }
117 '__interface' { ITinterface } -- interface keywords
118 '__export' { IT__export }
119 '__instimport' { ITinstimport }
120 '__forall' { IT__forall }
121 '__letrec' { ITletrec }
122 '__coerce' { ITcoerce }
123 '__depends' { ITdepends }
124 '__inline' { ITinline }
125 '__DEFAULT' { ITdefaultbranch }
127 '__integer' { ITinteger_lit }
128 '__float' { ITfloat_lit }
129 '__rational' { ITrational_lit }
130 '__addr' { ITaddr_lit }
131 '__label' { ITlabel_lit }
132 '__litlit' { ITlit_lit }
133 '__string' { ITstring_lit }
134 '__ccall' { ITccall $$ }
136 '__sccC' { ITsccAllCafs }
139 '__P' { ITspecialise }
141 '__U' { ITunfold $$ }
142 '__S' { ITstrict $$ }
143 '__M' { ITcprinfo $$ }
146 '..' { ITdotdot } -- reserved symbols
160 '{' { ITocurly } -- special symbols
164 vccurly { ITvccurly } -- virtual close curly (from layout)
175 VARID { ITvarid $$ } -- identifiers
177 VARSYM { ITvarsym $$ }
178 CONSYM { ITconsym $$ }
179 QVARID { ITqvarid $$ }
180 QCONID { ITqconid $$ }
181 QVARSYM { ITqvarsym $$ }
182 QCONSYM { ITqconsym $$ }
184 IPVARID { ITipvarid $$ } -- GHC extension
187 STRING { ITstring $$ }
188 INTEGER { ITinteger $$ }
189 RATIONAL { ITrational $$ }
191 PRIMCHAR { ITprimchar $$ }
192 PRIMSTRING { ITprimstring $$ }
193 PRIMINTEGER { ITprimint $$ }
194 PRIMFLOAT { ITprimfloat $$ }
195 PRIMDOUBLE { ITprimdouble $$ }
196 CLITLIT { ITlitlit $$ }
198 %monad { P } { thenP } { returnP }
199 %lexer { lexer } { ITeof }
204 -----------------------------------------------------------------------------
207 -- The place for module deprecation is really too restrictive, but if it
208 -- was allowed at its natural place just before 'module', we get an ugly
209 -- s/r conflict with the second alternative. Another solution would be the
210 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
211 -- either, and DEPRECATED is only expected to be used by people who really
212 -- know what they are doing. :-)
214 module :: { RdrNameHsModule }
215 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
216 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
218 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
220 maybemoddeprec :: { Maybe DeprecTxt }
221 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
222 | {- empty -} { Nothing }
224 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
226 | layout_on top close { $2 }
228 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
229 : importdecls { (reverse $1,[]) }
230 | importdecls ';' cvtopdecls { (reverse $1,$3) }
231 | cvtopdecls { ([],$1) }
233 cvtopdecls :: { [RdrNameHsDecl] }
234 : topdecls { cvTopDecls (groupBindings $1)}
236 -----------------------------------------------------------------------------
239 maybeexports :: { Maybe [RdrNameIE] }
240 : '(' exportlist ')' { Just $2 }
241 | {- empty -} { Nothing }
243 exportlist :: { [RdrNameIE] }
244 : exportlist ',' export { $3 : $1 }
245 | exportlist ',' { $1 }
249 -- GHC extension: we allow things like [] and (,,,) to be exported
250 export :: { RdrNameIE }
252 | gtycon { IEThingAbs $1 }
253 | gtycon '(' '..' ')' { IEThingAll $1 }
254 | gtycon '(' ')' { IEThingWith $1 [] }
255 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
256 | 'module' modid { IEModuleContents $2 }
258 qcnames :: { [RdrName] }
259 : qcnames ',' qcname { $3 : $1 }
262 qcname :: { RdrName }
266 -----------------------------------------------------------------------------
267 -- Import Declarations
269 -- import decls can be *empty*, or even just a string of semicolons
270 -- whereas topdecls must contain at least one topdecl.
272 importdecls :: { [RdrNameImportDecl] }
273 : importdecls ';' importdecl { $3 : $1 }
274 | importdecls ';' { $1 }
275 | importdecl { [ $1 ] }
278 importdecl :: { RdrNameImportDecl }
279 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
280 { ImportDecl (mkModuleNameFS $5) $3 $4 $6 $7 $2 }
282 maybe_src :: { WhereFrom }
283 : '{-# SOURCE' '#-}' { ImportByUserSource }
284 | {- empty -} { ImportByUser }
286 optqualified :: { Bool }
287 : 'qualified' { True }
288 | {- empty -} { False }
290 maybeas :: { Maybe ModuleName }
291 : 'as' modid { Just $2 }
292 | {- empty -} { Nothing }
294 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
295 : impspec { Just $1 }
296 | {- empty -} { Nothing }
298 impspec :: { (Bool, [RdrNameIE]) }
299 : '(' exportlist ')' { (False, reverse $2) }
300 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
302 -----------------------------------------------------------------------------
303 -- Fixity Declarations
307 | INTEGER {% checkPrec $1 `thenP_`
308 returnP (fromInteger $1) }
310 infix :: { FixityDirection }
312 | 'infixl' { InfixL }
313 | 'infixr' { InfixR }
316 : ops ',' op { $3 : $1 }
319 -----------------------------------------------------------------------------
320 -- Top-Level Declarations
322 topdecls :: { [RdrBinding] }
323 : topdecls ';' topdecl { ($3 : $1) }
324 | topdecls ';' { $1 }
327 topdecl :: { RdrBinding }
328 : srcloc 'type' simpletype '=' sigtype
329 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
331 | srcloc 'data' ctype '=' constrs deriving
332 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
333 returnP (RdrHsDecl (TyClD
334 (mkTyData DataType cs c ts (reverse $5) (length $5) $6 $1))) }
336 | srcloc 'newtype' ctype '=' newconstr deriving
337 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
338 returnP (RdrHsDecl (TyClD
339 (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
341 | srcloc 'class' ctype fds where
342 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
344 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
346 returnP (RdrHsDecl (TyClD
347 (mkClassDecl cs c ts $4 sigs binds $1))) }
349 | srcloc 'instance' inst_type where
351 = cvMonoBindsAndSigs cvInstDeclSig
353 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
355 | srcloc 'default' '(' types0 ')'
356 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
358 | srcloc 'foreign' 'import' callconv ext_name
359 unsafe_flag varid_no_unsafe '::' sigtype
360 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
362 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
363 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
365 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
366 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
367 defaultCallConv $1)) }
369 | '{-# DEPRECATED' deprecations '#-}' { $2 }
370 | '{-# RULES' rules '#-}' { $2 }
373 decls :: { [RdrBinding] }
374 : decls ';' decl { $3 : $1 }
379 decl :: { RdrBinding }
382 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
383 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
384 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
385 { foldr1 RdrAndBindings
386 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
387 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
388 { RdrSig (SpecInstSig $4 $2) }
390 opt_phase :: { Maybe Int }
391 : INTEGER { Just (fromInteger $1) }
392 | {- empty -} { Nothing }
394 wherebinds :: { RdrNameHsBinds }
395 : where { cvBinds cvValSig (groupBindings $1) }
397 where :: { [RdrBinding] }
398 : 'where' decllist { $2 }
401 declbinds :: { RdrNameHsBinds }
402 : decllist { cvBinds cvValSig (groupBindings $1) }
404 decllist :: { [RdrBinding] }
405 : '{' decls '}' { $2 }
406 | layout_on decls close { $2 }
408 fixdecl :: { RdrBinding }
409 : srcloc infix prec ops { foldr1 RdrAndBindings
410 [ RdrSig (FixSig (FixitySig n
414 -----------------------------------------------------------------------------
415 -- Transformation Rules
417 rules :: { RdrBinding }
418 : rules ';' rule { $1 `RdrAndBindings` $3 }
421 | {- empty -} { RdrNullBind }
423 rule :: { RdrBinding }
424 : STRING rule_forall fexp '=' srcloc exp
425 { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
427 rule_forall :: { [RdrNameRuleBndr] }
428 : 'forall' rule_var_list '.' { $2 }
431 rule_var_list :: { [RdrNameRuleBndr] }
433 | rule_var rule_var_list { $1 : $2 }
435 rule_var :: { RdrNameRuleBndr }
436 : varid { RuleBndr $1 }
437 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
439 -----------------------------------------------------------------------------
442 deprecations :: { RdrBinding }
443 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
444 | deprecations ';' { $1 }
446 | {- empty -} { RdrNullBind }
448 -- SUP: TEMPORARY HACK, not checking for `module Foo'
449 deprecation :: { RdrBinding }
450 : srcloc depreclist STRING
451 { foldr RdrAndBindings RdrNullBind
452 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
454 -----------------------------------------------------------------------------
455 -- Foreign import/export
458 : 'stdcall' { stdCallConv }
459 | 'ccall' { cCallConv }
460 | {- empty -} { defaultCallConv }
462 unsafe_flag :: { Bool }
464 | {- empty -} { False }
466 ext_name :: { Maybe ExtName }
467 : 'dynamic' { Just Dynamic }
468 | STRING { Just (ExtName $1 Nothing) }
469 | STRING STRING { Just (ExtName $2 (Just $1)) }
470 | {- empty -} { Nothing }
473 -----------------------------------------------------------------------------
476 opt_sig :: { Maybe RdrNameHsType }
477 : {- empty -} { Nothing }
478 | '::' sigtype { Just $2 }
480 opt_asig :: { Maybe RdrNameHsType }
481 : {- empty -} { Nothing }
482 | '::' atype { Just $2 }
484 sigtypes :: { [RdrNameHsType] }
486 | sigtypes ',' sigtype { $3 : $1 }
488 sigtype :: { RdrNameHsType }
489 : ctype { (mkHsForAllTy Nothing [] $1) }
491 sig_vars :: { [RdrName] }
492 : sig_vars ',' var { $3 : $1 }
495 -----------------------------------------------------------------------------
498 -- A ctype is a for-all type
499 ctype :: { RdrNameHsType }
500 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
501 | context type { mkHsForAllTy Nothing $1 $2 }
502 -- A type of form (context => type) is an *implicit* HsForAllTy
505 type :: { RdrNameHsType }
506 : gentype '->' type { HsFunTy $1 $3 }
507 | ipvar '::' type { mkHsIParamTy $1 $3 }
510 gentype :: { RdrNameHsType }
513 | atype tyconop atype { HsOpTy $1 $2 $3 }
515 btype :: { RdrNameHsType }
516 : btype atype { (HsAppTy $1 $2) }
519 atype :: { RdrNameHsType }
520 : gtycon { HsTyVar $1 }
521 | tyvar { HsTyVar $1 }
522 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
523 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
524 | '[' type ']' { HsListTy $2 }
525 | '(' ctype ')' { $2 }
527 | INTEGER { HsNumTy $1 }
529 -- An inst_type is what occurs in the head of an instance decl
530 -- e.g. (Foo a, Gaz b) => Wibble a b
531 -- It's kept as a single type, with a MonoDictTy at the right
532 -- hand corner, for convenience.
533 inst_type :: { RdrNameHsType }
534 : ctype {% checkInstType $1 }
536 types0 :: { [RdrNameHsType] }
540 types :: { [RdrNameHsType] }
542 | types ',' type { $3 : $1 }
544 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
545 : tycon tyvars { ($1, reverse $2) }
547 tyvars :: { [RdrNameHsTyVar] }
548 : tyvars tyvar { UserTyVar $2 : $1 }
551 fds :: { [([RdrName], [RdrName])] }
553 | '|' fds1 { reverse $2 }
555 fds1 :: { [([RdrName], [RdrName])] }
556 : fds1 ',' fd { $3 : $1 }
559 fd :: { ([RdrName], [RdrName]) }
560 : varids0 '->' varids0 { (reverse $1, reverse $3) }
562 varids0 :: { [RdrName] }
564 | varids0 tyvar { $2 : $1 }
566 -----------------------------------------------------------------------------
567 -- Datatype declarations
569 newconstr :: { RdrNameConDecl }
570 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [Unbanged $3]) $1 }
571 | srcloc conid '{' var '::' type '}'
572 { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $1 }
574 constrs :: { [RdrNameConDecl] }
575 : constrs '|' constr { $3 : $1 }
578 constr :: { RdrNameConDecl }
579 : srcloc forall context constr_stuff
580 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
581 | srcloc forall constr_stuff
582 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
584 forall :: { [RdrNameHsTyVar] }
585 : 'forall' tyvars '.' { $2 }
588 context :: { RdrNameContext }
589 : btype '=>' {% checkContext $1 }
591 constr_stuff :: { (RdrName, RdrNameConDetails) }
592 : btype {% mkVanillaCon $1 [] }
593 | btype '!' atype satypes {% mkVanillaCon $1 (Banged $3 : $4) }
594 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
595 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
597 satypes :: { [RdrNameBangType] }
598 : atype satypes { Unbanged $1 : $2 }
599 | '!' atype satypes { Banged $2 : $3 }
602 sbtype :: { RdrNameBangType }
603 : btype { Unbanged $1 }
604 | '!' atype { Banged $2 }
606 fielddecls :: { [([RdrName],RdrNameBangType)] }
607 : fielddecl ',' fielddecls { $1 : $3 }
610 fielddecl :: { ([RdrName],RdrNameBangType) }
611 : sig_vars '::' stype { (reverse $1, $3) }
613 stype :: { RdrNameBangType }
614 : ctype { Unbanged $1 }
615 | '!' atype { Banged $2 }
617 deriving :: { Maybe [RdrName] }
618 : {- empty -} { Nothing }
619 | 'deriving' qtycls { Just [$2] }
620 | 'deriving' '(' ')' { Just [] }
621 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
623 dclasses :: { [RdrName] }
624 : dclasses ',' qtycls { $3 : $1 }
627 -----------------------------------------------------------------------------
630 {- There's an awkward overlap with a type signature. Consider
631 f :: Int -> Int = ...rhs...
632 Then we can't tell whether it's a type signature or a value
633 definition with a result signature until we see the '='.
634 So we have to inline enough to postpone reductions until we know.
638 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
639 instead of qvar, we get another shift/reduce-conflict. Consider the
642 { (^^) :: Int->Int ; } Type signature; only var allowed
644 { (^^) :: Int->Int = ... ; } Value defn with result signature;
645 qvar allowed (because of instance decls)
647 We can't tell whether to reduce var to qvar until after we've read the signatures.
650 valdef :: { RdrBinding }
651 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
652 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
653 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
654 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
658 rhs :: { RdrNameGRHSs }
659 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2)
661 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
663 gdrhs :: { [RdrNameGRHS] }
664 : gdrhs gdrh { $2 : $1 }
667 gdrh :: { RdrNameGRHS }
668 : '|' srcloc quals '=' exp { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
670 -----------------------------------------------------------------------------
673 exp :: { RdrNameHsExpr }
674 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
675 | infixexp 'with' dbinding { HsWith $1 $3 }
678 infixexp :: { RdrNameHsExpr }
680 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
681 (panic "fixity") $3 )}
683 exp10 :: { RdrNameHsExpr }
684 : '\\' aexp aexps opt_asig '->' srcloc exp
685 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
686 returnP (HsLam (Match [] ps $4
687 (GRHSs (unguardedRHS $7 $6)
688 EmptyBinds Nothing))) }
689 | 'let' declbinds 'in' exp { HsLet $2 $4 }
690 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
691 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
692 | '-' fexp { mkHsNegApp $2 }
693 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
695 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 False False cbot }
696 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 True False cbot }
697 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 False True cbot }
698 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 True True cbot }
700 | '_scc_' STRING exp { if opt_SccProfilingOn
706 ccallid :: { FAST_STRING }
710 fexp :: { RdrNameHsExpr }
711 : fexp aexp { (HsApp $1 $2) }
714 aexps0 :: { [RdrNameHsExpr] }
715 : aexps { (reverse $1) }
717 aexps :: { [RdrNameHsExpr] }
718 : aexps aexp { $2 : $1 }
721 aexp :: { RdrNameHsExpr }
722 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
723 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
727 var_or_con :: { RdrNameHsExpr }
731 aexp1 :: { RdrNameHsExpr }
732 : ipvar { HsIPVar $1 }
734 | literal { HsLit $1 }
735 | INTEGER { HsOverLit (HsIntegral $1 fromInteger_RDR) }
736 | RATIONAL { HsOverLit (HsFractional $1 fromRational_RDR) }
737 | '(' exp ')' { HsPar $2 }
738 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
739 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
740 | '[' list ']' { $2 }
741 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
742 | '(' qopm infixexp ')' { (SectionR $2 $3) }
743 | qvar '@' aexp { EAsPat $1 $3 }
745 | '~' aexp1 { ELazyPat $2 }
747 texps :: { [RdrNameHsExpr] }
748 : texps ',' exp { $3 : $1 }
752 -----------------------------------------------------------------------------
755 -- The rules below are little bit contorted to keep lexps left-recursive while
756 -- avoiding another shift/reduce-conflict.
758 list :: { RdrNameHsExpr }
759 : exp { ExplicitList [$1] }
760 | lexps { ExplicitList (reverse $1) }
761 | exp '..' { ArithSeqIn (From $1) }
762 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
763 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
764 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
765 | exp srcloc pquals {% let { body [qs] = qs;
766 body qss = [ParStmt (map reverse qss)] }
768 returnP ( HsDo ListComp
769 (reverse (ReturnStmt $1 : body $3))
774 lexps :: { [RdrNameHsExpr] }
775 : lexps ',' exp { $3 : $1 }
776 | exp ',' exp { [$3,$1] }
778 -----------------------------------------------------------------------------
779 -- List Comprehensions
781 pquals :: { [[RdrNameStmt]] }
782 : pquals '|' quals { $3 : $1 }
785 quals :: { [RdrNameStmt] }
786 : quals ',' qual { $3 : $1 }
789 qual :: { RdrNameStmt }
790 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
791 returnP (BindStmt p $4 $1) }
792 | srcloc exp { GuardStmt $2 $1 }
793 | srcloc 'let' declbinds { LetStmt $3 }
795 -----------------------------------------------------------------------------
798 altslist :: { [RdrNameMatch] }
799 : '{' alts '}' { reverse $2 }
800 | layout_on alts close { reverse $2 }
802 alts :: { [RdrNameMatch] }
806 alts1 :: { [RdrNameMatch] }
807 : alts1 ';' alt { $3 : $1 }
811 alt :: { RdrNameMatch }
812 : infixexp opt_sig ralt wherebinds
813 {% (checkPattern $1 `thenP` \p ->
814 returnP (Match [] [p] $2
815 (GRHSs $3 $4 Nothing)) )}
817 ralt :: { [RdrNameGRHS] }
818 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
819 | gdpats { (reverse $1) }
821 gdpats :: { [RdrNameGRHS] }
822 : gdpats gdpat { $2 : $1 }
825 gdpat :: { RdrNameGRHS }
826 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
828 -----------------------------------------------------------------------------
829 -- Statement sequences
831 stmtlist :: { [RdrNameStmt] }
832 : '{' stmts '}' { reverse $2 }
833 | layout_on_for_do stmts close { reverse $2 }
835 -- Stmt list should really end in an expression, but it's not
836 -- convenient to enforce this here, so we throw out erroneous
837 -- statement sequences in the renamer instead.
839 stmts :: { [RdrNameStmt] }
843 stmts1 :: { [RdrNameStmt] }
844 : stmts1 ';' stmt { $3 : $1 }
848 stmt :: { RdrNameStmt }
849 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
850 returnP (BindStmt p $4 $1) }
851 | srcloc exp { ExprStmt $2 $1 }
852 | srcloc 'let' declbinds { LetStmt $3 }
854 -----------------------------------------------------------------------------
855 -- Record Field Update/Construction
857 fbinds :: { RdrNameHsRecordBinds }
858 : fbinds ',' fbind { $3 : $1 }
863 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
864 : qvar '=' exp { ($1,$3,False) }
866 -----------------------------------------------------------------------------
867 -- Implicit Parameter Bindings
869 dbinding :: { [(RdrName, RdrNameHsExpr)] }
870 : '{' dbinds '}' { $2 }
871 | layout_on dbinds close { $2 }
873 dbinds :: { [(RdrName, RdrNameHsExpr)] }
874 : dbinds ';' dbind { $3 : $1 }
879 dbind :: { (RdrName, RdrNameHsExpr) }
880 dbind : ipvar '=' exp { ($1, $3) }
882 -----------------------------------------------------------------------------
883 -- Variables, Constructors and Operators.
885 depreclist :: { [RdrName] }
886 depreclist : deprec_var { [$1] }
887 | deprec_var ',' depreclist { $1 : $3 }
889 deprec_var :: { RdrName }
890 deprec_var : var { $1 }
893 gtycon :: { RdrName }
895 | '(' qtyconop ')' { $2 }
896 | '(' ')' { unitTyCon_RDR }
897 | '(' '->' ')' { funTyCon_RDR }
898 | '[' ']' { listTyCon_RDR }
899 | '(' commas ')' { tupleTyCon_RDR $2 }
902 : '(' ')' { unitCon_RDR }
903 | '[' ']' { nilCon_RDR }
904 | '(' commas ')' { tupleCon_RDR $2 }
909 | '(' varsym ')' { $2 }
913 | '(' varsym ')' { $2 }
914 | '(' qvarsym1 ')' { $2 }
915 -- We've inlined qvarsym here so that the decision about
916 -- whether it's a qvar or a var can be postponed until
917 -- *after* we see the close paren.
920 : IPVARID { (mkUnqual ipName (tailFS $1)) }
924 | '(' qconsym ')' { $2 }
928 | '`' varid '`' { $2 }
930 qvarop :: { RdrName }
932 | '`' qvarid '`' { $2 }
934 qvaropm :: { RdrName }
935 : qvarsym_no_minus { $1 }
936 | '`' qvarid '`' { $2 }
940 | '`' conid '`' { $2 }
942 qconop :: { RdrName }
944 | '`' qconid '`' { $2 }
946 -----------------------------------------------------------------------------
949 op :: { RdrName } -- used in infix decls
953 qop :: { RdrName {-HsExpr-} } -- used in sections
957 qopm :: { RdrNameHsExpr } -- used in sections
958 : qvaropm { HsVar $1 }
959 | qconop { HsVar $1 }
961 -----------------------------------------------------------------------------
964 qvarid :: { RdrName }
966 | QVARID { mkQual varName $1 }
969 : varid_no_unsafe { $1 }
970 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
972 varid_no_unsafe :: { RdrName }
973 : VARID { mkUnqual varName $1 }
974 | special_id { mkUnqual varName $1 }
975 | 'forall' { mkUnqual varName SLIT("forall") }
978 : VARID { mkUnqual tvName $1 }
979 | special_id { mkUnqual tvName $1 }
980 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
982 -- These special_ids are treated as keywords in various places,
983 -- but as ordinary ids elsewhere. A special_id collects all thsee
984 -- except 'unsafe' and 'forall' whose treatment differs depending on context
985 special_id :: { UserFS }
987 : 'as' { SLIT("as") }
988 | 'qualified' { SLIT("qualified") }
989 | 'hiding' { SLIT("hiding") }
990 | 'export' { SLIT("export") }
991 | 'label' { SLIT("label") }
992 | 'dynamic' { SLIT("dynamic") }
993 | 'stdcall' { SLIT("stdcall") }
994 | 'ccall' { SLIT("ccall") }
996 -----------------------------------------------------------------------------
999 qconid :: { RdrName }
1001 | QCONID { mkQual dataName $1 }
1003 conid :: { RdrName }
1004 : CONID { mkUnqual dataName $1 }
1006 -----------------------------------------------------------------------------
1009 qconsym :: { RdrName }
1011 | QCONSYM { mkQual dataName $1 }
1013 consym :: { RdrName }
1014 : CONSYM { mkUnqual dataName $1 }
1016 -----------------------------------------------------------------------------
1019 qvarsym :: { RdrName }
1023 qvarsym_no_minus :: { RdrName }
1024 : varsym_no_minus { $1 }
1027 qvarsym1 :: { RdrName }
1028 qvarsym1 : QVARSYM { mkQual varName $1 }
1030 varsym :: { RdrName }
1031 : varsym_no_minus { $1 }
1032 | '-' { mkUnqual varName SLIT("-") }
1034 varsym_no_minus :: { RdrName } -- varsym not including '-'
1035 : VARSYM { mkUnqual varName $1 }
1036 | special_sym { mkUnqual varName $1 }
1039 -- See comments with special_id
1040 special_sym :: { UserFS }
1041 special_sym : '!' { SLIT("!") }
1044 -----------------------------------------------------------------------------
1047 literal :: { HsLit }
1048 : CHAR { HsChar $1 }
1049 | STRING { HsString $1 }
1050 | PRIMINTEGER { HsIntPrim $1 }
1051 | PRIMCHAR { HsCharPrim $1 }
1052 | PRIMSTRING { HsStringPrim $1 }
1053 | PRIMFLOAT { HsFloatPrim $1 }
1054 | PRIMDOUBLE { HsDoublePrim $1 }
1055 | CLITLIT { HsLitLit $1 (error "Parser.y: CLITLIT") }
1057 srcloc :: { SrcLoc } : {% getSrcLocP }
1059 -----------------------------------------------------------------------------
1063 : vccurly { () } -- context popped in lexer.
1064 | error {% popContext }
1066 layout_on :: { () } : {% layoutOn True{-strict-} }
1067 layout_on_for_do :: { () } : {% layoutOn False }
1069 -----------------------------------------------------------------------------
1070 -- Miscellaneous (mostly renamings)
1072 modid :: { ModuleName }
1073 : CONID { mkModuleNameFS $1 }
1075 tycon :: { RdrName }
1076 : CONID { mkUnqual tcClsName $1 }
1078 tyconop :: { RdrName }
1079 : CONSYM { mkUnqual tcClsName $1 }
1081 qtycon :: { RdrName }
1083 | QCONID { mkQual tcClsName $1 }
1085 qtyconop :: { RdrName }
1087 | QCONSYM { mkQual tcClsName $1 }
1089 qtycls :: { RdrName }
1093 : commas ',' { $1 + 1 }
1096 -----------------------------------------------------------------------------
1100 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)