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