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