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