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