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