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