[project @ 1999-06-01 16:40:41 by simonmar]
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.1 1999/06/01 16:40:48 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 (error "rule tyvars") 
433                   $2 $3 $6 $5)) }
434
435 rule_forall :: { [RdrNameRuleBndr] }
436         : 'forall' rule_var_list '.'            { $2 }
437         | {- empty -}                           { [] }
438
439 rule_var_list :: { [RdrNameRuleBndr] }
440         : rule_var                              { [$1] }
441         | rule_var ',' rule_var_list            { $1 : $3 }
442
443 rule_var :: { RdrNameRuleBndr }
444         : varid                                 { RuleBndr $1 }
445         | varid '::' ctype                      { RuleBndrSig $1 $3 }
446
447 -----------------------------------------------------------------------------
448 -- Foreign import/export
449
450 callconv :: { Int }
451         : VARID                 {% checkCallConv $1 }
452         | {- empty -}           { defaultCallConv }
453
454 unsafe_flag :: { Bool }
455         : 'unsafe'              { True }
456         | {- empty -}           { False }
457
458 ext_name :: { ExtName }
459         : 'dynamic'             { Dynamic }
460         | STRING                { ExtName $1 Nothing }
461         | STRING STRING         { ExtName $2 (Just $1) }
462
463 -----------------------------------------------------------------------------
464 -- Types
465
466 {- ToDo: forall stuff -}
467
468 type :: { RdrNameHsType }
469         : btype '->' type               { MonoFunTy $1 $3 }
470         | btype                         { $1 }
471
472 btype :: { RdrNameHsType }
473         : btype atype                   { MonoTyApp $1 $2 }
474         | atype                         { $1 }
475
476 atype :: { RdrNameHsType }
477         : gtycon                        { MonoTyVar $1 }
478         | tyvar                         { MonoTyVar $1 }
479         | '(' type ',' types ')'        { MonoTupleTy ($2 : reverse $4) True }
480         | '(#' types '#)'               { MonoTupleTy (reverse $2) False }
481         | '[' type ']'                  { MonoListTy $2 }
482         | '(' ctype ')'                 { $2 }
483
484 gtycon  :: { RdrName }
485         : qtycon                        { $1 }
486         | '(' ')'                       { unitTyCon_RDR }
487         | '(' '->' ')'                  { funTyCon_RDR }
488         | '[' ']'                       { listTyCon_RDR }
489         | '(' commas ')'                { tupleTyCon_RDR $2 }
490
491 -- An inst_type is what occurs in the head of an instance decl
492 --      e.g.  (Foo a, Gaz b) => Wibble a b
493 -- It's kept as a single type, with a MonoDictTy at the right
494 -- hand corner, for convenience.
495 inst_type :: { RdrNameHsType }
496         : ctype                         {% checkInstType $1 }
497
498 ctype   :: { RdrNameHsType }
499         : 'forall' tyvars '.' btype '=>' type
500                                         {% checkContext $4 `thenP` \c ->
501                                            returnP (HsForAllTy (Just $2) c $6) }
502         | 'forall' tyvars '.' type      { HsForAllTy (Just $2) [] $4 }
503         | btype '=>' type               {% checkContext $1 `thenP` \c ->
504                                            returnP (HsForAllTy Nothing c $3) }
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 {- ToDo: existential stuff -}
530
531 constr :: { RdrNameConDecl }
532         : srcloc scontype   
533                 { ConDecl (fst $2) [] [] (VanillaCon (snd $2)) $1 }
534         | srcloc sbtype conop sbtype    
535                 { ConDecl $3 [] [] (InfixCon $2 $4) $1 }
536         | srcloc con '{' fielddecls '}' 
537                 { ConDecl $2 [] [] (RecCon (reverse $4)) $1 }
538
539 newconstr :: { RdrNameConDecl }
540         : srcloc conid atype    { ConDecl $2 [] [] (NewCon $3 Nothing) $1 }
541         | srcloc conid '{' var '::' type '}'
542                                 { ConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
543
544 scontype :: { (RdrName, [RdrNameBangType]) }
545         : btype                         {% splitForConApp $1 [] }
546         | scontype1                     { $1 }
547
548 scontype1 :: { (RdrName, [RdrNameBangType]) }
549         : btype '!' atype               {% splitForConApp $1 [Banged $3] }
550         | scontype1 satype              { (fst $1, snd $1 ++ [$2] ) }
551
552 satype :: { RdrNameBangType }
553         : atype                         { Unbanged $1 }
554         | '!' atype                     { Banged   $2 }
555
556 sbtype :: { RdrNameBangType }
557         : btype                         { Unbanged $1 }
558         | '!' atype                     { Banged   $2 }
559
560 fielddecls :: { [([RdrName],RdrNameBangType)] }
561         : fielddecls ',' fielddecl      { $3 : $1 }
562         | fielddecl                     { [$1] }
563
564 fielddecl :: { ([RdrName],RdrNameBangType) }
565         : vars '::' stype               { (reverse $1, $3) }
566
567 stype :: { RdrNameBangType }
568         : type                          { Unbanged $1 } 
569         | '!' atype                     { Banged   $2 }
570
571 deriving :: { Maybe [RdrName] }
572         : {- empty -}                   { Nothing }
573         | 'deriving' qtycls             { Just [$2] }
574         | 'deriving' '('          ')'   { Just [] }
575         | 'deriving' '(' dclasses ')'   { Just (reverse $3) }
576
577 dclasses :: { [RdrName] }
578         : dclasses ',' qtycls           { $3 : $1 }
579         | qtycls                        { [$1] }
580
581 -----------------------------------------------------------------------------
582 -- Value definitions
583
584 valdef :: { RdrNameMonoBinds }
585         : infixexp {-ToDo: opt_sig-} srcloc rhs 
586                                         {% checkValDef $1 Nothing $3 $2 }
587
588 rhs     :: { RdrNameGRHSs }
589         : '=' srcloc exp wherebinds     { GRHSs (unguardedRHS $3 $2) 
590                                                                 $4 Nothing}
591         | gdrhs wherebinds              { GRHSs (reverse $1) $2 Nothing }
592
593 gdrhs :: { [RdrNameGRHS] }
594         : gdrhs gdrh                    { $2 : $1 }
595         | gdrh                          { [$1] }
596
597 gdrh :: { RdrNameGRHS }
598         : '|' srcloc quals '=' exp      { GRHS (reverse 
599                                                   (ExprStmt $5 $2 : $3)) $2 }
600
601 -----------------------------------------------------------------------------
602 -- Expressions
603
604 exp   :: { RdrNameHsExpr }
605         : infixexp '::' sigtype         { ExprWithTySig $1 $3 }
606         | infixexp                      { $1 }
607
608 infixexp :: { RdrNameHsExpr }
609         : exp10                         { $1 }
610         | infixexp qop exp10            { OpApp $1 $2 (panic "fixity") $3 }
611
612 exp10 :: { RdrNameHsExpr }
613         : '\\' aexp aexps opt_asig '->' srcloc exp      
614                         {% checkPatterns ($2 : reverse $3) `thenP` \ ps -> 
615                            returnP (HsLam (Match [] ps $4 
616                                             (GRHSs (unguardedRHS $7 $6) 
617                                                    EmptyBinds Nothing))) }
618         | 'let' declbinds 'in' exp              { HsLet $2 $4 }
619         | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
620         | 'case' srcloc exp 'of' altslist       { HsCase $3 $5 $2 }
621         | '-' fexp                              { NegApp $2 (error "NegApp") }
622         | srcloc 'do' stmtlist                  { HsDo DoStmt $3 $1 }
623
624         | '_ccall_'    ccallid aexps0           { CCall $2 $3 False False cbot }
625         | '_ccall_GC_' ccallid aexps0           { CCall $2 $3 True  False cbot }
626         | '_casm_'     CLITLIT aexps0           { CCall $2 $3 False True  cbot }
627         | '_casm_GC_'  CLITLIT aexps0           { CCall $2 $3 True  True  cbot }
628
629         | fexp                                  { $1 }
630
631 ccallid :: { FAST_STRING }
632         :  VARID                                { $1 }
633         |  CONID                                { $1 }
634
635 fexp    :: { RdrNameHsExpr }
636         : fexp aexp                             { HsApp $1 $2 }
637         | aexp                                  { $1 }
638
639 aexps0  :: { [RdrNameHsExpr] }
640         : aexps                                 { reverse $1 }
641
642 aexps   :: { [RdrNameHsExpr] }
643         : aexps aexp                            { $2 : $1 }
644         | {- empty -}                           { [] }
645
646 aexp    :: { RdrNameHsExpr }
647         : aexp '{' fbinds '}'           {% mkRecConstrOrUpdate $1 (reverse $3) }
648         | aexp1                         { $1 }
649
650 aexp1   :: { RdrNameHsExpr }
651         : qvar                          { HsVar $1 }
652         | gcon                          { HsVar $1 }
653         | literal                       { HsLit $1 }
654         | '(' exp ')'                   { HsPar $2 }
655         | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) True }
656         | '(#' texps '#)'               { ExplicitTuple (reverse $2) False }
657         | '[' list ']'                  { $2 }
658         | '(' infixexp qop ')'          { SectionL $2 $3  }
659         | '(' qopm infixexp ')'         { SectionR $2 $3 }
660         | qvar '@' aexp1                { EAsPat $1 $3 }
661         | '_'                           { EWildPat }
662         | '~' aexp1                     { ELazyPat $2 }
663
664 commas :: { Int }
665         : commas ','                    { $1 + 1 }
666         | ','                           { 2 }
667
668 texps :: { [RdrNameHsExpr] }
669         : texps ',' exp                 { $3 : $1 }
670         | exp                           { [$1] }
671
672 -----------------------------------------------------------------------------
673 -- List expressions
674
675 -- The rules below are little bit contorted to keep lexps left-recursive while
676 -- avoiding another shift/reduce-conflict.
677
678 list :: { RdrNameHsExpr }
679         : exp                           { ExplicitList [$1] }
680         | lexps                         { ExplicitList (reverse $1) }
681         | exp '..'                      { ArithSeqIn (From $1) }
682         | exp ',' exp '..'              { ArithSeqIn (FromThen $1 $3) }
683         | exp '..' exp                  { ArithSeqIn (FromTo $1 $3) }
684         | exp ',' exp '..' exp          { ArithSeqIn (FromThenTo $1 $3 $5) }
685         | exp srcloc '|' quals                  { HsDo ListComp (reverse 
686                                                 (ReturnStmt $1 : $4)) $2 }
687
688 lexps :: { [RdrNameHsExpr] }
689         : lexps ',' exp                 { $3 : $1 }
690         | exp ',' exp                   { [$3,$1] }
691
692 -----------------------------------------------------------------------------
693 -- List Comprehensions
694
695 quals :: { [RdrNameStmt] }
696         : quals ',' qual                { $3 : $1 }
697         | qual                          { [$1] }
698
699 qual  :: { RdrNameStmt }
700         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
701                                            returnP (BindStmt p $4 $1) }
702         | srcloc exp                    { GuardStmt $2 $1 }
703         | srcloc 'let' declbinds        { LetStmt $3 }
704
705 -----------------------------------------------------------------------------
706 -- Case alternatives
707
708 altslist :: { [RdrNameMatch] }
709         : '{'            alts '}'       { reverse $2 }
710         |     layout_on  alts  close    { reverse $2 }
711
712
713 alts    :: { [RdrNameMatch] }
714         : alts ';' alt                  { $3 : $1 }
715         | alts ';'                      { $1 }
716         | alt                           { [$1] }
717         | {- empty -}                   { [] }
718
719 alt     :: { RdrNameMatch }
720         : infixexp opt_sig ralt wherebinds
721                                         {% checkPattern $1 `thenP` \p ->
722                                            returnP (Match [] [p] $2
723                                                      (GRHSs $3 $4 Nothing)) }
724
725 opt_sig :: { Maybe RdrNameHsType }
726         : {- empty -}                   { Nothing }
727         | '::' type                     { Just $2 }
728
729 opt_asig :: { Maybe RdrNameHsType }
730         : {- empty -}                   { Nothing }
731         | '::' atype                    { Just $2 }
732
733 ralt :: { [RdrNameGRHS] }
734         : '->' srcloc exp               { [GRHS [ExprStmt $3 $2] $2] }
735         | gdpats                        { (reverse $1) }
736
737 gdpats :: { [RdrNameGRHS] }
738         : gdpats gdpat                  { $2 : $1 }
739         | gdpat                         { [$1] }
740
741 gdpat   :: { RdrNameGRHS }
742         : srcloc '|' quals '->' exp     { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
743
744 -----------------------------------------------------------------------------
745 -- Statement sequences
746
747 stmtlist :: { [RdrNameStmt] }
748         : '{'            stmts '}'      { reverse $2 }
749         |     layout_on  stmts close    { reverse $2 }
750
751 stmts :: { [RdrNameStmt] }
752         : stmts ';' stmt                { $3 : $1 }
753         | stmts ';'                     { $1 }
754         | stmt                          { [$1] }
755         | {- empty -}                   { [] }
756
757 stmt  :: { RdrNameStmt }
758         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
759                                            returnP (BindStmt p $4 $1) }
760         | srcloc exp                    { ExprStmt $2 $1 }
761         | srcloc 'let' declbinds        { LetStmt $3 }
762
763 -----------------------------------------------------------------------------
764 -- Record Field Update/Construction
765
766 fbinds  :: { RdrNameHsRecordBinds }
767         : fbinds ',' fbind              { $3 : $1 }
768         | fbinds ','                    { $1 }
769         | fbind                         { [$1] }
770         | {- empty -}                   { [] }
771
772 fbind   :: { (RdrName, RdrNameHsExpr, Bool) }
773         : qvar '=' exp                  { ($1,$3,False) }
774
775 -----------------------------------------------------------------------------
776 -- Variables, Constructors and Operators.
777
778 gcon    :: { RdrName }
779         : '(' ')'               { unitCon_RDR }
780         | '[' ']'               { nilCon_RDR }
781         | '(' commas ')'        { tupleCon_RDR $2 }
782         | qcon                  { $1 }
783
784 var     :: { RdrName }
785         : varid                 { $1 }
786         | '(' varsym ')'        { $2 }
787
788 qvar    :: { RdrName }
789         : qvarid                { $1 }
790         | '(' qvarsym ')'       { $2 }
791
792 con     :: { RdrName }
793         : conid                 { $1 }
794         | '(' consym ')'        { $2 }
795
796 qcon    :: { RdrName }
797         : qconid                { $1 }
798         | '(' qconsym ')'       { $2 }
799
800 varop   :: { RdrName }
801         : varsym                { $1 }
802         | '`' varid '`'         { $2 }
803
804 qvarop :: { RdrName }
805         : qvarsym               { $1 }
806         | '`' qvarid '`'        { $2 }
807
808 qvaropm :: { RdrName }
809         : qvarsymm              { $1 }
810         | '`' qvarid '`'        { $2 }
811
812 conop :: { RdrName }
813         : consym                { $1 }  
814         | '`' conid '`'         { $2 }
815
816 qconop :: { RdrName }
817         : qconsym               { $1 }
818         | '`' qconid '`'        { $2 }
819
820 -----------------------------------------------------------------------------
821 -- Any operator
822
823 op      :: { RdrName }   -- used in infix decls
824         : varop                 { $1 }
825         | conop                 { $1 }
826
827 qop     :: { RdrNameHsExpr }   -- used in sections
828         : qvarop                { HsVar $1 }
829         | qconop                { HsVar $1 }
830
831 qopm    :: { RdrNameHsExpr }   -- used in sections
832         : qvaropm               { HsVar $1 }
833         | qconop                { HsVar $1 }
834
835 -----------------------------------------------------------------------------
836 -- VarIds
837
838 qvarid :: { RdrName }
839         : varid                 { $1 }
840         | QVARID                { case $1 of { (mod,n) ->
841                                   mkSrcQual varName mod n } }
842
843 varid :: { RdrName }
844         : VARID                 { mkSrcUnqual varName $1 }
845         | 'as'                  { as_var_RDR }
846         | 'qualified'           { qualified_var_RDR }
847         | 'hiding'              { hiding_var_RDR }
848         | 'forall'              { forall_var_RDR }
849         | 'export'              { export_var_RDR }
850         | 'label'               { label_var_RDR }
851         | 'dynamic'             { dynamic_var_RDR }
852         | 'unsafe'              { unsafe_var_RDR }
853
854 varid_no_unsafe :: { RdrName }
855         : VARID                 { mkSrcUnqual varName $1 }
856         | 'as'                  { as_var_RDR }
857         | 'qualified'           { qualified_var_RDR }
858         | 'hiding'              { hiding_var_RDR }
859         | 'forall'              { forall_var_RDR }
860         | 'export'              { export_var_RDR }
861         | 'label'               { label_var_RDR }
862         | 'dynamic'             { dynamic_var_RDR }
863
864 -----------------------------------------------------------------------------
865 -- ConIds
866
867 qconid :: { RdrName }
868         : conid                 { $1 }
869         | QCONID                { case $1 of { (mod,n) ->
870                                   mkSrcQual dataName mod n } }
871
872 conid   :: { RdrName }
873         : CONID                 { mkSrcUnqual dataName $1 }
874
875 -----------------------------------------------------------------------------
876 -- ConSyms
877
878 qconsym :: { RdrName }
879         : consym                { $1 }
880         | QCONSYM               { case $1 of { (mod,n) ->
881                                   mkSrcQual dataName mod n } }
882
883 consym :: { RdrName }
884         : CONSYM                { mkSrcUnqual dataName $1 }
885
886 -----------------------------------------------------------------------------
887 -- VarSyms
888
889 qvarsym :: { RdrName }
890         : varsym                { $1 }
891         | qvarsym1              { $1 }
892
893 qvarsymm :: { RdrName }
894         : varsymm               { $1 }
895         | qvarsym1              { $1 }
896
897 varsym :: { RdrName }
898         : VARSYM                { mkSrcUnqual varName $1 }
899         | '-'                   { minus_RDR }
900         | '!'                   { pling_RDR }
901         | '.'                   { dot_RDR }
902
903 varsymm :: { RdrName } -- varsym not including '-'
904         : VARSYM                { mkSrcUnqual varName $1 }
905         | '!'                   { pling_RDR }
906         | '.'                   { dot_RDR }
907
908 qvarsym1 :: { RdrName }
909         : QVARSYM               { case $1 of { (mod,n) ->
910                                   mkSrcQual varName mod n } }
911
912 literal :: { HsLit }
913         : INTEGER               { HsInt    $1 }
914         | CHAR                  { HsChar   $1 }
915         | RATIONAL              { HsFrac   $1 }
916         | STRING                { HsString $1 }
917
918         | PRIMINTEGER           { HsIntPrim    $1 }
919         | PRIMCHAR              { HsCharPrim   $1 }
920         | PRIMSTRING            { HsStringPrim $1 }
921         | PRIMFLOAT             { HsFloatPrim  $1 }
922         | PRIMDOUBLE            { HsDoublePrim $1 }
923         | CLITLIT               { HsLitLit     $1 }
924
925 srcloc :: { SrcLoc }    :       {% getSrcLocP }
926  
927 -----------------------------------------------------------------------------
928 -- Layout
929
930 close :: { () }
931         : vccurly               { () } -- context popped in lexer.
932         | error                 {% popContext }
933
934 layout_on  :: { () }    :       {% layoutOn  }
935
936 -----------------------------------------------------------------------------
937 -- Miscellaneous (mostly renamings)
938
939 modid   :: { ModuleName }
940         : CONID                 { mkSrcModuleFS $1 }
941
942 tycon   :: { RdrName }
943         : CONID                 { mkSrcUnqual tcClsName $1 }
944
945 qtycon :: { RdrName }
946         : tycon                 { $1 }
947         | QCONID                { case $1 of { (mod,n) ->
948                                   mkSrcQual tcClsName mod n } }
949
950 qtycls  :: { RdrName }
951         : qtycon                { $1 }
952
953 tyvar   :: { RdrName }
954         : VARID                 { mkSrcUnqual tvName $1 }
955         | 'as'                  { as_tyvar_RDR }
956         | 'qualified'           { qualified_tyvar_RDR }
957         | 'hiding'              { hiding_tyvar_RDR }
958         | 'export'              { export_var_RDR }
959         | 'label'               { label_var_RDR }
960         | 'dynamic'             { dynamic_var_RDR }
961         | 'unsafe'              { unsafe_var_RDR }
962         -- NOTE: no 'forall'
963
964 -----------------------------------------------------------------------------
965
966 {
967 happyError :: P a
968 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
969 }