2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.90 2002/02/15 22:13:33 sof 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(..),
25 CCallConv(..), CCallTarget(..), defaultCCallConv,
27 import OccName ( UserFS, varName, tcName, dataName, tcClsName, tvName )
28 import TyCon ( DataConDetails(..) )
29 import SrcLoc ( SrcLoc )
31 import CmdLineOpts ( opt_SccProfilingOn )
32 import Type ( Kind, mkArrowKind, liftedTypeKind )
33 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
34 NewOrData(..), StrictnessMark(..), Activation(..) )
38 import CStrings ( CLabelString )
40 import Maybes ( orElse )
43 #include "HsVersions.h"
47 -----------------------------------------------------------------------------
48 Conflicts: 21 shift/reduce, -=chak[4Feb2]
50 9 for abiguity in 'if x then y else z + 1'
51 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
52 8 because op might be: - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
53 1 for ambiguity in 'if x then y else z :: T'
54 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
55 1 for ambiguity in 'if x then y else z with ?x=3'
56 (shift parses as 'if x then y else (z with ?x=3)'
58 3 for ambiguity in 'case x of y :: a -> b'
59 (don't know whether to reduce 'a' as a btype or shift the '->'.
60 conclusion: bogus expression anyway, doesn't matter)
62 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
63 since 'forall' is a valid variable name, we don't know whether
64 to treat a forall on the input as the beginning of a quantifier
65 or the beginning of the rule itself. Resolving to shift means
66 it's always treated as a quantifier, hence the above is disallowed.
67 This saves explicitly defining a grammar for the rule lhs that
68 doesn't include 'forall'.
70 1 for ambiguity in 'x @ Rec{..}'.
71 Only sensible parse is 'x @ (Rec{..})', which is what resolving
74 6 for conflicts between `fdecl' and `fdeclDEPRECATED', which are resolved
75 correctly, and moreover, should go away when `fdeclDEPRECATED' is removed.
77 -----------------------------------------------------------------------------
81 '_' { ITunderscore } -- Haskell keywords
86 'default' { ITdefault }
87 'deriving' { ITderiving }
97 'instance' { ITinstance }
100 'newtype' { ITnewtype }
102 'qualified' { ITqualified }
106 '_scc_' { ITscc } -- ToDo: remove
108 'forall' { ITforall } -- GHC extension keywords
109 'foreign' { ITforeign }
110 'export' { ITexport }
112 'dynamic' { ITdynamic }
114 'threadsafe' { ITthreadsafe }
115 'unsafe' { ITunsafe }
117 'stdcall' { ITstdcallconv }
118 'ccall' { ITccallconv }
119 'dotnet' { ITdotnet }
120 '_ccall_' { ITccall (False, False, PlayRisky) }
121 '_ccall_GC_' { ITccall (False, False, PlaySafe False) }
122 '_casm_' { ITccall (False, True, PlayRisky) }
123 '_casm_GC_' { ITccall (False, True, PlaySafe False) }
125 '{-# SPECIALISE' { ITspecialise_prag }
126 '{-# SOURCE' { ITsource_prag }
127 '{-# INLINE' { ITinline_prag }
128 '{-# NOINLINE' { ITnoinline_prag }
129 '{-# RULES' { ITrules_prag }
130 '{-# SCC' { ITscc_prag }
131 '{-# DEPRECATED' { ITdeprecated_prag }
132 '#-}' { ITclose_prag }
135 '__interface' { ITinterface } -- interface keywords
136 '__export' { IT__export }
137 '__instimport' { ITinstimport }
138 '__forall' { IT__forall }
139 '__letrec' { ITletrec }
140 '__coerce' { ITcoerce }
141 '__depends' { ITdepends }
142 '__inline' { ITinline }
143 '__DEFAULT' { ITdefaultbranch }
145 '__integer' { ITinteger_lit }
146 '__float' { ITfloat_lit }
147 '__rational' { ITrational_lit }
148 '__addr' { ITaddr_lit }
149 '__label' { ITlabel_lit }
150 '__litlit' { ITlit_lit }
151 '__string' { ITstring_lit }
152 '__ccall' { ITccall $$ }
154 '__sccC' { ITsccAllCafs }
157 '__P' { ITspecialise }
160 '__S' { ITstrict $$ }
161 '__M' { ITcprinfo $$ }
164 '..' { ITdotdot } -- reserved symbols
179 '{' { ITocurly } -- special symbols
183 vccurly { ITvccurly } -- virtual close curly (from layout)
196 VARID { ITvarid $$ } -- identifiers
198 VARSYM { ITvarsym $$ }
199 CONSYM { ITconsym $$ }
200 QVARID { ITqvarid $$ }
201 QCONID { ITqconid $$ }
202 QVARSYM { ITqvarsym $$ }
203 QCONSYM { ITqconsym $$ }
205 IPDUPVARID { ITdupipvarid $$ } -- GHC extension
206 IPSPLITVARID { ITsplitipvarid $$ } -- GHC extension
209 STRING { ITstring $$ }
210 INTEGER { ITinteger $$ }
211 RATIONAL { ITrational $$ }
213 PRIMCHAR { ITprimchar $$ }
214 PRIMSTRING { ITprimstring $$ }
215 PRIMINTEGER { ITprimint $$ }
216 PRIMFLOAT { ITprimfloat $$ }
217 PRIMDOUBLE { ITprimdouble $$ }
218 CLITLIT { ITlitlit $$ }
220 %monad { P } { thenP } { returnP }
221 %lexer { lexer } { ITeof }
222 %name parseModule module
223 %name parseStmt maybe_stmt
224 %name parseIdentifier identifier
228 -----------------------------------------------------------------------------
231 -- The place for module deprecation is really too restrictive, but if it
232 -- was allowed at its natural place just before 'module', we get an ugly
233 -- s/r conflict with the second alternative. Another solution would be the
234 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
235 -- either, and DEPRECATED is only expected to be used by people who really
236 -- know what they are doing. :-)
238 module :: { RdrNameHsModule }
239 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
240 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
242 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
244 maybemoddeprec :: { Maybe DeprecTxt }
245 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
246 | {- empty -} { Nothing }
248 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
250 | layout_on top close { $2 }
252 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
253 : importdecls { (reverse $1,[]) }
254 | importdecls ';' cvtopdecls { (reverse $1,$3) }
255 | cvtopdecls { ([],$1) }
257 cvtopdecls :: { [RdrNameHsDecl] }
258 : topdecls { cvTopDecls (groupBindings $1)}
260 -----------------------------------------------------------------------------
263 maybeexports :: { Maybe [RdrNameIE] }
264 : '(' exportlist ')' { Just $2 }
265 | {- empty -} { Nothing }
267 exportlist :: { [RdrNameIE] }
268 : exportlist ',' export { $3 : $1 }
269 | exportlist ',' { $1 }
273 -- GHC extension: we allow things like [] and (,,,) to be exported
274 export :: { RdrNameIE }
276 | gtycon { IEThingAbs $1 }
277 | gtycon '(' '..' ')' { IEThingAll $1 }
278 | gtycon '(' ')' { IEThingWith $1 [] }
279 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
280 | 'module' modid { IEModuleContents $2 }
282 qcnames :: { [RdrName] }
283 : qcnames ',' qcname { $3 : $1 }
286 qcname :: { RdrName }
290 -----------------------------------------------------------------------------
291 -- Import Declarations
293 -- import decls can be *empty*, or even just a string of semicolons
294 -- whereas topdecls must contain at least one topdecl.
296 importdecls :: { [RdrNameImportDecl] }
297 : importdecls ';' importdecl { $3 : $1 }
298 | importdecls ';' { $1 }
299 | importdecl { [ $1 ] }
302 importdecl :: { RdrNameImportDecl }
303 : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec
304 { ImportDecl $5 $3 $4 $6 $7 $2 }
306 maybe_src :: { WhereFrom }
307 : '{-# SOURCE' '#-}' { ImportByUserSource }
308 | {- empty -} { ImportByUser }
310 optqualified :: { Bool }
311 : 'qualified' { True }
312 | {- empty -} { False }
314 maybeas :: { Maybe ModuleName }
315 : 'as' modid { Just $2 }
316 | {- empty -} { Nothing }
318 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
319 : impspec { Just $1 }
320 | {- empty -} { Nothing }
322 impspec :: { (Bool, [RdrNameIE]) }
323 : '(' exportlist ')' { (False, reverse $2) }
324 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
326 -----------------------------------------------------------------------------
327 -- Fixity Declarations
331 | INTEGER {% checkPrec $1 `thenP_`
332 returnP (fromInteger $1) }
334 infix :: { FixityDirection }
336 | 'infixl' { InfixL }
337 | 'infixr' { InfixR }
340 : ops ',' op { $3 : $1 }
343 -----------------------------------------------------------------------------
344 -- Top-Level Declarations
346 topdecls :: { [RdrBinding] }
347 : topdecls ';' topdecl { ($3 : $1) }
348 | topdecls ';' { $1 }
351 topdecl :: { RdrBinding }
352 : srcloc 'type' tycon tv_bndrs '=' ctype
353 -- Note ctype, not sigtype.
354 -- We allow an explicit for-all but we don't insert one
355 -- in type Foo a = (b,b)
356 -- Instead we just say b is out of scope
357 { RdrHsDecl (TyClD (TySynonym $3 $4 $6 $1)) }
360 | srcloc 'data' tycl_hdr constrs deriving
361 {% returnP (RdrHsDecl (TyClD
362 (mkTyData DataType $3 (DataCons (reverse $4)) $5 $1))) }
364 | srcloc 'newtype' tycl_hdr '=' newconstr deriving
365 {% returnP (RdrHsDecl (TyClD
366 (mkTyData NewType $3 (DataCons [$5]) $6 $1))) }
368 | srcloc 'class' tycl_hdr fds where
370 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
372 returnP (RdrHsDecl (TyClD
373 (mkClassDecl $3 $4 sigs (Just binds) $1))) }
375 | srcloc 'instance' inst_type where
377 = cvMonoBindsAndSigs cvInstDeclSig
379 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
381 | srcloc 'default' '(' comma_types0 ')' { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
382 | 'foreign' fdecl { RdrHsDecl $2 }
383 | '{-# DEPRECATED' deprecations '#-}' { $2 }
384 | '{-# RULES' rules '#-}' { $2 }
387 -- tycl_hdr parses the header of a type or class decl,
388 -- which takes the form
391 -- (Eq a, Ord b) => T a b
392 -- Rather a lot of inlining here, else we get reduce/reduce errors
393 tycl_hdr :: { (RdrNameContext, RdrName, [RdrNameHsTyVar]) }
394 : '(' comma_types1 ')' '=>' gtycon tv_bndrs {% mapP checkPred $2 `thenP` \ cxt ->
395 returnP (cxt, $5, $6) }
396 -- qtycon for the class below name would lead to many s/r conflicts
397 -- FIXME: does the renamer pick up all wrong forms and raise an
399 | gtycon atypes1 '=>' gtycon atypes0 {% checkTyVars $5 `thenP` \ tvs ->
400 returnP ([HsClassP $1 $2], $4, tvs) }
401 | gtycon atypes0 {% checkTyVars $2 `thenP` \ tvs ->
402 returnP ([], $1, tvs) }
403 -- We have to have qtycon in this production to avoid s/r
404 -- conflicts with the previous one. The renamer will complain
405 -- if we use a qualified tycon.
407 -- Using a `gtycon' throughout. This enables special syntax,
408 -- such as "[]" for tycons as well as tycon ops in
409 -- parentheses. This is beyond H98, but used repeatedly in
410 -- the Prelude modules. (So, it would be a good idea to raise
411 -- an error in the renamer if some non-H98 form is used and
412 -- -fglasgow-exts is not given.) -=chak
414 decls :: { [RdrBinding] }
415 : decls ';' decl { $3 : $1 }
420 decl :: { RdrBinding }
423 | '{-# INLINE' srcloc activation qvar '#-}' { RdrSig (InlineSig True $4 $3 $2) }
424 | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
425 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
426 { foldr1 RdrAndBindings
427 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
428 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
429 { RdrSig (SpecInstSig $4 $2) }
431 wherebinds :: { RdrNameHsBinds }
432 : where { cvBinds cvValSig (groupBindings $1) }
434 where :: { [RdrBinding] }
435 : 'where' decllist { $2 }
438 declbinds :: { RdrNameHsBinds }
439 : decllist { cvBinds cvValSig (groupBindings $1) }
441 decllist :: { [RdrBinding] }
442 : '{' decls '}' { $2 }
443 | layout_on decls close { $2 }
445 fixdecl :: { RdrBinding }
446 : srcloc infix prec ops { foldr1 RdrAndBindings
447 [ RdrSig (FixSig (FixitySig n
451 -----------------------------------------------------------------------------
452 -- Transformation Rules
454 rules :: { RdrBinding }
455 : rules ';' rule { $1 `RdrAndBindings` $3 }
458 | {- empty -} { RdrNullBind }
460 rule :: { RdrBinding }
461 : STRING activation rule_forall infixexp '=' srcloc exp
462 { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
464 activation :: { Activation } -- Omitted means AlwaysActive
465 : {- empty -} { AlwaysActive }
466 | explicit_activation { $1 }
468 inverse_activation :: { Activation } -- Omitted means NeverActive
469 : {- empty -} { NeverActive }
470 | explicit_activation { $1 }
472 explicit_activation :: { Activation } -- In brackets
473 : '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
474 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger $3) }
476 rule_forall :: { [RdrNameRuleBndr] }
477 : 'forall' rule_var_list '.' { $2 }
480 rule_var_list :: { [RdrNameRuleBndr] }
482 | rule_var rule_var_list { $1 : $2 }
484 rule_var :: { RdrNameRuleBndr }
485 : varid { RuleBndr $1 }
486 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
488 -----------------------------------------------------------------------------
491 deprecations :: { RdrBinding }
492 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
493 | deprecations ';' { $1 }
495 | {- empty -} { RdrNullBind }
497 -- SUP: TEMPORARY HACK, not checking for `module Foo'
498 deprecation :: { RdrBinding }
499 : srcloc depreclist STRING
500 { foldr RdrAndBindings RdrNullBind
501 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
504 -----------------------------------------------------------------------------
505 -- Foreign import and export declarations
507 -- for the time being, the following accepts foreign declarations conforming
508 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
510 -- * a flag indicates whether pre-standard declarations have been used and
511 -- triggers a deprecation warning further down the road
513 -- NB: The first two rules could be combined into one by replacing `safety1'
514 -- with `safety'. However, the combined rule conflicts with the
517 fdecl :: { RdrNameHsDecl }
518 fdecl : srcloc 'import' callconv safety1 fspec {% mkImport $3 $4 $5 $1 }
519 | srcloc 'import' callconv fspec {% mkImport $3 (PlaySafe False) $4 $1 }
520 | srcloc 'export' callconv fspec {% mkExport $3 $4 $1 }
521 -- the following syntax is DEPRECATED
522 | srcloc fdecl1DEPRECATED { ForD ($2 True $1) }
523 | srcloc fdecl2DEPRECATED { $2 $1 }
525 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
527 ----------- DEPRECATED label decls ------------
528 : 'label' ext_name varid '::' sigtype
529 { ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) _NIL_ _NIL_
530 (CLabel ($2 `orElse` mkExtName $3))) }
532 ----------- DEPRECATED ccall/stdcall decls ------------
534 -- NB: This business with the case expression below may seem overly
535 -- complicated, but it is necessary to avoid some conflicts.
537 -- DEPRECATED variant #1: lack of a calling convention specification
539 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
541 target = StaticTarget ($2 `orElse` mkExtName $4)
543 ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
544 (CFunction target)) }
546 -- DEPRECATED variant #2: external name consists of two separate strings
547 -- (module name and function name) (import)
548 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
550 DNCall -> parseError "Illegal format of .NET foreign import"
551 CCall cconv -> returnP $
553 imp = CFunction (StaticTarget $4)
555 ForeignImport $6 $8 (CImport cconv $5 _NIL_ _NIL_ imp) }
557 -- DEPRECATED variant #3: `unsafe' after entity
558 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
560 DNCall -> parseError "Illegal format of .NET foreign import"
561 CCall cconv -> returnP $
563 imp = CFunction (StaticTarget $3)
565 ForeignImport $5 $7 (CImport cconv PlayRisky _NIL_ _NIL_ imp) }
567 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
568 -- an explicit calling convention (import)
569 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
570 { ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
571 (CFunction DynamicTarget)) }
573 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
574 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
576 DNCall -> parseError "Illegal format of .NET foreign import"
577 CCall cconv -> returnP $
578 ForeignImport $5 $7 (CImport cconv $4 _NIL_ _NIL_
579 (CFunction DynamicTarget)) }
581 -- DEPRECATED variant #6: lack of a calling convention specification
583 | 'export' {-no callconv-} ext_name varid '::' sigtype
584 { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3)
587 -- DEPRECATED variant #7: external name consists of two separate strings
588 -- (module name and function name) (export)
589 | 'export' callconv STRING STRING varid '::' sigtype
591 DNCall -> parseError "Illegal format of .NET foreign import"
592 CCall cconv -> returnP $
594 (CExport (CExportStatic $4 cconv)) }
596 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
597 -- an explicit calling convention (export)
598 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
599 { ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) _NIL_ _NIL_
602 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
603 | 'export' callconv 'dynamic' varid '::' sigtype
605 DNCall -> parseError "Illegal format of .NET foreign import"
606 CCall cconv -> returnP $
607 ForeignImport $4 $6 (CImport cconv (PlaySafe False) _NIL_ _NIL_ CWrapper) }
609 ----------- DEPRECATED .NET decls ------------
610 -- NB: removed the .NET call declaration, as it is entirely subsumed
611 -- by the new standard FFI declarations
613 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
615 : 'import' 'dotnet' 'type' ext_name tycon
616 { \loc -> TyClD (ForeignType $5 $4 DNType loc) }
617 -- left this one unchanged for the moment as type imports are not
618 -- covered currently by the FFI standard -=chak
621 callconv :: { CallConv }
622 : 'stdcall' { CCall StdCallConv }
623 | 'ccall' { CCall CCallConv }
624 | 'dotnet' { DNCall }
627 : 'unsafe' { PlayRisky }
628 | 'safe' { PlaySafe False }
629 | 'threadsafe' { PlaySafe True }
630 | {- empty -} { PlaySafe False }
632 safety1 :: { Safety }
633 : 'unsafe' { PlayRisky }
634 | 'safe' { PlaySafe False }
635 | 'threadsafe' { PlaySafe True }
636 -- only needed to avoid conflicts with the DEPRECATED rules
638 fspec :: { (FAST_STRING, RdrName, RdrNameHsType) }
639 : STRING varid '::' sigtype { ($1 , $2, $4) }
640 | varid '::' sigtype { (SLIT(""), $1, $3) }
641 -- if the entity string is missing, it defaults to the empty string;
642 -- the meaning of an empty entity string depends on the calling
646 ext_name :: { Maybe CLabelString }
648 | STRING STRING { Just $2 } -- Ignore "module name" for now
649 | {- empty -} { Nothing }
652 -----------------------------------------------------------------------------
655 opt_sig :: { Maybe RdrNameHsType }
656 : {- empty -} { Nothing }
657 | '::' sigtype { Just $2 }
659 opt_asig :: { Maybe RdrNameHsType }
660 : {- empty -} { Nothing }
661 | '::' atype { Just $2 }
663 sigtypes :: { [RdrNameHsType] }
665 | sigtypes ',' sigtype { $3 : $1 }
667 sigtype :: { RdrNameHsType }
668 : ctype { mkHsForAllTy Nothing [] $1 }
670 sig_vars :: { [RdrName] }
671 : sig_vars ',' var { $3 : $1 }
674 -----------------------------------------------------------------------------
677 -- A ctype is a for-all type
678 ctype :: { RdrNameHsType }
679 : 'forall' tv_bndrs '.' ctype { mkHsForAllTy (Just $2) [] $4 }
680 | context '=>' type { mkHsForAllTy Nothing $1 $3 }
681 -- A type of form (context => type) is an *implicit* HsForAllTy
684 -- We parse a context as a btype so that we don't get reduce/reduce
685 -- errors in ctype. The basic problem is that
687 -- looks so much like a tuple type. We can't tell until we find the =>
688 context :: { RdrNameContext }
689 : btype {% checkContext $1 }
691 type :: { RdrNameHsType }
692 : gentype '->' type { HsFunTy $1 $3 }
693 | ipvar '::' type { mkHsIParamTy $1 $3 }
696 gentype :: { RdrNameHsType }
699 | atype tyconop atype { HsOpTy $1 $2 $3 }
701 btype :: { RdrNameHsType }
702 : btype atype { HsAppTy $1 $2 }
705 atype :: { RdrNameHsType }
706 : gtycon { HsTyVar $1 }
707 | tyvar { HsTyVar $1 }
708 | '(' type ',' comma_types1 ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2:$4) }
709 | '(#' comma_types1 '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
710 | '[' type ']' { HsListTy $2 }
711 | '[:' type ':]' { HsPArrTy $2 }
712 | '(' ctype ')' { $2 }
713 | '(' ctype '::' kind ')' { HsKindSig $2 $4 }
715 | INTEGER { HsNumTy $1 }
717 -- An inst_type is what occurs in the head of an instance decl
718 -- e.g. (Foo a, Gaz b) => Wibble a b
719 -- It's kept as a single type, with a MonoDictTy at the right
720 -- hand corner, for convenience.
721 inst_type :: { RdrNameHsType }
722 : ctype {% checkInstType $1 }
724 comma_types0 :: { [RdrNameHsType] }
725 : comma_types1 { $1 }
728 comma_types1 :: { [RdrNameHsType] }
730 | type ',' comma_types1 { $1 : $3 }
732 atypes0 :: { [RdrNameHsType] }
736 atypes1 :: { [RdrNameHsType] }
738 | atype atypes1 { $1 : $2 }
740 tv_bndrs :: { [RdrNameHsTyVar] }
741 : tv_bndr tv_bndrs { $1 : $2 }
744 tv_bndr :: { RdrNameHsTyVar }
745 : tyvar { UserTyVar $1 }
746 | '(' tyvar '::' kind ')' { IfaceTyVar $2 $4 }
748 fds :: { [([RdrName], [RdrName])] }
750 | '|' fds1 { reverse $2 }
752 fds1 :: { [([RdrName], [RdrName])] }
753 : fds1 ',' fd { $3 : $1 }
756 fd :: { ([RdrName], [RdrName]) }
757 : varids0 '->' varids0 { (reverse $1, reverse $3) }
759 varids0 :: { [RdrName] }
761 | varids0 tyvar { $2 : $1 }
763 -----------------------------------------------------------------------------
768 | akind '->' kind { mkArrowKind $1 $3 }
771 : '*' { liftedTypeKind }
772 | '(' kind ')' { $2 }
775 -----------------------------------------------------------------------------
776 -- Datatype declarations
778 newconstr :: { RdrNameConDecl }
779 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
780 | srcloc conid '{' var '::' ctype '}'
781 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
783 constrs :: { [RdrNameConDecl] }
784 : {- empty; a GHC extension -} { [] }
785 | '=' constrs1 { $2 }
787 constrs1 :: { [RdrNameConDecl] }
788 : constrs1 '|' constr { $3 : $1 }
791 constr :: { RdrNameConDecl }
792 : srcloc forall context '=>' constr_stuff
793 { mkConDecl (fst $5) $2 $3 (snd $5) $1 }
794 | srcloc forall constr_stuff
795 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
797 forall :: { [RdrNameHsTyVar] }
798 : 'forall' tv_bndrs '.' { $2 }
801 constr_stuff :: { (RdrName, RdrNameConDetails) }
802 : btype {% mkVanillaCon $1 [] }
803 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
804 | gtycon '{' '}' {% mkRecCon $1 [] }
805 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
806 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
808 satypes :: { [RdrNameBangType] }
809 : atype satypes { unbangedType $1 : $2 }
810 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
813 sbtype :: { RdrNameBangType }
814 : btype { unbangedType $1 }
815 | '!' atype { BangType MarkedUserStrict $2 }
817 fielddecls :: { [([RdrName],RdrNameBangType)] }
818 : fielddecl ',' fielddecls { $1 : $3 }
821 fielddecl :: { ([RdrName],RdrNameBangType) }
822 : sig_vars '::' stype { (reverse $1, $3) }
824 stype :: { RdrNameBangType }
825 : ctype { unbangedType $1 }
826 | '!' atype { BangType MarkedUserStrict $2 }
828 deriving :: { Maybe RdrNameContext }
829 : {- empty -} { Nothing }
830 | 'deriving' context { Just $2 }
831 -- Glasgow extension: allow partial
832 -- applications in derivings
834 -----------------------------------------------------------------------------
837 {- There's an awkward overlap with a type signature. Consider
838 f :: Int -> Int = ...rhs...
839 Then we can't tell whether it's a type signature or a value
840 definition with a result signature until we see the '='.
841 So we have to inline enough to postpone reductions until we know.
845 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
846 instead of qvar, we get another shift/reduce-conflict. Consider the
849 { (^^) :: Int->Int ; } Type signature; only var allowed
851 { (^^) :: Int->Int = ... ; } Value defn with result signature;
852 qvar allowed (because of instance decls)
854 We can't tell whether to reduce var to qvar until after we've read the signatures.
857 valdef :: { RdrBinding }
858 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
859 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
860 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
861 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
865 rhs :: { RdrNameGRHSs }
866 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
867 | gdrhs wherebinds { GRHSs (reverse $1) $2 placeHolderType }
869 gdrhs :: { [RdrNameGRHS] }
870 : gdrhs gdrh { $2 : $1 }
873 gdrh :: { RdrNameGRHS }
874 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
876 -----------------------------------------------------------------------------
879 exp :: { RdrNameHsExpr }
880 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
881 | infixexp 'with' dbinding { HsWith $1 $3 }
884 infixexp :: { RdrNameHsExpr }
886 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
887 (panic "fixity") $3 )}
889 exp10 :: { RdrNameHsExpr }
890 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
891 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
892 returnP (HsLam (Match ps $5
893 (GRHSs (unguardedRHS $8 $7)
894 EmptyBinds placeHolderType))) }
895 | 'let' declbinds 'in' exp { HsLet $2 $4 }
896 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
897 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
898 | '-' fexp { mkHsNegApp $2 }
899 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
900 returnP (HsDo DoExpr stmts $1) }
902 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False placeHolderType }
903 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 (PlaySafe False) False placeHolderType }
904 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True placeHolderType }
905 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 (PlaySafe False) True placeHolderType }
907 | scc_annot exp { if opt_SccProfilingOn
913 scc_annot :: { FAST_STRING }
914 : '_scc_' STRING { $2 }
915 | '{-# SCC' STRING '#-}' { $2 }
917 ccallid :: { FAST_STRING }
921 fexp :: { RdrNameHsExpr }
922 : fexp aexp { (HsApp $1 $2) }
925 aexps0 :: { [RdrNameHsExpr] }
926 : aexps { reverse $1 }
928 aexps :: { [RdrNameHsExpr] }
929 : aexps aexp { $2 : $1 }
932 aexp :: { RdrNameHsExpr }
933 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
934 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
938 var_or_con :: { RdrNameHsExpr }
942 aexp1 :: { RdrNameHsExpr }
943 : ipvar { HsIPVar $1 }
945 | literal { HsLit $1 }
946 | INTEGER { HsOverLit (mkHsIntegral $1) }
947 | RATIONAL { HsOverLit (mkHsFractional $1) }
948 | '(' exp ')' { HsPar $2 }
949 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
950 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
951 | '[' list ']' { $2 }
952 | '[:' parr ':]' { $2 }
953 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
954 | '(' qopm infixexp ')' { (SectionR $2 $3) }
955 | qvar '@' aexp { EAsPat $1 $3 }
957 | '~' aexp1 { ELazyPat $2 }
959 texps :: { [RdrNameHsExpr] }
960 : texps ',' exp { $3 : $1 }
964 -----------------------------------------------------------------------------
967 -- The rules below are little bit contorted to keep lexps left-recursive while
968 -- avoiding another shift/reduce-conflict.
970 list :: { RdrNameHsExpr }
971 : exp { ExplicitList placeHolderType [$1] }
972 | lexps { ExplicitList placeHolderType (reverse $1) }
973 | exp '..' { ArithSeqIn (From $1) }
974 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
975 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
976 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
977 | exp srcloc pquals {% let { body [qs] = qs;
978 body qss = [ParStmt (map reverse qss)] }
980 returnP ( HsDo ListComp
981 (reverse (ResultStmt $1 $2 : body $3))
986 lexps :: { [RdrNameHsExpr] }
987 : lexps ',' exp { $3 : $1 }
988 | exp ',' exp { [$3,$1] }
990 -----------------------------------------------------------------------------
991 -- List Comprehensions
993 pquals :: { [[RdrNameStmt]] }
994 : pquals '|' quals { $3 : $1 }
997 quals :: { [RdrNameStmt] }
998 : quals ',' stmt { $3 : $1 }
1001 -----------------------------------------------------------------------------
1002 -- Parallel array expressions
1004 -- The rules below are little bit contorted; see the list case for details.
1005 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1006 -- Moreover, we allow explicit arrays with no element (represented by the nil
1007 -- constructor in the list case).
1009 parr :: { RdrNameHsExpr }
1010 : { ExplicitPArr placeHolderType [] }
1011 | exp { ExplicitPArr placeHolderType [$1] }
1012 | lexps { ExplicitPArr placeHolderType
1014 | exp '..' exp { PArrSeqIn (FromTo $1 $3) }
1015 | exp ',' exp '..' exp { PArrSeqIn (FromThenTo $1 $3 $5) }
1016 | exp srcloc pquals {% let {
1023 (reverse (ResultStmt $1 $2
1028 -- We are reusing `lexps' and `pquals' from the list case.
1030 -----------------------------------------------------------------------------
1031 -- Case alternatives
1033 altslist :: { [RdrNameMatch] }
1034 : '{' alts '}' { reverse $2 }
1035 | layout_on alts close { reverse $2 }
1037 alts :: { [RdrNameMatch] }
1041 alts1 :: { [RdrNameMatch] }
1042 : alts1 ';' alt { $3 : $1 }
1046 alt :: { RdrNameMatch }
1047 : srcloc infixexp opt_sig ralt wherebinds
1048 {% (checkPattern $1 $2 `thenP` \p ->
1049 returnP (Match [p] $3
1050 (GRHSs $4 $5 placeHolderType)) )}
1052 ralt :: { [RdrNameGRHS] }
1053 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
1054 | gdpats { reverse $1 }
1056 gdpats :: { [RdrNameGRHS] }
1057 : gdpats gdpat { $2 : $1 }
1060 gdpat :: { RdrNameGRHS }
1061 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
1063 -----------------------------------------------------------------------------
1064 -- Statement sequences
1066 stmtlist :: { [RdrNameStmt] }
1067 : '{' stmts '}' { $2 }
1068 | layout_on_for_do stmts close { $2 }
1070 -- do { ;; s ; s ; ; s ;; }
1071 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1072 -- here, because we need too much lookahead if we see do { e ; }
1073 -- So we use ExprStmts throughout, and switch the last one over
1074 -- in ParseUtils.checkDo instead
1075 stmts :: { [RdrNameStmt] }
1076 : stmt stmts_help { $1 : $2 }
1078 | {- empty -} { [] }
1080 stmts_help :: { [RdrNameStmt] }
1082 | {- empty -} { [] }
1084 -- For typing stmts at the GHCi prompt, where
1085 -- the input may consist of just comments.
1086 maybe_stmt :: { Maybe RdrNameStmt }
1088 | {- nothing -} { Nothing }
1090 stmt :: { RdrNameStmt }
1091 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
1092 returnP (BindStmt p $4 $1) }
1093 | srcloc exp { ExprStmt $2 placeHolderType $1 }
1094 | srcloc 'let' declbinds { LetStmt $3 }
1096 -----------------------------------------------------------------------------
1097 -- Record Field Update/Construction
1099 fbinds :: { RdrNameHsRecordBinds }
1100 : fbinds ',' fbind { $3 : $1 }
1103 | {- empty -} { [] }
1105 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
1106 : qvar '=' exp { ($1,$3,False) }
1108 -----------------------------------------------------------------------------
1109 -- Implicit Parameter Bindings
1111 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1112 : '{' dbinds '}' { $2 }
1113 | layout_on dbinds close { $2 }
1115 dbinds :: { [(IPName RdrName, RdrNameHsExpr)] }
1116 : dbinds ';' dbind { $3 : $1 }
1119 | {- empty -} { [] }
1121 dbind :: { (IPName RdrName, RdrNameHsExpr) }
1122 dbind : ipvar '=' exp { ($1, $3) }
1124 -----------------------------------------------------------------------------
1125 -- Variables, Constructors and Operators.
1127 identifier :: { RdrName }
1132 depreclist :: { [RdrName] }
1133 depreclist : deprec_var { [$1] }
1134 | deprec_var ',' depreclist { $1 : $3 }
1136 deprec_var :: { RdrName }
1137 deprec_var : var { $1 }
1140 gtycon :: { RdrName }
1142 | '(' qtyconop ')' { $2 }
1143 | '(' ')' { unitTyCon_RDR }
1144 | '(' '->' ')' { funTyCon_RDR }
1145 | '[' ']' { listTyCon_RDR }
1146 | '[:' ':]' { parrTyCon_RDR }
1147 | '(' commas ')' { tupleTyCon_RDR $2 }
1149 gcon :: { RdrName } -- Data constructor namespace
1150 : '(' ')' { unitCon_RDR }
1151 | '[' ']' { nilCon_RDR }
1152 | '(' commas ')' { tupleCon_RDR $2 }
1154 -- the case of '[:' ':]' is part of the production `parr'
1158 | '(' varsym ')' { $2 }
1162 | '(' varsym ')' { $2 }
1163 | '(' qvarsym1 ')' { $2 }
1164 -- We've inlined qvarsym here so that the decision about
1165 -- whether it's a qvar or a var can be postponed until
1166 -- *after* we see the close paren.
1168 ipvar :: { IPName RdrName }
1169 : IPDUPVARID { Dupable (mkUnqual varName $1) }
1170 | IPSPLITVARID { Linear (mkUnqual varName $1) }
1174 | '(' qconsym ')' { $2 }
1176 varop :: { RdrName }
1178 | '`' varid '`' { $2 }
1180 qvarop :: { RdrName }
1182 | '`' qvarid '`' { $2 }
1184 qvaropm :: { RdrName }
1185 : qvarsym_no_minus { $1 }
1186 | '`' qvarid '`' { $2 }
1188 conop :: { RdrName }
1190 | '`' conid '`' { $2 }
1192 qconop :: { RdrName }
1194 | '`' qconid '`' { $2 }
1196 -----------------------------------------------------------------------------
1199 op :: { RdrName } -- used in infix decls
1203 qop :: { RdrName {-HsExpr-} } -- used in sections
1207 qopm :: { RdrNameHsExpr } -- used in sections
1208 : qvaropm { HsVar $1 }
1209 | qconop { HsVar $1 }
1211 -----------------------------------------------------------------------------
1214 qvarid :: { RdrName }
1216 | QVARID { mkQual varName $1 }
1218 varid :: { RdrName }
1219 : varid_no_unsafe { $1 }
1220 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1222 varid_no_unsafe :: { RdrName }
1223 : VARID { mkUnqual varName $1 }
1224 | special_id { mkUnqual varName $1 }
1225 | 'forall' { mkUnqual varName SLIT("forall") }
1227 tyvar :: { RdrName }
1228 : VARID { mkUnqual tvName $1 }
1229 | special_id { mkUnqual tvName $1 }
1230 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1232 -- These special_ids are treated as keywords in various places,
1233 -- but as ordinary ids elsewhere. A special_id collects all thsee
1234 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1235 special_id :: { UserFS }
1237 : 'as' { SLIT("as") }
1238 | 'qualified' { SLIT("qualified") }
1239 | 'hiding' { SLIT("hiding") }
1240 | 'export' { SLIT("export") }
1241 | 'label' { SLIT("label") }
1242 | 'dynamic' { SLIT("dynamic") }
1243 | 'stdcall' { SLIT("stdcall") }
1244 | 'ccall' { SLIT("ccall") }
1246 -----------------------------------------------------------------------------
1249 qconid :: { RdrName } -- Qualified or unqualifiedb
1251 | QCONID { mkQual dataName $1 }
1253 conid :: { RdrName }
1254 : CONID { mkUnqual dataName $1 }
1256 -----------------------------------------------------------------------------
1259 qconsym :: { RdrName } -- Qualified or unqualifiedb
1261 | QCONSYM { mkQual dataName $1 }
1263 consym :: { RdrName }
1264 : CONSYM { mkUnqual dataName $1 }
1266 -----------------------------------------------------------------------------
1269 qvarsym :: { RdrName }
1273 qvarsym_no_minus :: { RdrName }
1274 : varsym_no_minus { $1 }
1277 qvarsym1 :: { RdrName }
1278 qvarsym1 : QVARSYM { mkQual varName $1 }
1280 varsym :: { RdrName }
1281 : varsym_no_minus { $1 }
1282 | '-' { mkUnqual varName SLIT("-") }
1284 varsym_no_minus :: { RdrName } -- varsym not including '-'
1285 : VARSYM { mkUnqual varName $1 }
1286 | special_sym { mkUnqual varName $1 }
1289 -- See comments with special_id
1290 special_sym :: { UserFS }
1291 special_sym : '!' { SLIT("!") }
1295 -----------------------------------------------------------------------------
1298 literal :: { HsLit }
1299 : CHAR { HsChar $1 }
1300 | STRING { HsString $1 }
1301 | PRIMINTEGER { HsIntPrim $1 }
1302 | PRIMCHAR { HsCharPrim $1 }
1303 | PRIMSTRING { HsStringPrim $1 }
1304 | PRIMFLOAT { HsFloatPrim $1 }
1305 | PRIMDOUBLE { HsDoublePrim $1 }
1306 | CLITLIT { HsLitLit $1 placeHolderType }
1308 srcloc :: { SrcLoc } : {% getSrcLocP }
1310 -----------------------------------------------------------------------------
1314 : vccurly { () } -- context popped in lexer.
1315 | error {% popContext }
1317 layout_on :: { () } : {% layoutOn True{-strict-} }
1318 layout_on_for_do :: { () } : {% layoutOn False }
1320 -----------------------------------------------------------------------------
1321 -- Miscellaneous (mostly renamings)
1323 modid :: { ModuleName }
1324 : CONID { mkModuleNameFS $1 }
1325 | QCONID { mkModuleNameFS
1327 (unpackFS (fst $1) ++
1328 '.':unpackFS (snd $1)))
1331 tycon :: { RdrName }
1332 : CONID { mkUnqual tcClsName $1 }
1334 tyconop :: { RdrName }
1335 : CONSYM { mkUnqual tcClsName $1 }
1337 qtycon :: { RdrName } -- Qualified or unqualified
1338 : QCONID { mkQual tcClsName $1 }
1341 qtyconop :: { RdrName } -- Qualified or unqualified
1342 : QCONSYM { mkQual tcClsName $1 }
1346 : commas ',' { $1 + 1 }
1349 -----------------------------------------------------------------------------
1353 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)