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