2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.35 2000/09/14 13:46:40 simonpj Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parse ) where
16 import HsTypes ( mkHsTupCon )
22 import PrelInfo ( mAIN_Name )
23 import OccName ( 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 )
34 #include "HsVersions.h"
38 -----------------------------------------------------------------------------
39 Conflicts: 14 shift/reduce
40 (note: it's currently 21 -- JRL, 31/1/2000)
42 8 for abiguity in 'if x then y else z + 1'
43 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
44 1 for ambiguity in 'if x then y else z :: T'
45 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
46 3 for ambiguity in 'case x of y :: a -> b'
47 (don't know whether to reduce 'a' as a btype or shift the '->'.
48 conclusion: bogus expression anyway, doesn't matter)
50 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
51 since 'forall' is a valid variable name, we don't know whether
52 to treat a forall on the input as the beginning of a quantifier
53 or the beginning of the rule itself. Resolving to shift means
54 it's always treated as a quantifier, hence the above is disallowed.
55 This saves explicitly defining a grammar for the rule lhs that
56 doesn't include 'forall'.
58 1 for ambiguity in 'x @ Rec{..}'.
59 Only sensible parse is 'x @ (Rec{..})', which is what resolving
62 -----------------------------------------------------------------------------
66 '_' { ITunderscore } -- Haskell keywords
71 'default' { ITdefault }
72 'deriving' { ITderiving }
82 'instance' { ITinstance }
85 'newtype' { ITnewtype }
87 'qualified' { ITqualified }
93 'forall' { ITforall } -- GHC extension keywords
94 'foreign' { ITforeign }
97 'dynamic' { ITdynamic }
100 'stdcall' { ITstdcallconv }
101 'ccall' { ITccallconv }
102 '_ccall_' { ITccall (False, False, False) }
103 '_ccall_GC_' { ITccall (False, False, True) }
104 '_casm_' { ITccall (False, True, False) }
105 '_casm_GC_' { ITccall (False, True, True) }
107 '{-# SPECIALISE' { ITspecialise_prag }
108 '{-# SOURCE' { ITsource_prag }
109 '{-# INLINE' { ITinline_prag }
110 '{-# NOINLINE' { ITnoinline_prag }
111 '{-# RULES' { ITrules_prag }
112 '{-# DEPRECATED' { ITdeprecated_prag }
113 '#-}' { ITclose_prag }
116 '__interface' { ITinterface } -- interface keywords
117 '__export' { IT__export }
118 '__instimport' { ITinstimport }
119 '__forall' { IT__forall }
120 '__letrec' { ITletrec }
121 '__coerce' { ITcoerce }
122 '__depends' { ITdepends }
123 '__inline' { ITinline }
124 '__DEFAULT' { ITdefaultbranch }
126 '__integer' { ITinteger_lit }
127 '__float' { ITfloat_lit }
128 '__rational' { ITrational_lit }
129 '__addr' { ITaddr_lit }
130 '__label' { ITlabel_lit }
131 '__litlit' { ITlit_lit }
132 '__string' { ITstring_lit }
133 '__ccall' { ITccall $$ }
135 '__sccC' { ITsccAllCafs }
138 '__P' { ITspecialise }
140 '__U' { ITunfold $$ }
141 '__S' { ITstrict $$ }
142 '__M' { ITcprinfo $$ }
145 '..' { ITdotdot } -- reserved symbols
159 '/\\' { ITbiglam } -- GHC-extension symbols
161 '{' { ITocurly } -- special symbols
163 vccurly { ITvccurly } -- virtual close curly (from layout)
174 VARID { ITvarid $$ } -- identifiers
176 VARSYM { ITvarsym $$ }
177 CONSYM { ITconsym $$ }
178 QVARID { ITqvarid $$ }
179 QCONID { ITqconid $$ }
180 QVARSYM { ITqvarsym $$ }
181 QCONSYM { ITqconsym $$ }
183 IPVARID { ITipvarid $$ } -- GHC extension
185 PRAGMA { ITpragma $$ }
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 UNKNOWN { ITunknown $$ }
201 %monad { P } { thenP } { returnP }
202 %lexer { lexer } { ITeof }
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 (mkSrcModuleFS $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 '=' sigtype
332 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
334 | srcloc 'data' ctype '=' constrs deriving
335 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
336 returnP (RdrHsDecl (TyClD
337 (TyData DataType cs c ts (reverse $5) (length $5) $6
338 NoDataPragmas $1))) }
340 | srcloc 'newtype' ctype '=' newconstr deriving
341 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
342 returnP (RdrHsDecl (TyClD
343 (TyData NewType cs c ts [$5] 1 $6
344 NoDataPragmas $1))) }
346 | srcloc 'class' ctype fds where
347 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
349 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
351 returnP (RdrHsDecl (TyClD
352 (mkClassDecl cs c ts $4 sigs binds
353 NoClassPragmas $1))) }
355 | srcloc 'instance' inst_type where
357 = cvMonoBindsAndSigs cvInstDeclSig
359 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
361 | srcloc 'default' '(' types0 ')'
362 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
364 | srcloc 'foreign' 'import' callconv ext_name
365 unsafe_flag varid_no_unsafe '::' sigtype
366 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
368 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
369 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
371 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
372 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
373 defaultCallConv $1)) }
375 | '{-# DEPRECATED' deprecations '#-}' { $2 }
376 | '{-# RULES' rules '#-}' { $2 }
379 decls :: { [RdrBinding] }
380 : decls ';' decl { $3 : $1 }
385 decl :: { RdrBinding }
388 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
389 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
390 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
391 { foldr1 RdrAndBindings
392 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
393 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
394 { RdrSig (SpecInstSig $4 $2) }
396 opt_phase :: { Maybe Int }
397 : INTEGER { Just (fromInteger $1) }
398 | {- empty -} { Nothing }
400 wherebinds :: { RdrNameHsBinds }
401 : where { cvBinds cvValSig (groupBindings $1) }
403 where :: { [RdrBinding] }
404 : 'where' decllist { $2 }
407 declbinds :: { RdrNameHsBinds }
408 : decllist { cvBinds cvValSig (groupBindings $1) }
410 decllist :: { [RdrBinding] }
411 : '{' decls '}' { $2 }
412 | layout_on decls close { $2 }
414 fixdecl :: { RdrBinding }
415 : srcloc infix prec ops { foldr1 RdrAndBindings
416 [ RdrSig (FixSig (FixitySig n
420 -----------------------------------------------------------------------------
421 -- Transformation Rules
423 rules :: { RdrBinding }
424 : rules ';' rule { $1 `RdrAndBindings` $3 }
427 | {- empty -} { RdrNullBind }
429 rule :: { RdrBinding }
430 : STRING rule_forall fexp '=' srcloc exp
431 { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
433 rule_forall :: { [RdrNameRuleBndr] }
434 : 'forall' rule_var_list '.' { $2 }
437 rule_var_list :: { [RdrNameRuleBndr] }
439 | rule_var rule_var_list { $1 : $2 }
441 rule_var :: { RdrNameRuleBndr }
442 : varid { RuleBndr $1 }
443 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
445 -----------------------------------------------------------------------------
448 deprecations :: { RdrBinding }
449 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
450 | deprecations ';' { $1 }
452 | {- empty -} { RdrNullBind }
454 -- SUP: TEMPORARY HACK, not checking for `module Foo'
455 deprecation :: { RdrBinding }
456 : srcloc exportlist STRING
457 { foldr RdrAndBindings RdrNullBind
458 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
460 -----------------------------------------------------------------------------
461 -- Foreign import/export
464 : 'stdcall' { stdCallConv }
465 | 'ccall' { cCallConv }
466 | {- empty -} { defaultCallConv }
468 unsafe_flag :: { Bool }
470 | {- empty -} { False }
472 ext_name :: { Maybe ExtName }
473 : 'dynamic' { Just Dynamic }
474 | STRING { Just (ExtName $1 Nothing) }
475 | STRING STRING { Just (ExtName $2 (Just $1)) }
476 | {- empty -} { Nothing }
479 -----------------------------------------------------------------------------
482 opt_sig :: { Maybe RdrNameHsType }
483 : {- empty -} { Nothing }
484 | '::' sigtype { Just $2 }
486 opt_asig :: { Maybe RdrNameHsType }
487 : {- empty -} { Nothing }
488 | '::' atype { Just $2 }
490 sigtypes :: { [RdrNameHsType] }
492 | sigtypes ',' sigtype { $3 : $1 }
494 sigtype :: { RdrNameHsType }
495 : ctype { mkHsForAllTy Nothing [] $1 }
497 sig_vars :: { [RdrName] }
498 : sig_vars ',' var { $3 : $1 }
501 -----------------------------------------------------------------------------
504 -- A ctype is a for-all type
505 ctype :: { RdrNameHsType }
506 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
507 | context type { mkHsForAllTy Nothing $1 $2 }
508 -- A type of form (context => type) is an *implicit* HsForAllTy
511 type :: { RdrNameHsType }
512 : btype '->' type { HsFunTy $1 $3 }
513 | ipvar '::' type { mkHsIParamTy $1 $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 -- An inst_type is what occurs in the head of an instance decl
529 -- e.g. (Foo a, Gaz b) => Wibble a b
530 -- It's kept as a single type, with a MonoDictTy at the right
531 -- hand corner, for convenience.
532 inst_type :: { RdrNameHsType }
533 : ctype {% checkInstType $1 }
535 types0 :: { [RdrNameHsType] }
539 types :: { [RdrNameHsType] }
541 | types ',' type { $3 : $1 }
543 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
544 : tycon tyvars { ($1, reverse $2) }
546 tyvars :: { [RdrNameHsTyVar] }
547 : tyvars tyvar { UserTyVar $2 : $1 }
550 fds :: { [([RdrName], [RdrName])] }
552 | '|' fds1 { reverse $2 }
554 fds1 :: { [([RdrName], [RdrName])] }
555 : fds1 ',' fd { $3 : $1 }
558 fd :: { ([RdrName], [RdrName]) }
559 : varids0 '->' varids0 { (reverse $1, reverse $3) }
561 varids0 :: { [RdrName] }
563 | varids0 tyvar { $2 : $1 }
565 -----------------------------------------------------------------------------
566 -- Datatype declarations
568 constrs :: { [RdrNameConDecl] }
569 : constrs '|' constr { $3 : $1 }
572 constr :: { RdrNameConDecl }
573 : srcloc forall context constr_stuff
574 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
575 | srcloc forall constr_stuff
576 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
578 forall :: { [RdrNameHsTyVar] }
579 : 'forall' tyvars '.' { $2 }
582 context :: { RdrNameContext }
583 : btype '=>' {% checkContext $1 }
585 constr_stuff :: { (RdrName, RdrNameConDetails) }
586 : scontype { (fst $1, VanillaCon (snd $1)) }
587 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
588 | con '{' fielddecls '}' { ($1, RecCon (reverse $3)) }
590 newconstr :: { RdrNameConDecl }
591 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [Unbanged $3]) $1 }
592 | srcloc conid '{' var '::' type '}'
593 { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $1 }
595 scontype :: { (RdrName, [RdrNameBangType]) }
596 : btype {% splitForConApp $1 [] }
599 scontype1 :: { (RdrName, [RdrNameBangType]) }
600 : btype '!' atype {% splitForConApp $1 [Banged $3] }
601 | scontype1 satype { (fst $1, snd $1 ++ [$2] ) }
602 | '(' consym ')' { ($2,[]) }
604 satype :: { RdrNameBangType }
605 : atype { Unbanged $1 }
606 | '!' atype { Banged $2 }
608 sbtype :: { RdrNameBangType }
609 : btype { Unbanged $1 }
610 | '!' atype { Banged $2 }
612 fielddecls :: { [([RdrName],RdrNameBangType)] }
613 : fielddecls ',' fielddecl { $3 : $1 }
616 fielddecl :: { ([RdrName],RdrNameBangType) }
617 : sig_vars '::' stype { (reverse $1, $3) }
619 stype :: { RdrNameBangType }
620 : ctype { Unbanged $1 }
621 | '!' atype { Banged $2 }
623 deriving :: { Maybe [RdrName] }
624 : {- empty -} { Nothing }
625 | 'deriving' qtycls { Just [$2] }
626 | 'deriving' '(' ')' { Just [] }
627 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
629 dclasses :: { [RdrName] }
630 : dclasses ',' qtycls { $3 : $1 }
633 -----------------------------------------------------------------------------
636 {- There's an awkward overlap with a type signature. Consider
637 f :: Int -> Int = ...rhs...
638 Then we can't tell whether it's a type signature or a value
639 definition with a result signature until we see the '='.
640 So we have to inline enough to postpone reductions until we know.
644 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
645 instead of qvar, we get another shift/reduce-conflict. Consider the
648 { (^^) :: Int->Int ; } Type signature; only var allowed
650 { (^^) :: Int->Int = ... ; } Value defn with result signature;
651 qvar allowed (because of instance decls)
653 We can't tell whether to reduce var to qvar until after we've read the signatures.
656 valdef :: { RdrBinding }
657 : infixexp srcloc opt_sig rhs {% checkValDef $1 $3 $4 $2 }
658 | infixexp srcloc '::' sigtype {% checkValSig $1 $4 $2 }
659 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
660 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
663 rhs :: { RdrNameGRHSs }
664 : '=' srcloc exp wherebinds { GRHSs (unguardedRHS $3 $2)
666 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
668 gdrhs :: { [RdrNameGRHS] }
669 : gdrhs gdrh { $2 : $1 }
672 gdrh :: { RdrNameGRHS }
673 : '|' srcloc quals '=' exp { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
675 -----------------------------------------------------------------------------
678 exp :: { RdrNameHsExpr }
679 : infixexp '::' sigtype { ExprWithTySig $1 $3 }
680 | infixexp 'with' dbinding { HsWith $1 $3 }
683 infixexp :: { RdrNameHsExpr }
685 | infixexp qop exp10 { OpApp $1 $2 (panic "fixity") $3 }
687 exp10 :: { RdrNameHsExpr }
688 : '\\' aexp aexps opt_asig '->' srcloc exp
689 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
690 returnP (HsLam (Match [] ps $4
691 (GRHSs (unguardedRHS $7 $6)
692 EmptyBinds Nothing))) }
693 | 'let' declbinds 'in' exp { HsLet $2 $4 }
694 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
695 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
696 | '-' fexp { NegApp $2 (error "NegApp") }
697 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
699 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 False False cbot }
700 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 True False cbot }
701 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 False True cbot }
702 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 True True cbot }
704 | '_scc_' STRING exp { if opt_SccProfilingOn
710 ccallid :: { FAST_STRING }
714 fexp :: { RdrNameHsExpr }
715 : fexp aexp { HsApp $1 $2 }
718 aexps0 :: { [RdrNameHsExpr] }
719 : aexps { reverse $1 }
721 aexps :: { [RdrNameHsExpr] }
722 : aexps aexp { $2 : $1 }
725 aexp :: { RdrNameHsExpr }
726 : aexp '{' fbinds '}' {% mkRecConstrOrUpdate $1 (reverse $3) }
729 aexp1 :: { RdrNameHsExpr }
731 | ipvar { HsIPVar $1 }
733 | literal { HsLit $1 }
734 | '(' exp ')' { HsPar $2 }
735 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
736 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
737 | '[' list ']' { $2 }
738 | '(' infixexp qop ')' { SectionL $2 $3 }
739 | '(' qopm infixexp ')' { SectionR $2 $3 }
740 | qvar '@' aexp { EAsPat $1 $3 }
742 | '~' aexp1 { ELazyPat $2 }
744 texps :: { [RdrNameHsExpr] }
745 : texps ',' exp { $3 : $1 }
748 -----------------------------------------------------------------------------
751 -- The rules below are little bit contorted to keep lexps left-recursive while
752 -- avoiding another shift/reduce-conflict.
754 list :: { RdrNameHsExpr }
755 : exp { ExplicitList [$1] }
756 | lexps { ExplicitList (reverse $1) }
757 | exp '..' { ArithSeqIn (From $1) }
758 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
759 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
760 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
761 | exp srcloc '|' quals { HsDo ListComp (reverse
762 (ReturnStmt $1 : $4)) $2 }
764 lexps :: { [RdrNameHsExpr] }
765 : lexps ',' exp { $3 : $1 }
766 | exp ',' exp { [$3,$1] }
768 -----------------------------------------------------------------------------
769 -- List Comprehensions
771 quals :: { [RdrNameStmt] }
772 : quals ',' qual { $3 : $1 }
775 qual :: { RdrNameStmt }
776 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
777 returnP (BindStmt p $4 $1) }
778 | srcloc exp { GuardStmt $2 $1 }
779 | srcloc 'let' declbinds { LetStmt $3 }
781 -----------------------------------------------------------------------------
784 altslist :: { [RdrNameMatch] }
785 : '{' alts '}' { reverse $2 }
786 | layout_on alts close { reverse $2 }
788 alts :: { [RdrNameMatch] }
792 alts1 :: { [RdrNameMatch] }
793 : alts1 ';' alt { $3 : $1 }
797 alt :: { RdrNameMatch }
798 : infixexp opt_sig ralt wherebinds
799 {% checkPattern $1 `thenP` \p ->
800 returnP (Match [] [p] $2
801 (GRHSs $3 $4 Nothing)) }
803 ralt :: { [RdrNameGRHS] }
804 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
805 | gdpats { (reverse $1) }
807 gdpats :: { [RdrNameGRHS] }
808 : gdpats gdpat { $2 : $1 }
811 gdpat :: { RdrNameGRHS }
812 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
814 -----------------------------------------------------------------------------
815 -- Statement sequences
817 stmtlist :: { [RdrNameStmt] }
818 : '{' stmts '}' { reverse $2 }
819 | layout_on_for_do stmts close { reverse $2 }
821 -- Stmt list should really end in an expression, but it's not
822 -- convenient to enforce this here, so we throw out erroneous
823 -- statement sequences in the renamer instead.
825 stmts :: { [RdrNameStmt] }
829 stmts1 :: { [RdrNameStmt] }
830 : stmts1 ';' stmt { $3 : $1 }
834 stmt :: { RdrNameStmt }
835 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
836 returnP (BindStmt p $4 $1) }
837 | srcloc exp { ExprStmt $2 $1 }
838 | srcloc 'let' declbinds { LetStmt $3 }
840 -----------------------------------------------------------------------------
841 -- Record Field Update/Construction
843 fbinds :: { RdrNameHsRecordBinds }
844 : fbinds ',' fbind { $3 : $1 }
849 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
850 : qvar '=' exp { ($1,$3,False) }
852 -----------------------------------------------------------------------------
853 -- Implicit Parameter Bindings
855 dbinding :: { [(RdrName, RdrNameHsExpr)] }
856 : '{' dbinds '}' { $2 }
857 | layout_on dbinds close { $2 }
859 dbinds :: { [(RdrName, RdrNameHsExpr)] }
860 : dbinds ';' dbind { $3 : $1 }
865 dbind :: { (RdrName, RdrNameHsExpr) }
866 dbind : ipvar '=' exp { ($1, $3) }
868 -----------------------------------------------------------------------------
869 -- Variables, Constructors and Operators.
871 gtycon :: { RdrName }
873 | '(' ')' { unitTyCon_RDR }
874 | '(' '->' ')' { funTyCon_RDR }
875 | '[' ']' { listTyCon_RDR }
876 | '(' commas ')' { tupleTyCon_RDR $2 }
879 : '(' ')' { unitCon_RDR }
880 | '[' ']' { nilCon_RDR }
881 | '(' commas ')' { tupleCon_RDR $2 }
886 | '(' varsym ')' { $2 }
890 | '(' varsym ')' { $2 }
891 | '(' qvarsym1 ')' { $2 }
892 -- We've inlined qvarsym here so that the decision about
893 -- whether it's a qvar or a var can be postponed until
894 -- *after* we see the close paren.
897 : IPVARID { (mkSrcUnqual ipName (tailFS $1)) }
901 | '(' consym ')' { $2 }
905 | '(' qconsym ')' { $2 }
909 | '`' varid '`' { $2 }
911 qvarop :: { RdrName }
913 | '`' qvarid '`' { $2 }
915 qvaropm :: { RdrName }
917 | '`' qvarid '`' { $2 }
921 | '`' conid '`' { $2 }
923 qconop :: { RdrName }
925 | '`' qconid '`' { $2 }
927 -----------------------------------------------------------------------------
930 op :: { RdrName } -- used in infix decls
934 qop :: { RdrNameHsExpr } -- used in sections
935 : qvarop { HsVar $1 }
936 | qconop { HsVar $1 }
938 qopm :: { RdrNameHsExpr } -- used in sections
939 : qvaropm { HsVar $1 }
940 | qconop { HsVar $1 }
942 -----------------------------------------------------------------------------
945 qvarid :: { RdrName }
947 | QVARID { case $1 of { (mod,n) ->
948 mkSrcQual varName mod n } }
951 : VARID { mkSrcUnqual varName $1 }
952 | 'as' { as_var_RDR }
953 | 'qualified' { qualified_var_RDR }
954 | 'hiding' { hiding_var_RDR }
955 | 'forall' { forall_var_RDR }
956 | 'export' { export_var_RDR }
957 | 'label' { label_var_RDR }
958 | 'dynamic' { dynamic_var_RDR }
959 | 'unsafe' { unsafe_var_RDR }
960 | 'stdcall' { stdcall_var_RDR }
961 | 'ccall' { ccall_var_RDR }
963 varid_no_unsafe :: { RdrName }
964 : VARID { mkSrcUnqual varName $1 }
965 | 'as' { as_var_RDR }
966 | 'qualified' { qualified_var_RDR }
967 | 'hiding' { hiding_var_RDR }
968 | 'forall' { forall_var_RDR }
969 | 'export' { export_var_RDR }
970 | 'label' { label_var_RDR }
971 | 'dynamic' { dynamic_var_RDR }
972 | 'stdcall' { stdcall_var_RDR }
973 | 'ccall' { ccall_var_RDR }
975 -----------------------------------------------------------------------------
978 qconid :: { RdrName }
980 | QCONID { case $1 of { (mod,n) ->
981 mkSrcQual dataName mod n } }
984 : CONID { mkSrcUnqual dataName $1 }
986 -----------------------------------------------------------------------------
989 qconsym :: { RdrName }
991 | QCONSYM { case $1 of { (mod,n) ->
992 mkSrcQual dataName mod n } }
994 consym :: { RdrName }
995 : CONSYM { mkSrcUnqual dataName $1 }
997 -----------------------------------------------------------------------------
1000 qvarsym :: { RdrName }
1004 qvarsymm :: { RdrName }
1008 varsym :: { RdrName }
1009 : VARSYM { mkSrcUnqual varName $1 }
1014 varsymm :: { RdrName } -- varsym not including '-'
1015 : VARSYM { mkSrcUnqual varName $1 }
1019 qvarsym1 :: { RdrName }
1020 : QVARSYM { case $1 of { (mod,n) ->
1021 mkSrcQual varName mod n } }
1023 literal :: { HsLit }
1024 : INTEGER { HsInt $1 }
1025 | CHAR { HsChar $1 }
1026 | RATIONAL { HsFrac $1 }
1027 | STRING { HsString $1 }
1029 | PRIMINTEGER { HsIntPrim $1 }
1030 | PRIMCHAR { HsCharPrim $1 }
1031 | PRIMSTRING { HsStringPrim $1 }
1032 | PRIMFLOAT { HsFloatPrim $1 }
1033 | PRIMDOUBLE { HsDoublePrim $1 }
1034 | CLITLIT { HsLitLit $1 }
1036 srcloc :: { SrcLoc } : {% getSrcLocP }
1038 -----------------------------------------------------------------------------
1042 : vccurly { () } -- context popped in lexer.
1043 | error {% popContext }
1045 layout_on :: { () } : {% layoutOn True{-strict-} }
1046 layout_on_for_do :: { () } : {% layoutOn False }
1048 -----------------------------------------------------------------------------
1049 -- Miscellaneous (mostly renamings)
1051 modid :: { ModuleName }
1052 : CONID { mkSrcModuleFS $1 }
1054 tycon :: { RdrName }
1055 : CONID { mkSrcUnqual tcClsName $1 }
1057 qtycon :: { RdrName }
1059 | QCONID { case $1 of { (mod,n) ->
1060 mkSrcQual tcClsName mod n } }
1062 qtycls :: { RdrName }
1065 tyvar :: { RdrName }
1066 : VARID { mkSrcUnqual tvName $1 }
1067 | 'as' { as_tyvar_RDR }
1068 | 'qualified' { qualified_tyvar_RDR }
1069 | 'hiding' { hiding_tyvar_RDR }
1070 | 'export' { export_tyvar_RDR }
1071 | 'label' { label_tyvar_RDR }
1072 | 'dynamic' { dynamic_tyvar_RDR }
1073 | 'unsafe' { unsafe_tyvar_RDR }
1074 | 'stdcall' { stdcall_tyvar_RDR }
1075 | 'ccall' { ccall_tyvar_RDR }
1076 -- NOTE: no 'forall'
1079 : commas ',' { $1 + 1 }
1082 -----------------------------------------------------------------------------
1086 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)