5ca2359585c9419859a49a60f9b063638fd1133f
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-                                                              -*-haskell-*-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.118 2003/05/19 15:10:40 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                 {       -- Behave as if we'd said 
271                         --      module Main( main ) where ...
272                   let
273                         main_RDR_Unqual = mkUnqual varName FSLIT("main")
274                         -- We definitely don't want an Orig RdrName, because
275                         -- main might, in principle, be imported into module Main
276                   in
277                   HsModule (mkHomeModule mAIN_Name) 
278                            Nothing 
279                            (Just [IEVar main_RDR_Unqual])
280                            (fst $2) (snd $2) Nothing $1 }
281
282 maybemoddeprec :: { Maybe DeprecTxt }
283         : '{-# DEPRECATED' STRING '#-}'         { Just $2 }
284         |  {- empty -}                          { Nothing }
285
286 body    :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
287         :  '{'            top '}'               { $2 }
288         |      layout_on  top close             { $2 }
289
290 top     :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
291         : importdecls                           { (reverse $1,[]) }
292         | importdecls ';' cvtopdecls            { (reverse $1,$3) }
293         | cvtopdecls                            { ([],$1) }
294
295 cvtopdecls :: { [RdrNameHsDecl] }
296         : topdecls                      { cvTopDecls $1 }
297
298 -----------------------------------------------------------------------------
299 -- Interfaces (.hi-boot files)
300
301 iface   :: { ParsedIface }
302         : 'module' modid 'where' ifacebody
303           {         ParsedIface {
304                         pi_mod     = $2,
305                         pi_pkg     = opt_InPackage,
306                         pi_vers    = 1,                 -- Module version
307                         pi_orphan  = False,
308                         pi_exports = (1,[($2,mkIfaceExports $4)]),
309                         pi_deps    = noDependencies,
310                         pi_usages  = [],
311                         pi_fixity  = [],
312                         pi_insts   = [],
313                         pi_decls   = map (\x -> (1,x)) $4,
314                         pi_rules   = (1,[]),
315                         pi_deprecs = Nothing
316                     }
317            }
318
319 ifacebody :: { [RdrNameTyClDecl] }
320         :  '{'            ifacedecls '}'                { $2 }
321         |      layout_on  ifacedecls close              { $2 }
322
323 ifacedecls :: { [RdrNameTyClDecl] }
324         : ifacedecl ';' ifacedecls      { $1 : $3 }
325         | ';' ifacedecls                { $2 }
326         | ifacedecl                     { [$1] }
327         | {- empty -}                   { [] }
328
329 ifacedecl :: { RdrNameTyClDecl }
330         : tycl_decl                     { $1 }
331         | srcloc var '::' sigtype       { IfaceSig $2 $4 [] $1 }
332
333 -----------------------------------------------------------------------------
334 -- The Export List
335
336 maybeexports :: { Maybe [RdrNameIE] }
337         :  '(' exportlist ')'                   { Just $2 }
338         |  {- empty -}                          { Nothing }
339
340 exportlist :: { [RdrNameIE] }
341         :  exportlist ',' export                { $3 : $1 }
342         |  exportlist ','                       { $1 }
343         |  export                               { [$1]  }
344         |  {- empty -}                          { [] }
345
346    -- No longer allow things like [] and (,,,) to be exported
347    -- They are built in syntax, always available
348 export  :: { RdrNameIE }
349         :  qvar                                 { IEVar $1 }
350         |  oqtycon                              { IEThingAbs $1 }
351         |  oqtycon '(' '..' ')'                 { IEThingAll $1 }
352         |  oqtycon '(' ')'                      { IEThingWith $1 [] }
353         |  oqtycon '(' qcnames ')'              { IEThingWith $1 (reverse $3) }
354         |  'module' modid                       { IEModuleContents $2 }
355
356 qcnames :: { [RdrName] }
357         :  qcnames ',' qcname                   { $3 : $1 }
358         |  qcname                               { [$1]  }
359
360 qcname  :: { RdrName }  -- Variable or data constructor
361         :  qvar                                 { $1 }
362         |  gcon                                 { $1 }
363
364 -----------------------------------------------------------------------------
365 -- Import Declarations
366
367 -- import decls can be *empty*, or even just a string of semicolons
368 -- whereas topdecls must contain at least one topdecl.
369
370 importdecls :: { [RdrNameImportDecl] }
371         : importdecls ';' importdecl            { $3 : $1 }
372         | importdecls ';'                       { $1 }
373         | importdecl                            { [ $1 ] }
374         | {- empty -}                           { [] }
375
376 importdecl :: { RdrNameImportDecl }
377         : 'import' srcloc maybe_src optqualified modid maybeas maybeimpspec 
378                 { ImportDecl $5 $3 $4 $6 $7 $2 }
379
380 maybe_src :: { IsBootInterface }
381         : '{-# SOURCE' '#-}'                    { True }
382         | {- empty -}                           { False }
383
384 optqualified :: { Bool }
385         : 'qualified'                           { True  }
386         | {- empty -}                           { False }
387
388 maybeas :: { Maybe ModuleName }
389         : 'as' modid                            { Just $2 }
390         | {- empty -}                           { Nothing }
391
392 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
393         : impspec                               { Just $1 }
394         | {- empty -}                           { Nothing }
395
396 impspec :: { (Bool, [RdrNameIE]) }
397         :  '(' exportlist ')'                   { (False, reverse $2) }
398         |  'hiding' '(' exportlist ')'          { (True,  reverse $3) }
399
400 -----------------------------------------------------------------------------
401 -- Fixity Declarations
402
403 prec    :: { Int }
404         : {- empty -}                           { 9 }
405         | INTEGER                               {% checkPrecP (fromInteger $1) }
406
407 infix   :: { FixityDirection }
408         : 'infix'                               { InfixN  }
409         | 'infixl'                              { InfixL  }
410         | 'infixr'                              { InfixR }
411
412 ops     :: { [RdrName] }
413         : ops ',' op                            { $3 : $1 }
414         | op                                    { [$1] }
415
416 -----------------------------------------------------------------------------
417 -- Top-Level Declarations
418
419 topdecls :: { [RdrBinding] }    -- Reversed
420         : topdecls ';' topdecl          { $3 : $1 }
421         | topdecls ';'                  { $1 }
422         | topdecl                       { [$1] }
423
424 topdecl :: { RdrBinding }
425         : tycl_decl                     { RdrHsDecl (TyClD $1) }
426         | srcloc 'instance' inst_type where
427                 { let (binds,sigs) = cvMonoBindsAndSigs $4
428                   in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
429         | srcloc 'default' '(' comma_types0 ')'         { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
430         | 'foreign' fdecl                               { RdrHsDecl $2 }
431         | '{-# DEPRECATED' deprecations '#-}'           { RdrBindings (reverse $2) }
432         | '{-# RULES' rules '#-}'                       { RdrBindings (reverse $2) }
433         | srcloc '$(' exp ')'                           { RdrHsDecl (SpliceD (SpliceDecl $3 $1)) }
434         | decl                                          { $1 }
435
436 tycl_decl :: { RdrNameTyClDecl }
437         : srcloc 'type' syn_hdr '=' ctype       
438                 -- Note ctype, not sigtype.
439                 -- We allow an explicit for-all but we don't insert one
440                 -- in   type Foo a = (b,b)
441                 -- Instead we just say b is out of scope
442                 { let (tc,tvs) = $3 in TySynonym tc tvs $5 $1 }
443
444
445         | srcloc 'data' tycl_hdr constrs deriving
446                 { mkTyData DataType $3 (DataCons (reverse $4)) $5 $1 }
447
448         | srcloc 'newtype' tycl_hdr '=' newconstr deriving
449                 { mkTyData NewType $3 (DataCons [$5]) $6 $1 }
450
451         | srcloc 'class' tycl_hdr fds where
452                 { let 
453                         (binds,sigs) = cvMonoBindsAndSigs $5 
454                   in
455                   mkClassDecl $3 $4 sigs (Just binds) $1 }
456
457 syn_hdr :: { (RdrName, [RdrNameHsTyVar]) }      -- We don't retain the syntax of an infix
458                                                 -- type synonym declaration. Oh well.
459         : tycon tv_bndrs                { ($1, $2) }
460         | tv_bndr tyconop tv_bndr       { ($2, [$1,$3]) }
461
462 -- tycl_hdr parses the header of a type or class decl,
463 -- which takes the form
464 --      T a b
465 --      Eq a => T a
466 --      (Eq a, Ord b) => T a b
467 -- Rather a lot of inlining here, else we get reduce/reduce errors
468 tycl_hdr :: { (RdrNameContext, RdrName, [RdrNameHsTyVar]) }
469         : context '=>' type             {% checkTyClHdr $3      `thenP` \ (tc,tvs) ->
470                                            returnP ($1, tc, tvs) }
471         | type                          {% checkTyClHdr $1      `thenP` \ (tc,tvs) ->
472                                            returnP ([], tc, tvs) }
473
474 -----------------------------------------------------------------------------
475 -- Nested declarations
476
477 decls   :: { [RdrBinding] }     -- Reversed
478         : decls ';' decl                { $3 : $1 }
479         | decls ';'                     { $1 }
480         | decl                          { [$1] }
481         | {- empty -}                   { [] }
482
483
484 decllist :: { [RdrBinding] }    -- Reversed
485         : '{'            decls '}'      { $2 }
486         |     layout_on  decls close    { $2 }
487
488 where   :: { [RdrBinding] }     -- Reversed
489                                 -- No implicit parameters
490         : 'where' decllist              { $2 }
491         | {- empty -}                   { [] }
492
493 binds   ::  { RdrNameHsBinds }  -- May have implicit parameters
494         : decllist                      { cvBinds $1 }
495         | '{'            dbinds '}'     { IPBinds $2 False{-not with-} }
496         |     layout_on  dbinds close   { IPBinds $2 False{-not with-} }
497
498 wherebinds :: { RdrNameHsBinds }        -- May have implicit parameters
499         : 'where' binds                 { $2 }
500         | {- empty -}                   { EmptyBinds }
501
502
503
504 -----------------------------------------------------------------------------
505 -- Transformation Rules
506
507 rules   :: { [RdrBinding] }     -- Reversed
508         :  rules ';' rule                       { $3 : $1 }
509         |  rules ';'                            { $1 }
510         |  rule                                 { [$1] }
511         |  {- empty -}                          { [] }
512
513 rule    :: { RdrBinding }
514         : STRING activation rule_forall infixexp '=' srcloc exp
515              { RdrHsDecl (RuleD (HsRule $1 $2 $3 $4 $7 $6)) }
516
517 activation :: { Activation }           -- Omitted means AlwaysActive
518         : {- empty -}                           { AlwaysActive }
519         | explicit_activation                   { $1 }
520
521 inverse_activation :: { Activation }   -- Omitted means NeverActive
522         : {- empty -}                           { NeverActive }
523         | explicit_activation                   { $1 }
524
525 explicit_activation :: { Activation }  -- In brackets
526         : '[' INTEGER ']'                       { ActiveAfter  (fromInteger $2) }
527         | '[' '~' INTEGER ']'                   { ActiveBefore (fromInteger $3) }
528
529 rule_forall :: { [RdrNameRuleBndr] }
530         : 'forall' rule_var_list '.'            { $2 }
531         | {- empty -}                           { [] }
532
533 rule_var_list :: { [RdrNameRuleBndr] }
534         : rule_var                              { [$1] }
535         | rule_var rule_var_list                { $1 : $2 }
536
537 rule_var :: { RdrNameRuleBndr }
538         : varid                                 { RuleBndr $1 }
539         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
540
541 -----------------------------------------------------------------------------
542 -- Deprecations (c.f. rules)
543
544 deprecations :: { [RdrBinding] }        -- Reversed
545         : deprecations ';' deprecation          { $3 : $1 }
546         | deprecations ';'                      { $1 }
547         | deprecation                           { [$1] }
548         | {- empty -}                           { [] }
549
550 -- SUP: TEMPORARY HACK, not checking for `module Foo'
551 deprecation :: { RdrBinding }
552         : srcloc depreclist STRING
553                 { RdrBindings
554                         [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
555
556
557 -----------------------------------------------------------------------------
558 -- Foreign import and export declarations
559
560 -- for the time being, the following accepts foreign declarations conforming
561 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
562 --
563 -- * a flag indicates whether pre-standard declarations have been used and
564 --   triggers a deprecation warning further down the road
565 --
566 -- NB: The first two rules could be combined into one by replacing `safety1'
567 --     with `safety'.  However, the combined rule conflicts with the
568 --     DEPRECATED rules.
569 --
570 fdecl :: { RdrNameHsDecl }
571 fdecl : srcloc 'import' callconv safety1 fspec  {% mkImport $3 $4       $5 $1 }
572       | srcloc 'import' callconv         fspec  {% mkImport $3 (PlaySafe False) $4 $1 }
573       | srcloc 'export' callconv         fspec  {% mkExport $3          $4 $1 }
574         -- the following syntax is DEPRECATED
575       | srcloc fdecl1DEPRECATED                 { ForD ($2 True $1) }
576       | srcloc fdecl2DEPRECATED                 { $2 $1 }
577
578 fdecl1DEPRECATED :: { Bool -> SrcLoc -> ForeignDecl RdrName }
579 fdecl1DEPRECATED 
580   ----------- DEPRECATED label decls ------------
581   : 'label' ext_name varid '::' sigtype
582     { ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS 
583                                    (CLabel ($2 `orElse` mkExtName $3))) }
584
585   ----------- DEPRECATED ccall/stdcall decls ------------
586   --
587   -- NB: This business with the case expression below may seem overly
588   --     complicated, but it is necessary to avoid some conflicts.
589
590     -- DEPRECATED variant #1: lack of a calling convention specification
591     --                        (import) 
592   | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
593     { let
594         target = StaticTarget ($2 `orElse` mkExtName $4)
595       in
596       ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS 
597                                    (CFunction target)) }
598
599     -- DEPRECATED variant #2: external name consists of two separate strings
600     --                        (module name and function name) (import)
601   | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
602     {% case $2 of
603          DNCall      -> parseError "Illegal format of .NET foreign import"
604          CCall cconv -> returnP $
605            let
606              imp = CFunction (StaticTarget $4)
607            in
608            ForeignImport $6 $8 (CImport cconv $5 nilFS nilFS imp) }
609
610     -- DEPRECATED variant #3: `unsafe' after entity
611   | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
612     {% case $2 of
613          DNCall      -> parseError "Illegal format of .NET foreign import"
614          CCall cconv -> returnP $
615            let
616              imp = CFunction (StaticTarget $3)
617            in
618            ForeignImport $5 $7 (CImport cconv PlayRisky nilFS nilFS imp) }
619
620     -- DEPRECATED variant #4: use of the special identifier `dynamic' without
621     --                        an explicit calling convention (import)
622   | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
623     { ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS 
624                                    (CFunction DynamicTarget)) }
625
626     -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
627   | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
628     {% case $2 of
629          DNCall      -> parseError "Illegal format of .NET foreign import"
630          CCall cconv -> returnP $
631            ForeignImport $5 $7 (CImport cconv $4 nilFS nilFS 
632                                         (CFunction DynamicTarget)) }
633
634     -- DEPRECATED variant #6: lack of a calling convention specification
635     --                        (export) 
636   | 'export' {-no callconv-} ext_name varid '::' sigtype
637     { ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName $3) 
638                                    defaultCCallConv)) }
639
640     -- DEPRECATED variant #7: external name consists of two separate strings
641     --                        (module name and function name) (export)
642   | 'export' callconv STRING STRING varid '::' sigtype
643     {% case $2 of
644          DNCall      -> parseError "Illegal format of .NET foreign import"
645          CCall cconv -> returnP $
646            ForeignExport $5 $7 
647                          (CExport (CExportStatic $4 cconv)) }
648
649     -- DEPRECATED variant #8: use of the special identifier `dynamic' without
650     --                        an explicit calling convention (export)
651   | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
652     { ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS 
653                                    CWrapper) }
654
655     -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
656   | 'export' callconv 'dynamic' varid '::' sigtype
657     {% case $2 of
658          DNCall      -> parseError "Illegal format of .NET foreign import"
659          CCall cconv -> returnP $
660            ForeignImport $4 $6 (CImport cconv (PlaySafe False) nilFS nilFS CWrapper) }
661
662   ----------- DEPRECATED .NET decls ------------
663   -- NB: removed the .NET call declaration, as it is entirely subsumed
664   --     by the new standard FFI declarations
665
666 fdecl2DEPRECATED :: { SrcLoc -> RdrNameHsDecl }
667 fdecl2DEPRECATED 
668   : 'import' 'dotnet' 'type' ext_name tycon
669           { \loc -> TyClD (ForeignType $5 $4 DNType loc) }
670     -- left this one unchanged for the moment as type imports are not
671     -- covered currently by the FFI standard -=chak
672
673
674 callconv :: { CallConv }
675           : 'stdcall'                   { CCall  StdCallConv }
676           | 'ccall'                     { CCall  CCallConv   }
677           | 'dotnet'                    { DNCall             }
678
679 safety :: { Safety }
680         : 'unsafe'                      { PlayRisky }
681         | 'safe'                        { PlaySafe False }
682         | 'threadsafe'                  { PlaySafe True  }
683         | {- empty -}                   { PlaySafe False }
684
685 safety1 :: { Safety }
686         : 'unsafe'                      { PlayRisky }
687         | 'safe'                        { PlaySafe  False }
688         | 'threadsafe'                  { PlaySafe  True }
689           -- only needed to avoid conflicts with the DEPRECATED rules
690
691 fspec :: { (FastString, RdrName, RdrNameHsType) }
692        : STRING var '::' sigtype      { ($1      , $2, $4) }
693        |        var '::' sigtype      { (nilFS, $1, $3) }
694          -- if the entity string is missing, it defaults to the empty string;
695          -- the meaning of an empty entity string depends on the calling
696          -- convention
697
698 -- DEPRECATED syntax
699 ext_name :: { Maybe CLabelString }
700         : STRING                { Just $1 }
701         | STRING STRING         { Just $2 }     -- Ignore "module name" for now
702         | {- empty -}           { Nothing }
703
704
705 -----------------------------------------------------------------------------
706 -- Type signatures
707
708 opt_sig :: { Maybe RdrNameHsType }
709         : {- empty -}                   { Nothing }
710         | '::' sigtype                  { Just $2 }
711
712 opt_asig :: { Maybe RdrNameHsType }
713         : {- empty -}                   { Nothing }
714         | '::' atype                    { Just $2 }
715
716 sigtypes :: { [RdrNameHsType] }
717         : sigtype                       { [ $1 ] }
718         | sigtypes ',' sigtype          { $3 : $1 }
719
720 sigtype :: { RdrNameHsType }
721         : ctype                         { mkHsForAllTy Nothing [] $1 }
722
723 sig_vars :: { [RdrName] }
724          : sig_vars ',' var             { $3 : $1 }
725          | var                          { [ $1 ] }
726
727 -----------------------------------------------------------------------------
728 -- Types
729
730 -- A ctype is a for-all type
731 ctype   :: { RdrNameHsType }
732         : 'forall' tv_bndrs '.' ctype   { mkHsForAllTy (Just $2) [] $4 }
733         | context '=>' type             { mkHsForAllTy Nothing   $1 $3 }
734         -- A type of form (context => type) is an *implicit* HsForAllTy
735         | type                          { $1 }
736
737 -- We parse a context as a btype so that we don't get reduce/reduce
738 -- errors in ctype.  The basic problem is that
739 --      (Eq a, Ord a)
740 -- looks so much like a tuple type.  We can't tell until we find the =>
741 context :: { RdrNameContext }
742         : btype                         {% checkContext $1 }
743
744 type :: { RdrNameHsType }
745         : ipvar '::' gentype            { mkHsIParamTy $1 $3 }
746         | gentype                       { $1 }
747
748 gentype :: { RdrNameHsType }
749         : btype                         { $1 }
750         | btype qtyconop gentype        { HsOpTy $1 (HsTyOp $2) $3 }
751         | btype  '`' tyvar '`' gentype  { HsOpTy $1 (HsTyOp $3) $5 }
752         | btype '->' gentype            { HsOpTy $1 HsArrow $3 }
753
754 btype :: { RdrNameHsType }
755         : btype atype                   { HsAppTy $1 $2 }
756         | atype                         { $1 }
757
758 atype :: { RdrNameHsType }
759         : gtycon                        { HsTyVar $1 }
760         | tyvar                         { HsTyVar $1 }
761         | '(' type ',' comma_types1 ')' { HsTupleTy (mkHsTupCon tcName Boxed  ($2:$4)) ($2:$4) }
762         | '(#' comma_types1 '#)'        { HsTupleTy (mkHsTupCon tcName Unboxed     $2) $2      }
763         | '[' type ']'                  { HsListTy  $2 }
764         | '[:' type ':]'                { HsPArrTy  $2 }
765         | '(' ctype ')'                 { HsParTy   $2 }
766         | '(' ctype '::' kind ')'       { HsKindSig $2 $4 }
767 -- Generics
768         | INTEGER                       { HsNumTy $1 }
769
770 -- An inst_type is what occurs in the head of an instance decl
771 --      e.g.  (Foo a, Gaz b) => Wibble a b
772 -- It's kept as a single type, with a MonoDictTy at the right
773 -- hand corner, for convenience.
774 inst_type :: { RdrNameHsType }
775         : ctype                         {% checkInstType $1 }
776
777 comma_types0  :: { [RdrNameHsType] }
778         : comma_types1                  { $1 }
779         | {- empty -}                   { [] }
780
781 comma_types1    :: { [RdrNameHsType] }
782         : type                          { [$1] }
783         | type  ',' comma_types1        { $1 : $3 }
784
785 tv_bndrs :: { [RdrNameHsTyVar] }
786          : tv_bndr tv_bndrs             { $1 : $2 }
787          | {- empty -}                  { [] }
788
789 tv_bndr :: { RdrNameHsTyVar }
790         : tyvar                         { UserTyVar $1 }
791         | '(' tyvar '::' kind ')'       { IfaceTyVar $2 $4 }
792
793 fds :: { [([RdrName], [RdrName])] }
794         : {- empty -}                   { [] }
795         | '|' fds1                      { reverse $2 }
796
797 fds1 :: { [([RdrName], [RdrName])] }
798         : fds1 ',' fd                   { $3 : $1 }
799         | fd                            { [$1] }
800
801 fd :: { ([RdrName], [RdrName]) }
802         : varids0 '->' varids0          { (reverse $1, reverse $3) }
803
804 varids0 :: { [RdrName] }
805         : {- empty -}                   { [] }
806         | varids0 tyvar                 { $2 : $1 }
807
808 -----------------------------------------------------------------------------
809 -- Kinds
810
811 kind    :: { Kind }
812         : akind                 { $1 }
813         | akind '->' kind       { mkArrowKind $1 $3 }
814
815 akind   :: { Kind }
816         : '*'                   { liftedTypeKind }
817         | '(' kind ')'          { $2 }
818
819
820 -----------------------------------------------------------------------------
821 -- Datatype declarations
822
823 newconstr :: { RdrNameConDecl }
824         : srcloc conid atype    { ConDecl $2 [] [] (PrefixCon [unbangedType $3]) $1 }
825         | srcloc conid '{' var '::' ctype '}'
826                                 { ConDecl $2 [] [] (RecCon [($4, unbangedType $6)]) $1 }
827
828 constrs :: { [RdrNameConDecl] }
829         : {- empty; a GHC extension -}  { [] }
830         | '=' constrs1                  { $2 }
831
832 constrs1 :: { [RdrNameConDecl] }
833         : constrs1 '|' constr           { $3 : $1 }
834         | constr                        { [$1] }
835
836 constr :: { RdrNameConDecl }
837         : srcloc forall context '=>' constr_stuff
838                 { ConDecl (fst $5) $2 $3 (snd $5) $1 }
839         | srcloc forall constr_stuff
840                 { ConDecl (fst $3) $2 [] (snd $3) $1 }
841
842 forall :: { [RdrNameHsTyVar] }
843         : 'forall' tv_bndrs '.'         { $2 }
844         | {- empty -}                   { [] }
845
846 constr_stuff :: { (RdrName, RdrNameConDetails) }
847         : btype                         {% mkPrefixCon $1 [] }
848         | btype '!' atype satypes       {% mkPrefixCon $1 (BangType MarkedUserStrict $3 : $4) }
849         | oqtycon '{' '}'               {% mkRecCon $1 [] }
850         | oqtycon '{' fielddecls '}'    {% mkRecCon $1 $3 }
851         | sbtype conop sbtype           { ($2, InfixCon $1 $3) }
852
853 satypes :: { [RdrNameBangType] }
854         : atype satypes                 { unbangedType $1 : $2 }
855         | '!' atype satypes             { BangType MarkedUserStrict $2 : $3 }
856         | {- empty -}                   { [] }
857
858 sbtype :: { RdrNameBangType }
859         : btype                         { unbangedType $1 }
860         | '!' atype                     { BangType MarkedUserStrict $2 }
861
862 fielddecls :: { [([RdrName],RdrNameBangType)] }
863         : fielddecl ',' fielddecls      { $1 : $3 }
864         | fielddecl                     { [$1] }
865
866 fielddecl :: { ([RdrName],RdrNameBangType) }
867         : sig_vars '::' stype           { (reverse $1, $3) }
868
869 stype :: { RdrNameBangType }
870         : ctype                         { unbangedType $1 }
871         | '!' atype                     { BangType MarkedUserStrict $2 }
872
873 deriving :: { Maybe RdrNameContext }
874         : {- empty -}                   { Nothing }
875         | 'deriving' context            { Just $2 }
876              -- Glasgow extension: allow partial 
877              -- applications in derivings
878
879 -----------------------------------------------------------------------------
880 -- Value definitions
881
882 {- There's an awkward overlap with a type signature.  Consider
883         f :: Int -> Int = ...rhs...
884    Then we can't tell whether it's a type signature or a value
885    definition with a result signature until we see the '='.
886    So we have to inline enough to postpone reductions until we know.
887 -}
888
889 {-
890   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
891   instead of qvar, we get another shift/reduce-conflict. Consider the
892   following programs:
893   
894      { (^^) :: Int->Int ; }          Type signature; only var allowed
895
896      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
897                                      qvar allowed (because of instance decls)
898   
899   We can't tell whether to reduce var to qvar until after we've read the signatures.
900 -}
901
902 decl    :: { RdrBinding }
903         : sigdecl                       { $1 }
904         | infixexp srcloc opt_sig rhs   {% checkValDef $1 $3 $4 $2 }
905
906 rhs     :: { RdrNameGRHSs }
907         : '=' srcloc exp wherebinds     { GRHSs (unguardedRHS $3 $2) $4 placeHolderType }
908         | gdrhs wherebinds              { GRHSs (reverse $1)         $2 placeHolderType }
909
910 gdrhs :: { [RdrNameGRHS] }
911         : gdrhs gdrh                    { $2 : $1 }
912         | gdrh                          { [$1] }
913
914 gdrh :: { RdrNameGRHS }
915         : '|' srcloc quals '=' exp      { GRHS (reverse (ResultStmt $5 $2 : $3)) $2 }
916
917 sigdecl :: { RdrBinding }
918         : infixexp srcloc '::' sigtype          
919                                 {% checkValSig $1 $4 $2 }
920                 -- See the above notes for why we need infixexp here
921         | var ',' sig_vars srcloc '::' sigtype  
922                                 { mkSigDecls [ Sig n $6 $4 | n <- $1:$3 ] }
923         | srcloc infix prec ops { mkSigDecls [ FixSig (FixitySig n (Fixity $3 $2) $1)
924                                              | n <- $4 ] }
925         | '{-# INLINE'   srcloc activation qvar '#-}'         
926                                 { RdrHsDecl (SigD (InlineSig True  $4 $3 $2)) }
927         | '{-# NOINLINE' srcloc inverse_activation qvar '#-}' 
928                                 { RdrHsDecl (SigD (InlineSig False $4 $3 $2)) }
929         | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
930                                 { mkSigDecls  [ SpecSig $3 t $2 | t <- $5] }
931         | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
932                                 { RdrHsDecl (SigD (SpecInstSig $4 $2)) }
933
934 -----------------------------------------------------------------------------
935 -- Expressions
936
937 exp   :: { RdrNameHsExpr }
938         : infixexp '::' sigtype         { ExprWithTySig $1 $3 }
939         | infixexp 'with' dbinding      { HsLet (IPBinds $3 True{-not a let-}) $1 }
940         | infixexp                      { $1 }
941
942 infixexp :: { RdrNameHsExpr }
943         : exp10                         { $1 }
944         | infixexp qop exp10            { (OpApp $1 (HsVar $2) 
945                                                 (panic "fixity") $3 )}
946
947 exp10 :: { RdrNameHsExpr }
948         : '\\' srcloc aexp aexps opt_asig '->' srcloc exp       
949                         {% checkPatterns $2 ($3 : reverse $4) `thenP` \ ps -> 
950                            returnP (HsLam (Match ps $5 
951                                             (GRHSs (unguardedRHS $8 $7) 
952                                                    EmptyBinds placeHolderType))) }
953         | 'let' binds 'in' exp                  { HsLet $2 $4 }
954         | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
955         | 'case' srcloc exp 'of' altslist       { HsCase $3 $5 $2 }
956         | '-' fexp                              { mkHsNegApp $2 }
957         | srcloc 'do' stmtlist                  {% checkDo $3  `thenP` \ stmts ->
958                                                    returnP (mkHsDo DoExpr stmts $1) }
959         | srcloc 'mdo' stmtlist                 {% checkMDo $3  `thenP` \ stmts ->
960                                                    returnP (mkHsDo MDoExpr stmts $1) }
961
962         | '_ccall_'    ccallid aexps0           { HsCCall $2 $3 PlayRisky False placeHolderType }
963         | '_ccall_GC_' ccallid aexps0           { HsCCall $2 $3 (PlaySafe False) False placeHolderType }
964         | '_casm_'     CLITLIT aexps0           { HsCCall $2 $3 PlayRisky True  placeHolderType }
965         | '_casm_GC_'  CLITLIT aexps0           { HsCCall $2 $3 (PlaySafe False) True  placeHolderType }
966
967         | scc_annot exp                         { if opt_SccProfilingOn
968                                                         then HsSCC $1 $2
969                                                         else HsPar $2 }
970
971         | '{-# CORE' STRING '#-}' exp           { HsCoreAnn $2 $4 }    -- hdaume: core annotation
972
973         | reifyexp                              { HsReify $1 }
974         | fexp                                  { $1 }
975
976 scc_annot :: { FastString }
977         : '_scc_' STRING                        { $2 }
978         | '{-# SCC' STRING '#-}'                { $2 }
979
980 ccallid :: { FastString }
981         :  VARID                                { $1 }
982         |  CONID                                { $1 }
983
984 fexp    :: { RdrNameHsExpr }
985         : fexp aexp                             { (HsApp $1 $2) }
986         | aexp                                  { $1 }
987
988 reifyexp :: { HsReify RdrName }
989         : REIFY_DECL gtycon                     { Reify ReifyDecl $2 }
990         | REIFY_DECL qvar                       { Reify ReifyDecl $2 }
991         | REIFY_TYPE qcname                     { Reify ReifyType $2 }
992         | REIFY_FIXITY qcname                   { Reify ReifyFixity $2 }
993
994 aexps0  :: { [RdrNameHsExpr] }
995         : aexps                                 { reverse $1 }
996
997 aexps   :: { [RdrNameHsExpr] }
998         : aexps aexp                            { $2 : $1 }
999         | {- empty -}                           { [] }
1000
1001 aexp    :: { RdrNameHsExpr }
1002         : qvar '@' aexp                 { EAsPat $1 $3 }
1003         | '~' aexp                      { ELazyPat $2 }
1004         | aexp1                         { $1 }
1005
1006 aexp1   :: { RdrNameHsExpr }
1007         : aexp1 '{' fbinds '}'          {% (mkRecConstrOrUpdate $1 (reverse $3)) }
1008         | aexp2                         { $1 }
1009
1010 -- Here was the syntax for type applications that I was planning
1011 -- but there are difficulties (e.g. what order for type args)
1012 -- so it's not enabled yet.
1013         | qcname '{|' gentype '|}'          { (HsApp (HsVar $1) (HsType $3)) }
1014
1015 aexp2   :: { RdrNameHsExpr }
1016         : ipvar                         { HsIPVar $1 }
1017         | qcname                        { HsVar $1 }
1018         | literal                       { HsLit $1 }
1019         | INTEGER                       { HsOverLit (mkHsIntegral   $1) }
1020         | RATIONAL                      { HsOverLit (mkHsFractional $1) }
1021         | '(' exp ')'                   { HsPar $2 }
1022         | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) Boxed}
1023         | '(#' texps '#)'               { ExplicitTuple (reverse $2)      Unboxed }
1024         | '[' list ']'                  { $2 }
1025         | '[:' parr ':]'                { $2 }
1026         | '(' infixexp qop ')'          { (SectionL $2 (HsVar $3))  }
1027         | '(' qopm infixexp ')'         { (SectionR $2 $3) }
1028         | '_'                           { EWildPat }
1029         
1030         -- MetaHaskell Extension
1031         | srcloc ID_SPLICE              { mkHsSplice (HsVar (mkUnqual varName $2)) $1 }  -- $x
1032         | srcloc '$(' exp ')'           { mkHsSplice $3 $1 }                             -- $( exp )
1033         | srcloc '[|' exp '|]'          { HsBracket (ExpBr $3) $1 }                       
1034         | srcloc '[t|' ctype '|]'       { HsBracket (TypBr $3) $1 }                       
1035         | srcloc '[p|' infixexp '|]'    {% checkPattern $1 $3 `thenP` \p ->
1036                                            returnP (HsBracket (PatBr p) $1) }
1037         | srcloc '[d|' cvtopbody '|]'   { HsBracket (DecBr (mkGroup $3)) $1 }
1038
1039 cvtopbody :: { [RdrNameHsDecl] }
1040         :  '{'            cvtopdecls '}'                { $2 }
1041         |      layout_on  cvtopdecls close              { $2 }
1042
1043 texps :: { [RdrNameHsExpr] }
1044         : texps ',' exp                 { $3 : $1 }
1045         | exp                           { [$1] }
1046
1047
1048 -----------------------------------------------------------------------------
1049 -- List expressions
1050
1051 -- The rules below are little bit contorted to keep lexps left-recursive while
1052 -- avoiding another shift/reduce-conflict.
1053
1054 list :: { RdrNameHsExpr }
1055         : exp                           { ExplicitList placeHolderType [$1] }
1056         | lexps                         { ExplicitList placeHolderType (reverse $1) }
1057         | exp '..'                      { ArithSeqIn (From $1) }
1058         | exp ',' exp '..'              { ArithSeqIn (FromThen $1 $3) }
1059         | exp '..' exp                  { ArithSeqIn (FromTo $1 $3) }
1060         | exp ',' exp '..' exp          { ArithSeqIn (FromThenTo $1 $3 $5) }
1061         | exp srcloc pquals             {% let { body [qs] = qs;
1062                                                  body  qss = [ParStmt (map reverse qss)] }
1063                                            in
1064                                            returnP ( mkHsDo ListComp
1065                                                             (reverse (ResultStmt $1 $2 : body $3))
1066                                                             $2
1067                                                   )
1068                                         }
1069
1070 lexps :: { [RdrNameHsExpr] }
1071         : lexps ',' exp                 { $3 : $1 }
1072         | exp ',' exp                   { [$3,$1] }
1073
1074 -----------------------------------------------------------------------------
1075 -- List Comprehensions
1076
1077 pquals :: { [[RdrNameStmt]] }
1078         : pquals '|' quals              { $3 : $1 }
1079         | '|' quals                     { [$2] }
1080
1081 quals :: { [RdrNameStmt] }
1082         : quals ',' stmt                { $3 : $1 }
1083         | stmt                          { [$1] }
1084
1085 -----------------------------------------------------------------------------
1086 -- Parallel array expressions
1087
1088 -- The rules below are little bit contorted; see the list case for details.
1089 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1090 -- Moreover, we allow explicit arrays with no element (represented by the nil
1091 -- constructor in the list case).
1092
1093 parr :: { RdrNameHsExpr }
1094         :                               { ExplicitPArr placeHolderType [] }
1095         | exp                           { ExplicitPArr placeHolderType [$1] }
1096         | lexps                         { ExplicitPArr placeHolderType 
1097                                                        (reverse $1) }
1098         | exp '..' exp                  { PArrSeqIn (FromTo $1 $3) }
1099         | exp ',' exp '..' exp          { PArrSeqIn (FromThenTo $1 $3 $5) }
1100         | exp srcloc pquals             {% let {
1101                                              body [qs] = qs;
1102                                              body  qss = [ParStmt 
1103                                                            (map reverse qss)]}
1104                                            in
1105                                            returnP $ 
1106                                              mkHsDo PArrComp 
1107                                                     (reverse (ResultStmt $1 $2 
1108                                                              : body $3))
1109                                                     $2
1110                                         }
1111
1112 -- We are reusing `lexps' and `pquals' from the list case.
1113
1114 -----------------------------------------------------------------------------
1115 -- Case alternatives
1116
1117 altslist :: { [RdrNameMatch] }
1118         : '{'            alts '}'       { reverse $2 }
1119         |     layout_on  alts  close    { reverse $2 }
1120
1121 alts    :: { [RdrNameMatch] }
1122         : alts1                         { $1 }
1123         | ';' alts                      { $2 }
1124
1125 alts1   :: { [RdrNameMatch] }
1126         : alts1 ';' alt                 { $3 : $1 }
1127         | alts1 ';'                     { $1 }
1128         | alt                           { [$1] }
1129
1130 alt     :: { RdrNameMatch }
1131         : srcloc infixexp opt_sig ralt wherebinds
1132                                         {% (checkPattern $1 $2 `thenP` \p ->
1133                                            returnP (Match [p] $3
1134                                                      (GRHSs $4 $5 placeHolderType))  )}
1135
1136 ralt :: { [RdrNameGRHS] }
1137         : '->' srcloc exp               { [GRHS [ResultStmt $3 $2] $2] }
1138         | gdpats                        { reverse $1 }
1139
1140 gdpats :: { [RdrNameGRHS] }
1141         : gdpats gdpat                  { $2 : $1 }
1142         | gdpat                         { [$1] }
1143
1144 gdpat   :: { RdrNameGRHS }
1145         : srcloc '|' quals '->' exp     { GRHS (reverse (ResultStmt $5 $1:$3)) $1}
1146
1147 -----------------------------------------------------------------------------
1148 -- Statement sequences
1149
1150 stmtlist :: { [RdrNameStmt] }
1151         : '{'                   stmts '}'       { $2 }
1152         |     layout_on_for_do  stmts close     { $2 }
1153
1154 --      do { ;; s ; s ; ; s ;; }
1155 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1156 -- here, because we need too much lookahead if we see do { e ; }
1157 -- So we use ExprStmts throughout, and switch the last one over
1158 -- in ParseUtils.checkDo instead
1159 stmts :: { [RdrNameStmt] }
1160         : stmt stmts_help               { $1 : $2 }
1161         | ';' stmts                     { $2 }
1162         | {- empty -}                   { [] }
1163
1164 stmts_help :: { [RdrNameStmt] }
1165         : ';' stmts                     { $2 }
1166         | {- empty -}                   { [] }
1167
1168 -- For typing stmts at the GHCi prompt, where 
1169 -- the input may consist of just comments.
1170 maybe_stmt :: { Maybe RdrNameStmt }
1171         : stmt                          { Just $1 }
1172         | {- nothing -}                 { Nothing }
1173
1174 stmt  :: { RdrNameStmt }
1175         : srcloc infixexp '<-' exp      {% checkPattern $1 $2 `thenP` \p ->
1176                                            returnP (BindStmt p $4 $1) }
1177         | srcloc exp                    { ExprStmt $2 placeHolderType $1 }
1178         | srcloc 'let' binds            { LetStmt $3 }
1179
1180 -----------------------------------------------------------------------------
1181 -- Record Field Update/Construction
1182
1183 fbinds  :: { RdrNameHsRecordBinds }
1184         : fbinds ',' fbind              { $3 : $1 }
1185         | fbinds ','                    { $1 }
1186         | fbind                         { [$1] }
1187         | {- empty -}                   { [] }
1188
1189 fbind   :: { (RdrName, RdrNameHsExpr) }
1190         : qvar '=' exp                  { ($1,$3) }
1191
1192 -----------------------------------------------------------------------------
1193 -- Implicit Parameter Bindings
1194
1195 dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
1196         : '{' dbinds '}'                { $2 }
1197         | layout_on dbinds close        { $2 }
1198
1199 dbinds  :: { [(IPName RdrName, RdrNameHsExpr)] }
1200         : dbinds ';' dbind              { $3 : $1 }
1201         | dbinds ';'                    { $1 }
1202         | dbind                         { [$1] }
1203 --      | {- empty -}                   { [] }
1204
1205 dbind   :: { (IPName RdrName, RdrNameHsExpr) }
1206 dbind   : ipvar '=' exp                 { ($1, $3) }
1207
1208 -----------------------------------------------------------------------------
1209 -- Variables, Constructors and Operators.
1210
1211 identifier :: { RdrName }
1212         : qvar                          { $1 }
1213         | gcon                          { $1 }
1214         | qop                           { $1 }
1215
1216 depreclist :: { [RdrName] }
1217 depreclist : deprec_var                 { [$1] }
1218            | deprec_var ',' depreclist  { $1 : $3 }
1219
1220 deprec_var :: { RdrName }
1221 deprec_var : var                        { $1 }
1222            | tycon                      { $1 }
1223
1224 gcon    :: { RdrName }  -- Data constructor namespace
1225         : sysdcon               { nameRdrName (dataConName $1) }
1226         | qcon                  { $1 }
1227 -- the case of '[:' ':]' is part of the production `parr'
1228
1229 sysdcon :: { DataCon }  -- Wired in data constructors
1230         : '(' ')'               { unitDataCon }
1231         | '(' commas ')'        { tupleCon Boxed $2 }
1232         | '[' ']'               { nilDataCon }
1233
1234 var     :: { RdrName }
1235         : varid                 { $1 }
1236         | '(' varsym ')'        { $2 }
1237
1238 qvar    :: { RdrName }
1239         : qvarid                { $1 }
1240         | '(' varsym ')'        { $2 }
1241         | '(' qvarsym1 ')'      { $2 }
1242 -- We've inlined qvarsym here so that the decision about
1243 -- whether it's a qvar or a var can be postponed until
1244 -- *after* we see the close paren.
1245
1246 ipvar   :: { IPName RdrName }
1247         : IPDUPVARID            { Dupable (mkUnqual varName $1) }
1248         | IPSPLITVARID          { Linear  (mkUnqual varName $1) }
1249
1250 qcon    :: { RdrName }
1251         : qconid                { $1 }
1252         | '(' qconsym ')'       { $2 }
1253
1254 varop   :: { RdrName }
1255         : varsym                { $1 }
1256         | '`' varid '`'         { $2 }
1257
1258 qvarop :: { RdrName }
1259         : qvarsym               { $1 }
1260         | '`' qvarid '`'        { $2 }
1261
1262 qvaropm :: { RdrName }
1263         : qvarsym_no_minus      { $1 }
1264         | '`' qvarid '`'        { $2 }
1265
1266 conop :: { RdrName }
1267         : consym                { $1 }  
1268         | '`' conid '`'         { $2 }
1269
1270 qconop :: { RdrName }
1271         : qconsym               { $1 }
1272         | '`' qconid '`'        { $2 }
1273
1274 -----------------------------------------------------------------------------
1275 -- Type constructors
1276
1277 gtycon  :: { RdrName }  -- A "general" qualified tycon
1278         : oqtycon                       { $1 }
1279         | '(' ')'                       { getRdrName unitTyCon }
1280         | '(' commas ')'                { getRdrName (tupleTyCon Boxed $2) }
1281         | '(' '->' ')'                  { nameRdrName funTyConName }
1282         | '[' ']'                       { nameRdrName listTyConName }
1283         | '[:' ':]'                     { nameRdrName parrTyConName }
1284
1285 oqtycon :: { RdrName }  -- An "ordinary" qualified tycon
1286         : qtycon                        { $1 }
1287         | '(' qtyconsym ')'             { $2 }
1288
1289 qtyconop :: { RdrName } -- Qualified or unqualified
1290         : qtyconsym                     { $1 }
1291         | '`' qtycon '`'                { $2 }
1292
1293 tyconop :: { RdrName }  -- Unqualified
1294         : tyconsym                      { $1 }
1295         | '`' tycon '`'                 { $2 }
1296
1297 qtycon :: { RdrName }   -- Qualified or unqualified
1298         : QCONID                        { mkQual tcClsName $1 }
1299         | tycon                         { $1 }
1300
1301 tycon   :: { RdrName }  -- Unqualified
1302         : CONID                         { mkUnqual tcClsName $1 }
1303
1304 qtyconsym :: { RdrName }
1305         : QCONSYM                       { mkQual tcClsName $1 }
1306         | tyconsym                      { $1 }
1307
1308 tyconsym :: { RdrName }
1309         : CONSYM                        { mkUnqual tcClsName $1 }
1310
1311 -----------------------------------------------------------------------------
1312 -- Any operator
1313
1314 op      :: { RdrName }   -- used in infix decls
1315         : varop                 { $1 }
1316         | conop                 { $1 }
1317
1318 qop     :: { RdrName {-HsExpr-} }   -- used in sections
1319         : qvarop                { $1 }
1320         | qconop                { $1 }
1321
1322 qopm    :: { RdrNameHsExpr }   -- used in sections
1323         : qvaropm               { HsVar $1 }
1324         | qconop                { HsVar $1 }
1325
1326 -----------------------------------------------------------------------------
1327 -- VarIds
1328
1329 qvarid :: { RdrName }
1330         : varid                 { $1 }
1331         | QVARID                { mkQual varName $1 }
1332
1333 varid :: { RdrName }
1334         : varid_no_unsafe       { $1 }
1335         | 'unsafe'              { mkUnqual varName FSLIT("unsafe") }
1336         | 'safe'                { mkUnqual varName FSLIT("safe") }
1337         | 'threadsafe'          { mkUnqual varName FSLIT("threadsafe") }
1338
1339 varid_no_unsafe :: { RdrName }
1340         : VARID                 { mkUnqual varName $1 }
1341         | special_id            { mkUnqual varName $1 }
1342         | 'forall'              { mkUnqual varName FSLIT("forall") }
1343
1344 tyvar   :: { RdrName }
1345         : VARID                 { mkUnqual tvName $1 }
1346         | special_id            { mkUnqual tvName $1 }
1347         | 'unsafe'              { mkUnqual tvName FSLIT("unsafe") }
1348         | 'safe'                { mkUnqual tvName FSLIT("safe") }
1349         | 'threadsafe'          { mkUnqual tvName FSLIT("threadsafe") }
1350
1351 -- These special_ids are treated as keywords in various places, 
1352 -- but as ordinary ids elsewhere.   'special_id' collects all these
1353 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1354 special_id :: { UserFS }
1355 special_id
1356         : 'as'                  { FSLIT("as") }
1357         | 'qualified'           { FSLIT("qualified") }
1358         | 'hiding'              { FSLIT("hiding") }
1359         | 'export'              { FSLIT("export") }
1360         | 'label'               { FSLIT("label")  }
1361         | 'dynamic'             { FSLIT("dynamic") }
1362         | 'stdcall'             { FSLIT("stdcall") }
1363         | 'ccall'               { FSLIT("ccall") }
1364
1365 -----------------------------------------------------------------------------
1366 -- Variables 
1367
1368 qvarsym :: { RdrName }
1369         : varsym                { $1 }
1370         | qvarsym1              { $1 }
1371
1372 qvarsym_no_minus :: { RdrName }
1373         : varsym_no_minus       { $1 }
1374         | qvarsym1              { $1 }
1375
1376 qvarsym1 :: { RdrName }
1377 qvarsym1 : QVARSYM              { mkQual varName $1 }
1378
1379 varsym :: { RdrName }
1380         : varsym_no_minus       { $1 }
1381         | '-'                   { mkUnqual varName FSLIT("-") }
1382
1383 varsym_no_minus :: { RdrName } -- varsym not including '-'
1384         : VARSYM                { mkUnqual varName $1 }
1385         | special_sym           { mkUnqual varName $1 }
1386
1387
1388 -- See comments with special_id
1389 special_sym :: { UserFS }
1390 special_sym : '!'       { FSLIT("!") }
1391             | '.'       { FSLIT(".") }
1392             | '*'       { FSLIT("*") }
1393
1394 -----------------------------------------------------------------------------
1395 -- Data constructors
1396
1397 qconid :: { RdrName }   -- Qualified or unqualifiedb
1398         : conid                 { $1 }
1399         | QCONID                { mkQual dataName $1 }
1400
1401 conid   :: { RdrName }
1402         : CONID                 { mkUnqual dataName $1 }
1403
1404 qconsym :: { RdrName }  -- Qualified or unqualified
1405         : consym                { $1 }
1406         | QCONSYM               { mkQual dataName $1 }
1407
1408 consym :: { RdrName }
1409         : CONSYM                { mkUnqual dataName $1 }
1410
1411         -- ':' means only list cons
1412         | ':'                   { nameRdrName consDataConName }
1413                                 -- NB: SrcName because we are reading source
1414
1415
1416 -----------------------------------------------------------------------------
1417 -- Literals
1418
1419 literal :: { HsLit }
1420         : CHAR                  { HsChar       $1 }
1421         | STRING                { HsString     $1 }
1422         | PRIMINTEGER           { HsIntPrim    $1 }
1423         | PRIMCHAR              { HsCharPrim   $1 }
1424         | PRIMSTRING            { HsStringPrim $1 }
1425         | PRIMFLOAT             { HsFloatPrim  $1 }
1426         | PRIMDOUBLE            { HsDoublePrim $1 }
1427         | CLITLIT               { HsLitLit     $1 placeHolderType }
1428
1429 srcloc :: { SrcLoc }    :       {% getSrcLocP }
1430  
1431 -----------------------------------------------------------------------------
1432 -- Layout
1433
1434 close :: { () }
1435         : vccurly               { () } -- context popped in lexer.
1436         | error                 {% popContext }
1437
1438 layout_on         :: { () }     : {% layoutOn True{-strict-} }
1439 layout_on_for_do  :: { () }     : {% layoutOn False }
1440
1441 -----------------------------------------------------------------------------
1442 -- Miscellaneous (mostly renamings)
1443
1444 modid   :: { ModuleName }
1445         : CONID                 { mkModuleNameFS $1 }
1446         | QCONID                { mkModuleNameFS
1447                                    (mkFastString
1448                                      (unpackFS (fst $1) ++ 
1449                                         '.':unpackFS (snd $1)))
1450                                 }
1451
1452 commas :: { Int }
1453         : commas ','                    { $1 + 1 }
1454         | ','                           { 2 }
1455
1456 -----------------------------------------------------------------------------
1457
1458 {
1459 happyError :: P a
1460 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
1461 }