7631659b9da4c0cfd5d68afa5dc3589c6c7e7906
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.55 2001/02/26 15:06:59 simonmar Exp $
4
5 Haskell grammar.
6
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
9 -}
10
11 {
12 module Parser ( parseModule, parseStmt ) 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 '=' sigtype  
332                 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
333
334         | srcloc 'data' ctype '=' constrs deriving
335                 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
336                    returnP (RdrHsDecl (TyClD
337                       (mkTyData DataType cs c ts (reverse $5) (length $5) $6 $1))) }
338
339         | srcloc 'newtype' ctype '=' newconstr deriving
340                 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
341                    returnP (RdrHsDecl (TyClD
342                       (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
343
344         | srcloc 'class' ctype fds where
345                 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
346                    let 
347                         (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5) 
348                    in
349                    returnP (RdrHsDecl (TyClD
350                       (mkClassDecl cs c ts $4 sigs (Just binds) $1))) }
351
352         | srcloc 'instance' inst_type where
353                 { let (binds,sigs) 
354                         = cvMonoBindsAndSigs cvInstDeclSig 
355                                 (groupBindings $4)
356                   in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
357
358         | srcloc 'default' '(' types0 ')'
359                 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
360
361         | srcloc 'foreign' 'import' callconv ext_name 
362           unsafe_flag varid_no_unsafe '::' sigtype
363                 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
364
365         | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
366                 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
367
368         | srcloc 'foreign' 'label' ext_name varid '::' sigtype
369                 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
370                                         defaultCallConv $1)) }
371
372         | '{-# DEPRECATED' deprecations '#-}'           { $2 }
373         | '{-# RULES' rules '#-}'                       { $2 }
374         | decl                                          { $1 }
375
376 decls   :: { [RdrBinding] }
377         : decls ';' decl                { $3 : $1 }
378         | decls ';'                     { $1 }
379         | decl                          { [$1] }
380         | {- empty -}                   { [] }
381
382 decl    :: { RdrBinding }
383         : fixdecl                       { $1 }
384         | valdef                        { $1 }
385         | '{-# INLINE'   srcloc opt_phase qvar '#-}'    { RdrSig (InlineSig $4 $3 $2) }
386         | '{-# NOINLINE' srcloc opt_phase qvar '#-}'    { RdrSig (NoInlineSig $4 $3 $2) }
387         | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
388                 { foldr1 RdrAndBindings 
389                     (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
390         | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
391                 { RdrSig (SpecInstSig $4 $2) }
392
393 opt_phase :: { Maybe Int }
394           : INTEGER                     { Just (fromInteger $1) }
395           | {- empty -}                 { Nothing }
396
397 wherebinds :: { RdrNameHsBinds }
398         : where                 { cvBinds cvValSig (groupBindings $1) }
399
400 where   :: { [RdrBinding] }
401         : 'where' decllist              { $2 }
402         | {- empty -}                   { [] }
403
404 declbinds :: { RdrNameHsBinds }
405         : decllist                      { cvBinds cvValSig (groupBindings $1) }
406
407 decllist :: { [RdrBinding] }
408         : '{'            decls '}'      { $2 }
409         |     layout_on  decls close    { $2 }
410
411 fixdecl :: { RdrBinding }
412         : srcloc infix prec ops         { foldr1 RdrAndBindings
413                                             [ RdrSig (FixSig (FixitySig n 
414                                                             (Fixity $3 $2) $1))
415                                             | n <- $4 ] }
416
417 -----------------------------------------------------------------------------
418 -- Transformation Rules
419
420 rules   :: { RdrBinding }
421         :  rules ';' rule                       { $1 `RdrAndBindings` $3 }
422         |  rules ';'                            { $1 }
423         |  rule                                 { $1 }
424         |  {- empty -}                          { RdrNullBind }
425
426 rule    :: { RdrBinding }
427         : STRING rule_forall fexp '=' srcloc exp
428              { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
429
430 rule_forall :: { [RdrNameRuleBndr] }
431         : 'forall' rule_var_list '.'            { $2 }
432         | {- empty -}                           { [] }
433
434 rule_var_list :: { [RdrNameRuleBndr] }
435         : rule_var                              { [$1] }
436         | rule_var rule_var_list                { $1 : $2 }
437
438 rule_var :: { RdrNameRuleBndr }
439         : varid                                 { RuleBndr $1 }
440         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
441
442 -----------------------------------------------------------------------------
443 -- Deprecations
444
445 deprecations :: { RdrBinding }
446         : deprecations ';' deprecation          { $1 `RdrAndBindings` $3 }
447         | deprecations ';'                      { $1 }
448         | deprecation                           { $1 }
449         | {- empty -}                           { RdrNullBind }
450
451 -- SUP: TEMPORARY HACK, not checking for `module Foo'
452 deprecation :: { RdrBinding }
453         : srcloc depreclist STRING
454                 { foldr RdrAndBindings RdrNullBind 
455                         [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
456
457 -----------------------------------------------------------------------------
458 -- Foreign import/export
459
460 callconv :: { Int }
461         : 'stdcall'             { stdCallConv }
462         | 'ccall'               { cCallConv }
463         | {- empty -}           { defaultCallConv }
464
465 unsafe_flag :: { Bool }
466         : 'unsafe'              { True }
467         | {- empty -}           { False }
468
469 ext_name :: { Maybe ExtName }
470         : 'dynamic'             { Just Dynamic }
471         | STRING                { Just (ExtName $1 Nothing)   }
472         | STRING STRING         { Just (ExtName $2 (Just $1)) }
473         | {- empty -}           { Nothing }
474
475
476 -----------------------------------------------------------------------------
477 -- Type signatures
478
479 opt_sig :: { Maybe RdrNameHsType }
480         : {- empty -}                   { Nothing }
481         | '::' sigtype                  { Just $2 }
482
483 opt_asig :: { Maybe RdrNameHsType }
484         : {- empty -}                   { Nothing }
485         | '::' atype                    { Just $2 }
486
487 sigtypes :: { [RdrNameHsType] }
488         : sigtype                       { [ $1 ] }
489         | sigtypes ',' sigtype          { $3 : $1 }
490
491 sigtype :: { RdrNameHsType }
492         : ctype                         { (mkHsForAllTy Nothing [] $1) }
493
494 sig_vars :: { [RdrName] }
495          : sig_vars ',' var             { $3 : $1 }
496          | var                          { [ $1 ] }
497
498 -----------------------------------------------------------------------------
499 -- Types
500
501 -- A ctype is a for-all type
502 ctype   :: { RdrNameHsType }
503         : 'forall' tyvars '.' ctype     { mkHsForAllTy (Just $2) [] $4 }
504         | context type                  { mkHsForAllTy Nothing   $1 $2 }
505         -- A type of form (context => type) is an *implicit* HsForAllTy
506         | type                          { $1 }
507
508 type :: { RdrNameHsType }
509         : gentype '->' type             { HsFunTy $1 $3 }
510         | ipvar '::' type               { mkHsIParamTy $1 $3 }
511         | gentype                       { $1 }
512
513 gentype :: { RdrNameHsType }
514         : btype                         { $1 }
515 -- Generics
516         | atype tyconop atype           { HsOpTy $1 $2 $3 }
517
518 btype :: { RdrNameHsType }
519         : btype atype                   { (HsAppTy $1 $2) }
520         | atype                         { $1 }
521
522 atype :: { RdrNameHsType }
523         : gtycon                        { HsTyVar $1 }
524         | tyvar                         { HsTyVar $1 }
525         | '(' type ',' types ')'        { HsTupleTy (mkHsTupCon tcName Boxed  ($2:$4)) ($2 : reverse $4) }
526         | '(#' types '#)'               { HsTupleTy (mkHsTupCon tcName Unboxed     $2) (reverse $2)      }
527         | '[' type ']'                  { HsListTy $2 }
528         | '(' ctype ')'                 { $2 }
529 -- Generics
530         | INTEGER                       { HsNumTy $1 }
531
532 -- An inst_type is what occurs in the head of an instance decl
533 --      e.g.  (Foo a, Gaz b) => Wibble a b
534 -- It's kept as a single type, with a MonoDictTy at the right
535 -- hand corner, for convenience.
536 inst_type :: { RdrNameHsType }
537         : ctype                         {% checkInstType $1 }
538
539 types0  :: { [RdrNameHsType] }
540         : types                         { reverse $1 }
541         | {- empty -}                   { [] }
542
543 types   :: { [RdrNameHsType] }
544         : type                          { [$1] }
545         | types  ',' type               { $3 : $1 }
546
547 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
548         : tycon tyvars                  { ($1, reverse $2) }
549
550 tyvars :: { [RdrNameHsTyVar] }
551         : tyvars tyvar                  { UserTyVar $2 : $1 }
552         | {- empty -}                   { [] }
553
554 fds :: { [([RdrName], [RdrName])] }
555         : {- empty -}                   { [] }
556         | '|' fds1                      { reverse $2 }
557
558 fds1 :: { [([RdrName], [RdrName])] }
559         : fds1 ',' fd                   { $3 : $1 }
560         | fd                            { [$1] }
561
562 fd :: { ([RdrName], [RdrName]) }
563         : varids0 '->' varids0          { (reverse $1, reverse $3) }
564
565 varids0 :: { [RdrName] }
566         : {- empty -}                   { [] }
567         | varids0 tyvar                 { $2 : $1 }
568
569 -----------------------------------------------------------------------------
570 -- Datatype declarations
571
572 newconstr :: { RdrNameConDecl }
573         : srcloc conid atype    { mkConDecl $2 [] [] (VanillaCon [Unbanged $3]) $1 }
574         | srcloc conid '{' var '::' type '}'
575                                 { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $1 }
576
577 constrs :: { [RdrNameConDecl] }
578         : constrs '|' constr            { $3 : $1 }
579         | constr                        { [$1] }
580
581 constr :: { RdrNameConDecl }
582         : srcloc forall context constr_stuff
583                 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
584         | srcloc forall constr_stuff
585                 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
586
587 forall :: { [RdrNameHsTyVar] }
588         : 'forall' tyvars '.'           { $2 }
589         | {- empty -}                   { [] }
590
591 context :: { RdrNameContext }
592         : btype '=>'                    {% checkContext $1 }
593
594 constr_stuff :: { (RdrName, RdrNameConDetails) }
595         : btype                         {% mkVanillaCon $1 []               }
596         | btype '!' atype satypes       {% mkVanillaCon $1 (Banged $3 : $4) }
597         | gtycon '{' fielddecls '}'     {% mkRecCon $1 $3 }
598         | sbtype conop sbtype           { ($2, InfixCon $1 $3) }
599
600 satypes :: { [RdrNameBangType] }
601         : atype satypes                 { Unbanged $1 : $2 }
602         | '!' atype satypes             { Banged   $2 : $3 }
603         | {- empty -}                   { [] }
604
605 sbtype :: { RdrNameBangType }
606         : btype                         { Unbanged $1 }
607         | '!' atype                     { Banged   $2 }
608
609 fielddecls :: { [([RdrName],RdrNameBangType)] }
610         : fielddecl ',' fielddecls      { $1 : $3 }
611         | fielddecl                     { [$1] }
612
613 fielddecl :: { ([RdrName],RdrNameBangType) }
614         : sig_vars '::' stype           { (reverse $1, $3) }
615
616 stype :: { RdrNameBangType }
617         : ctype                         { Unbanged $1 } 
618         | '!' atype                     { Banged   $2 }
619
620 deriving :: { Maybe [RdrName] }
621         : {- empty -}                   { Nothing }
622         | 'deriving' qtycls             { Just [$2] }
623         | 'deriving' '('          ')'   { Just [] }
624         | 'deriving' '(' dclasses ')'   { Just (reverse $3) }
625
626 dclasses :: { [RdrName] }
627         : dclasses ',' qtycls           { $3 : $1 }
628         | qtycls                        { [$1] }
629
630 -----------------------------------------------------------------------------
631 -- Value definitions
632
633 {- There's an awkward overlap with a type signature.  Consider
634         f :: Int -> Int = ...rhs...
635    Then we can't tell whether it's a type signature or a value
636    definition with a result signature until we see the '='.
637    So we have to inline enough to postpone reductions until we know.
638 -}
639
640 {-
641   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
642   instead of qvar, we get another shift/reduce-conflict. Consider the
643   following programs:
644   
645      { (^^) :: Int->Int ; }          Type signature; only var allowed
646
647      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
648                                      qvar allowed (because of instance decls)
649   
650   We can't tell whether to reduce var to qvar until after we've read the signatures.
651 -}
652
653 valdef :: { RdrBinding }
654         : infixexp srcloc opt_sig rhs           {% (checkValDef $1 $3 $4 $2) }
655         | infixexp srcloc '::' sigtype          {% (checkValSig $1 $4 $2) }
656         | var ',' sig_vars srcloc '::' sigtype  { foldr1 RdrAndBindings 
657                                                          [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
658                                                 }
659
660
661 rhs     :: { RdrNameGRHSs }
662         : '=' srcloc exp wherebinds     { (GRHSs (unguardedRHS $3 $2) 
663                                                                 $4 Nothing)}
664         | gdrhs wherebinds              { GRHSs (reverse $1) $2 Nothing }
665
666 gdrhs :: { [RdrNameGRHS] }
667         : gdrhs gdrh                    { $2 : $1 }
668         | gdrh                          { [$1] }
669
670 gdrh :: { RdrNameGRHS }
671         : '|' srcloc quals '=' exp      { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
672
673 -----------------------------------------------------------------------------
674 -- Expressions
675
676 exp   :: { RdrNameHsExpr }
677         : infixexp '::' sigtype         { (ExprWithTySig $1 $3) }
678         | infixexp 'with' dbinding      { HsWith $1 $3 }
679         | infixexp                      { $1 }
680
681 infixexp :: { RdrNameHsExpr }
682         : exp10                         { $1 }
683         | infixexp qop exp10            { (OpApp $1 (HsVar $2) 
684                                                 (panic "fixity") $3 )}
685
686 exp10 :: { RdrNameHsExpr }
687         : '\\' aexp aexps opt_asig '->' srcloc exp      
688                         {% checkPatterns ($2 : reverse $3) `thenP` \ ps -> 
689                            returnP (HsLam (Match [] ps $4 
690                                             (GRHSs (unguardedRHS $7 $6) 
691                                                    EmptyBinds Nothing))) }
692         | 'let' declbinds 'in' exp              { HsLet $2 $4 }
693         | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
694         | 'case' srcloc exp 'of' altslist       { HsCase $3 $5 $2 }
695         | '-' fexp                              { mkHsNegApp $2 }
696         | srcloc 'do' stmtlist                  { HsDo DoExpr $3 $1 }
697
698         | '_ccall_'    ccallid aexps0           { HsCCall $2 $3 False False cbot }
699         | '_ccall_GC_' ccallid aexps0           { HsCCall $2 $3 True  False cbot }
700         | '_casm_'     CLITLIT aexps0           { HsCCall $2 $3 False True  cbot }
701         | '_casm_GC_'  CLITLIT aexps0           { HsCCall $2 $3 True  True  cbot }
702
703         | scc_annot exp                         { if opt_SccProfilingOn
704                                                         then HsSCC $1 $2
705                                                         else HsPar $2 }
706
707         | fexp                                  { $1 }
708
709 scc_annot :: { FAST_STRING }
710         : '_scc_' STRING                        { $2 }
711         | '{-# SCC' STRING '#-}'                { $2 }
712
713 ccallid :: { FAST_STRING }
714         :  VARID                                { $1 }
715         |  CONID                                { $1 }
716
717 fexp    :: { RdrNameHsExpr }
718         : fexp aexp                             { (HsApp $1 $2) }
719         | aexp                                  { $1 }
720
721 aexps0  :: { [RdrNameHsExpr] }
722         : aexps                                 { (reverse $1) }
723
724 aexps   :: { [RdrNameHsExpr] }
725         : aexps aexp                            { $2 : $1 }
726         | {- empty -}                           { [] }
727
728 aexp    :: { RdrNameHsExpr }
729         : var_or_con '{|' gentype '|}'          { (HsApp $1 (HsType $3)) }
730         | aexp '{' fbinds '}'                   {% (mkRecConstrOrUpdate $1 
731                                                         (reverse $3)) }
732         | aexp1                                 { $1 }
733
734 var_or_con :: { RdrNameHsExpr }
735         : qvar                          { HsVar $1 }
736         | gcon                          { HsVar $1 }
737
738 aexp1   :: { RdrNameHsExpr }
739         : ipvar                         { HsIPVar $1 }
740         | var_or_con                    { $1 }
741         | literal                       { HsLit $1 }
742         | INTEGER                       { HsOverLit (HsIntegral   $1) }
743         | RATIONAL                      { HsOverLit (HsFractional $1) }
744         | '(' exp ')'                   { HsPar $2 }
745         | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) Boxed}
746         | '(#' texps '#)'               { ExplicitTuple (reverse $2)      Unboxed }
747         | '[' list ']'                  { $2 }
748         | '(' infixexp qop ')'          { (SectionL $2 (HsVar $3))  }
749         | '(' qopm infixexp ')'         { (SectionR $2 $3) }
750         | qvar '@' aexp                 { EAsPat $1 $3 }
751         | '_'                           { EWildPat }
752         | '~' aexp1                     { ELazyPat $2 }
753
754 texps :: { [RdrNameHsExpr] }
755         : texps ',' exp                 { $3 : $1 }
756         | exp                           { [$1] }
757
758
759 -----------------------------------------------------------------------------
760 -- List expressions
761
762 -- The rules below are little bit contorted to keep lexps left-recursive while
763 -- avoiding another shift/reduce-conflict.
764
765 list :: { RdrNameHsExpr }
766         : exp                           { ExplicitList [$1] }
767         | lexps                         { ExplicitList (reverse $1) }
768         | exp '..'                      { ArithSeqIn (From $1) }
769         | exp ',' exp '..'              { ArithSeqIn (FromThen $1 $3) }
770         | exp '..' exp                  { ArithSeqIn (FromTo $1 $3) }
771         | exp ',' exp '..' exp          { ArithSeqIn (FromThenTo $1 $3 $5) }
772         | exp srcloc pquals             {% let { body [qs] = qs;
773                                                  body  qss = [ParStmt (map reverse qss)] }
774                                            in
775                                            returnP ( HsDo ListComp
776                                                            (reverse (ExprStmt $1 $2 : body $3))
777                                                            $2
778                                                   )
779                                         }
780
781 lexps :: { [RdrNameHsExpr] }
782         : lexps ',' exp                 { $3 : $1 }
783         | exp ',' exp                   { [$3,$1] }
784
785 -----------------------------------------------------------------------------
786 -- List Comprehensions
787
788 pquals :: { [[RdrNameStmt]] }
789         : pquals '|' quals              { $3 : $1 }
790         | '|' quals                     { [$2] }
791
792 quals :: { [RdrNameStmt] }
793         : quals ',' stmt                { $3 : $1 }
794         | stmt                          { [$1] }
795
796 -----------------------------------------------------------------------------
797 -- Case alternatives
798
799 altslist :: { [RdrNameMatch] }
800         : '{'            alts '}'       { reverse $2 }
801         |     layout_on  alts  close    { reverse $2 }
802
803 alts    :: { [RdrNameMatch] }
804         : alts1                         { $1 }
805         | ';' alts                      { $2 }
806
807 alts1   :: { [RdrNameMatch] }
808         : alts1 ';' alt                 { $3 : $1 }
809         | alts1 ';'                     { $1 }
810         | alt                           { [$1] }
811
812 alt     :: { RdrNameMatch }
813         : infixexp opt_sig ralt wherebinds
814                                         {% (checkPattern $1 `thenP` \p ->
815                                            returnP (Match [] [p] $2
816                                                      (GRHSs $3 $4 Nothing))  )}
817
818 ralt :: { [RdrNameGRHS] }
819         : '->' srcloc exp               { [GRHS [ExprStmt $3 $2] $2] }
820         | gdpats                        { (reverse $1) }
821
822 gdpats :: { [RdrNameGRHS] }
823         : gdpats gdpat                  { $2 : $1 }
824         | gdpat                         { [$1] }
825
826 gdpat   :: { RdrNameGRHS }
827         : srcloc '|' quals '->' exp     { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
828
829 -----------------------------------------------------------------------------
830 -- Statement sequences
831
832 stmtlist :: { [RdrNameStmt] }
833         : '{'                   stmts '}'       { reverse $2 }
834         |     layout_on_for_do  stmts close     { reverse $2 }
835
836 -- Stmt list should really end in an expression, but it's not
837 -- convenient to enforce this here, so we throw out erroneous
838 -- statement sequences in the renamer instead.
839
840 stmts :: { [RdrNameStmt] }
841         : ';' stmts1                    { $2 }
842         | stmts1                        { $1 }
843
844 stmts1 :: { [RdrNameStmt] }
845         : stmts1 ';' stmt               { $3 : $1 }
846         | stmts1 ';'                    { $1 }
847         | stmt                          { [$1] }
848
849 -- for typing stmts at the GHCi prompt, where the input may consist of
850 -- just comments.
851 maybe_stmt :: { Maybe RdrNameStmt }
852         : stmt                          { Just $1 }
853         | {- nothing -}                 { Nothing }
854
855 stmt  :: { RdrNameStmt }
856         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
857                                            returnP (BindStmt p $4 $1) }
858         | srcloc exp                    { ExprStmt $2 $1 }
859         | srcloc 'let' declbinds        { LetStmt $3 }
860
861 -----------------------------------------------------------------------------
862 -- Record Field Update/Construction
863
864 fbinds  :: { RdrNameHsRecordBinds }
865         : fbinds ',' fbind              { $3 : $1 }
866         | fbinds ','                    { $1 }
867         | fbind                         { [$1] }
868         | {- empty -}                   { [] }
869
870 fbind   :: { (RdrName, RdrNameHsExpr, Bool) }
871         : qvar '=' exp                  { ($1,$3,False) }
872
873 -----------------------------------------------------------------------------
874 -- Implicit Parameter Bindings
875
876 dbinding :: { [(RdrName, RdrNameHsExpr)] }
877         : '{' dbinds '}'                { $2 }
878         | layout_on dbinds close        { $2 }
879
880 dbinds  :: { [(RdrName, RdrNameHsExpr)] }
881         : dbinds ';' dbind              { $3 : $1 }
882         | dbinds ';'                    { $1 }
883         | dbind                         { [$1] }
884         | {- empty -}                   { [] }
885
886 dbind   :: { (RdrName, RdrNameHsExpr) }
887 dbind   : ipvar '=' exp                 { ($1, $3) }
888
889 -----------------------------------------------------------------------------
890 -- Variables, Constructors and Operators.
891
892 depreclist :: { [RdrName] }
893 depreclist : deprec_var                 { [$1] }
894            | deprec_var ',' depreclist  { $1 : $3 }
895
896 deprec_var :: { RdrName }
897 deprec_var : var                        { $1 }
898            | tycon                      { $1 }
899
900 gtycon  :: { RdrName }
901         : qtycon                        { $1 }
902         | '(' qtyconop ')'              { $2 }
903         | '(' ')'                       { unitTyCon_RDR }
904         | '(' '->' ')'                  { funTyCon_RDR }
905         | '[' ']'                       { listTyCon_RDR }
906         | '(' commas ')'                { tupleTyCon_RDR $2 }
907
908 gcon    :: { RdrName }
909         : '(' ')'               { unitCon_RDR }
910         | '[' ']'               { nilCon_RDR }
911         | '(' commas ')'        { tupleCon_RDR $2 }
912         | qcon                  { $1 }
913
914 var     :: { RdrName }
915         : varid                 { $1 }
916         | '(' varsym ')'        { $2 }
917
918 qvar    :: { RdrName }
919         : qvarid                { $1 }
920         | '(' varsym ')'        { $2 }
921         | '(' qvarsym1 ')'      { $2 }
922 -- We've inlined qvarsym here so that the decision about
923 -- whether it's a qvar or a var can be postponed until
924 -- *after* we see the close paren.
925
926 ipvar   :: { RdrName }
927         : IPVARID               { (mkUnqual varName (tailFS $1)) }
928
929 qcon    :: { RdrName }
930         : qconid                { $1 }
931         | '(' qconsym ')'       { $2 }
932
933 varop   :: { RdrName }
934         : varsym                { $1 }
935         | '`' varid '`'         { $2 }
936
937 qvarop :: { RdrName }
938         : qvarsym               { $1 }
939         | '`' qvarid '`'        { $2 }
940
941 qvaropm :: { RdrName }
942         : qvarsym_no_minus      { $1 }
943         | '`' qvarid '`'        { $2 }
944
945 conop :: { RdrName }
946         : consym                { $1 }  
947         | '`' conid '`'         { $2 }
948
949 qconop :: { RdrName }
950         : qconsym               { $1 }
951         | '`' qconid '`'        { $2 }
952
953 -----------------------------------------------------------------------------
954 -- Any operator
955
956 op      :: { RdrName }   -- used in infix decls
957         : varop                 { $1 }
958         | conop                 { $1 }
959
960 qop     :: { RdrName {-HsExpr-} }   -- used in sections
961         : qvarop                { $1 }
962         | qconop                { $1 }
963
964 qopm    :: { RdrNameHsExpr }   -- used in sections
965         : qvaropm               { HsVar $1 }
966         | qconop                { HsVar $1 }
967
968 -----------------------------------------------------------------------------
969 -- VarIds
970
971 qvarid :: { RdrName }
972         : varid                 { $1 }
973         | QVARID                { mkQual varName $1 }
974
975 varid :: { RdrName }
976         : varid_no_unsafe       { $1 }
977         | 'unsafe'              { mkUnqual varName SLIT("unsafe") }
978
979 varid_no_unsafe :: { RdrName }
980         : VARID                 { mkUnqual varName $1 }
981         | special_id            { mkUnqual varName $1 }
982         | 'forall'              { mkUnqual varName SLIT("forall") }
983
984 tyvar   :: { RdrName }
985         : VARID                 { mkUnqual tvName $1 }
986         | special_id            { mkUnqual tvName $1 }
987         | 'unsafe'              { mkUnqual tvName SLIT("unsafe") }
988
989 -- These special_ids are treated as keywords in various places, 
990 -- but as ordinary ids elsewhere.   A special_id collects all thsee
991 -- except 'unsafe' and 'forall' whose treatment differs depending on context
992 special_id :: { UserFS }
993 special_id
994         : 'as'                  { SLIT("as") }
995         | 'qualified'           { SLIT("qualified") }
996         | 'hiding'              { SLIT("hiding") }
997         | 'export'              { SLIT("export") }
998         | 'label'               { SLIT("label")  }
999         | 'dynamic'             { SLIT("dynamic") }
1000         | 'stdcall'             { SLIT("stdcall") }
1001         | 'ccall'               { SLIT("ccall") }
1002
1003 -----------------------------------------------------------------------------
1004 -- ConIds
1005
1006 qconid :: { RdrName }
1007         : conid                 { $1 }
1008         | QCONID                { mkQual dataName $1 }
1009
1010 conid   :: { RdrName }
1011         : CONID                 { mkUnqual dataName $1 }
1012
1013 -----------------------------------------------------------------------------
1014 -- ConSyms
1015
1016 qconsym :: { RdrName }
1017         : consym                { $1 }
1018         | QCONSYM               { mkQual dataName $1 }
1019
1020 consym :: { RdrName }
1021         : CONSYM                { mkUnqual dataName $1 }
1022
1023 -----------------------------------------------------------------------------
1024 -- VarSyms
1025
1026 qvarsym :: { RdrName }
1027         : varsym                { $1 }
1028         | qvarsym1              { $1 }
1029
1030 qvarsym_no_minus :: { RdrName }
1031         : varsym_no_minus       { $1 }
1032         | qvarsym1              { $1 }
1033
1034 qvarsym1 :: { RdrName }
1035 qvarsym1 : QVARSYM              { mkQual varName $1 }
1036
1037 varsym :: { RdrName }
1038         : varsym_no_minus       { $1 }
1039         | '-'                   { mkUnqual varName SLIT("-") }
1040
1041 varsym_no_minus :: { RdrName } -- varsym not including '-'
1042         : VARSYM                { mkUnqual varName $1 }
1043         | special_sym           { mkUnqual varName $1 }
1044
1045
1046 -- See comments with special_id
1047 special_sym :: { UserFS }
1048 special_sym : '!'       { SLIT("!") }
1049             | '.'       { SLIT(".") }
1050
1051 -----------------------------------------------------------------------------
1052 -- Literals
1053
1054 literal :: { HsLit }
1055         : CHAR                  { HsChar       $1 }
1056         | STRING                { HsString     $1 }
1057         | PRIMINTEGER           { HsIntPrim    $1 }
1058         | PRIMCHAR              { HsCharPrim   $1 }
1059         | PRIMSTRING            { HsStringPrim $1 }
1060         | PRIMFLOAT             { HsFloatPrim  $1 }
1061         | PRIMDOUBLE            { HsDoublePrim $1 }
1062         | CLITLIT               { HsLitLit     $1 (error "Parser.y: CLITLIT") }
1063
1064 srcloc :: { SrcLoc }    :       {% getSrcLocP }
1065  
1066 -----------------------------------------------------------------------------
1067 -- Layout
1068
1069 close :: { () }
1070         : vccurly               { () } -- context popped in lexer.
1071         | error                 {% popContext }
1072
1073 layout_on         :: { () }     : {% layoutOn True{-strict-} }
1074 layout_on_for_do  :: { () }     : {% layoutOn False }
1075
1076 -----------------------------------------------------------------------------
1077 -- Miscellaneous (mostly renamings)
1078
1079 modid   :: { ModuleName }
1080         : CONID                 { mkModuleNameFS $1 }
1081
1082 tycon   :: { RdrName }
1083         : CONID                 { mkUnqual tcClsName $1 }
1084
1085 tyconop :: { RdrName }
1086         : CONSYM                { mkUnqual tcClsName $1 }
1087
1088 qtycon :: { RdrName }
1089         : tycon                 { $1 }
1090         | QCONID                { mkQual tcClsName $1 }
1091
1092 qtyconop :: { RdrName }
1093           : tyconop             { $1 }
1094           | QCONSYM             { mkQual tcClsName $1 }
1095
1096 qtycls  :: { RdrName }
1097         : qtycon                { $1 }
1098
1099 commas :: { Int }
1100         : commas ','                    { $1 + 1 }
1101         | ','                           { 2 }
1102
1103 -----------------------------------------------------------------------------
1104
1105 {
1106 happyError :: P a
1107 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
1108 }