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