2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.17 1999/11/30 16:10:11 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, 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 }
90 'forall' { ITforall } -- GHC extension keywords
91 'foreign' { ITforeign }
94 'dynamic' { ITdynamic }
96 'stdcall' { ITstdcallconv }
97 'ccall' { ITccallconv }
98 '_ccall_' { ITccall (False, False, False) }
99 '_ccall_GC_' { ITccall (False, False, True) }
100 '_casm_' { ITccall (False, True, False) }
101 '_casm_GC_' { ITccall (False, True, True) }
103 '{-# SPECIALISE' { ITspecialise_prag }
104 '{-# SOURCE' { ITsource_prag }
105 '{-# INLINE' { ITinline_prag }
106 '{-# NOINLINE' { ITnoinline_prag }
107 '{-# RULES' { ITrules_prag }
108 '#-}' { ITclose_prag }
111 '__interface' { ITinterface } -- interface keywords
112 '__export' { IT__export }
113 '__instimport' { ITinstimport }
114 '__forall' { IT__forall }
115 '__letrec' { ITletrec }
116 '__coerce' { ITcoerce }
117 '__depends' { ITdepends }
118 '__inline' { ITinline }
119 '__DEFAULT' { ITdefaultbranch }
121 '__integer' { ITinteger_lit }
122 '__float' { ITfloat_lit }
123 '__rational' { ITrational_lit }
124 '__addr' { ITaddr_lit }
125 '__litlit' { ITlit_lit }
126 '__string' { ITstring_lit }
127 '__ccall' { ITccall $$ }
129 '__sccC' { ITsccAllCafs }
132 '__P' { ITspecialise }
134 '__U' { ITunfold $$ }
135 '__S' { ITstrict $$ }
136 '__M' { ITcprinfo $$ }
139 '..' { ITdotdot } -- reserved symbols
153 '/\\' { ITbiglam } -- GHC-extension symbols
155 '{' { ITocurly } -- special symbols
157 vccurly { ITvccurly } -- virtual close curly (from layout)
168 VARID { ITvarid $$ } -- identifiers
170 VARSYM { ITvarsym $$ }
171 CONSYM { ITconsym $$ }
172 QVARID { ITqvarid $$ }
173 QCONID { ITqconid $$ }
174 QVARSYM { ITqvarsym $$ }
175 QCONSYM { ITqconsym $$ }
177 PRAGMA { ITpragma $$ }
180 STRING { ITstring $$ }
181 INTEGER { ITinteger $$ }
182 RATIONAL { ITrational $$ }
184 PRIMCHAR { ITprimchar $$ }
185 PRIMSTRING { ITprimstring $$ }
186 PRIMINTEGER { ITprimint $$ }
187 PRIMFLOAT { ITprimfloat $$ }
188 PRIMDOUBLE { ITprimdouble $$ }
189 CLITLIT { ITlitlit $$ }
191 UNKNOWN { ITunknown $$ }
193 %monad { P } { thenP } { returnP }
194 %lexer { lexer } { ITeof }
199 -----------------------------------------------------------------------------
202 module :: { RdrNameHsModule }
203 : srcloc 'module' modid maybeexports 'where' body
204 { HsModule $3 Nothing $4 (fst $6) (snd $6) $1 }
206 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) $1 }
208 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
210 | layout_on top close { $2 }
212 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
213 : importdecls ';' cvtopdecls { (reverse $1,$3) }
214 | importdecls { (reverse $1,[]) }
215 | cvtopdecls { ([],$1) }
217 cvtopdecls :: { [RdrNameHsDecl] }
218 : topdecls { cvTopDecls (groupBindings $1)}
220 -----------------------------------------------------------------------------
223 maybeexports :: { Maybe [RdrNameIE] }
224 : '(' exportlist ')' { Just $2 }
225 | {- empty -} { Nothing }
227 exportlist :: { [RdrNameIE] }
228 : exportlist ',' export { $3 : $1 }
229 | exportlist ',' { $1 }
233 -- GHC extension: we allow things like [] and (,,,) to be exported
234 export :: { RdrNameIE }
236 | gtycon { IEThingAbs $1 }
237 | gtycon '(' '..' ')' { IEThingAll $1 }
238 | gtycon '(' ')' { IEThingWith $1 [] }
239 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
240 | 'module' modid { IEModuleContents $2 }
242 qcnames :: { [RdrName] }
243 : qcnames ',' qcname { $3 : $1 }
246 qcname :: { RdrName }
250 -----------------------------------------------------------------------------
251 -- Import Declarations
253 -- import decls can be *empty*, or even just a string of semicolons
254 -- whereas topdecls must contain at least one topdecl.
256 importdecls :: { [RdrNameImportDecl] }
257 : importdecls ';' importdecl { $3 : $1 }
258 | importdecls ';' { $1 }
259 | importdecl { [ $1 ] }
262 importdecl :: { RdrNameImportDecl }
263 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
264 { ImportDecl (mkSrcModuleFS $5) $3 $4 $6 $7 $2 }
266 maybe_src :: { WhereFrom }
267 : '{-# SOURCE' '#-}' { ImportByUserSource }
268 | {- empty -} { ImportByUser }
270 optqualified :: { Bool }
271 : 'qualified' { True }
272 | {- empty -} { False }
274 maybeas :: { Maybe ModuleName }
275 : 'as' modid { Just $2 }
276 | {- empty -} { Nothing }
278 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
279 : impspec { Just $1 }
280 | {- empty -} { Nothing }
282 impspec :: { (Bool, [RdrNameIE]) }
283 : '(' exportlist ')' { (False, reverse $2) }
284 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
286 -----------------------------------------------------------------------------
287 -- Fixity Declarations
291 | INTEGER {% checkPrec $1 `thenP_`
292 returnP (fromInteger $1) }
294 infix :: { FixityDirection }
296 | 'infixl' { InfixL }
297 | 'infixr' { InfixR }
300 : ops ',' op { $3 : $1 }
303 -----------------------------------------------------------------------------
304 -- Top-Level Declarations
306 topdecls :: { [RdrBinding] }
307 : topdecls ';' topdecl { ($3 : $1) }
308 | topdecls ';' { $1 }
311 topdecl :: { RdrBinding }
312 : srcloc 'type' simpletype '=' type
313 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
315 | srcloc 'data' ctype '=' constrs deriving
316 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
317 returnP (RdrHsDecl (TyClD
318 (TyData DataType cs c ts (reverse $5) $6
319 NoDataPragmas $1))) }
321 | srcloc 'newtype' ctype '=' newconstr deriving
322 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
323 returnP (RdrHsDecl (TyClD
324 (TyData NewType cs c ts [$5] $6
325 NoDataPragmas $1))) }
327 | srcloc 'class' ctype fds where
328 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
330 = cvMonoBindsAndSigs cvClassOpSig
333 returnP (RdrHsDecl (TyClD
334 (mkClassDecl cs c ts $4 sigs binds
335 NoClassPragmas $1))) }
337 | srcloc 'instance' inst_type where
339 = cvMonoBindsAndSigs cvInstDeclSig
342 (InstDecl $3 binds sigs dummyRdrVarName $1)) }
344 | srcloc 'default' '(' types0 ')'
345 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
347 | srcloc 'foreign' 'import' callconv ext_name
348 unsafe_flag varid_no_unsafe '::' sigtype
349 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
351 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
352 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
354 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
355 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
356 defaultCallConv $1)) }
360 decls :: { [RdrBinding] }
361 : decls ';' decl { $3 : $1 }
366 decl :: { RdrBinding }
369 | valdef { RdrValBinding $1 }
370 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
371 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
372 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
373 { foldr1 RdrAndBindings
374 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
375 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
376 { RdrSig (SpecInstSig $4 $2) }
377 | '{-# RULES' rules '#-}' { $2 }
379 opt_phase :: { Maybe Int }
380 : INTEGER { Just (fromInteger $1) }
381 | {- empty -} { Nothing }
383 sigtypes :: { [RdrNameHsType] }
385 | sigtypes ',' sigtype { $3 : $1 }
387 wherebinds :: { RdrNameHsBinds }
388 : where { cvBinds cvValSig (groupBindings $1) }
390 where :: { [RdrBinding] }
391 : 'where' decllist { $2 }
394 declbinds :: { RdrNameHsBinds }
395 : decllist { cvBinds cvValSig (groupBindings $1) }
397 decllist :: { [RdrBinding] }
398 : '{' decls '}' { $2 }
399 | layout_on decls close { $2 }
401 fixdecl :: { RdrBinding }
402 : srcloc infix prec ops { foldr1 RdrAndBindings
403 [ RdrSig (FixSig (FixitySig n
407 signdecl :: { RdrBinding }
408 : vars srcloc '::' sigtype { foldr1 RdrAndBindings
409 [ RdrSig (Sig n $4 $2) | n <- $1 ] }
411 sigtype :: { RdrNameHsType }
412 : ctype { mkHsForAllTy Nothing [] $1 }
415 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
416 instead of qvar, we get another shift/reduce-conflict. Consider the
419 { (+) :: ... } only var
420 { (+) x y = ... } could (incorrectly) be qvar
422 We re-use expressions for patterns, so a qvar would be allowed in patterns
423 instead of a var only (which would be correct). But deciding what the + is,
424 would require more lookahead. So let's check for ourselves...
427 vars :: { [RdrName] }
428 : vars ',' var { $3 : $1 }
431 -----------------------------------------------------------------------------
432 -- Transformation Rules
434 rules :: { RdrBinding }
435 : rules ';' rule { $1 `RdrAndBindings` $3 }
438 | {- empty -} { RdrNullBind }
440 rule :: { RdrBinding }
441 : STRING rule_forall fexp '=' srcloc exp
442 { RdrHsDecl (RuleD (RuleDecl $1 [] $2 $3 $6 $5)) }
444 rule_forall :: { [RdrNameRuleBndr] }
445 : 'forall' rule_var_list '.' { $2 }
448 rule_var_list :: { [RdrNameRuleBndr] }
450 | rule_var rule_var_list { $1 : $2 }
452 rule_var :: { RdrNameRuleBndr }
453 : varid { RuleBndr $1 }
454 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
456 -----------------------------------------------------------------------------
457 -- Foreign import/export
460 : 'stdcall' { stdCallConv }
461 | 'ccall' { cCallConv }
462 | {- empty -} { defaultCallConv }
464 unsafe_flag :: { Bool }
466 | {- empty -} { False }
468 ext_name :: { Maybe ExtName }
469 : 'dynamic' { Just Dynamic }
470 | STRING { Just (ExtName $1 Nothing) }
471 | STRING STRING { Just (ExtName $2 (Just $1)) }
472 | {- empty -} { Nothing }
474 -----------------------------------------------------------------------------
477 -- A ctype is a for-all type
478 ctype :: { RdrNameHsType }
479 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
480 | context type { mkHsForAllTy Nothing $1 $2 }
481 -- A type of form (context => type) is an *implicit* HsForAllTy
484 type :: { RdrNameHsType }
485 : btype '->' type { MonoFunTy $1 $3 }
488 btype :: { RdrNameHsType }
489 : btype atype { MonoTyApp $1 $2 }
492 atype :: { RdrNameHsType }
493 : gtycon { MonoTyVar $1 }
494 | tyvar { MonoTyVar $1 }
495 | '(' type ',' types ')' { MonoTupleTy ($2 : reverse $4) True }
496 | '(#' types '#)' { MonoTupleTy (reverse $2) False }
497 | '[' type ']' { MonoListTy $2 }
498 | '(' ctype ')' { $2 }
500 gtycon :: { RdrName }
502 | '(' ')' { unitTyCon_RDR }
503 | '(' '->' ')' { funTyCon_RDR }
504 | '[' ']' { listTyCon_RDR }
505 | '(' commas ')' { tupleTyCon_RDR $2 }
507 -- An inst_type is what occurs in the head of an instance decl
508 -- e.g. (Foo a, Gaz b) => Wibble a b
509 -- It's kept as a single type, with a MonoDictTy at the right
510 -- hand corner, for convenience.
511 inst_type :: { RdrNameHsType }
512 : ctype {% checkInstType $1 }
514 types0 :: { [RdrNameHsType] }
518 types :: { [RdrNameHsType] }
520 | types ',' type { $3 : $1 }
522 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
523 : tycon tyvars { ($1, reverse $2) }
525 tyvars :: { [RdrNameHsTyVar] }
526 : tyvars tyvar { UserTyVar $2 : $1 }
529 fds :: { [([RdrName], [RdrName])] }
531 | '|' fds1 { reverse $2 }
533 fds1 :: { [([RdrName], [RdrName])] }
534 : fds1 ',' fd { $3 : $1 }
537 fd :: { ([RdrName], [RdrName]) }
538 : varids0 '->' varids0 { (reverse $1, reverse $3) }
540 varids0 :: { [RdrName] }
542 | varids0 tyvar { $2 : $1 }
544 -----------------------------------------------------------------------------
545 -- Datatype declarations
547 constrs :: { [RdrNameConDecl] }
548 : constrs '|' constr { $3 : $1 }
551 constr :: { RdrNameConDecl }
552 : srcloc forall context constr_stuff
553 { ConDecl (fst $4) $2 $3 (snd $4) $1 }
554 | srcloc forall constr_stuff
555 { ConDecl (fst $3) $2 [] (snd $3) $1 }
557 forall :: { [RdrNameHsTyVar] }
558 : 'forall' tyvars '.' { $2 }
561 context :: { RdrNameContext }
562 : btype '=>' {% checkContext $1 }
564 constr_stuff :: { (RdrName, RdrNameConDetails) }
565 : scontype { (fst $1, VanillaCon (snd $1)) }
566 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
567 | con '{' fielddecls '}' { ($1, RecCon (reverse $3)) }
569 newconstr :: { RdrNameConDecl }
570 : srcloc conid atype { ConDecl $2 [] [] (NewCon $3 Nothing) $1 }
571 | srcloc conid '{' var '::' type '}'
572 { ConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
574 scontype :: { (RdrName, [RdrNameBangType]) }
575 : btype {% splitForConApp $1 [] }
578 scontype1 :: { (RdrName, [RdrNameBangType]) }
579 : btype '!' atype {% splitForConApp $1 [Banged $3] }
580 | scontype1 satype { (fst $1, snd $1 ++ [$2] ) }
582 satype :: { RdrNameBangType }
583 : atype { Unbanged $1 }
584 | '!' atype { Banged $2 }
586 sbtype :: { RdrNameBangType }
587 : btype { Unbanged $1 }
588 | '!' atype { Banged $2 }
590 fielddecls :: { [([RdrName],RdrNameBangType)] }
591 : fielddecls ',' fielddecl { $3 : $1 }
594 fielddecl :: { ([RdrName],RdrNameBangType) }
595 : vars '::' stype { (reverse $1, $3) }
597 stype :: { RdrNameBangType }
598 : ctype { Unbanged $1 }
599 | '!' atype { Banged $2 }
601 deriving :: { Maybe [RdrName] }
602 : {- empty -} { Nothing }
603 | 'deriving' qtycls { Just [$2] }
604 | 'deriving' '(' ')' { Just [] }
605 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
607 dclasses :: { [RdrName] }
608 : dclasses ',' qtycls { $3 : $1 }
611 -----------------------------------------------------------------------------
614 valdef :: { RdrNameMonoBinds }
615 : infixexp {-ToDo: opt_sig-} srcloc rhs
616 {% checkValDef $1 Nothing $3 $2 }
618 rhs :: { RdrNameGRHSs }
619 : '=' srcloc exp wherebinds { GRHSs (unguardedRHS $3 $2)
621 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
623 gdrhs :: { [RdrNameGRHS] }
624 : gdrhs gdrh { $2 : $1 }
627 gdrh :: { RdrNameGRHS }
628 : '|' srcloc quals '=' exp { GRHS (reverse
629 (ExprStmt $5 $2 : $3)) $2 }
631 -----------------------------------------------------------------------------
634 exp :: { RdrNameHsExpr }
635 : infixexp '::' sigtype { ExprWithTySig $1 $3 }
638 infixexp :: { RdrNameHsExpr }
640 | infixexp qop exp10 { OpApp $1 $2 (panic "fixity") $3 }
642 exp10 :: { RdrNameHsExpr }
643 : '\\' aexp aexps opt_asig '->' srcloc exp
644 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
645 returnP (HsLam (Match [] ps $4
646 (GRHSs (unguardedRHS $7 $6)
647 EmptyBinds Nothing))) }
648 | 'let' declbinds 'in' exp { HsLet $2 $4 }
649 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
650 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
651 | '-' fexp { NegApp $2 (error "NegApp") }
652 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
654 | '_ccall_' ccallid aexps0 { CCall $2 $3 False False cbot }
655 | '_ccall_GC_' ccallid aexps0 { CCall $2 $3 True False cbot }
656 | '_casm_' CLITLIT aexps0 { CCall $2 $3 False True cbot }
657 | '_casm_GC_' CLITLIT aexps0 { CCall $2 $3 True True cbot }
659 | '_scc_' STRING exp { if opt_SccProfilingOn
665 ccallid :: { FAST_STRING }
669 fexp :: { RdrNameHsExpr }
670 : fexp aexp { HsApp $1 $2 }
673 aexps0 :: { [RdrNameHsExpr] }
674 : aexps { reverse $1 }
676 aexps :: { [RdrNameHsExpr] }
677 : aexps aexp { $2 : $1 }
680 aexp :: { RdrNameHsExpr }
681 : aexp '{' fbinds '}' {% mkRecConstrOrUpdate $1 (reverse $3) }
684 aexp1 :: { RdrNameHsExpr }
687 | literal { HsLit $1 }
688 | '(' exp ')' { HsPar $2 }
689 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) True }
690 | '(#' texps '#)' { ExplicitTuple (reverse $2) False }
691 | '[' list ']' { $2 }
692 | '(' infixexp qop ')' { SectionL $2 $3 }
693 | '(' qopm infixexp ')' { SectionR $2 $3 }
694 | qvar '@' aexp { EAsPat $1 $3 }
696 | '~' aexp1 { ELazyPat $2 }
699 : commas ',' { $1 + 1 }
702 texps :: { [RdrNameHsExpr] }
703 : texps ',' exp { $3 : $1 }
706 -----------------------------------------------------------------------------
709 -- The rules below are little bit contorted to keep lexps left-recursive while
710 -- avoiding another shift/reduce-conflict.
712 list :: { RdrNameHsExpr }
713 : exp { ExplicitList [$1] }
714 | lexps { ExplicitList (reverse $1) }
715 | exp '..' { ArithSeqIn (From $1) }
716 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
717 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
718 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
719 | exp srcloc '|' quals { HsDo ListComp (reverse
720 (ReturnStmt $1 : $4)) $2 }
722 lexps :: { [RdrNameHsExpr] }
723 : lexps ',' exp { $3 : $1 }
724 | exp ',' exp { [$3,$1] }
726 -----------------------------------------------------------------------------
727 -- List Comprehensions
729 quals :: { [RdrNameStmt] }
730 : quals ',' qual { $3 : $1 }
733 qual :: { RdrNameStmt }
734 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
735 returnP (BindStmt p $4 $1) }
736 | srcloc exp { GuardStmt $2 $1 }
737 | srcloc 'let' declbinds { LetStmt $3 }
739 -----------------------------------------------------------------------------
742 altslist :: { [RdrNameMatch] }
743 : '{' alts '}' { reverse $2 }
744 | layout_on alts close { reverse $2 }
747 alts :: { [RdrNameMatch] }
748 : alts ';' alt { $3 : $1 }
753 alt :: { RdrNameMatch }
754 : infixexp opt_sig ralt wherebinds
755 {% checkPattern $1 `thenP` \p ->
756 returnP (Match [] [p] $2
757 (GRHSs $3 $4 Nothing)) }
759 opt_sig :: { Maybe RdrNameHsType }
760 : {- empty -} { Nothing }
761 | '::' type { Just $2 }
763 opt_asig :: { Maybe RdrNameHsType }
764 : {- empty -} { Nothing }
765 | '::' atype { Just $2 }
767 ralt :: { [RdrNameGRHS] }
768 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
769 | gdpats { (reverse $1) }
771 gdpats :: { [RdrNameGRHS] }
772 : gdpats gdpat { $2 : $1 }
775 gdpat :: { RdrNameGRHS }
776 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
778 -----------------------------------------------------------------------------
779 -- Statement sequences
781 stmtlist :: { [RdrNameStmt] }
782 : '{' stmts '}' { reverse $2 }
783 | layout_on_for_do stmts close { reverse $2 }
785 -- Stmt list should really end in an expression, but it's not
786 -- convenient to enforce this here, so we throw out erroneous
787 -- statement sequences in the renamer instead.
789 stmts :: { [RdrNameStmt] }
793 stmts1 :: { [RdrNameStmt] }
794 : stmts1 ';' stmt { $3 : $1 }
798 stmt :: { RdrNameStmt }
799 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
800 returnP (BindStmt p $4 $1) }
801 | srcloc exp { ExprStmt $2 $1 }
802 | srcloc 'let' declbinds { LetStmt $3 }
804 -----------------------------------------------------------------------------
805 -- Record Field Update/Construction
807 fbinds :: { RdrNameHsRecordBinds }
808 : fbinds ',' fbind { $3 : $1 }
813 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
814 : qvar '=' exp { ($1,$3,False) }
816 -----------------------------------------------------------------------------
817 -- Variables, Constructors and Operators.
820 : '(' ')' { unitCon_RDR }
821 | '[' ']' { nilCon_RDR }
822 | '(' commas ')' { tupleCon_RDR $2 }
827 | '(' varsym ')' { $2 }
831 | '(' qvarsym ')' { $2 }
835 | '(' consym ')' { $2 }
839 | '(' qconsym ')' { $2 }
843 | '`' varid '`' { $2 }
845 qvarop :: { RdrName }
847 | '`' qvarid '`' { $2 }
849 qvaropm :: { RdrName }
851 | '`' qvarid '`' { $2 }
855 | '`' conid '`' { $2 }
857 qconop :: { RdrName }
859 | '`' qconid '`' { $2 }
861 -----------------------------------------------------------------------------
864 op :: { RdrName } -- used in infix decls
868 qop :: { RdrNameHsExpr } -- used in sections
869 : qvarop { HsVar $1 }
870 | qconop { HsVar $1 }
872 qopm :: { RdrNameHsExpr } -- used in sections
873 : qvaropm { HsVar $1 }
874 | qconop { HsVar $1 }
876 -----------------------------------------------------------------------------
879 qvarid :: { RdrName }
881 | QVARID { case $1 of { (mod,n) ->
882 mkSrcQual varName mod n } }
885 : VARID { mkSrcUnqual varName $1 }
886 | 'as' { as_var_RDR }
887 | 'qualified' { qualified_var_RDR }
888 | 'hiding' { hiding_var_RDR }
889 | 'forall' { forall_var_RDR }
890 | 'export' { export_var_RDR }
891 | 'label' { label_var_RDR }
892 | 'dynamic' { dynamic_var_RDR }
893 | 'unsafe' { unsafe_var_RDR }
894 | 'stdcall' { stdcall_var_RDR }
895 | 'ccall' { ccall_var_RDR }
897 varid_no_unsafe :: { RdrName }
898 : VARID { mkSrcUnqual varName $1 }
899 | 'as' { as_var_RDR }
900 | 'qualified' { qualified_var_RDR }
901 | 'hiding' { hiding_var_RDR }
902 | 'forall' { forall_var_RDR }
903 | 'export' { export_var_RDR }
904 | 'label' { label_var_RDR }
905 | 'dynamic' { dynamic_var_RDR }
906 | 'stdcall' { stdcall_var_RDR }
907 | 'ccall' { ccall_var_RDR }
909 -----------------------------------------------------------------------------
912 qconid :: { RdrName }
914 | QCONID { case $1 of { (mod,n) ->
915 mkSrcQual dataName mod n } }
918 : CONID { mkSrcUnqual dataName $1 }
920 -----------------------------------------------------------------------------
923 qconsym :: { RdrName }
925 | QCONSYM { case $1 of { (mod,n) ->
926 mkSrcQual dataName mod n } }
928 consym :: { RdrName }
929 : CONSYM { mkSrcUnqual dataName $1 }
931 -----------------------------------------------------------------------------
934 qvarsym :: { RdrName }
938 qvarsymm :: { RdrName }
942 varsym :: { RdrName }
943 : VARSYM { mkSrcUnqual varName $1 }
948 varsymm :: { RdrName } -- varsym not including '-'
949 : VARSYM { mkSrcUnqual varName $1 }
953 qvarsym1 :: { RdrName }
954 : QVARSYM { case $1 of { (mod,n) ->
955 mkSrcQual varName mod n } }
958 : INTEGER { HsInt $1 }
960 | RATIONAL { HsFrac $1 }
961 | STRING { HsString $1 }
963 | PRIMINTEGER { HsIntPrim $1 }
964 | PRIMCHAR { HsCharPrim $1 }
965 | PRIMSTRING { HsStringPrim $1 }
966 | PRIMFLOAT { HsFloatPrim $1 }
967 | PRIMDOUBLE { HsDoublePrim $1 }
968 | CLITLIT { HsLitLit $1 }
970 srcloc :: { SrcLoc } : {% getSrcLocP }
972 -----------------------------------------------------------------------------
976 : vccurly { () } -- context popped in lexer.
977 | error {% popContext }
979 layout_on :: { () } : {% layoutOn True{-strict-} }
980 layout_on_for_do :: { () } : {% layoutOn False }
982 -----------------------------------------------------------------------------
983 -- Miscellaneous (mostly renamings)
985 modid :: { ModuleName }
986 : CONID { mkSrcModuleFS $1 }
989 : CONID { mkSrcUnqual tcClsName $1 }
991 qtycon :: { RdrName }
993 | QCONID { case $1 of { (mod,n) ->
994 mkSrcQual tcClsName mod n } }
996 qtycls :: { RdrName }
1000 : VARID { mkSrcUnqual tvName $1 }
1001 | 'as' { as_tyvar_RDR }
1002 | 'qualified' { qualified_tyvar_RDR }
1003 | 'hiding' { hiding_tyvar_RDR }
1004 | 'export' { export_tyvar_RDR }
1005 | 'label' { label_tyvar_RDR }
1006 | 'dynamic' { dynamic_tyvar_RDR }
1007 | 'unsafe' { unsafe_tyvar_RDR }
1008 | 'stdcall' { stdcall_tyvar_RDR }
1009 | 'ccall' { ccall_tyvar_RDR }
1010 -- NOTE: no 'forall'
1012 -----------------------------------------------------------------------------
1016 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)