2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.32 2000/06/01 08:51:46 simonmar 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 '__litlit' { ITlit_lit }
131 '__string' { ITstring_lit }
132 '__ccall' { ITccall $$ }
134 '__sccC' { ITsccAllCafs }
137 '__P' { ITspecialise }
139 '__U' { ITunfold $$ }
140 '__S' { ITstrict $$ }
141 '__M' { ITcprinfo $$ }
144 '..' { ITdotdot } -- reserved symbols
158 '/\\' { ITbiglam } -- GHC-extension symbols
160 '{' { ITocurly } -- special symbols
162 vccurly { ITvccurly } -- virtual close curly (from layout)
173 VARID { ITvarid $$ } -- identifiers
175 VARSYM { ITvarsym $$ }
176 CONSYM { ITconsym $$ }
177 QVARID { ITqvarid $$ }
178 QCONID { ITqconid $$ }
179 QVARSYM { ITqvarsym $$ }
180 QCONSYM { ITqconsym $$ }
182 IPVARID { ITipvarid $$ } -- GHC extension
184 PRAGMA { ITpragma $$ }
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 UNKNOWN { ITunknown $$ }
200 %monad { P } { thenP } { returnP }
201 %lexer { lexer } { ITeof }
206 -----------------------------------------------------------------------------
209 -- The place for module deprecation is really too restrictive, but if it
210 -- was allowed at its natural place just before 'module', we get an ugly
211 -- s/r conflict with the second alternative. Another solution would be the
212 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
213 -- either, and DEPRECATED is only expected to be used by people who really
214 -- know what they are doing. :-)
216 module :: { RdrNameHsModule }
217 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
218 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
220 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
222 maybemoddeprec :: { Maybe DeprecTxt }
223 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
224 | {- empty -} { Nothing }
226 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
228 | layout_on top close { $2 }
230 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
231 : importdecls ';' cvtopdecls { (reverse $1,$3) }
232 | importdecls { (reverse $1,[]) }
233 | cvtopdecls { ([],$1) }
235 cvtopdecls :: { [RdrNameHsDecl] }
236 : topdecls { cvTopDecls (groupBindings $1)}
238 -----------------------------------------------------------------------------
241 maybeexports :: { Maybe [RdrNameIE] }
242 : '(' exportlist ')' { Just $2 }
243 | {- empty -} { Nothing }
245 exportlist :: { [RdrNameIE] }
246 : exportlist ',' export { $3 : $1 }
247 | exportlist ',' { $1 }
251 -- GHC extension: we allow things like [] and (,,,) to be exported
252 export :: { RdrNameIE }
254 | gtycon { IEThingAbs $1 }
255 | gtycon '(' '..' ')' { IEThingAll $1 }
256 | gtycon '(' ')' { IEThingWith $1 [] }
257 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
258 | 'module' modid { IEModuleContents $2 }
260 qcnames :: { [RdrName] }
261 : qcnames ',' qcname { $3 : $1 }
264 qcname :: { RdrName }
268 -----------------------------------------------------------------------------
269 -- Import Declarations
271 -- import decls can be *empty*, or even just a string of semicolons
272 -- whereas topdecls must contain at least one topdecl.
274 importdecls :: { [RdrNameImportDecl] }
275 : importdecls ';' importdecl { $3 : $1 }
276 | importdecls ';' { $1 }
277 | importdecl { [ $1 ] }
280 importdecl :: { RdrNameImportDecl }
281 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
282 { ImportDecl (mkSrcModuleFS $5) $3 $4 $6 $7 $2 }
284 maybe_src :: { WhereFrom }
285 : '{-# SOURCE' '#-}' { ImportByUserSource }
286 | {- empty -} { ImportByUser }
288 optqualified :: { Bool }
289 : 'qualified' { True }
290 | {- empty -} { False }
292 maybeas :: { Maybe ModuleName }
293 : 'as' modid { Just $2 }
294 | {- empty -} { Nothing }
296 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
297 : impspec { Just $1 }
298 | {- empty -} { Nothing }
300 impspec :: { (Bool, [RdrNameIE]) }
301 : '(' exportlist ')' { (False, reverse $2) }
302 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
304 -----------------------------------------------------------------------------
305 -- Fixity Declarations
309 | INTEGER {% checkPrec $1 `thenP_`
310 returnP (fromInteger $1) }
312 infix :: { FixityDirection }
314 | 'infixl' { InfixL }
315 | 'infixr' { InfixR }
318 : ops ',' op { $3 : $1 }
321 -----------------------------------------------------------------------------
322 -- Top-Level Declarations
324 topdecls :: { [RdrBinding] }
325 : topdecls ';' topdecl { ($3 : $1) }
326 | topdecls ';' { $1 }
329 topdecl :: { RdrBinding }
330 : srcloc 'type' simpletype '=' sigtype
331 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
333 | srcloc 'data' ctype '=' constrs deriving
334 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
335 returnP (RdrHsDecl (TyClD
336 (TyData DataType cs c ts (reverse $5) (length $5) $6
337 NoDataPragmas $1))) }
339 | srcloc 'newtype' ctype '=' newconstr deriving
340 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
341 returnP (RdrHsDecl (TyClD
342 (TyData NewType cs c ts [$5] 1 $6
343 NoDataPragmas $1))) }
345 | srcloc 'class' ctype fds where
346 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
348 = cvMonoBindsAndSigs cvClassOpSig
351 returnP (RdrHsDecl (TyClD
352 (mkClassDecl cs c ts $4 sigs binds
353 NoClassPragmas $1))) }
355 | srcloc 'instance' inst_type where
357 = cvMonoBindsAndSigs cvInstDeclSig
360 (InstDecl $3 binds sigs dummyRdrVarName $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 exportlist 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 : btype '->' type { HsFunTy $1 $3 }
514 | ipvar '::' type { mkHsIParamTy $1 $3 }
517 btype :: { RdrNameHsType }
518 : btype atype { HsAppTy $1 $2 }
521 atype :: { RdrNameHsType }
522 : gtycon { HsTyVar $1 }
523 | tyvar { HsTyVar $1 }
524 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
525 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
526 | '[' type ']' { HsListTy $2 }
527 | '(' ctype ')' { $2 }
529 gtycon :: { RdrName }
531 | '(' ')' { unitTyCon_RDR }
532 | '(' '->' ')' { funTyCon_RDR }
533 | '[' ']' { listTyCon_RDR }
534 | '(' commas ')' { tupleTyCon_RDR $2 }
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] }
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 constrs :: { [RdrNameConDecl] }
577 : constrs '|' constr { $3 : $1 }
580 constr :: { RdrNameConDecl }
581 : srcloc forall context constr_stuff
582 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
583 | srcloc forall constr_stuff
584 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
586 forall :: { [RdrNameHsTyVar] }
587 : 'forall' tyvars '.' { $2 }
590 context :: { RdrNameContext }
591 : btype '=>' {% checkContext $1 }
593 constr_stuff :: { (RdrName, RdrNameConDetails) }
594 : scontype { (fst $1, VanillaCon (snd $1)) }
595 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
596 | con '{' fielddecls '}' { ($1, RecCon (reverse $3)) }
598 newconstr :: { RdrNameConDecl }
599 : srcloc conid atype { mkConDecl $2 [] [] (NewCon $3 Nothing) $1 }
600 | srcloc conid '{' var '::' type '}'
601 { mkConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
603 scontype :: { (RdrName, [RdrNameBangType]) }
604 : btype {% splitForConApp $1 [] }
607 scontype1 :: { (RdrName, [RdrNameBangType]) }
608 : btype '!' atype {% splitForConApp $1 [Banged $3] }
609 | scontype1 satype { (fst $1, snd $1 ++ [$2] ) }
610 | '(' consym ')' { ($2,[]) }
612 satype :: { RdrNameBangType }
613 : atype { Unbanged $1 }
614 | '!' atype { Banged $2 }
616 sbtype :: { RdrNameBangType }
617 : btype { Unbanged $1 }
618 | '!' atype { Banged $2 }
620 fielddecls :: { [([RdrName],RdrNameBangType)] }
621 : fielddecls ',' fielddecl { $3 : $1 }
624 fielddecl :: { ([RdrName],RdrNameBangType) }
625 : sig_vars '::' stype { (reverse $1, $3) }
627 stype :: { RdrNameBangType }
628 : ctype { Unbanged $1 }
629 | '!' atype { Banged $2 }
631 deriving :: { Maybe [RdrName] }
632 : {- empty -} { Nothing }
633 | 'deriving' qtycls { Just [$2] }
634 | 'deriving' '(' ')' { Just [] }
635 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
637 dclasses :: { [RdrName] }
638 : dclasses ',' qtycls { $3 : $1 }
641 -----------------------------------------------------------------------------
644 {- There's an awkward overlap with a type signature. Consider
645 f :: Int -> Int = ...rhs...
646 Then we can't tell whether it's a type signature or a value
647 definition with a result signature until we see the '='.
648 So we have to inline enough to postpone reductions until we know.
652 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
653 instead of qvar, we get another shift/reduce-conflict. Consider the
656 { (^^) :: Int->Int ; } Type signature; only var allowed
658 { (^^) :: Int->Int = ... ; } Value defn with result signature;
659 qvar allowed (because of instance decls)
661 We can't tell whether to reduce var to qvar until after we've read the signatures.
664 valdef :: { RdrBinding }
665 : infixexp srcloc opt_sig rhs {% checkValDef $1 $3 $4 $2 }
666 | infixexp srcloc '::' sigtype {% checkValSig $1 $4 $2 }
667 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
668 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
671 rhs :: { RdrNameGRHSs }
672 : '=' srcloc exp wherebinds { GRHSs (unguardedRHS $3 $2)
674 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
676 gdrhs :: { [RdrNameGRHS] }
677 : gdrhs gdrh { $2 : $1 }
680 gdrh :: { RdrNameGRHS }
681 : '|' srcloc quals '=' exp { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
683 -----------------------------------------------------------------------------
686 exp :: { RdrNameHsExpr }
687 : infixexp '::' sigtype { ExprWithTySig $1 $3 }
688 | infixexp 'with' dbinding { HsWith $1 $3 }
691 infixexp :: { RdrNameHsExpr }
693 | infixexp qop exp10 { OpApp $1 $2 (panic "fixity") $3 }
695 exp10 :: { RdrNameHsExpr }
696 : '\\' aexp aexps opt_asig '->' srcloc exp
697 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
698 returnP (HsLam (Match [] ps $4
699 (GRHSs (unguardedRHS $7 $6)
700 EmptyBinds Nothing))) }
701 | 'let' declbinds 'in' exp { HsLet $2 $4 }
702 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
703 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
704 | '-' fexp { NegApp $2 (error "NegApp") }
705 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
707 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 False False cbot }
708 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 True False cbot }
709 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 False True cbot }
710 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 True True cbot }
712 | '_scc_' STRING exp { if opt_SccProfilingOn
718 ccallid :: { FAST_STRING }
722 fexp :: { RdrNameHsExpr }
723 : fexp aexp { HsApp $1 $2 }
726 aexps0 :: { [RdrNameHsExpr] }
727 : aexps { reverse $1 }
729 aexps :: { [RdrNameHsExpr] }
730 : aexps aexp { $2 : $1 }
733 aexp :: { RdrNameHsExpr }
734 : aexp '{' fbinds '}' {% mkRecConstrOrUpdate $1 (reverse $3) }
737 aexp1 :: { RdrNameHsExpr }
739 | ipvar { HsIPVar $1 }
741 | literal { HsLit $1 }
742 | '(' exp ')' { HsPar $2 }
743 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
744 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
745 | '[' list ']' { $2 }
746 | '(' infixexp qop ')' { SectionL $2 $3 }
747 | '(' qopm infixexp ')' { SectionR $2 $3 }
748 | qvar '@' aexp { EAsPat $1 $3 }
750 | '~' aexp1 { ELazyPat $2 }
753 : commas ',' { $1 + 1 }
756 texps :: { [RdrNameHsExpr] }
757 : texps ',' exp { $3 : $1 }
760 -----------------------------------------------------------------------------
763 -- The rules below are little bit contorted to keep lexps left-recursive while
764 -- avoiding another shift/reduce-conflict.
766 list :: { RdrNameHsExpr }
767 : exp { ExplicitList [$1] }
768 | lexps { ExplicitList (reverse $1) }
769 | exp '..' { ArithSeqIn (From $1) }
770 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
771 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
772 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
773 | exp srcloc '|' quals { HsDo ListComp (reverse
774 (ReturnStmt $1 : $4)) $2 }
776 lexps :: { [RdrNameHsExpr] }
777 : lexps ',' exp { $3 : $1 }
778 | exp ',' exp { [$3,$1] }
780 -----------------------------------------------------------------------------
781 -- List Comprehensions
783 quals :: { [RdrNameStmt] }
784 : quals ',' qual { $3 : $1 }
787 qual :: { RdrNameStmt }
788 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
789 returnP (BindStmt p $4 $1) }
790 | srcloc exp { GuardStmt $2 $1 }
791 | srcloc 'let' declbinds { LetStmt $3 }
793 -----------------------------------------------------------------------------
796 altslist :: { [RdrNameMatch] }
797 : '{' alts '}' { reverse $2 }
798 | layout_on alts close { reverse $2 }
800 alts :: { [RdrNameMatch] }
804 alts1 :: { [RdrNameMatch] }
805 : alts1 ';' alt { $3 : $1 }
809 alt :: { RdrNameMatch }
810 : infixexp opt_sig ralt wherebinds
811 {% checkPattern $1 `thenP` \p ->
812 returnP (Match [] [p] $2
813 (GRHSs $3 $4 Nothing)) }
815 ralt :: { [RdrNameGRHS] }
816 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
817 | gdpats { (reverse $1) }
819 gdpats :: { [RdrNameGRHS] }
820 : gdpats gdpat { $2 : $1 }
823 gdpat :: { RdrNameGRHS }
824 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
826 -----------------------------------------------------------------------------
827 -- Statement sequences
829 stmtlist :: { [RdrNameStmt] }
830 : '{' stmts '}' { reverse $2 }
831 | layout_on_for_do stmts close { reverse $2 }
833 -- Stmt list should really end in an expression, but it's not
834 -- convenient to enforce this here, so we throw out erroneous
835 -- statement sequences in the renamer instead.
837 stmts :: { [RdrNameStmt] }
841 stmts1 :: { [RdrNameStmt] }
842 : stmts1 ';' stmt { $3 : $1 }
846 stmt :: { RdrNameStmt }
847 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
848 returnP (BindStmt p $4 $1) }
849 | srcloc exp { ExprStmt $2 $1 }
850 | srcloc 'let' declbinds { LetStmt $3 }
852 -----------------------------------------------------------------------------
853 -- Record Field Update/Construction
855 fbinds :: { RdrNameHsRecordBinds }
856 : fbinds ',' fbind { $3 : $1 }
861 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
862 : qvar '=' exp { ($1,$3,False) }
864 -----------------------------------------------------------------------------
865 -- Implicit Parameter Bindings
867 dbinding :: { [(RdrName, RdrNameHsExpr)] }
868 : '{' dbinds '}' { $2 }
869 | layout_on dbinds close { $2 }
871 dbinds :: { [(RdrName, RdrNameHsExpr)] }
872 : dbinds ';' dbind { $3 : $1 }
877 dbind :: { (RdrName, RdrNameHsExpr) }
878 dbind : ipvar '=' exp { ($1, $3) }
880 -----------------------------------------------------------------------------
881 -- Variables, Constructors and Operators.
884 : '(' ')' { unitCon_RDR }
885 | '[' ']' { nilCon_RDR }
886 | '(' commas ')' { tupleCon_RDR $2 }
891 | '(' varsym ')' { $2 }
895 | '(' varsym ')' { $2 }
896 | '(' qvarsym1 ')' { $2 }
897 -- We've inlined qvarsym here so that the decision about
898 -- whether it's a qvar or a var can be postponed until
899 -- *after* we see the close paren.
902 : IPVARID { (mkSrcUnqual ipName (tailFS $1)) }
906 | '(' consym ')' { $2 }
910 | '(' qconsym ')' { $2 }
914 | '`' varid '`' { $2 }
916 qvarop :: { RdrName }
918 | '`' qvarid '`' { $2 }
920 qvaropm :: { RdrName }
922 | '`' qvarid '`' { $2 }
926 | '`' conid '`' { $2 }
928 qconop :: { RdrName }
930 | '`' qconid '`' { $2 }
932 -----------------------------------------------------------------------------
935 op :: { RdrName } -- used in infix decls
939 qop :: { RdrNameHsExpr } -- used in sections
940 : qvarop { HsVar $1 }
941 | qconop { HsVar $1 }
943 qopm :: { RdrNameHsExpr } -- used in sections
944 : qvaropm { HsVar $1 }
945 | qconop { HsVar $1 }
947 -----------------------------------------------------------------------------
950 qvarid :: { RdrName }
952 | QVARID { case $1 of { (mod,n) ->
953 mkSrcQual varName mod n } }
956 : VARID { mkSrcUnqual varName $1 }
957 | 'as' { as_var_RDR }
958 | 'qualified' { qualified_var_RDR }
959 | 'hiding' { hiding_var_RDR }
960 | 'forall' { forall_var_RDR }
961 | 'export' { export_var_RDR }
962 | 'label' { label_var_RDR }
963 | 'dynamic' { dynamic_var_RDR }
964 | 'unsafe' { unsafe_var_RDR }
965 | 'stdcall' { stdcall_var_RDR }
966 | 'ccall' { ccall_var_RDR }
968 varid_no_unsafe :: { RdrName }
969 : VARID { mkSrcUnqual varName $1 }
970 | 'as' { as_var_RDR }
971 | 'qualified' { qualified_var_RDR }
972 | 'hiding' { hiding_var_RDR }
973 | 'forall' { forall_var_RDR }
974 | 'export' { export_var_RDR }
975 | 'label' { label_var_RDR }
976 | 'dynamic' { dynamic_var_RDR }
977 | 'stdcall' { stdcall_var_RDR }
978 | 'ccall' { ccall_var_RDR }
980 -----------------------------------------------------------------------------
983 qconid :: { RdrName }
985 | QCONID { case $1 of { (mod,n) ->
986 mkSrcQual dataName mod n } }
989 : CONID { mkSrcUnqual dataName $1 }
991 -----------------------------------------------------------------------------
994 qconsym :: { RdrName }
996 | QCONSYM { case $1 of { (mod,n) ->
997 mkSrcQual dataName mod n } }
999 consym :: { RdrName }
1000 : CONSYM { mkSrcUnqual dataName $1 }
1002 -----------------------------------------------------------------------------
1005 qvarsym :: { RdrName }
1009 qvarsymm :: { RdrName }
1013 varsym :: { RdrName }
1014 : VARSYM { mkSrcUnqual varName $1 }
1019 varsymm :: { RdrName } -- varsym not including '-'
1020 : VARSYM { mkSrcUnqual varName $1 }
1024 qvarsym1 :: { RdrName }
1025 : QVARSYM { case $1 of { (mod,n) ->
1026 mkSrcQual varName mod n } }
1028 literal :: { HsLit }
1029 : INTEGER { HsInt $1 }
1030 | CHAR { HsChar $1 }
1031 | RATIONAL { HsFrac $1 }
1032 | STRING { HsString $1 }
1034 | PRIMINTEGER { HsIntPrim $1 }
1035 | PRIMCHAR { HsCharPrim $1 }
1036 | PRIMSTRING { HsStringPrim $1 }
1037 | PRIMFLOAT { HsFloatPrim $1 }
1038 | PRIMDOUBLE { HsDoublePrim $1 }
1039 | CLITLIT { HsLitLit $1 }
1041 srcloc :: { SrcLoc } : {% getSrcLocP }
1043 -----------------------------------------------------------------------------
1047 : vccurly { () } -- context popped in lexer.
1048 | error {% popContext }
1050 layout_on :: { () } : {% layoutOn True{-strict-} }
1051 layout_on_for_do :: { () } : {% layoutOn False }
1053 -----------------------------------------------------------------------------
1054 -- Miscellaneous (mostly renamings)
1056 modid :: { ModuleName }
1057 : CONID { mkSrcModuleFS $1 }
1059 tycon :: { RdrName }
1060 : CONID { mkSrcUnqual tcClsName $1 }
1062 qtycon :: { RdrName }
1064 | QCONID { case $1 of { (mod,n) ->
1065 mkSrcQual tcClsName mod n } }
1067 qtycls :: { RdrName }
1070 tyvar :: { RdrName }
1071 : VARID { mkSrcUnqual tvName $1 }
1072 | 'as' { as_tyvar_RDR }
1073 | 'qualified' { qualified_tyvar_RDR }
1074 | 'hiding' { hiding_tyvar_RDR }
1075 | 'export' { export_tyvar_RDR }
1076 | 'label' { label_tyvar_RDR }
1077 | 'dynamic' { dynamic_tyvar_RDR }
1078 | 'unsafe' { unsafe_tyvar_RDR }
1079 | 'stdcall' { stdcall_tyvar_RDR }
1080 | 'ccall' { ccall_tyvar_RDR }
1081 -- NOTE: no 'forall'
1083 -----------------------------------------------------------------------------
1087 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)