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