2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.26 2000/02/28 21:59:32 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 '{-# DEPRECATED' { ITdeprecated_prag }
111 '#-}' { ITclose_prag }
114 '__interface' { ITinterface } -- interface keywords
115 '__export' { IT__export }
116 '__instimport' { ITinstimport }
117 '__forall' { IT__forall }
118 '__letrec' { ITletrec }
119 '__coerce' { ITcoerce }
120 '__depends' { ITdepends }
121 '__inline' { ITinline }
122 '__DEFAULT' { ITdefaultbranch }
124 '__integer' { ITinteger_lit }
125 '__float' { ITfloat_lit }
126 '__rational' { ITrational_lit }
127 '__addr' { ITaddr_lit }
128 '__litlit' { ITlit_lit }
129 '__string' { ITstring_lit }
130 '__ccall' { ITccall $$ }
132 '__sccC' { ITsccAllCafs }
135 '__P' { ITspecialise }
137 '__U' { ITunfold $$ }
138 '__S' { ITstrict $$ }
139 '__M' { ITcprinfo $$ }
142 '..' { ITdotdot } -- reserved symbols
156 '/\\' { ITbiglam } -- GHC-extension symbols
158 '{' { ITocurly } -- special symbols
160 vccurly { ITvccurly } -- virtual close curly (from layout)
171 VARID { ITvarid $$ } -- identifiers
173 VARSYM { ITvarsym $$ }
174 CONSYM { ITconsym $$ }
175 QVARID { ITqvarid $$ }
176 QCONID { ITqconid $$ }
177 QVARSYM { ITqvarsym $$ }
178 QCONSYM { ITqconsym $$ }
180 IPVARID { ITipvarid $$ } -- GHC extension
182 PRAGMA { ITpragma $$ }
185 STRING { ITstring $$ }
186 INTEGER { ITinteger $$ }
187 RATIONAL { ITrational $$ }
189 PRIMCHAR { ITprimchar $$ }
190 PRIMSTRING { ITprimstring $$ }
191 PRIMINTEGER { ITprimint $$ }
192 PRIMFLOAT { ITprimfloat $$ }
193 PRIMDOUBLE { ITprimdouble $$ }
194 CLITLIT { ITlitlit $$ }
196 UNKNOWN { ITunknown $$ }
198 %monad { P } { thenP } { returnP }
199 %lexer { lexer } { ITeof }
204 -----------------------------------------------------------------------------
207 -- The place for module deprecation is really too restrictive, but if it
208 -- was allowed at its natural place just before 'module', we get an ugly
209 -- s/r conflict with the second alternative. Another solution would be the
210 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
211 -- either, and DEPRECATED is only expected to be used by people who really
212 -- know what they are doing. :-)
214 module :: { RdrNameHsModule }
215 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
216 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
218 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
220 maybemoddeprec :: { Maybe DeprecTxt }
221 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
222 | {- empty -} { Nothing }
224 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
226 | layout_on top close { $2 }
228 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
229 : importdecls ';' cvtopdecls { (reverse $1,$3) }
230 | importdecls { (reverse $1,[]) }
231 | cvtopdecls { ([],$1) }
233 cvtopdecls :: { [RdrNameHsDecl] }
234 : topdecls { cvTopDecls (groupBindings $1)}
236 -----------------------------------------------------------------------------
239 maybeexports :: { Maybe [RdrNameIE] }
240 : '(' exportlist ')' { Just $2 }
241 | {- empty -} { Nothing }
243 exportlist :: { [RdrNameIE] }
244 : exportlist ',' export { $3 : $1 }
245 | exportlist ',' { $1 }
249 -- GHC extension: we allow things like [] and (,,,) to be exported
250 export :: { RdrNameIE }
252 | gtycon { IEThingAbs $1 }
253 | gtycon '(' '..' ')' { IEThingAll $1 }
254 | gtycon '(' ')' { IEThingWith $1 [] }
255 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
256 | 'module' modid { IEModuleContents $2 }
258 qcnames :: { [RdrName] }
259 : qcnames ',' qcname { $3 : $1 }
262 qcname :: { RdrName }
266 -----------------------------------------------------------------------------
267 -- Import Declarations
269 -- import decls can be *empty*, or even just a string of semicolons
270 -- whereas topdecls must contain at least one topdecl.
272 importdecls :: { [RdrNameImportDecl] }
273 : importdecls ';' importdecl { $3 : $1 }
274 | importdecls ';' { $1 }
275 | importdecl { [ $1 ] }
278 importdecl :: { RdrNameImportDecl }
279 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
280 { ImportDecl (mkSrcModuleFS $5) $3 $4 $6 $7 $2 }
282 maybe_src :: { WhereFrom }
283 : '{-# SOURCE' '#-}' { ImportByUserSource }
284 | {- empty -} { ImportByUser }
286 optqualified :: { Bool }
287 : 'qualified' { True }
288 | {- empty -} { False }
290 maybeas :: { Maybe ModuleName }
291 : 'as' modid { Just $2 }
292 | {- empty -} { Nothing }
294 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
295 : impspec { Just $1 }
296 | {- empty -} { Nothing }
298 impspec :: { (Bool, [RdrNameIE]) }
299 : '(' exportlist ')' { (False, reverse $2) }
300 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
302 -----------------------------------------------------------------------------
303 -- Fixity Declarations
307 | INTEGER {% checkPrec $1 `thenP_`
308 returnP (fromInteger $1) }
310 infix :: { FixityDirection }
312 | 'infixl' { InfixL }
313 | 'infixr' { InfixR }
316 : ops ',' op { $3 : $1 }
319 -----------------------------------------------------------------------------
320 -- Top-Level Declarations
322 topdecls :: { [RdrBinding] }
323 : topdecls ';' topdecl { ($3 : $1) }
324 | topdecls ';' { $1 }
327 topdecl :: { RdrBinding }
328 : srcloc 'type' simpletype '=' type
329 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
331 | srcloc 'data' ctype '=' constrs deriving
332 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
333 returnP (RdrHsDecl (TyClD
334 (TyData DataType cs c ts (reverse $5) $6
335 NoDataPragmas $1))) }
337 | srcloc 'newtype' ctype '=' newconstr deriving
338 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
339 returnP (RdrHsDecl (TyClD
340 (TyData NewType cs c ts [$5] $6
341 NoDataPragmas $1))) }
343 | srcloc 'class' ctype fds where
344 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
346 = cvMonoBindsAndSigs cvClassOpSig
349 returnP (RdrHsDecl (TyClD
350 (mkClassDecl cs c ts $4 sigs binds
351 NoClassPragmas $1))) }
353 | srcloc 'instance' inst_type where
355 = cvMonoBindsAndSigs cvInstDeclSig
358 (InstDecl $3 binds sigs dummyRdrVarName $1)) }
360 | srcloc 'default' '(' types0 ')'
361 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
363 | srcloc 'foreign' 'import' callconv ext_name
364 unsafe_flag varid_no_unsafe '::' sigtype
365 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
367 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
368 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
370 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
371 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
372 defaultCallConv $1)) }
376 decls :: { [RdrBinding] }
377 : decls ';' decl { $3 : $1 }
382 decl :: { RdrBinding }
385 | valdef { RdrValBinding $1 }
386 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
387 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
388 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
389 { foldr1 RdrAndBindings
390 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
391 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
392 { RdrSig (SpecInstSig $4 $2) }
393 | '{-# RULES' rules '#-}' { $2 }
394 | '{-# DEPRECATED' deprecations '#-}' { $2 }
396 opt_phase :: { Maybe Int }
397 : INTEGER { Just (fromInteger $1) }
398 | {- empty -} { Nothing }
400 sigtypes :: { [RdrNameHsType] }
402 | sigtypes ',' sigtype { $3 : $1 }
404 wherebinds :: { RdrNameHsBinds }
405 : where { cvBinds cvValSig (groupBindings $1) }
407 where :: { [RdrBinding] }
408 : 'where' decllist { $2 }
411 declbinds :: { RdrNameHsBinds }
412 : decllist { cvBinds cvValSig (groupBindings $1) }
414 decllist :: { [RdrBinding] }
415 : '{' decls '}' { $2 }
416 | layout_on decls close { $2 }
418 fixdecl :: { RdrBinding }
419 : srcloc infix prec ops { foldr1 RdrAndBindings
420 [ RdrSig (FixSig (FixitySig n
424 signdecl :: { RdrBinding }
425 : vars srcloc '::' sigtype { foldr1 RdrAndBindings
426 [ RdrSig (Sig n $4 $2) | n <- $1 ] }
428 sigtype :: { RdrNameHsType }
429 : ctype { mkHsForAllTy Nothing [] $1 }
432 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
433 instead of qvar, we get another shift/reduce-conflict. Consider the
436 { (+) :: ... } only var
437 { (+) x y = ... } could (incorrectly) be qvar
439 We re-use expressions for patterns, so a qvar would be allowed in patterns
440 instead of a var only (which would be correct). But deciding what the + is,
441 would require more lookahead. So let's check for ourselves...
444 vars :: { [RdrName] }
445 : vars ',' var { $3 : $1 }
448 -----------------------------------------------------------------------------
449 -- Transformation Rules
451 rules :: { RdrBinding }
452 : rules ';' rule { $1 `RdrAndBindings` $3 }
455 | {- empty -} { RdrNullBind }
457 rule :: { RdrBinding }
458 : STRING rule_forall fexp '=' srcloc exp
459 { RdrHsDecl (RuleD (RuleDecl $1 [] $2 $3 $6 $5)) }
461 rule_forall :: { [RdrNameRuleBndr] }
462 : 'forall' rule_var_list '.' { $2 }
465 rule_var_list :: { [RdrNameRuleBndr] }
467 | rule_var rule_var_list { $1 : $2 }
469 rule_var :: { RdrNameRuleBndr }
470 : varid { RuleBndr $1 }
471 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
473 -----------------------------------------------------------------------------
476 deprecations :: { RdrBinding }
477 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
478 | deprecations ';' { $1 }
480 | {- empty -} { RdrNullBind }
482 -- SUP: TEMPORARY HACK, not checking for `module Foo'
483 deprecation :: { RdrBinding }
484 : srcloc exportlist STRING
485 { foldr1 RdrAndBindings [ RdrSig (DeprecSig (Deprecation n $3) $1) | n <- $2 ] }
487 -----------------------------------------------------------------------------
488 -- Foreign import/export
491 : 'stdcall' { stdCallConv }
492 | 'ccall' { cCallConv }
493 | {- empty -} { defaultCallConv }
495 unsafe_flag :: { Bool }
497 | {- empty -} { False }
499 ext_name :: { Maybe ExtName }
500 : 'dynamic' { Just Dynamic }
501 | STRING { Just (ExtName $1 Nothing) }
502 | STRING STRING { Just (ExtName $2 (Just $1)) }
503 | {- empty -} { Nothing }
505 -----------------------------------------------------------------------------
508 -- A ctype is a for-all type
509 ctype :: { RdrNameHsType }
510 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
511 | context type { mkHsForAllTy Nothing $1 $2 }
512 -- A type of form (context => type) is an *implicit* HsForAllTy
515 type :: { RdrNameHsType }
516 : btype '->' type { MonoFunTy $1 $3 }
517 | IPVARID '::' type { MonoIParamTy (mkSrcUnqual ipName $1) $3 }
520 btype :: { RdrNameHsType }
521 : btype atype { MonoTyApp $1 $2 }
524 atype :: { RdrNameHsType }
525 : gtycon { MonoTyVar $1 }
526 | tyvar { MonoTyVar $1 }
527 | '(' type ',' types ')' { MonoTupleTy ($2 : reverse $4) True }
528 | '(#' types '#)' { MonoTupleTy (reverse $2) False }
529 | '[' type ']' { MonoListTy $2 }
530 | '(' ctype ')' { $2 }
532 gtycon :: { RdrName }
534 | '(' ')' { unitTyCon_RDR }
535 | '(' '->' ')' { funTyCon_RDR }
536 | '[' ']' { listTyCon_RDR }
537 | '(' commas ')' { tupleTyCon_RDR $2 }
539 -- An inst_type is what occurs in the head of an instance decl
540 -- e.g. (Foo a, Gaz b) => Wibble a b
541 -- It's kept as a single type, with a MonoDictTy at the right
542 -- hand corner, for convenience.
543 inst_type :: { RdrNameHsType }
544 : ctype {% checkInstType $1 }
546 types0 :: { [RdrNameHsType] }
550 types :: { [RdrNameHsType] }
552 | types ',' type { $3 : $1 }
554 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
555 : tycon tyvars { ($1, reverse $2) }
557 tyvars :: { [RdrNameHsTyVar] }
558 : tyvars tyvar { UserTyVar $2 : $1 }
561 fds :: { [([RdrName], [RdrName])] }
563 | '|' fds1 { reverse $2 }
565 fds1 :: { [([RdrName], [RdrName])] }
566 : fds1 ',' fd { $3 : $1 }
569 fd :: { ([RdrName], [RdrName]) }
570 : varids0 '->' varids0 { (reverse $1, reverse $3) }
572 varids0 :: { [RdrName] }
574 | varids0 tyvar { $2 : $1 }
576 -----------------------------------------------------------------------------
577 -- Datatype declarations
579 constrs :: { [RdrNameConDecl] }
580 : constrs '|' constr { $3 : $1 }
583 constr :: { RdrNameConDecl }
584 : srcloc forall context constr_stuff
585 { ConDecl (fst $4) $2 $3 (snd $4) $1 }
586 | srcloc forall constr_stuff
587 { ConDecl (fst $3) $2 [] (snd $3) $1 }
589 forall :: { [RdrNameHsTyVar] }
590 : 'forall' tyvars '.' { $2 }
593 context :: { RdrNameContext }
594 : btype '=>' {% checkContext $1 }
596 constr_stuff :: { (RdrName, RdrNameConDetails) }
597 : scontype { (fst $1, VanillaCon (snd $1)) }
598 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
599 | con '{' fielddecls '}' { ($1, RecCon (reverse $3)) }
601 newconstr :: { RdrNameConDecl }
602 : srcloc conid atype { ConDecl $2 [] [] (NewCon $3 Nothing) $1 }
603 | srcloc conid '{' var '::' type '}'
604 { ConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
606 scontype :: { (RdrName, [RdrNameBangType]) }
607 : btype {% splitForConApp $1 [] }
610 scontype1 :: { (RdrName, [RdrNameBangType]) }
611 : btype '!' atype {% splitForConApp $1 [Banged $3] }
612 | scontype1 satype { (fst $1, snd $1 ++ [$2] ) }
614 satype :: { RdrNameBangType }
615 : atype { Unbanged $1 }
616 | '!' atype { Banged $2 }
618 sbtype :: { RdrNameBangType }
619 : btype { Unbanged $1 }
620 | '!' atype { Banged $2 }
622 fielddecls :: { [([RdrName],RdrNameBangType)] }
623 : fielddecls ',' fielddecl { $3 : $1 }
626 fielddecl :: { ([RdrName],RdrNameBangType) }
627 : vars '::' stype { (reverse $1, $3) }
629 stype :: { RdrNameBangType }
630 : ctype { Unbanged $1 }
631 | '!' atype { Banged $2 }
633 deriving :: { Maybe [RdrName] }
634 : {- empty -} { Nothing }
635 | 'deriving' qtycls { Just [$2] }
636 | 'deriving' '(' ')' { Just [] }
637 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
639 dclasses :: { [RdrName] }
640 : dclasses ',' qtycls { $3 : $1 }
643 -----------------------------------------------------------------------------
646 valdef :: { RdrNameMonoBinds }
647 : infixexp {-ToDo: opt_sig-} srcloc rhs
648 {% checkValDef $1 Nothing $3 $2 }
650 rhs :: { RdrNameGRHSs }
651 : '=' srcloc exp wherebinds { GRHSs (unguardedRHS $3 $2)
653 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
655 gdrhs :: { [RdrNameGRHS] }
656 : gdrhs gdrh { $2 : $1 }
659 gdrh :: { RdrNameGRHS }
660 : '|' srcloc quals '=' exp { GRHS (reverse
661 (ExprStmt $5 $2 : $3)) $2 }
663 -----------------------------------------------------------------------------
666 exp :: { RdrNameHsExpr }
667 : infixexp '::' sigtype { ExprWithTySig $1 $3 }
668 | infixexp 'with' dbinding { HsWith $1 $3 }
671 infixexp :: { RdrNameHsExpr }
673 | infixexp qop exp10 { OpApp $1 $2 (panic "fixity") $3 }
675 exp10 :: { RdrNameHsExpr }
676 : '\\' aexp aexps opt_asig '->' srcloc exp
677 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
678 returnP (HsLam (Match [] ps $4
679 (GRHSs (unguardedRHS $7 $6)
680 EmptyBinds Nothing))) }
681 | 'let' declbinds 'in' exp { HsLet $2 $4 }
682 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
683 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
684 | '-' fexp { NegApp $2 (error "NegApp") }
685 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
687 | '_ccall_' ccallid aexps0 { CCall $2 $3 False False cbot }
688 | '_ccall_GC_' ccallid aexps0 { CCall $2 $3 True False cbot }
689 | '_casm_' CLITLIT aexps0 { CCall $2 $3 False True cbot }
690 | '_casm_GC_' CLITLIT aexps0 { CCall $2 $3 True True cbot }
692 | '_scc_' STRING exp { if opt_SccProfilingOn
698 ccallid :: { FAST_STRING }
702 fexp :: { RdrNameHsExpr }
703 : fexp aexp { HsApp $1 $2 }
706 aexps0 :: { [RdrNameHsExpr] }
707 : aexps { reverse $1 }
709 aexps :: { [RdrNameHsExpr] }
710 : aexps aexp { $2 : $1 }
713 aexp :: { RdrNameHsExpr }
714 : aexp '{' fbinds '}' {% mkRecConstrOrUpdate $1 (reverse $3) }
717 aexp1 :: { RdrNameHsExpr }
719 | IPVARID { HsIPVar (mkSrcUnqual ipName $1) }
721 | literal { HsLit $1 }
722 | '(' exp ')' { HsPar $2 }
723 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) True }
724 | '(#' texps '#)' { ExplicitTuple (reverse $2) False }
725 | '[' list ']' { $2 }
726 | '(' infixexp qop ')' { SectionL $2 $3 }
727 | '(' qopm infixexp ')' { SectionR $2 $3 }
728 | qvar '@' aexp { EAsPat $1 $3 }
730 | '~' aexp1 { ELazyPat $2 }
733 : commas ',' { $1 + 1 }
736 texps :: { [RdrNameHsExpr] }
737 : texps ',' exp { $3 : $1 }
740 -----------------------------------------------------------------------------
743 -- The rules below are little bit contorted to keep lexps left-recursive while
744 -- avoiding another shift/reduce-conflict.
746 list :: { RdrNameHsExpr }
747 : exp { ExplicitList [$1] }
748 | lexps { ExplicitList (reverse $1) }
749 | exp '..' { ArithSeqIn (From $1) }
750 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
751 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
752 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
753 | exp srcloc '|' quals { HsDo ListComp (reverse
754 (ReturnStmt $1 : $4)) $2 }
756 lexps :: { [RdrNameHsExpr] }
757 : lexps ',' exp { $3 : $1 }
758 | exp ',' exp { [$3,$1] }
760 -----------------------------------------------------------------------------
761 -- List Comprehensions
763 quals :: { [RdrNameStmt] }
764 : quals ',' qual { $3 : $1 }
767 qual :: { RdrNameStmt }
768 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
769 returnP (BindStmt p $4 $1) }
770 | srcloc exp { GuardStmt $2 $1 }
771 | srcloc 'let' declbinds { LetStmt $3 }
773 -----------------------------------------------------------------------------
776 altslist :: { [RdrNameMatch] }
777 : '{' alts '}' { reverse $2 }
778 | layout_on alts close { reverse $2 }
780 alts :: { [RdrNameMatch] }
784 alts1 :: { [RdrNameMatch] }
785 : alts1 ';' alt { $3 : $1 }
789 alt :: { RdrNameMatch }
790 : infixexp opt_sig ralt wherebinds
791 {% checkPattern $1 `thenP` \p ->
792 returnP (Match [] [p] $2
793 (GRHSs $3 $4 Nothing)) }
795 opt_sig :: { Maybe RdrNameHsType }
796 : {- empty -} { Nothing }
797 | '::' type { Just $2 }
799 opt_asig :: { Maybe RdrNameHsType }
800 : {- empty -} { Nothing }
801 | '::' atype { Just $2 }
803 ralt :: { [RdrNameGRHS] }
804 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
805 | gdpats { (reverse $1) }
807 gdpats :: { [RdrNameGRHS] }
808 : gdpats gdpat { $2 : $1 }
811 gdpat :: { RdrNameGRHS }
812 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
814 -----------------------------------------------------------------------------
815 -- Statement sequences
817 stmtlist :: { [RdrNameStmt] }
818 : '{' stmts '}' { reverse $2 }
819 | layout_on_for_do stmts close { reverse $2 }
821 -- Stmt list should really end in an expression, but it's not
822 -- convenient to enforce this here, so we throw out erroneous
823 -- statement sequences in the renamer instead.
825 stmts :: { [RdrNameStmt] }
829 stmts1 :: { [RdrNameStmt] }
830 : stmts1 ';' stmt { $3 : $1 }
834 stmt :: { RdrNameStmt }
835 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
836 returnP (BindStmt p $4 $1) }
837 | srcloc exp { ExprStmt $2 $1 }
838 | srcloc 'let' declbinds { LetStmt $3 }
840 -----------------------------------------------------------------------------
841 -- Record Field Update/Construction
843 fbinds :: { RdrNameHsRecordBinds }
844 : fbinds ',' fbind { $3 : $1 }
849 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
850 : qvar '=' exp { ($1,$3,False) }
852 -----------------------------------------------------------------------------
853 -- Implicit Parameter Bindings
855 dbinding :: { [(RdrName, RdrNameHsExpr)] }
856 : '{' dbinds '}' { $2 }
857 | layout_on dbinds close { $2 }
859 dbinds :: { [(RdrName, RdrNameHsExpr)] }
860 : dbinds ';' dbind { $3 : $1 }
865 dbind :: { (RdrName, RdrNameHsExpr) }
866 dbind : IPVARID '=' exp { (mkSrcUnqual ipName $1, $3) }
868 -----------------------------------------------------------------------------
869 -- Variables, Constructors and Operators.
872 : '(' ')' { unitCon_RDR }
873 | '[' ']' { nilCon_RDR }
874 | '(' commas ')' { tupleCon_RDR $2 }
879 | '(' varsym ')' { $2 }
883 | '(' qvarsym ')' { $2 }
887 | '(' consym ')' { $2 }
891 | '(' qconsym ')' { $2 }
895 | '`' varid '`' { $2 }
897 qvarop :: { RdrName }
899 | '`' qvarid '`' { $2 }
901 qvaropm :: { RdrName }
903 | '`' qvarid '`' { $2 }
907 | '`' conid '`' { $2 }
909 qconop :: { RdrName }
911 | '`' qconid '`' { $2 }
913 -----------------------------------------------------------------------------
916 op :: { RdrName } -- used in infix decls
920 qop :: { RdrNameHsExpr } -- used in sections
921 : qvarop { HsVar $1 }
922 | qconop { HsVar $1 }
924 qopm :: { RdrNameHsExpr } -- used in sections
925 : qvaropm { HsVar $1 }
926 | qconop { HsVar $1 }
928 -----------------------------------------------------------------------------
931 qvarid :: { RdrName }
933 | QVARID { case $1 of { (mod,n) ->
934 mkSrcQual varName mod n } }
937 : VARID { mkSrcUnqual varName $1 }
938 | 'as' { as_var_RDR }
939 | 'qualified' { qualified_var_RDR }
940 | 'hiding' { hiding_var_RDR }
941 | 'forall' { forall_var_RDR }
942 | 'export' { export_var_RDR }
943 | 'label' { label_var_RDR }
944 | 'dynamic' { dynamic_var_RDR }
945 | 'unsafe' { unsafe_var_RDR }
946 | 'stdcall' { stdcall_var_RDR }
947 | 'ccall' { ccall_var_RDR }
949 varid_no_unsafe :: { RdrName }
950 : VARID { mkSrcUnqual varName $1 }
951 | 'as' { as_var_RDR }
952 | 'qualified' { qualified_var_RDR }
953 | 'hiding' { hiding_var_RDR }
954 | 'forall' { forall_var_RDR }
955 | 'export' { export_var_RDR }
956 | 'label' { label_var_RDR }
957 | 'dynamic' { dynamic_var_RDR }
958 | 'stdcall' { stdcall_var_RDR }
959 | 'ccall' { ccall_var_RDR }
961 -----------------------------------------------------------------------------
964 qconid :: { RdrName }
966 | QCONID { case $1 of { (mod,n) ->
967 mkSrcQual dataName mod n } }
970 : CONID { mkSrcUnqual dataName $1 }
972 -----------------------------------------------------------------------------
975 qconsym :: { RdrName }
977 | QCONSYM { case $1 of { (mod,n) ->
978 mkSrcQual dataName mod n } }
980 consym :: { RdrName }
981 : CONSYM { mkSrcUnqual dataName $1 }
983 -----------------------------------------------------------------------------
986 qvarsym :: { RdrName }
990 qvarsymm :: { RdrName }
994 varsym :: { RdrName }
995 : VARSYM { mkSrcUnqual varName $1 }
1000 varsymm :: { RdrName } -- varsym not including '-'
1001 : VARSYM { mkSrcUnqual varName $1 }
1005 qvarsym1 :: { RdrName }
1006 : QVARSYM { case $1 of { (mod,n) ->
1007 mkSrcQual varName mod n } }
1009 literal :: { HsLit }
1010 : INTEGER { HsInt $1 }
1011 | CHAR { HsChar $1 }
1012 | RATIONAL { HsFrac $1 }
1013 | STRING { HsString $1 }
1015 | PRIMINTEGER { HsIntPrim $1 }
1016 | PRIMCHAR { HsCharPrim $1 }
1017 | PRIMSTRING { HsStringPrim $1 }
1018 | PRIMFLOAT { HsFloatPrim $1 }
1019 | PRIMDOUBLE { HsDoublePrim $1 }
1020 | CLITLIT { HsLitLit $1 }
1022 srcloc :: { SrcLoc } : {% getSrcLocP }
1024 -----------------------------------------------------------------------------
1028 : vccurly { () } -- context popped in lexer.
1029 | error {% popContext }
1031 layout_on :: { () } : {% layoutOn True{-strict-} }
1032 layout_on_for_do :: { () } : {% layoutOn False }
1034 -----------------------------------------------------------------------------
1035 -- Miscellaneous (mostly renamings)
1037 modid :: { ModuleName }
1038 : CONID { mkSrcModuleFS $1 }
1040 tycon :: { RdrName }
1041 : CONID { mkSrcUnqual tcClsName $1 }
1043 qtycon :: { RdrName }
1045 | QCONID { case $1 of { (mod,n) ->
1046 mkSrcQual tcClsName mod n } }
1048 qtycls :: { RdrName }
1051 tyvar :: { RdrName }
1052 : VARID { mkSrcUnqual tvName $1 }
1053 | 'as' { as_tyvar_RDR }
1054 | 'qualified' { qualified_tyvar_RDR }
1055 | 'hiding' { hiding_tyvar_RDR }
1056 | 'export' { export_tyvar_RDR }
1057 | 'label' { label_tyvar_RDR }
1058 | 'dynamic' { dynamic_tyvar_RDR }
1059 | 'unsafe' { unsafe_tyvar_RDR }
1060 | 'stdcall' { stdcall_tyvar_RDR }
1061 | 'ccall' { ccall_tyvar_RDR }
1062 -- NOTE: no 'forall'
1064 -----------------------------------------------------------------------------
1068 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)