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