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