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