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