61a3275c645ab02e2361455a2a772b4d919f3f55
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.59 2001/05/03 08:08:44 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 ) where
13
14 import HsSyn
15 import HsTypes          ( mkHsTupCon )
16
17 import RdrHsSyn
18 import Lex
19 import ParseUtil
20 import RdrName
21 import PrelNames        ( mAIN_Name, unitTyCon_RDR, funTyCon_RDR, listTyCon_RDR,
22                           tupleTyCon_RDR, unitCon_RDR, nilCon_RDR, tupleCon_RDR
23                         )
24 import OccName          ( UserFS, varName, tcName, dataName, tcClsName, tvName )
25 import SrcLoc           ( SrcLoc )
26 import Module
27 import CallConv
28 import CmdLineOpts      ( opt_SccProfilingOn )
29 import BasicTypes       ( Boxity(..), Fixity(..), FixityDirection(..), NewOrData(..) )
30 import Panic
31
32 import GlaExts
33 import FastString       ( tailFS )
34 import Outputable
35
36 #include "HsVersions.h"
37 }
38
39 {-
40 -----------------------------------------------------------------------------
41 Conflicts: 14 shift/reduce
42         (note: it's currently 21 -- JRL, 31/1/2000)
43
44 8 for abiguity in 'if x then y else z + 1'
45         (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
46 1 for ambiguity in 'if x then y else z :: T'
47         (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
48 3 for ambiguity in 'case x of y :: a -> b'
49         (don't know whether to reduce 'a' as a btype or shift the '->'.
50          conclusion:  bogus expression anyway, doesn't matter)
51
52 1 for ambiguity in '{-# RULES "name" forall = ... #-}' 
53         since 'forall' is a valid variable name, we don't know whether
54         to treat a forall on the input as the beginning of a quantifier
55         or the beginning of the rule itself.  Resolving to shift means
56         it's always treated as a quantifier, hence the above is disallowed.
57         This saves explicitly defining a grammar for the rule lhs that
58         doesn't include 'forall'.
59
60 1 for ambiguity in 'x @ Rec{..}'.  
61         Only sensible parse is 'x @ (Rec{..})', which is what resolving
62         to shift gives us.
63
64 -----------------------------------------------------------------------------
65 -}
66
67 %token
68  '_'            { ITunderscore }                -- Haskell keywords
69  'as'           { ITas }
70  'case'         { ITcase }      
71  'class'        { ITclass } 
72  'data'         { ITdata } 
73  'default'      { ITdefault }
74  'deriving'     { ITderiving }
75  'do'           { ITdo }
76  'else'         { ITelse }
77  'hiding'       { IThiding }
78  'if'           { ITif }
79  'import'       { ITimport }
80  'in'           { ITin }
81  'infix'        { ITinfix }
82  'infixl'       { ITinfixl }
83  'infixr'       { ITinfixr }
84  'instance'     { ITinstance }
85  'let'          { ITlet }
86  'module'       { ITmodule }
87  'newtype'      { ITnewtype }
88  'of'           { ITof }
89  'qualified'    { ITqualified }
90  'then'         { ITthen }
91  'type'         { ITtype }
92  'where'        { ITwhere }
93  '_scc_'        { ITscc }             -- ToDo: remove
94
95  'forall'       { ITforall }                    -- GHC extension keywords
96  'foreign'      { ITforeign }
97  'export'       { ITexport }
98  'label'        { ITlabel } 
99  'dynamic'      { ITdynamic }
100  'unsafe'       { ITunsafe }
101  'with'         { ITwith }
102  'stdcall'      { ITstdcallconv }
103  'ccall'        { ITccallconv }
104  '_ccall_'      { ITccall (False, False, False) }
105  '_ccall_GC_'   { ITccall (False, False, True)  }
106  '_casm_'       { ITccall (False, True,  False) }
107  '_casm_GC_'    { ITccall (False, True,  True)  }
108
109  '{-# SPECIALISE'  { ITspecialise_prag }
110  '{-# SOURCE'      { ITsource_prag }
111  '{-# INLINE'      { ITinline_prag }
112  '{-# NOINLINE'    { ITnoinline_prag }
113  '{-# RULES'       { ITrules_prag }
114  '{-# SCC'         { ITscc_prag }
115  '{-# DEPRECATED'  { ITdeprecated_prag }
116  '#-}'             { ITclose_prag }
117
118 {-
119  '__interface'  { ITinterface }                 -- interface keywords
120  '__export'     { IT__export }
121  '__instimport' { ITinstimport }
122  '__forall'     { IT__forall }
123  '__letrec'     { ITletrec }
124  '__coerce'     { ITcoerce }
125  '__depends'    { ITdepends }
126  '__inline'     { ITinline }
127  '__DEFAULT'    { ITdefaultbranch }
128  '__bot'        { ITbottom }
129  '__integer'    { ITinteger_lit }
130  '__float'      { ITfloat_lit }
131  '__rational'   { ITrational_lit }
132  '__addr'       { ITaddr_lit }
133  '__label'      { ITlabel_lit }
134  '__litlit'     { ITlit_lit }
135  '__string'     { ITstring_lit }
136  '__ccall'      { ITccall $$ }
137  '__scc'        { IT__scc }
138  '__sccC'       { ITsccAllCafs }
139
140  '__A'          { ITarity }
141  '__P'          { ITspecialise }
142  '__C'          { ITnocaf }
143  '__U'          { ITunfold $$ }
144  '__S'          { ITstrict $$ }
145  '__M'          { ITcprinfo $$ }
146 -}
147
148  '..'           { ITdotdot }                    -- reserved symbols
149  '::'           { ITdcolon }
150  '='            { ITequal }
151  '\\'           { ITlam }
152  '|'            { ITvbar }
153  '<-'           { ITlarrow }
154  '->'           { ITrarrow }
155  '@'            { ITat }
156  '~'            { ITtilde }
157  '=>'           { ITdarrow }
158  '-'            { ITminus }
159  '!'            { ITbang }
160  '.'            { ITdot }
161
162  '{'            { ITocurly }                    -- special symbols
163  '}'            { ITccurly }
164  '{|'           { ITocurlybar }
165  '|}'           { ITccurlybar }
166  vccurly        { ITvccurly } -- virtual close curly (from layout)
167  '['            { ITobrack }
168  ']'            { ITcbrack }
169  '('            { IToparen }
170  ')'            { ITcparen }
171  '(#'           { IToubxparen }
172  '#)'           { ITcubxparen }
173  ';'            { ITsemi }
174  ','            { ITcomma }
175  '`'            { ITbackquote }
176
177  VARID          { ITvarid    $$ }               -- identifiers
178  CONID          { ITconid    $$ }
179  VARSYM         { ITvarsym   $$ }
180  CONSYM         { ITconsym   $$ }
181  QVARID         { ITqvarid   $$ }
182  QCONID         { ITqconid   $$ }
183  QVARSYM        { ITqvarsym  $$ }
184  QCONSYM        { ITqconsym  $$ }
185
186  IPVARID        { ITipvarid  $$ }               -- GHC extension
187
188  CHAR           { ITchar     $$ }
189  STRING         { ITstring   $$ }
190  INTEGER        { ITinteger  $$ }
191  RATIONAL       { ITrational $$ }
192
193  PRIMCHAR       { ITprimchar   $$ }
194  PRIMSTRING     { ITprimstring $$ }
195  PRIMINTEGER    { ITprimint    $$ }
196  PRIMFLOAT      { ITprimfloat  $$ }
197  PRIMDOUBLE     { ITprimdouble $$ }
198  CLITLIT        { ITlitlit     $$ }
199
200 %monad { P } { thenP } { returnP }
201 %lexer { lexer } { ITeof }
202 %name parseModule module
203 %name parseStmt   maybe_stmt
204 %tokentype { Token }
205 %%
206
207 -----------------------------------------------------------------------------
208 -- Module Header
209
210 -- The place for module deprecation is really too restrictive, but if it
211 -- was allowed at its natural place just before 'module', we get an ugly
212 -- s/r conflict with the second alternative. Another solution would be the
213 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
214 -- either, and DEPRECATED is only expected to be used by people who really
215 -- know what they are doing. :-)
216
217 module  :: { RdrNameHsModule }
218         : srcloc 'module' modid maybemoddeprec maybeexports 'where' body 
219                 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
220         | srcloc body
221                 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
222
223 maybemoddeprec :: { Maybe DeprecTxt }
224         : '{-# DEPRECATED' STRING '#-}'         { Just $2 }
225         |  {- empty -}                          { Nothing }
226
227 body    :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
228         :  '{'            top '}'               { $2 }
229         |      layout_on  top close             { $2 }
230
231 top     :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
232         : importdecls                           { (reverse $1,[]) }
233         | importdecls ';' cvtopdecls            { (reverse $1,$3) }
234         | cvtopdecls                            { ([],$1) }
235
236 cvtopdecls :: { [RdrNameHsDecl] }
237         : topdecls                              { cvTopDecls (groupBindings $1)}
238
239 -----------------------------------------------------------------------------
240 -- The Export List
241
242 maybeexports :: { Maybe [RdrNameIE] }
243         :  '(' exportlist ')'                   { Just $2 }
244         |  {- empty -}                          { Nothing }
245
246 exportlist :: { [RdrNameIE] }
247         :  exportlist ',' export                { $3 : $1 }
248         |  exportlist ','                       { $1 }
249         |  export                               { [$1]  }
250         |  {- empty -}                          { [] }
251
252    -- GHC extension: we allow things like [] and (,,,) to be exported
253 export  :: { RdrNameIE }
254         :  qvar                                 { IEVar $1 }
255         |  gtycon                               { IEThingAbs $1 }
256         |  gtycon '(' '..' ')'                  { IEThingAll $1 }
257         |  gtycon '(' ')'                       { IEThingWith $1 [] }
258         |  gtycon '(' qcnames ')'               { IEThingWith $1 (reverse $3) }
259         |  'module' modid                       { IEModuleContents $2 }
260
261 qcnames :: { [RdrName] }
262         :  qcnames ',' qcname                   { $3 : $1 }
263         |  qcname                               { [$1]  }
264
265 qcname  :: { RdrName }
266         :  qvar                                 { $1 }
267         |  gcon                                 { $1 }
268
269 -----------------------------------------------------------------------------
270 -- Import Declarations
271
272 -- import decls can be *empty*, or even just a string of semicolons
273 -- whereas topdecls must contain at least one topdecl.
274
275 importdecls :: { [RdrNameImportDecl] }
276         : importdecls ';' importdecl            { $3 : $1 }
277         | importdecls ';'                       { $1 }
278         | importdecl                            { [ $1 ] }
279         | {- empty -}                           { [] }
280
281 importdecl :: { RdrNameImportDecl }
282         : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec 
283                 { ImportDecl (mkModuleNameFS $5) $3 $4 $6 $7 $2 }
284
285 maybe_src :: { WhereFrom }
286         : '{-# SOURCE' '#-}'                    { ImportByUserSource }
287         | {- empty -}                           { ImportByUser }
288
289 optqualified :: { Bool }
290         : 'qualified'                           { True  }
291         | {- empty -}                           { False }
292
293 maybeas :: { Maybe ModuleName }
294         : 'as' modid                            { Just $2 }
295         | {- empty -}                           { Nothing }
296
297 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
298         : impspec                               { Just $1 }
299         | {- empty -}                           { Nothing }
300
301 impspec :: { (Bool, [RdrNameIE]) }
302         :  '(' exportlist ')'                   { (False, reverse $2) }
303         |  'hiding' '(' exportlist ')'          { (True,  reverse $3) }
304
305 -----------------------------------------------------------------------------
306 -- Fixity Declarations
307
308 prec    :: { Int }
309         : {- empty -}                           { 9 }
310         | INTEGER                               {%  checkPrec $1 `thenP_`
311                                                     returnP (fromInteger $1) }
312
313 infix   :: { FixityDirection }
314         : 'infix'                               { InfixN  }
315         | 'infixl'                              { InfixL  }
316         | 'infixr'                              { InfixR }
317
318 ops     :: { [RdrName] }
319         : ops ',' op                            { $3 : $1 }
320         | op                                    { [$1] }
321
322 -----------------------------------------------------------------------------
323 -- Top-Level Declarations
324
325 topdecls :: { [RdrBinding] }
326         : topdecls ';' topdecl          { ($3 : $1) }
327         | topdecls ';'                  { $1 }
328         | topdecl                       { [$1] }
329
330 topdecl :: { RdrBinding }
331         : srcloc 'type' simpletype '=' ctype    
332                 -- Note ctype, not sigtype.
333                 -- We allow an explicit for-all but we don't insert one
334                 -- in   type Foo a = (b,b)
335                 -- Instead we just say b is out of scope
336                 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
337
338         | srcloc 'data' ctype '=' constrs deriving
339                 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
340                    returnP (RdrHsDecl (TyClD
341                       (mkTyData DataType cs c ts (reverse $5) (length $5) $6 $1))) }
342
343         | srcloc 'newtype' ctype '=' newconstr deriving
344                 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
345                    returnP (RdrHsDecl (TyClD
346                       (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
347
348         | srcloc 'class' ctype fds where
349                 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
350                    let 
351                         (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5) 
352                    in
353                    returnP (RdrHsDecl (TyClD
354                       (mkClassDecl cs c ts $4 sigs (Just binds) $1))) }
355
356         | srcloc 'instance' inst_type where
357                 { let (binds,sigs) 
358                         = cvMonoBindsAndSigs cvInstDeclSig 
359                                 (groupBindings $4)
360                   in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
361
362         | srcloc 'default' '(' types0 ')'
363                 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
364
365         | srcloc 'foreign' 'import' callconv ext_name 
366           unsafe_flag varid_no_unsafe '::' sigtype
367                 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
368
369         | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
370                 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
371
372         | srcloc 'foreign' 'label' ext_name varid '::' sigtype
373                 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
374                                         defaultCallConv $1)) }
375
376         | '{-# DEPRECATED' deprecations '#-}'           { $2 }
377         | '{-# RULES' rules '#-}'                       { $2 }
378         | decl                                          { $1 }
379
380 decls   :: { [RdrBinding] }
381         : decls ';' decl                { $3 : $1 }
382         | decls ';'                     { $1 }
383         | decl                          { [$1] }
384         | {- empty -}                   { [] }
385
386 decl    :: { RdrBinding }
387         : fixdecl                       { $1 }
388         | valdef                        { $1 }
389         | '{-# INLINE'   srcloc opt_phase qvar '#-}'     { RdrSig (InlineSig $4 $3 $2) }
390         | '{-# NOINLINE' srcloc opt_phase qvar '#-}'     { RdrSig (NoInlineSig $4 $3 $2) }
391         | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
392                 { foldr1 RdrAndBindings 
393                     (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
394         | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
395                 { RdrSig (SpecInstSig $4 $2) }
396
397 opt_phase :: { Maybe Int }
398           : INTEGER                     { Just (fromInteger $1) }
399           | {- empty -}                 { Nothing }
400
401 wherebinds :: { RdrNameHsBinds }
402         : where                 { cvBinds cvValSig (groupBindings $1) }
403
404 where   :: { [RdrBinding] }
405         : 'where' decllist              { $2 }
406         | {- empty -}                   { [] }
407
408 declbinds :: { RdrNameHsBinds }
409         : decllist                      { cvBinds cvValSig (groupBindings $1) }
410
411 decllist :: { [RdrBinding] }
412         : '{'            decls '}'      { $2 }
413         |     layout_on  decls close    { $2 }
414
415 fixdecl :: { RdrBinding }
416         : srcloc infix prec ops         { foldr1 RdrAndBindings
417                                             [ RdrSig (FixSig (FixitySig n 
418                                                             (Fixity $3 $2) $1))
419                                             | n <- $4 ] }
420
421 -----------------------------------------------------------------------------
422 -- Transformation Rules
423
424 rules   :: { RdrBinding }
425         :  rules ';' rule                       { $1 `RdrAndBindings` $3 }
426         |  rules ';'                            { $1 }
427         |  rule                                 { $1 }
428         |  {- empty -}                          { RdrNullBind }
429
430 rule    :: { RdrBinding }
431         : STRING rule_forall infixexp '=' srcloc exp
432              { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
433
434 rule_forall :: { [RdrNameRuleBndr] }
435         : 'forall' rule_var_list '.'            { $2 }
436         | {- empty -}                           { [] }
437
438 rule_var_list :: { [RdrNameRuleBndr] }
439         : rule_var                              { [$1] }
440         | rule_var rule_var_list                { $1 : $2 }
441
442 rule_var :: { RdrNameRuleBndr }
443         : varid                                 { RuleBndr $1 }
444         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
445
446 -----------------------------------------------------------------------------
447 -- Deprecations
448
449 deprecations :: { RdrBinding }
450         : deprecations ';' deprecation          { $1 `RdrAndBindings` $3 }
451         | deprecations ';'                      { $1 }
452         | deprecation                           { $1 }
453         | {- empty -}                           { RdrNullBind }
454
455 -- SUP: TEMPORARY HACK, not checking for `module Foo'
456 deprecation :: { RdrBinding }
457         : srcloc depreclist STRING
458                 { foldr RdrAndBindings RdrNullBind 
459                         [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
460
461 -----------------------------------------------------------------------------
462 -- Foreign import/export
463
464 callconv :: { Int }
465         : 'stdcall'             { stdCallConv }
466         | 'ccall'               { cCallConv }
467         | {- empty -}           { defaultCallConv }
468
469 unsafe_flag :: { Bool }
470         : 'unsafe'              { True }
471         | {- empty -}           { False }
472
473 ext_name :: { Maybe ExtName }
474         : 'dynamic'             { Just Dynamic }
475         | STRING                { Just (ExtName $1 Nothing)   }
476         | STRING STRING         { Just (ExtName $2 (Just $1)) }
477         | {- empty -}           { Nothing }
478
479
480 -----------------------------------------------------------------------------
481 -- Type signatures
482
483 opt_sig :: { Maybe RdrNameHsType }
484         : {- empty -}                   { Nothing }
485         | '::' sigtype                  { Just $2 }
486
487 opt_asig :: { Maybe RdrNameHsType }
488         : {- empty -}                   { Nothing }
489         | '::' atype                    { Just $2 }
490
491 sigtypes :: { [RdrNameHsType] }
492         : sigtype                       { [ $1 ] }
493         | sigtypes ',' sigtype          { $3 : $1 }
494
495 sigtype :: { RdrNameHsType }
496         : ctype                         { (mkHsForAllTy Nothing [] $1) }
497
498 sig_vars :: { [RdrName] }
499          : sig_vars ',' var             { $3 : $1 }
500          | var                          { [ $1 ] }
501
502 -----------------------------------------------------------------------------
503 -- Types
504
505 -- A ctype is a for-all type
506 ctype   :: { RdrNameHsType }
507         : 'forall' tyvars '.' ctype     { mkHsForAllTy (Just $2) [] $4 }
508         | context type                  { mkHsForAllTy Nothing   $1 $2 }
509         -- A type of form (context => type) is an *implicit* HsForAllTy
510         | type                          { $1 }
511
512 type :: { RdrNameHsType }
513         : gentype '->' type             { HsFunTy $1 $3 }
514         | ipvar '::' type               { mkHsIParamTy $1 $3 }
515         | gentype                       { $1 }
516
517 gentype :: { RdrNameHsType }
518         : btype                         { $1 }
519 -- Generics
520         | atype tyconop atype           { HsOpTy $1 $2 $3 }
521
522 btype :: { RdrNameHsType }
523         : btype atype                   { (HsAppTy $1 $2) }
524         | atype                         { $1 }
525
526 atype :: { RdrNameHsType }
527         : gtycon                        { HsTyVar $1 }
528         | tyvar                         { HsTyVar $1 }
529         | '(' type ',' types ')'        { HsTupleTy (mkHsTupCon tcName Boxed  ($2:$4)) ($2 : reverse $4) }
530         | '(#' types '#)'               { HsTupleTy (mkHsTupCon tcName Unboxed     $2) (reverse $2)      }
531         | '[' type ']'                  { HsListTy $2 }
532         | '(' ctype ')'                 { $2 }
533 -- Generics
534         | INTEGER                       { HsNumTy $1 }
535
536 -- An inst_type is what occurs in the head of an instance decl
537 --      e.g.  (Foo a, Gaz b) => Wibble a b
538 -- It's kept as a single type, with a MonoDictTy at the right
539 -- hand corner, for convenience.
540 inst_type :: { RdrNameHsType }
541         : ctype                         {% checkInstType $1 }
542
543 types0  :: { [RdrNameHsType] }
544         : types                         { reverse $1 }
545         | {- empty -}                   { [] }
546
547 types   :: { [RdrNameHsType] }
548         : type                          { [$1] }
549         | types  ',' type               { $3 : $1 }
550
551 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
552         : tycon tyvars                  { ($1, reverse $2) }
553
554 tyvars :: { [RdrNameHsTyVar] }
555         : tyvars tyvar                  { UserTyVar $2 : $1 }
556         | {- empty -}                   { [] }
557
558 fds :: { [([RdrName], [RdrName])] }
559         : {- empty -}                   { [] }
560         | '|' fds1                      { reverse $2 }
561
562 fds1 :: { [([RdrName], [RdrName])] }
563         : fds1 ',' fd                   { $3 : $1 }
564         | fd                            { [$1] }
565
566 fd :: { ([RdrName], [RdrName]) }
567         : varids0 '->' varids0          { (reverse $1, reverse $3) }
568
569 varids0 :: { [RdrName] }
570         : {- empty -}                   { [] }
571         | varids0 tyvar                 { $2 : $1 }
572
573 -----------------------------------------------------------------------------
574 -- Datatype declarations
575
576 newconstr :: { RdrNameConDecl }
577         : srcloc conid atype    { mkConDecl $2 [] [] (VanillaCon [Unbanged $3]) $1 }
578         | srcloc conid '{' var '::' type '}'
579                                 { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $1 }
580
581 constrs :: { [RdrNameConDecl] }
582         : constrs '|' constr            { $3 : $1 }
583         | constr                        { [$1] }
584
585 constr :: { RdrNameConDecl }
586         : srcloc forall context constr_stuff
587                 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
588         | srcloc forall constr_stuff
589                 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
590
591 forall :: { [RdrNameHsTyVar] }
592         : 'forall' tyvars '.'           { $2 }
593         | {- empty -}                   { [] }
594
595 context :: { RdrNameContext }
596         : btype '=>'                    {% checkContext $1 }
597
598 constr_stuff :: { (RdrName, RdrNameConDetails) }
599         : btype                         {% mkVanillaCon $1 []               }
600         | btype '!' atype satypes       {% mkVanillaCon $1 (Banged $3 : $4) }
601         | gtycon '{' fielddecls '}'     {% mkRecCon $1 $3 }
602         | sbtype conop sbtype           { ($2, InfixCon $1 $3) }
603
604 satypes :: { [RdrNameBangType] }
605         : atype satypes                 { Unbanged $1 : $2 }
606         | '!' atype satypes             { Banged   $2 : $3 }
607         | {- empty -}                   { [] }
608
609 sbtype :: { RdrNameBangType }
610         : btype                         { Unbanged $1 }
611         | '!' atype                     { Banged   $2 }
612
613 fielddecls :: { [([RdrName],RdrNameBangType)] }
614         : fielddecl ',' fielddecls      { $1 : $3 }
615         | fielddecl                     { [$1] }
616
617 fielddecl :: { ([RdrName],RdrNameBangType) }
618         : sig_vars '::' stype           { (reverse $1, $3) }
619
620 stype :: { RdrNameBangType }
621         : ctype                         { Unbanged $1 } 
622         | '!' atype                     { Banged   $2 }
623
624 deriving :: { Maybe [RdrName] }
625         : {- empty -}                   { Nothing }
626         | 'deriving' qtycls             { Just [$2] }
627         | 'deriving' '('          ')'   { Just [] }
628         | 'deriving' '(' dclasses ')'   { Just (reverse $3) }
629
630 dclasses :: { [RdrName] }
631         : dclasses ',' qtycls           { $3 : $1 }
632         | qtycls                        { [$1] }
633
634 -----------------------------------------------------------------------------
635 -- Value definitions
636
637 {- There's an awkward overlap with a type signature.  Consider
638         f :: Int -> Int = ...rhs...
639    Then we can't tell whether it's a type signature or a value
640    definition with a result signature until we see the '='.
641    So we have to inline enough to postpone reductions until we know.
642 -}
643
644 {-
645   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
646   instead of qvar, we get another shift/reduce-conflict. Consider the
647   following programs:
648   
649      { (^^) :: Int->Int ; }          Type signature; only var allowed
650
651      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
652                                      qvar allowed (because of instance decls)
653   
654   We can't tell whether to reduce var to qvar until after we've read the signatures.
655 -}
656
657 valdef :: { RdrBinding }
658         : infixexp srcloc opt_sig rhs           {% (checkValDef $1 $3 $4 $2) }
659         | infixexp srcloc '::' sigtype          {% (checkValSig $1 $4 $2) }
660         | var ',' sig_vars srcloc '::' sigtype  { foldr1 RdrAndBindings 
661                                                          [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
662                                                 }
663
664
665 rhs     :: { RdrNameGRHSs }
666         : '=' srcloc exp wherebinds     { (GRHSs (unguardedRHS $3 $2) 
667                                                                 $4 Nothing)}
668         | gdrhs wherebinds              { GRHSs (reverse $1) $2 Nothing }
669
670 gdrhs :: { [RdrNameGRHS] }
671         : gdrhs gdrh                    { $2 : $1 }
672         | gdrh                          { [$1] }
673
674 gdrh :: { RdrNameGRHS }
675         : '|' srcloc quals '=' exp      { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
676
677 -----------------------------------------------------------------------------
678 -- Expressions
679
680 exp   :: { RdrNameHsExpr }
681         : infixexp '::' sigtype         { (ExprWithTySig $1 $3) }
682         | infixexp 'with' dbinding      { HsWith $1 $3 }
683         | infixexp                      { $1 }
684
685 infixexp :: { RdrNameHsExpr }
686         : exp10                         { $1 }
687         | infixexp qop exp10            { (OpApp $1 (HsVar $2) 
688                                                 (panic "fixity") $3 )}
689
690 exp10 :: { RdrNameHsExpr }
691         : '\\' aexp aexps opt_asig '->' srcloc exp      
692                         {% checkPatterns ($2 : reverse $3) `thenP` \ ps -> 
693                            returnP (HsLam (Match [] ps $4 
694                                             (GRHSs (unguardedRHS $7 $6) 
695                                                    EmptyBinds Nothing))) }
696         | 'let' declbinds 'in' exp              { HsLet $2 $4 }
697         | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
698         | 'case' srcloc exp 'of' altslist       { HsCase $3 $5 $2 }
699         | '-' fexp                              { mkHsNegApp $2 }
700         | srcloc 'do' stmtlist                  { HsDo DoExpr $3 $1 }
701
702         | '_ccall_'    ccallid aexps0           { HsCCall $2 $3 False False cbot }
703         | '_ccall_GC_' ccallid aexps0           { HsCCall $2 $3 True  False cbot }
704         | '_casm_'     CLITLIT aexps0           { HsCCall $2 $3 False True  cbot }
705         | '_casm_GC_'  CLITLIT aexps0           { HsCCall $2 $3 True  True  cbot }
706
707         | scc_annot exp                         { if opt_SccProfilingOn
708                                                         then HsSCC $1 $2
709                                                         else HsPar $2 }
710
711         | fexp                                  { $1 }
712
713 scc_annot :: { FAST_STRING }
714         : '_scc_' STRING                        { $2 }
715         | '{-# SCC' STRING '#-}'                { $2 }
716
717 ccallid :: { FAST_STRING }
718         :  VARID                                { $1 }
719         |  CONID                                { $1 }
720
721 fexp    :: { RdrNameHsExpr }
722         : fexp aexp                             { (HsApp $1 $2) }
723         | aexp                                  { $1 }
724
725 aexps0  :: { [RdrNameHsExpr] }
726         : aexps                                 { (reverse $1) }
727
728 aexps   :: { [RdrNameHsExpr] }
729         : aexps aexp                            { $2 : $1 }
730         | {- empty -}                           { [] }
731
732 aexp    :: { RdrNameHsExpr }
733         : var_or_con '{|' gentype '|}'          { (HsApp $1 (HsType $3)) }
734         | aexp '{' fbinds '}'                   {% (mkRecConstrOrUpdate $1 
735                                                         (reverse $3)) }
736         | aexp1                                 { $1 }
737
738 var_or_con :: { RdrNameHsExpr }
739         : qvar                          { HsVar $1 }
740         | gcon                          { HsVar $1 }
741
742 aexp1   :: { RdrNameHsExpr }
743         : ipvar                         { HsIPVar $1 }
744         | var_or_con                    { $1 }
745         | literal                       { HsLit $1 }
746         | INTEGER                       { HsOverLit (HsIntegral   $1) }
747         | RATIONAL                      { HsOverLit (HsFractional $1) }
748         | '(' exp ')'                   { HsPar $2 }
749         | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) Boxed}
750         | '(#' texps '#)'               { ExplicitTuple (reverse $2)      Unboxed }
751         | '[' list ']'                  { $2 }
752         | '(' infixexp qop ')'          { (SectionL $2 (HsVar $3))  }
753         | '(' qopm infixexp ')'         { (SectionR $2 $3) }
754         | qvar '@' aexp                 { EAsPat $1 $3 }
755         | '_'                           { EWildPat }
756         | '~' aexp1                     { ELazyPat $2 }
757
758 texps :: { [RdrNameHsExpr] }
759         : texps ',' exp                 { $3 : $1 }
760         | exp                           { [$1] }
761
762
763 -----------------------------------------------------------------------------
764 -- List expressions
765
766 -- The rules below are little bit contorted to keep lexps left-recursive while
767 -- avoiding another shift/reduce-conflict.
768
769 list :: { RdrNameHsExpr }
770         : exp                           { ExplicitList [$1] }
771         | lexps                         { ExplicitList (reverse $1) }
772         | exp '..'                      { ArithSeqIn (From $1) }
773         | exp ',' exp '..'              { ArithSeqIn (FromThen $1 $3) }
774         | exp '..' exp                  { ArithSeqIn (FromTo $1 $3) }
775         | exp ',' exp '..' exp          { ArithSeqIn (FromThenTo $1 $3 $5) }
776         | exp srcloc pquals             {% let { body [qs] = qs;
777                                                  body  qss = [ParStmt (map reverse qss)] }
778                                            in
779                                            returnP ( HsDo ListComp
780                                                            (reverse (ExprStmt $1 $2 : body $3))
781                                                            $2
782                                                   )
783                                         }
784
785 lexps :: { [RdrNameHsExpr] }
786         : lexps ',' exp                 { $3 : $1 }
787         | exp ',' exp                   { [$3,$1] }
788
789 -----------------------------------------------------------------------------
790 -- List Comprehensions
791
792 pquals :: { [[RdrNameStmt]] }
793         : pquals '|' quals              { $3 : $1 }
794         | '|' quals                     { [$2] }
795
796 quals :: { [RdrNameStmt] }
797         : quals ',' stmt                { $3 : $1 }
798         | stmt                          { [$1] }
799
800 -----------------------------------------------------------------------------
801 -- Case alternatives
802
803 altslist :: { [RdrNameMatch] }
804         : '{'            alts '}'       { reverse $2 }
805         |     layout_on  alts  close    { reverse $2 }
806
807 alts    :: { [RdrNameMatch] }
808         : alts1                         { $1 }
809         | ';' alts                      { $2 }
810
811 alts1   :: { [RdrNameMatch] }
812         : alts1 ';' alt                 { $3 : $1 }
813         | alts1 ';'                     { $1 }
814         | alt                           { [$1] }
815
816 alt     :: { RdrNameMatch }
817         : infixexp opt_sig ralt wherebinds
818                                         {% (checkPattern $1 `thenP` \p ->
819                                            returnP (Match [] [p] $2
820                                                      (GRHSs $3 $4 Nothing))  )}
821
822 ralt :: { [RdrNameGRHS] }
823         : '->' srcloc exp               { [GRHS [ExprStmt $3 $2] $2] }
824         | gdpats                        { (reverse $1) }
825
826 gdpats :: { [RdrNameGRHS] }
827         : gdpats gdpat                  { $2 : $1 }
828         | gdpat                         { [$1] }
829
830 gdpat   :: { RdrNameGRHS }
831         : srcloc '|' quals '->' exp     { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
832
833 -----------------------------------------------------------------------------
834 -- Statement sequences
835
836 stmtlist :: { [RdrNameStmt] }
837         : '{'                   stmts '}'       { reverse $2 }
838         |     layout_on_for_do  stmts close     { reverse $2 }
839
840 -- Stmt list should really end in an expression, but it's not
841 -- convenient to enforce this here, so we throw out erroneous
842 -- statement sequences in the renamer instead.
843
844 stmts :: { [RdrNameStmt] }
845         : ';' stmts1                    { $2 }
846         | stmts1                        { $1 }
847
848 stmts1 :: { [RdrNameStmt] }
849         : stmts1 ';' stmt               { $3 : $1 }
850         | stmts1 ';'                    { $1 }
851         | stmt                          { [$1] }
852
853 -- for typing stmts at the GHCi prompt, where the input may consist of
854 -- just comments.
855 maybe_stmt :: { Maybe RdrNameStmt }
856         : stmt                          { Just $1 }
857         | {- nothing -}                 { Nothing }
858
859 stmt  :: { RdrNameStmt }
860         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
861                                            returnP (BindStmt p $4 $1) }
862         | srcloc exp                    { ExprStmt $2 $1 }
863         | srcloc 'let' declbinds        { LetStmt $3 }
864
865 -----------------------------------------------------------------------------
866 -- Record Field Update/Construction
867
868 fbinds  :: { RdrNameHsRecordBinds }
869         : fbinds ',' fbind              { $3 : $1 }
870         | fbinds ','                    { $1 }
871         | fbind                         { [$1] }
872         | {- empty -}                   { [] }
873
874 fbind   :: { (RdrName, RdrNameHsExpr, Bool) }
875         : qvar '=' exp                  { ($1,$3,False) }
876
877 -----------------------------------------------------------------------------
878 -- Implicit Parameter Bindings
879
880 dbinding :: { [(RdrName, RdrNameHsExpr)] }
881         : '{' dbinds '}'                { $2 }
882         | layout_on dbinds close        { $2 }
883
884 dbinds  :: { [(RdrName, RdrNameHsExpr)] }
885         : dbinds ';' dbind              { $3 : $1 }
886         | dbinds ';'                    { $1 }
887         | dbind                         { [$1] }
888         | {- empty -}                   { [] }
889
890 dbind   :: { (RdrName, RdrNameHsExpr) }
891 dbind   : ipvar '=' exp                 { ($1, $3) }
892
893 -----------------------------------------------------------------------------
894 -- Variables, Constructors and Operators.
895
896 depreclist :: { [RdrName] }
897 depreclist : deprec_var                 { [$1] }
898            | deprec_var ',' depreclist  { $1 : $3 }
899
900 deprec_var :: { RdrName }
901 deprec_var : var                        { $1 }
902            | tycon                      { $1 }
903
904 gtycon  :: { RdrName }
905         : qtycon                        { $1 }
906         | '(' qtyconop ')'              { $2 }
907         | '(' ')'                       { unitTyCon_RDR }
908         | '(' '->' ')'                  { funTyCon_RDR }
909         | '[' ']'                       { listTyCon_RDR }
910         | '(' commas ')'                { tupleTyCon_RDR $2 }
911
912 gcon    :: { RdrName }
913         : '(' ')'               { unitCon_RDR }
914         | '[' ']'               { nilCon_RDR }
915         | '(' commas ')'        { tupleCon_RDR $2 }
916         | qcon                  { $1 }
917
918 var     :: { RdrName }
919         : varid                 { $1 }
920         | '(' varsym ')'        { $2 }
921
922 qvar    :: { RdrName }
923         : qvarid                { $1 }
924         | '(' varsym ')'        { $2 }
925         | '(' qvarsym1 ')'      { $2 }
926 -- We've inlined qvarsym here so that the decision about
927 -- whether it's a qvar or a var can be postponed until
928 -- *after* we see the close paren.
929
930 ipvar   :: { RdrName }
931         : IPVARID               { (mkUnqual varName (tailFS $1)) }
932
933 qcon    :: { RdrName }
934         : qconid                { $1 }
935         | '(' qconsym ')'       { $2 }
936
937 varop   :: { RdrName }
938         : varsym                { $1 }
939         | '`' varid '`'         { $2 }
940
941 qvarop :: { RdrName }
942         : qvarsym               { $1 }
943         | '`' qvarid '`'        { $2 }
944
945 qvaropm :: { RdrName }
946         : qvarsym_no_minus      { $1 }
947         | '`' qvarid '`'        { $2 }
948
949 conop :: { RdrName }
950         : consym                { $1 }  
951         | '`' conid '`'         { $2 }
952
953 qconop :: { RdrName }
954         : qconsym               { $1 }
955         | '`' qconid '`'        { $2 }
956
957 -----------------------------------------------------------------------------
958 -- Any operator
959
960 op      :: { RdrName }   -- used in infix decls
961         : varop                 { $1 }
962         | conop                 { $1 }
963
964 qop     :: { RdrName {-HsExpr-} }   -- used in sections
965         : qvarop                { $1 }
966         | qconop                { $1 }
967
968 qopm    :: { RdrNameHsExpr }   -- used in sections
969         : qvaropm               { HsVar $1 }
970         | qconop                { HsVar $1 }
971
972 -----------------------------------------------------------------------------
973 -- VarIds
974
975 qvarid :: { RdrName }
976         : varid                 { $1 }
977         | QVARID                { mkQual varName $1 }
978
979 varid :: { RdrName }
980         : varid_no_unsafe       { $1 }
981         | 'unsafe'              { mkUnqual varName SLIT("unsafe") }
982
983 varid_no_unsafe :: { RdrName }
984         : VARID                 { mkUnqual varName $1 }
985         | special_id            { mkUnqual varName $1 }
986         | 'forall'              { mkUnqual varName SLIT("forall") }
987
988 tyvar   :: { RdrName }
989         : VARID                 { mkUnqual tvName $1 }
990         | special_id            { mkUnqual tvName $1 }
991         | 'unsafe'              { mkUnqual tvName SLIT("unsafe") }
992
993 -- These special_ids are treated as keywords in various places, 
994 -- but as ordinary ids elsewhere.   A special_id collects all thsee
995 -- except 'unsafe' and 'forall' whose treatment differs depending on context
996 special_id :: { UserFS }
997 special_id
998         : 'as'                  { SLIT("as") }
999         | 'qualified'           { SLIT("qualified") }
1000         | 'hiding'              { SLIT("hiding") }
1001         | 'export'              { SLIT("export") }
1002         | 'label'               { SLIT("label")  }
1003         | 'dynamic'             { SLIT("dynamic") }
1004         | 'stdcall'             { SLIT("stdcall") }
1005         | 'ccall'               { SLIT("ccall") }
1006
1007 -----------------------------------------------------------------------------
1008 -- ConIds
1009
1010 qconid :: { RdrName }
1011         : conid                 { $1 }
1012         | QCONID                { mkQual dataName $1 }
1013
1014 conid   :: { RdrName }
1015         : CONID                 { mkUnqual dataName $1 }
1016
1017 -----------------------------------------------------------------------------
1018 -- ConSyms
1019
1020 qconsym :: { RdrName }
1021         : consym                { $1 }
1022         | QCONSYM               { mkQual dataName $1 }
1023
1024 consym :: { RdrName }
1025         : CONSYM                { mkUnqual dataName $1 }
1026
1027 -----------------------------------------------------------------------------
1028 -- VarSyms
1029
1030 qvarsym :: { RdrName }
1031         : varsym                { $1 }
1032         | qvarsym1              { $1 }
1033
1034 qvarsym_no_minus :: { RdrName }
1035         : varsym_no_minus       { $1 }
1036         | qvarsym1              { $1 }
1037
1038 qvarsym1 :: { RdrName }
1039 qvarsym1 : QVARSYM              { mkQual varName $1 }
1040
1041 varsym :: { RdrName }
1042         : varsym_no_minus       { $1 }
1043         | '-'                   { mkUnqual varName SLIT("-") }
1044
1045 varsym_no_minus :: { RdrName } -- varsym not including '-'
1046         : VARSYM                { mkUnqual varName $1 }
1047         | special_sym           { mkUnqual varName $1 }
1048
1049
1050 -- See comments with special_id
1051 special_sym :: { UserFS }
1052 special_sym : '!'       { SLIT("!") }
1053             | '.'       { SLIT(".") }
1054
1055 -----------------------------------------------------------------------------
1056 -- Literals
1057
1058 literal :: { HsLit }
1059         : CHAR                  { HsChar       $1 }
1060         | STRING                { HsString     $1 }
1061         | PRIMINTEGER           { HsIntPrim    $1 }
1062         | PRIMCHAR              { HsCharPrim   $1 }
1063         | PRIMSTRING            { HsStringPrim $1 }
1064         | PRIMFLOAT             { HsFloatPrim  $1 }
1065         | PRIMDOUBLE            { HsDoublePrim $1 }
1066         | CLITLIT               { HsLitLit     $1 (error "Parser.y: CLITLIT") }
1067
1068 srcloc :: { SrcLoc }    :       {% getSrcLocP }
1069  
1070 -----------------------------------------------------------------------------
1071 -- Layout
1072
1073 close :: { () }
1074         : vccurly               { () } -- context popped in lexer.
1075         | error                 {% popContext }
1076
1077 layout_on         :: { () }     : {% layoutOn True{-strict-} }
1078 layout_on_for_do  :: { () }     : {% layoutOn False }
1079
1080 -----------------------------------------------------------------------------
1081 -- Miscellaneous (mostly renamings)
1082
1083 modid   :: { ModuleName }
1084         : CONID                 { mkModuleNameFS $1 }
1085
1086 tycon   :: { RdrName }
1087         : CONID                 { mkUnqual tcClsName $1 }
1088
1089 tyconop :: { RdrName }
1090         : CONSYM                { mkUnqual tcClsName $1 }
1091
1092 qtycon :: { RdrName }
1093         : tycon                 { $1 }
1094         | QCONID                { mkQual tcClsName $1 }
1095
1096 qtyconop :: { RdrName }
1097           : tyconop             { $1 }
1098           | QCONSYM             { mkQual tcClsName $1 }
1099
1100 qtycls  :: { RdrName }
1101         : qtycon                { $1 }
1102
1103 commas :: { Int }
1104         : commas ','                    { $1 + 1 }
1105         | ','                           { 2 }
1106
1107 -----------------------------------------------------------------------------
1108
1109 {
1110 happyError :: P a
1111 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
1112 }