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