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