2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.33 2000/07/06 14:08:31 simonmar 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 ( 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 '/\\' { ITbiglam } -- GHC-extension symbols
161 '{' { ITocurly } -- special symbols
163 vccurly { ITvccurly } -- virtual close curly (from layout)
174 VARID { ITvarid $$ } -- identifiers
176 VARSYM { ITvarsym $$ }
177 CONSYM { ITconsym $$ }
178 QVARID { ITqvarid $$ }
179 QCONID { ITqconid $$ }
180 QVARSYM { ITqvarsym $$ }
181 QCONSYM { ITqconsym $$ }
183 IPVARID { ITipvarid $$ } -- GHC extension
185 PRAGMA { ITpragma $$ }
188 STRING { ITstring $$ }
189 INTEGER { ITinteger $$ }
190 RATIONAL { ITrational $$ }
192 PRIMCHAR { ITprimchar $$ }
193 PRIMSTRING { ITprimstring $$ }
194 PRIMINTEGER { ITprimint $$ }
195 PRIMFLOAT { ITprimfloat $$ }
196 PRIMDOUBLE { ITprimdouble $$ }
197 CLITLIT { ITlitlit $$ }
199 UNKNOWN { ITunknown $$ }
201 %monad { P } { thenP } { returnP }
202 %lexer { lexer } { ITeof }
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 ';' cvtopdecls { (reverse $1,$3) }
233 | importdecls { (reverse $1,[]) }
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 (mkSrcModuleFS $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 (TyData DataType cs c ts (reverse $5) (length $5) $6
338 NoDataPragmas $1))) }
340 | srcloc 'newtype' ctype '=' newconstr deriving
341 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
342 returnP (RdrHsDecl (TyClD
343 (TyData NewType cs c ts [$5] 1 $6
344 NoDataPragmas $1))) }
346 | srcloc 'class' ctype fds where
347 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
349 = cvMonoBindsAndSigs cvClassOpSig
352 returnP (RdrHsDecl (TyClD
353 (mkClassDecl cs c ts $4 sigs binds
354 NoClassPragmas $1))) }
356 | srcloc 'instance' inst_type where
358 = cvMonoBindsAndSigs cvInstDeclSig
361 (InstDecl $3 binds sigs dummyRdrVarName $1)) }
363 | srcloc 'default' '(' types0 ')'
364 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
366 | srcloc 'foreign' 'import' callconv ext_name
367 unsafe_flag varid_no_unsafe '::' sigtype
368 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
370 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
371 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
373 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
374 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
375 defaultCallConv $1)) }
377 | '{-# DEPRECATED' deprecations '#-}' { $2 }
378 | '{-# RULES' rules '#-}' { $2 }
381 decls :: { [RdrBinding] }
382 : decls ';' decl { $3 : $1 }
387 decl :: { RdrBinding }
390 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
391 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
392 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
393 { foldr1 RdrAndBindings
394 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
395 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
396 { RdrSig (SpecInstSig $4 $2) }
398 opt_phase :: { Maybe Int }
399 : INTEGER { Just (fromInteger $1) }
400 | {- empty -} { Nothing }
402 wherebinds :: { RdrNameHsBinds }
403 : where { cvBinds cvValSig (groupBindings $1) }
405 where :: { [RdrBinding] }
406 : 'where' decllist { $2 }
409 declbinds :: { RdrNameHsBinds }
410 : decllist { cvBinds cvValSig (groupBindings $1) }
412 decllist :: { [RdrBinding] }
413 : '{' decls '}' { $2 }
414 | layout_on decls close { $2 }
416 fixdecl :: { RdrBinding }
417 : srcloc infix prec ops { foldr1 RdrAndBindings
418 [ RdrSig (FixSig (FixitySig n
422 -----------------------------------------------------------------------------
423 -- Transformation Rules
425 rules :: { RdrBinding }
426 : rules ';' rule { $1 `RdrAndBindings` $3 }
429 | {- empty -} { RdrNullBind }
431 rule :: { RdrBinding }
432 : STRING rule_forall fexp '=' srcloc exp
433 { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
435 rule_forall :: { [RdrNameRuleBndr] }
436 : 'forall' rule_var_list '.' { $2 }
439 rule_var_list :: { [RdrNameRuleBndr] }
441 | rule_var rule_var_list { $1 : $2 }
443 rule_var :: { RdrNameRuleBndr }
444 : varid { RuleBndr $1 }
445 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
447 -----------------------------------------------------------------------------
450 deprecations :: { RdrBinding }
451 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
452 | deprecations ';' { $1 }
454 | {- empty -} { RdrNullBind }
456 -- SUP: TEMPORARY HACK, not checking for `module Foo'
457 deprecation :: { RdrBinding }
458 : srcloc exportlist STRING
459 { foldr RdrAndBindings RdrNullBind
460 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
462 -----------------------------------------------------------------------------
463 -- Foreign import/export
466 : 'stdcall' { stdCallConv }
467 | 'ccall' { cCallConv }
468 | {- empty -} { defaultCallConv }
470 unsafe_flag :: { Bool }
472 | {- empty -} { False }
474 ext_name :: { Maybe ExtName }
475 : 'dynamic' { Just Dynamic }
476 | STRING { Just (ExtName $1 Nothing) }
477 | STRING STRING { Just (ExtName $2 (Just $1)) }
478 | {- empty -} { Nothing }
481 -----------------------------------------------------------------------------
484 opt_sig :: { Maybe RdrNameHsType }
485 : {- empty -} { Nothing }
486 | '::' sigtype { Just $2 }
488 opt_asig :: { Maybe RdrNameHsType }
489 : {- empty -} { Nothing }
490 | '::' atype { Just $2 }
492 sigtypes :: { [RdrNameHsType] }
494 | sigtypes ',' sigtype { $3 : $1 }
496 sigtype :: { RdrNameHsType }
497 : ctype { mkHsForAllTy Nothing [] $1 }
499 sig_vars :: { [RdrName] }
500 : sig_vars ',' var { $3 : $1 }
503 -----------------------------------------------------------------------------
506 -- A ctype is a for-all type
507 ctype :: { RdrNameHsType }
508 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
509 | context type { mkHsForAllTy Nothing $1 $2 }
510 -- A type of form (context => type) is an *implicit* HsForAllTy
513 type :: { RdrNameHsType }
514 : btype '->' type { HsFunTy $1 $3 }
515 | ipvar '::' type { mkHsIParamTy $1 $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 gtycon :: { RdrName }
532 | '(' ')' { unitTyCon_RDR }
533 | '(' '->' ')' { funTyCon_RDR }
534 | '[' ']' { listTyCon_RDR }
535 | '(' commas ')' { tupleTyCon_RDR $2 }
537 -- An inst_type is what occurs in the head of an instance decl
538 -- e.g. (Foo a, Gaz b) => Wibble a b
539 -- It's kept as a single type, with a MonoDictTy at the right
540 -- hand corner, for convenience.
541 inst_type :: { RdrNameHsType }
542 : ctype {% checkInstType $1 }
544 types0 :: { [RdrNameHsType] }
548 types :: { [RdrNameHsType] }
550 | types ',' type { $3 : $1 }
552 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
553 : tycon tyvars { ($1, reverse $2) }
555 tyvars :: { [RdrNameHsTyVar] }
556 : tyvars tyvar { UserTyVar $2 : $1 }
559 fds :: { [([RdrName], [RdrName])] }
561 | '|' fds1 { reverse $2 }
563 fds1 :: { [([RdrName], [RdrName])] }
564 : fds1 ',' fd { $3 : $1 }
567 fd :: { ([RdrName], [RdrName]) }
568 : varids0 '->' varids0 { (reverse $1, reverse $3) }
570 varids0 :: { [RdrName] }
572 | varids0 tyvar { $2 : $1 }
574 -----------------------------------------------------------------------------
575 -- Datatype declarations
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 : scontype { (fst $1, VanillaCon (snd $1)) }
596 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
597 | con '{' fielddecls '}' { ($1, RecCon (reverse $3)) }
599 newconstr :: { RdrNameConDecl }
600 : srcloc conid atype { mkConDecl $2 [] [] (NewCon $3 Nothing) $1 }
601 | srcloc conid '{' var '::' type '}'
602 { mkConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
604 scontype :: { (RdrName, [RdrNameBangType]) }
605 : btype {% splitForConApp $1 [] }
608 scontype1 :: { (RdrName, [RdrNameBangType]) }
609 : btype '!' atype {% splitForConApp $1 [Banged $3] }
610 | scontype1 satype { (fst $1, snd $1 ++ [$2] ) }
611 | '(' consym ')' { ($2,[]) }
613 satype :: { RdrNameBangType }
614 : atype { Unbanged $1 }
615 | '!' atype { Banged $2 }
617 sbtype :: { RdrNameBangType }
618 : btype { Unbanged $1 }
619 | '!' atype { Banged $2 }
621 fielddecls :: { [([RdrName],RdrNameBangType)] }
622 : fielddecls ',' fielddecl { $3 : $1 }
625 fielddecl :: { ([RdrName],RdrNameBangType) }
626 : sig_vars '::' stype { (reverse $1, $3) }
628 stype :: { RdrNameBangType }
629 : ctype { Unbanged $1 }
630 | '!' atype { Banged $2 }
632 deriving :: { Maybe [RdrName] }
633 : {- empty -} { Nothing }
634 | 'deriving' qtycls { Just [$2] }
635 | 'deriving' '(' ')' { Just [] }
636 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
638 dclasses :: { [RdrName] }
639 : dclasses ',' qtycls { $3 : $1 }
642 -----------------------------------------------------------------------------
645 {- There's an awkward overlap with a type signature. Consider
646 f :: Int -> Int = ...rhs...
647 Then we can't tell whether it's a type signature or a value
648 definition with a result signature until we see the '='.
649 So we have to inline enough to postpone reductions until we know.
653 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
654 instead of qvar, we get another shift/reduce-conflict. Consider the
657 { (^^) :: Int->Int ; } Type signature; only var allowed
659 { (^^) :: Int->Int = ... ; } Value defn with result signature;
660 qvar allowed (because of instance decls)
662 We can't tell whether to reduce var to qvar until after we've read the signatures.
665 valdef :: { RdrBinding }
666 : infixexp srcloc opt_sig rhs {% checkValDef $1 $3 $4 $2 }
667 | infixexp srcloc '::' sigtype {% checkValSig $1 $4 $2 }
668 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
669 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
672 rhs :: { RdrNameGRHSs }
673 : '=' srcloc exp wherebinds { GRHSs (unguardedRHS $3 $2)
675 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
677 gdrhs :: { [RdrNameGRHS] }
678 : gdrhs gdrh { $2 : $1 }
681 gdrh :: { RdrNameGRHS }
682 : '|' srcloc quals '=' exp { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
684 -----------------------------------------------------------------------------
687 exp :: { RdrNameHsExpr }
688 : infixexp '::' sigtype { ExprWithTySig $1 $3 }
689 | infixexp 'with' dbinding { HsWith $1 $3 }
692 infixexp :: { RdrNameHsExpr }
694 | infixexp qop exp10 { OpApp $1 $2 (panic "fixity") $3 }
696 exp10 :: { RdrNameHsExpr }
697 : '\\' aexp aexps opt_asig '->' srcloc exp
698 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
699 returnP (HsLam (Match [] ps $4
700 (GRHSs (unguardedRHS $7 $6)
701 EmptyBinds Nothing))) }
702 | 'let' declbinds 'in' exp { HsLet $2 $4 }
703 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
704 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
705 | '-' fexp { NegApp $2 (error "NegApp") }
706 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
708 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 False False cbot }
709 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 True False cbot }
710 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 False True cbot }
711 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 True True cbot }
713 | '_scc_' STRING exp { if opt_SccProfilingOn
719 ccallid :: { FAST_STRING }
723 fexp :: { RdrNameHsExpr }
724 : fexp aexp { HsApp $1 $2 }
727 aexps0 :: { [RdrNameHsExpr] }
728 : aexps { reverse $1 }
730 aexps :: { [RdrNameHsExpr] }
731 : aexps aexp { $2 : $1 }
734 aexp :: { RdrNameHsExpr }
735 : aexp '{' fbinds '}' {% mkRecConstrOrUpdate $1 (reverse $3) }
738 aexp1 :: { RdrNameHsExpr }
740 | ipvar { HsIPVar $1 }
742 | literal { HsLit $1 }
743 | '(' exp ')' { HsPar $2 }
744 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
745 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
746 | '[' list ']' { $2 }
747 | '(' infixexp qop ')' { SectionL $2 $3 }
748 | '(' qopm infixexp ')' { SectionR $2 $3 }
749 | qvar '@' aexp { EAsPat $1 $3 }
751 | '~' aexp1 { ELazyPat $2 }
754 : commas ',' { $1 + 1 }
757 texps :: { [RdrNameHsExpr] }
758 : texps ',' exp { $3 : $1 }
761 -----------------------------------------------------------------------------
764 -- The rules below are little bit contorted to keep lexps left-recursive while
765 -- avoiding another shift/reduce-conflict.
767 list :: { RdrNameHsExpr }
768 : exp { ExplicitList [$1] }
769 | lexps { ExplicitList (reverse $1) }
770 | exp '..' { ArithSeqIn (From $1) }
771 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
772 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
773 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
774 | exp srcloc '|' quals { HsDo ListComp (reverse
775 (ReturnStmt $1 : $4)) $2 }
777 lexps :: { [RdrNameHsExpr] }
778 : lexps ',' exp { $3 : $1 }
779 | exp ',' exp { [$3,$1] }
781 -----------------------------------------------------------------------------
782 -- List Comprehensions
784 quals :: { [RdrNameStmt] }
785 : quals ',' qual { $3 : $1 }
788 qual :: { RdrNameStmt }
789 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
790 returnP (BindStmt p $4 $1) }
791 | srcloc exp { GuardStmt $2 $1 }
792 | srcloc 'let' declbinds { LetStmt $3 }
794 -----------------------------------------------------------------------------
797 altslist :: { [RdrNameMatch] }
798 : '{' alts '}' { reverse $2 }
799 | layout_on alts close { reverse $2 }
801 alts :: { [RdrNameMatch] }
805 alts1 :: { [RdrNameMatch] }
806 : alts1 ';' alt { $3 : $1 }
810 alt :: { RdrNameMatch }
811 : infixexp opt_sig ralt wherebinds
812 {% checkPattern $1 `thenP` \p ->
813 returnP (Match [] [p] $2
814 (GRHSs $3 $4 Nothing)) }
816 ralt :: { [RdrNameGRHS] }
817 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
818 | gdpats { (reverse $1) }
820 gdpats :: { [RdrNameGRHS] }
821 : gdpats gdpat { $2 : $1 }
824 gdpat :: { RdrNameGRHS }
825 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
827 -----------------------------------------------------------------------------
828 -- Statement sequences
830 stmtlist :: { [RdrNameStmt] }
831 : '{' stmts '}' { reverse $2 }
832 | layout_on_for_do stmts close { reverse $2 }
834 -- Stmt list should really end in an expression, but it's not
835 -- convenient to enforce this here, so we throw out erroneous
836 -- statement sequences in the renamer instead.
838 stmts :: { [RdrNameStmt] }
842 stmts1 :: { [RdrNameStmt] }
843 : stmts1 ';' stmt { $3 : $1 }
847 stmt :: { RdrNameStmt }
848 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
849 returnP (BindStmt p $4 $1) }
850 | srcloc exp { ExprStmt $2 $1 }
851 | srcloc 'let' declbinds { LetStmt $3 }
853 -----------------------------------------------------------------------------
854 -- Record Field Update/Construction
856 fbinds :: { RdrNameHsRecordBinds }
857 : fbinds ',' fbind { $3 : $1 }
862 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
863 : qvar '=' exp { ($1,$3,False) }
865 -----------------------------------------------------------------------------
866 -- Implicit Parameter Bindings
868 dbinding :: { [(RdrName, RdrNameHsExpr)] }
869 : '{' dbinds '}' { $2 }
870 | layout_on dbinds close { $2 }
872 dbinds :: { [(RdrName, RdrNameHsExpr)] }
873 : dbinds ';' dbind { $3 : $1 }
878 dbind :: { (RdrName, RdrNameHsExpr) }
879 dbind : ipvar '=' exp { ($1, $3) }
881 -----------------------------------------------------------------------------
882 -- Variables, Constructors and Operators.
885 : '(' ')' { unitCon_RDR }
886 | '[' ']' { nilCon_RDR }
887 | '(' commas ')' { tupleCon_RDR $2 }
892 | '(' varsym ')' { $2 }
896 | '(' varsym ')' { $2 }
897 | '(' qvarsym1 ')' { $2 }
898 -- We've inlined qvarsym here so that the decision about
899 -- whether it's a qvar or a var can be postponed until
900 -- *after* we see the close paren.
903 : IPVARID { (mkSrcUnqual ipName (tailFS $1)) }
907 | '(' consym ')' { $2 }
911 | '(' qconsym ')' { $2 }
915 | '`' varid '`' { $2 }
917 qvarop :: { RdrName }
919 | '`' qvarid '`' { $2 }
921 qvaropm :: { RdrName }
923 | '`' qvarid '`' { $2 }
927 | '`' conid '`' { $2 }
929 qconop :: { RdrName }
931 | '`' qconid '`' { $2 }
933 -----------------------------------------------------------------------------
936 op :: { RdrName } -- used in infix decls
940 qop :: { RdrNameHsExpr } -- used in sections
941 : qvarop { HsVar $1 }
942 | qconop { HsVar $1 }
944 qopm :: { RdrNameHsExpr } -- used in sections
945 : qvaropm { HsVar $1 }
946 | qconop { HsVar $1 }
948 -----------------------------------------------------------------------------
951 qvarid :: { RdrName }
953 | QVARID { case $1 of { (mod,n) ->
954 mkSrcQual varName mod n } }
957 : VARID { mkSrcUnqual varName $1 }
958 | 'as' { as_var_RDR }
959 | 'qualified' { qualified_var_RDR }
960 | 'hiding' { hiding_var_RDR }
961 | 'forall' { forall_var_RDR }
962 | 'export' { export_var_RDR }
963 | 'label' { label_var_RDR }
964 | 'dynamic' { dynamic_var_RDR }
965 | 'unsafe' { unsafe_var_RDR }
966 | 'stdcall' { stdcall_var_RDR }
967 | 'ccall' { ccall_var_RDR }
969 varid_no_unsafe :: { RdrName }
970 : VARID { mkSrcUnqual varName $1 }
971 | 'as' { as_var_RDR }
972 | 'qualified' { qualified_var_RDR }
973 | 'hiding' { hiding_var_RDR }
974 | 'forall' { forall_var_RDR }
975 | 'export' { export_var_RDR }
976 | 'label' { label_var_RDR }
977 | 'dynamic' { dynamic_var_RDR }
978 | 'stdcall' { stdcall_var_RDR }
979 | 'ccall' { ccall_var_RDR }
981 -----------------------------------------------------------------------------
984 qconid :: { RdrName }
986 | QCONID { case $1 of { (mod,n) ->
987 mkSrcQual dataName mod n } }
990 : CONID { mkSrcUnqual dataName $1 }
992 -----------------------------------------------------------------------------
995 qconsym :: { RdrName }
997 | QCONSYM { case $1 of { (mod,n) ->
998 mkSrcQual dataName mod n } }
1000 consym :: { RdrName }
1001 : CONSYM { mkSrcUnqual dataName $1 }
1003 -----------------------------------------------------------------------------
1006 qvarsym :: { RdrName }
1010 qvarsymm :: { RdrName }
1014 varsym :: { RdrName }
1015 : VARSYM { mkSrcUnqual varName $1 }
1020 varsymm :: { RdrName } -- varsym not including '-'
1021 : VARSYM { mkSrcUnqual varName $1 }
1025 qvarsym1 :: { RdrName }
1026 : QVARSYM { case $1 of { (mod,n) ->
1027 mkSrcQual varName mod n } }
1029 literal :: { HsLit }
1030 : INTEGER { HsInt $1 }
1031 | CHAR { HsChar $1 }
1032 | RATIONAL { HsFrac $1 }
1033 | STRING { HsString $1 }
1035 | PRIMINTEGER { HsIntPrim $1 }
1036 | PRIMCHAR { HsCharPrim $1 }
1037 | PRIMSTRING { HsStringPrim $1 }
1038 | PRIMFLOAT { HsFloatPrim $1 }
1039 | PRIMDOUBLE { HsDoublePrim $1 }
1040 | CLITLIT { HsLitLit $1 }
1042 srcloc :: { SrcLoc } : {% getSrcLocP }
1044 -----------------------------------------------------------------------------
1048 : vccurly { () } -- context popped in lexer.
1049 | error {% popContext }
1051 layout_on :: { () } : {% layoutOn True{-strict-} }
1052 layout_on_for_do :: { () } : {% layoutOn False }
1054 -----------------------------------------------------------------------------
1055 -- Miscellaneous (mostly renamings)
1057 modid :: { ModuleName }
1058 : CONID { mkSrcModuleFS $1 }
1060 tycon :: { RdrName }
1061 : CONID { mkSrcUnqual tcClsName $1 }
1063 qtycon :: { RdrName }
1065 | QCONID { case $1 of { (mod,n) ->
1066 mkSrcQual tcClsName mod n } }
1068 qtycls :: { RdrName }
1071 tyvar :: { RdrName }
1072 : VARID { mkSrcUnqual tvName $1 }
1073 | 'as' { as_tyvar_RDR }
1074 | 'qualified' { qualified_tyvar_RDR }
1075 | 'hiding' { hiding_tyvar_RDR }
1076 | 'export' { export_tyvar_RDR }
1077 | 'label' { label_tyvar_RDR }
1078 | 'dynamic' { dynamic_tyvar_RDR }
1079 | 'unsafe' { unsafe_tyvar_RDR }
1080 | 'stdcall' { stdcall_tyvar_RDR }
1081 | 'ccall' { ccall_tyvar_RDR }
1082 -- NOTE: no 'forall'
1084 -----------------------------------------------------------------------------
1088 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)