2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.19 2000/01/28 20:52:39 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
39 8 for abiguity in 'if x then y else z + 1'
40 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
41 1 for ambiguity in 'if x then y else z :: T'
42 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
43 3 for ambiguity in 'case x of y :: a -> b'
44 (don't know whether to reduce 'a' as a btype or shift the '->'.
45 conclusion: bogus expression anyway, doesn't matter)
47 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
48 since 'forall' is a valid variable name, we don't know whether
49 to treat a forall on the input as the beginning of a quantifier
50 or the beginning of the rule itself. Resolving to shift means
51 it's always treated as a quantifier, hence the above is disallowed.
52 This saves explicitly defining a grammar for the rule lhs that
53 doesn't include 'forall'.
55 1 for ambiguity in 'x @ Rec{..}'.
56 Only sensible parse is 'x @ (Rec{..})', which is what resolving
59 -----------------------------------------------------------------------------
63 '_' { ITunderscore } -- Haskell keywords
68 'default' { ITdefault }
69 'deriving' { ITderiving }
79 'instance' { ITinstance }
82 'newtype' { ITnewtype }
84 'qualified' { ITqualified }
91 'forall' { ITforall } -- GHC extension keywords
92 'foreign' { ITforeign }
95 'dynamic' { ITdynamic }
97 'stdcall' { ITstdcallconv }
98 'ccall' { ITccallconv }
99 '_ccall_' { ITccall (False, False, False) }
100 '_ccall_GC_' { ITccall (False, False, True) }
101 '_casm_' { ITccall (False, True, False) }
102 '_casm_GC_' { ITccall (False, True, True) }
104 '{-# SPECIALISE' { ITspecialise_prag }
105 '{-# SOURCE' { ITsource_prag }
106 '{-# INLINE' { ITinline_prag }
107 '{-# NOINLINE' { ITnoinline_prag }
108 '{-# RULES' { ITrules_prag }
109 '#-}' { ITclose_prag }
112 '__interface' { ITinterface } -- interface keywords
113 '__export' { IT__export }
114 '__instimport' { ITinstimport }
115 '__forall' { IT__forall }
116 '__letrec' { ITletrec }
117 '__coerce' { ITcoerce }
118 '__depends' { ITdepends }
119 '__inline' { ITinline }
120 '__DEFAULT' { ITdefaultbranch }
122 '__integer' { ITinteger_lit }
123 '__float' { ITfloat_lit }
124 '__rational' { ITrational_lit }
125 '__addr' { ITaddr_lit }
126 '__litlit' { ITlit_lit }
127 '__string' { ITstring_lit }
128 '__ccall' { ITccall $$ }
130 '__sccC' { ITsccAllCafs }
133 '__P' { ITspecialise }
135 '__U' { ITunfold $$ }
136 '__S' { ITstrict $$ }
137 '__M' { ITcprinfo $$ }
140 '..' { ITdotdot } -- reserved symbols
154 '/\\' { ITbiglam } -- GHC-extension symbols
156 '{' { ITocurly } -- special symbols
158 vccurly { ITvccurly } -- virtual close curly (from layout)
169 VARID { ITvarid $$ } -- identifiers
171 VARSYM { ITvarsym $$ }
172 CONSYM { ITconsym $$ }
173 QVARID { ITqvarid $$ }
174 QCONID { ITqconid $$ }
175 QVARSYM { ITqvarsym $$ }
176 QCONSYM { ITqconsym $$ }
177 IPVARID { ITipvarid $$ }
179 PRAGMA { ITpragma $$ }
182 STRING { ITstring $$ }
183 INTEGER { ITinteger $$ }
184 RATIONAL { ITrational $$ }
186 PRIMCHAR { ITprimchar $$ }
187 PRIMSTRING { ITprimstring $$ }
188 PRIMINTEGER { ITprimint $$ }
189 PRIMFLOAT { ITprimfloat $$ }
190 PRIMDOUBLE { ITprimdouble $$ }
191 CLITLIT { ITlitlit $$ }
193 UNKNOWN { ITunknown $$ }
195 %monad { P } { thenP } { returnP }
196 %lexer { lexer } { ITeof }
201 -----------------------------------------------------------------------------
204 module :: { RdrNameHsModule }
205 : srcloc 'module' modid maybeexports 'where' body
206 { HsModule $3 Nothing $4 (fst $6) (snd $6) $1 }
208 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) $1 }
210 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
212 | layout_on top close { $2 }
214 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
215 : importdecls ';' cvtopdecls { (reverse $1,$3) }
216 | importdecls { (reverse $1,[]) }
217 | cvtopdecls { ([],$1) }
219 cvtopdecls :: { [RdrNameHsDecl] }
220 : topdecls { cvTopDecls (groupBindings $1)}
222 -----------------------------------------------------------------------------
225 maybeexports :: { Maybe [RdrNameIE] }
226 : '(' exportlist ')' { Just $2 }
227 | {- empty -} { Nothing }
229 exportlist :: { [RdrNameIE] }
230 : exportlist ',' export { $3 : $1 }
231 | exportlist ',' { $1 }
235 -- GHC extension: we allow things like [] and (,,,) to be exported
236 export :: { RdrNameIE }
238 | gtycon { IEThingAbs $1 }
239 | gtycon '(' '..' ')' { IEThingAll $1 }
240 | gtycon '(' ')' { IEThingWith $1 [] }
241 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
242 | 'module' modid { IEModuleContents $2 }
244 qcnames :: { [RdrName] }
245 : qcnames ',' qcname { $3 : $1 }
248 qcname :: { RdrName }
252 -----------------------------------------------------------------------------
253 -- Import Declarations
255 -- import decls can be *empty*, or even just a string of semicolons
256 -- whereas topdecls must contain at least one topdecl.
258 importdecls :: { [RdrNameImportDecl] }
259 : importdecls ';' importdecl { $3 : $1 }
260 | importdecls ';' { $1 }
261 | importdecl { [ $1 ] }
264 importdecl :: { RdrNameImportDecl }
265 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
266 { ImportDecl (mkSrcModuleFS $5) $3 $4 $6 $7 $2 }
268 maybe_src :: { WhereFrom }
269 : '{-# SOURCE' '#-}' { ImportByUserSource }
270 | {- empty -} { ImportByUser }
272 optqualified :: { Bool }
273 : 'qualified' { True }
274 | {- empty -} { False }
276 maybeas :: { Maybe ModuleName }
277 : 'as' modid { Just $2 }
278 | {- empty -} { Nothing }
280 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
281 : impspec { Just $1 }
282 | {- empty -} { Nothing }
284 impspec :: { (Bool, [RdrNameIE]) }
285 : '(' exportlist ')' { (False, reverse $2) }
286 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
288 -----------------------------------------------------------------------------
289 -- Fixity Declarations
293 | INTEGER {% checkPrec $1 `thenP_`
294 returnP (fromInteger $1) }
296 infix :: { FixityDirection }
298 | 'infixl' { InfixL }
299 | 'infixr' { InfixR }
302 : ops ',' op { $3 : $1 }
305 -----------------------------------------------------------------------------
306 -- Top-Level Declarations
308 topdecls :: { [RdrBinding] }
309 : topdecls ';' topdecl { ($3 : $1) }
310 | topdecls ';' { $1 }
313 topdecl :: { RdrBinding }
314 : srcloc 'type' simpletype '=' type
315 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
317 | srcloc 'data' ctype '=' constrs deriving
318 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
319 returnP (RdrHsDecl (TyClD
320 (TyData DataType cs c ts (reverse $5) $6
321 NoDataPragmas $1))) }
323 | srcloc 'newtype' ctype '=' newconstr deriving
324 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
325 returnP (RdrHsDecl (TyClD
326 (TyData NewType cs c ts [$5] $6
327 NoDataPragmas $1))) }
329 | srcloc 'class' ctype fds where
330 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
332 = cvMonoBindsAndSigs cvClassOpSig
335 returnP (RdrHsDecl (TyClD
336 (mkClassDecl cs c ts $4 sigs binds
337 NoClassPragmas $1))) }
339 | srcloc 'instance' inst_type where
341 = cvMonoBindsAndSigs cvInstDeclSig
344 (InstDecl $3 binds sigs dummyRdrVarName $1)) }
346 | srcloc 'default' '(' types0 ')'
347 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
349 | srcloc 'foreign' 'import' callconv ext_name
350 unsafe_flag varid_no_unsafe '::' sigtype
351 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
353 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
354 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
356 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
357 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
358 defaultCallConv $1)) }
362 decls :: { [RdrBinding] }
363 : decls ';' decl { $3 : $1 }
368 decl :: { RdrBinding }
371 | valdef { RdrValBinding $1 }
372 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
373 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
374 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
375 { foldr1 RdrAndBindings
376 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
377 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
378 { RdrSig (SpecInstSig $4 $2) }
379 | '{-# RULES' rules '#-}' { $2 }
381 opt_phase :: { Maybe Int }
382 : INTEGER { Just (fromInteger $1) }
383 | {- empty -} { Nothing }
385 sigtypes :: { [RdrNameHsType] }
387 | sigtypes ',' sigtype { $3 : $1 }
389 wherebinds :: { RdrNameHsBinds }
390 : where { cvBinds cvValSig (groupBindings $1) }
392 where :: { [RdrBinding] }
393 : 'where' decllist { $2 }
396 declbinds :: { RdrNameHsBinds }
397 : decllist { cvBinds cvValSig (groupBindings $1) }
399 decllist :: { [RdrBinding] }
400 : '{' decls '}' { $2 }
401 | layout_on decls close { $2 }
403 fixdecl :: { RdrBinding }
404 : srcloc infix prec ops { foldr1 RdrAndBindings
405 [ RdrSig (FixSig (FixitySig n
409 signdecl :: { RdrBinding }
410 : vars srcloc '::' sigtype { foldr1 RdrAndBindings
411 [ RdrSig (Sig n $4 $2) | n <- $1 ] }
413 sigtype :: { RdrNameHsType }
414 : ctype { mkHsForAllTy Nothing [] $1 }
417 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
418 instead of qvar, we get another shift/reduce-conflict. Consider the
421 { (+) :: ... } only var
422 { (+) x y = ... } could (incorrectly) be qvar
424 We re-use expressions for patterns, so a qvar would be allowed in patterns
425 instead of a var only (which would be correct). But deciding what the + is,
426 would require more lookahead. So let's check for ourselves...
429 vars :: { [RdrName] }
430 : vars ',' var { $3 : $1 }
433 -----------------------------------------------------------------------------
434 -- Transformation Rules
436 rules :: { RdrBinding }
437 : rules ';' rule { $1 `RdrAndBindings` $3 }
440 | {- empty -} { RdrNullBind }
442 rule :: { RdrBinding }
443 : STRING rule_forall fexp '=' srcloc exp
444 { RdrHsDecl (RuleD (RuleDecl $1 [] $2 $3 $6 $5)) }
446 rule_forall :: { [RdrNameRuleBndr] }
447 : 'forall' rule_var_list '.' { $2 }
450 rule_var_list :: { [RdrNameRuleBndr] }
452 | rule_var rule_var_list { $1 : $2 }
454 rule_var :: { RdrNameRuleBndr }
455 : varid { RuleBndr $1 }
456 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
458 -----------------------------------------------------------------------------
459 -- Foreign import/export
462 : 'stdcall' { stdCallConv }
463 | 'ccall' { cCallConv }
464 | {- empty -} { defaultCallConv }
466 unsafe_flag :: { Bool }
468 | {- empty -} { False }
470 ext_name :: { Maybe ExtName }
471 : 'dynamic' { Just Dynamic }
472 | STRING { Just (ExtName $1 Nothing) }
473 | STRING STRING { Just (ExtName $2 (Just $1)) }
474 | {- empty -} { Nothing }
476 -----------------------------------------------------------------------------
479 -- A ctype is a for-all type
480 ctype :: { RdrNameHsType }
481 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
482 | context type { mkHsForAllTy Nothing $1 $2 }
483 -- A type of form (context => type) is an *implicit* HsForAllTy
486 type :: { RdrNameHsType }
487 : btype '->' type { MonoFunTy $1 $3 }
490 btype :: { RdrNameHsType }
491 : btype atype { MonoTyApp $1 $2 }
494 atype :: { RdrNameHsType }
495 : gtycon { MonoTyVar $1 }
496 | tyvar { MonoTyVar $1 }
497 | '(' type ',' types ')' { MonoTupleTy ($2 : reverse $4) True }
498 | '(#' types '#)' { MonoTupleTy (reverse $2) False }
499 | '[' type ']' { MonoListTy $2 }
500 | '(' ctype ')' { $2 }
502 gtycon :: { RdrName }
504 | '(' ')' { unitTyCon_RDR }
505 | '(' '->' ')' { funTyCon_RDR }
506 | '[' ']' { listTyCon_RDR }
507 | '(' commas ')' { tupleTyCon_RDR $2 }
509 -- An inst_type is what occurs in the head of an instance decl
510 -- e.g. (Foo a, Gaz b) => Wibble a b
511 -- It's kept as a single type, with a MonoDictTy at the right
512 -- hand corner, for convenience.
513 inst_type :: { RdrNameHsType }
514 : ctype {% checkInstType $1 }
516 types0 :: { [RdrNameHsType] }
520 types :: { [RdrNameHsType] }
522 | types ',' type { $3 : $1 }
524 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
525 : tycon tyvars { ($1, reverse $2) }
527 tyvars :: { [RdrNameHsTyVar] }
528 : tyvars tyvar { UserTyVar $2 : $1 }
531 fds :: { [([RdrName], [RdrName])] }
533 | '|' fds1 { reverse $2 }
535 fds1 :: { [([RdrName], [RdrName])] }
536 : fds1 ',' fd { $3 : $1 }
539 fd :: { ([RdrName], [RdrName]) }
540 : varids0 '->' varids0 { (reverse $1, reverse $3) }
542 varids0 :: { [RdrName] }
544 | varids0 tyvar { $2 : $1 }
546 -----------------------------------------------------------------------------
547 -- Datatype declarations
549 constrs :: { [RdrNameConDecl] }
550 : constrs '|' constr { $3 : $1 }
553 constr :: { RdrNameConDecl }
554 : srcloc forall context constr_stuff
555 { ConDecl (fst $4) $2 $3 (snd $4) $1 }
556 | srcloc forall constr_stuff
557 { ConDecl (fst $3) $2 [] (snd $3) $1 }
559 forall :: { [RdrNameHsTyVar] }
560 : 'forall' tyvars '.' { $2 }
563 context :: { RdrNameContext }
564 : btype '=>' {% checkContext $1 }
566 constr_stuff :: { (RdrName, RdrNameConDetails) }
567 : scontype { (fst $1, VanillaCon (snd $1)) }
568 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
569 | con '{' fielddecls '}' { ($1, RecCon (reverse $3)) }
571 newconstr :: { RdrNameConDecl }
572 : srcloc conid atype { ConDecl $2 [] [] (NewCon $3 Nothing) $1 }
573 | srcloc conid '{' var '::' type '}'
574 { ConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
576 scontype :: { (RdrName, [RdrNameBangType]) }
577 : btype {% splitForConApp $1 [] }
580 scontype1 :: { (RdrName, [RdrNameBangType]) }
581 : btype '!' atype {% splitForConApp $1 [Banged $3] }
582 | scontype1 satype { (fst $1, snd $1 ++ [$2] ) }
584 satype :: { RdrNameBangType }
585 : atype { Unbanged $1 }
586 | '!' atype { Banged $2 }
588 sbtype :: { RdrNameBangType }
589 : btype { Unbanged $1 }
590 | '!' atype { Banged $2 }
592 fielddecls :: { [([RdrName],RdrNameBangType)] }
593 : fielddecls ',' fielddecl { $3 : $1 }
596 fielddecl :: { ([RdrName],RdrNameBangType) }
597 : vars '::' stype { (reverse $1, $3) }
599 stype :: { RdrNameBangType }
600 : ctype { Unbanged $1 }
601 | '!' atype { Banged $2 }
603 deriving :: { Maybe [RdrName] }
604 : {- empty -} { Nothing }
605 | 'deriving' qtycls { Just [$2] }
606 | 'deriving' '(' ')' { Just [] }
607 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
609 dclasses :: { [RdrName] }
610 : dclasses ',' qtycls { $3 : $1 }
613 -----------------------------------------------------------------------------
616 valdef :: { RdrNameMonoBinds }
617 : infixexp {-ToDo: opt_sig-} srcloc rhs
618 {% checkValDef $1 Nothing $3 $2 }
620 rhs :: { RdrNameGRHSs }
621 : '=' srcloc exp wherebinds { GRHSs (unguardedRHS $3 $2)
623 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
625 gdrhs :: { [RdrNameGRHS] }
626 : gdrhs gdrh { $2 : $1 }
629 gdrh :: { RdrNameGRHS }
630 : '|' srcloc quals '=' exp { GRHS (reverse
631 (ExprStmt $5 $2 : $3)) $2 }
633 -----------------------------------------------------------------------------
636 exp :: { RdrNameHsExpr }
637 : infixexp '::' sigtype { ExprWithTySig $1 $3 }
638 | infixexp 'with' dbinding { HsWith $1 $3 }
641 infixexp :: { RdrNameHsExpr }
643 | infixexp qop exp10 { OpApp $1 $2 (panic "fixity") $3 }
645 exp10 :: { RdrNameHsExpr }
646 : '\\' aexp aexps opt_asig '->' srcloc exp
647 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
648 returnP (HsLam (Match [] ps $4
649 (GRHSs (unguardedRHS $7 $6)
650 EmptyBinds Nothing))) }
651 | 'let' declbinds 'in' exp { HsLet $2 $4 }
652 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
653 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
654 | '-' fexp { NegApp $2 (error "NegApp") }
655 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
657 | '_ccall_' ccallid aexps0 { CCall $2 $3 False False cbot }
658 | '_ccall_GC_' ccallid aexps0 { CCall $2 $3 True False cbot }
659 | '_casm_' CLITLIT aexps0 { CCall $2 $3 False True cbot }
660 | '_casm_GC_' CLITLIT aexps0 { CCall $2 $3 True True cbot }
662 | '_scc_' STRING exp { if opt_SccProfilingOn
668 ccallid :: { FAST_STRING }
672 fexp :: { RdrNameHsExpr }
673 : fexp aexp { HsApp $1 $2 }
676 aexps0 :: { [RdrNameHsExpr] }
677 : aexps { reverse $1 }
679 aexps :: { [RdrNameHsExpr] }
680 : aexps aexp { $2 : $1 }
683 aexp :: { RdrNameHsExpr }
684 : aexp '{' fbinds '}' {% mkRecConstrOrUpdate $1 (reverse $3) }
687 aexp1 :: { RdrNameHsExpr }
689 | IPVARID { HsIPVar (mkSrcUnqual ipName $1) }
691 | literal { HsLit $1 }
692 | '(' exp ')' { HsPar $2 }
693 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) True }
694 | '(#' texps '#)' { ExplicitTuple (reverse $2) False }
695 | '[' list ']' { $2 }
696 | '(' infixexp qop ')' { SectionL $2 $3 }
697 | '(' qopm infixexp ')' { SectionR $2 $3 }
698 | qvar '@' aexp { EAsPat $1 $3 }
700 | '~' aexp1 { ELazyPat $2 }
703 : commas ',' { $1 + 1 }
706 texps :: { [RdrNameHsExpr] }
707 : texps ',' exp { $3 : $1 }
710 -----------------------------------------------------------------------------
713 -- The rules below are little bit contorted to keep lexps left-recursive while
714 -- avoiding another shift/reduce-conflict.
716 list :: { RdrNameHsExpr }
717 : exp { ExplicitList [$1] }
718 | lexps { ExplicitList (reverse $1) }
719 | exp '..' { ArithSeqIn (From $1) }
720 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
721 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
722 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
723 | exp srcloc '|' quals { HsDo ListComp (reverse
724 (ReturnStmt $1 : $4)) $2 }
726 lexps :: { [RdrNameHsExpr] }
727 : lexps ',' exp { $3 : $1 }
728 | exp ',' exp { [$3,$1] }
730 -----------------------------------------------------------------------------
731 -- List Comprehensions
733 quals :: { [RdrNameStmt] }
734 : quals ',' qual { $3 : $1 }
737 qual :: { RdrNameStmt }
738 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
739 returnP (BindStmt p $4 $1) }
740 | srcloc exp { GuardStmt $2 $1 }
741 | srcloc 'let' declbinds { LetStmt $3 }
743 -----------------------------------------------------------------------------
746 altslist :: { [RdrNameMatch] }
747 : '{' alts '}' { reverse $2 }
748 | layout_on alts close { reverse $2 }
750 alts :: { [RdrNameMatch] }
754 alts1 :: { [RdrNameMatch] }
755 : alts1 ';' alt { $3 : $1 }
759 alt :: { RdrNameMatch }
760 : infixexp opt_sig ralt wherebinds
761 {% checkPattern $1 `thenP` \p ->
762 returnP (Match [] [p] $2
763 (GRHSs $3 $4 Nothing)) }
765 opt_sig :: { Maybe RdrNameHsType }
766 : {- empty -} { Nothing }
767 | '::' type { Just $2 }
769 opt_asig :: { Maybe RdrNameHsType }
770 : {- empty -} { Nothing }
771 | '::' atype { Just $2 }
773 ralt :: { [RdrNameGRHS] }
774 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
775 | gdpats { (reverse $1) }
777 gdpats :: { [RdrNameGRHS] }
778 : gdpats gdpat { $2 : $1 }
781 gdpat :: { RdrNameGRHS }
782 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
784 -----------------------------------------------------------------------------
785 -- Statement sequences
787 stmtlist :: { [RdrNameStmt] }
788 : '{' stmts '}' { reverse $2 }
789 | layout_on_for_do stmts close { reverse $2 }
791 -- Stmt list should really end in an expression, but it's not
792 -- convenient to enforce this here, so we throw out erroneous
793 -- statement sequences in the renamer instead.
795 stmts :: { [RdrNameStmt] }
799 stmts1 :: { [RdrNameStmt] }
800 : stmts1 ';' stmt { $3 : $1 }
804 stmt :: { RdrNameStmt }
805 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
806 returnP (BindStmt p $4 $1) }
807 | srcloc exp { ExprStmt $2 $1 }
808 | srcloc 'let' declbinds { LetStmt $3 }
810 -----------------------------------------------------------------------------
811 -- Record Field Update/Construction
813 fbinds :: { RdrNameHsRecordBinds }
814 : fbinds ',' fbind { $3 : $1 }
819 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
820 : qvar '=' exp { ($1,$3,False) }
822 -----------------------------------------------------------------------------
823 -- Implicit Parameter Bindings
825 dbinding :: { [(RdrName, RdrNameHsExpr)] }
826 : '{' dbinds '}' { $2 }
827 | layout_on dbinds close { $2 }
829 dbinds :: { [(RdrName, RdrNameHsExpr)] }
830 : dbinds ';' dbind { $3 : $1 }
835 dbind :: { (RdrName, RdrNameHsExpr) }
836 dbind : IPVARID '=' exp { (mkSrcUnqual ipName $1, $3) }
838 -----------------------------------------------------------------------------
839 -- Variables, Constructors and Operators.
842 : '(' ')' { unitCon_RDR }
843 | '[' ']' { nilCon_RDR }
844 | '(' commas ')' { tupleCon_RDR $2 }
849 | '(' varsym ')' { $2 }
853 | '(' qvarsym ')' { $2 }
857 | '(' consym ')' { $2 }
861 | '(' qconsym ')' { $2 }
865 | '`' varid '`' { $2 }
867 qvarop :: { RdrName }
869 | '`' qvarid '`' { $2 }
871 qvaropm :: { RdrName }
873 | '`' qvarid '`' { $2 }
877 | '`' conid '`' { $2 }
879 qconop :: { RdrName }
881 | '`' qconid '`' { $2 }
883 -----------------------------------------------------------------------------
886 op :: { RdrName } -- used in infix decls
890 qop :: { RdrNameHsExpr } -- used in sections
891 : qvarop { HsVar $1 }
892 | qconop { HsVar $1 }
894 qopm :: { RdrNameHsExpr } -- used in sections
895 : qvaropm { HsVar $1 }
896 | qconop { HsVar $1 }
898 -----------------------------------------------------------------------------
901 qvarid :: { RdrName }
903 | QVARID { case $1 of { (mod,n) ->
904 mkSrcQual varName mod n } }
907 : VARID { mkSrcUnqual varName $1 }
908 | 'as' { as_var_RDR }
909 | 'qualified' { qualified_var_RDR }
910 | 'hiding' { hiding_var_RDR }
911 | 'forall' { forall_var_RDR }
912 | 'export' { export_var_RDR }
913 | 'label' { label_var_RDR }
914 | 'dynamic' { dynamic_var_RDR }
915 | 'unsafe' { unsafe_var_RDR }
916 | 'stdcall' { stdcall_var_RDR }
917 | 'ccall' { ccall_var_RDR }
919 varid_no_unsafe :: { RdrName }
920 : VARID { mkSrcUnqual varName $1 }
921 | 'as' { as_var_RDR }
922 | 'qualified' { qualified_var_RDR }
923 | 'hiding' { hiding_var_RDR }
924 | 'forall' { forall_var_RDR }
925 | 'export' { export_var_RDR }
926 | 'label' { label_var_RDR }
927 | 'dynamic' { dynamic_var_RDR }
928 | 'stdcall' { stdcall_var_RDR }
929 | 'ccall' { ccall_var_RDR }
931 -----------------------------------------------------------------------------
934 qconid :: { RdrName }
936 | QCONID { case $1 of { (mod,n) ->
937 mkSrcQual dataName mod n } }
940 : CONID { mkSrcUnqual dataName $1 }
942 -----------------------------------------------------------------------------
945 qconsym :: { RdrName }
947 | QCONSYM { case $1 of { (mod,n) ->
948 mkSrcQual dataName mod n } }
950 consym :: { RdrName }
951 : CONSYM { mkSrcUnqual dataName $1 }
953 -----------------------------------------------------------------------------
956 qvarsym :: { RdrName }
960 qvarsymm :: { RdrName }
964 varsym :: { RdrName }
965 : VARSYM { mkSrcUnqual varName $1 }
970 varsymm :: { RdrName } -- varsym not including '-'
971 : VARSYM { mkSrcUnqual varName $1 }
975 qvarsym1 :: { RdrName }
976 : QVARSYM { case $1 of { (mod,n) ->
977 mkSrcQual varName mod n } }
980 : INTEGER { HsInt $1 }
982 | RATIONAL { HsFrac $1 }
983 | STRING { HsString $1 }
985 | PRIMINTEGER { HsIntPrim $1 }
986 | PRIMCHAR { HsCharPrim $1 }
987 | PRIMSTRING { HsStringPrim $1 }
988 | PRIMFLOAT { HsFloatPrim $1 }
989 | PRIMDOUBLE { HsDoublePrim $1 }
990 | CLITLIT { HsLitLit $1 }
992 srcloc :: { SrcLoc } : {% getSrcLocP }
994 -----------------------------------------------------------------------------
998 : vccurly { () } -- context popped in lexer.
999 | error {% popContext }
1001 layout_on :: { () } : {% layoutOn True{-strict-} }
1002 layout_on_for_do :: { () } : {% layoutOn False }
1004 -----------------------------------------------------------------------------
1005 -- Miscellaneous (mostly renamings)
1007 modid :: { ModuleName }
1008 : CONID { mkSrcModuleFS $1 }
1010 tycon :: { RdrName }
1011 : CONID { mkSrcUnqual tcClsName $1 }
1013 qtycon :: { RdrName }
1015 | QCONID { case $1 of { (mod,n) ->
1016 mkSrcQual tcClsName mod n } }
1018 qtycls :: { RdrName }
1021 tyvar :: { RdrName }
1022 : VARID { mkSrcUnqual tvName $1 }
1023 | 'as' { as_tyvar_RDR }
1024 | 'qualified' { qualified_tyvar_RDR }
1025 | 'hiding' { hiding_tyvar_RDR }
1026 | 'export' { export_tyvar_RDR }
1027 | 'label' { label_tyvar_RDR }
1028 | 'dynamic' { dynamic_tyvar_RDR }
1029 | 'unsafe' { unsafe_tyvar_RDR }
1030 | 'stdcall' { stdcall_tyvar_RDR }
1031 | 'ccall' { ccall_tyvar_RDR }
1032 -- NOTE: no 'forall'
1034 -----------------------------------------------------------------------------
1038 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)