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