2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.20 2000/02/09 18:32:10 lewie Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parse ) where
21 import PrelMods ( mAIN_Name )
22 import OccName ( varName, ipName, dataName, tcClsName, tvName )
23 import SrcLoc ( SrcLoc )
26 import CmdLineOpts ( opt_SccProfilingOn )
27 import BasicTypes ( Fixity(..), FixityDirection(..), NewOrData(..) )
32 #include "HsVersions.h"
36 -----------------------------------------------------------------------------
37 Conflicts: 14 shift/reduce
38 (note: it's currently 21 -- JRL, 31/1/2000)
40 8 for abiguity in 'if x then y else z + 1'
41 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
42 1 for ambiguity in 'if x then y else z :: T'
43 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
44 3 for ambiguity in 'case x of y :: a -> b'
45 (don't know whether to reduce 'a' as a btype or shift the '->'.
46 conclusion: bogus expression anyway, doesn't matter)
48 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
49 since 'forall' is a valid variable name, we don't know whether
50 to treat a forall on the input as the beginning of a quantifier
51 or the beginning of the rule itself. Resolving to shift means
52 it's always treated as a quantifier, hence the above is disallowed.
53 This saves explicitly defining a grammar for the rule lhs that
54 doesn't include 'forall'.
56 1 for ambiguity in 'x @ Rec{..}'.
57 Only sensible parse is 'x @ (Rec{..})', which is what resolving
60 -----------------------------------------------------------------------------
64 '_' { ITunderscore } -- Haskell keywords
69 'default' { ITdefault }
70 'deriving' { ITderiving }
80 'instance' { ITinstance }
83 'newtype' { ITnewtype }
85 'qualified' { ITqualified }
91 'forall' { ITforall } -- GHC extension keywords
92 'foreign' { ITforeign }
95 'dynamic' { ITdynamic }
98 'stdcall' { ITstdcallconv }
99 'ccall' { ITccallconv }
100 '_ccall_' { ITccall (False, False, False) }
101 '_ccall_GC_' { ITccall (False, False, True) }
102 '_casm_' { ITccall (False, True, False) }
103 '_casm_GC_' { ITccall (False, True, True) }
105 '{-# SPECIALISE' { ITspecialise_prag }
106 '{-# SOURCE' { ITsource_prag }
107 '{-# INLINE' { ITinline_prag }
108 '{-# NOINLINE' { ITnoinline_prag }
109 '{-# RULES' { ITrules_prag }
110 '#-}' { ITclose_prag }
113 '__interface' { ITinterface } -- interface keywords
114 '__export' { IT__export }
115 '__instimport' { ITinstimport }
116 '__forall' { IT__forall }
117 '__letrec' { ITletrec }
118 '__coerce' { ITcoerce }
119 '__depends' { ITdepends }
120 '__inline' { ITinline }
121 '__DEFAULT' { ITdefaultbranch }
123 '__integer' { ITinteger_lit }
124 '__float' { ITfloat_lit }
125 '__rational' { ITrational_lit }
126 '__addr' { ITaddr_lit }
127 '__litlit' { ITlit_lit }
128 '__string' { ITstring_lit }
129 '__ccall' { ITccall $$ }
131 '__sccC' { ITsccAllCafs }
134 '__P' { ITspecialise }
136 '__U' { ITunfold $$ }
137 '__S' { ITstrict $$ }
138 '__M' { ITcprinfo $$ }
141 '..' { ITdotdot } -- reserved symbols
155 '/\\' { ITbiglam } -- GHC-extension symbols
157 '{' { ITocurly } -- special symbols
159 vccurly { ITvccurly } -- virtual close curly (from layout)
170 VARID { ITvarid $$ } -- identifiers
172 VARSYM { ITvarsym $$ }
173 CONSYM { ITconsym $$ }
174 QVARID { ITqvarid $$ }
175 QCONID { ITqconid $$ }
176 QVARSYM { ITqvarsym $$ }
177 QCONSYM { ITqconsym $$ }
179 IPVARID { ITipvarid $$ } -- GHC extension
181 PRAGMA { ITpragma $$ }
184 STRING { ITstring $$ }
185 INTEGER { ITinteger $$ }
186 RATIONAL { ITrational $$ }
188 PRIMCHAR { ITprimchar $$ }
189 PRIMSTRING { ITprimstring $$ }
190 PRIMINTEGER { ITprimint $$ }
191 PRIMFLOAT { ITprimfloat $$ }
192 PRIMDOUBLE { ITprimdouble $$ }
193 CLITLIT { ITlitlit $$ }
195 UNKNOWN { ITunknown $$ }
197 %monad { P } { thenP } { returnP }
198 %lexer { lexer } { ITeof }
203 -----------------------------------------------------------------------------
206 module :: { RdrNameHsModule }
207 : srcloc 'module' modid maybeexports 'where' body
208 { HsModule $3 Nothing $4 (fst $6) (snd $6) $1 }
210 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) $1 }
212 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
214 | layout_on top close { $2 }
216 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
217 : importdecls ';' cvtopdecls { (reverse $1,$3) }
218 | importdecls { (reverse $1,[]) }
219 | cvtopdecls { ([],$1) }
221 cvtopdecls :: { [RdrNameHsDecl] }
222 : topdecls { cvTopDecls (groupBindings $1)}
224 -----------------------------------------------------------------------------
227 maybeexports :: { Maybe [RdrNameIE] }
228 : '(' exportlist ')' { Just $2 }
229 | {- empty -} { Nothing }
231 exportlist :: { [RdrNameIE] }
232 : exportlist ',' export { $3 : $1 }
233 | exportlist ',' { $1 }
237 -- GHC extension: we allow things like [] and (,,,) to be exported
238 export :: { RdrNameIE }
240 | gtycon { IEThingAbs $1 }
241 | gtycon '(' '..' ')' { IEThingAll $1 }
242 | gtycon '(' ')' { IEThingWith $1 [] }
243 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
244 | 'module' modid { IEModuleContents $2 }
246 qcnames :: { [RdrName] }
247 : qcnames ',' qcname { $3 : $1 }
250 qcname :: { RdrName }
254 -----------------------------------------------------------------------------
255 -- Import Declarations
257 -- import decls can be *empty*, or even just a string of semicolons
258 -- whereas topdecls must contain at least one topdecl.
260 importdecls :: { [RdrNameImportDecl] }
261 : importdecls ';' importdecl { $3 : $1 }
262 | importdecls ';' { $1 }
263 | importdecl { [ $1 ] }
266 importdecl :: { RdrNameImportDecl }
267 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
268 { ImportDecl (mkSrcModuleFS $5) $3 $4 $6 $7 $2 }
270 maybe_src :: { WhereFrom }
271 : '{-# SOURCE' '#-}' { ImportByUserSource }
272 | {- empty -} { ImportByUser }
274 optqualified :: { Bool }
275 : 'qualified' { True }
276 | {- empty -} { False }
278 maybeas :: { Maybe ModuleName }
279 : 'as' modid { Just $2 }
280 | {- empty -} { Nothing }
282 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
283 : impspec { Just $1 }
284 | {- empty -} { Nothing }
286 impspec :: { (Bool, [RdrNameIE]) }
287 : '(' exportlist ')' { (False, reverse $2) }
288 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
290 -----------------------------------------------------------------------------
291 -- Fixity Declarations
295 | INTEGER {% checkPrec $1 `thenP_`
296 returnP (fromInteger $1) }
298 infix :: { FixityDirection }
300 | 'infixl' { InfixL }
301 | 'infixr' { InfixR }
304 : ops ',' op { $3 : $1 }
307 -----------------------------------------------------------------------------
308 -- Top-Level Declarations
310 topdecls :: { [RdrBinding] }
311 : topdecls ';' topdecl { ($3 : $1) }
312 | topdecls ';' { $1 }
315 topdecl :: { RdrBinding }
316 : srcloc 'type' simpletype '=' type
317 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
319 | srcloc 'data' ctype '=' constrs deriving
320 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
321 returnP (RdrHsDecl (TyClD
322 (TyData DataType cs c ts (reverse $5) $6
323 NoDataPragmas $1))) }
325 | srcloc 'newtype' ctype '=' newconstr deriving
326 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
327 returnP (RdrHsDecl (TyClD
328 (TyData NewType cs c ts [$5] $6
329 NoDataPragmas $1))) }
331 | srcloc 'class' ctype fds where
332 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
334 = cvMonoBindsAndSigs cvClassOpSig
337 returnP (RdrHsDecl (TyClD
338 (mkClassDecl cs c ts $4 sigs binds
339 NoClassPragmas $1))) }
341 | srcloc 'instance' inst_type where
343 = cvMonoBindsAndSigs cvInstDeclSig
346 (InstDecl $3 binds sigs dummyRdrVarName $1)) }
348 | srcloc 'default' '(' types0 ')'
349 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
351 | srcloc 'foreign' 'import' callconv ext_name
352 unsafe_flag varid_no_unsafe '::' sigtype
353 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
355 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
356 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
358 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
359 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
360 defaultCallConv $1)) }
364 decls :: { [RdrBinding] }
365 : decls ';' decl { $3 : $1 }
370 decl :: { RdrBinding }
373 | valdef { RdrValBinding $1 }
374 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
375 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
376 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
377 { foldr1 RdrAndBindings
378 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
379 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
380 { RdrSig (SpecInstSig $4 $2) }
381 | '{-# RULES' rules '#-}' { $2 }
383 opt_phase :: { Maybe Int }
384 : INTEGER { Just (fromInteger $1) }
385 | {- empty -} { Nothing }
387 sigtypes :: { [RdrNameHsType] }
389 | sigtypes ',' sigtype { $3 : $1 }
391 wherebinds :: { RdrNameHsBinds }
392 : where { cvBinds cvValSig (groupBindings $1) }
394 where :: { [RdrBinding] }
395 : 'where' decllist { $2 }
398 declbinds :: { RdrNameHsBinds }
399 : decllist { cvBinds cvValSig (groupBindings $1) }
401 decllist :: { [RdrBinding] }
402 : '{' decls '}' { $2 }
403 | layout_on decls close { $2 }
405 fixdecl :: { RdrBinding }
406 : srcloc infix prec ops { foldr1 RdrAndBindings
407 [ RdrSig (FixSig (FixitySig n
411 signdecl :: { RdrBinding }
412 : vars srcloc '::' sigtype { foldr1 RdrAndBindings
413 [ RdrSig (Sig n $4 $2) | n <- $1 ] }
415 sigtype :: { RdrNameHsType }
416 : ctype { mkHsForAllTy Nothing [] $1 }
419 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
420 instead of qvar, we get another shift/reduce-conflict. Consider the
423 { (+) :: ... } only var
424 { (+) x y = ... } could (incorrectly) be qvar
426 We re-use expressions for patterns, so a qvar would be allowed in patterns
427 instead of a var only (which would be correct). But deciding what the + is,
428 would require more lookahead. So let's check for ourselves...
431 vars :: { [RdrName] }
432 : vars ',' var { $3 : $1 }
435 -----------------------------------------------------------------------------
436 -- Transformation Rules
438 rules :: { RdrBinding }
439 : rules ';' rule { $1 `RdrAndBindings` $3 }
442 | {- empty -} { RdrNullBind }
444 rule :: { RdrBinding }
445 : STRING rule_forall fexp '=' srcloc exp
446 { RdrHsDecl (RuleD (RuleDecl $1 [] $2 $3 $6 $5)) }
448 rule_forall :: { [RdrNameRuleBndr] }
449 : 'forall' rule_var_list '.' { $2 }
452 rule_var_list :: { [RdrNameRuleBndr] }
454 | rule_var rule_var_list { $1 : $2 }
456 rule_var :: { RdrNameRuleBndr }
457 : varid { RuleBndr $1 }
458 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
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 }
478 -----------------------------------------------------------------------------
481 -- A ctype is a for-all type
482 ctype :: { RdrNameHsType }
483 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
484 | context type { mkHsForAllTy Nothing $1 $2 }
485 -- A type of form (context => type) is an *implicit* HsForAllTy
488 type :: { RdrNameHsType }
489 : btype '->' type { MonoFunTy $1 $3 }
492 btype :: { RdrNameHsType }
493 : btype atype { MonoTyApp $1 $2 }
494 | IPVARID '::' type { MonoIParamTy (mkSrcUnqual ipName $1) $3 }
497 atype :: { RdrNameHsType }
498 : gtycon { MonoTyVar $1 }
499 | tyvar { MonoTyVar $1 }
500 | '(' type ',' types ')' { MonoTupleTy ($2 : reverse $4) True }
501 | '(#' types '#)' { MonoTupleTy (reverse $2) False }
502 | '[' type ']' { MonoListTy $2 }
503 | '(' ctype ')' { $2 }
505 gtycon :: { RdrName }
507 | '(' ')' { unitTyCon_RDR }
508 | '(' '->' ')' { funTyCon_RDR }
509 | '[' ']' { listTyCon_RDR }
510 | '(' commas ')' { tupleTyCon_RDR $2 }
512 -- An inst_type is what occurs in the head of an instance decl
513 -- e.g. (Foo a, Gaz b) => Wibble a b
514 -- It's kept as a single type, with a MonoDictTy at the right
515 -- hand corner, for convenience.
516 inst_type :: { RdrNameHsType }
517 : ctype {% checkInstType $1 }
519 types0 :: { [RdrNameHsType] }
523 types :: { [RdrNameHsType] }
525 | types ',' type { $3 : $1 }
527 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
528 : tycon tyvars { ($1, reverse $2) }
530 tyvars :: { [RdrNameHsTyVar] }
531 : tyvars tyvar { UserTyVar $2 : $1 }
534 fds :: { [([RdrName], [RdrName])] }
536 | '|' fds1 { reverse $2 }
538 fds1 :: { [([RdrName], [RdrName])] }
539 : fds1 ',' fd { $3 : $1 }
542 fd :: { ([RdrName], [RdrName]) }
543 : varids0 '->' varids0 { (reverse $1, reverse $3) }
545 varids0 :: { [RdrName] }
547 | varids0 tyvar { $2 : $1 }
549 -----------------------------------------------------------------------------
550 -- Datatype declarations
552 constrs :: { [RdrNameConDecl] }
553 : constrs '|' constr { $3 : $1 }
556 constr :: { RdrNameConDecl }
557 : srcloc forall context constr_stuff
558 { ConDecl (fst $4) $2 $3 (snd $4) $1 }
559 | srcloc forall constr_stuff
560 { ConDecl (fst $3) $2 [] (snd $3) $1 }
562 forall :: { [RdrNameHsTyVar] }
563 : 'forall' tyvars '.' { $2 }
566 context :: { RdrNameContext }
567 : btype '=>' {% checkContext $1 }
569 constr_stuff :: { (RdrName, RdrNameConDetails) }
570 : scontype { (fst $1, VanillaCon (snd $1)) }
571 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
572 | con '{' fielddecls '}' { ($1, RecCon (reverse $3)) }
574 newconstr :: { RdrNameConDecl }
575 : srcloc conid atype { ConDecl $2 [] [] (NewCon $3 Nothing) $1 }
576 | srcloc conid '{' var '::' type '}'
577 { ConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
579 scontype :: { (RdrName, [RdrNameBangType]) }
580 : btype {% splitForConApp $1 [] }
583 scontype1 :: { (RdrName, [RdrNameBangType]) }
584 : btype '!' atype {% splitForConApp $1 [Banged $3] }
585 | scontype1 satype { (fst $1, snd $1 ++ [$2] ) }
587 satype :: { RdrNameBangType }
588 : atype { Unbanged $1 }
589 | '!' atype { Banged $2 }
591 sbtype :: { RdrNameBangType }
592 : btype { Unbanged $1 }
593 | '!' atype { Banged $2 }
595 fielddecls :: { [([RdrName],RdrNameBangType)] }
596 : fielddecls ',' fielddecl { $3 : $1 }
599 fielddecl :: { ([RdrName],RdrNameBangType) }
600 : vars '::' stype { (reverse $1, $3) }
602 stype :: { RdrNameBangType }
603 : ctype { Unbanged $1 }
604 | '!' atype { Banged $2 }
606 deriving :: { Maybe [RdrName] }
607 : {- empty -} { Nothing }
608 | 'deriving' qtycls { Just [$2] }
609 | 'deriving' '(' ')' { Just [] }
610 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
612 dclasses :: { [RdrName] }
613 : dclasses ',' qtycls { $3 : $1 }
616 -----------------------------------------------------------------------------
619 valdef :: { RdrNameMonoBinds }
620 : infixexp {-ToDo: opt_sig-} srcloc rhs
621 {% checkValDef $1 Nothing $3 $2 }
623 rhs :: { RdrNameGRHSs }
624 : '=' srcloc exp wherebinds { GRHSs (unguardedRHS $3 $2)
626 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
628 gdrhs :: { [RdrNameGRHS] }
629 : gdrhs gdrh { $2 : $1 }
632 gdrh :: { RdrNameGRHS }
633 : '|' srcloc quals '=' exp { GRHS (reverse
634 (ExprStmt $5 $2 : $3)) $2 }
636 -----------------------------------------------------------------------------
639 exp :: { RdrNameHsExpr }
640 : infixexp '::' sigtype { ExprWithTySig $1 $3 }
641 | infixexp 'with' dbinding { HsWith $1 $3 }
644 infixexp :: { RdrNameHsExpr }
646 | infixexp qop exp10 { OpApp $1 $2 (panic "fixity") $3 }
648 exp10 :: { RdrNameHsExpr }
649 : '\\' aexp aexps opt_asig '->' srcloc exp
650 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
651 returnP (HsLam (Match [] ps $4
652 (GRHSs (unguardedRHS $7 $6)
653 EmptyBinds Nothing))) }
654 | 'let' declbinds 'in' exp { HsLet $2 $4 }
655 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
656 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
657 | '-' fexp { NegApp $2 (error "NegApp") }
658 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
660 | '_ccall_' ccallid aexps0 { CCall $2 $3 False False cbot }
661 | '_ccall_GC_' ccallid aexps0 { CCall $2 $3 True False cbot }
662 | '_casm_' CLITLIT aexps0 { CCall $2 $3 False True cbot }
663 | '_casm_GC_' CLITLIT aexps0 { CCall $2 $3 True True cbot }
665 | '_scc_' STRING exp { if opt_SccProfilingOn
671 ccallid :: { FAST_STRING }
675 fexp :: { RdrNameHsExpr }
676 : fexp aexp { HsApp $1 $2 }
679 aexps0 :: { [RdrNameHsExpr] }
680 : aexps { reverse $1 }
682 aexps :: { [RdrNameHsExpr] }
683 : aexps aexp { $2 : $1 }
686 aexp :: { RdrNameHsExpr }
687 : aexp '{' fbinds '}' {% mkRecConstrOrUpdate $1 (reverse $3) }
690 aexp1 :: { RdrNameHsExpr }
692 | IPVARID { HsIPVar (mkSrcUnqual ipName $1) }
694 | literal { HsLit $1 }
695 | '(' exp ')' { HsPar $2 }
696 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) True }
697 | '(#' texps '#)' { ExplicitTuple (reverse $2) False }
698 | '[' list ']' { $2 }
699 | '(' infixexp qop ')' { SectionL $2 $3 }
700 | '(' qopm infixexp ')' { SectionR $2 $3 }
701 | qvar '@' aexp { EAsPat $1 $3 }
703 | '~' aexp1 { ELazyPat $2 }
706 : commas ',' { $1 + 1 }
709 texps :: { [RdrNameHsExpr] }
710 : texps ',' exp { $3 : $1 }
713 -----------------------------------------------------------------------------
716 -- The rules below are little bit contorted to keep lexps left-recursive while
717 -- avoiding another shift/reduce-conflict.
719 list :: { RdrNameHsExpr }
720 : exp { ExplicitList [$1] }
721 | lexps { ExplicitList (reverse $1) }
722 | exp '..' { ArithSeqIn (From $1) }
723 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
724 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
725 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
726 | exp srcloc '|' quals { HsDo ListComp (reverse
727 (ReturnStmt $1 : $4)) $2 }
729 lexps :: { [RdrNameHsExpr] }
730 : lexps ',' exp { $3 : $1 }
731 | exp ',' exp { [$3,$1] }
733 -----------------------------------------------------------------------------
734 -- List Comprehensions
736 quals :: { [RdrNameStmt] }
737 : quals ',' qual { $3 : $1 }
740 qual :: { RdrNameStmt }
741 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
742 returnP (BindStmt p $4 $1) }
743 | srcloc exp { GuardStmt $2 $1 }
744 | srcloc 'let' declbinds { LetStmt $3 }
746 -----------------------------------------------------------------------------
749 altslist :: { [RdrNameMatch] }
750 : '{' alts '}' { reverse $2 }
751 | layout_on alts close { reverse $2 }
753 alts :: { [RdrNameMatch] }
757 alts1 :: { [RdrNameMatch] }
758 : alts1 ';' alt { $3 : $1 }
762 alt :: { RdrNameMatch }
763 : infixexp opt_sig ralt wherebinds
764 {% checkPattern $1 `thenP` \p ->
765 returnP (Match [] [p] $2
766 (GRHSs $3 $4 Nothing)) }
768 opt_sig :: { Maybe RdrNameHsType }
769 : {- empty -} { Nothing }
770 | '::' type { Just $2 }
772 opt_asig :: { Maybe RdrNameHsType }
773 : {- empty -} { Nothing }
774 | '::' atype { Just $2 }
776 ralt :: { [RdrNameGRHS] }
777 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
778 | gdpats { (reverse $1) }
780 gdpats :: { [RdrNameGRHS] }
781 : gdpats gdpat { $2 : $1 }
784 gdpat :: { RdrNameGRHS }
785 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
787 -----------------------------------------------------------------------------
788 -- Statement sequences
790 stmtlist :: { [RdrNameStmt] }
791 : '{' stmts '}' { reverse $2 }
792 | layout_on_for_do stmts close { reverse $2 }
794 -- Stmt list should really end in an expression, but it's not
795 -- convenient to enforce this here, so we throw out erroneous
796 -- statement sequences in the renamer instead.
798 stmts :: { [RdrNameStmt] }
802 stmts1 :: { [RdrNameStmt] }
803 : stmts1 ';' stmt { $3 : $1 }
807 stmt :: { RdrNameStmt }
808 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
809 returnP (BindStmt p $4 $1) }
810 | srcloc exp { ExprStmt $2 $1 }
811 | srcloc 'let' declbinds { LetStmt $3 }
813 -----------------------------------------------------------------------------
814 -- Record Field Update/Construction
816 fbinds :: { RdrNameHsRecordBinds }
817 : fbinds ',' fbind { $3 : $1 }
822 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
823 : qvar '=' exp { ($1,$3,False) }
825 -----------------------------------------------------------------------------
826 -- Implicit Parameter Bindings
828 dbinding :: { [(RdrName, RdrNameHsExpr)] }
829 : '{' dbinds '}' { $2 }
830 | layout_on dbinds close { $2 }
832 dbinds :: { [(RdrName, RdrNameHsExpr)] }
833 : dbinds ';' dbind { $3 : $1 }
838 dbind :: { (RdrName, RdrNameHsExpr) }
839 dbind : IPVARID '=' exp { (mkSrcUnqual ipName $1, $3) }
841 -----------------------------------------------------------------------------
842 -- Variables, Constructors and Operators.
845 : '(' ')' { unitCon_RDR }
846 | '[' ']' { nilCon_RDR }
847 | '(' commas ')' { tupleCon_RDR $2 }
852 | '(' varsym ')' { $2 }
856 | '(' qvarsym ')' { $2 }
860 | '(' consym ')' { $2 }
864 | '(' qconsym ')' { $2 }
868 | '`' varid '`' { $2 }
870 qvarop :: { RdrName }
872 | '`' qvarid '`' { $2 }
874 qvaropm :: { RdrName }
876 | '`' qvarid '`' { $2 }
880 | '`' conid '`' { $2 }
882 qconop :: { RdrName }
884 | '`' qconid '`' { $2 }
886 -----------------------------------------------------------------------------
889 op :: { RdrName } -- used in infix decls
893 qop :: { RdrNameHsExpr } -- used in sections
894 : qvarop { HsVar $1 }
895 | qconop { HsVar $1 }
897 qopm :: { RdrNameHsExpr } -- used in sections
898 : qvaropm { HsVar $1 }
899 | qconop { HsVar $1 }
901 -----------------------------------------------------------------------------
904 qvarid :: { RdrName }
906 | QVARID { case $1 of { (mod,n) ->
907 mkSrcQual varName mod n } }
910 : VARID { mkSrcUnqual varName $1 }
911 | 'as' { as_var_RDR }
912 | 'qualified' { qualified_var_RDR }
913 | 'hiding' { hiding_var_RDR }
914 | 'forall' { forall_var_RDR }
915 | 'export' { export_var_RDR }
916 | 'label' { label_var_RDR }
917 | 'dynamic' { dynamic_var_RDR }
918 | 'unsafe' { unsafe_var_RDR }
919 | 'stdcall' { stdcall_var_RDR }
920 | 'ccall' { ccall_var_RDR }
922 varid_no_unsafe :: { RdrName }
923 : VARID { mkSrcUnqual varName $1 }
924 | 'as' { as_var_RDR }
925 | 'qualified' { qualified_var_RDR }
926 | 'hiding' { hiding_var_RDR }
927 | 'forall' { forall_var_RDR }
928 | 'export' { export_var_RDR }
929 | 'label' { label_var_RDR }
930 | 'dynamic' { dynamic_var_RDR }
931 | 'stdcall' { stdcall_var_RDR }
932 | 'ccall' { ccall_var_RDR }
934 -----------------------------------------------------------------------------
937 qconid :: { RdrName }
939 | QCONID { case $1 of { (mod,n) ->
940 mkSrcQual dataName mod n } }
943 : CONID { mkSrcUnqual dataName $1 }
945 -----------------------------------------------------------------------------
948 qconsym :: { RdrName }
950 | QCONSYM { case $1 of { (mod,n) ->
951 mkSrcQual dataName mod n } }
953 consym :: { RdrName }
954 : CONSYM { mkSrcUnqual dataName $1 }
956 -----------------------------------------------------------------------------
959 qvarsym :: { RdrName }
963 qvarsymm :: { RdrName }
967 varsym :: { RdrName }
968 : VARSYM { mkSrcUnqual varName $1 }
973 varsymm :: { RdrName } -- varsym not including '-'
974 : VARSYM { mkSrcUnqual varName $1 }
978 qvarsym1 :: { RdrName }
979 : QVARSYM { case $1 of { (mod,n) ->
980 mkSrcQual varName mod n } }
983 : INTEGER { HsInt $1 }
985 | RATIONAL { HsFrac $1 }
986 | STRING { HsString $1 }
988 | PRIMINTEGER { HsIntPrim $1 }
989 | PRIMCHAR { HsCharPrim $1 }
990 | PRIMSTRING { HsStringPrim $1 }
991 | PRIMFLOAT { HsFloatPrim $1 }
992 | PRIMDOUBLE { HsDoublePrim $1 }
993 | CLITLIT { HsLitLit $1 }
995 srcloc :: { SrcLoc } : {% getSrcLocP }
997 -----------------------------------------------------------------------------
1001 : vccurly { () } -- context popped in lexer.
1002 | error {% popContext }
1004 layout_on :: { () } : {% layoutOn True{-strict-} }
1005 layout_on_for_do :: { () } : {% layoutOn False }
1007 -----------------------------------------------------------------------------
1008 -- Miscellaneous (mostly renamings)
1010 modid :: { ModuleName }
1011 : CONID { mkSrcModuleFS $1 }
1013 tycon :: { RdrName }
1014 : CONID { mkSrcUnqual tcClsName $1 }
1016 qtycon :: { RdrName }
1018 | QCONID { case $1 of { (mod,n) ->
1019 mkSrcQual tcClsName mod n } }
1021 qtycls :: { RdrName }
1024 tyvar :: { RdrName }
1025 : VARID { mkSrcUnqual tvName $1 }
1026 | 'as' { as_tyvar_RDR }
1027 | 'qualified' { qualified_tyvar_RDR }
1028 | 'hiding' { hiding_tyvar_RDR }
1029 | 'export' { export_tyvar_RDR }
1030 | 'label' { label_tyvar_RDR }
1031 | 'dynamic' { dynamic_tyvar_RDR }
1032 | 'unsafe' { unsafe_tyvar_RDR }
1033 | 'stdcall' { stdcall_tyvar_RDR }
1034 | 'ccall' { ccall_tyvar_RDR }
1035 -- NOTE: no 'forall'
1037 -----------------------------------------------------------------------------
1041 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)