2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.88 2002/02/13 14:05:51 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(..),
25 CCallConv(..), CCallTarget(..), defaultCCallConv,
27 import OccName ( UserFS, varName, tcName, dataName, tcClsName, tvName )
28 import SrcLoc ( SrcLoc )
30 import CmdLineOpts ( opt_SccProfilingOn )
31 import Type ( Kind, mkArrowKind, liftedTypeKind )
32 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
33 NewOrData(..), StrictnessMark(..), Activation(..) )
37 import CStrings ( CLabelString )
39 import Maybes ( orElse )
42 #include "HsVersions.h"
46 -----------------------------------------------------------------------------
47 Conflicts: 21 shift/reduce, -=chak[4Feb2]
49 9 for abiguity in 'if x then y else z + 1'
50 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
51 8 because op might be: - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
52 1 for ambiguity in 'if x then y else z :: T'
53 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
54 1 for ambiguity in 'if x then y else z with ?x=3'
55 (shift parses as 'if x then y else (z with ?x=3)'
57 3 for ambiguity in 'case x of y :: a -> b'
58 (don't know whether to reduce 'a' as a btype or shift the '->'.
59 conclusion: bogus expression anyway, doesn't matter)
61 1 for ambiguity in '{-# RULES "name" forall = ... #-}'
62 since 'forall' is a valid variable name, we don't know whether
63 to treat a forall on the input as the beginning of a quantifier
64 or the beginning of the rule itself. Resolving to shift means
65 it's always treated as a quantifier, hence the above is disallowed.
66 This saves explicitly defining a grammar for the rule lhs that
67 doesn't include 'forall'.
69 1 for ambiguity in 'x @ Rec{..}'.
70 Only sensible parse is 'x @ (Rec{..})', which is what resolving
73 6 for conflicts between `fdecl' and `fdeclDEPRECATED', which are resolved
74 correctly, and moreover, should go away when `fdeclDEPRECATED' is removed.
76 -----------------------------------------------------------------------------
80 '_' { ITunderscore } -- Haskell keywords
85 'default' { ITdefault }
86 'deriving' { ITderiving }
96 'instance' { ITinstance }
99 'newtype' { ITnewtype }
101 'qualified' { ITqualified }
105 '_scc_' { ITscc } -- ToDo: remove
107 'forall' { ITforall } -- GHC extension keywords
108 'foreign' { ITforeign }
109 'export' { ITexport }
111 'dynamic' { ITdynamic }
113 'unsafe' { ITunsafe }
115 'stdcall' { ITstdcallconv }
116 'ccall' { ITccallconv }
117 'dotnet' { ITdotnet }
118 '_ccall_' { ITccall (False, False, PlayRisky) }
119 '_ccall_GC_' { ITccall (False, False, PlaySafe) }
120 '_casm_' { ITccall (False, True, PlayRisky) }
121 '_casm_GC_' { ITccall (False, True, PlaySafe) }
123 '{-# SPECIALISE' { ITspecialise_prag }
124 '{-# SOURCE' { ITsource_prag }
125 '{-# INLINE' { ITinline_prag }
126 '{-# NOINLINE' { ITnoinline_prag }
127 '{-# RULES' { ITrules_prag }
128 '{-# SCC' { ITscc_prag }
129 '{-# DEPRECATED' { ITdeprecated_prag }
130 '#-}' { ITclose_prag }
133 '__interface' { ITinterface } -- interface keywords
134 '__export' { IT__export }
135 '__instimport' { ITinstimport }
136 '__forall' { IT__forall }
137 '__letrec' { ITletrec }
138 '__coerce' { ITcoerce }
139 '__depends' { ITdepends }
140 '__inline' { ITinline }
141 '__DEFAULT' { ITdefaultbranch }
143 '__integer' { ITinteger_lit }
144 '__float' { ITfloat_lit }
145 '__rational' { ITrational_lit }
146 '__addr' { ITaddr_lit }
147 '__label' { ITlabel_lit }
148 '__litlit' { ITlit_lit }
149 '__string' { ITstring_lit }
150 '__ccall' { ITccall $$ }
152 '__sccC' { ITsccAllCafs }
155 '__P' { ITspecialise }
158 '__S' { ITstrict $$ }
159 '__M' { ITcprinfo $$ }
162 '..' { ITdotdot } -- reserved symbols
177 '{' { ITocurly } -- special symbols
181 vccurly { ITvccurly } -- virtual close curly (from layout)
194 VARID { ITvarid $$ } -- identifiers
196 VARSYM { ITvarsym $$ }
197 CONSYM { ITconsym $$ }
198 QVARID { ITqvarid $$ }
199 QCONID { ITqconid $$ }
200 QVARSYM { ITqvarsym $$ }
201 QCONSYM { ITqconsym $$ }
203 IPDUPVARID { ITdupipvarid $$ } -- GHC extension
204 IPSPLITVARID { ITsplitipvarid $$ } -- GHC extension
207 STRING { ITstring $$ }
208 INTEGER { ITinteger $$ }
209 RATIONAL { ITrational $$ }
211 PRIMCHAR { ITprimchar $$ }
212 PRIMSTRING { ITprimstring $$ }
213 PRIMINTEGER { ITprimint $$ }
214 PRIMFLOAT { ITprimfloat $$ }
215 PRIMDOUBLE { ITprimdouble $$ }
216 CLITLIT { ITlitlit $$ }
218 %monad { P } { thenP } { returnP }
219 %lexer { lexer } { ITeof }
220 %name parseModule module
221 %name parseStmt maybe_stmt
222 %name parseIdentifier identifier
226 -----------------------------------------------------------------------------
229 -- The place for module deprecation is really too restrictive, but if it
230 -- was allowed at its natural place just before 'module', we get an ugly
231 -- s/r conflict with the second alternative. Another solution would be the
232 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
233 -- either, and DEPRECATED is only expected to be used by people who really
234 -- know what they are doing. :-)
236 module :: { RdrNameHsModule }
237 : srcloc 'module' modid maybemoddeprec maybeexports 'where' body
238 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
240 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
242 maybemoddeprec :: { Maybe DeprecTxt }
243 : '{-# DEPRECATED' STRING '#-}' { Just $2 }
244 | {- empty -} { Nothing }
246 body :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
248 | layout_on top close { $2 }
250 top :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
251 : importdecls { (reverse $1,[]) }
252 | importdecls ';' cvtopdecls { (reverse $1,$3) }
253 | cvtopdecls { ([],$1) }
255 cvtopdecls :: { [RdrNameHsDecl] }
256 : topdecls { cvTopDecls (groupBindings $1)}
258 -----------------------------------------------------------------------------
261 maybeexports :: { Maybe [RdrNameIE] }
262 : '(' exportlist ')' { Just $2 }
263 | {- empty -} { Nothing }
265 exportlist :: { [RdrNameIE] }
266 : exportlist ',' export { $3 : $1 }
267 | exportlist ',' { $1 }
271 -- GHC extension: we allow things like [] and (,,,) to be exported
272 export :: { RdrNameIE }
274 | gtycon { IEThingAbs $1 }
275 | gtycon '(' '..' ')' { IEThingAll $1 }
276 | gtycon '(' ')' { IEThingWith $1 [] }
277 | gtycon '(' qcnames ')' { IEThingWith $1 (reverse $3) }
278 | 'module' modid { IEModuleContents $2 }
280 qcnames :: { [RdrName] }
281 : qcnames ',' qcname { $3 : $1 }
284 qcname :: { RdrName }
288 -----------------------------------------------------------------------------
289 -- Import Declarations
291 -- import decls can be *empty*, or even just a string of semicolons
292 -- whereas topdecls must contain at least one topdecl.
294 importdecls :: { [RdrNameImportDecl] }
295 : importdecls ';' importdecl { $3 : $1 }
296 | importdecls ';' { $1 }
297 | importdecl { [ $1 ] }
300 importdecl :: { RdrNameImportDecl }
301 : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec
302 { ImportDecl $5 $3 $4 $6 $7 $2 }
304 maybe_src :: { WhereFrom }
305 : '{-# SOURCE' '#-}' { ImportByUserSource }
306 | {- empty -} { ImportByUser }
308 optqualified :: { Bool }
309 : 'qualified' { True }
310 | {- empty -} { False }
312 maybeas :: { Maybe ModuleName }
313 : 'as' modid { Just $2 }
314 | {- empty -} { Nothing }
316 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
317 : impspec { Just $1 }
318 | {- empty -} { Nothing }
320 impspec :: { (Bool, [RdrNameIE]) }
321 : '(' exportlist ')' { (False, reverse $2) }
322 | 'hiding' '(' exportlist ')' { (True, reverse $3) }
324 -----------------------------------------------------------------------------
325 -- Fixity Declarations
329 | INTEGER {% checkPrec $1 `thenP_`
330 returnP (fromInteger $1) }
332 infix :: { FixityDirection }
334 | 'infixl' { InfixL }
335 | 'infixr' { InfixR }
338 : ops ',' op { $3 : $1 }
341 -----------------------------------------------------------------------------
342 -- Top-Level Declarations
344 topdecls :: { [RdrBinding] }
345 : topdecls ';' topdecl { ($3 : $1) }
346 | topdecls ';' { $1 }
349 topdecl :: { RdrBinding }
350 : srcloc 'type' tycon tv_bndrs '=' ctype
351 -- Note ctype, not sigtype.
352 -- We allow an explicit for-all but we don't insert one
353 -- in type Foo a = (b,b)
354 -- Instead we just say b is out of scope
355 { RdrHsDecl (TyClD (TySynonym $3 $4 $6 $1)) }
358 | srcloc 'data' tycl_hdr constrs deriving
359 {% returnP (RdrHsDecl (TyClD
360 (mkTyData DataType $3 (reverse $4) (length $4) $5 $1))) }
362 | srcloc 'newtype' tycl_hdr '=' newconstr deriving
363 {% returnP (RdrHsDecl (TyClD
364 (mkTyData NewType $3 [$5] 1 $6 $1))) }
366 | srcloc 'class' tycl_hdr fds where
368 (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5)
370 returnP (RdrHsDecl (TyClD
371 (mkClassDecl $3 $4 sigs (Just binds) $1))) }
373 | srcloc 'instance' inst_type where
375 = cvMonoBindsAndSigs cvInstDeclSig
377 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
379 | srcloc 'default' '(' comma_types0 ')' { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
380 | 'foreign' fdecl { RdrHsDecl $2 }
381 | '{-# DEPRECATED' deprecations '#-}' { $2 }
382 | '{-# RULES' rules '#-}' { $2 }
385 -- tycl_hdr parses the header of a type or class decl,
386 -- which takes the form
389 -- (Eq a, Ord b) => T a b
390 -- Rather a lot of inlining here, else we get reduce/reduce errors
391 tycl_hdr :: { (RdrNameContext, RdrName, [RdrNameHsTyVar]) }
392 : '(' comma_types1 ')' '=>' gtycon tv_bndrs {% mapP checkPred $2 `thenP` \ cxt ->
393 returnP (cxt, $5, $6) }
394 -- qtycon for the class below name would lead to many s/r conflicts
395 -- FIXME: does the renamer pick up all wrong forms and raise an
397 | gtycon atypes1 '=>' gtycon atypes0 {% checkTyVars $5 `thenP` \ tvs ->
398 returnP ([HsClassP $1 $2], $4, tvs) }
399 | gtycon atypes0 {% checkTyVars $2 `thenP` \ tvs ->
400 returnP ([], $1, tvs) }
401 -- We have to have qtycon in this production to avoid s/r
402 -- conflicts with the previous one. The renamer will complain
403 -- if we use a qualified tycon.
405 -- Using a `gtycon' throughout. This enables special syntax,
406 -- such as "[]" for tycons as well as tycon ops in
407 -- parentheses. This is beyond H98, but used repeatedly in
408 -- the Prelude modules. (So, it would be a good idea to raise
409 -- an error in the renamer if some non-H98 form is used and
410 -- -fglasgow-exts is not given.) -=chak
412 decls :: { [RdrBinding] }
413 : decls ';' decl { $3 : $1 }
418 decl :: { RdrBinding }
421 | '{-# INLINE' srcloc activation qvar '#-}' { RdrSig (InlineSig True $4 $3 $2) }
422 | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
423 | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
424 { foldr1 RdrAndBindings
425 (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
426 | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
427 { RdrSig (SpecInstSig $4 $2) }
429 wherebinds :: { RdrNameHsBinds }
430 : where { cvBinds cvValSig (groupBindings $1) }
432 where :: { [RdrBinding] }
433 : 'where' decllist { $2 }
436 declbinds :: { RdrNameHsBinds }
437 : decllist { cvBinds cvValSig (groupBindings $1) }
439 decllist :: { [RdrBinding] }
440 : '{' decls '}' { $2 }
441 | layout_on decls close { $2 }
443 fixdecl :: { RdrBinding }
444 : srcloc infix prec ops { foldr1 RdrAndBindings
445 [ RdrSig (FixSig (FixitySig n
449 -----------------------------------------------------------------------------
450 -- Transformation Rules
452 rules :: { RdrBinding }
453 : rules ';' rule { $1 `RdrAndBindings` $3 }
456 | {- empty -} { RdrNullBind }
458 rule :: { RdrBinding }
459 : STRING activation rule_forall infixexp '=' srcloc exp
460 { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
462 activation :: { Activation } -- Omitted means AlwaysActive
463 : {- empty -} { AlwaysActive }
464 | explicit_activation { $1 }
466 inverse_activation :: { Activation } -- Omitted means NeverActive
467 : {- empty -} { NeverActive }
468 | explicit_activation { $1 }
470 explicit_activation :: { Activation } -- In brackets
471 : '[' INTEGER ']' { ActiveAfter (fromInteger $2) }
472 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger $3) }
474 rule_forall :: { [RdrNameRuleBndr] }
475 : 'forall' rule_var_list '.' { $2 }
478 rule_var_list :: { [RdrNameRuleBndr] }
480 | rule_var rule_var_list { $1 : $2 }
482 rule_var :: { RdrNameRuleBndr }
483 : varid { RuleBndr $1 }
484 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
486 -----------------------------------------------------------------------------
489 deprecations :: { RdrBinding }
490 : deprecations ';' deprecation { $1 `RdrAndBindings` $3 }
491 | deprecations ';' { $1 }
493 | {- empty -} { RdrNullBind }
495 -- SUP: TEMPORARY HACK, not checking for `module Foo'
496 deprecation :: { RdrBinding }
497 : srcloc depreclist STRING
498 { foldr RdrAndBindings RdrNullBind
499 [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
502 -----------------------------------------------------------------------------
503 -- Foreign import and export declarations
505 -- for the time being, the following accepts foreign declarations conforming
506 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
508 -- * a flag indicates whether pre-standard declarations have been used and
509 -- triggers a deprecation warning further down the road
511 -- NB: The first two rules could be combined into one by replacing `safety1'
512 -- with `safety'. However, the combined rule conflicts with the
515 fdecl :: { RdrNameHsDecl }
516 fdecl : srcloc 'import' callconv safety1 fspec {% mkImport $3 $4 $5 $1 }
517 | srcloc 'import' callconv fspec {% mkImport $3 PlaySafe $4 $1 }
518 | srcloc 'export' callconv fspec {% mkExport $3 $4 $1 }
519 -- the following syntax is DEPRECATED
520 | srcloc fdecl1DEPRECATED { ForD ($2 True $1) }
521 | srcloc fdecl2DEPRECATED { $2 $1 }
523 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
525 ----------- DEPRECATED label decls ------------
526 : 'label' ext_name varid '::' sigtype
527 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
528 (CLabel ($2 `orElse` mkExtName $3))) }
530 ----------- DEPRECATED ccall/stdcall decls ------------
532 -- NB: This business with the case expression below may seem overly
533 -- complicated, but it is necessary to avoid some conflicts.
535 -- DEPRECATED variant #1: lack of a calling convention specification
537 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
539 target = StaticTarget ($2 `orElse` mkExtName $4)
541 ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
542 (CFunction target)) }
544 -- DEPRECATED variant #2: external name consists of two separate strings
545 -- (module name and function name) (import)
546 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
548 DNCall -> parseError "Illegal format of .NET foreign import"
549 CCall cconv -> returnP $
551 imp = CFunction (StaticTarget $4)
553 ForeignImport $6 $8 (CImport cconv $5 _NIL_ _NIL_ imp) }
555 -- DEPRECATED variant #3: `unsafe' after entity
556 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
558 DNCall -> parseError "Illegal format of .NET foreign import"
559 CCall cconv -> returnP $
561 imp = CFunction (StaticTarget $3)
563 ForeignImport $5 $7 (CImport cconv PlayRisky _NIL_ _NIL_ imp) }
565 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
566 -- an explicit calling convention (import)
567 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
568 { ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_
569 (CFunction DynamicTarget)) }
571 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
572 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
574 DNCall -> parseError "Illegal format of .NET foreign import"
575 CCall cconv -> returnP $
576 ForeignImport $5 $7 (CImport cconv $4 _NIL_ _NIL_
577 (CFunction DynamicTarget)) }
579 -- DEPRECATED variant #6: lack of a calling convention specification
581 | 'export' {-no callconv-} ext_name varid '::' sigtype
582 { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3)
585 -- DEPRECATED variant #7: external name consists of two separate strings
586 -- (module name and function name) (export)
587 | 'export' callconv STRING STRING varid '::' sigtype
589 DNCall -> parseError "Illegal format of .NET foreign import"
590 CCall cconv -> returnP $
592 (CExport (CExportStatic $4 cconv)) }
594 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
595 -- an explicit calling convention (export)
596 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
597 { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_
600 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
601 | 'export' callconv 'dynamic' varid '::' sigtype
603 DNCall -> parseError "Illegal format of .NET foreign import"
604 CCall cconv -> returnP $
605 ForeignImport $4 $6 (CImport cconv PlaySafe _NIL_ _NIL_ CWrapper) }
607 ----------- DEPRECATED .NET decls ------------
608 -- NB: removed the .NET call declaration, as it is entirely subsumed
609 -- by the new standard FFI declarations
611 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
613 : 'import' 'dotnet' 'type' ext_name tycon
614 { \loc -> TyClD (ForeignType $5 $4 DNType loc) }
615 -- left this one unchanged for the moment as type imports are not
616 -- covered currently by the FFI standard -=chak
619 callconv :: { CallConv }
620 : 'stdcall' { CCall StdCallConv }
621 | 'ccall' { CCall CCallConv }
622 | 'dotnet' { DNCall }
625 : 'unsafe' { PlayRisky }
626 | 'safe' { PlaySafe }
627 | {- empty -} { PlaySafe }
629 safety1 :: { Safety }
630 : 'unsafe' { PlayRisky }
631 | 'safe' { PlaySafe }
632 -- only needed to avoid conflicts with the DEPRECATED rules
634 fspec :: { (FAST_STRING, RdrName, RdrNameHsType) }
635 : STRING varid '::' sigtype { ($1 , $2, $4) }
636 | varid '::' sigtype { (SLIT(""), $1, $3) }
637 -- if the entity string is missing, it defaults to the empty string;
638 -- the meaning of an empty entity string depends on the calling
642 ext_name :: { Maybe CLabelString }
644 | STRING STRING { Just $2 } -- Ignore "module name" for now
645 | {- empty -} { Nothing }
648 -----------------------------------------------------------------------------
651 opt_sig :: { Maybe RdrNameHsType }
652 : {- empty -} { Nothing }
653 | '::' sigtype { Just $2 }
655 opt_asig :: { Maybe RdrNameHsType }
656 : {- empty -} { Nothing }
657 | '::' atype { Just $2 }
659 sigtypes :: { [RdrNameHsType] }
661 | sigtypes ',' sigtype { $3 : $1 }
663 sigtype :: { RdrNameHsType }
664 : ctype { mkHsForAllTy Nothing [] $1 }
666 sig_vars :: { [RdrName] }
667 : sig_vars ',' var { $3 : $1 }
670 -----------------------------------------------------------------------------
673 -- A ctype is a for-all type
674 ctype :: { RdrNameHsType }
675 : 'forall' tv_bndrs '.' ctype { mkHsForAllTy (Just $2) [] $4 }
676 | context '=>' type { mkHsForAllTy Nothing $1 $3 }
677 -- A type of form (context => type) is an *implicit* HsForAllTy
680 -- We parse a context as a btype so that we don't get reduce/reduce
681 -- errors in ctype. The basic problem is that
683 -- looks so much like a tuple type. We can't tell until we find the =>
684 context :: { RdrNameContext }
685 : btype {% checkContext $1 }
687 type :: { RdrNameHsType }
688 : gentype '->' type { HsFunTy $1 $3 }
689 | ipvar '::' type { mkHsIParamTy $1 $3 }
692 gentype :: { RdrNameHsType }
695 | atype tyconop atype { HsOpTy $1 $2 $3 }
697 btype :: { RdrNameHsType }
698 : btype atype { HsAppTy $1 $2 }
701 atype :: { RdrNameHsType }
702 : gtycon { HsTyVar $1 }
703 | tyvar { HsTyVar $1 }
704 | '(' type ',' comma_types1 ')' { HsTupleTy (mkHsTupCon tcName Boxed ($2:$4)) ($2:$4) }
705 | '(#' comma_types1 '#)' { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
706 | '[' type ']' { HsListTy $2 }
707 | '[:' type ':]' { HsPArrTy $2 }
708 | '(' ctype ')' { $2 }
709 | '(' ctype '::' kind ')' { HsKindSig $2 $4 }
711 | INTEGER { HsNumTy $1 }
713 -- An inst_type is what occurs in the head of an instance decl
714 -- e.g. (Foo a, Gaz b) => Wibble a b
715 -- It's kept as a single type, with a MonoDictTy at the right
716 -- hand corner, for convenience.
717 inst_type :: { RdrNameHsType }
718 : ctype {% checkInstType $1 }
720 comma_types0 :: { [RdrNameHsType] }
721 : comma_types1 { $1 }
724 comma_types1 :: { [RdrNameHsType] }
726 | type ',' comma_types1 { $1 : $3 }
728 atypes0 :: { [RdrNameHsType] }
732 atypes1 :: { [RdrNameHsType] }
734 | atype atypes1 { $1 : $2 }
736 tv_bndrs :: { [RdrNameHsTyVar] }
737 : tv_bndr tv_bndrs { $1 : $2 }
740 tv_bndr :: { RdrNameHsTyVar }
741 : tyvar { UserTyVar $1 }
742 | '(' tyvar '::' kind ')' { IfaceTyVar $2 $4 }
744 fds :: { [([RdrName], [RdrName])] }
746 | '|' fds1 { reverse $2 }
748 fds1 :: { [([RdrName], [RdrName])] }
749 : fds1 ',' fd { $3 : $1 }
752 fd :: { ([RdrName], [RdrName]) }
753 : varids0 '->' varids0 { (reverse $1, reverse $3) }
755 varids0 :: { [RdrName] }
757 | varids0 tyvar { $2 : $1 }
759 -----------------------------------------------------------------------------
764 | akind '->' kind { mkArrowKind $1 $3 }
767 : '*' { liftedTypeKind }
768 | '(' kind ')' { $2 }
771 -----------------------------------------------------------------------------
772 -- Datatype declarations
774 newconstr :: { RdrNameConDecl }
775 : srcloc conid atype { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
776 | srcloc conid '{' var '::' ctype '}'
777 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
779 constrs :: { [RdrNameConDecl] }
780 : {- empty; a GHC extension -} { [] }
781 | '=' constrs1 { $2 }
783 constrs1 :: { [RdrNameConDecl] }
784 : constrs1 '|' constr { $3 : $1 }
787 constr :: { RdrNameConDecl }
788 : srcloc forall context '=>' constr_stuff
789 { mkConDecl (fst $5) $2 $3 (snd $5) $1 }
790 | srcloc forall constr_stuff
791 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
793 forall :: { [RdrNameHsTyVar] }
794 : 'forall' tv_bndrs '.' { $2 }
797 constr_stuff :: { (RdrName, RdrNameConDetails) }
798 : btype {% mkVanillaCon $1 [] }
799 | btype '!' atype satypes {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
800 | gtycon '{' '}' {% mkRecCon $1 [] }
801 | gtycon '{' fielddecls '}' {% mkRecCon $1 $3 }
802 | sbtype conop sbtype { ($2, InfixCon $1 $3) }
804 satypes :: { [RdrNameBangType] }
805 : atype satypes { unbangedType $1 : $2 }
806 | '!' atype satypes { BangType MarkedUserStrict $2 : $3 }
809 sbtype :: { RdrNameBangType }
810 : btype { unbangedType $1 }
811 | '!' atype { BangType MarkedUserStrict $2 }
813 fielddecls :: { [([RdrName],RdrNameBangType)] }
814 : fielddecl ',' fielddecls { $1 : $3 }
817 fielddecl :: { ([RdrName],RdrNameBangType) }
818 : sig_vars '::' stype { (reverse $1, $3) }
820 stype :: { RdrNameBangType }
821 : ctype { unbangedType $1 }
822 | '!' atype { BangType MarkedUserStrict $2 }
824 deriving :: { Maybe RdrNameContext }
825 : {- empty -} { Nothing }
826 | 'deriving' context { Just $2 }
827 -- Glasgow extension: allow partial
828 -- applications in derivings
830 -----------------------------------------------------------------------------
833 {- There's an awkward overlap with a type signature. Consider
834 f :: Int -> Int = ...rhs...
835 Then we can't tell whether it's a type signature or a value
836 definition with a result signature until we see the '='.
837 So we have to inline enough to postpone reductions until we know.
841 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
842 instead of qvar, we get another shift/reduce-conflict. Consider the
845 { (^^) :: Int->Int ; } Type signature; only var allowed
847 { (^^) :: Int->Int = ... ; } Value defn with result signature;
848 qvar allowed (because of instance decls)
850 We can't tell whether to reduce var to qvar until after we've read the signatures.
853 valdef :: { RdrBinding }
854 : infixexp srcloc opt_sig rhs {% (checkValDef $1 $3 $4 $2) }
855 | infixexp srcloc '::' sigtype {% (checkValSig $1 $4 $2) }
856 | var ',' sig_vars srcloc '::' sigtype { foldr1 RdrAndBindings
857 [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
861 rhs :: { RdrNameGRHSs }
862 : '=' srcloc exp wherebinds { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
863 | gdrhs wherebinds { GRHSs (reverse $1) $2 placeHolderType }
865 gdrhs :: { [RdrNameGRHS] }
866 : gdrhs gdrh { $2 : $1 }
869 gdrh :: { RdrNameGRHS }
870 : '|' srcloc quals '=' exp { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
872 -----------------------------------------------------------------------------
875 exp :: { RdrNameHsExpr }
876 : infixexp '::' sigtype { (ExprWithTySig $1 $3) }
877 | infixexp 'with' dbinding { HsWith $1 $3 }
880 infixexp :: { RdrNameHsExpr }
882 | infixexp qop exp10 { (OpApp $1 (HsVar $2)
883 (panic "fixity") $3 )}
885 exp10 :: { RdrNameHsExpr }
886 : '\\' srcloc aexp aexps opt_asig '->' srcloc exp
887 {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps ->
888 returnP (HsLam (Match ps $5
889 (GRHSs (unguardedRHS $8 $7)
890 EmptyBinds placeHolderType))) }
891 | 'let' declbinds 'in' exp { HsLet $2 $4 }
892 | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
893 | 'case' srcloc exp 'of' altslist { HsCase $3 $5 $2 }
894 | '-' fexp { mkHsNegApp $2 }
895 | srcloc 'do' stmtlist {% checkDo $3 `thenP` \ stmts ->
896 returnP (HsDo DoExpr stmts $1) }
898 | '_ccall_' ccallid aexps0 { HsCCall $2 $3 PlayRisky False placeHolderType }
899 | '_ccall_GC_' ccallid aexps0 { HsCCall $2 $3 PlaySafe False placeHolderType }
900 | '_casm_' CLITLIT aexps0 { HsCCall $2 $3 PlayRisky True placeHolderType }
901 | '_casm_GC_' CLITLIT aexps0 { HsCCall $2 $3 PlaySafe True placeHolderType }
903 | scc_annot exp { if opt_SccProfilingOn
909 scc_annot :: { FAST_STRING }
910 : '_scc_' STRING { $2 }
911 | '{-# SCC' STRING '#-}' { $2 }
913 ccallid :: { FAST_STRING }
917 fexp :: { RdrNameHsExpr }
918 : fexp aexp { (HsApp $1 $2) }
921 aexps0 :: { [RdrNameHsExpr] }
922 : aexps { reverse $1 }
924 aexps :: { [RdrNameHsExpr] }
925 : aexps aexp { $2 : $1 }
928 aexp :: { RdrNameHsExpr }
929 : var_or_con '{|' gentype '|}' { (HsApp $1 (HsType $3)) }
930 | aexp '{' fbinds '}' {% (mkRecConstrOrUpdate $1
934 var_or_con :: { RdrNameHsExpr }
938 aexp1 :: { RdrNameHsExpr }
939 : ipvar { HsIPVar $1 }
941 | literal { HsLit $1 }
942 | INTEGER { HsOverLit (mkHsIntegral $1) }
943 | RATIONAL { HsOverLit (mkHsFractional $1) }
944 | '(' exp ')' { HsPar $2 }
945 | '(' exp ',' texps ')' { ExplicitTuple ($2 : reverse $4) Boxed}
946 | '(#' texps '#)' { ExplicitTuple (reverse $2) Unboxed }
947 | '[' list ']' { $2 }
948 | '[:' parr ':]' { $2 }
949 | '(' infixexp qop ')' { (SectionL $2 (HsVar $3)) }
950 | '(' qopm infixexp ')' { (SectionR $2 $3) }
951 | qvar '@' aexp { EAsPat $1 $3 }
953 | '~' aexp1 { ELazyPat $2 }
955 texps :: { [RdrNameHsExpr] }
956 : texps ',' exp { $3 : $1 }
960 -----------------------------------------------------------------------------
963 -- The rules below are little bit contorted to keep lexps left-recursive while
964 -- avoiding another shift/reduce-conflict.
966 list :: { RdrNameHsExpr }
967 : exp { ExplicitList placeHolderType [$1] }
968 | lexps { ExplicitList placeHolderType (reverse $1) }
969 | exp '..' { ArithSeqIn (From $1) }
970 | exp ',' exp '..' { ArithSeqIn (FromThen $1 $3) }
971 | exp '..' exp { ArithSeqIn (FromTo $1 $3) }
972 | exp ',' exp '..' exp { ArithSeqIn (FromThenTo $1 $3 $5) }
973 | exp srcloc pquals {% let { body [qs] = qs;
974 body qss = [ParStmt (map reverse qss)] }
976 returnP ( HsDo ListComp
977 (reverse (ResultStmt $1 $2 : body $3))
982 lexps :: { [RdrNameHsExpr] }
983 : lexps ',' exp { $3 : $1 }
984 | exp ',' exp { [$3,$1] }
986 -----------------------------------------------------------------------------
987 -- List Comprehensions
989 pquals :: { [[RdrNameStmt]] }
990 : pquals '|' quals { $3 : $1 }
993 quals :: { [RdrNameStmt] }
994 : quals ',' stmt { $3 : $1 }
997 -----------------------------------------------------------------------------
998 -- Parallel array expressions
1000 -- The rules below are little bit contorted; see the list case for details.
1001 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1002 -- Moreover, we allow explicit arrays with no element (represented by the nil
1003 -- constructor in the list case).
1005 parr :: { RdrNameHsExpr }
1006 : { ExplicitPArr placeHolderType [] }
1007 | exp { ExplicitPArr placeHolderType [$1] }
1008 | lexps { ExplicitPArr placeHolderType
1010 | exp '..' exp { PArrSeqIn (FromTo $1 $3) }
1011 | exp ',' exp '..' exp { PArrSeqIn (FromThenTo $1 $3 $5) }
1012 | exp srcloc pquals {% let {
1019 (reverse (ResultStmt $1 $2
1024 -- We are reusing `lexps' and `pquals' from the list case.
1026 -----------------------------------------------------------------------------
1027 -- Case alternatives
1029 altslist :: { [RdrNameMatch] }
1030 : '{' alts '}' { reverse $2 }
1031 | layout_on alts close { reverse $2 }
1033 alts :: { [RdrNameMatch] }
1037 alts1 :: { [RdrNameMatch] }
1038 : alts1 ';' alt { $3 : $1 }
1042 alt :: { RdrNameMatch }
1043 : srcloc infixexp opt_sig ralt wherebinds
1044 {% (checkPattern $1 $2 `thenP` \p ->
1045 returnP (Match [p] $3
1046 (GRHSs $4 $5 placeHolderType)) )}
1048 ralt :: { [RdrNameGRHS] }
1049 : '->' srcloc exp { [GRHS [ResultStmt $3 $2] $2] }
1050 | gdpats { reverse $1 }
1052 gdpats :: { [RdrNameGRHS] }
1053 : gdpats gdpat { $2 : $1 }
1056 gdpat :: { RdrNameGRHS }
1057 : srcloc '|' quals '->' exp { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
1059 -----------------------------------------------------------------------------
1060 -- Statement sequences
1062 stmtlist :: { [RdrNameStmt] }
1063 : '{' stmts '}' { $2 }
1064 | layout_on_for_do stmts close { $2 }
1066 -- do { ;; s ; s ; ; s ;; }
1067 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1068 -- here, because we need too much lookahead if we see do { e ; }
1069 -- So we use ExprStmts throughout, and switch the last one over
1070 -- in ParseUtils.checkDo instead
1071 stmts :: { [RdrNameStmt] }
1072 : stmt stmts_help { $1 : $2 }
1074 | {- empty -} { [] }
1076 stmts_help :: { [RdrNameStmt] }
1078 | {- empty -} { [] }
1080 -- For typing stmts at the GHCi prompt, where
1081 -- the input may consist of just comments.
1082 maybe_stmt :: { Maybe RdrNameStmt }
1084 | {- nothing -} { Nothing }
1086 stmt :: { RdrNameStmt }
1087 : srcloc infixexp '<-' exp {% checkPattern $1 $2 `thenP` \p ->
1088 returnP (BindStmt p $4 $1) }
1089 | srcloc exp { ExprStmt $2 placeHolderType $1 }
1090 | srcloc 'let' declbinds { LetStmt $3 }
1092 -----------------------------------------------------------------------------
1093 -- Record Field Update/Construction
1095 fbinds :: { RdrNameHsRecordBinds }
1096 : fbinds ',' fbind { $3 : $1 }
1099 | {- empty -} { [] }
1101 fbind :: { (RdrName, RdrNameHsExpr, Bool) }
1102 : qvar '=' exp { ($1,$3,False) }
1104 -----------------------------------------------------------------------------
1105 -- Implicit Parameter Bindings
1107 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1108 : '{' dbinds '}' { $2 }
1109 | layout_on dbinds close { $2 }
1111 dbinds :: { [(IPName RdrName, RdrNameHsExpr)] }
1112 : dbinds ';' dbind { $3 : $1 }
1115 | {- empty -} { [] }
1117 dbind :: { (IPName RdrName, RdrNameHsExpr) }
1118 dbind : ipvar '=' exp { ($1, $3) }
1120 -----------------------------------------------------------------------------
1121 -- Variables, Constructors and Operators.
1123 identifier :: { RdrName }
1128 depreclist :: { [RdrName] }
1129 depreclist : deprec_var { [$1] }
1130 | deprec_var ',' depreclist { $1 : $3 }
1132 deprec_var :: { RdrName }
1133 deprec_var : var { $1 }
1136 gtycon :: { RdrName }
1138 | '(' qtyconop ')' { $2 }
1139 | '(' ')' { unitTyCon_RDR }
1140 | '(' '->' ')' { funTyCon_RDR }
1141 | '[' ']' { listTyCon_RDR }
1142 | '[:' ':]' { parrTyCon_RDR }
1143 | '(' commas ')' { tupleTyCon_RDR $2 }
1145 gcon :: { RdrName } -- Data constructor namespace
1146 : '(' ')' { unitCon_RDR }
1147 | '[' ']' { nilCon_RDR }
1148 | '(' commas ')' { tupleCon_RDR $2 }
1150 -- the case of '[:' ':]' is part of the production `parr'
1154 | '(' varsym ')' { $2 }
1158 | '(' varsym ')' { $2 }
1159 | '(' qvarsym1 ')' { $2 }
1160 -- We've inlined qvarsym here so that the decision about
1161 -- whether it's a qvar or a var can be postponed until
1162 -- *after* we see the close paren.
1164 ipvar :: { IPName RdrName }
1165 : IPDUPVARID { Dupable (mkUnqual varName $1) }
1166 | IPSPLITVARID { Linear (mkUnqual varName $1) }
1170 | '(' qconsym ')' { $2 }
1172 varop :: { RdrName }
1174 | '`' varid '`' { $2 }
1176 qvarop :: { RdrName }
1178 | '`' qvarid '`' { $2 }
1180 qvaropm :: { RdrName }
1181 : qvarsym_no_minus { $1 }
1182 | '`' qvarid '`' { $2 }
1184 conop :: { RdrName }
1186 | '`' conid '`' { $2 }
1188 qconop :: { RdrName }
1190 | '`' qconid '`' { $2 }
1192 -----------------------------------------------------------------------------
1195 op :: { RdrName } -- used in infix decls
1199 qop :: { RdrName {-HsExpr-} } -- used in sections
1203 qopm :: { RdrNameHsExpr } -- used in sections
1204 : qvaropm { HsVar $1 }
1205 | qconop { HsVar $1 }
1207 -----------------------------------------------------------------------------
1210 qvarid :: { RdrName }
1212 | QVARID { mkQual varName $1 }
1214 varid :: { RdrName }
1215 : varid_no_unsafe { $1 }
1216 | 'unsafe' { mkUnqual varName SLIT("unsafe") }
1218 varid_no_unsafe :: { RdrName }
1219 : VARID { mkUnqual varName $1 }
1220 | special_id { mkUnqual varName $1 }
1221 | 'forall' { mkUnqual varName SLIT("forall") }
1223 tyvar :: { RdrName }
1224 : VARID { mkUnqual tvName $1 }
1225 | special_id { mkUnqual tvName $1 }
1226 | 'unsafe' { mkUnqual tvName SLIT("unsafe") }
1228 -- These special_ids are treated as keywords in various places,
1229 -- but as ordinary ids elsewhere. A special_id collects all thsee
1230 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1231 special_id :: { UserFS }
1233 : 'as' { SLIT("as") }
1234 | 'qualified' { SLIT("qualified") }
1235 | 'hiding' { SLIT("hiding") }
1236 | 'export' { SLIT("export") }
1237 | 'label' { SLIT("label") }
1238 | 'dynamic' { SLIT("dynamic") }
1239 | 'stdcall' { SLIT("stdcall") }
1240 | 'ccall' { SLIT("ccall") }
1242 -----------------------------------------------------------------------------
1245 qconid :: { RdrName } -- Qualified or unqualifiedb
1247 | QCONID { mkQual dataName $1 }
1249 conid :: { RdrName }
1250 : CONID { mkUnqual dataName $1 }
1252 -----------------------------------------------------------------------------
1255 qconsym :: { RdrName } -- Qualified or unqualifiedb
1257 | QCONSYM { mkQual dataName $1 }
1259 consym :: { RdrName }
1260 : CONSYM { mkUnqual dataName $1 }
1262 -----------------------------------------------------------------------------
1265 qvarsym :: { RdrName }
1269 qvarsym_no_minus :: { RdrName }
1270 : varsym_no_minus { $1 }
1273 qvarsym1 :: { RdrName }
1274 qvarsym1 : QVARSYM { mkQual varName $1 }
1276 varsym :: { RdrName }
1277 : varsym_no_minus { $1 }
1278 | '-' { mkUnqual varName SLIT("-") }
1280 varsym_no_minus :: { RdrName } -- varsym not including '-'
1281 : VARSYM { mkUnqual varName $1 }
1282 | special_sym { mkUnqual varName $1 }
1285 -- See comments with special_id
1286 special_sym :: { UserFS }
1287 special_sym : '!' { SLIT("!") }
1291 -----------------------------------------------------------------------------
1294 literal :: { HsLit }
1295 : CHAR { HsChar $1 }
1296 | STRING { HsString $1 }
1297 | PRIMINTEGER { HsIntPrim $1 }
1298 | PRIMCHAR { HsCharPrim $1 }
1299 | PRIMSTRING { HsStringPrim $1 }
1300 | PRIMFLOAT { HsFloatPrim $1 }
1301 | PRIMDOUBLE { HsDoublePrim $1 }
1302 | CLITLIT { HsLitLit $1 placeHolderType }
1304 srcloc :: { SrcLoc } : {% getSrcLocP }
1306 -----------------------------------------------------------------------------
1310 : vccurly { () } -- context popped in lexer.
1311 | error {% popContext }
1313 layout_on :: { () } : {% layoutOn True{-strict-} }
1314 layout_on_for_do :: { () } : {% layoutOn False }
1316 -----------------------------------------------------------------------------
1317 -- Miscellaneous (mostly renamings)
1319 modid :: { ModuleName }
1320 : CONID { mkModuleNameFS $1 }
1321 | QCONID { mkModuleNameFS
1323 (unpackFS (fst $1) ++
1324 '.':unpackFS (snd $1)))
1327 tycon :: { RdrName }
1328 : CONID { mkUnqual tcClsName $1 }
1330 tyconop :: { RdrName }
1331 : CONSYM { mkUnqual tcClsName $1 }
1333 qtycon :: { RdrName } -- Qualified or unqualified
1334 : QCONID { mkQual tcClsName $1 }
1337 qtyconop :: { RdrName } -- Qualified or unqualified
1338 : QCONSYM { mkQual tcClsName $1 }
1342 : commas ',' { $1 + 1 }
1345 -----------------------------------------------------------------------------
1349 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)