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