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