[project @ 2002-02-13 15:19:17 by simonpj]
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-                                                              -*-haskell-*-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.89 2002/02/13 15:19:19 simonpj 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  'unsafe'       { ITunsafe }
115  'with'         { ITwith }
116  'stdcall'      { ITstdcallconv }
117  'ccall'        { ITccallconv }
118  'dotnet'       { ITdotnet }
119  '_ccall_'      { ITccall (False, False, PlayRisky) }
120  '_ccall_GC_'   { ITccall (False, False, PlaySafe)  }
121  '_casm_'       { ITccall (False, True,  PlayRisky) }
122  '_casm_GC_'    { ITccall (False, True,  PlaySafe)  }
123
124  '{-# SPECIALISE'  { ITspecialise_prag }
125  '{-# SOURCE'      { ITsource_prag }
126  '{-# INLINE'      { ITinline_prag }
127  '{-# NOINLINE'    { ITnoinline_prag }
128  '{-# RULES'       { ITrules_prag }
129  '{-# SCC'         { ITscc_prag }
130  '{-# DEPRECATED'  { ITdeprecated_prag }
131  '#-}'             { ITclose_prag }
132
133 {-
134  '__interface'  { ITinterface }                 -- interface keywords
135  '__export'     { IT__export }
136  '__instimport' { ITinstimport }
137  '__forall'     { IT__forall }
138  '__letrec'     { ITletrec }
139  '__coerce'     { ITcoerce }
140  '__depends'    { ITdepends }
141  '__inline'     { ITinline }
142  '__DEFAULT'    { ITdefaultbranch }
143  '__bot'        { ITbottom }
144  '__integer'    { ITinteger_lit }
145  '__float'      { ITfloat_lit }
146  '__rational'   { ITrational_lit }
147  '__addr'       { ITaddr_lit }
148  '__label'      { ITlabel_lit }
149  '__litlit'     { ITlit_lit }
150  '__string'     { ITstring_lit }
151  '__ccall'      { ITccall $$ }
152  '__scc'        { IT__scc }
153  '__sccC'       { ITsccAllCafs }
154
155  '__A'          { ITarity }
156  '__P'          { ITspecialise }
157  '__C'          { ITnocaf }
158  '__U'          { ITunfold }
159  '__S'          { ITstrict $$ }
160  '__M'          { ITcprinfo $$ }
161 -}
162
163  '..'           { ITdotdot }                    -- reserved symbols
164  '::'           { ITdcolon }
165  '='            { ITequal }
166  '\\'           { ITlam }
167  '|'            { ITvbar }
168  '<-'           { ITlarrow }
169  '->'           { ITrarrow }
170  '@'            { ITat }
171  '~'            { ITtilde }
172  '=>'           { ITdarrow }
173  '-'            { ITminus }
174  '!'            { ITbang }
175  '*'            { ITstar }
176  '.'            { ITdot }
177
178  '{'            { ITocurly }                    -- special symbols
179  '}'            { ITccurly }
180  '{|'           { ITocurlybar }
181  '|}'           { ITccurlybar }
182  vccurly        { ITvccurly } -- virtual close curly (from layout)
183  '['            { ITobrack }
184  ']'            { ITcbrack }
185  '[:'           { ITopabrack }
186  ':]'           { ITcpabrack }
187  '('            { IToparen }
188  ')'            { ITcparen }
189  '(#'           { IToubxparen }
190  '#)'           { ITcubxparen }
191  ';'            { ITsemi }
192  ','            { ITcomma }
193  '`'            { ITbackquote }
194
195  VARID          { ITvarid    $$ }               -- identifiers
196  CONID          { ITconid    $$ }
197  VARSYM         { ITvarsym   $$ }
198  CONSYM         { ITconsym   $$ }
199  QVARID         { ITqvarid   $$ }
200  QCONID         { ITqconid   $$ }
201  QVARSYM        { ITqvarsym  $$ }
202  QCONSYM        { ITqconsym  $$ }
203
204  IPDUPVARID     { ITdupipvarid   $$ }           -- GHC extension
205  IPSPLITVARID   { ITsplitipvarid $$ }           -- GHC extension
206
207  CHAR           { ITchar     $$ }
208  STRING         { ITstring   $$ }
209  INTEGER        { ITinteger  $$ }
210  RATIONAL       { ITrational $$ }
211
212  PRIMCHAR       { ITprimchar   $$ }
213  PRIMSTRING     { ITprimstring $$ }
214  PRIMINTEGER    { ITprimint    $$ }
215  PRIMFLOAT      { ITprimfloat  $$ }
216  PRIMDOUBLE     { ITprimdouble $$ }
217  CLITLIT        { ITlitlit     $$ }
218
219 %monad { P } { thenP } { returnP }
220 %lexer { lexer } { ITeof }
221 %name parseModule module
222 %name parseStmt   maybe_stmt
223 %name parseIdentifier  identifier
224 %tokentype { Token }
225 %%
226
227 -----------------------------------------------------------------------------
228 -- Module Header
229
230 -- The place for module deprecation is really too restrictive, but if it
231 -- was allowed at its natural place just before 'module', we get an ugly
232 -- s/r conflict with the second alternative. Another solution would be the
233 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
234 -- either, and DEPRECATED is only expected to be used by people who really
235 -- know what they are doing. :-)
236
237 module  :: { RdrNameHsModule }
238         : srcloc 'module' modid maybemoddeprec maybeexports 'where' body 
239                 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
240         | srcloc body
241                 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
242
243 maybemoddeprec :: { Maybe DeprecTxt }
244         : '{-# DEPRECATED' STRING '#-}'         { Just $2 }
245         |  {- empty -}                          { Nothing }
246
247 body    :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
248         :  '{'            top '}'               { $2 }
249         |      layout_on  top close             { $2 }
250
251 top     :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
252         : importdecls                           { (reverse $1,[]) }
253         | importdecls ';' cvtopdecls            { (reverse $1,$3) }
254         | cvtopdecls                            { ([],$1) }
255
256 cvtopdecls :: { [RdrNameHsDecl] }
257         : topdecls                              { cvTopDecls (groupBindings $1)}
258
259 -----------------------------------------------------------------------------
260 -- The Export List
261
262 maybeexports :: { Maybe [RdrNameIE] }
263         :  '(' exportlist ')'                   { Just $2 }
264         |  {- empty -}                          { Nothing }
265
266 exportlist :: { [RdrNameIE] }
267         :  exportlist ',' export                { $3 : $1 }
268         |  exportlist ','                       { $1 }
269         |  export                               { [$1]  }
270         |  {- empty -}                          { [] }
271
272    -- GHC extension: we allow things like [] and (,,,) to be exported
273 export  :: { RdrNameIE }
274         :  qvar                                 { IEVar $1 }
275         |  gtycon                               { IEThingAbs $1 }
276         |  gtycon '(' '..' ')'                  { IEThingAll $1 }
277         |  gtycon '(' ')'                       { IEThingWith $1 [] }
278         |  gtycon '(' qcnames ')'               { IEThingWith $1 (reverse $3) }
279         |  'module' modid                       { IEModuleContents $2 }
280
281 qcnames :: { [RdrName] }
282         :  qcnames ',' qcname                   { $3 : $1 }
283         |  qcname                               { [$1]  }
284
285 qcname  :: { RdrName }
286         :  qvar                                 { $1 }
287         |  gcon                                 { $1 }
288
289 -----------------------------------------------------------------------------
290 -- Import Declarations
291
292 -- import decls can be *empty*, or even just a string of semicolons
293 -- whereas topdecls must contain at least one topdecl.
294
295 importdecls :: { [RdrNameImportDecl] }
296         : importdecls ';' importdecl            { $3 : $1 }
297         | importdecls ';'                       { $1 }
298         | importdecl                            { [ $1 ] }
299         | {- empty -}                           { [] }
300
301 importdecl :: { RdrNameImportDecl }
302         : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec 
303                 { ImportDecl $5 $3 $4 $6 $7 $2 }
304
305 maybe_src :: { WhereFrom }
306         : '{-# SOURCE' '#-}'                    { ImportByUserSource }
307         | {- empty -}                           { ImportByUser }
308
309 optqualified :: { Bool }
310         : 'qualified'                           { True  }
311         | {- empty -}                           { False }
312
313 maybeas :: { Maybe ModuleName }
314         : 'as' modid                            { Just $2 }
315         | {- empty -}                           { Nothing }
316
317 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
318         : impspec                               { Just $1 }
319         | {- empty -}                           { Nothing }
320
321 impspec :: { (Bool, [RdrNameIE]) }
322         :  '(' exportlist ')'                   { (False, reverse $2) }
323         |  'hiding' '(' exportlist ')'          { (True,  reverse $3) }
324
325 -----------------------------------------------------------------------------
326 -- Fixity Declarations
327
328 prec    :: { Int }
329         : {- empty -}                           { 9 }
330         | INTEGER                               {%  checkPrec $1 `thenP_`
331                                                     returnP (fromInteger $1) }
332
333 infix   :: { FixityDirection }
334         : 'infix'                               { InfixN  }
335         | 'infixl'                              { InfixL  }
336         | 'infixr'                              { InfixR }
337
338 ops     :: { [RdrName] }
339         : ops ',' op                            { $3 : $1 }
340         | op                                    { [$1] }
341
342 -----------------------------------------------------------------------------
343 -- Top-Level Declarations
344
345 topdecls :: { [RdrBinding] }
346         : topdecls ';' topdecl          { ($3 : $1) }
347         | topdecls ';'                  { $1 }
348         | topdecl                       { [$1] }
349
350 topdecl :: { RdrBinding }
351         : srcloc 'type' tycon tv_bndrs '=' ctype        
352                 -- Note ctype, not sigtype.
353                 -- We allow an explicit for-all but we don't insert one
354                 -- in   type Foo a = (b,b)
355                 -- Instead we just say b is out of scope
356                 { RdrHsDecl (TyClD (TySynonym $3 $4 $6 $1)) }
357
358
359         | srcloc 'data' tycl_hdr constrs deriving
360                 {% returnP (RdrHsDecl (TyClD
361                       (mkTyData DataType $3 (DataCons (reverse $4)) $5 $1))) }
362
363         | srcloc 'newtype' tycl_hdr '=' newconstr deriving
364                 {% returnP (RdrHsDecl (TyClD
365                       (mkTyData NewType $3 (DataCons [$5]) $6 $1))) }
366
367         | srcloc 'class' tycl_hdr fds where
368                 {% let 
369                         (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5) 
370                    in
371                    returnP (RdrHsDecl (TyClD
372                       (mkClassDecl $3 $4 sigs (Just binds) $1))) }
373
374         | srcloc 'instance' inst_type where
375                 { let (binds,sigs) 
376                         = cvMonoBindsAndSigs cvInstDeclSig 
377                                 (groupBindings $4)
378                   in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
379
380         | srcloc 'default' '(' comma_types0 ')'         { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
381         | 'foreign' fdecl                               { RdrHsDecl $2 }
382         | '{-# DEPRECATED' deprecations '#-}'           { $2 }
383         | '{-# RULES' rules '#-}'                       { $2 }
384         | decl                                          { $1 }
385
386 -- tycl_hdr parses the header of a type or class decl,
387 -- which takes the form
388 --      T a b
389 --      Eq a => T a
390 --      (Eq a, Ord b) => T a b
391 -- Rather a lot of inlining here, else we get reduce/reduce errors
392 tycl_hdr :: { (RdrNameContext, RdrName, [RdrNameHsTyVar]) }
393         : '(' comma_types1 ')' '=>' gtycon tv_bndrs     {% mapP checkPred $2    `thenP` \ cxt ->
394                                                            returnP (cxt, $5, $6) }
395           -- qtycon for the class below name would lead to many s/r conflicts
396           --   FIXME: does the renamer pick up all wrong forms and raise an
397           --          error 
398         | gtycon atypes1 '=>' gtycon atypes0            {% checkTyVars $5       `thenP` \ tvs ->
399                                                            returnP ([HsClassP $1 $2], $4, tvs) }
400         | gtycon  atypes0                               {% checkTyVars $2       `thenP` \ tvs ->
401                                                            returnP ([], $1, tvs) }
402                 -- We have to have qtycon in this production to avoid s/r
403                 -- conflicts with the previous one.  The renamer will complain
404                 -- if we use a qualified tycon.
405                 --
406                 -- Using a `gtycon' throughout.  This enables special syntax,
407                 -- such as "[]" for tycons as well as tycon ops in
408                 -- parentheses.  This is beyond H98, but used repeatedly in
409                 -- the Prelude modules.  (So, it would be a good idea to raise
410                 -- an error in the renamer if some non-H98 form is used and
411                 -- -fglasgow-exts is not given.)  -=chak 
412
413 decls   :: { [RdrBinding] }
414         : decls ';' decl                { $3 : $1 }
415         | decls ';'                     { $1 }
416         | decl                          { [$1] }
417         | {- empty -}                   { [] }
418
419 decl    :: { RdrBinding }
420         : fixdecl                       { $1 }
421         | valdef                        { $1 }
422         | '{-# INLINE'   srcloc activation qvar '#-}'         { RdrSig (InlineSig True  $4 $3 $2) }
423         | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
424         | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
425                 { foldr1 RdrAndBindings 
426                     (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
427         | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
428                 { RdrSig (SpecInstSig $4 $2) }
429
430 wherebinds :: { RdrNameHsBinds }
431         : where                 { cvBinds cvValSig (groupBindings $1) }
432
433 where   :: { [RdrBinding] }
434         : 'where' decllist              { $2 }
435         | {- empty -}                   { [] }
436
437 declbinds :: { RdrNameHsBinds }
438         : decllist                      { cvBinds cvValSig (groupBindings $1) }
439
440 decllist :: { [RdrBinding] }
441         : '{'            decls '}'      { $2 }
442         |     layout_on  decls close    { $2 }
443
444 fixdecl :: { RdrBinding }
445         : srcloc infix prec ops         { foldr1 RdrAndBindings
446                                             [ RdrSig (FixSig (FixitySig n 
447                                                             (Fixity $3 $2) $1))
448                                             | n <- $4 ] }
449
450 -----------------------------------------------------------------------------
451 -- Transformation Rules
452
453 rules   :: { RdrBinding }
454         :  rules ';' rule                       { $1 `RdrAndBindings` $3 }
455         |  rules ';'                            { $1 }
456         |  rule                                 { $1 }
457         |  {- empty -}                          { RdrNullBind }
458
459 rule    :: { RdrBinding }
460         : STRING activation rule_forall infixexp '=' srcloc exp
461              { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
462
463 activation :: { Activation }           -- Omitted means AlwaysActive
464         : {- empty -}                           { AlwaysActive }
465         | explicit_activation                   { $1 }
466
467 inverse_activation :: { Activation }   -- Omitted means NeverActive
468         : {- empty -}                           { NeverActive }
469         | explicit_activation                   { $1 }
470
471 explicit_activation :: { Activation }  -- In brackets
472         : '[' INTEGER ']'                       { ActiveAfter  (fromInteger $2) }
473         | '[' '~' INTEGER ']'                   { ActiveBefore (fromInteger $3) }
474
475 rule_forall :: { [RdrNameRuleBndr] }
476         : 'forall' rule_var_list '.'            { $2 }
477         | {- empty -}                           { [] }
478
479 rule_var_list :: { [RdrNameRuleBndr] }
480         : rule_var                              { [$1] }
481         | rule_var rule_var_list                { $1 : $2 }
482
483 rule_var :: { RdrNameRuleBndr }
484         : varid                                 { RuleBndr $1 }
485         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
486
487 -----------------------------------------------------------------------------
488 -- Deprecations
489
490 deprecations :: { RdrBinding }
491         : deprecations ';' deprecation          { $1 `RdrAndBindings` $3 }
492         | deprecations ';'                      { $1 }
493         | deprecation                           { $1 }
494         | {- empty -}                           { RdrNullBind }
495
496 -- SUP: TEMPORARY HACK, not checking for `module Foo'
497 deprecation :: { RdrBinding }
498         : srcloc depreclist STRING
499                 { foldr RdrAndBindings RdrNullBind 
500                         [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
501
502
503 -----------------------------------------------------------------------------
504 -- Foreign import and export declarations
505
506 -- for the time being, the following accepts foreign declarations conforming
507 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
508 --
509 -- * a flag indicates whether pre-standard declarations have been used and
510 --   triggers a deprecation warning further down the road
511 --
512 -- NB: The first two rules could be combined into one by replacing `safety1'
513 --     with `safety'.  However, the combined rule conflicts with the
514 --     DEPRECATED rules.
515 --
516 fdecl :: { RdrNameHsDecl }
517 fdecl : srcloc 'import' callconv safety1 fspec  {% mkImport $3 $4       $5 $1 }
518       | srcloc 'import' callconv         fspec  {% mkImport $3 PlaySafe $4 $1 }
519       | srcloc 'export' callconv         fspec  {% mkExport $3          $4 $1 }
520         -- the following syntax is DEPRECATED
521       | srcloc fdecl1DEPRECATED                 { ForD ($2 True $1) }
522       | srcloc fdecl2DEPRECATED                 { $2 $1 }
523
524 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
525 fdecl1DEPRECATED 
526   ----------- DEPRECATED label decls ------------
527   : 'label' ext_name varid '::' sigtype
528     { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_ 
529                                    (CLabel ($2 `orElse` mkExtName $3))) }
530
531   ----------- DEPRECATED ccall/stdcall decls ------------
532   --
533   -- NB: This business with the case expression below may seem overly
534   --     complicated, but it is necessary to avoid some conflicts.
535
536     -- DEPRECATED variant #1: lack of a calling convention specification
537     --                        (import) 
538   | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
539     { let
540         target = StaticTarget ($2 `orElse` mkExtName $4)
541       in
542       ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_ 
543                                    (CFunction target)) }
544
545     -- DEPRECATED variant #2: external name consists of two separate strings
546     --                        (module name and function name) (import)
547   | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
548     {% case $2 of
549          DNCall      -> parseError "Illegal format of .NET foreign import"
550          CCall cconv -> returnP $
551            let
552              imp = CFunction (StaticTarget $4)
553            in
554            ForeignImport $6 $8 (CImport cconv $5 _NIL_ _NIL_ imp) }
555
556     -- DEPRECATED variant #3: `unsafe' after entity
557   | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
558     {% case $2 of
559          DNCall      -> parseError "Illegal format of .NET foreign import"
560          CCall cconv -> returnP $
561            let
562              imp = CFunction (StaticTarget $3)
563            in
564            ForeignImport $5 $7 (CImport cconv PlayRisky _NIL_ _NIL_ imp) }
565
566     -- DEPRECATED variant #4: use of the special identifier `dynamic' without
567     --                        an explicit calling convention (import)
568   | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
569     { ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_ 
570                                    (CFunction DynamicTarget)) }
571
572     -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
573   | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
574     {% case $2 of
575          DNCall      -> parseError "Illegal format of .NET foreign import"
576          CCall cconv -> returnP $
577            ForeignImport $5 $7 (CImport cconv $4 _NIL_ _NIL_ 
578                                         (CFunction DynamicTarget)) }
579
580     -- DEPRECATED variant #6: lack of a calling convention specification
581     --                        (export) 
582   | 'export' {-no callconv-} ext_name varid '::' sigtype
583     { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3) 
584                                    defaultCCallConv)) }
585
586     -- DEPRECATED variant #7: external name consists of two separate strings
587     --                        (module name and function name) (export)
588   | 'export' callconv STRING STRING varid '::' sigtype
589     {% case $2 of
590          DNCall      -> parseError "Illegal format of .NET foreign import"
591          CCall cconv -> returnP $
592            ForeignExport $5 $7 
593                          (CExport (CExportStatic $4 cconv)) }
594
595     -- DEPRECATED variant #8: use of the special identifier `dynamic' without
596     --                        an explicit calling convention (export)
597   | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
598     { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_ 
599                                    CWrapper) }
600
601     -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
602   | 'export' callconv 'dynamic' varid '::' sigtype
603     {% case $2 of
604          DNCall      -> parseError "Illegal format of .NET foreign import"
605          CCall cconv -> returnP $
606            ForeignImport $4 $6 (CImport cconv PlaySafe _NIL_ _NIL_ CWrapper) }
607
608   ----------- DEPRECATED .NET decls ------------
609   -- NB: removed the .NET call declaration, as it is entirely subsumed
610   --     by the new standard FFI declarations
611
612 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
613 fdecl2DEPRECATED 
614   : 'import' 'dotnet' 'type' ext_name tycon
615           { \loc -> TyClD (ForeignType $5 $4 DNType loc) }
616     -- left this one unchanged for the moment as type imports are not
617     -- covered currently by the FFI standard -=chak
618
619
620 callconv :: { CallConv }
621           : 'stdcall'                   { CCall  StdCallConv }
622           | 'ccall'                     { CCall  CCallConv   }
623           | 'dotnet'                    { DNCall             }
624
625 safety :: { Safety }
626         : 'unsafe'                      { PlayRisky }
627         | 'safe'                        { PlaySafe  }
628         | {- empty -}                   { PlaySafe  }
629
630 safety1 :: { Safety }
631         : 'unsafe'                      { PlayRisky }
632         | 'safe'                        { PlaySafe  }
633           -- only needed to avoid conflicts with the DEPRECATED rules
634
635 fspec :: { (FAST_STRING, RdrName, RdrNameHsType) }
636        : STRING varid '::' sigtype      { ($1      , $2, $4) }
637        |        varid '::' sigtype      { (SLIT(""), $1, $3) }
638          -- if the entity string is missing, it defaults to the empty string;
639          -- the meaning of an empty entity string depends on the calling
640          -- convention
641
642 -- DEPRECATED syntax
643 ext_name :: { Maybe CLabelString }
644         : STRING                { Just $1 }
645         | STRING STRING         { Just $2 }     -- Ignore "module name" for now
646         | {- empty -}           { Nothing }
647
648
649 -----------------------------------------------------------------------------
650 -- Type signatures
651
652 opt_sig :: { Maybe RdrNameHsType }
653         : {- empty -}                   { Nothing }
654         | '::' sigtype                  { Just $2 }
655
656 opt_asig :: { Maybe RdrNameHsType }
657         : {- empty -}                   { Nothing }
658         | '::' atype                    { Just $2 }
659
660 sigtypes :: { [RdrNameHsType] }
661         : sigtype                       { [ $1 ] }
662         | sigtypes ',' sigtype          { $3 : $1 }
663
664 sigtype :: { RdrNameHsType }
665         : ctype                         { mkHsForAllTy Nothing [] $1 }
666
667 sig_vars :: { [RdrName] }
668          : sig_vars ',' var             { $3 : $1 }
669          | var                          { [ $1 ] }
670
671 -----------------------------------------------------------------------------
672 -- Types
673
674 -- A ctype is a for-all type
675 ctype   :: { RdrNameHsType }
676         : 'forall' tv_bndrs '.' ctype   { mkHsForAllTy (Just $2) [] $4 }
677         | context '=>' type             { mkHsForAllTy Nothing   $1 $3 }
678         -- A type of form (context => type) is an *implicit* HsForAllTy
679         | type                          { $1 }
680
681 -- We parse a context as a btype so that we don't get reduce/reduce
682 -- errors in ctype.  The basic problem is that
683 --      (Eq a, Ord a)
684 -- looks so much like a tuple type.  We can't tell until we find the =>
685 context :: { RdrNameContext }
686         : btype                         {% checkContext $1 }
687
688 type :: { RdrNameHsType }
689         : gentype '->' type             { HsFunTy $1 $3 }
690         | ipvar '::' type               { mkHsIParamTy $1 $3 }
691         | gentype                       { $1 }
692
693 gentype :: { RdrNameHsType }
694         : btype                         { $1 }
695 -- Generics
696         | atype tyconop atype           { HsOpTy $1 $2 $3 }
697
698 btype :: { RdrNameHsType }
699         : btype atype                   { HsAppTy $1 $2 }
700         | atype                         { $1 }
701
702 atype :: { RdrNameHsType }
703         : gtycon                        { HsTyVar $1 }
704         | tyvar                         { HsTyVar $1 }
705         | '(' type ',' comma_types1 ')' { HsTupleTy (mkHsTupCon tcName Boxed  ($2:$4)) ($2:$4) }
706         | '(#' comma_types1 '#)'        { HsTupleTy (mkHsTupCon tcName Unboxed     $2) $2      }
707         | '[' type ']'                  { HsListTy $2 }
708         | '[:' type ':]'                { HsPArrTy $2 }
709         | '(' ctype ')'                 { $2 }
710         | '(' ctype '::' kind ')'       { HsKindSig $2 $4 }
711 -- Generics
712         | INTEGER                       { HsNumTy $1 }
713
714 -- An inst_type is what occurs in the head of an instance decl
715 --      e.g.  (Foo a, Gaz b) => Wibble a b
716 -- It's kept as a single type, with a MonoDictTy at the right
717 -- hand corner, for convenience.
718 inst_type :: { RdrNameHsType }
719         : ctype                         {% checkInstType $1 }
720
721 comma_types0  :: { [RdrNameHsType] }
722         : comma_types1                  { $1 }
723         | {- empty -}                   { [] }
724
725 comma_types1    :: { [RdrNameHsType] }
726         : type                          { [$1] }
727         | type  ',' comma_types1        { $1 : $3 }
728
729 atypes0 :: { [RdrNameHsType] }
730         : atypes1                       { $1 }
731         | {- empty -}                   { [] }
732
733 atypes1 :: { [RdrNameHsType] }
734         : atype                         { [$1] }
735         | atype atypes1                 { $1 : $2 }
736
737 tv_bndrs :: { [RdrNameHsTyVar] }
738          : tv_bndr tv_bndrs             { $1 : $2 }
739          | {- empty -}                  { [] }
740
741 tv_bndr :: { RdrNameHsTyVar }
742         : tyvar                         { UserTyVar $1 }
743         | '(' tyvar '::' kind ')'       { IfaceTyVar $2 $4 }
744
745 fds :: { [([RdrName], [RdrName])] }
746         : {- empty -}                   { [] }
747         | '|' fds1                      { reverse $2 }
748
749 fds1 :: { [([RdrName], [RdrName])] }
750         : fds1 ',' fd                   { $3 : $1 }
751         | fd                            { [$1] }
752
753 fd :: { ([RdrName], [RdrName]) }
754         : varids0 '->' varids0          { (reverse $1, reverse $3) }
755
756 varids0 :: { [RdrName] }
757         : {- empty -}                   { [] }
758         | varids0 tyvar                 { $2 : $1 }
759
760 -----------------------------------------------------------------------------
761 -- Kinds
762
763 kind    :: { Kind }
764         : akind                 { $1 }
765         | akind '->' kind       { mkArrowKind $1 $3 }
766
767 akind   :: { Kind }
768         : '*'                   { liftedTypeKind }
769         | '(' kind ')'          { $2 }
770
771
772 -----------------------------------------------------------------------------
773 -- Datatype declarations
774
775 newconstr :: { RdrNameConDecl }
776         : srcloc conid atype    { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
777         | srcloc conid '{' var '::' ctype '}'
778                                 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
779
780 constrs :: { [RdrNameConDecl] }
781         : {- empty; a GHC extension -}  { [] }
782         | '=' constrs1                  { $2 }
783
784 constrs1 :: { [RdrNameConDecl] }
785         : constrs1 '|' constr           { $3 : $1 }
786         | constr                        { [$1] }
787
788 constr :: { RdrNameConDecl }
789         : srcloc forall context '=>' constr_stuff
790                 { mkConDecl (fst $5) $2 $3 (snd $5) $1 }
791         | srcloc forall constr_stuff
792                 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
793
794 forall :: { [RdrNameHsTyVar] }
795         : 'forall' tv_bndrs '.'         { $2 }
796         | {- empty -}                   { [] }
797
798 constr_stuff :: { (RdrName, RdrNameConDetails) }
799         : btype                         {% mkVanillaCon $1 []               }
800         | btype '!' atype satypes       {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
801         | gtycon '{' '}'                {% mkRecCon $1 [] }
802         | gtycon '{' fielddecls '}'     {% mkRecCon $1 $3 }
803         | sbtype conop sbtype           { ($2, InfixCon $1 $3) }
804
805 satypes :: { [RdrNameBangType] }
806         : atype satypes                 { unbangedType $1 : $2 }
807         | '!' atype satypes             { BangType MarkedUserStrict $2 : $3 }
808         | {- empty -}                   { [] }
809
810 sbtype :: { RdrNameBangType }
811         : btype                         { unbangedType $1 }
812         | '!' atype                     { BangType MarkedUserStrict $2 }
813
814 fielddecls :: { [([RdrName],RdrNameBangType)] }
815         : fielddecl ',' fielddecls      { $1 : $3 }
816         | fielddecl                     { [$1] }
817
818 fielddecl :: { ([RdrName],RdrNameBangType) }
819         : sig_vars '::' stype           { (reverse $1, $3) }
820
821 stype :: { RdrNameBangType }
822         : ctype                         { unbangedType $1 }
823         | '!' atype                     { BangType MarkedUserStrict $2 }
824
825 deriving :: { Maybe RdrNameContext }
826         : {- empty -}                   { Nothing }
827         | 'deriving' context            { Just $2 }
828              -- Glasgow extension: allow partial 
829              -- applications in derivings
830
831 -----------------------------------------------------------------------------
832 -- Value definitions
833
834 {- There's an awkward overlap with a type signature.  Consider
835         f :: Int -> Int = ...rhs...
836    Then we can't tell whether it's a type signature or a value
837    definition with a result signature until we see the '='.
838    So we have to inline enough to postpone reductions until we know.
839 -}
840
841 {-
842   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
843   instead of qvar, we get another shift/reduce-conflict. Consider the
844   following programs:
845   
846      { (^^) :: Int->Int ; }          Type signature; only var allowed
847
848      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
849                                      qvar allowed (because of instance decls)
850   
851   We can't tell whether to reduce var to qvar until after we've read the signatures.
852 -}
853
854 valdef :: { RdrBinding }
855         : infixexp srcloc opt_sig rhs           {% (checkValDef $1 $3 $4 $2) }
856         | infixexp srcloc '::' sigtype          {% (checkValSig $1 $4 $2) }
857         | var ',' sig_vars srcloc '::' sigtype  { foldr1 RdrAndBindings 
858                                                          [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
859                                                 }
860
861
862 rhs     :: { RdrNameGRHSs }
863         : '=' srcloc exp wherebinds     { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
864         | gdrhs wherebinds              { GRHSs (reverse $1) $2 placeHolderType }
865
866 gdrhs :: { [RdrNameGRHS] }
867         : gdrhs gdrh                    { $2 : $1 }
868         | gdrh                          { [$1] }
869
870 gdrh :: { RdrNameGRHS }
871         : '|' srcloc quals '=' exp      { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
872
873 -----------------------------------------------------------------------------
874 -- Expressions
875
876 exp   :: { RdrNameHsExpr }
877         : infixexp '::' sigtype         { (ExprWithTySig $1 $3) }
878         | infixexp 'with' dbinding      { HsWith $1 $3 }
879         | infixexp                      { $1 }
880
881 infixexp :: { RdrNameHsExpr }
882         : exp10                         { $1 }
883         | infixexp qop exp10            { (OpApp $1 (HsVar $2) 
884                                                 (panic "fixity") $3 )}
885
886 exp10 :: { RdrNameHsExpr }
887         : '\\' srcloc aexp aexps opt_asig '->' srcloc exp       
888                         {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps -> 
889                            returnP (HsLam (Match ps $5 
890                                             (GRHSs (unguardedRHS $8 $7) 
891                                                    EmptyBinds placeHolderType))) }
892         | 'let' declbinds 'in' exp              { HsLet $2 $4 }
893         | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
894         | 'case' srcloc exp 'of' altslist       { HsCase $3 $5 $2 }
895         | '-' fexp                              { mkHsNegApp $2 }
896         | srcloc 'do' stmtlist                  {% checkDo $3  `thenP` \ stmts ->
897                                                    returnP (HsDo DoExpr stmts $1) }
898
899         | '_ccall_'    ccallid aexps0           { HsCCall $2 $3 PlayRisky False placeHolderType }
900         | '_ccall_GC_' ccallid aexps0           { HsCCall $2 $3 PlaySafe  False placeHolderType }
901         | '_casm_'     CLITLIT aexps0           { HsCCall $2 $3 PlayRisky True  placeHolderType }
902         | '_casm_GC_'  CLITLIT aexps0           { HsCCall $2 $3 PlaySafe  True  placeHolderType }
903
904         | scc_annot exp                         { if opt_SccProfilingOn
905                                                         then HsSCC $1 $2
906                                                         else HsPar $2 }
907
908         | fexp                                  { $1 }
909
910 scc_annot :: { FAST_STRING }
911         : '_scc_' STRING                        { $2 }
912         | '{-# SCC' STRING '#-}'                { $2 }
913
914 ccallid :: { FAST_STRING }
915         :  VARID                                { $1 }
916         |  CONID                                { $1 }
917
918 fexp    :: { RdrNameHsExpr }
919         : fexp aexp                             { (HsApp $1 $2) }
920         | aexp                                  { $1 }
921
922 aexps0  :: { [RdrNameHsExpr] }
923         : aexps                                 { reverse $1 }
924
925 aexps   :: { [RdrNameHsExpr] }
926         : aexps aexp                            { $2 : $1 }
927         | {- empty -}                           { [] }
928
929 aexp    :: { RdrNameHsExpr }
930         : var_or_con '{|' gentype '|}'          { (HsApp $1 (HsType $3)) }
931         | aexp '{' fbinds '}'                   {% (mkRecConstrOrUpdate $1 
932                                                         (reverse $3)) }
933         | aexp1                                 { $1 }
934
935 var_or_con :: { RdrNameHsExpr }
936         : qvar                          { HsVar $1 }
937         | gcon                          { HsVar $1 }
938
939 aexp1   :: { RdrNameHsExpr }
940         : ipvar                         { HsIPVar $1 }
941         | var_or_con                    { $1 }
942         | literal                       { HsLit $1 }
943         | INTEGER                       { HsOverLit (mkHsIntegral   $1) }
944         | RATIONAL                      { HsOverLit (mkHsFractional $1) }
945         | '(' exp ')'                   { HsPar $2 }
946         | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) Boxed}
947         | '(#' texps '#)'               { ExplicitTuple (reverse $2)      Unboxed }
948         | '[' list ']'                  { $2 }
949         | '[:' parr ':]'                { $2 }
950         | '(' infixexp qop ')'          { (SectionL $2 (HsVar $3))  }
951         | '(' qopm infixexp ')'         { (SectionR $2 $3) }
952         | qvar '@' aexp                 { EAsPat $1 $3 }
953         | '_'                           { EWildPat }
954         | '~' aexp1                     { ELazyPat $2 }
955
956 texps :: { [RdrNameHsExpr] }
957         : texps ',' exp                 { $3 : $1 }
958         | exp                           { [$1] }
959
960
961 -----------------------------------------------------------------------------
962 -- List expressions
963
964 -- The rules below are little bit contorted to keep lexps left-recursive while
965 -- avoiding another shift/reduce-conflict.
966
967 list :: { RdrNameHsExpr }
968         : exp                           { ExplicitList placeHolderType [$1] }
969         | lexps                         { ExplicitList placeHolderType (reverse $1) }
970         | exp '..'                      { ArithSeqIn (From $1) }
971         | exp ',' exp '..'              { ArithSeqIn (FromThen $1 $3) }
972         | exp '..' exp                  { ArithSeqIn (FromTo $1 $3) }
973         | exp ',' exp '..' exp          { ArithSeqIn (FromThenTo $1 $3 $5) }
974         | exp srcloc pquals             {% let { body [qs] = qs;
975                                                  body  qss = [ParStmt (map reverse qss)] }
976                                            in
977                                            returnP ( HsDo ListComp
978                                                            (reverse (ResultStmt $1 $2 : body $3))
979                                                            $2
980                                                   )
981                                         }
982
983 lexps :: { [RdrNameHsExpr] }
984         : lexps ',' exp                 { $3 : $1 }
985         | exp ',' exp                   { [$3,$1] }
986
987 -----------------------------------------------------------------------------
988 -- List Comprehensions
989
990 pquals :: { [[RdrNameStmt]] }
991         : pquals '|' quals              { $3 : $1 }
992         | '|' quals                     { [$2] }
993
994 quals :: { [RdrNameStmt] }
995         : quals ',' stmt                { $3 : $1 }
996         | stmt                          { [$1] }
997
998 -----------------------------------------------------------------------------
999 -- Parallel array expressions
1000
1001 -- The rules below are little bit contorted; see the list case for details.
1002 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1003 -- Moreover, we allow explicit arrays with no element (represented by the nil
1004 -- constructor in the list case).
1005
1006 parr :: { RdrNameHsExpr }
1007         :                               { ExplicitPArr placeHolderType [] }
1008         | exp                           { ExplicitPArr placeHolderType [$1] }
1009         | lexps                         { ExplicitPArr placeHolderType 
1010                                                        (reverse $1) }
1011         | exp '..' exp                  { PArrSeqIn (FromTo $1 $3) }
1012         | exp ',' exp '..' exp          { PArrSeqIn (FromThenTo $1 $3 $5) }
1013         | exp srcloc pquals             {% let {
1014                                              body [qs] = qs;
1015                                              body  qss = [ParStmt 
1016                                                            (map reverse qss)]}
1017                                            in
1018                                            returnP $ 
1019                                              HsDo PArrComp 
1020                                                   (reverse (ResultStmt $1 $2 
1021                                                             : body $3))
1022                                                   $2
1023                                         }
1024
1025 -- We are reusing `lexps' and `pquals' from the list case.
1026
1027 -----------------------------------------------------------------------------
1028 -- Case alternatives
1029
1030 altslist :: { [RdrNameMatch] }
1031         : '{'            alts '}'       { reverse $2 }
1032         |     layout_on  alts  close    { reverse $2 }
1033
1034 alts    :: { [RdrNameMatch] }
1035         : alts1                         { $1 }
1036         | ';' alts                      { $2 }
1037
1038 alts1   :: { [RdrNameMatch] }
1039         : alts1 ';' alt                 { $3 : $1 }
1040         | alts1 ';'                     { $1 }
1041         | alt                           { [$1] }
1042
1043 alt     :: { RdrNameMatch }
1044         : srcloc infixexp opt_sig ralt wherebinds
1045                                         {% (checkPattern $1 $2 `thenP` \p ->
1046                                            returnP (Match [p] $3
1047                                                      (GRHSs $4 $5 placeHolderType))  )}
1048
1049 ralt :: { [RdrNameGRHS] }
1050         : '->' srcloc exp               { [GRHS [ResultStmt $3 $2] $2] }
1051         | gdpats                        { reverse $1 }
1052
1053 gdpats :: { [RdrNameGRHS] }
1054         : gdpats gdpat                  { $2 : $1 }
1055         | gdpat                         { [$1] }
1056
1057 gdpat   :: { RdrNameGRHS }
1058         : srcloc '|' quals '->' exp     { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
1059
1060 -----------------------------------------------------------------------------
1061 -- Statement sequences
1062
1063 stmtlist :: { [RdrNameStmt] }
1064         : '{'                   stmts '}'       { $2 }
1065         |     layout_on_for_do  stmts close     { $2 }
1066
1067 --      do { ;; s ; s ; ; s ;; }
1068 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1069 -- here, because we need too much lookahead if we see do { e ; }
1070 -- So we use ExprStmts throughout, and switch the last one over
1071 -- in ParseUtils.checkDo instead
1072 stmts :: { [RdrNameStmt] }
1073         : stmt stmts_help               { $1 : $2 }
1074         | ';' stmts                     { $2 }
1075         | {- empty -}                   { [] }
1076
1077 stmts_help :: { [RdrNameStmt] }
1078         : ';' stmts                     { $2 }
1079         | {- empty -}                   { [] }
1080
1081 -- For typing stmts at the GHCi prompt, where 
1082 -- the input may consist of just comments.
1083 maybe_stmt :: { Maybe RdrNameStmt }
1084         : stmt                          { Just $1 }
1085         | {- nothing -}                 { Nothing }
1086
1087 stmt  :: { RdrNameStmt }
1088         : srcloc infixexp '<-' exp      {% checkPattern $1 $2 `thenP` \p ->
1089                                            returnP (BindStmt p $4 $1) }
1090         | srcloc exp                    { ExprStmt $2 placeHolderType $1 }
1091         | srcloc 'let' declbinds        { LetStmt $3 }
1092
1093 -----------------------------------------------------------------------------
1094 -- Record Field Update/Construction
1095
1096 fbinds  :: { RdrNameHsRecordBinds }
1097         : fbinds ',' fbind              { $3 : $1 }
1098         | fbinds ','                    { $1 }
1099         | fbind                         { [$1] }
1100         | {- empty -}                   { [] }
1101
1102 fbind   :: { (RdrName, RdrNameHsExpr, Bool) }
1103         : qvar '=' exp                  { ($1,$3,False) }
1104
1105 -----------------------------------------------------------------------------
1106 -- Implicit Parameter Bindings
1107
1108 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1109         : '{' dbinds '}'                { $2 }
1110         | layout_on dbinds close        { $2 }
1111
1112 dbinds  :: { [(IPName RdrName, RdrNameHsExpr)] }
1113         : dbinds ';' dbind              { $3 : $1 }
1114         | dbinds ';'                    { $1 }
1115         | dbind                         { [$1] }
1116         | {- empty -}                   { [] }
1117
1118 dbind   :: { (IPName RdrName, RdrNameHsExpr) }
1119 dbind   : ipvar '=' exp                 { ($1, $3) }
1120
1121 -----------------------------------------------------------------------------
1122 -- Variables, Constructors and Operators.
1123
1124 identifier :: { RdrName }
1125         : qvar                          { $1 }
1126         | gcon                          { $1 }
1127         | qop                           { $1 }
1128
1129 depreclist :: { [RdrName] }
1130 depreclist : deprec_var                 { [$1] }
1131            | deprec_var ',' depreclist  { $1 : $3 }
1132
1133 deprec_var :: { RdrName }
1134 deprec_var : var                        { $1 }
1135            | tycon                      { $1 }
1136
1137 gtycon  :: { RdrName }
1138         : qtycon                        { $1 }
1139         | '(' qtyconop ')'              { $2 }
1140         | '(' ')'                       { unitTyCon_RDR }
1141         | '(' '->' ')'                  { funTyCon_RDR }
1142         | '[' ']'                       { listTyCon_RDR }
1143         | '[:' ':]'                     { parrTyCon_RDR }
1144         | '(' commas ')'                { tupleTyCon_RDR $2 }
1145
1146 gcon    :: { RdrName }  -- Data constructor namespace
1147         : '(' ')'               { unitCon_RDR }
1148         | '[' ']'               { nilCon_RDR }
1149         | '(' commas ')'        { tupleCon_RDR $2 }
1150         | qcon                  { $1 }
1151 -- the case of '[:' ':]' is part of the production `parr'
1152
1153 var     :: { RdrName }
1154         : varid                 { $1 }
1155         | '(' varsym ')'        { $2 }
1156
1157 qvar    :: { RdrName }
1158         : qvarid                { $1 }
1159         | '(' varsym ')'        { $2 }
1160         | '(' qvarsym1 ')'      { $2 }
1161 -- We've inlined qvarsym here so that the decision about
1162 -- whether it's a qvar or a var can be postponed until
1163 -- *after* we see the close paren.
1164
1165 ipvar   :: { IPName RdrName }
1166         : IPDUPVARID            { Dupable (mkUnqual varName $1) }
1167         | IPSPLITVARID          { Linear  (mkUnqual varName $1) }
1168
1169 qcon    :: { RdrName }
1170         : qconid                { $1 }
1171         | '(' qconsym ')'       { $2 }
1172
1173 varop   :: { RdrName }
1174         : varsym                { $1 }
1175         | '`' varid '`'         { $2 }
1176
1177 qvarop :: { RdrName }
1178         : qvarsym               { $1 }
1179         | '`' qvarid '`'        { $2 }
1180
1181 qvaropm :: { RdrName }
1182         : qvarsym_no_minus      { $1 }
1183         | '`' qvarid '`'        { $2 }
1184
1185 conop :: { RdrName }
1186         : consym                { $1 }  
1187         | '`' conid '`'         { $2 }
1188
1189 qconop :: { RdrName }
1190         : qconsym               { $1 }
1191         | '`' qconid '`'        { $2 }
1192
1193 -----------------------------------------------------------------------------
1194 -- Any operator
1195
1196 op      :: { RdrName }   -- used in infix decls
1197         : varop                 { $1 }
1198         | conop                 { $1 }
1199
1200 qop     :: { RdrName {-HsExpr-} }   -- used in sections
1201         : qvarop                { $1 }
1202         | qconop                { $1 }
1203
1204 qopm    :: { RdrNameHsExpr }   -- used in sections
1205         : qvaropm               { HsVar $1 }
1206         | qconop                { HsVar $1 }
1207
1208 -----------------------------------------------------------------------------
1209 -- VarIds
1210
1211 qvarid :: { RdrName }
1212         : varid                 { $1 }
1213         | QVARID                { mkQual varName $1 }
1214
1215 varid :: { RdrName }
1216         : varid_no_unsafe       { $1 }
1217         | 'unsafe'              { mkUnqual varName SLIT("unsafe") }
1218
1219 varid_no_unsafe :: { RdrName }
1220         : VARID                 { mkUnqual varName $1 }
1221         | special_id            { mkUnqual varName $1 }
1222         | 'forall'              { mkUnqual varName SLIT("forall") }
1223
1224 tyvar   :: { RdrName }
1225         : VARID                 { mkUnqual tvName $1 }
1226         | special_id            { mkUnqual tvName $1 }
1227         | 'unsafe'              { mkUnqual tvName SLIT("unsafe") }
1228
1229 -- These special_ids are treated as keywords in various places, 
1230 -- but as ordinary ids elsewhere.   A special_id collects all thsee
1231 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1232 special_id :: { UserFS }
1233 special_id
1234         : 'as'                  { SLIT("as") }
1235         | 'qualified'           { SLIT("qualified") }
1236         | 'hiding'              { SLIT("hiding") }
1237         | 'export'              { SLIT("export") }
1238         | 'label'               { SLIT("label")  }
1239         | 'dynamic'             { SLIT("dynamic") }
1240         | 'stdcall'             { SLIT("stdcall") }
1241         | 'ccall'               { SLIT("ccall") }
1242
1243 -----------------------------------------------------------------------------
1244 -- ConIds
1245
1246 qconid :: { RdrName }   -- Qualified or unqualifiedb
1247         : conid                 { $1 }
1248         | QCONID                { mkQual dataName $1 }
1249
1250 conid   :: { RdrName }
1251         : CONID                 { mkUnqual dataName $1 }
1252
1253 -----------------------------------------------------------------------------
1254 -- ConSyms
1255
1256 qconsym :: { RdrName }  -- Qualified or unqualifiedb
1257         : consym                { $1 }
1258         | QCONSYM               { mkQual dataName $1 }
1259
1260 consym :: { RdrName }
1261         : CONSYM                { mkUnqual dataName $1 }
1262
1263 -----------------------------------------------------------------------------
1264 -- VarSyms
1265
1266 qvarsym :: { RdrName }
1267         : varsym                { $1 }
1268         | qvarsym1              { $1 }
1269
1270 qvarsym_no_minus :: { RdrName }
1271         : varsym_no_minus       { $1 }
1272         | qvarsym1              { $1 }
1273
1274 qvarsym1 :: { RdrName }
1275 qvarsym1 : QVARSYM              { mkQual varName $1 }
1276
1277 varsym :: { RdrName }
1278         : varsym_no_minus       { $1 }
1279         | '-'                   { mkUnqual varName SLIT("-") }
1280
1281 varsym_no_minus :: { RdrName } -- varsym not including '-'
1282         : VARSYM                { mkUnqual varName $1 }
1283         | special_sym           { mkUnqual varName $1 }
1284
1285
1286 -- See comments with special_id
1287 special_sym :: { UserFS }
1288 special_sym : '!'       { SLIT("!") }
1289             | '.'       { SLIT(".") }
1290             | '*'       { SLIT("*") }
1291
1292 -----------------------------------------------------------------------------
1293 -- Literals
1294
1295 literal :: { HsLit }
1296         : CHAR                  { HsChar       $1 }
1297         | STRING                { HsString     $1 }
1298         | PRIMINTEGER           { HsIntPrim    $1 }
1299         | PRIMCHAR              { HsCharPrim   $1 }
1300         | PRIMSTRING            { HsStringPrim $1 }
1301         | PRIMFLOAT             { HsFloatPrim  $1 }
1302         | PRIMDOUBLE            { HsDoublePrim $1 }
1303         | CLITLIT               { HsLitLit     $1 placeHolderType }
1304
1305 srcloc :: { SrcLoc }    :       {% getSrcLocP }
1306  
1307 -----------------------------------------------------------------------------
1308 -- Layout
1309
1310 close :: { () }
1311         : vccurly               { () } -- context popped in lexer.
1312         | error                 {% popContext }
1313
1314 layout_on         :: { () }     : {% layoutOn True{-strict-} }
1315 layout_on_for_do  :: { () }     : {% layoutOn False }
1316
1317 -----------------------------------------------------------------------------
1318 -- Miscellaneous (mostly renamings)
1319
1320 modid   :: { ModuleName }
1321         : CONID                 { mkModuleNameFS $1 }
1322         | QCONID                { mkModuleNameFS
1323                                    (mkFastString
1324                                      (unpackFS (fst $1) ++ 
1325                                         '.':unpackFS (snd $1)))
1326                                 }
1327
1328 tycon   :: { RdrName }
1329         : CONID                 { mkUnqual tcClsName $1 }
1330
1331 tyconop :: { RdrName }
1332         : CONSYM                { mkUnqual tcClsName $1 }
1333
1334 qtycon :: { RdrName }   -- Qualified or unqualified
1335         : QCONID                { mkQual tcClsName $1 }
1336         | tycon                 { $1 }
1337
1338 qtyconop :: { RdrName } -- Qualified or unqualified
1339           : QCONSYM             { mkQual tcClsName $1 }
1340           | tyconop             { $1 }
1341
1342 commas :: { Int }
1343         : commas ','                    { $1 + 1 }
1344         | ','                           { 2 }
1345
1346 -----------------------------------------------------------------------------
1347
1348 {
1349 happyError :: P a
1350 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
1351 }