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