e98b1ff98283b5b033e7157c11a16e65a20430a6
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-                                                              -*-haskell-*-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.86 2002/02/11 15:16:26 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(..), 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 ')' '=>' tycon tv_bndrs      {% mapP checkPred $2    `thenP` \ cxt ->
393                                                            returnP (cxt, $5, $6) }
394         | qtycon atypes1 '=>' tycon atypes0             {% checkTyVars $5       `thenP` \ tvs ->
395                                                            returnP ([HsClassP $1 $2], $4, tvs) }
396         | qtycon  atypes0                               {% checkTyVars $2       `thenP` \ tvs ->
397                                                            returnP ([], $1, tvs) }
398                 -- We have to have qtycon in this production to avoid s/r conflicts
399                 -- with the previous one.  The renamer will complain if we use
400                 -- a qualified tycon.
401
402 decls   :: { [RdrBinding] }
403         : decls ';' decl                { $3 : $1 }
404         | decls ';'                     { $1 }
405         | decl                          { [$1] }
406         | {- empty -}                   { [] }
407
408 decl    :: { RdrBinding }
409         : fixdecl                       { $1 }
410         | valdef                        { $1 }
411         | '{-# INLINE'   srcloc activation qvar '#-}'         { RdrSig (InlineSig True  $4 $3 $2) }
412         | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' { RdrSig (InlineSig False $4 $3 $2) }
413         | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
414                 { foldr1 RdrAndBindings 
415                     (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
416         | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
417                 { RdrSig (SpecInstSig $4 $2) }
418
419 wherebinds :: { RdrNameHsBinds }
420         : where                 { cvBinds cvValSig (groupBindings $1) }
421
422 where   :: { [RdrBinding] }
423         : 'where' decllist              { $2 }
424         | {- empty -}                   { [] }
425
426 declbinds :: { RdrNameHsBinds }
427         : decllist                      { cvBinds cvValSig (groupBindings $1) }
428
429 decllist :: { [RdrBinding] }
430         : '{'            decls '}'      { $2 }
431         |     layout_on  decls close    { $2 }
432
433 fixdecl :: { RdrBinding }
434         : srcloc infix prec ops         { foldr1 RdrAndBindings
435                                             [ RdrSig (FixSig (FixitySig n 
436                                                             (Fixity $3 $2) $1))
437                                             | n <- $4 ] }
438
439 -----------------------------------------------------------------------------
440 -- Transformation Rules
441
442 rules   :: { RdrBinding }
443         :  rules ';' rule                       { $1 `RdrAndBindings` $3 }
444         |  rules ';'                            { $1 }
445         |  rule                                 { $1 }
446         |  {- empty -}                          { RdrNullBind }
447
448 rule    :: { RdrBinding }
449         : STRING activation rule_forall infixexp '=' srcloc exp
450              { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
451
452 activation :: { Activation }           -- Omitted means AlwaysActive
453         : {- empty -}                           { AlwaysActive }
454         | explicit_activation                   { $1 }
455
456 inverse_activation :: { Activation }   -- Omitted means NeverActive
457         : {- empty -}                           { NeverActive }
458         | explicit_activation                   { $1 }
459
460 explicit_activation :: { Activation }  -- In brackets
461         : '[' INTEGER ']'                       { ActiveAfter  (fromInteger $2) }
462         | '[' '~' INTEGER ']'                   { ActiveBefore (fromInteger $3) }
463
464 rule_forall :: { [RdrNameRuleBndr] }
465         : 'forall' rule_var_list '.'            { $2 }
466         | {- empty -}                           { [] }
467
468 rule_var_list :: { [RdrNameRuleBndr] }
469         : rule_var                              { [$1] }
470         | rule_var rule_var_list                { $1 : $2 }
471
472 rule_var :: { RdrNameRuleBndr }
473         : varid                                 { RuleBndr $1 }
474         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
475
476 -----------------------------------------------------------------------------
477 -- Deprecations
478
479 deprecations :: { RdrBinding }
480         : deprecations ';' deprecation          { $1 `RdrAndBindings` $3 }
481         | deprecations ';'                      { $1 }
482         | deprecation                           { $1 }
483         | {- empty -}                           { RdrNullBind }
484
485 -- SUP: TEMPORARY HACK, not checking for `module Foo'
486 deprecation :: { RdrBinding }
487         : srcloc depreclist STRING
488                 { foldr RdrAndBindings RdrNullBind 
489                         [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
490
491
492 -----------------------------------------------------------------------------
493 -- Foreign import and export declarations
494
495 -- for the time being, the following accepts foreign declarations conforming
496 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
497 --
498 -- * a flag indicates whether pre-standard declarations have been used and
499 --   triggers a deprecation warning further down the road
500 --
501 -- NB: The first two rules could be combined into one by replacing `safety1'
502 --     with `safety'.  However, the combined rule conflicts with the
503 --     DEPRECATED rules.
504 --
505 fdecl :: { RdrNameHsDecl }
506 fdecl : srcloc 'import' callconv safety1 fspec  {% mkImport $3 $4       $5 $1 }
507       | srcloc 'import' callconv         fspec  {% mkImport $3 PlaySafe $4 $1 }
508       | srcloc 'export' callconv         fspec  {% mkExport $3          $4 $1 }
509         -- the following syntax is DEPRECATED
510       | srcloc fdecl1DEPRECATED                 { ForD ($2 True $1) }
511       | srcloc fdecl2DEPRECATED                 { $2 $1 }
512
513 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
514 fdecl1DEPRECATED 
515   ----------- DEPRECATED label decls ------------
516   : 'label' ext_name varid '::' sigtype
517     { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_ 
518                                    (CLabel ($2 `orElse` mkExtName $3))) }
519
520   ----------- DEPRECATED ccall/stdcall decls ------------
521   --
522   -- NB: This business with the case expression below may seem overly
523   --     complicated, but it is necessary to avoid some conflicts.
524
525     -- DEPRECATED variant #1: lack of a calling convention specification
526     --                        (import) 
527   | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
528     { let
529         target = StaticTarget ($2 `orElse` mkExtName $4)
530       in
531       ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_ 
532                                    (CFunction target)) }
533
534     -- DEPRECATED variant #2: external name consists of two separate strings
535     --                        (module name and function name) (import)
536   | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
537     {% case $2 of
538          DNCall      -> parseError "Illegal format of .NET foreign import"
539          CCall cconv -> returnP $
540            let
541              imp = CFunction (StaticTarget $4)
542            in
543            ForeignImport $6 $8 (CImport cconv $5 _NIL_ _NIL_ imp) }
544
545     -- DEPRECATED variant #3: `unsafe' after entity
546   | 'import' callconv STRING 'unsafe' 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 $3)
552            in
553            ForeignImport $5 $7 (CImport cconv PlayRisky _NIL_ _NIL_ imp) }
554
555     -- DEPRECATED variant #4: use of the special identifier `dynamic' without
556     --                        an explicit calling convention (import)
557   | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
558     { ForeignImport $4 $6 (CImport defaultCCallConv $3 _NIL_ _NIL_ 
559                                    (CFunction DynamicTarget)) }
560
561     -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
562   | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
563     {% case $2 of
564          DNCall      -> parseError "Illegal format of .NET foreign import"
565          CCall cconv -> returnP $
566            ForeignImport $5 $7 (CImport cconv $4 _NIL_ _NIL_ 
567                                         (CFunction DynamicTarget)) }
568
569     -- DEPRECATED variant #6: lack of a calling convention specification
570     --                        (export) 
571   | 'export' {-no callconv-} ext_name varid '::' sigtype
572     { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3) 
573                                    defaultCCallConv)) }
574
575     -- DEPRECATED variant #7: external name consists of two separate strings
576     --                        (module name and function name) (export)
577   | 'export' callconv STRING STRING varid '::' sigtype
578     {% case $2 of
579          DNCall      -> parseError "Illegal format of .NET foreign import"
580          CCall cconv -> returnP $
581            ForeignExport $5 $7 
582                          (CExport (CExportStatic $4 cconv)) }
583
584     -- DEPRECATED variant #8: use of the special identifier `dynamic' without
585     --                        an explicit calling convention (export)
586   | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
587     { ForeignImport $3 $5 (CImport defaultCCallConv PlaySafe _NIL_ _NIL_ 
588                                    CWrapper) }
589
590     -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
591   | 'export' callconv 'dynamic' varid '::' sigtype
592     {% case $2 of
593          DNCall      -> parseError "Illegal format of .NET foreign import"
594          CCall cconv -> returnP $
595            ForeignImport $4 $6 (CImport cconv PlaySafe _NIL_ _NIL_ CWrapper) }
596
597   ----------- DEPRECATED .NET decls ------------
598   -- NB: removed the .NET call declaration, as it is entirely subsumed
599   --     by the new standard FFI declarations
600
601 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
602 fdecl2DEPRECATED 
603   : 'import' 'dotnet' 'type' ext_name tycon
604           { \loc -> TyClD (ForeignType $5 $4 DNType loc) }
605     -- left this one unchanged for the moment as type imports are not
606     -- covered currently by the FFI standard -=chak
607
608
609 callconv :: { CallConv }
610           : 'stdcall'                   { CCall  StdCallConv }
611           | 'ccall'                     { CCall  CCallConv   }
612           | 'dotnet'                    { DNCall             }
613
614 safety :: { Safety }
615         : 'unsafe'                      { PlayRisky }
616         | 'safe'                        { PlaySafe  }
617         | {- empty -}                   { PlaySafe  }
618
619 safety1 :: { Safety }
620         : 'unsafe'                      { PlayRisky }
621         | 'safe'                        { PlaySafe  }
622           -- only needed to avoid conflicts with the DEPRECATED rules
623
624 fspec :: { (FAST_STRING, RdrName, RdrNameHsType) }
625        : STRING varid '::' sigtype      { ($1      , $2, $4) }
626        |        varid '::' sigtype      { (SLIT(""), $1, $3) }
627          -- if the entity string is missing, it defaults to the empty string;
628          -- the meaning of an empty entity string depends on the calling
629          -- convention
630
631 -- DEPRECATED syntax
632 ext_name :: { Maybe CLabelString }
633         : STRING                { Just $1 }
634         | STRING STRING         { Just $2 }     -- Ignore "module name" for now
635         | {- empty -}           { Nothing }
636
637
638 -----------------------------------------------------------------------------
639 -- Type signatures
640
641 opt_sig :: { Maybe RdrNameHsType }
642         : {- empty -}                   { Nothing }
643         | '::' sigtype                  { Just $2 }
644
645 opt_asig :: { Maybe RdrNameHsType }
646         : {- empty -}                   { Nothing }
647         | '::' atype                    { Just $2 }
648
649 sigtypes :: { [RdrNameHsType] }
650         : sigtype                       { [ $1 ] }
651         | sigtypes ',' sigtype          { $3 : $1 }
652
653 sigtype :: { RdrNameHsType }
654         : ctype                         { mkHsForAllTy Nothing [] $1 }
655
656 sig_vars :: { [RdrName] }
657          : sig_vars ',' var             { $3 : $1 }
658          | var                          { [ $1 ] }
659
660 -----------------------------------------------------------------------------
661 -- Types
662
663 -- A ctype is a for-all type
664 ctype   :: { RdrNameHsType }
665         : 'forall' tv_bndrs '.' ctype   { mkHsForAllTy (Just $2) [] $4 }
666         | context '=>' type             { mkHsForAllTy Nothing   $1 $3 }
667         -- A type of form (context => type) is an *implicit* HsForAllTy
668         | type                          { $1 }
669
670 -- We parse a context as a btype so that we don't get reduce/reduce
671 -- errors in ctype.  The basic problem is that
672 --      (Eq a, Ord a)
673 -- looks so much like a tuple type.  We can't tell until we find the =>
674 context :: { RdrNameContext }
675         : btype                         {% checkContext $1 }
676
677 type :: { RdrNameHsType }
678         : gentype '->' type             { HsFunTy $1 $3 }
679         | ipvar '::' type               { mkHsIParamTy $1 $3 }
680         | gentype                       { $1 }
681
682 gentype :: { RdrNameHsType }
683         : btype                         { $1 }
684 -- Generics
685         | atype tyconop atype           { HsOpTy $1 $2 $3 }
686
687 btype :: { RdrNameHsType }
688         : btype atype                   { HsAppTy $1 $2 }
689         | atype                         { $1 }
690
691 atype :: { RdrNameHsType }
692         : gtycon                        { HsTyVar $1 }
693         | tyvar                         { HsTyVar $1 }
694         | '(' type ',' comma_types1 ')' { HsTupleTy (mkHsTupCon tcName Boxed  ($2:$4)) ($2:$4) }
695         | '(#' comma_types1 '#)'        { HsTupleTy (mkHsTupCon tcName Unboxed     $2) $2      }
696         | '[' type ']'                  { HsListTy $2 }
697         | '[:' type ':]'                { HsPArrTy $2 }
698         | '(' ctype ')'                 { $2 }
699         | '(' ctype '::' kind ')'       { HsKindSig $2 $4 }
700 -- Generics
701         | INTEGER                       { HsNumTy $1 }
702
703 -- An inst_type is what occurs in the head of an instance decl
704 --      e.g.  (Foo a, Gaz b) => Wibble a b
705 -- It's kept as a single type, with a MonoDictTy at the right
706 -- hand corner, for convenience.
707 inst_type :: { RdrNameHsType }
708         : ctype                         {% checkInstType $1 }
709
710 comma_types0  :: { [RdrNameHsType] }
711         : comma_types1                  { $1 }
712         | {- empty -}                   { [] }
713
714 comma_types1    :: { [RdrNameHsType] }
715         : type                          { [$1] }
716         | type  ',' comma_types1        { $1 : $3 }
717
718 atypes0 :: { [RdrNameHsType] }
719         : atypes1                       { $1 }
720         | {- empty -}                   { [] }
721
722 atypes1 :: { [RdrNameHsType] }
723         : atype                         { [$1] }
724         | atype atypes1                 { $1 : $2 }
725
726 tv_bndrs :: { [RdrNameHsTyVar] }
727          : tv_bndr tv_bndrs             { $1 : $2 }
728          | {- empty -}                  { [] }
729
730 tv_bndr :: { RdrNameHsTyVar }
731         : tyvar                         { UserTyVar $1 }
732         | '(' tyvar '::' kind ')'       { IfaceTyVar $2 $4 }
733
734 fds :: { [([RdrName], [RdrName])] }
735         : {- empty -}                   { [] }
736         | '|' fds1                      { reverse $2 }
737
738 fds1 :: { [([RdrName], [RdrName])] }
739         : fds1 ',' fd                   { $3 : $1 }
740         | fd                            { [$1] }
741
742 fd :: { ([RdrName], [RdrName]) }
743         : varids0 '->' varids0          { (reverse $1, reverse $3) }
744
745 varids0 :: { [RdrName] }
746         : {- empty -}                   { [] }
747         | varids0 tyvar                 { $2 : $1 }
748
749 -----------------------------------------------------------------------------
750 -- Kinds
751
752 kind    :: { Kind }
753         : akind                 { $1 }
754         | akind '->' kind       { mkArrowKind $1 $3 }
755
756 akind   :: { Kind }
757         : '*'                   { liftedTypeKind }
758         | '(' kind ')'          { $2 }
759
760
761 -----------------------------------------------------------------------------
762 -- Datatype declarations
763
764 newconstr :: { RdrNameConDecl }
765         : srcloc conid atype    { mkConDecl $2 [] [] (VanillaCon [unbangedType $3]) $1 }
766         | srcloc conid '{' var '::' ctype '}'
767                                 { mkConDecl $2 [] [] (RecCon [([$4], unbangedType $6)]) $1 }
768
769 constrs :: { [RdrNameConDecl] }
770         : {- empty; a GHC extension -}  { [] }
771         | '=' constrs1                  { $2 }
772
773 constrs1 :: { [RdrNameConDecl] }
774         : constrs1 '|' constr           { $3 : $1 }
775         | constr                        { [$1] }
776
777 constr :: { RdrNameConDecl }
778         : srcloc forall context '=>' constr_stuff
779                 { mkConDecl (fst $5) $2 $3 (snd $5) $1 }
780         | srcloc forall constr_stuff
781                 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
782
783 forall :: { [RdrNameHsTyVar] }
784         : 'forall' tv_bndrs '.'         { $2 }
785         | {- empty -}                   { [] }
786
787 constr_stuff :: { (RdrName, RdrNameConDetails) }
788         : btype                         {% mkVanillaCon $1 []               }
789         | btype '!' atype satypes       {% mkVanillaCon $1 (BangType MarkedUserStrict $3 : $4) }
790         | gtycon '{' '}'                {% mkRecCon $1 [] }
791         | gtycon '{' fielddecls '}'     {% mkRecCon $1 $3 }
792         | sbtype conop sbtype           { ($2, InfixCon $1 $3) }
793
794 satypes :: { [RdrNameBangType] }
795         : atype satypes                 { unbangedType $1 : $2 }
796         | '!' atype satypes             { BangType MarkedUserStrict $2 : $3 }
797         | {- empty -}                   { [] }
798
799 sbtype :: { RdrNameBangType }
800         : btype                         { unbangedType $1 }
801         | '!' atype                     { BangType MarkedUserStrict $2 }
802
803 fielddecls :: { [([RdrName],RdrNameBangType)] }
804         : fielddecl ',' fielddecls      { $1 : $3 }
805         | fielddecl                     { [$1] }
806
807 fielddecl :: { ([RdrName],RdrNameBangType) }
808         : sig_vars '::' stype           { (reverse $1, $3) }
809
810 stype :: { RdrNameBangType }
811         : ctype                         { unbangedType $1 }
812         | '!' atype                     { BangType MarkedUserStrict $2 }
813
814 deriving :: { Maybe RdrNameContext }
815         : {- empty -}                   { Nothing }
816         | 'deriving' context            { Just $2 }
817              -- Glasgow extension: allow partial 
818              -- applications in derivings
819
820 -----------------------------------------------------------------------------
821 -- Value definitions
822
823 {- There's an awkward overlap with a type signature.  Consider
824         f :: Int -> Int = ...rhs...
825    Then we can't tell whether it's a type signature or a value
826    definition with a result signature until we see the '='.
827    So we have to inline enough to postpone reductions until we know.
828 -}
829
830 {-
831   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
832   instead of qvar, we get another shift/reduce-conflict. Consider the
833   following programs:
834   
835      { (^^) :: Int->Int ; }          Type signature; only var allowed
836
837      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
838                                      qvar allowed (because of instance decls)
839   
840   We can't tell whether to reduce var to qvar until after we've read the signatures.
841 -}
842
843 valdef :: { RdrBinding }
844         : infixexp srcloc opt_sig rhs           {% (checkValDef $1 $3 $4 $2) }
845         | infixexp srcloc '::' sigtype          {% (checkValSig $1 $4 $2) }
846         | var ',' sig_vars srcloc '::' sigtype  { foldr1 RdrAndBindings 
847                                                          [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
848                                                 }
849
850
851 rhs     :: { RdrNameGRHSs }
852         : '=' srcloc exp wherebinds     { (GRHSs (unguardedRHS $3 $2) $4 placeHolderType)}
853         | gdrhs wherebinds              { GRHSs (reverse $1) $2 placeHolderType }
854
855 gdrhs :: { [RdrNameGRHS] }
856         : gdrhs gdrh                    { $2 : $1 }
857         | gdrh                          { [$1] }
858
859 gdrh :: { RdrNameGRHS }
860         : '|' srcloc quals '=' exp      { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
861
862 -----------------------------------------------------------------------------
863 -- Expressions
864
865 exp   :: { RdrNameHsExpr }
866         : infixexp '::' sigtype         { (ExprWithTySig $1 $3) }
867         | infixexp 'with' dbinding      { HsWith $1 $3 }
868         | infixexp                      { $1 }
869
870 infixexp :: { RdrNameHsExpr }
871         : exp10                         { $1 }
872         | infixexp qop exp10            { (OpApp $1 (HsVar $2) 
873                                                 (panic "fixity") $3 )}
874
875 exp10 :: { RdrNameHsExpr }
876         : '\\' srcloc aexp aexps opt_asig '->' srcloc exp       
877                         {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps -> 
878                            returnP (HsLam (Match ps $5 
879                                             (GRHSs (unguardedRHS $8 $7) 
880                                                    EmptyBinds placeHolderType))) }
881         | 'let' declbinds 'in' exp              { HsLet $2 $4 }
882         | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
883         | 'case' srcloc exp 'of' altslist       { HsCase $3 $5 $2 }
884         | '-' fexp                              { mkHsNegApp $2 }
885         | srcloc 'do' stmtlist                  {% checkDo $3  `thenP` \ stmts ->
886                                                    returnP (HsDo DoExpr stmts $1) }
887
888         | '_ccall_'    ccallid aexps0           { HsCCall $2 $3 PlayRisky False placeHolderType }
889         | '_ccall_GC_' ccallid aexps0           { HsCCall $2 $3 PlaySafe  False placeHolderType }
890         | '_casm_'     CLITLIT aexps0           { HsCCall $2 $3 PlayRisky True  placeHolderType }
891         | '_casm_GC_'  CLITLIT aexps0           { HsCCall $2 $3 PlaySafe  True  placeHolderType }
892
893         | scc_annot exp                         { if opt_SccProfilingOn
894                                                         then HsSCC $1 $2
895                                                         else HsPar $2 }
896
897         | fexp                                  { $1 }
898
899 scc_annot :: { FAST_STRING }
900         : '_scc_' STRING                        { $2 }
901         | '{-# SCC' STRING '#-}'                { $2 }
902
903 ccallid :: { FAST_STRING }
904         :  VARID                                { $1 }
905         |  CONID                                { $1 }
906
907 fexp    :: { RdrNameHsExpr }
908         : fexp aexp                             { (HsApp $1 $2) }
909         | aexp                                  { $1 }
910
911 aexps0  :: { [RdrNameHsExpr] }
912         : aexps                                 { reverse $1 }
913
914 aexps   :: { [RdrNameHsExpr] }
915         : aexps aexp                            { $2 : $1 }
916         | {- empty -}                           { [] }
917
918 aexp    :: { RdrNameHsExpr }
919         : var_or_con '{|' gentype '|}'          { (HsApp $1 (HsType $3)) }
920         | aexp '{' fbinds '}'                   {% (mkRecConstrOrUpdate $1 
921                                                         (reverse $3)) }
922         | aexp1                                 { $1 }
923
924 var_or_con :: { RdrNameHsExpr }
925         : qvar                          { HsVar $1 }
926         | gcon                          { HsVar $1 }
927
928 aexp1   :: { RdrNameHsExpr }
929         : ipvar                         { HsIPVar $1 }
930         | var_or_con                    { $1 }
931         | literal                       { HsLit $1 }
932         | INTEGER                       { HsOverLit (mkHsIntegral   $1) }
933         | RATIONAL                      { HsOverLit (mkHsFractional $1) }
934         | '(' exp ')'                   { HsPar $2 }
935         | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) Boxed}
936         | '(#' texps '#)'               { ExplicitTuple (reverse $2)      Unboxed }
937         | '[' list ']'                  { $2 }
938         | '[:' parr ':]'                { $2 }
939         | '(' infixexp qop ')'          { (SectionL $2 (HsVar $3))  }
940         | '(' qopm infixexp ')'         { (SectionR $2 $3) }
941         | qvar '@' aexp                 { EAsPat $1 $3 }
942         | '_'                           { EWildPat }
943         | '~' aexp1                     { ELazyPat $2 }
944
945 texps :: { [RdrNameHsExpr] }
946         : texps ',' exp                 { $3 : $1 }
947         | exp                           { [$1] }
948
949
950 -----------------------------------------------------------------------------
951 -- List expressions
952
953 -- The rules below are little bit contorted to keep lexps left-recursive while
954 -- avoiding another shift/reduce-conflict.
955
956 list :: { RdrNameHsExpr }
957         : exp                           { ExplicitList placeHolderType [$1] }
958         | lexps                         { ExplicitList placeHolderType (reverse $1) }
959         | exp '..'                      { ArithSeqIn (From $1) }
960         | exp ',' exp '..'              { ArithSeqIn (FromThen $1 $3) }
961         | exp '..' exp                  { ArithSeqIn (FromTo $1 $3) }
962         | exp ',' exp '..' exp          { ArithSeqIn (FromThenTo $1 $3 $5) }
963         | exp srcloc pquals             {% let { body [qs] = qs;
964                                                  body  qss = [ParStmt (map reverse qss)] }
965                                            in
966                                            returnP ( HsDo ListComp
967                                                            (reverse (ResultStmt $1 $2 : body $3))
968                                                            $2
969                                                   )
970                                         }
971
972 lexps :: { [RdrNameHsExpr] }
973         : lexps ',' exp                 { $3 : $1 }
974         | exp ',' exp                   { [$3,$1] }
975
976 -----------------------------------------------------------------------------
977 -- List Comprehensions
978
979 pquals :: { [[RdrNameStmt]] }
980         : pquals '|' quals              { $3 : $1 }
981         | '|' quals                     { [$2] }
982
983 quals :: { [RdrNameStmt] }
984         : quals ',' stmt                { $3 : $1 }
985         | stmt                          { [$1] }
986
987 -----------------------------------------------------------------------------
988 -- Parallel array expressions
989
990 -- The rules below are little bit contorted; see the list case for details.
991 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
992 -- Moreover, we allow explicit arrays with no element (represented by the nil
993 -- constructor in the list case).
994
995 parr :: { RdrNameHsExpr }
996         :                               { ExplicitPArr placeHolderType [] }
997         | exp                           { ExplicitPArr placeHolderType [$1] }
998         | lexps                         { ExplicitPArr placeHolderType 
999                                                        (reverse $1) }
1000         | exp '..' exp                  { PArrSeqIn (FromTo $1 $3) }
1001         | exp ',' exp '..' exp          { PArrSeqIn (FromThenTo $1 $3 $5) }
1002         | exp srcloc pquals             {% let {
1003                                              body [qs] = qs;
1004                                              body  qss = [ParStmt 
1005                                                            (map reverse qss)]}
1006                                            in
1007                                            returnP $ 
1008                                              HsDo PArrComp 
1009                                                   (reverse (ResultStmt $1 $2 
1010                                                             : body $3))
1011                                                   $2
1012                                         }
1013
1014 -- We are reusing `lexps' and `pquals' from the list case.
1015
1016 -----------------------------------------------------------------------------
1017 -- Case alternatives
1018
1019 altslist :: { [RdrNameMatch] }
1020         : '{'            alts '}'       { reverse $2 }
1021         |     layout_on  alts  close    { reverse $2 }
1022
1023 alts    :: { [RdrNameMatch] }
1024         : alts1                         { $1 }
1025         | ';' alts                      { $2 }
1026
1027 alts1   :: { [RdrNameMatch] }
1028         : alts1 ';' alt                 { $3 : $1 }
1029         | alts1 ';'                     { $1 }
1030         | alt                           { [$1] }
1031
1032 alt     :: { RdrNameMatch }
1033         : srcloc infixexp opt_sig ralt wherebinds
1034                                         {% (checkPattern $1 $2 `thenP` \p ->
1035                                            returnP (Match [p] $3
1036                                                      (GRHSs $4 $5 placeHolderType))  )}
1037
1038 ralt :: { [RdrNameGRHS] }
1039         : '->' srcloc exp               { [GRHS [ResultStmt $3 $2] $2] }
1040         | gdpats                        { reverse $1 }
1041
1042 gdpats :: { [RdrNameGRHS] }
1043         : gdpats gdpat                  { $2 : $1 }
1044         | gdpat                         { [$1] }
1045
1046 gdpat   :: { RdrNameGRHS }
1047         : srcloc '|' quals '->' exp     { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
1048
1049 -----------------------------------------------------------------------------
1050 -- Statement sequences
1051
1052 stmtlist :: { [RdrNameStmt] }
1053         : '{'                   stmts '}'       { $2 }
1054         |     layout_on_for_do  stmts close     { $2 }
1055
1056 --      do { ;; s ; s ; ; s ;; }
1057 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1058 -- here, because we need too much lookahead if we see do { e ; }
1059 -- So we use ExprStmts throughout, and switch the last one over
1060 -- in ParseUtils.checkDo instead
1061 stmts :: { [RdrNameStmt] }
1062         : stmt stmts_help               { $1 : $2 }
1063         | ';' stmts                     { $2 }
1064         | {- empty -}                   { [] }
1065
1066 stmts_help :: { [RdrNameStmt] }
1067         : ';' stmts                     { $2 }
1068         | {- empty -}                   { [] }
1069
1070 -- For typing stmts at the GHCi prompt, where 
1071 -- the input may consist of just comments.
1072 maybe_stmt :: { Maybe RdrNameStmt }
1073         : stmt                          { Just $1 }
1074         | {- nothing -}                 { Nothing }
1075
1076 stmt  :: { RdrNameStmt }
1077         : srcloc infixexp '<-' exp      {% checkPattern $1 $2 `thenP` \p ->
1078                                            returnP (BindStmt p $4 $1) }
1079         | srcloc exp                    { ExprStmt $2 placeHolderType $1 }
1080         | srcloc 'let' declbinds        { LetStmt $3 }
1081
1082 -----------------------------------------------------------------------------
1083 -- Record Field Update/Construction
1084
1085 fbinds  :: { RdrNameHsRecordBinds }
1086         : fbinds ',' fbind              { $3 : $1 }
1087         | fbinds ','                    { $1 }
1088         | fbind                         { [$1] }
1089         | {- empty -}                   { [] }
1090
1091 fbind   :: { (RdrName, RdrNameHsExpr, Bool) }
1092         : qvar '=' exp                  { ($1,$3,False) }
1093
1094 -----------------------------------------------------------------------------
1095 -- Implicit Parameter Bindings
1096
1097 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1098         : '{' dbinds '}'                { $2 }
1099         | layout_on dbinds close        { $2 }
1100
1101 dbinds  :: { [(IPName RdrName, RdrNameHsExpr)] }
1102         : dbinds ';' dbind              { $3 : $1 }
1103         | dbinds ';'                    { $1 }
1104         | dbind                         { [$1] }
1105         | {- empty -}                   { [] }
1106
1107 dbind   :: { (IPName RdrName, RdrNameHsExpr) }
1108 dbind   : ipvar '=' exp                 { ($1, $3) }
1109
1110 -----------------------------------------------------------------------------
1111 -- Variables, Constructors and Operators.
1112
1113 identifier :: { RdrName }
1114         : qvar                          { $1 }
1115         | gcon                          { $1 }
1116         | qop                           { $1 }
1117
1118 depreclist :: { [RdrName] }
1119 depreclist : deprec_var                 { [$1] }
1120            | deprec_var ',' depreclist  { $1 : $3 }
1121
1122 deprec_var :: { RdrName }
1123 deprec_var : var                        { $1 }
1124            | tycon                      { $1 }
1125
1126 gtycon  :: { RdrName }
1127         : qtycon                        { $1 }
1128         | '(' qtyconop ')'              { $2 }
1129         | '(' ')'                       { unitTyCon_RDR }
1130         | '(' '->' ')'                  { funTyCon_RDR }
1131         | '[' ']'                       { listTyCon_RDR }
1132         | '[:' ':]'                     { parrTyCon_RDR }
1133         | '(' commas ')'                { tupleTyCon_RDR $2 }
1134
1135 gcon    :: { RdrName }  -- Data constructor namespace
1136         : '(' ')'               { unitCon_RDR }
1137         | '[' ']'               { nilCon_RDR }
1138         | '(' commas ')'        { tupleCon_RDR $2 }
1139         | qcon                  { $1 }
1140 -- the case of '[:' ':]' is part of the production `parr'
1141
1142 var     :: { RdrName }
1143         : varid                 { $1 }
1144         | '(' varsym ')'        { $2 }
1145
1146 qvar    :: { RdrName }
1147         : qvarid                { $1 }
1148         | '(' varsym ')'        { $2 }
1149         | '(' qvarsym1 ')'      { $2 }
1150 -- We've inlined qvarsym here so that the decision about
1151 -- whether it's a qvar or a var can be postponed until
1152 -- *after* we see the close paren.
1153
1154 ipvar   :: { IPName RdrName }
1155         : IPDUPVARID            { Dupable (mkUnqual varName $1) }
1156         | IPSPLITVARID          { Linear  (mkUnqual varName $1) }
1157
1158 qcon    :: { RdrName }
1159         : qconid                { $1 }
1160         | '(' qconsym ')'       { $2 }
1161
1162 varop   :: { RdrName }
1163         : varsym                { $1 }
1164         | '`' varid '`'         { $2 }
1165
1166 qvarop :: { RdrName }
1167         : qvarsym               { $1 }
1168         | '`' qvarid '`'        { $2 }
1169
1170 qvaropm :: { RdrName }
1171         : qvarsym_no_minus      { $1 }
1172         | '`' qvarid '`'        { $2 }
1173
1174 conop :: { RdrName }
1175         : consym                { $1 }  
1176         | '`' conid '`'         { $2 }
1177
1178 qconop :: { RdrName }
1179         : qconsym               { $1 }
1180         | '`' qconid '`'        { $2 }
1181
1182 -----------------------------------------------------------------------------
1183 -- Any operator
1184
1185 op      :: { RdrName }   -- used in infix decls
1186         : varop                 { $1 }
1187         | conop                 { $1 }
1188
1189 qop     :: { RdrName {-HsExpr-} }   -- used in sections
1190         : qvarop                { $1 }
1191         | qconop                { $1 }
1192
1193 qopm    :: { RdrNameHsExpr }   -- used in sections
1194         : qvaropm               { HsVar $1 }
1195         | qconop                { HsVar $1 }
1196
1197 -----------------------------------------------------------------------------
1198 -- VarIds
1199
1200 qvarid :: { RdrName }
1201         : varid                 { $1 }
1202         | QVARID                { mkQual varName $1 }
1203
1204 varid :: { RdrName }
1205         : varid_no_unsafe       { $1 }
1206         | 'unsafe'              { mkUnqual varName SLIT("unsafe") }
1207
1208 varid_no_unsafe :: { RdrName }
1209         : VARID                 { mkUnqual varName $1 }
1210         | special_id            { mkUnqual varName $1 }
1211         | 'forall'              { mkUnqual varName SLIT("forall") }
1212
1213 tyvar   :: { RdrName }
1214         : VARID                 { mkUnqual tvName $1 }
1215         | special_id            { mkUnqual tvName $1 }
1216         | 'unsafe'              { mkUnqual tvName SLIT("unsafe") }
1217
1218 -- These special_ids are treated as keywords in various places, 
1219 -- but as ordinary ids elsewhere.   A special_id collects all thsee
1220 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1221 special_id :: { UserFS }
1222 special_id
1223         : 'as'                  { SLIT("as") }
1224         | 'qualified'           { SLIT("qualified") }
1225         | 'hiding'              { SLIT("hiding") }
1226         | 'export'              { SLIT("export") }
1227         | 'label'               { SLIT("label")  }
1228         | 'dynamic'             { SLIT("dynamic") }
1229         | 'stdcall'             { SLIT("stdcall") }
1230         | 'ccall'               { SLIT("ccall") }
1231
1232 -----------------------------------------------------------------------------
1233 -- ConIds
1234
1235 qconid :: { RdrName }   -- Qualified or unqualifiedb
1236         : conid                 { $1 }
1237         | QCONID                { mkQual dataName $1 }
1238
1239 conid   :: { RdrName }
1240         : CONID                 { mkUnqual dataName $1 }
1241
1242 -----------------------------------------------------------------------------
1243 -- ConSyms
1244
1245 qconsym :: { RdrName }  -- Qualified or unqualifiedb
1246         : consym                { $1 }
1247         | QCONSYM               { mkQual dataName $1 }
1248
1249 consym :: { RdrName }
1250         : CONSYM                { mkUnqual dataName $1 }
1251
1252 -----------------------------------------------------------------------------
1253 -- VarSyms
1254
1255 qvarsym :: { RdrName }
1256         : varsym                { $1 }
1257         | qvarsym1              { $1 }
1258
1259 qvarsym_no_minus :: { RdrName }
1260         : varsym_no_minus       { $1 }
1261         | qvarsym1              { $1 }
1262
1263 qvarsym1 :: { RdrName }
1264 qvarsym1 : QVARSYM              { mkQual varName $1 }
1265
1266 varsym :: { RdrName }
1267         : varsym_no_minus       { $1 }
1268         | '-'                   { mkUnqual varName SLIT("-") }
1269
1270 varsym_no_minus :: { RdrName } -- varsym not including '-'
1271         : VARSYM                { mkUnqual varName $1 }
1272         | special_sym           { mkUnqual varName $1 }
1273
1274
1275 -- See comments with special_id
1276 special_sym :: { UserFS }
1277 special_sym : '!'       { SLIT("!") }
1278             | '.'       { SLIT(".") }
1279             | '*'       { SLIT("*") }
1280
1281 -----------------------------------------------------------------------------
1282 -- Literals
1283
1284 literal :: { HsLit }
1285         : CHAR                  { HsChar       $1 }
1286         | STRING                { HsString     $1 }
1287         | PRIMINTEGER           { HsIntPrim    $1 }
1288         | PRIMCHAR              { HsCharPrim   $1 }
1289         | PRIMSTRING            { HsStringPrim $1 }
1290         | PRIMFLOAT             { HsFloatPrim  $1 }
1291         | PRIMDOUBLE            { HsDoublePrim $1 }
1292         | CLITLIT               { HsLitLit     $1 placeHolderType }
1293
1294 srcloc :: { SrcLoc }    :       {% getSrcLocP }
1295  
1296 -----------------------------------------------------------------------------
1297 -- Layout
1298
1299 close :: { () }
1300         : vccurly               { () } -- context popped in lexer.
1301         | error                 {% popContext }
1302
1303 layout_on         :: { () }     : {% layoutOn True{-strict-} }
1304 layout_on_for_do  :: { () }     : {% layoutOn False }
1305
1306 -----------------------------------------------------------------------------
1307 -- Miscellaneous (mostly renamings)
1308
1309 modid   :: { ModuleName }
1310         : CONID                 { mkModuleNameFS $1 }
1311         | QCONID                { mkModuleNameFS
1312                                    (mkFastString
1313                                      (unpackFS (fst $1) ++ 
1314                                         '.':unpackFS (snd $1)))
1315                                 }
1316
1317 tycon   :: { RdrName }
1318         : CONID                 { mkUnqual tcClsName $1 }
1319
1320 tyconop :: { RdrName }
1321         : CONSYM                { mkUnqual tcClsName $1 }
1322
1323 qtycon :: { RdrName }   -- Qualified or unqualified
1324         : QCONID                { mkQual tcClsName $1 }
1325         | tycon                 { $1 }
1326
1327 qtyconop :: { RdrName } -- Qualified or unqualified
1328           : QCONSYM             { mkQual tcClsName $1 }
1329           | tyconop             { $1 }
1330
1331 commas :: { Int }
1332         : commas ','                    { $1 + 1 }
1333         | ','                           { 2 }
1334
1335 -----------------------------------------------------------------------------
1336
1337 {
1338 happyError :: P a
1339 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
1340 }