cbc0a5bff62f2a1e058d18b646c9c078fc002514
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-                                                              -*-haskell-*-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.90 2002/02/15 22:13:33 sof Exp $
4
5 Haskell grammar.
6
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
9 -}
10
11 {
12 module Parser ( parseModule, parseStmt, parseIdentifier ) where
13
14 import HsSyn
15 import HsTypes          ( mkHsTupCon )
16
17 import RdrHsSyn
18 import Lex
19 import ParseUtil
20 import RdrName
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,
26                         )
27 import OccName          ( UserFS, varName, tcName, dataName, tcClsName, tvName )
28 import TyCon            ( DataConDetails(..) )
29 import SrcLoc           ( SrcLoc )
30 import Module
31 import CmdLineOpts      ( opt_SccProfilingOn )
32 import Type             ( Kind, mkArrowKind, liftedTypeKind )
33 import BasicTypes       ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
34                           NewOrData(..), StrictnessMark(..), Activation(..) )
35 import Panic
36
37 import GlaExts
38 import CStrings         ( CLabelString )
39 import FastString
40 import Maybes           ( orElse )
41 import Outputable
42
43 #include "HsVersions.h"
44 }
45
46 {-
47 -----------------------------------------------------------------------------
48 Conflicts: 21 shift/reduce, -=chak[4Feb2]
49
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)'
57
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)
61
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'.
69
70 1 for ambiguity in 'x @ Rec{..}'.  
71         Only sensible parse is 'x @ (Rec{..})', which is what resolving
72         to shift gives us.
73
74 6 for conflicts between `fdecl' and `fdeclDEPRECATED', which are resolved
75   correctly, and moreover, should go away when `fdeclDEPRECATED' is removed.
76
77 -----------------------------------------------------------------------------
78 -}
79
80 %token
81  '_'            { ITunderscore }                -- Haskell keywords
82  'as'           { ITas }
83  'case'         { ITcase }      
84  'class'        { ITclass } 
85  'data'         { ITdata } 
86  'default'      { ITdefault }
87  'deriving'     { ITderiving }
88  'do'           { ITdo }
89  'else'         { ITelse }
90  'hiding'       { IThiding }
91  'if'           { ITif }
92  'import'       { ITimport }
93  'in'           { ITin }
94  'infix'        { ITinfix }
95  'infixl'       { ITinfixl }
96  'infixr'       { ITinfixr }
97  'instance'     { ITinstance }
98  'let'          { ITlet }
99  'module'       { ITmodule }
100  'newtype'      { ITnewtype }
101  'of'           { ITof }
102  'qualified'    { ITqualified }
103  'then'         { ITthen }
104  'type'         { ITtype }
105  'where'        { ITwhere }
106  '_scc_'        { ITscc }             -- ToDo: remove
107
108  'forall'       { ITforall }                    -- GHC extension keywords
109  'foreign'      { ITforeign }
110  'export'       { ITexport }
111  'label'        { ITlabel } 
112  'dynamic'      { ITdynamic }
113  'safe'         { ITsafe }
114  'threadsafe'   { ITthreadsafe }
115  'unsafe'       { ITunsafe }
116  'with'         { ITwith }
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) }
124
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 }
133
134 {-
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 }
144  '__bot'        { ITbottom }
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 $$ }
153  '__scc'        { IT__scc }
154  '__sccC'       { ITsccAllCafs }
155
156  '__A'          { ITarity }
157  '__P'          { ITspecialise }
158  '__C'          { ITnocaf }
159  '__U'          { ITunfold }
160  '__S'          { ITstrict $$ }
161  '__M'          { ITcprinfo $$ }
162 -}
163
164  '..'           { ITdotdot }                    -- reserved symbols
165  '::'           { ITdcolon }
166  '='            { ITequal }
167  '\\'           { ITlam }
168  '|'            { ITvbar }
169  '<-'           { ITlarrow }
170  '->'           { ITrarrow }
171  '@'            { ITat }
172  '~'            { ITtilde }
173  '=>'           { ITdarrow }
174  '-'            { ITminus }
175  '!'            { ITbang }
176  '*'            { ITstar }
177  '.'            { ITdot }
178
179  '{'            { ITocurly }                    -- special symbols
180  '}'            { ITccurly }
181  '{|'           { ITocurlybar }
182  '|}'           { ITccurlybar }
183  vccurly        { ITvccurly } -- virtual close curly (from layout)
184  '['            { ITobrack }
185  ']'            { ITcbrack }
186  '[:'           { ITopabrack }
187  ':]'           { ITcpabrack }
188  '('            { IToparen }
189  ')'            { ITcparen }
190  '(#'           { IToubxparen }
191  '#)'           { ITcubxparen }
192  ';'            { ITsemi }
193  ','            { ITcomma }
194  '`'            { ITbackquote }
195
196  VARID          { ITvarid    $$ }               -- identifiers
197  CONID          { ITconid    $$ }
198  VARSYM         { ITvarsym   $$ }
199  CONSYM         { ITconsym   $$ }
200  QVARID         { ITqvarid   $$ }
201  QCONID         { ITqconid   $$ }
202  QVARSYM        { ITqvarsym  $$ }
203  QCONSYM        { ITqconsym  $$ }
204
205  IPDUPVARID     { ITdupipvarid   $$ }           -- GHC extension
206  IPSPLITVARID   { ITsplitipvarid $$ }           -- GHC extension
207
208  CHAR           { ITchar     $$ }
209  STRING         { ITstring   $$ }
210  INTEGER        { ITinteger  $$ }
211  RATIONAL       { ITrational $$ }
212
213  PRIMCHAR       { ITprimchar   $$ }
214  PRIMSTRING     { ITprimstring $$ }
215  PRIMINTEGER    { ITprimint    $$ }
216  PRIMFLOAT      { ITprimfloat  $$ }
217  PRIMDOUBLE     { ITprimdouble $$ }
218  CLITLIT        { ITlitlit     $$ }
219
220 %monad { P } { thenP } { returnP }
221 %lexer { lexer } { ITeof }
222 %name parseModule module
223 %name parseStmt   maybe_stmt
224 %name parseIdentifier  identifier
225 %tokentype { Token }
226 %%
227
228 -----------------------------------------------------------------------------
229 -- Module Header
230
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. :-)
237
238 module  :: { RdrNameHsModule }
239         : srcloc 'module' modid maybemoddeprec maybeexports 'where' body 
240                 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
241         | srcloc body
242                 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
243
244 maybemoddeprec :: { Maybe DeprecTxt }
245         : '{-# DEPRECATED' STRING '#-}'         { Just $2 }
246         |  {- empty -}                          { Nothing }
247
248 body    :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
249         :  '{'            top '}'               { $2 }
250         |      layout_on  top close             { $2 }
251
252 top     :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
253         : importdecls                           { (reverse $1,[]) }
254         | importdecls ';' cvtopdecls            { (reverse $1,$3) }
255         | cvtopdecls                            { ([],$1) }
256
257 cvtopdecls :: { [RdrNameHsDecl] }
258         : topdecls                              { cvTopDecls (groupBindings $1)}
259
260 -----------------------------------------------------------------------------
261 -- The Export List
262
263 maybeexports :: { Maybe [RdrNameIE] }
264         :  '(' exportlist ')'                   { Just $2 }
265         |  {- empty -}                          { Nothing }
266
267 exportlist :: { [RdrNameIE] }
268         :  exportlist ',' export                { $3 : $1 }
269         |  exportlist ','                       { $1 }
270         |  export                               { [$1]  }
271         |  {- empty -}                          { [] }
272
273    -- GHC extension: we allow things like [] and (,,,) to be exported
274 export  :: { RdrNameIE }
275         :  qvar                                 { IEVar $1 }
276         |  gtycon                               { IEThingAbs $1 }
277         |  gtycon '(' '..' ')'                  { IEThingAll $1 }
278         |  gtycon '(' ')'                       { IEThingWith $1 [] }
279         |  gtycon '(' qcnames ')'               { IEThingWith $1 (reverse $3) }
280         |  'module' modid                       { IEModuleContents $2 }
281
282 qcnames :: { [RdrName] }
283         :  qcnames ',' qcname                   { $3 : $1 }
284         |  qcname                               { [$1]  }
285
286 qcname  :: { RdrName }
287         :  qvar                                 { $1 }
288         |  gcon                                 { $1 }
289
290 -----------------------------------------------------------------------------
291 -- Import Declarations
292
293 -- import decls can be *empty*, or even just a string of semicolons
294 -- whereas topdecls must contain at least one topdecl.
295
296 importdecls :: { [RdrNameImportDecl] }
297         : importdecls ';' importdecl            { $3 : $1 }
298         | importdecls ';'                       { $1 }
299         | importdecl                            { [ $1 ] }
300         | {- empty -}                           { [] }
301
302 importdecl :: { RdrNameImportDecl }
303         : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec 
304                 { ImportDecl $5 $3 $4 $6 $7 $2 }
305
306 maybe_src :: { WhereFrom }
307         : '{-# SOURCE' '#-}'                    { ImportByUserSource }
308         | {- empty -}                           { ImportByUser }
309
310 optqualified :: { Bool }
311         : 'qualified'                           { True  }
312         | {- empty -}                           { False }
313
314 maybeas :: { Maybe ModuleName }
315         : 'as' modid                            { Just $2 }
316         | {- empty -}                           { Nothing }
317
318 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
319         : impspec                               { Just $1 }
320         | {- empty -}                           { Nothing }
321
322 impspec :: { (Bool, [RdrNameIE]) }
323         :  '(' exportlist ')'                   { (False, reverse $2) }
324         |  'hiding' '(' exportlist ')'          { (True,  reverse $3) }
325
326 -----------------------------------------------------------------------------
327 -- Fixity Declarations
328
329 prec    :: { Int }
330         : {- empty -}                           { 9 }
331         | INTEGER                               {%  checkPrec $1 `thenP_`
332                                                     returnP (fromInteger $1) }
333
334 infix   :: { FixityDirection }
335         : 'infix'                               { InfixN  }
336         | 'infixl'                              { InfixL  }
337         | 'infixr'                              { InfixR }
338
339 ops     :: { [RdrName] }
340         : ops ',' op                            { $3 : $1 }
341         | op                                    { [$1] }
342
343 -----------------------------------------------------------------------------
344 -- Top-Level Declarations
345
346 topdecls :: { [RdrBinding] }
347         : topdecls ';' topdecl          { ($3 : $1) }
348         | topdecls ';'                  { $1 }
349         | topdecl                       { [$1] }
350
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)) }
358
359
360         | srcloc 'data' tycl_hdr constrs deriving
361                 {% returnP (RdrHsDecl (TyClD
362                       (mkTyData DataType $3 (DataCons (reverse $4)) $5 $1))) }
363
364         | srcloc 'newtype' tycl_hdr '=' newconstr deriving
365                 {% returnP (RdrHsDecl (TyClD
366                       (mkTyData NewType $3 (DataCons [$5]) $6 $1))) }
367
368         | srcloc 'class' tycl_hdr fds where
369                 {% let 
370                         (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5) 
371                    in
372                    returnP (RdrHsDecl (TyClD
373                       (mkClassDecl $3 $4 sigs (Just binds) $1))) }
374
375         | srcloc 'instance' inst_type where
376                 { let (binds,sigs) 
377                         = cvMonoBindsAndSigs cvInstDeclSig 
378                                 (groupBindings $4)
379                   in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
380
381         | srcloc 'default' '(' comma_types0 ')'         { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
382         | 'foreign' fdecl                               { RdrHsDecl $2 }
383         | '{-# DEPRECATED' deprecations '#-}'           { $2 }
384         | '{-# RULES' rules '#-}'                       { $2 }
385         | decl                                          { $1 }
386
387 -- tycl_hdr parses the header of a type or class decl,
388 -- which takes the form
389 --      T a b
390 --      Eq a => T a
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
398           --          error 
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.
406                 --
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 
413
414 decls   :: { [RdrBinding] }
415         : decls ';' decl                { $3 : $1 }
416         | decls ';'                     { $1 }
417         | decl                          { [$1] }
418         | {- empty -}                   { [] }
419
420 decl    :: { RdrBinding }
421         : fixdecl                       { $1 }
422         | valdef                        { $1 }
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) }
430
431 wherebinds :: { RdrNameHsBinds }
432         : where                 { cvBinds cvValSig (groupBindings $1) }
433
434 where   :: { [RdrBinding] }
435         : 'where' decllist              { $2 }
436         | {- empty -}                   { [] }
437
438 declbinds :: { RdrNameHsBinds }
439         : decllist                      { cvBinds cvValSig (groupBindings $1) }
440
441 decllist :: { [RdrBinding] }
442         : '{'            decls '}'      { $2 }
443         |     layout_on  decls close    { $2 }
444
445 fixdecl :: { RdrBinding }
446         : srcloc infix prec ops         { foldr1 RdrAndBindings
447                                             [ RdrSig (FixSig (FixitySig n 
448                                                             (Fixity $3 $2) $1))
449                                             | n <- $4 ] }
450
451 -----------------------------------------------------------------------------
452 -- Transformation Rules
453
454 rules   :: { RdrBinding }
455         :  rules ';' rule                       { $1 `RdrAndBindings` $3 }
456         |  rules ';'                            { $1 }
457         |  rule                                 { $1 }
458         |  {- empty -}                          { RdrNullBind }
459
460 rule    :: { RdrBinding }
461         : STRING activation rule_forall infixexp '=' srcloc exp
462              { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
463
464 activation :: { Activation }           -- Omitted means AlwaysActive
465         : {- empty -}                           { AlwaysActive }
466         | explicit_activation                   { $1 }
467
468 inverse_activation :: { Activation }   -- Omitted means NeverActive
469         : {- empty -}                           { NeverActive }
470         | explicit_activation                   { $1 }
471
472 explicit_activation :: { Activation }  -- In brackets
473         : '[' INTEGER ']'                       { ActiveAfter  (fromInteger $2) }
474         | '[' '~' INTEGER ']'                   { ActiveBefore (fromInteger $3) }
475
476 rule_forall :: { [RdrNameRuleBndr] }
477         : 'forall' rule_var_list '.'            { $2 }
478         | {- empty -}                           { [] }
479
480 rule_var_list :: { [RdrNameRuleBndr] }
481         : rule_var                              { [$1] }
482         | rule_var rule_var_list                { $1 : $2 }
483
484 rule_var :: { RdrNameRuleBndr }
485         : varid                                 { RuleBndr $1 }
486         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
487
488 -----------------------------------------------------------------------------
489 -- Deprecations
490
491 deprecations :: { RdrBinding }
492         : deprecations ';' deprecation          { $1 `RdrAndBindings` $3 }
493         | deprecations ';'                      { $1 }
494         | deprecation                           { $1 }
495         | {- empty -}                           { RdrNullBind }
496
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 ] }
502
503
504 -----------------------------------------------------------------------------
505 -- Foreign import and export declarations
506
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
509 --
510 -- * a flag indicates whether pre-standard declarations have been used and
511 --   triggers a deprecation warning further down the road
512 --
513 -- NB: The first two rules could be combined into one by replacing `safety1'
514 --     with `safety'.  However, the combined rule conflicts with the
515 --     DEPRECATED rules.
516 --
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 }
524
525 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
526 fdecl1DEPRECATED 
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))) }
531
532   ----------- DEPRECATED ccall/stdcall decls ------------
533   --
534   -- NB: This business with the case expression below may seem overly
535   --     complicated, but it is necessary to avoid some conflicts.
536
537     -- DEPRECATED variant #1: lack of a calling convention specification
538     --                        (import) 
539   | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
540     { let
541         target = StaticTarget ($2 `orElse` mkExtName $4)
542       in
543       ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_ 
544                                    (CFunction target)) }
545
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
549     {% case $2 of
550          DNCall      -> parseError "Illegal format of .NET foreign import"
551          CCall cconv -> returnP $
552            let
553              imp = CFunction (StaticTarget $4)
554            in
555            ForeignImport $6 $8 (CImport cconv $5 _NIL_ _NIL_ imp) }
556
557     -- DEPRECATED variant #3: `unsafe' after entity
558   | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
559     {% case $2 of
560          DNCall      -> parseError "Illegal format of .NET foreign import"
561          CCall cconv -> returnP $
562            let
563              imp = CFunction (StaticTarget $3)
564            in
565            ForeignImport $5 $7 (CImport cconv PlayRisky _NIL_ _NIL_ imp) }
566
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)) }
572
573     -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
574   | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
575     {% case $2 of
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)) }
580
581     -- DEPRECATED variant #6: lack of a calling convention specification
582     --                        (export) 
583   | 'export' {-no callconv-} ext_name varid '::' sigtype
584     { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3) 
585                                    defaultCCallConv)) }
586
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
590     {% case $2 of
591          DNCall      -> parseError "Illegal format of .NET foreign import"
592          CCall cconv -> returnP $
593            ForeignExport $5 $7 
594                          (CExport (CExportStatic $4 cconv)) }
595
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_ 
600                                    CWrapper) }
601
602     -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
603   | 'export' callconv 'dynamic' varid '::' sigtype
604     {% case $2 of
605          DNCall      -> parseError "Illegal format of .NET foreign import"
606          CCall cconv -> returnP $
607            ForeignImport $4 $6 (CImport cconv (PlaySafe False) _NIL_ _NIL_ CWrapper) }
608
609   ----------- DEPRECATED .NET decls ------------
610   -- NB: removed the .NET call declaration, as it is entirely subsumed
611   --     by the new standard FFI declarations
612
613 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
614 fdecl2DEPRECATED 
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
619
620
621 callconv :: { CallConv }
622           : 'stdcall'                   { CCall  StdCallConv }
623           | 'ccall'                     { CCall  CCallConv   }
624           | 'dotnet'                    { DNCall             }
625
626 safety :: { Safety }
627         : 'unsafe'                      { PlayRisky }
628         | 'safe'                        { PlaySafe False }
629         | 'threadsafe'                  { PlaySafe True  }
630         | {- empty -}                   { PlaySafe False }
631
632 safety1 :: { Safety }
633         : 'unsafe'                      { PlayRisky }
634         | 'safe'                        { PlaySafe  False }
635         | 'threadsafe'                  { PlaySafe  True }
636           -- only needed to avoid conflicts with the DEPRECATED rules
637
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
643          -- convention
644
645 -- DEPRECATED syntax
646 ext_name :: { Maybe CLabelString }
647         : STRING                { Just $1 }
648         | STRING STRING         { Just $2 }     -- Ignore "module name" for now
649         | {- empty -}           { Nothing }
650
651
652 -----------------------------------------------------------------------------
653 -- Type signatures
654
655 opt_sig :: { Maybe RdrNameHsType }
656         : {- empty -}                   { Nothing }
657         | '::' sigtype                  { Just $2 }
658
659 opt_asig :: { Maybe RdrNameHsType }
660         : {- empty -}                   { Nothing }
661         | '::' atype                    { Just $2 }
662
663 sigtypes :: { [RdrNameHsType] }
664         : sigtype                       { [ $1 ] }
665         | sigtypes ',' sigtype          { $3 : $1 }
666
667 sigtype :: { RdrNameHsType }
668         : ctype                         { mkHsForAllTy Nothing [] $1 }
669
670 sig_vars :: { [RdrName] }
671          : sig_vars ',' var             { $3 : $1 }
672          | var                          { [ $1 ] }
673
674 -----------------------------------------------------------------------------
675 -- Types
676
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
682         | type                          { $1 }
683
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
686 --      (Eq a, Ord a)
687 -- looks so much like a tuple type.  We can't tell until we find the =>
688 context :: { RdrNameContext }
689         : btype                         {% checkContext $1 }
690
691 type :: { RdrNameHsType }
692         : gentype '->' type             { HsFunTy $1 $3 }
693         | ipvar '::' type               { mkHsIParamTy $1 $3 }
694         | gentype                       { $1 }
695
696 gentype :: { RdrNameHsType }
697         : btype                         { $1 }
698 -- Generics
699         | atype tyconop atype           { HsOpTy $1 $2 $3 }
700
701 btype :: { RdrNameHsType }
702         : btype atype                   { HsAppTy $1 $2 }
703         | atype                         { $1 }
704
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 }
714 -- Generics
715         | INTEGER                       { HsNumTy $1 }
716
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 }
723
724 comma_types0  :: { [RdrNameHsType] }
725         : comma_types1                  { $1 }
726         | {- empty -}                   { [] }
727
728 comma_types1    :: { [RdrNameHsType] }
729         : type                          { [$1] }
730         | type  ',' comma_types1        { $1 : $3 }
731
732 atypes0 :: { [RdrNameHsType] }
733         : atypes1                       { $1 }
734         | {- empty -}                   { [] }
735
736 atypes1 :: { [RdrNameHsType] }
737         : atype                         { [$1] }
738         | atype atypes1                 { $1 : $2 }
739
740 tv_bndrs :: { [RdrNameHsTyVar] }
741          : tv_bndr tv_bndrs             { $1 : $2 }
742          | {- empty -}                  { [] }
743
744 tv_bndr :: { RdrNameHsTyVar }
745         : tyvar                         { UserTyVar $1 }
746         | '(' tyvar '::' kind ')'       { IfaceTyVar $2 $4 }
747
748 fds :: { [([RdrName], [RdrName])] }
749         : {- empty -}                   { [] }
750         | '|' fds1                      { reverse $2 }
751
752 fds1 :: { [([RdrName], [RdrName])] }
753         : fds1 ',' fd                   { $3 : $1 }
754         | fd                            { [$1] }
755
756 fd :: { ([RdrName], [RdrName]) }
757         : varids0 '->' varids0          { (reverse $1, reverse $3) }
758
759 varids0 :: { [RdrName] }
760         : {- empty -}                   { [] }
761         | varids0 tyvar                 { $2 : $1 }
762
763 -----------------------------------------------------------------------------
764 -- Kinds
765
766 kind    :: { Kind }
767         : akind                 { $1 }
768         | akind '->' kind       { mkArrowKind $1 $3 }
769
770 akind   :: { Kind }
771         : '*'                   { liftedTypeKind }
772         | '(' kind ')'          { $2 }
773
774
775 -----------------------------------------------------------------------------
776 -- Datatype declarations
777
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 }
782
783 constrs :: { [RdrNameConDecl] }
784         : {- empty; a GHC extension -}  { [] }
785         | '=' constrs1                  { $2 }
786
787 constrs1 :: { [RdrNameConDecl] }
788         : constrs1 '|' constr           { $3 : $1 }
789         | constr                        { [$1] }
790
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 }
796
797 forall :: { [RdrNameHsTyVar] }
798         : 'forall' tv_bndrs '.'         { $2 }
799         | {- empty -}                   { [] }
800
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) }
807
808 satypes :: { [RdrNameBangType] }
809         : atype satypes                 { unbangedType $1 : $2 }
810         | '!' atype satypes             { BangType MarkedUserStrict $2 : $3 }
811         | {- empty -}                   { [] }
812
813 sbtype :: { RdrNameBangType }
814         : btype                         { unbangedType $1 }
815         | '!' atype                     { BangType MarkedUserStrict $2 }
816
817 fielddecls :: { [([RdrName],RdrNameBangType)] }
818         : fielddecl ',' fielddecls      { $1 : $3 }
819         | fielddecl                     { [$1] }
820
821 fielddecl :: { ([RdrName],RdrNameBangType) }
822         : sig_vars '::' stype           { (reverse $1, $3) }
823
824 stype :: { RdrNameBangType }
825         : ctype                         { unbangedType $1 }
826         | '!' atype                     { BangType MarkedUserStrict $2 }
827
828 deriving :: { Maybe RdrNameContext }
829         : {- empty -}                   { Nothing }
830         | 'deriving' context            { Just $2 }
831              -- Glasgow extension: allow partial 
832              -- applications in derivings
833
834 -----------------------------------------------------------------------------
835 -- Value definitions
836
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.
842 -}
843
844 {-
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
847   following programs:
848   
849      { (^^) :: Int->Int ; }          Type signature; only var allowed
850
851      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
852                                      qvar allowed (because of instance decls)
853   
854   We can't tell whether to reduce var to qvar until after we've read the signatures.
855 -}
856
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 ]
862                                                 }
863
864
865 rhs     :: { RdrNameGRHSs }
866         : '=' srcloc exp wherebinds     { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
867         | gdrhs wherebinds              { GRHSs (reverse $1) $2 placeHolderType }
868
869 gdrhs :: { [RdrNameGRHS] }
870         : gdrhs gdrh                    { $2 : $1 }
871         | gdrh                          { [$1] }
872
873 gdrh :: { RdrNameGRHS }
874         : '|' srcloc quals '=' exp      { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
875
876 -----------------------------------------------------------------------------
877 -- Expressions
878
879 exp   :: { RdrNameHsExpr }
880         : infixexp '::' sigtype         { (ExprWithTySig $1 $3) }
881         | infixexp 'with' dbinding      { HsWith $1 $3 }
882         | infixexp                      { $1 }
883
884 infixexp :: { RdrNameHsExpr }
885         : exp10                         { $1 }
886         | infixexp qop exp10            { (OpApp $1 (HsVar $2) 
887                                                 (panic "fixity") $3 )}
888
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) }
901
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 }
906
907         | scc_annot exp                         { if opt_SccProfilingOn
908                                                         then HsSCC $1 $2
909                                                         else HsPar $2 }
910
911         | fexp                                  { $1 }
912
913 scc_annot :: { FAST_STRING }
914         : '_scc_' STRING                        { $2 }
915         | '{-# SCC' STRING '#-}'                { $2 }
916
917 ccallid :: { FAST_STRING }
918         :  VARID                                { $1 }
919         |  CONID                                { $1 }
920
921 fexp    :: { RdrNameHsExpr }
922         : fexp aexp                             { (HsApp $1 $2) }
923         | aexp                                  { $1 }
924
925 aexps0  :: { [RdrNameHsExpr] }
926         : aexps                                 { reverse $1 }
927
928 aexps   :: { [RdrNameHsExpr] }
929         : aexps aexp                            { $2 : $1 }
930         | {- empty -}                           { [] }
931
932 aexp    :: { RdrNameHsExpr }
933         : var_or_con '{|' gentype '|}'          { (HsApp $1 (HsType $3)) }
934         | aexp '{' fbinds '}'                   {% (mkRecConstrOrUpdate $1 
935                                                         (reverse $3)) }
936         | aexp1                                 { $1 }
937
938 var_or_con :: { RdrNameHsExpr }
939         : qvar                          { HsVar $1 }
940         | gcon                          { HsVar $1 }
941
942 aexp1   :: { RdrNameHsExpr }
943         : ipvar                         { HsIPVar $1 }
944         | var_or_con                    { $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 }
956         | '_'                           { EWildPat }
957         | '~' aexp1                     { ELazyPat $2 }
958
959 texps :: { [RdrNameHsExpr] }
960         : texps ',' exp                 { $3 : $1 }
961         | exp                           { [$1] }
962
963
964 -----------------------------------------------------------------------------
965 -- List expressions
966
967 -- The rules below are little bit contorted to keep lexps left-recursive while
968 -- avoiding another shift/reduce-conflict.
969
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)] }
979                                            in
980                                            returnP ( HsDo ListComp
981                                                            (reverse (ResultStmt $1 $2 : body $3))
982                                                            $2
983                                                   )
984                                         }
985
986 lexps :: { [RdrNameHsExpr] }
987         : lexps ',' exp                 { $3 : $1 }
988         | exp ',' exp                   { [$3,$1] }
989
990 -----------------------------------------------------------------------------
991 -- List Comprehensions
992
993 pquals :: { [[RdrNameStmt]] }
994         : pquals '|' quals              { $3 : $1 }
995         | '|' quals                     { [$2] }
996
997 quals :: { [RdrNameStmt] }
998         : quals ',' stmt                { $3 : $1 }
999         | stmt                          { [$1] }
1000
1001 -----------------------------------------------------------------------------
1002 -- Parallel array expressions
1003
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).
1008
1009 parr :: { RdrNameHsExpr }
1010         :                               { ExplicitPArr placeHolderType [] }
1011         | exp                           { ExplicitPArr placeHolderType [$1] }
1012         | lexps                         { ExplicitPArr placeHolderType 
1013                                                        (reverse $1) }
1014         | exp '..' exp                  { PArrSeqIn (FromTo $1 $3) }
1015         | exp ',' exp '..' exp          { PArrSeqIn (FromThenTo $1 $3 $5) }
1016         | exp srcloc pquals             {% let {
1017                                              body [qs] = qs;
1018                                              body  qss = [ParStmt 
1019                                                            (map reverse qss)]}
1020                                            in
1021                                            returnP $ 
1022                                              HsDo PArrComp 
1023                                                   (reverse (ResultStmt $1 $2 
1024                                                             : body $3))
1025                                                   $2
1026                                         }
1027
1028 -- We are reusing `lexps' and `pquals' from the list case.
1029
1030 -----------------------------------------------------------------------------
1031 -- Case alternatives
1032
1033 altslist :: { [RdrNameMatch] }
1034         : '{'            alts '}'       { reverse $2 }
1035         |     layout_on  alts  close    { reverse $2 }
1036
1037 alts    :: { [RdrNameMatch] }
1038         : alts1                         { $1 }
1039         | ';' alts                      { $2 }
1040
1041 alts1   :: { [RdrNameMatch] }
1042         : alts1 ';' alt                 { $3 : $1 }
1043         | alts1 ';'                     { $1 }
1044         | alt                           { [$1] }
1045
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))  )}
1051
1052 ralt :: { [RdrNameGRHS] }
1053         : '->' srcloc exp               { [GRHS [ResultStmt $3 $2] $2] }
1054         | gdpats                        { reverse $1 }
1055
1056 gdpats :: { [RdrNameGRHS] }
1057         : gdpats gdpat                  { $2 : $1 }
1058         | gdpat                         { [$1] }
1059
1060 gdpat   :: { RdrNameGRHS }
1061         : srcloc '|' quals '->' exp     { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
1062
1063 -----------------------------------------------------------------------------
1064 -- Statement sequences
1065
1066 stmtlist :: { [RdrNameStmt] }
1067         : '{'                   stmts '}'       { $2 }
1068         |     layout_on_for_do  stmts close     { $2 }
1069
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 }
1077         | ';' stmts                     { $2 }
1078         | {- empty -}                   { [] }
1079
1080 stmts_help :: { [RdrNameStmt] }
1081         : ';' stmts                     { $2 }
1082         | {- empty -}                   { [] }
1083
1084 -- For typing stmts at the GHCi prompt, where 
1085 -- the input may consist of just comments.
1086 maybe_stmt :: { Maybe RdrNameStmt }
1087         : stmt                          { Just $1 }
1088         | {- nothing -}                 { Nothing }
1089
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 }
1095
1096 -----------------------------------------------------------------------------
1097 -- Record Field Update/Construction
1098
1099 fbinds  :: { RdrNameHsRecordBinds }
1100         : fbinds ',' fbind              { $3 : $1 }
1101         | fbinds ','                    { $1 }
1102         | fbind                         { [$1] }
1103         | {- empty -}                   { [] }
1104
1105 fbind   :: { (RdrName, RdrNameHsExpr, Bool) }
1106         : qvar '=' exp                  { ($1,$3,False) }
1107
1108 -----------------------------------------------------------------------------
1109 -- Implicit Parameter Bindings
1110
1111 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1112         : '{' dbinds '}'                { $2 }
1113         | layout_on dbinds close        { $2 }
1114
1115 dbinds  :: { [(IPName RdrName, RdrNameHsExpr)] }
1116         : dbinds ';' dbind              { $3 : $1 }
1117         | dbinds ';'                    { $1 }
1118         | dbind                         { [$1] }
1119         | {- empty -}                   { [] }
1120
1121 dbind   :: { (IPName RdrName, RdrNameHsExpr) }
1122 dbind   : ipvar '=' exp                 { ($1, $3) }
1123
1124 -----------------------------------------------------------------------------
1125 -- Variables, Constructors and Operators.
1126
1127 identifier :: { RdrName }
1128         : qvar                          { $1 }
1129         | gcon                          { $1 }
1130         | qop                           { $1 }
1131
1132 depreclist :: { [RdrName] }
1133 depreclist : deprec_var                 { [$1] }
1134            | deprec_var ',' depreclist  { $1 : $3 }
1135
1136 deprec_var :: { RdrName }
1137 deprec_var : var                        { $1 }
1138            | tycon                      { $1 }
1139
1140 gtycon  :: { RdrName }
1141         : qtycon                        { $1 }
1142         | '(' qtyconop ')'              { $2 }
1143         | '(' ')'                       { unitTyCon_RDR }
1144         | '(' '->' ')'                  { funTyCon_RDR }
1145         | '[' ']'                       { listTyCon_RDR }
1146         | '[:' ':]'                     { parrTyCon_RDR }
1147         | '(' commas ')'                { tupleTyCon_RDR $2 }
1148
1149 gcon    :: { RdrName }  -- Data constructor namespace
1150         : '(' ')'               { unitCon_RDR }
1151         | '[' ']'               { nilCon_RDR }
1152         | '(' commas ')'        { tupleCon_RDR $2 }
1153         | qcon                  { $1 }
1154 -- the case of '[:' ':]' is part of the production `parr'
1155
1156 var     :: { RdrName }
1157         : varid                 { $1 }
1158         | '(' varsym ')'        { $2 }
1159
1160 qvar    :: { RdrName }
1161         : qvarid                { $1 }
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.
1167
1168 ipvar   :: { IPName RdrName }
1169         : IPDUPVARID            { Dupable (mkUnqual varName $1) }
1170         | IPSPLITVARID          { Linear  (mkUnqual varName $1) }
1171
1172 qcon    :: { RdrName }
1173         : qconid                { $1 }
1174         | '(' qconsym ')'       { $2 }
1175
1176 varop   :: { RdrName }
1177         : varsym                { $1 }
1178         | '`' varid '`'         { $2 }
1179
1180 qvarop :: { RdrName }
1181         : qvarsym               { $1 }
1182         | '`' qvarid '`'        { $2 }
1183
1184 qvaropm :: { RdrName }
1185         : qvarsym_no_minus      { $1 }
1186         | '`' qvarid '`'        { $2 }
1187
1188 conop :: { RdrName }
1189         : consym                { $1 }  
1190         | '`' conid '`'         { $2 }
1191
1192 qconop :: { RdrName }
1193         : qconsym               { $1 }
1194         | '`' qconid '`'        { $2 }
1195
1196 -----------------------------------------------------------------------------
1197 -- Any operator
1198
1199 op      :: { RdrName }   -- used in infix decls
1200         : varop                 { $1 }
1201         | conop                 { $1 }
1202
1203 qop     :: { RdrName {-HsExpr-} }   -- used in sections
1204         : qvarop                { $1 }
1205         | qconop                { $1 }
1206
1207 qopm    :: { RdrNameHsExpr }   -- used in sections
1208         : qvaropm               { HsVar $1 }
1209         | qconop                { HsVar $1 }
1210
1211 -----------------------------------------------------------------------------
1212 -- VarIds
1213
1214 qvarid :: { RdrName }
1215         : varid                 { $1 }
1216         | QVARID                { mkQual varName $1 }
1217
1218 varid :: { RdrName }
1219         : varid_no_unsafe       { $1 }
1220         | 'unsafe'              { mkUnqual varName SLIT("unsafe") }
1221
1222 varid_no_unsafe :: { RdrName }
1223         : VARID                 { mkUnqual varName $1 }
1224         | special_id            { mkUnqual varName $1 }
1225         | 'forall'              { mkUnqual varName SLIT("forall") }
1226
1227 tyvar   :: { RdrName }
1228         : VARID                 { mkUnqual tvName $1 }
1229         | special_id            { mkUnqual tvName $1 }
1230         | 'unsafe'              { mkUnqual tvName SLIT("unsafe") }
1231
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 }
1236 special_id
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") }
1245
1246 -----------------------------------------------------------------------------
1247 -- ConIds
1248
1249 qconid :: { RdrName }   -- Qualified or unqualifiedb
1250         : conid                 { $1 }
1251         | QCONID                { mkQual dataName $1 }
1252
1253 conid   :: { RdrName }
1254         : CONID                 { mkUnqual dataName $1 }
1255
1256 -----------------------------------------------------------------------------
1257 -- ConSyms
1258
1259 qconsym :: { RdrName }  -- Qualified or unqualifiedb
1260         : consym                { $1 }
1261         | QCONSYM               { mkQual dataName $1 }
1262
1263 consym :: { RdrName }
1264         : CONSYM                { mkUnqual dataName $1 }
1265
1266 -----------------------------------------------------------------------------
1267 -- VarSyms
1268
1269 qvarsym :: { RdrName }
1270         : varsym                { $1 }
1271         | qvarsym1              { $1 }
1272
1273 qvarsym_no_minus :: { RdrName }
1274         : varsym_no_minus       { $1 }
1275         | qvarsym1              { $1 }
1276
1277 qvarsym1 :: { RdrName }
1278 qvarsym1 : QVARSYM              { mkQual varName $1 }
1279
1280 varsym :: { RdrName }
1281         : varsym_no_minus       { $1 }
1282         | '-'                   { mkUnqual varName SLIT("-") }
1283
1284 varsym_no_minus :: { RdrName } -- varsym not including '-'
1285         : VARSYM                { mkUnqual varName $1 }
1286         | special_sym           { mkUnqual varName $1 }
1287
1288
1289 -- See comments with special_id
1290 special_sym :: { UserFS }
1291 special_sym : '!'       { SLIT("!") }
1292             | '.'       { SLIT(".") }
1293             | '*'       { SLIT("*") }
1294
1295 -----------------------------------------------------------------------------
1296 -- Literals
1297
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 }
1307
1308 srcloc :: { SrcLoc }    :       {% getSrcLocP }
1309  
1310 -----------------------------------------------------------------------------
1311 -- Layout
1312
1313 close :: { () }
1314         : vccurly               { () } -- context popped in lexer.
1315         | error                 {% popContext }
1316
1317 layout_on         :: { () }     : {% layoutOn True{-strict-} }
1318 layout_on_for_do  :: { () }     : {% layoutOn False }
1319
1320 -----------------------------------------------------------------------------
1321 -- Miscellaneous (mostly renamings)
1322
1323 modid   :: { ModuleName }
1324         : CONID                 { mkModuleNameFS $1 }
1325         | QCONID                { mkModuleNameFS
1326                                    (mkFastString
1327                                      (unpackFS (fst $1) ++ 
1328                                         '.':unpackFS (snd $1)))
1329                                 }
1330
1331 tycon   :: { RdrName }
1332         : CONID                 { mkUnqual tcClsName $1 }
1333
1334 tyconop :: { RdrName }
1335         : CONSYM                { mkUnqual tcClsName $1 }
1336
1337 qtycon :: { RdrName }   -- Qualified or unqualified
1338         : QCONID                { mkQual tcClsName $1 }
1339         | tycon                 { $1 }
1340
1341 qtyconop :: { RdrName } -- Qualified or unqualified
1342           : QCONSYM             { mkQual tcClsName $1 }
1343           | tyconop             { $1 }
1344
1345 commas :: { Int }
1346         : commas ','                    { $1 + 1 }
1347         | ','                           { 2 }
1348
1349 -----------------------------------------------------------------------------
1350
1351 {
1352 happyError :: P a
1353 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
1354 }