[project @ 2000-06-01 08:51:46 by simonmar]
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.32 2000/06/01 08:51:46 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 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         | '(' consym ')'                { ($2,[]) }
611
612 satype :: { RdrNameBangType }
613         : atype                         { Unbanged $1 }
614         | '!' atype                     { Banged   $2 }
615
616 sbtype :: { RdrNameBangType }
617         : btype                         { Unbanged $1 }
618         | '!' atype                     { Banged   $2 }
619
620 fielddecls :: { [([RdrName],RdrNameBangType)] }
621         : fielddecls ',' fielddecl      { $3 : $1 }
622         | fielddecl                     { [$1] }
623
624 fielddecl :: { ([RdrName],RdrNameBangType) }
625         : sig_vars '::' stype           { (reverse $1, $3) }
626
627 stype :: { RdrNameBangType }
628         : ctype                         { Unbanged $1 } 
629         | '!' atype                     { Banged   $2 }
630
631 deriving :: { Maybe [RdrName] }
632         : {- empty -}                   { Nothing }
633         | 'deriving' qtycls             { Just [$2] }
634         | 'deriving' '('          ')'   { Just [] }
635         | 'deriving' '(' dclasses ')'   { Just (reverse $3) }
636
637 dclasses :: { [RdrName] }
638         : dclasses ',' qtycls           { $3 : $1 }
639         | qtycls                        { [$1] }
640
641 -----------------------------------------------------------------------------
642 -- Value definitions
643
644 {- There's an awkward overlap with a type signature.  Consider
645         f :: Int -> Int = ...rhs...
646    Then we can't tell whether it's a type signature or a value
647    definition with a result signature until we see the '='.
648    So we have to inline enough to postpone reductions until we know.
649 -}
650
651 {-
652   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
653   instead of qvar, we get another shift/reduce-conflict. Consider the
654   following programs:
655   
656      { (^^) :: Int->Int ; }          Type signature; only var allowed
657
658      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
659                                      qvar allowed (because of instance decls)
660   
661   We can't tell whether to reduce var to qvar until after we've read the signatures.
662 -}
663
664 valdef :: { RdrBinding }
665         : infixexp srcloc opt_sig rhs           {% checkValDef $1 $3 $4 $2 }
666         | infixexp srcloc '::' sigtype          {% checkValSig $1 $4 $2 }
667         | var ',' sig_vars srcloc '::' sigtype  { foldr1 RdrAndBindings 
668                                                          [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
669                                                 }
670
671 rhs     :: { RdrNameGRHSs }
672         : '=' srcloc exp wherebinds     { GRHSs (unguardedRHS $3 $2) 
673                                                                 $4 Nothing}
674         | gdrhs wherebinds              { GRHSs (reverse $1) $2 Nothing }
675
676 gdrhs :: { [RdrNameGRHS] }
677         : gdrhs gdrh                    { $2 : $1 }
678         | gdrh                          { [$1] }
679
680 gdrh :: { RdrNameGRHS }
681         : '|' srcloc quals '=' exp      { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
682
683 -----------------------------------------------------------------------------
684 -- Expressions
685
686 exp   :: { RdrNameHsExpr }
687         : infixexp '::' sigtype         { ExprWithTySig $1 $3 }
688         | infixexp 'with' dbinding      { HsWith $1 $3 }
689         | infixexp                      { $1 }
690
691 infixexp :: { RdrNameHsExpr }
692         : exp10                         { $1 }
693         | infixexp qop exp10            { OpApp $1 $2 (panic "fixity") $3 }
694
695 exp10 :: { RdrNameHsExpr }
696         : '\\' aexp aexps opt_asig '->' srcloc exp      
697                         {% checkPatterns ($2 : reverse $3) `thenP` \ ps -> 
698                            returnP (HsLam (Match [] ps $4 
699                                             (GRHSs (unguardedRHS $7 $6) 
700                                                    EmptyBinds Nothing))) }
701         | 'let' declbinds 'in' exp              { HsLet $2 $4 }
702         | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
703         | 'case' srcloc exp 'of' altslist       { HsCase $3 $5 $2 }
704         | '-' fexp                              { NegApp $2 (error "NegApp") }
705         | srcloc 'do' stmtlist                  { HsDo DoStmt $3 $1 }
706
707         | '_ccall_'    ccallid aexps0           { HsCCall $2 $3 False False cbot }
708         | '_ccall_GC_' ccallid aexps0           { HsCCall $2 $3 True  False cbot }
709         | '_casm_'     CLITLIT aexps0           { HsCCall $2 $3 False True  cbot }
710         | '_casm_GC_'  CLITLIT aexps0           { HsCCall $2 $3 True  True  cbot }
711
712         | '_scc_' STRING exp                    { if opt_SccProfilingOn
713                                                         then HsSCC $2 $3
714                                                         else HsPar $3 }
715
716         | fexp                                  { $1 }
717
718 ccallid :: { FAST_STRING }
719         :  VARID                                { $1 }
720         |  CONID                                { $1 }
721
722 fexp    :: { RdrNameHsExpr }
723         : fexp aexp                             { HsApp $1 $2 }
724         | aexp                                  { $1 }
725
726 aexps0  :: { [RdrNameHsExpr] }
727         : aexps                                 { reverse $1 }
728
729 aexps   :: { [RdrNameHsExpr] }
730         : aexps aexp                            { $2 : $1 }
731         | {- empty -}                           { [] }
732
733 aexp    :: { RdrNameHsExpr }
734         : aexp '{' fbinds '}'           {% mkRecConstrOrUpdate $1 (reverse $3) }
735         | aexp1                         { $1 }
736
737 aexp1   :: { RdrNameHsExpr }
738         : qvar                          { HsVar $1 }
739         | ipvar                         { HsIPVar $1 }
740         | gcon                          { HsVar $1 }
741         | literal                       { HsLit $1 }
742         | '(' exp ')'                   { HsPar $2 }
743         | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) Boxed}
744         | '(#' texps '#)'               { ExplicitTuple (reverse $2)      Unboxed }
745         | '[' list ']'                  { $2 }
746         | '(' infixexp qop ')'          { SectionL $2 $3  }
747         | '(' qopm infixexp ')'         { SectionR $2 $3 }
748         | qvar '@' aexp                 { EAsPat $1 $3 }
749         | '_'                           { EWildPat }
750         | '~' aexp1                     { ELazyPat $2 }
751
752 commas :: { Int }
753         : commas ','                    { $1 + 1 }
754         | ','                           { 2 }
755
756 texps :: { [RdrNameHsExpr] }
757         : texps ',' exp                 { $3 : $1 }
758         | exp                           { [$1] }
759
760 -----------------------------------------------------------------------------
761 -- List expressions
762
763 -- The rules below are little bit contorted to keep lexps left-recursive while
764 -- avoiding another shift/reduce-conflict.
765
766 list :: { RdrNameHsExpr }
767         : exp                           { ExplicitList [$1] }
768         | lexps                         { ExplicitList (reverse $1) }
769         | exp '..'                      { ArithSeqIn (From $1) }
770         | exp ',' exp '..'              { ArithSeqIn (FromThen $1 $3) }
771         | exp '..' exp                  { ArithSeqIn (FromTo $1 $3) }
772         | exp ',' exp '..' exp          { ArithSeqIn (FromThenTo $1 $3 $5) }
773         | exp srcloc '|' quals                  { HsDo ListComp (reverse 
774                                                 (ReturnStmt $1 : $4)) $2 }
775
776 lexps :: { [RdrNameHsExpr] }
777         : lexps ',' exp                 { $3 : $1 }
778         | exp ',' exp                   { [$3,$1] }
779
780 -----------------------------------------------------------------------------
781 -- List Comprehensions
782
783 quals :: { [RdrNameStmt] }
784         : quals ',' qual                { $3 : $1 }
785         | qual                          { [$1] }
786
787 qual  :: { RdrNameStmt }
788         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
789                                            returnP (BindStmt p $4 $1) }
790         | srcloc exp                    { GuardStmt $2 $1 }
791         | srcloc 'let' declbinds        { LetStmt $3 }
792
793 -----------------------------------------------------------------------------
794 -- Case alternatives
795
796 altslist :: { [RdrNameMatch] }
797         : '{'            alts '}'       { reverse $2 }
798         |     layout_on  alts  close    { reverse $2 }
799
800 alts    :: { [RdrNameMatch] }
801         : alts1                         { $1 }
802         | ';' alts                      { $2 }
803
804 alts1   :: { [RdrNameMatch] }
805         : alts1 ';' alt                 { $3 : $1 }
806         | alts1 ';'                     { $1 }
807         | alt                           { [$1] }
808
809 alt     :: { RdrNameMatch }
810         : infixexp opt_sig ralt wherebinds
811                                         {% checkPattern $1 `thenP` \p ->
812                                            returnP (Match [] [p] $2
813                                                      (GRHSs $3 $4 Nothing)) }
814
815 ralt :: { [RdrNameGRHS] }
816         : '->' srcloc exp               { [GRHS [ExprStmt $3 $2] $2] }
817         | gdpats                        { (reverse $1) }
818
819 gdpats :: { [RdrNameGRHS] }
820         : gdpats gdpat                  { $2 : $1 }
821         | gdpat                         { [$1] }
822
823 gdpat   :: { RdrNameGRHS }
824         : srcloc '|' quals '->' exp     { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
825
826 -----------------------------------------------------------------------------
827 -- Statement sequences
828
829 stmtlist :: { [RdrNameStmt] }
830         : '{'                   stmts '}'       { reverse $2 }
831         |     layout_on_for_do  stmts close     { reverse $2 }
832
833 -- Stmt list should really end in an expression, but it's not
834 -- convenient to enforce this here, so we throw out erroneous
835 -- statement sequences in the renamer instead.
836
837 stmts :: { [RdrNameStmt] }
838         : ';' stmts1                    { $2 }
839         | stmts1                        { $1 }
840
841 stmts1 :: { [RdrNameStmt] }
842         : stmts1 ';' stmt               { $3 : $1 }
843         | stmts1 ';'                    { $1 }
844         | stmt                          { [$1] }
845
846 stmt  :: { RdrNameStmt }
847         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
848                                            returnP (BindStmt p $4 $1) }
849         | srcloc exp                    { ExprStmt $2 $1 }
850         | srcloc 'let' declbinds        { LetStmt $3 }
851
852 -----------------------------------------------------------------------------
853 -- Record Field Update/Construction
854
855 fbinds  :: { RdrNameHsRecordBinds }
856         : fbinds ',' fbind              { $3 : $1 }
857         | fbinds ','                    { $1 }
858         | fbind                         { [$1] }
859         | {- empty -}                   { [] }
860
861 fbind   :: { (RdrName, RdrNameHsExpr, Bool) }
862         : qvar '=' exp                  { ($1,$3,False) }
863
864 -----------------------------------------------------------------------------
865 -- Implicit Parameter Bindings
866
867 dbinding :: { [(RdrName, RdrNameHsExpr)] }
868         : '{' dbinds '}'                { $2 }
869         | layout_on dbinds close        { $2 }
870
871 dbinds  :: { [(RdrName, RdrNameHsExpr)] }
872         : dbinds ';' dbind              { $3 : $1 }
873         | dbinds ';'                    { $1 }
874         | dbind                         { [$1] }
875         | {- empty -}                   { [] }
876
877 dbind   :: { (RdrName, RdrNameHsExpr) }
878 dbind   : ipvar '=' exp                 { ($1, $3) }
879
880 -----------------------------------------------------------------------------
881 -- Variables, Constructors and Operators.
882
883 gcon    :: { RdrName }
884         : '(' ')'               { unitCon_RDR }
885         | '[' ']'               { nilCon_RDR }
886         | '(' commas ')'        { tupleCon_RDR $2 }
887         | qcon                  { $1 }
888
889 var     :: { RdrName }
890         : varid                 { $1 }
891         | '(' varsym ')'        { $2 }
892
893 qvar    :: { RdrName }
894         : qvarid                { $1 }
895         | '(' varsym ')'        { $2 }
896         | '(' qvarsym1 ')'      { $2 }
897 -- We've inlined qvarsym here so that the decision about
898 -- whether it's a qvar or a var can be postponed until
899 -- *after* we see the close paren.
900
901 ipvar   :: { RdrName }
902         : IPVARID               { (mkSrcUnqual ipName (tailFS $1)) }
903
904 con     :: { RdrName }
905         : conid                 { $1 }
906         | '(' consym ')'        { $2 }
907
908 qcon    :: { RdrName }
909         : qconid                { $1 }
910         | '(' qconsym ')'       { $2 }
911
912 varop   :: { RdrName }
913         : varsym                { $1 }
914         | '`' varid '`'         { $2 }
915
916 qvarop :: { RdrName }
917         : qvarsym               { $1 }
918         | '`' qvarid '`'        { $2 }
919
920 qvaropm :: { RdrName }
921         : qvarsymm              { $1 }
922         | '`' qvarid '`'        { $2 }
923
924 conop :: { RdrName }
925         : consym                { $1 }  
926         | '`' conid '`'         { $2 }
927
928 qconop :: { RdrName }
929         : qconsym               { $1 }
930         | '`' qconid '`'        { $2 }
931
932 -----------------------------------------------------------------------------
933 -- Any operator
934
935 op      :: { RdrName }   -- used in infix decls
936         : varop                 { $1 }
937         | conop                 { $1 }
938
939 qop     :: { RdrNameHsExpr }   -- used in sections
940         : qvarop                { HsVar $1 }
941         | qconop                { HsVar $1 }
942
943 qopm    :: { RdrNameHsExpr }   -- used in sections
944         : qvaropm               { HsVar $1 }
945         | qconop                { HsVar $1 }
946
947 -----------------------------------------------------------------------------
948 -- VarIds
949
950 qvarid :: { RdrName }
951         : varid                 { $1 }
952         | QVARID                { case $1 of { (mod,n) ->
953                                   mkSrcQual varName mod n } }
954
955 varid :: { RdrName }
956         : VARID                 { mkSrcUnqual varName $1 }
957         | 'as'                  { as_var_RDR }
958         | 'qualified'           { qualified_var_RDR }
959         | 'hiding'              { hiding_var_RDR }
960         | 'forall'              { forall_var_RDR }
961         | 'export'              { export_var_RDR }
962         | 'label'               { label_var_RDR }
963         | 'dynamic'             { dynamic_var_RDR }
964         | 'unsafe'              { unsafe_var_RDR }
965         | 'stdcall'             { stdcall_var_RDR }
966         | 'ccall'               { ccall_var_RDR }
967
968 varid_no_unsafe :: { RdrName }
969         : VARID                 { mkSrcUnqual varName $1 }
970         | 'as'                  { as_var_RDR }
971         | 'qualified'           { qualified_var_RDR }
972         | 'hiding'              { hiding_var_RDR }
973         | 'forall'              { forall_var_RDR }
974         | 'export'              { export_var_RDR }
975         | 'label'               { label_var_RDR }
976         | 'dynamic'             { dynamic_var_RDR }
977         | 'stdcall'             { stdcall_var_RDR }
978         | 'ccall'               { ccall_var_RDR }
979
980 -----------------------------------------------------------------------------
981 -- ConIds
982
983 qconid :: { RdrName }
984         : conid                 { $1 }
985         | QCONID                { case $1 of { (mod,n) ->
986                                   mkSrcQual dataName mod n } }
987
988 conid   :: { RdrName }
989         : CONID                 { mkSrcUnqual dataName $1 }
990
991 -----------------------------------------------------------------------------
992 -- ConSyms
993
994 qconsym :: { RdrName }
995         : consym                { $1 }
996         | QCONSYM               { case $1 of { (mod,n) ->
997                                   mkSrcQual dataName mod n } }
998
999 consym :: { RdrName }
1000         : CONSYM                { mkSrcUnqual dataName $1 }
1001
1002 -----------------------------------------------------------------------------
1003 -- VarSyms
1004
1005 qvarsym :: { RdrName }
1006         : varsym                { $1 }
1007         | qvarsym1              { $1 }
1008
1009 qvarsymm :: { RdrName }
1010         : varsymm               { $1 }
1011         | qvarsym1              { $1 }
1012
1013 varsym :: { RdrName }
1014         : VARSYM                { mkSrcUnqual varName $1 }
1015         | '-'                   { minus_RDR }
1016         | '!'                   { pling_RDR }
1017         | '.'                   { dot_RDR }
1018
1019 varsymm :: { RdrName } -- varsym not including '-'
1020         : VARSYM                { mkSrcUnqual varName $1 }
1021         | '!'                   { pling_RDR }
1022         | '.'                   { dot_RDR }
1023
1024 qvarsym1 :: { RdrName }
1025         : QVARSYM               { case $1 of { (mod,n) ->
1026                                   mkSrcQual varName mod n } }
1027
1028 literal :: { HsLit }
1029         : INTEGER               { HsInt    $1 }
1030         | CHAR                  { HsChar   $1 }
1031         | RATIONAL              { HsFrac   $1 }
1032         | STRING                { HsString $1 }
1033
1034         | PRIMINTEGER           { HsIntPrim    $1 }
1035         | PRIMCHAR              { HsCharPrim   $1 }
1036         | PRIMSTRING            { HsStringPrim $1 }
1037         | PRIMFLOAT             { HsFloatPrim  $1 }
1038         | PRIMDOUBLE            { HsDoublePrim $1 }
1039         | CLITLIT               { HsLitLit     $1 }
1040
1041 srcloc :: { SrcLoc }    :       {% getSrcLocP }
1042  
1043 -----------------------------------------------------------------------------
1044 -- Layout
1045
1046 close :: { () }
1047         : vccurly               { () } -- context popped in lexer.
1048         | error                 {% popContext }
1049
1050 layout_on         :: { () }     : {% layoutOn True{-strict-} }
1051 layout_on_for_do  :: { () }     : {% layoutOn False }
1052
1053 -----------------------------------------------------------------------------
1054 -- Miscellaneous (mostly renamings)
1055
1056 modid   :: { ModuleName }
1057         : CONID                 { mkSrcModuleFS $1 }
1058
1059 tycon   :: { RdrName }
1060         : CONID                 { mkSrcUnqual tcClsName $1 }
1061
1062 qtycon :: { RdrName }
1063         : tycon                 { $1 }
1064         | QCONID                { case $1 of { (mod,n) ->
1065                                   mkSrcQual tcClsName mod n } }
1066
1067 qtycls  :: { RdrName }
1068         : qtycon                { $1 }
1069
1070 tyvar   :: { RdrName }
1071         : VARID                 { mkSrcUnqual tvName $1 }
1072         | 'as'                  { as_tyvar_RDR }
1073         | 'qualified'           { qualified_tyvar_RDR }
1074         | 'hiding'              { hiding_tyvar_RDR }
1075         | 'export'              { export_tyvar_RDR }
1076         | 'label'               { label_tyvar_RDR }
1077         | 'dynamic'             { dynamic_tyvar_RDR }
1078         | 'unsafe'              { unsafe_tyvar_RDR }
1079         | 'stdcall'             { stdcall_tyvar_RDR }
1080         | 'ccall'               { ccall_tyvar_RDR }
1081         -- NOTE: no 'forall'
1082
1083 -----------------------------------------------------------------------------
1084
1085 {
1086 happyError :: P a
1087 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
1088 }