2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.25 2000/02/28 09:17:54 simonmar Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parse ) where
21 import PrelMods ( mAIN_Name )
22 import OccName ( varName, ipName, dataName, tcClsName, tvName )
23 import SrcLoc ( SrcLoc )
26 import CmdLineOpts ( opt_SccProfilingOn )
27 import BasicTypes ( Fixity(..), FixityDirection(..), NewOrData(..) )
32 #include "HsVersions.h"
36 -----------------------------------------------------------------------------
37 Conflicts: 14 shift/reduce
38 (note: it's currently 21 -- JRL, 31/1/2000)
39 (note2: it's currently 36, but not because of me -- SUP, 15/2/2000 :-)
41 8 for abiguity in 'if x then y else z + 1'
42 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
43 1 for ambiguity in 'if x then y else z :: T'
44 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
45 3 for ambiguity in 'case x of y :: a -> b'
46 (don't know whether to reduce 'a' as a btype or shift the '->'.
47 conclusion: bogus expression anyway, doesn't matter)
49 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
50 since 'forall' is a valid variable name, we don't know whether
51 to treat a forall on the input as the beginning of a quantifier
52 or the beginning of the rule itself. Resolving to shift means
53 it's always treated as a quantifier, hence the above is disallowed.
54 This saves explicitly defining a grammar for the rule lhs that
55 doesn't include 'forall'.
57 1 for ambiguity in 'x @ Rec{..}'.
58 Only sensible parse is 'x @ (Rec{..})', which is what resolving
61 -----------------------------------------------------------------------------
65 '_' { ITunderscore } -- Haskell keywords
70 'default' { ITdefault }
71 'deriving' { ITderiving }
81 'instance' { ITinstance }
84 'newtype' { ITnewtype }
86 'qualified' { ITqualified }
92 'forall' { ITforall } -- GHC extension keywords
93 'foreign' { ITforeign }
96 'dynamic' { ITdynamic }
99 'stdcall' { ITstdcallconv }
100 'ccall' { ITccallconv }
101 '_ccall_' { ITccall (False, False, False) }
102 '_ccall_GC_' { ITccall (False, False, True) }
103 '_casm_' { ITccall (False, True, False) }
104 '_casm_GC_' { ITccall (False, True, True) }
106 '{-# SPECIALISE' { ITspecialise_prag }
107 '{-# SOURCE' { ITsource_prag }
108 '{-# INLINE' { ITinline_prag }
109 '{-# NOINLINE' { ITnoinline_prag }
110 '{-# RULES' { ITrules_prag }
111 '{-# DEPRECATED' { ITdeprecated_prag }
112 '#-}' { ITclose_prag }
115 '__interface' { ITinterface } -- interface keywords
116 '__export' { IT__export }
117 '__instimport' { ITinstimport }
118 '__forall' { IT__forall }
119 '__letrec' { ITletrec }
120 '__coerce' { ITcoerce }
121 '__depends' { ITdepends }
122 '__inline' { ITinline }
123 '__DEFAULT' { ITdefaultbranch }
125 '__integer' { ITinteger_lit }
126 '__float' { ITfloat_lit }
127 '__rational' { ITrational_lit }
128 '__addr' { ITaddr_lit }
129 '__litlit' { ITlit_lit }
130 '__string' { ITstring_lit }
131 '__ccall' { ITccall $$ }
133 '__sccC' { ITsccAllCafs }
136 '__P' { ITspecialise }
138 '__U' { ITunfold $$ }
139 '__S' { ITstrict $$ }
140 '__M' { ITcprinfo $$ }
143 '..' { ITdotdot } -- reserved symbols
157 '/\\' { ITbiglam } -- GHC-extension 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
183 PRAGMA { ITpragma $$ }
186 STRING { ITstring $$ }
187 INTEGER { ITinteger $$ }
188 RATIONAL { ITrational $$ }
190 PRIMCHAR { ITprimchar $$ }
191 PRIMSTRING { ITprimstring $$ }
192 PRIMINTEGER { ITprimint $$ }
193 PRIMFLOAT { ITprimfloat $$ }
194 PRIMDOUBLE { ITprimdouble $$ }
195 CLITLIT { ITlitlit $$ }
197 UNKNOWN { ITunknown $$ }
199 %monad { P } { thenP } { returnP }
200 %lexer { lexer } { ITeof }
205 -----------------------------------------------------------------------------
208 -- The place for module deprecation is really too restrictive, but if it
209 -- was allowed at its natural place just before 'module', we get an ugly
210 -- s/r conflict with the second alternative. Another solution would be the
211 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
212 -- either, and DEPRECATED is only expected to be used by people who really
213 -- know what they are doing. :-)
215 module :: { RdrNameHsModule }
216 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
217 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
219 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
221 maybemoddeprec :: { Maybe DeprecTxt }
222 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
223 | {- empty -} { Nothing }
225 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
227 | layout_on top close { $2 }
229 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
230 : importdecls ';' cvtopdecls { (reverse $1,$3) }
231 | importdecls { (reverse $1,[]) }
232 | cvtopdecls { ([],$1) }
234 cvtopdecls :: { [RdrNameHsDecl] }
235 : topdecls { cvTopDecls (groupBindings $1)}
237 -----------------------------------------------------------------------------
240 maybeexports :: { Maybe [RdrNameIE] }
241 : '(' exportlist ')' { Just $2 }
242 | {- empty -} { Nothing }
244 exportlist :: { [RdrNameIE] }
245 : exportlist ',' export { $3 : $1 }
246 | exportlist ',' { $1 }
250 -- GHC extension: we allow things like [] and (,,,) to be exported
251 export :: { RdrNameIE }
253 | gtycon { IEThingAbs $1 }
254 | gtycon '(' '..' ')' { IEThingAll $1 }
255 | gtycon '(' ')' { IEThingWith $1 [] }
256 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
257 | 'module' modid { IEModuleContents $2 }
259 qcnames :: { [RdrName] }
260 : qcnames ',' qcname { $3 : $1 }
263 qcname :: { RdrName }
267 -----------------------------------------------------------------------------
268 -- Import Declarations
270 -- import decls can be *empty*, or even just a string of semicolons
271 -- whereas topdecls must contain at least one topdecl.
273 importdecls :: { [RdrNameImportDecl] }
274 : importdecls ';' importdecl { $3 : $1 }
275 | importdecls ';' { $1 }
276 | importdecl { [ $1 ] }
279 importdecl :: { RdrNameImportDecl }
280 : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec
281 { ImportDecl (mkSrcModuleFS $5) $3 $4 $6 $7 $2 }
283 maybe_src :: { WhereFrom }
284 : '{-# SOURCE' '#-}' { ImportByUserSource }
285 | {- empty -} { ImportByUser }
287 optqualified :: { Bool }
288 : 'qualified' { True }
289 | {- empty -} { False }
291 maybeas :: { Maybe ModuleName }
292 : 'as' modid { Just $2 }
293 | {- empty -} { Nothing }
295 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
296 : impspec { Just $1 }
297 | {- empty -} { Nothing }
299 impspec :: { (Bool, [RdrNameIE]) }
300 : '(' exportlist ')' { (False, reverse $2) }
301 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
303 -----------------------------------------------------------------------------
304 -- Fixity Declarations
308 | INTEGER {% checkPrec $1 `thenP_`
309 returnP (fromInteger $1) }
311 infix :: { FixityDirection }
313 | 'infixl' { InfixL }
314 | 'infixr' { InfixR }
317 : ops ',' op { $3 : $1 }
320 -----------------------------------------------------------------------------
321 -- Top-Level Declarations
323 topdecls :: { [RdrBinding] }
324 : topdecls ';' topdecl { ($3 : $1) }
325 | topdecls ';' { $1 }
328 topdecl :: { RdrBinding }
329 : srcloc 'type' simpletype '=' type
330 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
332 | srcloc 'data' ctype '=' constrs deriving
333 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
334 returnP (RdrHsDecl (TyClD
335 (TyData DataType cs c ts (reverse $5) $6
336 NoDataPragmas $1))) }
338 | srcloc 'newtype' ctype '=' newconstr deriving
339 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
340 returnP (RdrHsDecl (TyClD
341 (TyData NewType cs c ts [$5] $6
342 NoDataPragmas $1))) }
344 | srcloc 'class' ctype fds where
345 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
347 = cvMonoBindsAndSigs cvClassOpSig
350 returnP (RdrHsDecl (TyClD
351 (mkClassDecl cs c ts $4 sigs binds
352 NoClassPragmas $1))) }
354 | srcloc 'instance' inst_type where
356 = cvMonoBindsAndSigs cvInstDeclSig
359 (InstDecl $3 binds sigs dummyRdrVarName $1)) }
361 | srcloc 'default' '(' types0 ')'
362 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
364 | srcloc 'foreign' 'import' callconv ext_name
365 unsafe_flag varid_no_unsafe '::' sigtype
366 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
368 | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
369 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
371 | srcloc 'foreign' 'label' ext_name varid '::' sigtype
372 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
373 defaultCallConv $1)) }
377 decls :: { [RdrBinding] }
378 : decls ';' decl { $3 : $1 }
383 decl :: { RdrBinding }
386 | valdef { RdrValBinding $1 }
387 | '{-# INLINE' srcloc opt_phase qvar '#-}' { RdrSig (InlineSig $4 $3 $2) }
388 | '{-# NOINLINE' srcloc opt_phase qvar '#-}' { RdrSig (NoInlineSig $4 $3 $2) }
389 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
390 { foldr1 RdrAndBindings
391 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
392 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
393 { RdrSig (SpecInstSig $4 $2) }
394 | '{-# RULES' rules '#-}' { $2 }
395 | '{-# DEPRECATED' deprecations '#-}' { $2 }
397 opt_phase :: { Maybe Int }
398 : INTEGER { Just (fromInteger $1) }
399 | {- empty -} { Nothing }
401 sigtypes :: { [RdrNameHsType] }
403 | sigtypes ',' sigtype { $3 : $1 }
405 wherebinds :: { RdrNameHsBinds }
406 : where { cvBinds cvValSig (groupBindings $1) }
408 where :: { [RdrBinding] }
409 : 'where' decllist { $2 }
412 declbinds :: { RdrNameHsBinds }
413 : decllist { cvBinds cvValSig (groupBindings $1) }
415 decllist :: { [RdrBinding] }
416 : '{' decls '}' { $2 }
417 | layout_on decls close { $2 }
419 fixdecl :: { RdrBinding }
420 : srcloc infix prec ops { foldr1 RdrAndBindings
421 [ RdrSig (FixSig (FixitySig n
425 signdecl :: { RdrBinding }
426 : vars srcloc '::' sigtype { foldr1 RdrAndBindings
427 [ RdrSig (Sig n $4 $2) | n <- $1 ] }
429 sigtype :: { RdrNameHsType }
430 : ctype { mkHsForAllTy Nothing [] $1 }
433 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
434 instead of qvar, we get another shift/reduce-conflict. Consider the
437 { (+) :: ... } only var
438 { (+) x y = ... } could (incorrectly) be qvar
440 We re-use expressions for patterns, so a qvar would be allowed in patterns
441 instead of a var only (which would be correct). But deciding what the + is,
442 would require more lookahead. So let's check for ourselves...
445 vars :: { [RdrName] }
446 : vars ',' var { $3 : $1 }
449 -----------------------------------------------------------------------------
450 -- Transformation Rules
452 rules :: { RdrBinding }
453 : rules ';' rule { $1 `RdrAndBindings` $3 }
456 | {- empty -} { RdrNullBind }
458 rule :: { RdrBinding }
459 : STRING rule_forall fexp '=' srcloc exp
460 { RdrHsDecl (RuleD (RuleDecl $1 [] $2 $3 $6 $5)) }
462 rule_forall :: { [RdrNameRuleBndr] }
463 : 'forall' rule_var_list '.' { $2 }
466 rule_var_list :: { [RdrNameRuleBndr] }
468 | rule_var rule_var_list { $1 : $2 }
470 rule_var :: { RdrNameRuleBndr }
471 : varid { RuleBndr $1 }
472 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
474 -----------------------------------------------------------------------------
477 deprecations :: { RdrBinding }
478 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
479 | deprecations ';' { $1 }
481 | {- empty -} { RdrNullBind }
483 -- SUP: TEMPORARY HACK, not checking for `module Foo'
484 deprecation :: { RdrBinding }
485 : srcloc exportlist STRING
486 { foldr1 RdrAndBindings [ RdrSig (DeprecSig (Deprecation n $3) $1) | n <- $2 ] }
488 -----------------------------------------------------------------------------
489 -- Foreign import/export
492 : 'stdcall' { stdCallConv }
493 | 'ccall' { cCallConv }
494 | {- empty -} { defaultCallConv }
496 unsafe_flag :: { Bool }
498 | {- empty -} { False }
500 ext_name :: { Maybe ExtName }
501 : 'dynamic' { Just Dynamic }
502 | STRING { Just (ExtName $1 Nothing) }
503 | STRING STRING { Just (ExtName $2 (Just $1)) }
504 | {- empty -} { Nothing }
506 -----------------------------------------------------------------------------
509 -- A ctype is a for-all type
510 ctype :: { RdrNameHsType }
511 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
512 | context type { mkHsForAllTy Nothing $1 $2 }
513 -- A type of form (context => type) is an *implicit* HsForAllTy
516 type :: { RdrNameHsType }
517 : btype '->' type { MonoFunTy $1 $3 }
518 | IPVARID '::' type { MonoIParamTy (mkSrcUnqual ipName $1) $3 }
521 btype :: { RdrNameHsType }
522 : btype atype { MonoTyApp $1 $2 }
525 atype :: { RdrNameHsType }
526 : gtycon { MonoTyVar $1 }
527 | tyvar { MonoTyVar $1 }
528 | '(' type ',' types ')' { MonoTupleTy ($2 : reverse $4) True }
529 | '(#' types '#)' { MonoTupleTy (reverse $2) False }
530 | '[' type ']' { MonoListTy $2 }
531 | '(' ctype ')' { $2 }
533 gtycon :: { RdrName }
535 | '(' ')' { unitTyCon_RDR }
536 | '(' '->' ')' { funTyCon_RDR }
537 | '[' ']' { listTyCon_RDR }
538 | '(' commas ')' { tupleTyCon_RDR $2 }
540 -- An inst_type is what occurs in the head of an instance decl
541 -- e.g. (Foo a, Gaz b) => Wibble a b
542 -- It's kept as a single type, with a MonoDictTy at the right
543 -- hand corner, for convenience.
544 inst_type :: { RdrNameHsType }
545 : ctype {% checkInstType $1 }
547 types0 :: { [RdrNameHsType] }
551 types :: { [RdrNameHsType] }
553 | types ',' type { $3 : $1 }
555 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
556 : tycon tyvars { ($1, reverse $2) }
558 tyvars :: { [RdrNameHsTyVar] }
559 : tyvars tyvar { UserTyVar $2 : $1 }
562 fds :: { [([RdrName], [RdrName])] }
564 | '|' fds1 { reverse $2 }
566 fds1 :: { [([RdrName], [RdrName])] }
567 : fds1 ',' fd { $3 : $1 }
570 fd :: { ([RdrName], [RdrName]) }
571 : varids0 '->' varids0 { (reverse $1, reverse $3) }
573 varids0 :: { [RdrName] }
575 | varids0 tyvar { $2 : $1 }
577 -----------------------------------------------------------------------------
578 -- Datatype declarations
580 constrs :: { [RdrNameConDecl] }
581 : constrs '|' constr { $3 : $1 }
584 constr :: { RdrNameConDecl }
585 : srcloc forall context constr_stuff
586 { ConDecl (fst $4) $2 $3 (snd $4) $1 }
587 | srcloc forall constr_stuff
588 { ConDecl (fst $3) $2 [] (snd $3) $1 }
590 forall :: { [RdrNameHsTyVar] }
591 : 'forall' tyvars '.' { $2 }
594 context :: { RdrNameContext }
595 : btype '=>' {% checkContext $1 }
597 constr_stuff :: { (RdrName, RdrNameConDetails) }
598 : scontype { (fst $1, VanillaCon (snd $1)) }
599 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
600 | con '{' fielddecls '}' { ($1, RecCon (reverse $3)) }
602 newconstr :: { RdrNameConDecl }
603 : srcloc conid atype { ConDecl $2 [] [] (NewCon $3 Nothing) $1 }
604 | srcloc conid '{' var '::' type '}'
605 { ConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
607 scontype :: { (RdrName, [RdrNameBangType]) }
608 : btype {% splitForConApp $1 [] }
611 scontype1 :: { (RdrName, [RdrNameBangType]) }
612 : btype '!' atype {% splitForConApp $1 [Banged $3] }
613 | scontype1 satype { (fst $1, snd $1 ++ [$2] ) }
615 satype :: { RdrNameBangType }
616 : atype { Unbanged $1 }
617 | '!' atype { Banged $2 }
619 sbtype :: { RdrNameBangType }
620 : btype { Unbanged $1 }
621 | '!' atype { Banged $2 }
623 fielddecls :: { [([RdrName],RdrNameBangType)] }
624 : fielddecls ',' fielddecl { $3 : $1 }
627 fielddecl :: { ([RdrName],RdrNameBangType) }
628 : vars '::' stype { (reverse $1, $3) }
630 stype :: { RdrNameBangType }
631 : ctype { Unbanged $1 }
632 | '!' atype { Banged $2 }
634 deriving :: { Maybe [RdrName] }
635 : {- empty -} { Nothing }
636 | 'deriving' qtycls { Just [$2] }
637 | 'deriving' '(' ')' { Just [] }
638 | 'deriving' '(' dclasses ')' { Just (reverse $3) }
640 dclasses :: { [RdrName] }
641 : dclasses ',' qtycls { $3 : $1 }
644 -----------------------------------------------------------------------------
647 valdef :: { RdrNameMonoBinds }
648 : infixexp {-ToDo: opt_sig-} srcloc rhs
649 {% checkValDef $1 Nothing $3 $2 }
651 rhs :: { RdrNameGRHSs }
652 : '=' srcloc exp wherebinds { GRHSs (unguardedRHS $3 $2)
654 | gdrhs wherebinds { GRHSs (reverse $1) $2 Nothing }
656 gdrhs :: { [RdrNameGRHS] }
657 : gdrhs gdrh { $2 : $1 }
660 gdrh :: { RdrNameGRHS }
661 : '|' srcloc quals '=' exp { GRHS (reverse
662 (ExprStmt $5 $2 : $3)) $2 }
664 -----------------------------------------------------------------------------
667 exp :: { RdrNameHsExpr }
668 : infixexp '::' sigtype { ExprWithTySig $1 $3 }
669 | infixexp 'with' dbinding { HsWith $1 $3 }
672 infixexp :: { RdrNameHsExpr }
674 | infixexp qop exp10 { OpApp $1 $2 (panic "fixity") $3 }
676 exp10 :: { RdrNameHsExpr }
677 : '\\' aexp aexps opt_asig '->' srcloc exp
678 {% checkPatterns ($2 : reverse $3) `thenP` \ ps ->
679 returnP (HsLam (Match [] ps $4
680 (GRHSs (unguardedRHS $7 $6)
681 EmptyBinds Nothing))) }
682 | 'let' declbinds 'in' exp { HsLet $2 $4 }
683 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
684 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
685 | '-' fexp { NegApp $2 (error "NegApp") }
686 | srcloc 'do' stmtlist { HsDo DoStmt $3 $1 }
688 | '_ccall_' ccallid aexps0 { CCall $2 $3 False False cbot }
689 | '_ccall_GC_' ccallid aexps0 { CCall $2 $3 True False cbot }
690 | '_casm_' CLITLIT aexps0 { CCall $2 $3 False True cbot }
691 | '_casm_GC_' CLITLIT aexps0 { CCall $2 $3 True True cbot }
693 | '_scc_' STRING exp { if opt_SccProfilingOn
699 ccallid :: { FAST_STRING }
703 fexp :: { RdrNameHsExpr }
704 : fexp aexp { HsApp $1 $2 }
707 aexps0 :: { [RdrNameHsExpr] }
708 : aexps { reverse $1 }
710 aexps :: { [RdrNameHsExpr] }
711 : aexps aexp { $2 : $1 }
714 aexp :: { RdrNameHsExpr }
715 : aexp '{' fbinds '}' {% mkRecConstrOrUpdate $1 (reverse $3) }
718 aexp1 :: { RdrNameHsExpr }
720 | IPVARID { HsIPVar (mkSrcUnqual ipName $1) }
722 | literal { HsLit $1 }
723 | '(' exp ')' { HsPar $2 }
724 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) True }
725 | '(#' texps '#)' { ExplicitTuple (reverse $2) False }
726 | '[' list ']' { $2 }
727 | '(' infixexp qop ')' { SectionL $2 $3 }
728 | '(' qopm infixexp ')' { SectionR $2 $3 }
729 | qvar '@' aexp { EAsPat $1 $3 }
731 | '~' aexp1 { ELazyPat $2 }
734 : commas ',' { $1 + 1 }
737 texps :: { [RdrNameHsExpr] }
738 : texps ',' exp { $3 : $1 }
741 -----------------------------------------------------------------------------
744 -- The rules below are little bit contorted to keep lexps left-recursive while
745 -- avoiding another shift/reduce-conflict.
747 list :: { RdrNameHsExpr }
748 : exp { ExplicitList [$1] }
749 | lexps { ExplicitList (reverse $1) }
750 | exp '..' { ArithSeqIn (From $1) }
751 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
752 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
753 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
754 | exp srcloc '|' quals { HsDo ListComp (reverse
755 (ReturnStmt $1 : $4)) $2 }
757 lexps :: { [RdrNameHsExpr] }
758 : lexps ',' exp { $3 : $1 }
759 | exp ',' exp { [$3,$1] }
761 -----------------------------------------------------------------------------
762 -- List Comprehensions
764 quals :: { [RdrNameStmt] }
765 : quals ',' qual { $3 : $1 }
768 qual :: { RdrNameStmt }
769 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
770 returnP (BindStmt p $4 $1) }
771 | srcloc exp { GuardStmt $2 $1 }
772 | srcloc 'let' declbinds { LetStmt $3 }
774 -----------------------------------------------------------------------------
777 altslist :: { [RdrNameMatch] }
778 : '{' alts '}' { reverse $2 }
779 | layout_on alts close { reverse $2 }
781 alts :: { [RdrNameMatch] }
785 alts1 :: { [RdrNameMatch] }
786 : alts1 ';' alt { $3 : $1 }
790 alt :: { RdrNameMatch }
791 : infixexp opt_sig ralt wherebinds
792 {% checkPattern $1 `thenP` \p ->
793 returnP (Match [] [p] $2
794 (GRHSs $3 $4 Nothing)) }
796 opt_sig :: { Maybe RdrNameHsType }
797 : {- empty -} { Nothing }
798 | '::' type { Just $2 }
800 opt_asig :: { Maybe RdrNameHsType }
801 : {- empty -} { Nothing }
802 | '::' atype { Just $2 }
804 ralt :: { [RdrNameGRHS] }
805 : '->' srcloc exp { [GRHS [ExprStmt $3 $2] $2] }
806 | gdpats { (reverse $1) }
808 gdpats :: { [RdrNameGRHS] }
809 : gdpats gdpat { $2 : $1 }
812 gdpat :: { RdrNameGRHS }
813 : srcloc '|' quals '->' exp { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
815 -----------------------------------------------------------------------------
816 -- Statement sequences
818 stmtlist :: { [RdrNameStmt] }
819 : '{' stmts '}' { reverse $2 }
820 | layout_on_for_do stmts close { reverse $2 }
822 -- Stmt list should really end in an expression, but it's not
823 -- convenient to enforce this here, so we throw out erroneous
824 -- statement sequences in the renamer instead.
826 stmts :: { [RdrNameStmt] }
830 stmts1 :: { [RdrNameStmt] }
831 : stmts1 ';' stmt { $3 : $1 }
835 stmt :: { RdrNameStmt }
836 : srcloc infixexp '<-' exp {% checkPattern $2 `thenP` \p ->
837 returnP (BindStmt p $4 $1) }
838 | srcloc exp { ExprStmt $2 $1 }
839 | srcloc 'let' declbinds { LetStmt $3 }
841 -----------------------------------------------------------------------------
842 -- Record Field Update/Construction
844 fbinds :: { RdrNameHsRecordBinds }
845 : fbinds ',' fbind { $3 : $1 }
850 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
851 : qvar '=' exp { ($1,$3,False) }
853 -----------------------------------------------------------------------------
854 -- Implicit Parameter Bindings
856 dbinding :: { [(RdrName, RdrNameHsExpr)] }
857 : '{' dbinds '}' { $2 }
858 | layout_on dbinds close { $2 }
860 dbinds :: { [(RdrName, RdrNameHsExpr)] }
861 : dbinds ';' dbind { $3 : $1 }
866 dbind :: { (RdrName, RdrNameHsExpr) }
867 dbind : IPVARID '=' exp { (mkSrcUnqual ipName $1, $3) }
869 -----------------------------------------------------------------------------
870 -- Variables, Constructors and Operators.
873 : '(' ')' { unitCon_RDR }
874 | '[' ']' { nilCon_RDR }
875 | '(' commas ')' { tupleCon_RDR $2 }
880 | '(' varsym ')' { $2 }
884 | '(' qvarsym ')' { $2 }
888 | '(' consym ')' { $2 }
892 | '(' qconsym ')' { $2 }
896 | '`' varid '`' { $2 }
898 qvarop :: { RdrName }
900 | '`' qvarid '`' { $2 }
902 qvaropm :: { RdrName }
904 | '`' qvarid '`' { $2 }
908 | '`' conid '`' { $2 }
910 qconop :: { RdrName }
912 | '`' qconid '`' { $2 }
914 -----------------------------------------------------------------------------
917 op :: { RdrName } -- used in infix decls
921 qop :: { RdrNameHsExpr } -- used in sections
922 : qvarop { HsVar $1 }
923 | qconop { HsVar $1 }
925 qopm :: { RdrNameHsExpr } -- used in sections
926 : qvaropm { HsVar $1 }
927 | qconop { HsVar $1 }
929 -----------------------------------------------------------------------------
932 qvarid :: { RdrName }
934 | QVARID { case $1 of { (mod,n) ->
935 mkSrcQual varName mod n } }
938 : VARID { mkSrcUnqual varName $1 }
939 | 'as' { as_var_RDR }
940 | 'qualified' { qualified_var_RDR }
941 | 'hiding' { hiding_var_RDR }
942 | 'forall' { forall_var_RDR }
943 | 'export' { export_var_RDR }
944 | 'label' { label_var_RDR }
945 | 'dynamic' { dynamic_var_RDR }
946 | 'unsafe' { unsafe_var_RDR }
947 | 'stdcall' { stdcall_var_RDR }
948 | 'ccall' { ccall_var_RDR }
950 varid_no_unsafe :: { RdrName }
951 : VARID { mkSrcUnqual varName $1 }
952 | 'as' { as_var_RDR }
953 | 'qualified' { qualified_var_RDR }
954 | 'hiding' { hiding_var_RDR }
955 | 'forall' { forall_var_RDR }
956 | 'export' { export_var_RDR }
957 | 'label' { label_var_RDR }
958 | 'dynamic' { dynamic_var_RDR }
959 | 'stdcall' { stdcall_var_RDR }
960 | 'ccall' { ccall_var_RDR }
962 -----------------------------------------------------------------------------
965 qconid :: { RdrName }
967 | QCONID { case $1 of { (mod,n) ->
968 mkSrcQual dataName mod n } }
971 : CONID { mkSrcUnqual dataName $1 }
973 -----------------------------------------------------------------------------
976 qconsym :: { RdrName }
978 | QCONSYM { case $1 of { (mod,n) ->
979 mkSrcQual dataName mod n } }
981 consym :: { RdrName }
982 : CONSYM { mkSrcUnqual dataName $1 }
984 -----------------------------------------------------------------------------
987 qvarsym :: { RdrName }
991 qvarsymm :: { RdrName }
995 varsym :: { RdrName }
996 : VARSYM { mkSrcUnqual varName $1 }
1001 varsymm :: { RdrName } -- varsym not including '-'
1002 : VARSYM { mkSrcUnqual varName $1 }
1006 qvarsym1 :: { RdrName }
1007 : QVARSYM { case $1 of { (mod,n) ->
1008 mkSrcQual varName mod n } }
1010 literal :: { HsLit }
1011 : INTEGER { HsInt $1 }
1012 | CHAR { HsChar $1 }
1013 | RATIONAL { HsFrac $1 }
1014 | STRING { HsString $1 }
1016 | PRIMINTEGER { HsIntPrim $1 }
1017 | PRIMCHAR { HsCharPrim $1 }
1018 | PRIMSTRING { HsStringPrim $1 }
1019 | PRIMFLOAT { HsFloatPrim $1 }
1020 | PRIMDOUBLE { HsDoublePrim $1 }
1021 | CLITLIT { HsLitLit $1 }
1023 srcloc :: { SrcLoc } : {% getSrcLocP }
1025 -----------------------------------------------------------------------------
1029 : vccurly { () } -- context popped in lexer.
1030 | error {% popContext }
1032 layout_on :: { () } : {% layoutOn True{-strict-} }
1033 layout_on_for_do :: { () } : {% layoutOn False }
1035 -----------------------------------------------------------------------------
1036 -- Miscellaneous (mostly renamings)
1038 modid :: { ModuleName }
1039 : CONID { mkSrcModuleFS $1 }
1041 tycon :: { RdrName }
1042 : CONID { mkSrcUnqual tcClsName $1 }
1044 qtycon :: { RdrName }
1046 | QCONID { case $1 of { (mod,n) ->
1047 mkSrcQual tcClsName mod n } }
1049 qtycls :: { RdrName }
1052 tyvar :: { RdrName }
1053 : VARID { mkSrcUnqual tvName $1 }
1054 | 'as' { as_tyvar_RDR }
1055 | 'qualified' { qualified_tyvar_RDR }
1056 | 'hiding' { hiding_tyvar_RDR }
1057 | 'export' { export_tyvar_RDR }
1058 | 'label' { label_tyvar_RDR }
1059 | 'dynamic' { dynamic_tyvar_RDR }
1060 | 'unsafe' { unsafe_tyvar_RDR }
1061 | 'stdcall' { stdcall_tyvar_RDR }
1062 | 'ccall' { ccall_tyvar_RDR }
1063 -- NOTE: no 'forall'
1065 -----------------------------------------------------------------------------
1069 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)