2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.36 2000/09/22 15:56:13 simonpj Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parse ) where
16 import HsTypes ( mkHsTupCon )
22 import PrelInfo ( mAIN_Name )
23 import OccName ( UserFS, varName, ipName, tcName, dataName, tcClsName, tvName )
24 import SrcLoc ( SrcLoc )
27 import CmdLineOpts ( opt_SccProfilingOn )
28 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), NewOrData(..) )
32 import FastString ( tailFS )
34 #include "HsVersions.h"
38 -----------------------------------------------------------------------------
39 Conflicts: 14 shift/reduce
40 (note: it's currently 21 -- JRL, 31/1/2000)
42 8 for abiguity in 'if x then y else z + 1'
43 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
44 1 for ambiguity in 'if x then y else z :: T'
45 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
46 3 for ambiguity in 'case x of y :: a -> b'
47 (don't know whether to reduce 'a' as a btype or shift the '->'.
48 conclusion: bogus expression anyway, doesn't matter)
50 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
51 since 'forall' is a valid variable name, we don't know whether
52 to treat a forall on the input as the beginning of a quantifier
53 or the beginning of the rule itself. Resolving to shift means
54 it's always treated as a quantifier, hence the above is disallowed.
55 This saves explicitly defining a grammar for the rule lhs that
56 doesn't include 'forall'.
58 1 for ambiguity in 'x @ Rec{..}'.
59 Only sensible parse is 'x @ (Rec{..})', which is what resolving
62 -----------------------------------------------------------------------------
66 '_' { ITunderscore } -- Haskell keywords
71 'default' { ITdefault }
72 'deriving' { ITderiving }
82 'instance' { ITinstance }
85 'newtype' { ITnewtype }
87 'qualified' { ITqualified }
93 'forall' { ITforall } -- GHC extension keywords
94 'foreign' { ITforeign }
97 'dynamic' { ITdynamic }
100 'stdcall' { ITstdcallconv }
101 'ccall' { ITccallconv }
102 '_ccall_' { ITccall (False, False, False) }
103 '_ccall_GC_' { ITccall (False, False, True) }
104 '_casm_' { ITccall (False, True, False) }
105 '_casm_GC_' { ITccall (False, True, True) }
107 '{-# SPECIALISE' { ITspecialise_prag }
108 '{-# SOURCE' { ITsource_prag }
109 '{-# INLINE' { ITinline_prag }
110 '{-# NOINLINE' { ITnoinline_prag }
111 '{-# RULES' { ITrules_prag }
112 '{-# DEPRECATED' { ITdeprecated_prag }
113 '#-}' { ITclose_prag }
116 '__interface' { ITinterface } -- interface keywords
117 '__export' { IT__export }
118 '__instimport' { ITinstimport }
119 '__forall' { IT__forall }
120 '__letrec' { ITletrec }
121 '__coerce' { ITcoerce }
122 '__depends' { ITdepends }
123 '__inline' { ITinline }
124 '__DEFAULT' { ITdefaultbranch }
126 '__integer' { ITinteger_lit }
127 '__float' { ITfloat_lit }
128 '__rational' { ITrational_lit }
129 '__addr' { ITaddr_lit }
130 '__label' { ITlabel_lit }
131 '__litlit' { ITlit_lit }
132 '__string' { ITstring_lit }
133 '__ccall' { ITccall $$ }
135 '__sccC' { ITsccAllCafs }
138 '__P' { ITspecialise }
140 '__U' { ITunfold $$ }
141 '__S' { ITstrict $$ }
142 '__M' { ITcprinfo $$ }
145 '..' { ITdotdot } -- reserved symbols
159 '{' { ITocurly } -- special symbols
161 vccurly { ITvccurly } -- virtual close curly (from layout)
172 VARID { ITvarid $$ } -- identifiers
174 VARSYM { ITvarsym $$ }
175 CONSYM { ITconsym $$ }
176 QVARID { ITqvarid $$ }
177 QCONID { ITqconid $$ }
178 QVARSYM { ITqvarsym $$ }
179 QCONSYM { ITqconsym $$ }
181 IPVARID { ITipvarid $$ } -- GHC extension
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 %monad { P } { thenP } { returnP }
196 %lexer { lexer } { ITeof }
201 -----------------------------------------------------------------------------
204 -- The place for module deprecation is really too restrictive, but if it
205 -- was allowed at its natural place just before 'module', we get an ugly
206 -- s/r conflict with the second alternative. Another solution would be the
207 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
208 -- either, and DEPRECATED is only expected to be used by people who really
209 -- know what they are doing. :-)
211 module :: { RdrNameHsModule }
212 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
213 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
215 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
217 maybemoddeprec :: { Maybe DeprecTxt }
218 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
219 | {- empty -} { Nothing }
221 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
223 | layout_on top close { $2 }
225 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
226 : importdecls { (reverse $1,[]) }
227 | importdecls ';' cvtopdecls { (reverse $1,$3) }
228 | cvtopdecls { ([],$1) }
230 cvtopdecls :: { [RdrNameHsDecl] }
231 : topdecls { cvTopDecls (groupBindings $1)}
233 -----------------------------------------------------------------------------
236 maybeexports :: { Maybe [RdrNameIE] }
237 : '(' exportlist ')' { Just $2 }
238 | {- empty -} { Nothing }
240 exportlist :: { [RdrNameIE] }
241 : exportlist ',' export { $3 : $1 }
242 | exportlist ',' { $1 }
246 -- GHC extension: we allow things like [] and (,,,) to be exported
247 export :: { RdrNameIE }
249 | gtycon { IEThingAbs $1 }
250 | gtycon '(' '..' ')' { IEThingAll $1 }
251 | gtycon '(' ')' { IEThingWith $1 [] }
252 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
253 | 'module' modid { IEModuleContents $2 }
255 qcnames :: { [RdrName] }
256 : qcnames ',' qcname { $3 : $1 }
259 qcname :: { RdrName }
263 -----------------------------------------------------------------------------
264 -- Import Declarations
266 -- import decls can be *empty*, or even just a string of semicolons
267 -- whereas topdecls must contain at least one topdecl.
269 importdecls :: { [RdrNameImportDecl] }
270 : importdecls ';' importdecl { $3 : $1 }
271 | importdecls ';' { $1 }
272 | importdecl { [ $1 ] }
275 importdecl :: { RdrNameImportDecl }
276 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
277 { ImportDecl (mkSrcModuleFS $5) $3 $4 $6 $7 $2 }
279 maybe_src :: { WhereFrom }
280 : '{-# SOURCE' '#-}' { ImportByUserSource }
281 | {- empty -} { ImportByUser }
283 optqualified :: { Bool }
284 : 'qualified' { True }
285 | {- empty -} { False }
287 maybeas :: { Maybe ModuleName }
288 : 'as' modid { Just $2 }
289 | {- empty -} { Nothing }
291 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
292 : impspec { Just $1 }
293 | {- empty -} { Nothing }
295 impspec :: { (Bool, [RdrNameIE]) }
296 : '(' exportlist ')' { (False, reverse $2) }
297 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
299 -----------------------------------------------------------------------------
300 -- Fixity Declarations
304 | INTEGER {% checkPrec $1 `thenP_`
305 returnP (fromInteger $1) }
307 infix :: { FixityDirection }
309 | 'infixl' { InfixL }
310 | 'infixr' { InfixR }
313 : ops ',' op { $3 : $1 }
316 -----------------------------------------------------------------------------
317 -- Top-Level Declarations
319 topdecls :: { [RdrBinding] }
320 : topdecls ';' topdecl { ($3 : $1) }
321 | topdecls ';' { $1 }
324 topdecl :: { RdrBinding }
325 : srcloc 'type' simpletype '=' sigtype
326 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
328 | srcloc 'data' ctype '=' constrs deriving
329 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
330 returnP (RdrHsDecl (TyClD
331 (TyData DataType cs c ts (reverse $5) (length $5) $6
332 NoDataPragmas $1))) }
334 | srcloc 'newtype' ctype '=' newconstr deriving
335 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
336 returnP (RdrHsDecl (TyClD
337 (TyData NewType cs c ts [$5] 1 $6
338 NoDataPragmas $1))) }
340 | srcloc 'class' ctype fds where
341 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
343 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
345 returnP (RdrHsDecl (TyClD
346 (mkClassDecl cs c ts $4 sigs binds
347 NoClassPragmas $1))) }
349 | srcloc 'instance' inst_type where
351 = cvMonoBindsAndSigs cvInstDeclSig
353 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
355 | srcloc 'default' '(' types0 ')'
356 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
358 | srcloc 'foreign' 'import' callconv ext_name
359 unsafe_flag varid_no_unsafe '::' sigtype
360 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
362 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
363 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
365 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
366 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
367 defaultCallConv $1)) }
369 | '{-# DEPRECATED' deprecations '#-}' { $2 }
370 | '{-# RULES' rules '#-}' { $2 }
373 decls :: { [RdrBinding] }
374 : decls ';' decl { $3 : $1 }
379 decl :: { RdrBinding }
382 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
383 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
384 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
385 { foldr1 RdrAndBindings
386 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
387 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
388 { RdrSig (SpecInstSig $4 $2) }
390 opt_phase :: { Maybe Int }
391 : INTEGER { Just (fromInteger $1) }
392 | {- empty -} { Nothing }
394 wherebinds :: { RdrNameHsBinds }
395 : where { cvBinds cvValSig (groupBindings $1) }
397 where :: { [RdrBinding] }
398 : 'where' decllist { $2 }
401 declbinds :: { RdrNameHsBinds }
402 : decllist { cvBinds cvValSig (groupBindings $1) }
404 decllist :: { [RdrBinding] }
405 : '{' decls '}' { $2 }
406 | layout_on decls close { $2 }
408 fixdecl :: { RdrBinding }
409 : srcloc infix prec ops { foldr1 RdrAndBindings
410 [ RdrSig (FixSig (FixitySig n
414 -----------------------------------------------------------------------------
415 -- Transformation Rules
417 rules :: { RdrBinding }
418 : rules ';' rule { $1 `RdrAndBindings` $3 }
421 | {- empty -} { RdrNullBind }
423 rule :: { RdrBinding }
424 : STRING rule_forall fexp '=' srcloc exp
425 { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
427 rule_forall :: { [RdrNameRuleBndr] }
428 : 'forall' rule_var_list '.' { $2 }
431 rule_var_list :: { [RdrNameRuleBndr] }
433 | rule_var rule_var_list { $1 : $2 }
435 rule_var :: { RdrNameRuleBndr }
436 : varid { RuleBndr $1 }
437 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
439 -----------------------------------------------------------------------------
442 deprecations :: { RdrBinding }
443 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
444 | deprecations ';' { $1 }
446 | {- empty -} { RdrNullBind }
448 -- SUP: TEMPORARY HACK, not checking for `module Foo'
449 deprecation :: { RdrBinding }
450 : srcloc exportlist STRING
451 { foldr RdrAndBindings RdrNullBind
452 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
454 -----------------------------------------------------------------------------
455 -- Foreign import/export
458 : 'stdcall' { stdCallConv }
459 | 'ccall' { cCallConv }
460 | {- empty -} { defaultCallConv }
462 unsafe_flag :: { Bool }
464 | {- empty -} { False }
466 ext_name :: { Maybe ExtName }
467 : 'dynamic' { Just Dynamic }
468 | STRING { Just (ExtName $1 Nothing) }
469 | STRING STRING { Just (ExtName $2 (Just $1)) }
470 | {- empty -} { Nothing }
473 -----------------------------------------------------------------------------
476 opt_sig :: { Maybe RdrNameHsType }
477 : {- empty -} { Nothing }
478 | '::' sigtype { Just $2 }
480 opt_asig :: { Maybe RdrNameHsType }
481 : {- empty -} { Nothing }
482 | '::' atype { Just $2 }
484 sigtypes :: { [RdrNameHsType] }
486 | sigtypes ',' sigtype { $3 : $1 }
488 sigtype :: { RdrNameHsType }
489 : ctype { mkHsForAllTy Nothing [] $1 }
491 sig_vars :: { [RdrName] }
492 : sig_vars ',' var { $3 : $1 }
495 -----------------------------------------------------------------------------
498 -- A ctype is a for-all type
499 ctype :: { RdrNameHsType }
500 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
501 | context type { mkHsForAllTy Nothing $1 $2 }
502 -- A type of form (context => type) is an *implicit* HsForAllTy
505 type :: { RdrNameHsType }
506 : btype '->' type { HsFunTy $1 $3 }
507 | ipvar '::' type { mkHsIParamTy $1 $3 }
510 btype :: { RdrNameHsType }
511 : btype atype { HsAppTy $1 $2 }
514 atype :: { RdrNameHsType }
515 : gtycon { HsTyVar $1 }
516 | tyvar { HsTyVar $1 }
517 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
518 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
519 | '[' type ']' { HsListTy $2 }
520 | '(' ctype ')' { $2 }
522 -- An inst_type is what occurs in the head of an instance decl
523 -- e.g. (Foo a, Gaz b) => Wibble a b
524 -- It's kept as a single type, with a MonoDictTy at the right
525 -- hand corner, for convenience.
526 inst_type :: { RdrNameHsType }
527 : ctype {% checkInstType $1 }
529 types0 :: { [RdrNameHsType] }
533 types :: { [RdrNameHsType] }
535 | types ',' type { $3 : $1 }
537 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
538 : tycon tyvars { ($1, reverse $2) }
540 tyvars :: { [RdrNameHsTyVar] }
541 : tyvars tyvar { UserTyVar $2 : $1 }
544 fds :: { [([RdrName], [RdrName])] }
546 | '|' fds1 { reverse $2 }
548 fds1 :: { [([RdrName], [RdrName])] }
549 : fds1 ',' fd { $3 : $1 }
552 fd :: { ([RdrName], [RdrName]) }
553 : varids0 '->' varids0 { (reverse $1, reverse $3) }
555 varids0 :: { [RdrName] }
557 | varids0 tyvar { $2 : $1 }
559 -----------------------------------------------------------------------------
560 -- Datatype declarations
562 constrs :: { [RdrNameConDecl] }
563 : constrs '|' constr { $3 : $1 }
566 constr :: { RdrNameConDecl }
567 : srcloc forall context constr_stuff
568 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
569 | srcloc forall constr_stuff
570 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
572 forall :: { [RdrNameHsTyVar] }
573 : 'forall' tyvars '.' { $2 }
576 context :: { RdrNameContext }
577 : btype '=>' {% checkContext $1 }
579 constr_stuff :: { (RdrName, RdrNameConDetails) }
580 : scontype { (fst $1, VanillaCon (snd $1)) }
581 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
582 | con '{' fielddecls '}' { ($1, RecCon (reverse $3)) }
584 newconstr :: { RdrNameConDecl }
585 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [Unbanged $3]) $1 }
586 | srcloc conid '{' var '::' type '}'
587 { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $1 }
589 scontype :: { (RdrName, [RdrNameBangType]) }
590 : btype {% splitForConApp $1 [] }
593 scontype1 :: { (RdrName, [RdrNameBangType]) }
594 : btype '!' atype {% splitForConApp $1 [Banged $3] }
595 | scontype1 satype { (fst $1, snd $1 ++ [$2] ) }
596 | '(' consym ')' { ($2,[]) }
598 satype :: { RdrNameBangType }
599 : atype { Unbanged $1 }
600 | '!' atype { Banged $2 }
602 sbtype :: { RdrNameBangType }
603 : btype { Unbanged $1 }
604 | '!' atype { Banged $2 }
606 fielddecls :: { [([RdrName],RdrNameBangType)] }
607 : fielddecls ',' fielddecl { $3 : $1 }
610 fielddecl :: { ([RdrName],RdrNameBangType) }
611 : sig_vars '::' stype { (reverse $1, $3) }
613 stype :: { RdrNameBangType }
614 : ctype { Unbanged $1 }
615 | '!' atype { Banged $2 }
617 deriving :: { Maybe [RdrName] }
618 : {- empty -} { Nothing }
619 | 'deriving' qtycls { Just [$2] }
620 | 'deriving' '(' ')' { Just [] }
621 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
623 dclasses :: { [RdrName] }
624 : dclasses ',' qtycls { $3 : $1 }
627 -----------------------------------------------------------------------------
630 {- There's an awkward overlap with a type signature. Consider
631 f :: Int -> Int = ...rhs...
632 Then we can't tell whether it's a type signature or a value
633 definition with a result signature until we see the '='.
634 So we have to inline enough to postpone reductions until we know.
638 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
639 instead of qvar, we get another shift/reduce-conflict. Consider the
642 { (^^) :: Int->Int ; } Type signature; only var allowed
644 { (^^) :: Int->Int = ... ; } Value defn with result signature;
645 qvar allowed (because of instance decls)
647 We can't tell whether to reduce var to qvar until after we've read the signatures.
650 valdef :: { RdrBinding }
651 : infixexp srcloc opt_sig rhs {% checkValDef $1 $3 $4 $2 }
652 | infixexp srcloc '::' sigtype {% checkValSig $1 $4 $2 }
653 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
654 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
657 rhs :: { RdrNameGRHSs }
658 : '=' srcloc exp wherebinds { GRHSs (unguardedRHS $3 $2)
660 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
662 gdrhs :: { [RdrNameGRHS] }
663 : gdrhs gdrh { $2 : $1 }
666 gdrh :: { RdrNameGRHS }
667 : '|' srcloc quals '=' exp { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
669 -----------------------------------------------------------------------------
672 exp :: { RdrNameHsExpr }
673 : infixexp '::' sigtype { ExprWithTySig $1 $3 }
674 | infixexp 'with' dbinding { HsWith $1 $3 }
677 infixexp :: { RdrNameHsExpr }
679 | infixexp qop exp10 { OpApp $1 $2 (panic "fixity") $3 }
681 exp10 :: { RdrNameHsExpr }
682 : '\\' aexp aexps opt_asig '->' srcloc exp
683 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
684 returnP (HsLam (Match [] ps $4
685 (GRHSs (unguardedRHS $7 $6)
686 EmptyBinds Nothing))) }
687 | 'let' declbinds 'in' exp { HsLet $2 $4 }
688 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
689 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
690 | '-' fexp { mkHsNegApp $2 }
691 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
693 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 False False cbot }
694 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 True False cbot }
695 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 False True cbot }
696 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 True True cbot }
698 | '_scc_' STRING exp { if opt_SccProfilingOn
704 ccallid :: { FAST_STRING }
708 fexp :: { RdrNameHsExpr }
709 : fexp aexp { HsApp $1 $2 }
712 aexps0 :: { [RdrNameHsExpr] }
713 : aexps { reverse $1 }
715 aexps :: { [RdrNameHsExpr] }
716 : aexps aexp { $2 : $1 }
719 aexp :: { RdrNameHsExpr }
720 : aexp '{' fbinds '}' {% mkRecConstrOrUpdate $1 (reverse $3) }
723 aexp1 :: { RdrNameHsExpr }
725 | ipvar { HsIPVar $1 }
727 | literal { HsLit $1 }
728 | INTEGER { HsOverLit (mkHsIntegralLit $1) }
729 | RATIONAL { HsOverLit (mkHsFractionalLit $1) }
730 | '(' exp ')' { HsPar $2 }
731 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
732 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
733 | '[' list ']' { $2 }
734 | '(' infixexp qop ')' { SectionL $2 $3 }
735 | '(' qopm infixexp ')' { SectionR $2 $3 }
736 | qvar '@' aexp { EAsPat $1 $3 }
738 | '~' aexp1 { ELazyPat $2 }
740 texps :: { [RdrNameHsExpr] }
741 : texps ',' exp { $3 : $1 }
744 -----------------------------------------------------------------------------
747 -- The rules below are little bit contorted to keep lexps left-recursive while
748 -- avoiding another shift/reduce-conflict.
750 list :: { RdrNameHsExpr }
751 : exp { ExplicitList [$1] }
752 | lexps { ExplicitList (reverse $1) }
753 | exp '..' { ArithSeqIn (From $1) }
754 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
755 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
756 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
757 | exp srcloc '|' quals { HsDo ListComp (reverse
758 (ReturnStmt $1 : $4)) $2 }
760 lexps :: { [RdrNameHsExpr] }
761 : lexps ',' exp { $3 : $1 }
762 | exp ',' exp { [$3,$1] }
764 -----------------------------------------------------------------------------
765 -- List Comprehensions
767 quals :: { [RdrNameStmt] }
768 : quals ',' qual { $3 : $1 }
771 qual :: { RdrNameStmt }
772 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
773 returnP (BindStmt p $4 $1) }
774 | srcloc exp { GuardStmt $2 $1 }
775 | srcloc 'let' declbinds { LetStmt $3 }
777 -----------------------------------------------------------------------------
780 altslist :: { [RdrNameMatch] }
781 : '{' alts '}' { reverse $2 }
782 | layout_on alts close { reverse $2 }
784 alts :: { [RdrNameMatch] }
788 alts1 :: { [RdrNameMatch] }
789 : alts1 ';' alt { $3 : $1 }
793 alt :: { RdrNameMatch }
794 : infixexp opt_sig ralt wherebinds
795 {% checkPattern $1 `thenP` \p ->
796 returnP (Match [] [p] $2
797 (GRHSs $3 $4 Nothing)) }
799 ralt :: { [RdrNameGRHS] }
800 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
801 | gdpats { (reverse $1) }
803 gdpats :: { [RdrNameGRHS] }
804 : gdpats gdpat { $2 : $1 }
807 gdpat :: { RdrNameGRHS }
808 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
810 -----------------------------------------------------------------------------
811 -- Statement sequences
813 stmtlist :: { [RdrNameStmt] }
814 : '{' stmts '}' { reverse $2 }
815 | layout_on_for_do stmts close { reverse $2 }
817 -- Stmt list should really end in an expression, but it's not
818 -- convenient to enforce this here, so we throw out erroneous
819 -- statement sequences in the renamer instead.
821 stmts :: { [RdrNameStmt] }
825 stmts1 :: { [RdrNameStmt] }
826 : stmts1 ';' stmt { $3 : $1 }
830 stmt :: { RdrNameStmt }
831 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
832 returnP (BindStmt p $4 $1) }
833 | srcloc exp { ExprStmt $2 $1 }
834 | srcloc 'let' declbinds { LetStmt $3 }
836 -----------------------------------------------------------------------------
837 -- Record Field Update/Construction
839 fbinds :: { RdrNameHsRecordBinds }
840 : fbinds ',' fbind { $3 : $1 }
845 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
846 : qvar '=' exp { ($1,$3,False) }
848 -----------------------------------------------------------------------------
849 -- Implicit Parameter Bindings
851 dbinding :: { [(RdrName, RdrNameHsExpr)] }
852 : '{' dbinds '}' { $2 }
853 | layout_on dbinds close { $2 }
855 dbinds :: { [(RdrName, RdrNameHsExpr)] }
856 : dbinds ';' dbind { $3 : $1 }
861 dbind :: { (RdrName, RdrNameHsExpr) }
862 dbind : ipvar '=' exp { ($1, $3) }
864 -----------------------------------------------------------------------------
865 -- Variables, Constructors and Operators.
867 gtycon :: { RdrName }
869 | '(' ')' { unitTyCon_RDR }
870 | '(' '->' ')' { funTyCon_RDR }
871 | '[' ']' { listTyCon_RDR }
872 | '(' commas ')' { tupleTyCon_RDR $2 }
875 : '(' ')' { unitCon_RDR }
876 | '[' ']' { nilCon_RDR }
877 | '(' commas ')' { tupleCon_RDR $2 }
882 | '(' varsym ')' { $2 }
886 | '(' varsym ')' { $2 }
887 | '(' qvarsym1 ')' { $2 }
888 -- We've inlined qvarsym here so that the decision about
889 -- whether it's a qvar or a var can be postponed until
890 -- *after* we see the close paren.
893 : IPVARID { (mkSrcUnqual ipName (tailFS $1)) }
897 | '(' consym ')' { $2 }
901 | '(' qconsym ')' { $2 }
905 | '`' varid '`' { $2 }
907 qvarop :: { RdrName }
909 | '`' qvarid '`' { $2 }
911 qvaropm :: { RdrName }
912 : qvarsym_no_minus { $1 }
913 | '`' qvarid '`' { $2 }
917 | '`' conid '`' { $2 }
919 qconop :: { RdrName }
921 | '`' qconid '`' { $2 }
923 -----------------------------------------------------------------------------
926 op :: { RdrName } -- used in infix decls
930 qop :: { RdrNameHsExpr } -- used in sections
931 : qvarop { HsVar $1 }
932 | qconop { HsVar $1 }
934 qopm :: { RdrNameHsExpr } -- used in sections
935 : qvaropm { HsVar $1 }
936 | qconop { HsVar $1 }
938 -----------------------------------------------------------------------------
941 qvarid :: { RdrName }
943 | QVARID { mkSrcQual varName $1 }
946 : varid_no_unsafe { $1 }
947 | 'unsafe' { mkSrcUnqual varName SLIT("unsafe") }
949 varid_no_unsafe :: { RdrName }
950 : VARID { mkSrcUnqual varName $1 }
951 | special_id { mkSrcUnqual varName $1 }
952 | 'forall' { mkSrcUnqual varName SLIT("forall") }
955 : VARID { mkSrcUnqual tvName $1 }
956 | special_id { mkSrcUnqual tvName $1 }
957 | 'unsafe' { mkSrcUnqual tvName SLIT("unsafe") }
959 -- These special_ids are treated as keywords in various places,
960 -- but as ordinary ids elsewhere. A special_id collects all thsee
961 -- except 'unsafe' and 'forall' whose treatment differs depending on context
962 special_id :: { UserFS }
964 : 'as' { SLIT("as") }
965 | 'qualified' { SLIT("qualified") }
966 | 'hiding' { SLIT("hiding") }
967 | 'export' { SLIT("export") }
968 | 'label' { SLIT("label") }
969 | 'dynamic' { SLIT("dynamic") }
970 | 'stdcall' { SLIT("stdcall") }
971 | 'ccall' { SLIT("ccall") }
973 -----------------------------------------------------------------------------
976 qconid :: { RdrName }
978 | QCONID { mkSrcQual dataName $1 }
981 : CONID { mkSrcUnqual dataName $1 }
983 -----------------------------------------------------------------------------
986 qconsym :: { RdrName }
988 | QCONSYM { mkSrcQual dataName $1 }
990 consym :: { RdrName }
991 : CONSYM { mkSrcUnqual dataName $1 }
993 -----------------------------------------------------------------------------
996 qvarsym :: { RdrName }
1000 qvarsym_no_minus :: { RdrName }
1001 : varsym_no_minus { $1 }
1004 qvarsym1 :: { RdrName }
1005 qvarsym1 : QVARSYM { mkSrcQual varName $1 }
1007 varsym :: { RdrName }
1008 : varsym_no_minus { $1 }
1009 | '-' { mkSrcUnqual varName SLIT("-") }
1011 varsym_no_minus :: { RdrName } -- varsym not including '-'
1012 : VARSYM { mkSrcUnqual varName $1 }
1013 | special_sym { mkSrcUnqual varName $1 }
1016 -- See comments with special_id
1017 special_sym :: { UserFS }
1018 special_sym : '!' { SLIT("!") }
1021 -----------------------------------------------------------------------------
1024 literal :: { HsLit }
1025 : CHAR { HsChar $1 }
1026 | STRING { HsString $1 }
1027 | PRIMINTEGER { HsIntPrim $1 }
1028 | PRIMCHAR { HsCharPrim $1 }
1029 | PRIMSTRING { HsStringPrim $1 }
1030 | PRIMFLOAT { HsFloatPrim $1 }
1031 | PRIMDOUBLE { HsDoublePrim $1 }
1032 | CLITLIT { HsLitLit $1 (error "Parser.y: CLITLIT") }
1034 srcloc :: { SrcLoc } : {% getSrcLocP }
1036 -----------------------------------------------------------------------------
1040 : vccurly { () } -- context popped in lexer.
1041 | error {% popContext }
1043 layout_on :: { () } : {% layoutOn True{-strict-} }
1044 layout_on_for_do :: { () } : {% layoutOn False }
1046 -----------------------------------------------------------------------------
1047 -- Miscellaneous (mostly renamings)
1049 modid :: { ModuleName }
1050 : CONID { mkSrcModuleFS $1 }
1052 tycon :: { RdrName }
1053 : CONID { mkSrcUnqual tcClsName $1 }
1055 qtycon :: { RdrName }
1057 | QCONID { mkSrcQual tcClsName $1 }
1059 qtycls :: { RdrName }
1063 : commas ',' { $1 + 1 }
1066 -----------------------------------------------------------------------------
1070 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)