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