2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.55 2001/02/26 15:06:59 simonmar Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parseModule, parseStmt ) where
15 import HsTypes ( mkHsTupCon )
21 import PrelNames ( mAIN_Name, unitTyCon_RDR, funTyCon_RDR, listTyCon_RDR,
22 tupleTyCon_RDR, unitCon_RDR, nilCon_RDR, tupleCon_RDR
24 import OccName ( UserFS, varName, tcName, dataName, tcClsName, tvName )
25 import SrcLoc ( SrcLoc )
28 import CmdLineOpts ( opt_SccProfilingOn )
29 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), NewOrData(..) )
33 import FastString ( tailFS )
36 #include "HsVersions.h"
40 -----------------------------------------------------------------------------
41 Conflicts: 14 shift/reduce
42 (note: it's currently 21 -- JRL, 31/1/2000)
44 8 for abiguity in 'if x then y else z + 1'
45 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
46 1 for ambiguity in 'if x then y else z :: T'
47 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
48 3 for ambiguity in 'case x of y :: a -> b'
49 (don't know whether to reduce 'a' as a btype or shift the '->'.
50 conclusion: bogus expression anyway, doesn't matter)
52 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
53 since 'forall' is a valid variable name, we don't know whether
54 to treat a forall on the input as the beginning of a quantifier
55 or the beginning of the rule itself. Resolving to shift means
56 it's always treated as a quantifier, hence the above is disallowed.
57 This saves explicitly defining a grammar for the rule lhs that
58 doesn't include 'forall'.
60 1 for ambiguity in 'x @ Rec{..}'.
61 Only sensible parse is 'x @ (Rec{..})', which is what resolving
64 -----------------------------------------------------------------------------
68 '_' { ITunderscore } -- Haskell keywords
73 'default' { ITdefault }
74 'deriving' { ITderiving }
84 'instance' { ITinstance }
87 'newtype' { ITnewtype }
89 'qualified' { ITqualified }
93 '_scc_' { ITscc } -- ToDo: remove
95 'forall' { ITforall } -- GHC extension keywords
96 'foreign' { ITforeign }
99 'dynamic' { ITdynamic }
100 'unsafe' { ITunsafe }
102 'stdcall' { ITstdcallconv }
103 'ccall' { ITccallconv }
104 '_ccall_' { ITccall (False, False, False) }
105 '_ccall_GC_' { ITccall (False, False, True) }
106 '_casm_' { ITccall (False, True, False) }
107 '_casm_GC_' { ITccall (False, True, True) }
109 '{-# SPECIALISE' { ITspecialise_prag }
110 '{-# SOURCE' { ITsource_prag }
111 '{-# INLINE' { ITinline_prag }
112 '{-# NOINLINE' { ITnoinline_prag }
113 '{-# RULES' { ITrules_prag }
114 '{-# SCC' { ITscc_prag }
115 '{-# DEPRECATED' { ITdeprecated_prag }
116 '#-}' { ITclose_prag }
119 '__interface' { ITinterface } -- interface keywords
120 '__export' { IT__export }
121 '__instimport' { ITinstimport }
122 '__forall' { IT__forall }
123 '__letrec' { ITletrec }
124 '__coerce' { ITcoerce }
125 '__depends' { ITdepends }
126 '__inline' { ITinline }
127 '__DEFAULT' { ITdefaultbranch }
129 '__integer' { ITinteger_lit }
130 '__float' { ITfloat_lit }
131 '__rational' { ITrational_lit }
132 '__addr' { ITaddr_lit }
133 '__label' { ITlabel_lit }
134 '__litlit' { ITlit_lit }
135 '__string' { ITstring_lit }
136 '__ccall' { ITccall $$ }
138 '__sccC' { ITsccAllCafs }
141 '__P' { ITspecialise }
143 '__U' { ITunfold $$ }
144 '__S' { ITstrict $$ }
145 '__M' { ITcprinfo $$ }
148 '..' { ITdotdot } -- reserved symbols
162 '{' { ITocurly } -- special symbols
166 vccurly { ITvccurly } -- virtual close curly (from layout)
177 VARID { ITvarid $$ } -- identifiers
179 VARSYM { ITvarsym $$ }
180 CONSYM { ITconsym $$ }
181 QVARID { ITqvarid $$ }
182 QCONID { ITqconid $$ }
183 QVARSYM { ITqvarsym $$ }
184 QCONSYM { ITqconsym $$ }
186 IPVARID { ITipvarid $$ } -- GHC extension
189 STRING { ITstring $$ }
190 INTEGER { ITinteger $$ }
191 RATIONAL { ITrational $$ }
193 PRIMCHAR { ITprimchar $$ }
194 PRIMSTRING { ITprimstring $$ }
195 PRIMINTEGER { ITprimint $$ }
196 PRIMFLOAT { ITprimfloat $$ }
197 PRIMDOUBLE { ITprimdouble $$ }
198 CLITLIT { ITlitlit $$ }
200 %monad { P } { thenP } { returnP }
201 %lexer { lexer } { ITeof }
202 %name parseModule module
203 %name parseStmt maybe_stmt
207 -----------------------------------------------------------------------------
210 -- The place for module deprecation is really too restrictive, but if it
211 -- was allowed at its natural place just before 'module', we get an ugly
212 -- s/r conflict with the second alternative. Another solution would be the
213 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
214 -- either, and DEPRECATED is only expected to be used by people who really
215 -- know what they are doing. :-)
217 module :: { RdrNameHsModule }
218 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
219 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
221 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
223 maybemoddeprec :: { Maybe DeprecTxt }
224 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
225 | {- empty -} { Nothing }
227 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
229 | layout_on top close { $2 }
231 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
232 : importdecls { (reverse $1,[]) }
233 | importdecls ';' cvtopdecls { (reverse $1,$3) }
234 | cvtopdecls { ([],$1) }
236 cvtopdecls :: { [RdrNameHsDecl] }
237 : topdecls { cvTopDecls (groupBindings $1)}
239 -----------------------------------------------------------------------------
242 maybeexports :: { Maybe [RdrNameIE] }
243 : '(' exportlist ')' { Just $2 }
244 | {- empty -} { Nothing }
246 exportlist :: { [RdrNameIE] }
247 : exportlist ',' export { $3 : $1 }
248 | exportlist ',' { $1 }
252 -- GHC extension: we allow things like [] and (,,,) to be exported
253 export :: { RdrNameIE }
255 | gtycon { IEThingAbs $1 }
256 | gtycon '(' '..' ')' { IEThingAll $1 }
257 | gtycon '(' ')' { IEThingWith $1 [] }
258 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
259 | 'module' modid { IEModuleContents $2 }
261 qcnames :: { [RdrName] }
262 : qcnames ',' qcname { $3 : $1 }
265 qcname :: { RdrName }
269 -----------------------------------------------------------------------------
270 -- Import Declarations
272 -- import decls can be *empty*, or even just a string of semicolons
273 -- whereas topdecls must contain at least one topdecl.
275 importdecls :: { [RdrNameImportDecl] }
276 : importdecls ';' importdecl { $3 : $1 }
277 | importdecls ';' { $1 }
278 | importdecl { [ $1 ] }
281 importdecl :: { RdrNameImportDecl }
282 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
283 { ImportDecl (mkModuleNameFS $5) $3 $4 $6 $7 $2 }
285 maybe_src :: { WhereFrom }
286 : '{-# SOURCE' '#-}' { ImportByUserSource }
287 | {- empty -} { ImportByUser }
289 optqualified :: { Bool }
290 : 'qualified' { True }
291 | {- empty -} { False }
293 maybeas :: { Maybe ModuleName }
294 : 'as' modid { Just $2 }
295 | {- empty -} { Nothing }
297 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
298 : impspec { Just $1 }
299 | {- empty -} { Nothing }
301 impspec :: { (Bool, [RdrNameIE]) }
302 : '(' exportlist ')' { (False, reverse $2) }
303 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
305 -----------------------------------------------------------------------------
306 -- Fixity Declarations
310 | INTEGER {% checkPrec $1 `thenP_`
311 returnP (fromInteger $1) }
313 infix :: { FixityDirection }
315 | 'infixl' { InfixL }
316 | 'infixr' { InfixR }
319 : ops ',' op { $3 : $1 }
322 -----------------------------------------------------------------------------
323 -- Top-Level Declarations
325 topdecls :: { [RdrBinding] }
326 : topdecls ';' topdecl { ($3 : $1) }
327 | topdecls ';' { $1 }
330 topdecl :: { RdrBinding }
331 : srcloc 'type' simpletype '=' sigtype
332 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
334 | srcloc 'data' ctype '=' constrs deriving
335 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
336 returnP (RdrHsDecl (TyClD
337 (mkTyData DataType cs c ts (reverse $5) (length $5) $6 $1))) }
339 | srcloc 'newtype' ctype '=' newconstr deriving
340 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
341 returnP (RdrHsDecl (TyClD
342 (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
344 | srcloc 'class' ctype fds where
345 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
347 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
349 returnP (RdrHsDecl (TyClD
350 (mkClassDecl cs c ts $4 sigs (Just binds) $1))) }
352 | srcloc 'instance' inst_type where
354 = cvMonoBindsAndSigs cvInstDeclSig
356 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
358 | srcloc 'default' '(' types0 ')'
359 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
361 | srcloc 'foreign' 'import' callconv ext_name
362 unsafe_flag varid_no_unsafe '::' sigtype
363 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
365 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
366 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
368 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
369 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
370 defaultCallConv $1)) }
372 | '{-# DEPRECATED' deprecations '#-}' { $2 }
373 | '{-# RULES' rules '#-}' { $2 }
376 decls :: { [RdrBinding] }
377 : decls ';' decl { $3 : $1 }
382 decl :: { RdrBinding }
385 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
386 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
387 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
388 { foldr1 RdrAndBindings
389 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
390 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
391 { RdrSig (SpecInstSig $4 $2) }
393 opt_phase :: { Maybe Int }
394 : INTEGER { Just (fromInteger $1) }
395 | {- empty -} { Nothing }
397 wherebinds :: { RdrNameHsBinds }
398 : where { cvBinds cvValSig (groupBindings $1) }
400 where :: { [RdrBinding] }
401 : 'where' decllist { $2 }
404 declbinds :: { RdrNameHsBinds }
405 : decllist { cvBinds cvValSig (groupBindings $1) }
407 decllist :: { [RdrBinding] }
408 : '{' decls '}' { $2 }
409 | layout_on decls close { $2 }
411 fixdecl :: { RdrBinding }
412 : srcloc infix prec ops { foldr1 RdrAndBindings
413 [ RdrSig (FixSig (FixitySig n
417 -----------------------------------------------------------------------------
418 -- Transformation Rules
420 rules :: { RdrBinding }
421 : rules ';' rule { $1 `RdrAndBindings` $3 }
424 | {- empty -} { RdrNullBind }
426 rule :: { RdrBinding }
427 : STRING rule_forall fexp '=' srcloc exp
428 { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
430 rule_forall :: { [RdrNameRuleBndr] }
431 : 'forall' rule_var_list '.' { $2 }
434 rule_var_list :: { [RdrNameRuleBndr] }
436 | rule_var rule_var_list { $1 : $2 }
438 rule_var :: { RdrNameRuleBndr }
439 : varid { RuleBndr $1 }
440 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
442 -----------------------------------------------------------------------------
445 deprecations :: { RdrBinding }
446 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
447 | deprecations ';' { $1 }
449 | {- empty -} { RdrNullBind }
451 -- SUP: TEMPORARY HACK, not checking for `module Foo'
452 deprecation :: { RdrBinding }
453 : srcloc depreclist STRING
454 { foldr RdrAndBindings RdrNullBind
455 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
457 -----------------------------------------------------------------------------
458 -- Foreign import/export
461 : 'stdcall' { stdCallConv }
462 | 'ccall' { cCallConv }
463 | {- empty -} { defaultCallConv }
465 unsafe_flag :: { Bool }
467 | {- empty -} { False }
469 ext_name :: { Maybe ExtName }
470 : 'dynamic' { Just Dynamic }
471 | STRING { Just (ExtName $1 Nothing) }
472 | STRING STRING { Just (ExtName $2 (Just $1)) }
473 | {- empty -} { Nothing }
476 -----------------------------------------------------------------------------
479 opt_sig :: { Maybe RdrNameHsType }
480 : {- empty -} { Nothing }
481 | '::' sigtype { Just $2 }
483 opt_asig :: { Maybe RdrNameHsType }
484 : {- empty -} { Nothing }
485 | '::' atype { Just $2 }
487 sigtypes :: { [RdrNameHsType] }
489 | sigtypes ',' sigtype { $3 : $1 }
491 sigtype :: { RdrNameHsType }
492 : ctype { (mkHsForAllTy Nothing [] $1) }
494 sig_vars :: { [RdrName] }
495 : sig_vars ',' var { $3 : $1 }
498 -----------------------------------------------------------------------------
501 -- A ctype is a for-all type
502 ctype :: { RdrNameHsType }
503 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
504 | context type { mkHsForAllTy Nothing $1 $2 }
505 -- A type of form (context => type) is an *implicit* HsForAllTy
508 type :: { RdrNameHsType }
509 : gentype '->' type { HsFunTy $1 $3 }
510 | ipvar '::' type { mkHsIParamTy $1 $3 }
513 gentype :: { RdrNameHsType }
516 | atype tyconop atype { HsOpTy $1 $2 $3 }
518 btype :: { RdrNameHsType }
519 : btype atype { (HsAppTy $1 $2) }
522 atype :: { RdrNameHsType }
523 : gtycon { HsTyVar $1 }
524 | tyvar { HsTyVar $1 }
525 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
526 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
527 | '[' type ']' { HsListTy $2 }
528 | '(' ctype ')' { $2 }
530 | INTEGER { HsNumTy $1 }
532 -- An inst_type is what occurs in the head of an instance decl
533 -- e.g. (Foo a, Gaz b) => Wibble a b
534 -- It's kept as a single type, with a MonoDictTy at the right
535 -- hand corner, for convenience.
536 inst_type :: { RdrNameHsType }
537 : ctype {% checkInstType $1 }
539 types0 :: { [RdrNameHsType] }
540 : types { reverse $1 }
543 types :: { [RdrNameHsType] }
545 | types ',' type { $3 : $1 }
547 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
548 : tycon tyvars { ($1, reverse $2) }
550 tyvars :: { [RdrNameHsTyVar] }
551 : tyvars tyvar { UserTyVar $2 : $1 }
554 fds :: { [([RdrName], [RdrName])] }
556 | '|' fds1 { reverse $2 }
558 fds1 :: { [([RdrName], [RdrName])] }
559 : fds1 ',' fd { $3 : $1 }
562 fd :: { ([RdrName], [RdrName]) }
563 : varids0 '->' varids0 { (reverse $1, reverse $3) }
565 varids0 :: { [RdrName] }
567 | varids0 tyvar { $2 : $1 }
569 -----------------------------------------------------------------------------
570 -- Datatype declarations
572 newconstr :: { RdrNameConDecl }
573 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [Unbanged $3]) $1 }
574 | srcloc conid '{' var '::' type '}'
575 { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $1 }
577 constrs :: { [RdrNameConDecl] }
578 : constrs '|' constr { $3 : $1 }
581 constr :: { RdrNameConDecl }
582 : srcloc forall context constr_stuff
583 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
584 | srcloc forall constr_stuff
585 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
587 forall :: { [RdrNameHsTyVar] }
588 : 'forall' tyvars '.' { $2 }
591 context :: { RdrNameContext }
592 : btype '=>' {% checkContext $1 }
594 constr_stuff :: { (RdrName, RdrNameConDetails) }
595 : btype {% mkVanillaCon $1 [] }
596 | btype '!' atype satypes {% mkVanillaCon $1 (Banged $3 : $4) }
597 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
598 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
600 satypes :: { [RdrNameBangType] }
601 : atype satypes { Unbanged $1 : $2 }
602 | '!' atype satypes { Banged $2 : $3 }
605 sbtype :: { RdrNameBangType }
606 : btype { Unbanged $1 }
607 | '!' atype { Banged $2 }
609 fielddecls :: { [([RdrName],RdrNameBangType)] }
610 : fielddecl ',' fielddecls { $1 : $3 }
613 fielddecl :: { ([RdrName],RdrNameBangType) }
614 : sig_vars '::' stype { (reverse $1, $3) }
616 stype :: { RdrNameBangType }
617 : ctype { Unbanged $1 }
618 | '!' atype { Banged $2 }
620 deriving :: { Maybe [RdrName] }
621 : {- empty -} { Nothing }
622 | 'deriving' qtycls { Just [$2] }
623 | 'deriving' '(' ')' { Just [] }
624 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
626 dclasses :: { [RdrName] }
627 : dclasses ',' qtycls { $3 : $1 }
630 -----------------------------------------------------------------------------
633 {- There's an awkward overlap with a type signature. Consider
634 f :: Int -> Int = ...rhs...
635 Then we can't tell whether it's a type signature or a value
636 definition with a result signature until we see the '='.
637 So we have to inline enough to postpone reductions until we know.
641 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
642 instead of qvar, we get another shift/reduce-conflict. Consider the
645 { (^^) :: Int->Int ; } Type signature; only var allowed
647 { (^^) :: Int->Int = ... ; } Value defn with result signature;
648 qvar allowed (because of instance decls)
650 We can't tell whether to reduce var to qvar until after we've read the signatures.
653 valdef :: { RdrBinding }
654 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
655 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
656 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
657 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
661 rhs :: { RdrNameGRHSs }
662 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2)
664 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
666 gdrhs :: { [RdrNameGRHS] }
667 : gdrhs gdrh { $2 : $1 }
670 gdrh :: { RdrNameGRHS }
671 : '|' srcloc quals '=' exp { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
673 -----------------------------------------------------------------------------
676 exp :: { RdrNameHsExpr }
677 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
678 | infixexp 'with' dbinding { HsWith $1 $3 }
681 infixexp :: { RdrNameHsExpr }
683 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
684 (panic "fixity") $3 )}
686 exp10 :: { RdrNameHsExpr }
687 : '\\' aexp aexps opt_asig '->' srcloc exp
688 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
689 returnP (HsLam (Match [] ps $4
690 (GRHSs (unguardedRHS $7 $6)
691 EmptyBinds Nothing))) }
692 | 'let' declbinds 'in' exp { HsLet $2 $4 }
693 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
694 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
695 | '-' fexp { mkHsNegApp $2 }
696 | srcloc 'do' stmtlist { HsDo DoExpr $3 $1 }
698 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 False False cbot }
699 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 True False cbot }
700 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 False True cbot }
701 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 True True cbot }
703 | scc_annot exp { if opt_SccProfilingOn
709 scc_annot :: { FAST_STRING }
710 : '_scc_' STRING { $2 }
711 | '{-# SCC' STRING '#-}' { $2 }
713 ccallid :: { FAST_STRING }
717 fexp :: { RdrNameHsExpr }
718 : fexp aexp { (HsApp $1 $2) }
721 aexps0 :: { [RdrNameHsExpr] }
722 : aexps { (reverse $1) }
724 aexps :: { [RdrNameHsExpr] }
725 : aexps aexp { $2 : $1 }
728 aexp :: { RdrNameHsExpr }
729 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
730 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
734 var_or_con :: { RdrNameHsExpr }
738 aexp1 :: { RdrNameHsExpr }
739 : ipvar { HsIPVar $1 }
741 | literal { HsLit $1 }
742 | INTEGER { HsOverLit (HsIntegral $1) }
743 | RATIONAL { HsOverLit (HsFractional $1) }
744 | '(' exp ')' { HsPar $2 }
745 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
746 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
747 | '[' list ']' { $2 }
748 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
749 | '(' qopm infixexp ')' { (SectionR $2 $3) }
750 | qvar '@' aexp { EAsPat $1 $3 }
752 | '~' aexp1 { ELazyPat $2 }
754 texps :: { [RdrNameHsExpr] }
755 : texps ',' exp { $3 : $1 }
759 -----------------------------------------------------------------------------
762 -- The rules below are little bit contorted to keep lexps left-recursive while
763 -- avoiding another shift/reduce-conflict.
765 list :: { RdrNameHsExpr }
766 : exp { ExplicitList [$1] }
767 | lexps { ExplicitList (reverse $1) }
768 | exp '..' { ArithSeqIn (From $1) }
769 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
770 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
771 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
772 | exp srcloc pquals {% let { body [qs] = qs;
773 body qss = [ParStmt (map reverse qss)] }
775 returnP ( HsDo ListComp
776 (reverse (ExprStmt $1 $2 : body $3))
781 lexps :: { [RdrNameHsExpr] }
782 : lexps ',' exp { $3 : $1 }
783 | exp ',' exp { [$3,$1] }
785 -----------------------------------------------------------------------------
786 -- List Comprehensions
788 pquals :: { [[RdrNameStmt]] }
789 : pquals '|' quals { $3 : $1 }
792 quals :: { [RdrNameStmt] }
793 : quals ',' stmt { $3 : $1 }
796 -----------------------------------------------------------------------------
799 altslist :: { [RdrNameMatch] }
800 : '{' alts '}' { reverse $2 }
801 | layout_on alts close { reverse $2 }
803 alts :: { [RdrNameMatch] }
807 alts1 :: { [RdrNameMatch] }
808 : alts1 ';' alt { $3 : $1 }
812 alt :: { RdrNameMatch }
813 : infixexp opt_sig ralt wherebinds
814 {% (checkPattern $1 `thenP` \p ->
815 returnP (Match [] [p] $2
816 (GRHSs $3 $4 Nothing)) )}
818 ralt :: { [RdrNameGRHS] }
819 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
820 | gdpats { (reverse $1) }
822 gdpats :: { [RdrNameGRHS] }
823 : gdpats gdpat { $2 : $1 }
826 gdpat :: { RdrNameGRHS }
827 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
829 -----------------------------------------------------------------------------
830 -- Statement sequences
832 stmtlist :: { [RdrNameStmt] }
833 : '{' stmts '}' { reverse $2 }
834 | layout_on_for_do stmts close { reverse $2 }
836 -- Stmt list should really end in an expression, but it's not
837 -- convenient to enforce this here, so we throw out erroneous
838 -- statement sequences in the renamer instead.
840 stmts :: { [RdrNameStmt] }
844 stmts1 :: { [RdrNameStmt] }
845 : stmts1 ';' stmt { $3 : $1 }
849 -- for typing stmts at the GHCi prompt, where the input may consist of
851 maybe_stmt :: { Maybe RdrNameStmt }
853 | {- nothing -} { Nothing }
855 stmt :: { RdrNameStmt }
856 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
857 returnP (BindStmt p $4 $1) }
858 | srcloc exp { ExprStmt $2 $1 }
859 | srcloc 'let' declbinds { LetStmt $3 }
861 -----------------------------------------------------------------------------
862 -- Record Field Update/Construction
864 fbinds :: { RdrNameHsRecordBinds }
865 : fbinds ',' fbind { $3 : $1 }
870 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
871 : qvar '=' exp { ($1,$3,False) }
873 -----------------------------------------------------------------------------
874 -- Implicit Parameter Bindings
876 dbinding :: { [(RdrName, RdrNameHsExpr)] }
877 : '{' dbinds '}' { $2 }
878 | layout_on dbinds close { $2 }
880 dbinds :: { [(RdrName, RdrNameHsExpr)] }
881 : dbinds ';' dbind { $3 : $1 }
886 dbind :: { (RdrName, RdrNameHsExpr) }
887 dbind : ipvar '=' exp { ($1, $3) }
889 -----------------------------------------------------------------------------
890 -- Variables, Constructors and Operators.
892 depreclist :: { [RdrName] }
893 depreclist : deprec_var { [$1] }
894 | deprec_var ',' depreclist { $1 : $3 }
896 deprec_var :: { RdrName }
897 deprec_var : var { $1 }
900 gtycon :: { RdrName }
902 | '(' qtyconop ')' { $2 }
903 | '(' ')' { unitTyCon_RDR }
904 | '(' '->' ')' { funTyCon_RDR }
905 | '[' ']' { listTyCon_RDR }
906 | '(' commas ')' { tupleTyCon_RDR $2 }
909 : '(' ')' { unitCon_RDR }
910 | '[' ']' { nilCon_RDR }
911 | '(' commas ')' { tupleCon_RDR $2 }
916 | '(' varsym ')' { $2 }
920 | '(' varsym ')' { $2 }
921 | '(' qvarsym1 ')' { $2 }
922 -- We've inlined qvarsym here so that the decision about
923 -- whether it's a qvar or a var can be postponed until
924 -- *after* we see the close paren.
927 : IPVARID { (mkUnqual varName (tailFS $1)) }
931 | '(' qconsym ')' { $2 }
935 | '`' varid '`' { $2 }
937 qvarop :: { RdrName }
939 | '`' qvarid '`' { $2 }
941 qvaropm :: { RdrName }
942 : qvarsym_no_minus { $1 }
943 | '`' qvarid '`' { $2 }
947 | '`' conid '`' { $2 }
949 qconop :: { RdrName }
951 | '`' qconid '`' { $2 }
953 -----------------------------------------------------------------------------
956 op :: { RdrName } -- used in infix decls
960 qop :: { RdrName {-HsExpr-} } -- used in sections
964 qopm :: { RdrNameHsExpr } -- used in sections
965 : qvaropm { HsVar $1 }
966 | qconop { HsVar $1 }
968 -----------------------------------------------------------------------------
971 qvarid :: { RdrName }
973 | QVARID { mkQual varName $1 }
976 : varid_no_unsafe { $1 }
977 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
979 varid_no_unsafe :: { RdrName }
980 : VARID { mkUnqual varName $1 }
981 | special_id { mkUnqual varName $1 }
982 | 'forall' { mkUnqual varName SLIT("forall") }
985 : VARID { mkUnqual tvName $1 }
986 | special_id { mkUnqual tvName $1 }
987 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
989 -- These special_ids are treated as keywords in various places,
990 -- but as ordinary ids elsewhere. A special_id collects all thsee
991 -- except 'unsafe' and 'forall' whose treatment differs depending on context
992 special_id :: { UserFS }
994 : 'as' { SLIT("as") }
995 | 'qualified' { SLIT("qualified") }
996 | 'hiding' { SLIT("hiding") }
997 | 'export' { SLIT("export") }
998 | 'label' { SLIT("label") }
999 | 'dynamic' { SLIT("dynamic") }
1000 | 'stdcall' { SLIT("stdcall") }
1001 | 'ccall' { SLIT("ccall") }
1003 -----------------------------------------------------------------------------
1006 qconid :: { RdrName }
1008 | QCONID { mkQual dataName $1 }
1010 conid :: { RdrName }
1011 : CONID { mkUnqual dataName $1 }
1013 -----------------------------------------------------------------------------
1016 qconsym :: { RdrName }
1018 | QCONSYM { mkQual dataName $1 }
1020 consym :: { RdrName }
1021 : CONSYM { mkUnqual dataName $1 }
1023 -----------------------------------------------------------------------------
1026 qvarsym :: { RdrName }
1030 qvarsym_no_minus :: { RdrName }
1031 : varsym_no_minus { $1 }
1034 qvarsym1 :: { RdrName }
1035 qvarsym1 : QVARSYM { mkQual varName $1 }
1037 varsym :: { RdrName }
1038 : varsym_no_minus { $1 }
1039 | '-' { mkUnqual varName SLIT("-") }
1041 varsym_no_minus :: { RdrName } -- varsym not including '-'
1042 : VARSYM { mkUnqual varName $1 }
1043 | special_sym { mkUnqual varName $1 }
1046 -- See comments with special_id
1047 special_sym :: { UserFS }
1048 special_sym : '!' { SLIT("!") }
1051 -----------------------------------------------------------------------------
1054 literal :: { HsLit }
1055 : CHAR { HsChar $1 }
1056 | STRING { HsString $1 }
1057 | PRIMINTEGER { HsIntPrim $1 }
1058 | PRIMCHAR { HsCharPrim $1 }
1059 | PRIMSTRING { HsStringPrim $1 }
1060 | PRIMFLOAT { HsFloatPrim $1 }
1061 | PRIMDOUBLE { HsDoublePrim $1 }
1062 | CLITLIT { HsLitLit $1 (error "Parser.y: CLITLIT") }
1064 srcloc :: { SrcLoc } : {% getSrcLocP }
1066 -----------------------------------------------------------------------------
1070 : vccurly { () } -- context popped in lexer.
1071 | error {% popContext }
1073 layout_on :: { () } : {% layoutOn True{-strict-} }
1074 layout_on_for_do :: { () } : {% layoutOn False }
1076 -----------------------------------------------------------------------------
1077 -- Miscellaneous (mostly renamings)
1079 modid :: { ModuleName }
1080 : CONID { mkModuleNameFS $1 }
1082 tycon :: { RdrName }
1083 : CONID { mkUnqual tcClsName $1 }
1085 tyconop :: { RdrName }
1086 : CONSYM { mkUnqual tcClsName $1 }
1088 qtycon :: { RdrName }
1090 | QCONID { mkQual tcClsName $1 }
1092 qtyconop :: { RdrName }
1094 | QCONSYM { mkQual tcClsName $1 }
1096 qtycls :: { RdrName }
1100 : commas ',' { $1 + 1 }
1103 -----------------------------------------------------------------------------
1107 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)