2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.31 2000/05/25 12:41:17 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 ( 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 '__litlit' { ITlit_lit }
131 '__string' { ITstring_lit }
132 '__ccall' { ITccall $$ }
134 '__sccC' { ITsccAllCafs }
137 '__P' { ITspecialise }
139 '__U' { ITunfold $$ }
140 '__S' { ITstrict $$ }
141 '__M' { ITcprinfo $$ }
144 '..' { ITdotdot } -- reserved symbols
158 '/\\' { ITbiglam } -- GHC-extension symbols
160 '{' { ITocurly } -- special symbols
162 vccurly { ITvccurly } -- virtual close curly (from layout)
173 VARID { ITvarid $$ } -- identifiers
175 VARSYM { ITvarsym $$ }
176 CONSYM { ITconsym $$ }
177 QVARID { ITqvarid $$ }
178 QCONID { ITqconid $$ }
179 QVARSYM { ITqvarsym $$ }
180 QCONSYM { ITqconsym $$ }
182 IPVARID { ITipvarid $$ } -- GHC extension
184 PRAGMA { ITpragma $$ }
187 STRING { ITstring $$ }
188 INTEGER { ITinteger $$ }
189 RATIONAL { ITrational $$ }
191 PRIMCHAR { ITprimchar $$ }
192 PRIMSTRING { ITprimstring $$ }
193 PRIMINTEGER { ITprimint $$ }
194 PRIMFLOAT { ITprimfloat $$ }
195 PRIMDOUBLE { ITprimdouble $$ }
196 CLITLIT { ITlitlit $$ }
198 UNKNOWN { ITunknown $$ }
200 %monad { P } { thenP } { returnP }
201 %lexer { lexer } { ITeof }
206 -----------------------------------------------------------------------------
209 -- The place for module deprecation is really too restrictive, but if it
210 -- was allowed at its natural place just before 'module', we get an ugly
211 -- s/r conflict with the second alternative. Another solution would be the
212 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
213 -- either, and DEPRECATED is only expected to be used by people who really
214 -- know what they are doing. :-)
216 module :: { RdrNameHsModule }
217 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
218 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
220 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
222 maybemoddeprec :: { Maybe DeprecTxt }
223 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
224 | {- empty -} { Nothing }
226 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
228 | layout_on top close { $2 }
230 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
231 : importdecls ';' cvtopdecls { (reverse $1,$3) }
232 | importdecls { (reverse $1,[]) }
233 | cvtopdecls { ([],$1) }
235 cvtopdecls :: { [RdrNameHsDecl] }
236 : topdecls { cvTopDecls (groupBindings $1)}
238 -----------------------------------------------------------------------------
241 maybeexports :: { Maybe [RdrNameIE] }
242 : '(' exportlist ')' { Just $2 }
243 | {- empty -} { Nothing }
245 exportlist :: { [RdrNameIE] }
246 : exportlist ',' export { $3 : $1 }
247 | exportlist ',' { $1 }
251 -- GHC extension: we allow things like [] and (,,,) to be exported
252 export :: { RdrNameIE }
254 | gtycon { IEThingAbs $1 }
255 | gtycon '(' '..' ')' { IEThingAll $1 }
256 | gtycon '(' ')' { IEThingWith $1 [] }
257 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
258 | 'module' modid { IEModuleContents $2 }
260 qcnames :: { [RdrName] }
261 : qcnames ',' qcname { $3 : $1 }
264 qcname :: { RdrName }
268 -----------------------------------------------------------------------------
269 -- Import Declarations
271 -- import decls can be *empty*, or even just a string of semicolons
272 -- whereas topdecls must contain at least one topdecl.
274 importdecls :: { [RdrNameImportDecl] }
275 : importdecls ';' importdecl { $3 : $1 }
276 | importdecls ';' { $1 }
277 | importdecl { [ $1 ] }
280 importdecl :: { RdrNameImportDecl }
281 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
282 { ImportDecl (mkSrcModuleFS $5) $3 $4 $6 $7 $2 }
284 maybe_src :: { WhereFrom }
285 : '{-# SOURCE' '#-}' { ImportByUserSource }
286 | {- empty -} { ImportByUser }
288 optqualified :: { Bool }
289 : 'qualified' { True }
290 | {- empty -} { False }
292 maybeas :: { Maybe ModuleName }
293 : 'as' modid { Just $2 }
294 | {- empty -} { Nothing }
296 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
297 : impspec { Just $1 }
298 | {- empty -} { Nothing }
300 impspec :: { (Bool, [RdrNameIE]) }
301 : '(' exportlist ')' { (False, reverse $2) }
302 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
304 -----------------------------------------------------------------------------
305 -- Fixity Declarations
309 | INTEGER {% checkPrec $1 `thenP_`
310 returnP (fromInteger $1) }
312 infix :: { FixityDirection }
314 | 'infixl' { InfixL }
315 | 'infixr' { InfixR }
318 : ops ',' op { $3 : $1 }
321 -----------------------------------------------------------------------------
322 -- Top-Level Declarations
324 topdecls :: { [RdrBinding] }
325 : topdecls ';' topdecl { ($3 : $1) }
326 | topdecls ';' { $1 }
329 topdecl :: { RdrBinding }
330 : srcloc 'type' simpletype '=' sigtype
331 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
333 | srcloc 'data' ctype '=' constrs deriving
334 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
335 returnP (RdrHsDecl (TyClD
336 (TyData DataType cs c ts (reverse $5) (length $5) $6
337 NoDataPragmas $1))) }
339 | srcloc 'newtype' ctype '=' newconstr deriving
340 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
341 returnP (RdrHsDecl (TyClD
342 (TyData NewType cs c ts [$5] 1 $6
343 NoDataPragmas $1))) }
345 | srcloc 'class' ctype fds where
346 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
348 = cvMonoBindsAndSigs cvClassOpSig
351 returnP (RdrHsDecl (TyClD
352 (mkClassDecl cs c ts $4 sigs binds
353 NoClassPragmas $1))) }
355 | srcloc 'instance' inst_type where
357 = cvMonoBindsAndSigs cvInstDeclSig
360 (InstDecl $3 binds sigs dummyRdrVarName $1)) }
362 | srcloc 'default' '(' types0 ')'
363 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
365 | srcloc 'foreign' 'import' callconv ext_name
366 unsafe_flag varid_no_unsafe '::' sigtype
367 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
369 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
370 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
372 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
373 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
374 defaultCallConv $1)) }
376 | '{-# DEPRECATED' deprecations '#-}' { $2 }
377 | '{-# RULES' rules '#-}' { $2 }
380 decls :: { [RdrBinding] }
381 : decls ';' decl { $3 : $1 }
386 decl :: { RdrBinding }
389 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
390 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
391 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
392 { foldr1 RdrAndBindings
393 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
394 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
395 { RdrSig (SpecInstSig $4 $2) }
397 opt_phase :: { Maybe Int }
398 : INTEGER { Just (fromInteger $1) }
399 | {- empty -} { Nothing }
401 wherebinds :: { RdrNameHsBinds }
402 : where { cvBinds cvValSig (groupBindings $1) }
404 where :: { [RdrBinding] }
405 : 'where' decllist { $2 }
408 declbinds :: { RdrNameHsBinds }
409 : decllist { cvBinds cvValSig (groupBindings $1) }
411 decllist :: { [RdrBinding] }
412 : '{' decls '}' { $2 }
413 | layout_on decls close { $2 }
415 fixdecl :: { RdrBinding }
416 : srcloc infix prec ops { foldr1 RdrAndBindings
417 [ RdrSig (FixSig (FixitySig n
421 -----------------------------------------------------------------------------
422 -- Transformation Rules
424 rules :: { RdrBinding }
425 : rules ';' rule { $1 `RdrAndBindings` $3 }
428 | {- empty -} { RdrNullBind }
430 rule :: { RdrBinding }
431 : STRING rule_forall fexp '=' srcloc exp
432 { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
434 rule_forall :: { [RdrNameRuleBndr] }
435 : 'forall' rule_var_list '.' { $2 }
438 rule_var_list :: { [RdrNameRuleBndr] }
440 | rule_var rule_var_list { $1 : $2 }
442 rule_var :: { RdrNameRuleBndr }
443 : varid { RuleBndr $1 }
444 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
446 -----------------------------------------------------------------------------
449 deprecations :: { RdrBinding }
450 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
451 | deprecations ';' { $1 }
453 | {- empty -} { RdrNullBind }
455 -- SUP: TEMPORARY HACK, not checking for `module Foo'
456 deprecation :: { RdrBinding }
457 : srcloc exportlist STRING
458 { foldr RdrAndBindings RdrNullBind
459 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
461 -----------------------------------------------------------------------------
462 -- Foreign import/export
465 : 'stdcall' { stdCallConv }
466 | 'ccall' { cCallConv }
467 | {- empty -} { defaultCallConv }
469 unsafe_flag :: { Bool }
471 | {- empty -} { False }
473 ext_name :: { Maybe ExtName }
474 : 'dynamic' { Just Dynamic }
475 | STRING { Just (ExtName $1 Nothing) }
476 | STRING STRING { Just (ExtName $2 (Just $1)) }
477 | {- empty -} { Nothing }
480 -----------------------------------------------------------------------------
483 opt_sig :: { Maybe RdrNameHsType }
484 : {- empty -} { Nothing }
485 | '::' sigtype { Just $2 }
487 opt_asig :: { Maybe RdrNameHsType }
488 : {- empty -} { Nothing }
489 | '::' atype { Just $2 }
491 sigtypes :: { [RdrNameHsType] }
493 | sigtypes ',' sigtype { $3 : $1 }
495 sigtype :: { RdrNameHsType }
496 : ctype { mkHsForAllTy Nothing [] $1 }
498 sig_vars :: { [RdrName] }
499 : sig_vars ',' var { $3 : $1 }
502 -----------------------------------------------------------------------------
505 -- A ctype is a for-all type
506 ctype :: { RdrNameHsType }
507 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
508 | context type { mkHsForAllTy Nothing $1 $2 }
509 -- A type of form (context => type) is an *implicit* HsForAllTy
512 type :: { RdrNameHsType }
513 : btype '->' type { HsFunTy $1 $3 }
514 | ipvar '::' type { mkHsIParamTy $1 $3 }
517 btype :: { RdrNameHsType }
518 : btype atype { HsAppTy $1 $2 }
521 atype :: { RdrNameHsType }
522 : gtycon { HsTyVar $1 }
523 | tyvar { HsTyVar $1 }
524 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
525 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
526 | '[' type ']' { HsListTy $2 }
527 | '(' ctype ')' { $2 }
529 gtycon :: { RdrName }
531 | '(' ')' { unitTyCon_RDR }
532 | '(' '->' ')' { funTyCon_RDR }
533 | '[' ']' { listTyCon_RDR }
534 | '(' commas ')' { tupleTyCon_RDR $2 }
536 -- An inst_type is what occurs in the head of an instance decl
537 -- e.g. (Foo a, Gaz b) => Wibble a b
538 -- It's kept as a single type, with a MonoDictTy at the right
539 -- hand corner, for convenience.
540 inst_type :: { RdrNameHsType }
541 : ctype {% checkInstType $1 }
543 types0 :: { [RdrNameHsType] }
547 types :: { [RdrNameHsType] }
549 | types ',' type { $3 : $1 }
551 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
552 : tycon tyvars { ($1, reverse $2) }
554 tyvars :: { [RdrNameHsTyVar] }
555 : tyvars tyvar { UserTyVar $2 : $1 }
558 fds :: { [([RdrName], [RdrName])] }
560 | '|' fds1 { reverse $2 }
562 fds1 :: { [([RdrName], [RdrName])] }
563 : fds1 ',' fd { $3 : $1 }
566 fd :: { ([RdrName], [RdrName]) }
567 : varids0 '->' varids0 { (reverse $1, reverse $3) }
569 varids0 :: { [RdrName] }
571 | varids0 tyvar { $2 : $1 }
573 -----------------------------------------------------------------------------
574 -- Datatype declarations
576 constrs :: { [RdrNameConDecl] }
577 : constrs '|' constr { $3 : $1 }
580 constr :: { RdrNameConDecl }
581 : srcloc forall context constr_stuff
582 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
583 | srcloc forall constr_stuff
584 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
586 forall :: { [RdrNameHsTyVar] }
587 : 'forall' tyvars '.' { $2 }
590 context :: { RdrNameContext }
591 : btype '=>' {% checkContext $1 }
593 constr_stuff :: { (RdrName, RdrNameConDetails) }
594 : scontype { (fst $1, VanillaCon (snd $1)) }
595 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
596 | con '{' fielddecls '}' { ($1, RecCon (reverse $3)) }
598 newconstr :: { RdrNameConDecl }
599 : srcloc conid atype { mkConDecl $2 [] [] (NewCon $3 Nothing) $1 }
600 | srcloc conid '{' var '::' type '}'
601 { mkConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
603 scontype :: { (RdrName, [RdrNameBangType]) }
604 : btype {% splitForConApp $1 [] }
607 scontype1 :: { (RdrName, [RdrNameBangType]) }
608 : btype '!' atype {% splitForConApp $1 [Banged $3] }
609 | scontype1 satype { (fst $1, snd $1 ++ [$2] ) }
611 satype :: { RdrNameBangType }
612 : atype { Unbanged $1 }
613 | '!' atype { Banged $2 }
615 sbtype :: { RdrNameBangType }
616 : btype { Unbanged $1 }
617 | '!' atype { Banged $2 }
619 fielddecls :: { [([RdrName],RdrNameBangType)] }
620 : fielddecls ',' fielddecl { $3 : $1 }
623 fielddecl :: { ([RdrName],RdrNameBangType) }
624 : sig_vars '::' stype { (reverse $1, $3) }
626 stype :: { RdrNameBangType }
627 : ctype { Unbanged $1 }
628 | '!' atype { Banged $2 }
630 deriving :: { Maybe [RdrName] }
631 : {- empty -} { Nothing }
632 | 'deriving' qtycls { Just [$2] }
633 | 'deriving' '(' ')' { Just [] }
634 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
636 dclasses :: { [RdrName] }
637 : dclasses ',' qtycls { $3 : $1 }
640 -----------------------------------------------------------------------------
643 {- There's an awkward overlap with a type signature. Consider
644 f :: Int -> Int = ...rhs...
645 Then we can't tell whether it's a type signature or a value
646 definition with a result signature until we see the '='.
647 So we have to inline enough to postpone reductions until we know.
651 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
652 instead of qvar, we get another shift/reduce-conflict. Consider the
655 { (^^) :: Int->Int ; } Type signature; only var allowed
657 { (^^) :: Int->Int = ... ; } Value defn with result signature;
658 qvar allowed (because of instance decls)
660 We can't tell whether to reduce var to qvar until after we've read the signatures.
663 valdef :: { RdrBinding }
664 : infixexp srcloc opt_sig rhs {% checkValDef $1 $3 $4 $2 }
665 | infixexp srcloc '::' sigtype {% checkValSig $1 $4 $2 }
666 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
667 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
670 rhs :: { RdrNameGRHSs }
671 : '=' srcloc exp wherebinds { GRHSs (unguardedRHS $3 $2)
673 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
675 gdrhs :: { [RdrNameGRHS] }
676 : gdrhs gdrh { $2 : $1 }
679 gdrh :: { RdrNameGRHS }
680 : '|' srcloc quals '=' exp { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
682 -----------------------------------------------------------------------------
685 exp :: { RdrNameHsExpr }
686 : infixexp '::' sigtype { ExprWithTySig $1 $3 }
687 | infixexp 'with' dbinding { HsWith $1 $3 }
690 infixexp :: { RdrNameHsExpr }
692 | infixexp qop exp10 { OpApp $1 $2 (panic "fixity") $3 }
694 exp10 :: { RdrNameHsExpr }
695 : '\\' aexp aexps opt_asig '->' srcloc exp
696 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
697 returnP (HsLam (Match [] ps $4
698 (GRHSs (unguardedRHS $7 $6)
699 EmptyBinds Nothing))) }
700 | 'let' declbinds 'in' exp { HsLet $2 $4 }
701 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
702 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
703 | '-' fexp { NegApp $2 (error "NegApp") }
704 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
706 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 False False cbot }
707 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 True False cbot }
708 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 False True cbot }
709 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 True True cbot }
711 | '_scc_' STRING exp { if opt_SccProfilingOn
717 ccallid :: { FAST_STRING }
721 fexp :: { RdrNameHsExpr }
722 : fexp aexp { HsApp $1 $2 }
725 aexps0 :: { [RdrNameHsExpr] }
726 : aexps { reverse $1 }
728 aexps :: { [RdrNameHsExpr] }
729 : aexps aexp { $2 : $1 }
732 aexp :: { RdrNameHsExpr }
733 : aexp '{' fbinds '}' {% mkRecConstrOrUpdate $1 (reverse $3) }
736 aexp1 :: { RdrNameHsExpr }
738 | ipvar { HsIPVar $1 }
740 | literal { HsLit $1 }
741 | '(' exp ')' { HsPar $2 }
742 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
743 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
744 | '[' list ']' { $2 }
745 | '(' infixexp qop ')' { SectionL $2 $3 }
746 | '(' qopm infixexp ')' { SectionR $2 $3 }
747 | qvar '@' aexp { EAsPat $1 $3 }
749 | '~' aexp1 { ELazyPat $2 }
752 : commas ',' { $1 + 1 }
755 texps :: { [RdrNameHsExpr] }
756 : 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 '|' quals { HsDo ListComp (reverse
773 (ReturnStmt $1 : $4)) $2 }
775 lexps :: { [RdrNameHsExpr] }
776 : lexps ',' exp { $3 : $1 }
777 | exp ',' exp { [$3,$1] }
779 -----------------------------------------------------------------------------
780 -- List Comprehensions
782 quals :: { [RdrNameStmt] }
783 : quals ',' qual { $3 : $1 }
786 qual :: { RdrNameStmt }
787 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
788 returnP (BindStmt p $4 $1) }
789 | srcloc exp { GuardStmt $2 $1 }
790 | srcloc 'let' declbinds { LetStmt $3 }
792 -----------------------------------------------------------------------------
795 altslist :: { [RdrNameMatch] }
796 : '{' alts '}' { reverse $2 }
797 | layout_on alts close { reverse $2 }
799 alts :: { [RdrNameMatch] }
803 alts1 :: { [RdrNameMatch] }
804 : alts1 ';' alt { $3 : $1 }
808 alt :: { RdrNameMatch }
809 : infixexp opt_sig ralt wherebinds
810 {% checkPattern $1 `thenP` \p ->
811 returnP (Match [] [p] $2
812 (GRHSs $3 $4 Nothing)) }
814 ralt :: { [RdrNameGRHS] }
815 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
816 | gdpats { (reverse $1) }
818 gdpats :: { [RdrNameGRHS] }
819 : gdpats gdpat { $2 : $1 }
822 gdpat :: { RdrNameGRHS }
823 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
825 -----------------------------------------------------------------------------
826 -- Statement sequences
828 stmtlist :: { [RdrNameStmt] }
829 : '{' stmts '}' { reverse $2 }
830 | layout_on_for_do stmts close { reverse $2 }
832 -- Stmt list should really end in an expression, but it's not
833 -- convenient to enforce this here, so we throw out erroneous
834 -- statement sequences in the renamer instead.
836 stmts :: { [RdrNameStmt] }
840 stmts1 :: { [RdrNameStmt] }
841 : stmts1 ';' stmt { $3 : $1 }
845 stmt :: { RdrNameStmt }
846 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
847 returnP (BindStmt p $4 $1) }
848 | srcloc exp { ExprStmt $2 $1 }
849 | srcloc 'let' declbinds { LetStmt $3 }
851 -----------------------------------------------------------------------------
852 -- Record Field Update/Construction
854 fbinds :: { RdrNameHsRecordBinds }
855 : fbinds ',' fbind { $3 : $1 }
860 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
861 : qvar '=' exp { ($1,$3,False) }
863 -----------------------------------------------------------------------------
864 -- Implicit Parameter Bindings
866 dbinding :: { [(RdrName, RdrNameHsExpr)] }
867 : '{' dbinds '}' { $2 }
868 | layout_on dbinds close { $2 }
870 dbinds :: { [(RdrName, RdrNameHsExpr)] }
871 : dbinds ';' dbind { $3 : $1 }
876 dbind :: { (RdrName, RdrNameHsExpr) }
877 dbind : ipvar '=' exp { ($1, $3) }
879 -----------------------------------------------------------------------------
880 -- Variables, Constructors and Operators.
883 : '(' ')' { unitCon_RDR }
884 | '[' ']' { nilCon_RDR }
885 | '(' commas ')' { tupleCon_RDR $2 }
890 | '(' varsym ')' { $2 }
894 | '(' varsym ')' { $2 }
895 | '(' qvarsym1 ')' { $2 }
896 -- We've inlined qvarsym here so that the decision about
897 -- whether it's a qvar or a var can be postponed until
898 -- *after* we see the close paren.
901 : IPVARID { (mkSrcUnqual ipName (tailFS $1)) }
905 | '(' consym ')' { $2 }
909 | '(' qconsym ')' { $2 }
913 | '`' varid '`' { $2 }
915 qvarop :: { RdrName }
917 | '`' qvarid '`' { $2 }
919 qvaropm :: { RdrName }
921 | '`' qvarid '`' { $2 }
925 | '`' conid '`' { $2 }
927 qconop :: { RdrName }
929 | '`' qconid '`' { $2 }
931 -----------------------------------------------------------------------------
934 op :: { RdrName } -- used in infix decls
938 qop :: { RdrNameHsExpr } -- used in sections
939 : qvarop { HsVar $1 }
940 | qconop { HsVar $1 }
942 qopm :: { RdrNameHsExpr } -- used in sections
943 : qvaropm { HsVar $1 }
944 | qconop { HsVar $1 }
946 -----------------------------------------------------------------------------
949 qvarid :: { RdrName }
951 | QVARID { case $1 of { (mod,n) ->
952 mkSrcQual varName mod n } }
955 : VARID { mkSrcUnqual varName $1 }
956 | 'as' { as_var_RDR }
957 | 'qualified' { qualified_var_RDR }
958 | 'hiding' { hiding_var_RDR }
959 | 'forall' { forall_var_RDR }
960 | 'export' { export_var_RDR }
961 | 'label' { label_var_RDR }
962 | 'dynamic' { dynamic_var_RDR }
963 | 'unsafe' { unsafe_var_RDR }
964 | 'stdcall' { stdcall_var_RDR }
965 | 'ccall' { ccall_var_RDR }
967 varid_no_unsafe :: { RdrName }
968 : VARID { mkSrcUnqual varName $1 }
969 | 'as' { as_var_RDR }
970 | 'qualified' { qualified_var_RDR }
971 | 'hiding' { hiding_var_RDR }
972 | 'forall' { forall_var_RDR }
973 | 'export' { export_var_RDR }
974 | 'label' { label_var_RDR }
975 | 'dynamic' { dynamic_var_RDR }
976 | 'stdcall' { stdcall_var_RDR }
977 | 'ccall' { ccall_var_RDR }
979 -----------------------------------------------------------------------------
982 qconid :: { RdrName }
984 | QCONID { case $1 of { (mod,n) ->
985 mkSrcQual dataName mod n } }
988 : CONID { mkSrcUnqual dataName $1 }
990 -----------------------------------------------------------------------------
993 qconsym :: { RdrName }
995 | QCONSYM { case $1 of { (mod,n) ->
996 mkSrcQual dataName mod n } }
998 consym :: { RdrName }
999 : CONSYM { mkSrcUnqual dataName $1 }
1001 -----------------------------------------------------------------------------
1004 qvarsym :: { RdrName }
1008 qvarsymm :: { RdrName }
1012 varsym :: { RdrName }
1013 : VARSYM { mkSrcUnqual varName $1 }
1018 varsymm :: { RdrName } -- varsym not including '-'
1019 : VARSYM { mkSrcUnqual varName $1 }
1023 qvarsym1 :: { RdrName }
1024 : QVARSYM { case $1 of { (mod,n) ->
1025 mkSrcQual varName mod n } }
1027 literal :: { HsLit }
1028 : INTEGER { HsInt $1 }
1029 | CHAR { HsChar $1 }
1030 | RATIONAL { HsFrac $1 }
1031 | STRING { HsString $1 }
1033 | PRIMINTEGER { HsIntPrim $1 }
1034 | PRIMCHAR { HsCharPrim $1 }
1035 | PRIMSTRING { HsStringPrim $1 }
1036 | PRIMFLOAT { HsFloatPrim $1 }
1037 | PRIMDOUBLE { HsDoublePrim $1 }
1038 | CLITLIT { HsLitLit $1 }
1040 srcloc :: { SrcLoc } : {% getSrcLocP }
1042 -----------------------------------------------------------------------------
1046 : vccurly { () } -- context popped in lexer.
1047 | error {% popContext }
1049 layout_on :: { () } : {% layoutOn True{-strict-} }
1050 layout_on_for_do :: { () } : {% layoutOn False }
1052 -----------------------------------------------------------------------------
1053 -- Miscellaneous (mostly renamings)
1055 modid :: { ModuleName }
1056 : CONID { mkSrcModuleFS $1 }
1058 tycon :: { RdrName }
1059 : CONID { mkSrcUnqual tcClsName $1 }
1061 qtycon :: { RdrName }
1063 | QCONID { case $1 of { (mod,n) ->
1064 mkSrcQual tcClsName mod n } }
1066 qtycls :: { RdrName }
1069 tyvar :: { RdrName }
1070 : VARID { mkSrcUnqual tvName $1 }
1071 | 'as' { as_tyvar_RDR }
1072 | 'qualified' { qualified_tyvar_RDR }
1073 | 'hiding' { hiding_tyvar_RDR }
1074 | 'export' { export_tyvar_RDR }
1075 | 'label' { label_tyvar_RDR }
1076 | 'dynamic' { dynamic_tyvar_RDR }
1077 | 'unsafe' { unsafe_tyvar_RDR }
1078 | 'stdcall' { stdcall_tyvar_RDR }
1079 | 'ccall' { ccall_tyvar_RDR }
1080 -- NOTE: no 'forall'
1082 -----------------------------------------------------------------------------
1086 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)