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