2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.85 2002/02/11 09:27:22 simonpj Exp $
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
12 module Parser ( parseModule, parseStmt, parseIdentifier ) where
15 import HsTypes ( mkHsTupCon )
21 import PrelNames ( mAIN_Name, unitTyCon_RDR, funTyCon_RDR,
22 listTyCon_RDR, parrTyCon_RDR, tupleTyCon_RDR,
23 unitCon_RDR, nilCon_RDR, tupleCon_RDR )
24 import ForeignCall ( Safety(..), CExportSpec(..), CCallSpec(..),
25 CCallConv(..), CCallTarget(..), defaultCCallConv,
27 import OccName ( UserFS, varName, tcName, dataName, tcClsName, tvName )
28 import SrcLoc ( SrcLoc )
30 import CmdLineOpts ( opt_SccProfilingOn )
31 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
32 NewOrData(..), StrictnessMark(..), Activation(..) )
36 import CStrings ( CLabelString )
38 import Maybes ( orElse )
41 #include "HsVersions.h"
45 -----------------------------------------------------------------------------
46 Conflicts: 21 shift/reduce, -=chak[4Feb2]
48 8 for abiguity in 'if x then y else z + 1'
49 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
50 1 for ambiguity in 'if x then y else z :: T'
51 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
52 3 for ambiguity in 'case x of y :: a -> b'
53 (don't know whether to reduce 'a' as a btype or shift the '->'.
54 conclusion: bogus expression anyway, doesn't matter)
56 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
57 since 'forall' is a valid variable name, we don't know whether
58 to treat a forall on the input as the beginning of a quantifier
59 or the beginning of the rule itself. Resolving to shift means
60 it's always treated as a quantifier, hence the above is disallowed.
61 This saves explicitly defining a grammar for the rule lhs that
62 doesn't include 'forall'.
64 1 for ambiguity in 'x @ Rec{..}'.
65 Only sensible parse is 'x @ (Rec{..})', which is what resolving
68 6 for conflicts between `fdecl' and `fdeclDEPRECATED', which are resolved
69 correctly, and moreover, should go away when `fdeclDEPRECATED' is removed.
71 -----------------------------------------------------------------------------
75 '_' { ITunderscore } -- Haskell keywords
80 'default' { ITdefault }
81 'deriving' { ITderiving }
91 'instance' { ITinstance }
94 'newtype' { ITnewtype }
96 'qualified' { ITqualified }
100 '_scc_' { ITscc } -- ToDo: remove
102 'forall' { ITforall } -- GHC extension keywords
103 'foreign' { ITforeign }
104 'export' { ITexport }
106 'dynamic' { ITdynamic }
108 'unsafe' { ITunsafe }
110 'stdcall' { ITstdcallconv }
111 'ccall' { ITccallconv }
112 'dotnet' { ITdotnet }
113 '_ccall_' { ITccall (False, False, PlayRisky) }
114 '_ccall_GC_' { ITccall (False, False, PlaySafe) }
115 '_casm_' { ITccall (False, True, PlayRisky) }
116 '_casm_GC_' { ITccall (False, True, PlaySafe) }
118 '{-# SPECIALISE' { ITspecialise_prag }
119 '{-# SOURCE' { ITsource_prag }
120 '{-# INLINE' { ITinline_prag }
121 '{-# NOINLINE' { ITnoinline_prag }
122 '{-# RULES' { ITrules_prag }
123 '{-# SCC' { ITscc_prag }
124 '{-# DEPRECATED' { ITdeprecated_prag }
125 '#-}' { ITclose_prag }
128 '__interface' { ITinterface } -- interface keywords
129 '__export' { IT__export }
130 '__instimport' { ITinstimport }
131 '__forall' { IT__forall }
132 '__letrec' { ITletrec }
133 '__coerce' { ITcoerce }
134 '__depends' { ITdepends }
135 '__inline' { ITinline }
136 '__DEFAULT' { ITdefaultbranch }
138 '__integer' { ITinteger_lit }
139 '__float' { ITfloat_lit }
140 '__rational' { ITrational_lit }
141 '__addr' { ITaddr_lit }
142 '__label' { ITlabel_lit }
143 '__litlit' { ITlit_lit }
144 '__string' { ITstring_lit }
145 '__ccall' { ITccall $$ }
147 '__sccC' { ITsccAllCafs }
150 '__P' { ITspecialise }
153 '__S' { ITstrict $$ }
154 '__M' { ITcprinfo $$ }
157 '..' { ITdotdot } -- reserved symbols
171 '{' { ITocurly } -- special symbols
175 vccurly { ITvccurly } -- virtual close curly (from layout)
188 VARID { ITvarid $$ } -- identifiers
190 VARSYM { ITvarsym $$ }
191 CONSYM { ITconsym $$ }
192 QVARID { ITqvarid $$ }
193 QCONID { ITqconid $$ }
194 QVARSYM { ITqvarsym $$ }
195 QCONSYM { ITqconsym $$ }
197 IPDUPVARID { ITdupipvarid $$ } -- GHC extension
198 IPSPLITVARID { ITsplitipvarid $$ } -- GHC extension
201 STRING { ITstring $$ }
202 INTEGER { ITinteger $$ }
203 RATIONAL { ITrational $$ }
205 PRIMCHAR { ITprimchar $$ }
206 PRIMSTRING { ITprimstring $$ }
207 PRIMINTEGER { ITprimint $$ }
208 PRIMFLOAT { ITprimfloat $$ }
209 PRIMDOUBLE { ITprimdouble $$ }
210 CLITLIT { ITlitlit $$ }
212 %monad { P } { thenP } { returnP }
213 %lexer { lexer } { ITeof }
214 %name parseModule module
215 %name parseStmt maybe_stmt
216 %name parseIdentifier identifier
220 -----------------------------------------------------------------------------
223 -- The place for module deprecation is really too restrictive, but if it
224 -- was allowed at its natural place just before 'module', we get an ugly
225 -- s/r conflict with the second alternative. Another solution would be the
226 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
227 -- either, and DEPRECATED is only expected to be used by people who really
228 -- know what they are doing. :-)
230 module :: { RdrNameHsModule }
231 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
232 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
234 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
236 maybemoddeprec :: { Maybe DeprecTxt }
237 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
238 | {- empty -} { Nothing }
240 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
242 | layout_on top close { $2 }
244 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
245 : importdecls { (reverse $1,[]) }
246 | importdecls ';' cvtopdecls { (reverse $1,$3) }
247 | cvtopdecls { ([],$1) }
249 cvtopdecls :: { [RdrNameHsDecl] }
250 : topdecls { cvTopDecls (groupBindings $1)}
252 -----------------------------------------------------------------------------
255 maybeexports :: { Maybe [RdrNameIE] }
256 : '(' exportlist ')' { Just $2 }
257 | {- empty -} { Nothing }
259 exportlist :: { [RdrNameIE] }
260 : exportlist ',' export { $3 : $1 }
261 | exportlist ',' { $1 }
265 -- GHC extension: we allow things like [] and (,,,) to be exported
266 export :: { RdrNameIE }
268 | gtycon { IEThingAbs $1 }
269 | gtycon '(' '..' ')' { IEThingAll $1 }
270 | gtycon '(' ')' { IEThingWith $1 [] }
271 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
272 | 'module' modid { IEModuleContents $2 }
274 qcnames :: { [RdrName] }
275 : qcnames ',' qcname { $3 : $1 }
278 qcname :: { RdrName }
282 -----------------------------------------------------------------------------
283 -- Import Declarations
285 -- import decls can be *empty*, or even just a string of semicolons
286 -- whereas topdecls must contain at least one topdecl.
288 importdecls :: { [RdrNameImportDecl] }
289 : importdecls ';' importdecl { $3 : $1 }
290 | importdecls ';' { $1 }
291 | importdecl { [ $1 ] }
294 importdecl :: { RdrNameImportDecl }
295 : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec
296 { ImportDecl $5 $3 $4 $6 $7 $2 }
298 maybe_src :: { WhereFrom }
299 : '{-# SOURCE' '#-}' { ImportByUserSource }
300 | {- empty -} { ImportByUser }
302 optqualified :: { Bool }
303 : 'qualified' { True }
304 | {- empty -} { False }
306 maybeas :: { Maybe ModuleName }
307 : 'as' modid { Just $2 }
308 | {- empty -} { Nothing }
310 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
311 : impspec { Just $1 }
312 | {- empty -} { Nothing }
314 impspec :: { (Bool, [RdrNameIE]) }
315 : '(' exportlist ')' { (False, reverse $2) }
316 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
318 -----------------------------------------------------------------------------
319 -- Fixity Declarations
323 | INTEGER {% checkPrec $1 `thenP_`
324 returnP (fromInteger $1) }
326 infix :: { FixityDirection }
328 | 'infixl' { InfixL }
329 | 'infixr' { InfixR }
332 : ops ',' op { $3 : $1 }
335 -----------------------------------------------------------------------------
336 -- Top-Level Declarations
338 topdecls :: { [RdrBinding] }
339 : topdecls ';' topdecl { ($3 : $1) }
340 | topdecls ';' { $1 }
343 topdecl :: { RdrBinding }
344 : srcloc 'type' simpletype '=' ctype
345 -- Note ctype, not sigtype.
346 -- We allow an explicit for-all but we don't insert one
347 -- in type Foo a = (b,b)
348 -- Instead we just say b is out of scope
349 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
351 | srcloc 'data' tycl_hdr constrs deriving
352 {% returnP (RdrHsDecl (TyClD
353 (mkTyData DataType $3 (reverse $4) (length $4) $5 $1))) }
355 | srcloc 'newtype' tycl_hdr '=' newconstr deriving
356 {% returnP (RdrHsDecl (TyClD
357 (mkTyData NewType $3 [$5] 1 $6 $1))) }
359 | srcloc 'class' tycl_hdr fds where
361 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
363 returnP (RdrHsDecl (TyClD
364 (mkClassDecl $3 $4 sigs (Just binds) $1))) }
366 | srcloc 'instance' inst_type where
368 = cvMonoBindsAndSigs cvInstDeclSig
370 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
372 | srcloc 'default' '(' types0 ')' { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
373 | 'foreign' fdecl { RdrHsDecl $2 }
374 | '{-# DEPRECATED' deprecations '#-}' { $2 }
375 | '{-# RULES' rules '#-}' { $2 }
378 -- tycl_hdr parses the header of a type or class decl,
379 -- which takes the form
382 -- (Eq a, Ord b) => T a b
383 -- Rather a lot of inlining here, else we get reduce/reduce errors
384 tycl_hdr :: { (RdrNameContext, RdrName, [RdrNameHsTyVar]) }
385 : '(' types ')' '=>' tycon tyvars {% mapP checkPred $2 `thenP` \ cxt ->
386 returnP (cxt, $5, $6) }
387 | tycon tyvars '=>' tycon tyvars {% checkTyVars $2 `thenP` \ args ->
388 returnP ([HsClassP $1 args], $4, $5) }
389 | qtycon tyvars '=>' tycon tyvars {% checkTyVars $2 `thenP` \ args ->
390 returnP ([HsClassP $1 args], $4, $5) }
391 | tycon tyvars { ([], $1, $2) }
393 decls :: { [RdrBinding] }
394 : decls ';' decl { $3 : $1 }
399 decl :: { RdrBinding }
402 | '{-# INLINE' srcloc activation qvar '#-}' { RdrSig (InlineSig True $4 $3 $2) }
403 | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
404 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
405 { foldr1 RdrAndBindings
406 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
407 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
408 { RdrSig (SpecInstSig $4 $2) }
410 wherebinds :: { RdrNameHsBinds }
411 : where { cvBinds cvValSig (groupBindings $1) }
413 where :: { [RdrBinding] }
414 : 'where' decllist { $2 }
417 declbinds :: { RdrNameHsBinds }
418 : decllist { cvBinds cvValSig (groupBindings $1) }
420 decllist :: { [RdrBinding] }
421 : '{' decls '}' { $2 }
422 | layout_on decls close { $2 }
424 fixdecl :: { RdrBinding }
425 : srcloc infix prec ops { foldr1 RdrAndBindings
426 [ RdrSig (FixSig (FixitySig n
430 -----------------------------------------------------------------------------
431 -- Transformation Rules
433 rules :: { RdrBinding }
434 : rules ';' rule { $1 `RdrAndBindings` $3 }
437 | {- empty -} { RdrNullBind }
439 rule :: { RdrBinding }
440 : STRING activation rule_forall infixexp '=' srcloc exp
441 { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
443 activation :: { Activation } -- Omitted means AlwaysActive
444 : {- empty -} { AlwaysActive }
445 | explicit_activation { $1 }
447 inverse_activation :: { Activation } -- Omitted means NeverActive
448 : {- empty -} { NeverActive }
449 | explicit_activation { $1 }
451 explicit_activation :: { Activation } -- In brackets
452 : '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
453 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger $3) }
455 rule_forall :: { [RdrNameRuleBndr] }
456 : 'forall' rule_var_list '.' { $2 }
459 rule_var_list :: { [RdrNameRuleBndr] }
461 | rule_var rule_var_list { $1 : $2 }
463 rule_var :: { RdrNameRuleBndr }
464 : varid { RuleBndr $1 }
465 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
467 -----------------------------------------------------------------------------
470 deprecations :: { RdrBinding }
471 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
472 | deprecations ';' { $1 }
474 | {- empty -} { RdrNullBind }
476 -- SUP: TEMPORARY HACK, not checking for `module Foo'
477 deprecation :: { RdrBinding }
478 : srcloc depreclist STRING
479 { foldr RdrAndBindings RdrNullBind
480 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
483 -----------------------------------------------------------------------------
484 -- Foreign import and export declarations
486 -- for the time being, the following accepts foreign declarations conforming
487 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
489 -- * a flag indicates whether pre-standard declarations have been used and
490 -- triggers a deprecation warning further down the road
492 -- NB: The first two rules could be combined into one by replacing `safety1'
493 -- with `safety'. However, the combined rule conflicts with the
496 fdecl :: { RdrNameHsDecl }
497 fdecl : srcloc 'import' callconv safety1 fspec {% mkImport $3 $4 $5 $1 }
498 | srcloc 'import' callconv fspec {% mkImport $3 PlaySafe $4 $1 }
499 | srcloc 'export' callconv fspec {% mkExport $3 $4 $1 }
500 -- the following syntax is DEPRECATED
501 | srcloc fdecl1DEPRECATED { ForD ($2 True $1) }
502 | srcloc fdecl2DEPRECATED { $2 $1 }
504 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
506 ----------- DEPRECATED label decls ------------
507 : 'label' ext_name varid '::' sigtype
508 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
509 (CLabel ($2 `orElse` mkExtName $3))) }
511 ----------- DEPRECATED ccall/stdcall decls ------------
513 -- NB: This business with the case expression below may seem overly
514 -- complicated, but it is necessary to avoid some conflicts.
516 -- DEPRECATED variant #1: lack of a calling convention specification
518 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
520 target = StaticTarget ($2 `orElse` mkExtName $4)
522 ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
523 (CFunction target)) }
525 -- DEPRECATED variant #2: external name consists of two separate strings
526 -- (module name and function name) (import)
527 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
529 DNCall -> parseError "Illegal format of .NET foreign import"
530 CCall cconv -> returnP $
532 imp = CFunction (StaticTarget $4)
534 ForeignImport $6 $8 (CImport cconv $5 _NIL_ _NIL_ imp) }
536 -- DEPRECATED variant #3: `unsafe' after entity
537 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
539 DNCall -> parseError "Illegal format of .NET foreign import"
540 CCall cconv -> returnP $
542 imp = CFunction (StaticTarget $3)
544 ForeignImport $5 $7 (CImport cconv PlayRisky _NIL_ _NIL_ imp) }
546 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
547 -- an explicit calling convention (import)
548 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
549 { ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
550 (CFunction DynamicTarget)) }
552 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
553 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
555 DNCall -> parseError "Illegal format of .NET foreign import"
556 CCall cconv -> returnP $
557 ForeignImport $5 $7 (CImport cconv $4 _NIL_ _NIL_
558 (CFunction DynamicTarget)) }
560 -- DEPRECATED variant #6: lack of a calling convention specification
562 | 'export' {-no callconv-} ext_name varid '::' sigtype
563 { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3)
566 -- DEPRECATED variant #7: external name consists of two separate strings
567 -- (module name and function name) (export)
568 | 'export' callconv STRING STRING varid '::' sigtype
570 DNCall -> parseError "Illegal format of .NET foreign import"
571 CCall cconv -> returnP $
573 (CExport (CExportStatic $4 cconv)) }
575 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
576 -- an explicit calling convention (export)
577 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
578 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
581 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
582 | 'export' callconv 'dynamic' varid '::' sigtype
584 DNCall -> parseError "Illegal format of .NET foreign import"
585 CCall cconv -> returnP $
586 ForeignImport $4 $6 (CImport cconv PlaySafe _NIL_ _NIL_ CWrapper) }
588 ----------- DEPRECATED .NET decls ------------
589 -- NB: removed the .NET call declaration, as it is entirely subsumed
590 -- by the new standard FFI declarations
592 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
594 : 'import' 'dotnet' 'type' ext_name tycon
595 { \loc -> TyClD (ForeignType $5 $4 DNType loc) }
596 -- left this one unchanged for the moment as type imports are not
597 -- covered currently by the FFI standard -=chak
600 callconv :: { CallConv }
601 : 'stdcall' { CCall StdCallConv }
602 | 'ccall' { CCall CCallConv }
603 | 'dotnet' { DNCall }
606 : 'unsafe' { PlayRisky }
607 | 'safe' { PlaySafe }
608 | {- empty -} { PlaySafe }
610 safety1 :: { Safety }
611 : 'unsafe' { PlayRisky }
612 | 'safe' { PlaySafe }
613 -- only needed to avoid conflicts with the DEPRECATED rules
615 fspec :: { (FAST_STRING, RdrName, RdrNameHsType) }
616 : STRING varid '::' sigtype { ($1 , $2, $4) }
617 | varid '::' sigtype { (SLIT(""), $1, $3) }
618 -- if the entity string is missing, it defaults to the empty string;
619 -- the meaning of an empty entity string depends on the calling
623 ext_name :: { Maybe CLabelString }
625 | STRING STRING { Just $2 } -- Ignore "module name" for now
626 | {- empty -} { Nothing }
629 -----------------------------------------------------------------------------
632 opt_sig :: { Maybe RdrNameHsType }
633 : {- empty -} { Nothing }
634 | '::' sigtype { Just $2 }
636 opt_asig :: { Maybe RdrNameHsType }
637 : {- empty -} { Nothing }
638 | '::' atype { Just $2 }
640 sigtypes :: { [RdrNameHsType] }
642 | sigtypes ',' sigtype { $3 : $1 }
644 sigtype :: { RdrNameHsType }
645 : ctype { (mkHsForAllTy Nothing [] $1) }
647 sig_vars :: { [RdrName] }
648 : sig_vars ',' var { $3 : $1 }
651 -----------------------------------------------------------------------------
654 -- A ctype is a for-all type
655 ctype :: { RdrNameHsType }
656 : 'forall' tyvars '.' ctype { mkHsForAllTy (Just $2) [] $4 }
657 | context '=>' type { mkHsForAllTy Nothing $1 $3 }
658 -- A type of form (context => type) is an *implicit* HsForAllTy
661 -- We parse a context as a btype so that we don't get reduce/reduce
662 -- errors in ctype. The basic problem is that
664 -- looks so much like a tuple type. We can't tell until we find the =>
665 context :: { RdrNameContext }
666 : btype {% checkContext $1 }
668 type :: { RdrNameHsType }
669 : gentype '->' type { HsFunTy $1 $3 }
670 | ipvar '::' type { mkHsIParamTy $1 $3 }
673 gentype :: { RdrNameHsType }
676 | atype tyconop atype { HsOpTy $1 $2 $3 }
678 btype :: { RdrNameHsType }
679 : btype atype { (HsAppTy $1 $2) }
682 atype :: { RdrNameHsType }
683 : gtycon { HsTyVar $1 }
684 | tyvar { HsTyVar $1 }
685 | '(' type ',' types ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2 : reverse $4) }
686 | '(#' types '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) (reverse $2) }
687 | '[' type ']' { HsListTy $2 }
688 | '[:' type ':]' { HsPArrTy $2 }
689 | '(' ctype ')' { $2 }
691 | INTEGER { HsNumTy $1 }
693 -- An inst_type is what occurs in the head of an instance decl
694 -- e.g. (Foo a, Gaz b) => Wibble a b
695 -- It's kept as a single type, with a MonoDictTy at the right
696 -- hand corner, for convenience.
697 inst_type :: { RdrNameHsType }
698 : ctype {% checkInstType $1 }
700 types0 :: { [RdrNameHsType] }
701 : types { reverse $1 }
704 types :: { [RdrNameHsType] }
706 | types ',' type { $3 : $1 }
708 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
709 : tycon tyvars { ($1, reverse $2) }
711 tyvars :: { [RdrNameHsTyVar] }
712 : tyvar tyvars { UserTyVar $1 : $2 }
715 fds :: { [([RdrName], [RdrName])] }
717 | '|' fds1 { reverse $2 }
719 fds1 :: { [([RdrName], [RdrName])] }
720 : fds1 ',' fd { $3 : $1 }
723 fd :: { ([RdrName], [RdrName]) }
724 : varids0 '->' varids0 { (reverse $1, reverse $3) }
726 varids0 :: { [RdrName] }
728 | varids0 tyvar { $2 : $1 }
730 -----------------------------------------------------------------------------
731 -- Datatype declarations
733 newconstr :: { RdrNameConDecl }
734 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
735 | srcloc conid '{' var '::' ctype '}'
736 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
738 constrs :: { [RdrNameConDecl] }
739 : {- empty; a GHC extension -} { [] }
740 | '=' constrs1 { $2 }
742 constrs1 :: { [RdrNameConDecl] }
743 : constrs1 '|' constr { $3 : $1 }
746 constr :: { RdrNameConDecl }
747 : srcloc forall context '=>' constr_stuff
748 { mkConDecl (fst $5) $2 $3 (snd $5) $1 }
749 | srcloc forall constr_stuff
750 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
752 forall :: { [RdrNameHsTyVar] }
753 : 'forall' tyvars '.' { $2 }
756 constr_stuff :: { (RdrName, RdrNameConDetails) }
757 : btype {% mkVanillaCon $1 [] }
758 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
759 | gtycon '{' '}' {% mkRecCon $1 [] }
760 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
761 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
763 satypes :: { [RdrNameBangType] }
764 : atype satypes { unbangedType $1 : $2 }
765 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
768 sbtype :: { RdrNameBangType }
769 : btype { unbangedType $1 }
770 | '!' atype { BangType MarkedUserStrict $2 }
772 fielddecls :: { [([RdrName],RdrNameBangType)] }
773 : fielddecl ',' fielddecls { $1 : $3 }
776 fielddecl :: { ([RdrName],RdrNameBangType) }
777 : sig_vars '::' stype { (reverse $1, $3) }
779 stype :: { RdrNameBangType }
780 : ctype { unbangedType $1 }
781 | '!' atype { BangType MarkedUserStrict $2 }
783 deriving :: { Maybe RdrNameContext }
784 : {- empty -} { Nothing }
785 | 'deriving' context { Just $2 }
786 -- Glasgow extension: allow partial
787 -- applications in derivings
789 -----------------------------------------------------------------------------
792 {- There's an awkward overlap with a type signature. Consider
793 f :: Int -> Int = ...rhs...
794 Then we can't tell whether it's a type signature or a value
795 definition with a result signature until we see the '='.
796 So we have to inline enough to postpone reductions until we know.
800 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
801 instead of qvar, we get another shift/reduce-conflict. Consider the
804 { (^^) :: Int->Int ; } Type signature; only var allowed
806 { (^^) :: Int->Int = ... ; } Value defn with result signature;
807 qvar allowed (because of instance decls)
809 We can't tell whether to reduce var to qvar until after we've read the signatures.
812 valdef :: { RdrBinding }
813 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
814 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
815 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
816 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
820 rhs :: { RdrNameGRHSs }
821 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
822 | gdrhs wherebinds { GRHSs (reverse $1) $2 placeHolderType }
824 gdrhs :: { [RdrNameGRHS] }
825 : gdrhs gdrh { $2 : $1 }
828 gdrh :: { RdrNameGRHS }
829 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
831 -----------------------------------------------------------------------------
834 exp :: { RdrNameHsExpr }
835 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
836 | infixexp 'with' dbinding { HsWith $1 $3 }
839 infixexp :: { RdrNameHsExpr }
841 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
842 (panic "fixity") $3 )}
844 exp10 :: { RdrNameHsExpr }
845 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
846 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
847 returnP (HsLam (Match ps $5
848 (GRHSs (unguardedRHS $8 $7)
849 EmptyBinds placeHolderType))) }
850 | 'let' declbinds 'in' exp { HsLet $2 $4 }
851 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
852 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
853 | '-' fexp { mkHsNegApp $2 }
854 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
855 returnP (HsDo DoExpr stmts $1) }
857 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False placeHolderType }
858 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 PlaySafe False placeHolderType }
859 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True placeHolderType }
860 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 PlaySafe True placeHolderType }
862 | scc_annot exp { if opt_SccProfilingOn
868 scc_annot :: { FAST_STRING }
869 : '_scc_' STRING { $2 }
870 | '{-# SCC' STRING '#-}' { $2 }
872 ccallid :: { FAST_STRING }
876 fexp :: { RdrNameHsExpr }
877 : fexp aexp { (HsApp $1 $2) }
880 aexps0 :: { [RdrNameHsExpr] }
881 : aexps { (reverse $1) }
883 aexps :: { [RdrNameHsExpr] }
884 : aexps aexp { $2 : $1 }
887 aexp :: { RdrNameHsExpr }
888 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
889 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
893 var_or_con :: { RdrNameHsExpr }
897 aexp1 :: { RdrNameHsExpr }
898 : ipvar { HsIPVar $1 }
900 | literal { HsLit $1 }
901 | INTEGER { HsOverLit (mkHsIntegral $1) }
902 | RATIONAL { HsOverLit (mkHsFractional $1) }
903 | '(' exp ')' { HsPar $2 }
904 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
905 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
906 | '[' list ']' { $2 }
907 | '[:' parr ':]' { $2 }
908 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
909 | '(' qopm infixexp ')' { (SectionR $2 $3) }
910 | qvar '@' aexp { EAsPat $1 $3 }
912 | '~' aexp1 { ELazyPat $2 }
914 texps :: { [RdrNameHsExpr] }
915 : texps ',' exp { $3 : $1 }
919 -----------------------------------------------------------------------------
922 -- The rules below are little bit contorted to keep lexps left-recursive while
923 -- avoiding another shift/reduce-conflict.
925 list :: { RdrNameHsExpr }
926 : exp { ExplicitList placeHolderType [$1] }
927 | lexps { ExplicitList placeHolderType (reverse $1) }
928 | exp '..' { ArithSeqIn (From $1) }
929 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
930 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
931 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
932 | exp srcloc pquals {% let { body [qs] = qs;
933 body qss = [ParStmt (map reverse qss)] }
935 returnP ( HsDo ListComp
936 (reverse (ResultStmt $1 $2 : body $3))
941 lexps :: { [RdrNameHsExpr] }
942 : lexps ',' exp { $3 : $1 }
943 | exp ',' exp { [$3,$1] }
945 -----------------------------------------------------------------------------
946 -- List Comprehensions
948 pquals :: { [[RdrNameStmt]] }
949 : pquals '|' quals { $3 : $1 }
952 quals :: { [RdrNameStmt] }
953 : quals ',' stmt { $3 : $1 }
956 -----------------------------------------------------------------------------
957 -- Parallel array expressions
959 -- The rules below are little bit contorted; see the list case for details.
960 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
961 -- Moreover, we allow explicit arrays with no element (represented by the nil
962 -- constructor in the list case).
964 parr :: { RdrNameHsExpr }
965 : { ExplicitPArr placeHolderType [] }
966 | exp { ExplicitPArr placeHolderType [$1] }
967 | lexps { ExplicitPArr placeHolderType
969 | exp '..' exp { PArrSeqIn (FromTo $1 $3) }
970 | exp ',' exp '..' exp { PArrSeqIn (FromThenTo $1 $3 $5) }
971 | exp srcloc pquals {% let {
978 (reverse (ResultStmt $1 $2
983 -- We are reusing `lexps' and `pquals' from the list case.
985 -----------------------------------------------------------------------------
988 altslist :: { [RdrNameMatch] }
989 : '{' alts '}' { reverse $2 }
990 | layout_on alts close { reverse $2 }
992 alts :: { [RdrNameMatch] }
996 alts1 :: { [RdrNameMatch] }
997 : alts1 ';' alt { $3 : $1 }
1001 alt :: { RdrNameMatch }
1002 : srcloc infixexp opt_sig ralt wherebinds
1003 {% (checkPattern $1 $2 `thenP` \p ->
1004 returnP (Match [p] $3
1005 (GRHSs $4 $5 placeHolderType)) )}
1007 ralt :: { [RdrNameGRHS] }
1008 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
1009 | gdpats { (reverse $1) }
1011 gdpats :: { [RdrNameGRHS] }
1012 : gdpats gdpat { $2 : $1 }
1015 gdpat :: { RdrNameGRHS }
1016 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
1018 -----------------------------------------------------------------------------
1019 -- Statement sequences
1021 stmtlist :: { [RdrNameStmt] }
1022 : '{' stmts '}' { $2 }
1023 | layout_on_for_do stmts close { $2 }
1025 -- do { ;; s ; s ; ; s ;; }
1026 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1027 -- here, because we need too much lookahead if we see do { e ; }
1028 -- So we use ExprStmts throughout, and switch the last one over
1029 -- in ParseUtils.checkDo instead
1030 stmts :: { [RdrNameStmt] }
1031 : stmt stmts_help { $1 : $2 }
1033 | {- empty -} { [] }
1035 stmts_help :: { [RdrNameStmt] }
1037 | {- empty -} { [] }
1039 -- For typing stmts at the GHCi prompt, where
1040 -- the input may consist of just comments.
1041 maybe_stmt :: { Maybe RdrNameStmt }
1043 | {- nothing -} { Nothing }
1045 stmt :: { RdrNameStmt }
1046 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
1047 returnP (BindStmt p $4 $1) }
1048 | srcloc exp { ExprStmt $2 placeHolderType $1 }
1049 | srcloc 'let' declbinds { LetStmt $3 }
1051 -----------------------------------------------------------------------------
1052 -- Record Field Update/Construction
1054 fbinds :: { RdrNameHsRecordBinds }
1055 : fbinds ',' fbind { $3 : $1 }
1058 | {- empty -} { [] }
1060 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
1061 : qvar '=' exp { ($1,$3,False) }
1063 -----------------------------------------------------------------------------
1064 -- Implicit Parameter Bindings
1066 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1067 : '{' dbinds '}' { $2 }
1068 | layout_on dbinds close { $2 }
1070 dbinds :: { [(IPName RdrName, RdrNameHsExpr)] }
1071 : dbinds ';' dbind { $3 : $1 }
1074 | {- empty -} { [] }
1076 dbind :: { (IPName RdrName, RdrNameHsExpr) }
1077 dbind : ipvar '=' exp { ($1, $3) }
1079 -----------------------------------------------------------------------------
1080 -- Variables, Constructors and Operators.
1082 identifier :: { RdrName }
1087 depreclist :: { [RdrName] }
1088 depreclist : deprec_var { [$1] }
1089 | deprec_var ',' depreclist { $1 : $3 }
1091 deprec_var :: { RdrName }
1092 deprec_var : var { $1 }
1095 gtycon :: { RdrName }
1098 | '(' tyconop ')' { $2 }
1099 | '(' qtyconop ')' { $2 }
1100 | '(' ')' { unitTyCon_RDR }
1101 | '(' '->' ')' { funTyCon_RDR }
1102 | '[' ']' { listTyCon_RDR }
1103 | '[:' ':]' { parrTyCon_RDR }
1104 | '(' commas ')' { tupleTyCon_RDR $2 }
1107 : '(' ')' { unitCon_RDR }
1108 | '[' ']' { nilCon_RDR }
1109 | '(' commas ')' { tupleCon_RDR $2 }
1111 -- the case of '[:' ':]' is part of the production `parr'
1115 | '(' varsym ')' { $2 }
1119 | '(' varsym ')' { $2 }
1120 | '(' qvarsym1 ')' { $2 }
1121 -- We've inlined qvarsym here so that the decision about
1122 -- whether it's a qvar or a var can be postponed until
1123 -- *after* we see the close paren.
1125 ipvar :: { IPName RdrName }
1126 : IPDUPVARID { Dupable (mkUnqual varName $1) }
1127 | IPSPLITVARID { Linear (mkUnqual varName $1) }
1131 | '(' qconsym ')' { $2 }
1133 varop :: { RdrName }
1135 | '`' varid '`' { $2 }
1137 qvarop :: { RdrName }
1139 | '`' qvarid '`' { $2 }
1141 qvaropm :: { RdrName }
1142 : qvarsym_no_minus { $1 }
1143 | '`' qvarid '`' { $2 }
1145 conop :: { RdrName }
1147 | '`' conid '`' { $2 }
1149 qconop :: { RdrName }
1151 | '`' qconid '`' { $2 }
1153 -----------------------------------------------------------------------------
1156 op :: { RdrName } -- used in infix decls
1160 qop :: { RdrName {-HsExpr-} } -- used in sections
1164 qopm :: { RdrNameHsExpr } -- used in sections
1165 : qvaropm { HsVar $1 }
1166 | qconop { HsVar $1 }
1168 -----------------------------------------------------------------------------
1171 qvarid :: { RdrName }
1173 | QVARID { mkQual varName $1 }
1175 varid :: { RdrName }
1176 : varid_no_unsafe { $1 }
1177 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1179 varid_no_unsafe :: { RdrName }
1180 : VARID { mkUnqual varName $1 }
1181 | special_id { mkUnqual varName $1 }
1182 | 'forall' { mkUnqual varName SLIT("forall") }
1184 tyvar :: { RdrName }
1185 : VARID { mkUnqual tvName $1 }
1186 | special_id { mkUnqual tvName $1 }
1187 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1189 -- These special_ids are treated as keywords in various places,
1190 -- but as ordinary ids elsewhere. A special_id collects all thsee
1191 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1192 special_id :: { UserFS }
1194 : 'as' { SLIT("as") }
1195 | 'qualified' { SLIT("qualified") }
1196 | 'hiding' { SLIT("hiding") }
1197 | 'export' { SLIT("export") }
1198 | 'label' { SLIT("label") }
1199 | 'dynamic' { SLIT("dynamic") }
1200 | 'stdcall' { SLIT("stdcall") }
1201 | 'ccall' { SLIT("ccall") }
1203 -----------------------------------------------------------------------------
1206 qconid :: { RdrName } -- Qualified or unqualifiedb
1208 | QCONID { mkQual dataName $1 }
1210 conid :: { RdrName }
1211 : CONID { mkUnqual dataName $1 }
1213 -----------------------------------------------------------------------------
1216 qconsym :: { RdrName } -- Qualified or unqualifiedb
1218 | QCONSYM { mkQual dataName $1 }
1220 consym :: { RdrName }
1221 : CONSYM { mkUnqual dataName $1 }
1223 -----------------------------------------------------------------------------
1226 qvarsym :: { RdrName }
1230 qvarsym_no_minus :: { RdrName }
1231 : varsym_no_minus { $1 }
1234 qvarsym1 :: { RdrName }
1235 qvarsym1 : QVARSYM { mkQual varName $1 }
1237 varsym :: { RdrName }
1238 : varsym_no_minus { $1 }
1239 | '-' { mkUnqual varName SLIT("-") }
1241 varsym_no_minus :: { RdrName } -- varsym not including '-'
1242 : VARSYM { mkUnqual varName $1 }
1243 | special_sym { mkUnqual varName $1 }
1246 -- See comments with special_id
1247 special_sym :: { UserFS }
1248 special_sym : '!' { SLIT("!") }
1251 -----------------------------------------------------------------------------
1254 literal :: { HsLit }
1255 : CHAR { HsChar $1 }
1256 | STRING { HsString $1 }
1257 | PRIMINTEGER { HsIntPrim $1 }
1258 | PRIMCHAR { HsCharPrim $1 }
1259 | PRIMSTRING { HsStringPrim $1 }
1260 | PRIMFLOAT { HsFloatPrim $1 }
1261 | PRIMDOUBLE { HsDoublePrim $1 }
1262 | CLITLIT { HsLitLit $1 placeHolderType }
1264 srcloc :: { SrcLoc } : {% getSrcLocP }
1266 -----------------------------------------------------------------------------
1270 : vccurly { () } -- context popped in lexer.
1271 | error {% popContext }
1273 layout_on :: { () } : {% layoutOn True{-strict-} }
1274 layout_on_for_do :: { () } : {% layoutOn False }
1276 -----------------------------------------------------------------------------
1277 -- Miscellaneous (mostly renamings)
1279 modid :: { ModuleName }
1280 : CONID { mkModuleNameFS $1 }
1281 | QCONID { mkModuleNameFS
1283 (unpackFS (fst $1) ++
1284 '.':unpackFS (snd $1)))
1287 tycon :: { RdrName }
1288 : CONID { mkUnqual tcClsName $1 }
1290 tyconop :: { RdrName }
1291 : CONSYM { mkUnqual tcClsName $1 }
1293 qtycon :: { RdrName } -- Just the qualified kind
1294 : QCONID { mkQual tcClsName $1 }
1296 qtyconop :: { RdrName } -- Just the qualified kind
1297 : QCONSYM { mkQual tcClsName $1 }
1300 : commas ',' { $1 + 1 }
1303 -----------------------------------------------------------------------------
1307 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)