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