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