[project @ 1999-09-01 14:08:19 by sof]
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.14 1999/09/01 14:08:19 sof 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 qvar '#-}'      { RdrSig (InlineSig $3 $2) }
371         | '{-# NOINLINE' srcloc qvar '#-}'      { RdrSig (NoInlineSig $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 sigtypes :: { [RdrNameHsType] }
380         : sigtype                       { [ $1 ] }
381         | sigtypes ',' sigtype          { $3 : $1 }
382
383 wherebinds :: { RdrNameHsBinds }
384         : where                 { cvBinds cvValSig (groupBindings $1) }
385
386 where   :: { [RdrBinding] }
387         : 'where' decllist              { $2 }
388         | {- empty -}                   { [] }
389
390 declbinds :: { RdrNameHsBinds }
391         : decllist                      { cvBinds cvValSig (groupBindings $1) }
392
393 decllist :: { [RdrBinding] }
394         : '{'            decls '}'      { $2 }
395         |     layout_on  decls close    { $2 }
396
397 fixdecl :: { RdrBinding }
398         : srcloc infix prec ops         { foldr1 RdrAndBindings
399                                             [ RdrSig (FixSig (FixitySig n 
400                                                             (Fixity $3 $2) $1))
401                                             | n <- $4 ] }
402
403 signdecl :: { RdrBinding }
404         : vars srcloc '::' sigtype      { foldr1 RdrAndBindings 
405                                               [ RdrSig (Sig n $4 $2) | n <- $1 ] }
406
407 sigtype :: { RdrNameHsType }
408         : ctype                 { mkHsForAllTy Nothing [] $1 }
409
410 {-
411   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
412   instead of qvar, we get another shift/reduce-conflict. Consider the
413   following programs:
414   
415      { (+) :: ... }          only var
416      { (+) x y  = ... }      could (incorrectly) be qvar
417   
418   We re-use expressions for patterns, so a qvar would be allowed in patterns
419   instead of a var only (which would be correct). But deciding what the + is,
420   would require more lookahead. So let's check for ourselves...
421 -}
422
423 vars    :: { [RdrName] }
424         : vars ',' var                  { $3 : $1 }
425         | qvar                          { [ $1 ] }
426
427 -----------------------------------------------------------------------------
428 -- Transformation Rules
429
430 rules   :: { RdrBinding }
431         :  rules ';' rule                       { $1 `RdrAndBindings` $3 }
432         |  rules ';'                            { $1 }
433         |  rule                                 { $1 }
434         |  {- empty -}                          { RdrNullBind }
435
436 rule    :: { RdrBinding }
437         : STRING rule_forall fexp '=' srcloc exp
438              { RdrHsDecl (RuleD (RuleDecl $1 [] $2 $3 $6 $5)) }
439
440 rule_forall :: { [RdrNameRuleBndr] }
441         : 'forall' rule_var_list '.'            { $2 }
442         | {- empty -}                           { [] }
443
444 rule_var_list :: { [RdrNameRuleBndr] }
445         : rule_var                              { [$1] }
446         | rule_var ',' rule_var_list            { $1 : $3 }
447
448 rule_var :: { RdrNameRuleBndr }
449         : varid                                 { RuleBndr $1 }
450         | varid '::' ctype                      { RuleBndrSig $1 $3 }
451
452 -----------------------------------------------------------------------------
453 -- Foreign import/export
454
455 callconv :: { Int }
456         : 'stdcall'             { stdCallConv }
457         | 'ccall'               { cCallConv }
458         | {- empty -}           { defaultCallConv }
459
460 unsafe_flag :: { Bool }
461         : 'unsafe'              { True }
462         | {- empty -}           { False }
463
464 ext_name :: { Maybe ExtName }
465         : 'dynamic'             { Just Dynamic }
466         | STRING                { Just (ExtName $1 Nothing)   }
467         | STRING STRING         { Just (ExtName $2 (Just $1)) }
468         | {- empty -}           { Nothing }
469
470 -----------------------------------------------------------------------------
471 -- Types
472
473 {- ToDo: forall stuff -}
474
475 type :: { RdrNameHsType }
476         : btype '->' type               { MonoFunTy $1 $3 }
477         | btype                         { $1 }
478
479 btype :: { RdrNameHsType }
480         : btype atype                   { MonoTyApp $1 $2 }
481         | atype                         { $1 }
482
483 atype :: { RdrNameHsType }
484         : gtycon                        { MonoTyVar $1 }
485         | tyvar                         { MonoTyVar $1 }
486         | '(' type ',' types ')'        { MonoTupleTy ($2 : reverse $4) True }
487         | '(#' types '#)'               { MonoTupleTy (reverse $2) False }
488         | '[' type ']'                  { MonoListTy $2 }
489         | '(' ctype ')'                 { $2 }
490
491 gtycon  :: { RdrName }
492         : qtycon                        { $1 }
493         | '(' ')'                       { unitTyCon_RDR }
494         | '(' '->' ')'                  { funTyCon_RDR }
495         | '[' ']'                       { listTyCon_RDR }
496         | '(' commas ')'                { tupleTyCon_RDR $2 }
497
498 -- An inst_type is what occurs in the head of an instance decl
499 --      e.g.  (Foo a, Gaz b) => Wibble a b
500 -- It's kept as a single type, with a MonoDictTy at the right
501 -- hand corner, for convenience.
502 inst_type :: { RdrNameHsType }
503         : ctype                         {% checkInstType $1 }
504
505 ctype   :: { RdrNameHsType }
506         : 'forall' tyvars '.' context type
507                                         { mkHsForAllTy (Just $2) $4 $5 }
508         | 'forall' tyvars '.' type      { mkHsForAllTy (Just $2) [] $4 }
509         | context type                  { mkHsForAllTy Nothing   $1 $2 }
510                 -- A type of form (context => type) is an *implicit* HsForAllTy
511         | type                          { $1 }
512
513 types0  :: { [RdrNameHsType] }
514         : types                         { $1 }
515         | {- empty -}                   { [] }
516
517 types   :: { [RdrNameHsType] }
518         : type                          { [$1] }
519         | types  ',' type               { $3 : $1 }
520
521 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
522         : tycon tyvars                  { ($1, reverse $2) }
523
524 tyvars :: { [RdrNameHsTyVar] }
525         : tyvars tyvar                  { UserTyVar $2 : $1 }
526         | {- empty -}                   { [] }
527
528 -----------------------------------------------------------------------------
529 -- Datatype declarations
530
531 constrs :: { [RdrNameConDecl] }
532         : constrs '|' constr            { $3 : $1 }
533         | constr                        { [$1] }
534
535 constr :: { RdrNameConDecl }
536         : srcloc forall context constr_stuff
537                 { ConDecl (fst $4) $2 $3 (snd $4) $1 }
538         | srcloc forall constr_stuff
539                 { ConDecl (fst $3) $2 [] (snd $3) $1 }
540
541 forall :: { [RdrNameHsTyVar] }
542         : 'forall' tyvars '.'           { $2 }
543         | {- empty -}                   { [] }
544
545 context :: { RdrNameContext }
546         : btype '=>'                    {% checkContext $1 }
547
548 constr_stuff :: { (RdrName, RdrNameConDetails) }
549         : scontype                      { (fst $1, VanillaCon (snd $1)) }
550         | sbtype conop sbtype           { ($2, InfixCon $1 $3) }
551         | con '{' fielddecls '}'        { ($1, RecCon (reverse $3)) }
552
553 newconstr :: { RdrNameConDecl }
554         : srcloc conid atype    { ConDecl $2 [] [] (NewCon $3 Nothing) $1 }
555         | srcloc conid '{' var '::' type '}'
556                                 { ConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
557
558 scontype :: { (RdrName, [RdrNameBangType]) }
559         : btype                         {% splitForConApp $1 [] }
560         | scontype1                     { $1 }
561
562 scontype1 :: { (RdrName, [RdrNameBangType]) }
563         : btype '!' atype               {% splitForConApp $1 [Banged $3] }
564         | scontype1 satype              { (fst $1, snd $1 ++ [$2] ) }
565
566 satype :: { RdrNameBangType }
567         : atype                         { Unbanged $1 }
568         | '!' atype                     { Banged   $2 }
569
570 sbtype :: { RdrNameBangType }
571         : btype                         { Unbanged $1 }
572         | '!' atype                     { Banged   $2 }
573
574 fielddecls :: { [([RdrName],RdrNameBangType)] }
575         : fielddecls ',' fielddecl      { $3 : $1 }
576         | fielddecl                     { [$1] }
577
578 fielddecl :: { ([RdrName],RdrNameBangType) }
579         : vars '::' stype               { (reverse $1, $3) }
580
581 stype :: { RdrNameBangType }
582         : type                          { Unbanged $1 } 
583         | '!' atype                     { Banged   $2 }
584
585 deriving :: { Maybe [RdrName] }
586         : {- empty -}                   { Nothing }
587         | 'deriving' qtycls             { Just [$2] }
588         | 'deriving' '('          ')'   { Just [] }
589         | 'deriving' '(' dclasses ')'   { Just (reverse $3) }
590
591 dclasses :: { [RdrName] }
592         : dclasses ',' qtycls           { $3 : $1 }
593         | qtycls                        { [$1] }
594
595 -----------------------------------------------------------------------------
596 -- Value definitions
597
598 valdef :: { RdrNameMonoBinds }
599         : infixexp {-ToDo: opt_sig-} srcloc rhs 
600                                         {% checkValDef $1 Nothing $3 $2 }
601
602 rhs     :: { RdrNameGRHSs }
603         : '=' srcloc exp wherebinds     { GRHSs (unguardedRHS $3 $2) 
604                                                                 $4 Nothing}
605         | gdrhs wherebinds              { GRHSs (reverse $1) $2 Nothing }
606
607 gdrhs :: { [RdrNameGRHS] }
608         : gdrhs gdrh                    { $2 : $1 }
609         | gdrh                          { [$1] }
610
611 gdrh :: { RdrNameGRHS }
612         : '|' srcloc quals '=' exp      { GRHS (reverse 
613                                                   (ExprStmt $5 $2 : $3)) $2 }
614
615 -----------------------------------------------------------------------------
616 -- Expressions
617
618 exp   :: { RdrNameHsExpr }
619         : infixexp '::' sigtype         { ExprWithTySig $1 $3 }
620         | infixexp                      { $1 }
621
622 infixexp :: { RdrNameHsExpr }
623         : exp10                         { $1 }
624         | infixexp qop exp10            { OpApp $1 $2 (panic "fixity") $3 }
625
626 exp10 :: { RdrNameHsExpr }
627         : '\\' aexp aexps opt_asig '->' srcloc exp      
628                         {% checkPatterns ($2 : reverse $3) `thenP` \ ps -> 
629                            returnP (HsLam (Match [] ps $4 
630                                             (GRHSs (unguardedRHS $7 $6) 
631                                                    EmptyBinds Nothing))) }
632         | 'let' declbinds 'in' exp              { HsLet $2 $4 }
633         | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
634         | 'case' srcloc exp 'of' altslist       { HsCase $3 $5 $2 }
635         | '-' fexp                              { NegApp $2 (error "NegApp") }
636         | srcloc 'do' stmtlist                  { HsDo DoStmt $3 $1 }
637
638         | '_ccall_'    ccallid aexps0           { CCall $2 $3 False False cbot }
639         | '_ccall_GC_' ccallid aexps0           { CCall $2 $3 True  False cbot }
640         | '_casm_'     CLITLIT aexps0           { CCall $2 $3 False True  cbot }
641         | '_casm_GC_'  CLITLIT aexps0           { CCall $2 $3 True  True  cbot }
642
643         | '_scc_' STRING exp                    { if opt_SccProfilingOn
644                                                         then HsSCC $2 $3
645                                                         else HsPar $3 }
646
647         | fexp                                  { $1 }
648
649 ccallid :: { FAST_STRING }
650         :  VARID                                { $1 }
651         |  CONID                                { $1 }
652
653 fexp    :: { RdrNameHsExpr }
654         : fexp aexp                             { HsApp $1 $2 }
655         | aexp                                  { $1 }
656
657 aexps0  :: { [RdrNameHsExpr] }
658         : aexps                                 { reverse $1 }
659
660 aexps   :: { [RdrNameHsExpr] }
661         : aexps aexp                            { $2 : $1 }
662         | {- empty -}                           { [] }
663
664 aexp    :: { RdrNameHsExpr }
665         : aexp '{' fbinds '}'           {% mkRecConstrOrUpdate $1 (reverse $3) }
666         | aexp1                         { $1 }
667
668 aexp1   :: { RdrNameHsExpr }
669         : qvar                          { HsVar $1 }
670         | gcon                          { HsVar $1 }
671         | literal                       { HsLit $1 }
672         | '(' exp ')'                   { HsPar $2 }
673         | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) True }
674         | '(#' texps '#)'               { ExplicitTuple (reverse $2) False }
675         | '[' list ']'                  { $2 }
676         | '(' infixexp qop ')'          { SectionL $2 $3  }
677         | '(' qopm infixexp ')'         { SectionR $2 $3 }
678         | qvar '@' aexp                 { EAsPat $1 $3 }
679         | '_'                           { EWildPat }
680         | '~' aexp1                     { ELazyPat $2 }
681
682 commas :: { Int }
683         : commas ','                    { $1 + 1 }
684         | ','                           { 2 }
685
686 texps :: { [RdrNameHsExpr] }
687         : texps ',' exp                 { $3 : $1 }
688         | exp                           { [$1] }
689
690 -----------------------------------------------------------------------------
691 -- List expressions
692
693 -- The rules below are little bit contorted to keep lexps left-recursive while
694 -- avoiding another shift/reduce-conflict.
695
696 list :: { RdrNameHsExpr }
697         : exp                           { ExplicitList [$1] }
698         | lexps                         { ExplicitList (reverse $1) }
699         | exp '..'                      { ArithSeqIn (From $1) }
700         | exp ',' exp '..'              { ArithSeqIn (FromThen $1 $3) }
701         | exp '..' exp                  { ArithSeqIn (FromTo $1 $3) }
702         | exp ',' exp '..' exp          { ArithSeqIn (FromThenTo $1 $3 $5) }
703         | exp srcloc '|' quals                  { HsDo ListComp (reverse 
704                                                 (ReturnStmt $1 : $4)) $2 }
705
706 lexps :: { [RdrNameHsExpr] }
707         : lexps ',' exp                 { $3 : $1 }
708         | exp ',' exp                   { [$3,$1] }
709
710 -----------------------------------------------------------------------------
711 -- List Comprehensions
712
713 quals :: { [RdrNameStmt] }
714         : quals ',' qual                { $3 : $1 }
715         | qual                          { [$1] }
716
717 qual  :: { RdrNameStmt }
718         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
719                                            returnP (BindStmt p $4 $1) }
720         | srcloc exp                    { GuardStmt $2 $1 }
721         | srcloc 'let' declbinds        { LetStmt $3 }
722
723 -----------------------------------------------------------------------------
724 -- Case alternatives
725
726 altslist :: { [RdrNameMatch] }
727         : '{'            alts '}'       { reverse $2 }
728         |     layout_on  alts  close    { reverse $2 }
729
730
731 alts    :: { [RdrNameMatch] }
732         : alts ';' alt                  { $3 : $1 }
733         | alts ';'                      { $1 }
734         | alt                           { [$1] }
735         | {- empty -}                   { [] }
736
737 alt     :: { RdrNameMatch }
738         : infixexp opt_sig ralt wherebinds
739                                         {% checkPattern $1 `thenP` \p ->
740                                            returnP (Match [] [p] $2
741                                                      (GRHSs $3 $4 Nothing)) }
742
743 opt_sig :: { Maybe RdrNameHsType }
744         : {- empty -}                   { Nothing }
745         | '::' type                     { Just $2 }
746
747 opt_asig :: { Maybe RdrNameHsType }
748         : {- empty -}                   { Nothing }
749         | '::' atype                    { Just $2 }
750
751 ralt :: { [RdrNameGRHS] }
752         : '->' srcloc exp               { [GRHS [ExprStmt $3 $2] $2] }
753         | gdpats                        { (reverse $1) }
754
755 gdpats :: { [RdrNameGRHS] }
756         : gdpats gdpat                  { $2 : $1 }
757         | gdpat                         { [$1] }
758
759 gdpat   :: { RdrNameGRHS }
760         : srcloc '|' quals '->' exp     { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
761
762 -----------------------------------------------------------------------------
763 -- Statement sequences
764
765 stmtlist :: { [RdrNameStmt] }
766         : '{'                   stmts '}'       { reverse $2 }
767         |     layout_on_for_do  stmts close     { reverse $2 }
768
769 -- Stmt list should really end in an expression, but it's not
770 -- convenient to enforce this here, so we throw out erroneous
771 -- statement sequences in the renamer instead.
772
773 stmts :: { [RdrNameStmt] }
774         : ';' stmts1                    { $2 }
775         | stmts1                        { $1 }
776
777 stmts1 :: { [RdrNameStmt] }
778         : stmts1 ';' stmt               { $3 : $1 }
779         | stmts1 ';'                    { $1 }
780         | stmt                          { [$1] }
781
782 stmt  :: { RdrNameStmt }
783         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
784                                            returnP (BindStmt p $4 $1) }
785         | srcloc exp                    { ExprStmt $2 $1 }
786         | srcloc 'let' declbinds        { LetStmt $3 }
787
788 -----------------------------------------------------------------------------
789 -- Record Field Update/Construction
790
791 fbinds  :: { RdrNameHsRecordBinds }
792         : fbinds ',' fbind              { $3 : $1 }
793         | fbinds ','                    { $1 }
794         | fbind                         { [$1] }
795         | {- empty -}                   { [] }
796
797 fbind   :: { (RdrName, RdrNameHsExpr, Bool) }
798         : qvar '=' exp                  { ($1,$3,False) }
799
800 -----------------------------------------------------------------------------
801 -- Variables, Constructors and Operators.
802
803 gcon    :: { RdrName }
804         : '(' ')'               { unitCon_RDR }
805         | '[' ']'               { nilCon_RDR }
806         | '(' commas ')'        { tupleCon_RDR $2 }
807         | qcon                  { $1 }
808
809 var     :: { RdrName }
810         : varid                 { $1 }
811         | '(' varsym ')'        { $2 }
812
813 qvar    :: { RdrName }
814         : qvarid                { $1 }
815         | '(' qvarsym ')'       { $2 }
816
817 con     :: { RdrName }
818         : conid                 { $1 }
819         | '(' consym ')'        { $2 }
820
821 qcon    :: { RdrName }
822         : qconid                { $1 }
823         | '(' qconsym ')'       { $2 }
824
825 varop   :: { RdrName }
826         : varsym                { $1 }
827         | '`' varid '`'         { $2 }
828
829 qvarop :: { RdrName }
830         : qvarsym               { $1 }
831         | '`' qvarid '`'        { $2 }
832
833 qvaropm :: { RdrName }
834         : qvarsymm              { $1 }
835         | '`' qvarid '`'        { $2 }
836
837 conop :: { RdrName }
838         : consym                { $1 }  
839         | '`' conid '`'         { $2 }
840
841 qconop :: { RdrName }
842         : qconsym               { $1 }
843         | '`' qconid '`'        { $2 }
844
845 -----------------------------------------------------------------------------
846 -- Any operator
847
848 op      :: { RdrName }   -- used in infix decls
849         : varop                 { $1 }
850         | conop                 { $1 }
851
852 qop     :: { RdrNameHsExpr }   -- used in sections
853         : qvarop                { HsVar $1 }
854         | qconop                { HsVar $1 }
855
856 qopm    :: { RdrNameHsExpr }   -- used in sections
857         : qvaropm               { HsVar $1 }
858         | qconop                { HsVar $1 }
859
860 -----------------------------------------------------------------------------
861 -- VarIds
862
863 qvarid :: { RdrName }
864         : varid                 { $1 }
865         | QVARID                { case $1 of { (mod,n) ->
866                                   mkSrcQual varName mod n } }
867
868 varid :: { RdrName }
869         : VARID                 { mkSrcUnqual varName $1 }
870         | 'as'                  { as_var_RDR }
871         | 'qualified'           { qualified_var_RDR }
872         | 'hiding'              { hiding_var_RDR }
873         | 'forall'              { forall_var_RDR }
874         | 'export'              { export_var_RDR }
875         | 'label'               { label_var_RDR }
876         | 'dynamic'             { dynamic_var_RDR }
877         | 'unsafe'              { unsafe_var_RDR }
878         | 'stdcall'             { stdcall_var_RDR }
879         | 'ccall'               { ccall_var_RDR }
880
881 varid_no_unsafe :: { RdrName }
882         : VARID                 { mkSrcUnqual varName $1 }
883         | 'as'                  { as_var_RDR }
884         | 'qualified'           { qualified_var_RDR }
885         | 'hiding'              { hiding_var_RDR }
886         | 'forall'              { forall_var_RDR }
887         | 'export'              { export_var_RDR }
888         | 'label'               { label_var_RDR }
889         | 'dynamic'             { dynamic_var_RDR }
890         | 'stdcall'             { stdcall_var_RDR }
891         | 'ccall'               { ccall_var_RDR }
892
893 -----------------------------------------------------------------------------
894 -- ConIds
895
896 qconid :: { RdrName }
897         : conid                 { $1 }
898         | QCONID                { case $1 of { (mod,n) ->
899                                   mkSrcQual dataName mod n } }
900
901 conid   :: { RdrName }
902         : CONID                 { mkSrcUnqual dataName $1 }
903
904 -----------------------------------------------------------------------------
905 -- ConSyms
906
907 qconsym :: { RdrName }
908         : consym                { $1 }
909         | QCONSYM               { case $1 of { (mod,n) ->
910                                   mkSrcQual dataName mod n } }
911
912 consym :: { RdrName }
913         : CONSYM                { mkSrcUnqual dataName $1 }
914
915 -----------------------------------------------------------------------------
916 -- VarSyms
917
918 qvarsym :: { RdrName }
919         : varsym                { $1 }
920         | qvarsym1              { $1 }
921
922 qvarsymm :: { RdrName }
923         : varsymm               { $1 }
924         | qvarsym1              { $1 }
925
926 varsym :: { RdrName }
927         : VARSYM                { mkSrcUnqual varName $1 }
928         | '-'                   { minus_RDR }
929         | '!'                   { pling_RDR }
930         | '.'                   { dot_RDR }
931
932 varsymm :: { RdrName } -- varsym not including '-'
933         : VARSYM                { mkSrcUnqual varName $1 }
934         | '!'                   { pling_RDR }
935         | '.'                   { dot_RDR }
936
937 qvarsym1 :: { RdrName }
938         : QVARSYM               { case $1 of { (mod,n) ->
939                                   mkSrcQual varName mod n } }
940
941 literal :: { HsLit }
942         : INTEGER               { HsInt    $1 }
943         | CHAR                  { HsChar   $1 }
944         | RATIONAL              { HsFrac   $1 }
945         | STRING                { HsString $1 }
946
947         | PRIMINTEGER           { HsIntPrim    $1 }
948         | PRIMCHAR              { HsCharPrim   $1 }
949         | PRIMSTRING            { HsStringPrim $1 }
950         | PRIMFLOAT             { HsFloatPrim  $1 }
951         | PRIMDOUBLE            { HsDoublePrim $1 }
952         | CLITLIT               { HsLitLit     $1 }
953
954 srcloc :: { SrcLoc }    :       {% getSrcLocP }
955  
956 -----------------------------------------------------------------------------
957 -- Layout
958
959 close :: { () }
960         : vccurly               { () } -- context popped in lexer.
961         | error                 {% popContext }
962
963 layout_on         :: { () }     : {% layoutOn True{-strict-} }
964 layout_on_for_do  :: { () }     : {% layoutOn False }
965
966 -----------------------------------------------------------------------------
967 -- Miscellaneous (mostly renamings)
968
969 modid   :: { ModuleName }
970         : CONID                 { mkSrcModuleFS $1 }
971
972 tycon   :: { RdrName }
973         : CONID                 { mkSrcUnqual tcClsName $1 }
974
975 qtycon :: { RdrName }
976         : tycon                 { $1 }
977         | QCONID                { case $1 of { (mod,n) ->
978                                   mkSrcQual tcClsName mod n } }
979
980 qtycls  :: { RdrName }
981         : qtycon                { $1 }
982
983 tyvar   :: { RdrName }
984         : VARID                 { mkSrcUnqual tvName $1 }
985         | 'as'                  { as_tyvar_RDR }
986         | 'qualified'           { qualified_tyvar_RDR }
987         | 'hiding'              { hiding_tyvar_RDR }
988         | 'export'              { export_tyvar_RDR }
989         | 'label'               { label_tyvar_RDR }
990         | 'dynamic'             { dynamic_tyvar_RDR }
991         | 'unsafe'              { unsafe_tyvar_RDR }
992         | 'stdcall'             { stdcall_tyvar_RDR }
993         | 'ccall'               { ccall_tyvar_RDR }
994         -- NOTE: no 'forall'
995
996 -----------------------------------------------------------------------------
997
998 {
999 happyError :: P a
1000 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
1001 }