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