095f82897a1efa4711f789fe938c78d3361977d3
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.34 2000/08/01 09:08:27 simonpj Exp $
4
5 Haskell grammar.
6
7 Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -----------------------------------------------------------------------------
9 -}
10
11 {
12 module Parser ( parse ) where
13
14 import HsSyn
15 import HsPragmas
16 import HsTypes          ( mkHsTupCon )
17
18 import RdrHsSyn
19 import Lex
20 import ParseUtil
21 import RdrName
22 import PrelInfo         ( mAIN_Name )
23 import OccName          ( varName, ipName, tcName, dataName, tcClsName, tvName )
24 import SrcLoc           ( SrcLoc )
25 import Module
26 import CallConv
27 import CmdLineOpts      ( opt_SccProfilingOn )
28 import BasicTypes       ( Boxity(..), Fixity(..), FixityDirection(..), NewOrData(..) )
29 import Panic
30
31 import GlaExts
32 import FastString       ( tailFS )
33
34 #include "HsVersions.h"
35 }
36
37 {-
38 -----------------------------------------------------------------------------
39 Conflicts: 14 shift/reduce
40         (note: it's currently 21 -- JRL, 31/1/2000)
41
42 8 for abiguity in 'if x then y else z + 1'
43         (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
44 1 for ambiguity in 'if x then y else z :: T'
45         (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
46 3 for ambiguity in 'case x of y :: a -> b'
47         (don't know whether to reduce 'a' as a btype or shift the '->'.
48          conclusion:  bogus expression anyway, doesn't matter)
49
50 1 for ambiguity in '{-# RULES "name" forall = ... #-}' 
51         since 'forall' is a valid variable name, we don't know whether
52         to treat a forall on the input as the beginning of a quantifier
53         or the beginning of the rule itself.  Resolving to shift means
54         it's always treated as a quantifier, hence the above is disallowed.
55         This saves explicitly defining a grammar for the rule lhs that
56         doesn't include 'forall'.
57
58 1 for ambiguity in 'x @ Rec{..}'.  
59         Only sensible parse is 'x @ (Rec{..})', which is what resolving
60         to shift gives us.
61
62 -----------------------------------------------------------------------------
63 -}
64
65 %token
66  '_'            { ITunderscore }                -- Haskell keywords
67  'as'           { ITas }
68  'case'         { ITcase }      
69  'class'        { ITclass } 
70  'data'         { ITdata } 
71  'default'      { ITdefault }
72  'deriving'     { ITderiving }
73  'do'           { ITdo }
74  'else'         { ITelse }
75  'hiding'       { IThiding }
76  'if'           { ITif }
77  'import'       { ITimport }
78  'in'           { ITin }
79  'infix'        { ITinfix }
80  'infixl'       { ITinfixl }
81  'infixr'       { ITinfixr }
82  'instance'     { ITinstance }
83  'let'          { ITlet }
84  'module'       { ITmodule }
85  'newtype'      { ITnewtype }
86  'of'           { ITof }
87  'qualified'    { ITqualified }
88  'then'         { ITthen }
89  'type'         { ITtype }
90  'where'        { ITwhere }
91  '_scc_'        { ITscc }
92
93  'forall'       { ITforall }                    -- GHC extension keywords
94  'foreign'      { ITforeign }
95  'export'       { ITexport }
96  'label'        { ITlabel } 
97  'dynamic'      { ITdynamic }
98  'unsafe'       { ITunsafe }
99  'with'         { ITwith }
100  'stdcall'      { ITstdcallconv }
101  'ccall'        { ITccallconv }
102  '_ccall_'      { ITccall (False, False, False) }
103  '_ccall_GC_'   { ITccall (False, False, True)  }
104  '_casm_'       { ITccall (False, True,  False) }
105  '_casm_GC_'    { ITccall (False, True,  True)  }
106
107  '{-# SPECIALISE'  { ITspecialise_prag }
108  '{-# SOURCE'      { ITsource_prag }
109  '{-# INLINE'      { ITinline_prag }
110  '{-# NOINLINE'    { ITnoinline_prag }
111  '{-# RULES'       { ITrules_prag }
112  '{-# DEPRECATED'  { ITdeprecated_prag }
113  '#-}'             { ITclose_prag }
114
115 {-
116  '__interface'  { ITinterface }                 -- interface keywords
117  '__export'     { IT__export }
118  '__instimport' { ITinstimport }
119  '__forall'     { IT__forall }
120  '__letrec'     { ITletrec }
121  '__coerce'     { ITcoerce }
122  '__depends'    { ITdepends }
123  '__inline'     { ITinline }
124  '__DEFAULT'    { ITdefaultbranch }
125  '__bot'        { ITbottom }
126  '__integer'    { ITinteger_lit }
127  '__float'      { ITfloat_lit }
128  '__rational'   { ITrational_lit }
129  '__addr'       { ITaddr_lit }
130  '__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                           { (reverse $1,[]) }
233         | importdecls ';' cvtopdecls            { (reverse $1,$3) }
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 
349                         (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (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 (InstDecl $3 binds sigs Nothing $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         | '{-# DEPRECATED' deprecations '#-}'           { $2 }
376         | '{-# RULES' rules '#-}'                       { $2 }
377         | decl                                          { $1 }
378
379 decls   :: { [RdrBinding] }
380         : decls ';' decl                { $3 : $1 }
381         | decls ';'                     { $1 }
382         | decl                          { [$1] }
383         | {- empty -}                   { [] }
384
385 decl    :: { RdrBinding }
386         : fixdecl                       { $1 }
387         | valdef                        { $1 }
388         | '{-# INLINE'   srcloc opt_phase qvar '#-}'    { RdrSig (InlineSig $4 $3 $2) }
389         | '{-# NOINLINE' srcloc opt_phase qvar '#-}'    { RdrSig (NoInlineSig $4 $3 $2) }
390         | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
391                 { foldr1 RdrAndBindings 
392                     (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
393         | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
394                 { RdrSig (SpecInstSig $4 $2) }
395
396 opt_phase :: { Maybe Int }
397           : INTEGER                     { Just (fromInteger $1) }
398           | {- empty -}                 { Nothing }
399
400 wherebinds :: { RdrNameHsBinds }
401         : where                 { cvBinds cvValSig (groupBindings $1) }
402
403 where   :: { [RdrBinding] }
404         : 'where' decllist              { $2 }
405         | {- empty -}                   { [] }
406
407 declbinds :: { RdrNameHsBinds }
408         : decllist                      { cvBinds cvValSig (groupBindings $1) }
409
410 decllist :: { [RdrBinding] }
411         : '{'            decls '}'      { $2 }
412         |     layout_on  decls close    { $2 }
413
414 fixdecl :: { RdrBinding }
415         : srcloc infix prec ops         { foldr1 RdrAndBindings
416                                             [ RdrSig (FixSig (FixitySig n 
417                                                             (Fixity $3 $2) $1))
418                                             | n <- $4 ] }
419
420 -----------------------------------------------------------------------------
421 -- Transformation Rules
422
423 rules   :: { RdrBinding }
424         :  rules ';' rule                       { $1 `RdrAndBindings` $3 }
425         |  rules ';'                            { $1 }
426         |  rule                                 { $1 }
427         |  {- empty -}                          { RdrNullBind }
428
429 rule    :: { RdrBinding }
430         : STRING rule_forall fexp '=' srcloc exp
431              { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
432
433 rule_forall :: { [RdrNameRuleBndr] }
434         : 'forall' rule_var_list '.'            { $2 }
435         | {- empty -}                           { [] }
436
437 rule_var_list :: { [RdrNameRuleBndr] }
438         : rule_var                              { [$1] }
439         | rule_var rule_var_list                { $1 : $2 }
440
441 rule_var :: { RdrNameRuleBndr }
442         : varid                                 { RuleBndr $1 }
443         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
444
445 -----------------------------------------------------------------------------
446 -- Deprecations
447
448 deprecations :: { RdrBinding }
449         : deprecations ';' deprecation          { $1 `RdrAndBindings` $3 }
450         | deprecations ';'                      { $1 }
451         | deprecation                           { $1 }
452         | {- empty -}                           { RdrNullBind }
453
454 -- SUP: TEMPORARY HACK, not checking for `module Foo'
455 deprecation :: { RdrBinding }
456         : srcloc exportlist STRING
457                 { foldr RdrAndBindings RdrNullBind 
458                         [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
459
460 -----------------------------------------------------------------------------
461 -- Foreign import/export
462
463 callconv :: { Int }
464         : 'stdcall'             { stdCallConv }
465         | 'ccall'               { cCallConv }
466         | {- empty -}           { defaultCallConv }
467
468 unsafe_flag :: { Bool }
469         : 'unsafe'              { True }
470         | {- empty -}           { False }
471
472 ext_name :: { Maybe ExtName }
473         : 'dynamic'             { Just Dynamic }
474         | STRING                { Just (ExtName $1 Nothing)   }
475         | STRING STRING         { Just (ExtName $2 (Just $1)) }
476         | {- empty -}           { Nothing }
477
478
479 -----------------------------------------------------------------------------
480 -- Type signatures
481
482 opt_sig :: { Maybe RdrNameHsType }
483         : {- empty -}                   { Nothing }
484         | '::' sigtype                  { Just $2 }
485
486 opt_asig :: { Maybe RdrNameHsType }
487         : {- empty -}                   { Nothing }
488         | '::' atype                    { Just $2 }
489
490 sigtypes :: { [RdrNameHsType] }
491         : sigtype                       { [ $1 ] }
492         | sigtypes ',' sigtype          { $3 : $1 }
493
494 sigtype :: { RdrNameHsType }
495         : ctype                         { mkHsForAllTy Nothing [] $1 }
496
497 sig_vars :: { [RdrName] }
498          : sig_vars ',' var             { $3 : $1 }
499          | var                          { [ $1 ] }
500
501 -----------------------------------------------------------------------------
502 -- Types
503
504 -- A ctype is a for-all type
505 ctype   :: { RdrNameHsType }
506         : 'forall' tyvars '.' ctype     { mkHsForAllTy (Just $2) [] $4 }
507         | context type                  { mkHsForAllTy Nothing   $1 $2 }
508                 -- A type of form (context => type) is an *implicit* HsForAllTy
509         | type                          { $1 }
510
511 type :: { RdrNameHsType }
512         : btype '->' type               { HsFunTy $1 $3 }
513         | ipvar '::' type               { mkHsIParamTy $1 $3 }
514         | btype                         { $1 }
515
516 btype :: { RdrNameHsType }
517         : btype atype                   { HsAppTy $1 $2 }
518         | atype                         { $1 }
519
520 atype :: { RdrNameHsType }
521         : gtycon                        { HsTyVar $1 }
522         | tyvar                         { HsTyVar $1 }
523         | '(' type ',' types ')'        { HsTupleTy (mkHsTupCon tcName Boxed  ($2:$4)) ($2 : reverse $4) }
524         | '(#' types '#)'               { HsTupleTy (mkHsTupCon tcName Unboxed     $2) (reverse $2)      }
525         | '[' type ']'                  { HsListTy $2 }
526         | '(' ctype ')'                 { $2 }
527
528 -- An inst_type is what occurs in the head of an instance decl
529 --      e.g.  (Foo a, Gaz b) => Wibble a b
530 -- It's kept as a single type, with a MonoDictTy at the right
531 -- hand corner, for convenience.
532 inst_type :: { RdrNameHsType }
533         : ctype                         {% checkInstType $1 }
534
535 types0  :: { [RdrNameHsType] }
536         : types                         { $1 }
537         | {- empty -}                   { [] }
538
539 types   :: { [RdrNameHsType] }
540         : type                          { [$1] }
541         | types  ',' type               { $3 : $1 }
542
543 simpletype :: { (RdrName, [RdrNameHsTyVar]) }
544         : tycon tyvars                  { ($1, reverse $2) }
545
546 tyvars :: { [RdrNameHsTyVar] }
547         : tyvars tyvar                  { UserTyVar $2 : $1 }
548         | {- empty -}                   { [] }
549
550 fds :: { [([RdrName], [RdrName])] }
551         : {- empty -}                   { [] }
552         | '|' fds1                      { reverse $2 }
553
554 fds1 :: { [([RdrName], [RdrName])] }
555         : fds1 ',' fd                   { $3 : $1 }
556         | fd                            { [$1] }
557
558 fd :: { ([RdrName], [RdrName]) }
559         : varids0 '->' varids0          { (reverse $1, reverse $3) }
560
561 varids0 :: { [RdrName] }
562         : {- empty -}                   { [] }
563         | varids0 tyvar                 { $2 : $1 }
564
565 -----------------------------------------------------------------------------
566 -- Datatype declarations
567
568 constrs :: { [RdrNameConDecl] }
569         : constrs '|' constr            { $3 : $1 }
570         | constr                        { [$1] }
571
572 constr :: { RdrNameConDecl }
573         : srcloc forall context constr_stuff
574                 { mkConDecl (fst $4) $2 $3 (snd $4) $1 }
575         | srcloc forall constr_stuff
576                 { mkConDecl (fst $3) $2 [] (snd $3) $1 }
577
578 forall :: { [RdrNameHsTyVar] }
579         : 'forall' tyvars '.'           { $2 }
580         | {- empty -}                   { [] }
581
582 context :: { RdrNameContext }
583         : btype '=>'                    {% checkContext $1 }
584
585 constr_stuff :: { (RdrName, RdrNameConDetails) }
586         : scontype                      { (fst $1, VanillaCon (snd $1)) }
587         | sbtype conop sbtype           { ($2, InfixCon $1 $3) }
588         | con '{' fielddecls '}'        { ($1, RecCon (reverse $3)) }
589
590 newconstr :: { RdrNameConDecl }
591         : srcloc conid atype    { mkConDecl $2 [] [] (NewCon $3 Nothing) $1 }
592         | srcloc conid '{' var '::' type '}'
593                                 { mkConDecl $2 [] [] (NewCon $6 (Just $4)) $1 }
594
595 scontype :: { (RdrName, [RdrNameBangType]) }
596         : btype                         {% splitForConApp $1 [] }
597         | scontype1                     { $1 }
598
599 scontype1 :: { (RdrName, [RdrNameBangType]) }
600         : btype '!' atype               {% splitForConApp $1 [Banged $3] }
601         | scontype1 satype              { (fst $1, snd $1 ++ [$2] ) }
602         | '(' consym ')'                { ($2,[]) }
603
604 satype :: { RdrNameBangType }
605         : atype                         { Unbanged $1 }
606         | '!' atype                     { Banged   $2 }
607
608 sbtype :: { RdrNameBangType }
609         : btype                         { Unbanged $1 }
610         | '!' atype                     { Banged   $2 }
611
612 fielddecls :: { [([RdrName],RdrNameBangType)] }
613         : fielddecls ',' fielddecl      { $3 : $1 }
614         | fielddecl                     { [$1] }
615
616 fielddecl :: { ([RdrName],RdrNameBangType) }
617         : sig_vars '::' stype           { (reverse $1, $3) }
618
619 stype :: { RdrNameBangType }
620         : ctype                         { Unbanged $1 } 
621         | '!' atype                     { Banged   $2 }
622
623 deriving :: { Maybe [RdrName] }
624         : {- empty -}                   { Nothing }
625         | 'deriving' qtycls             { Just [$2] }
626         | 'deriving' '('          ')'   { Just [] }
627         | 'deriving' '(' dclasses ')'   { Just (reverse $3) }
628
629 dclasses :: { [RdrName] }
630         : dclasses ',' qtycls           { $3 : $1 }
631         | qtycls                        { [$1] }
632
633 -----------------------------------------------------------------------------
634 -- Value definitions
635
636 {- There's an awkward overlap with a type signature.  Consider
637         f :: Int -> Int = ...rhs...
638    Then we can't tell whether it's a type signature or a value
639    definition with a result signature until we see the '='.
640    So we have to inline enough to postpone reductions until we know.
641 -}
642
643 {-
644   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
645   instead of qvar, we get another shift/reduce-conflict. Consider the
646   following programs:
647   
648      { (^^) :: Int->Int ; }          Type signature; only var allowed
649
650      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
651                                      qvar allowed (because of instance decls)
652   
653   We can't tell whether to reduce var to qvar until after we've read the signatures.
654 -}
655
656 valdef :: { RdrBinding }
657         : infixexp srcloc opt_sig rhs           {% checkValDef $1 $3 $4 $2 }
658         | infixexp srcloc '::' sigtype          {% checkValSig $1 $4 $2 }
659         | var ',' sig_vars srcloc '::' sigtype  { foldr1 RdrAndBindings 
660                                                          [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
661                                                 }
662
663 rhs     :: { RdrNameGRHSs }
664         : '=' srcloc exp wherebinds     { GRHSs (unguardedRHS $3 $2) 
665                                                                 $4 Nothing}
666         | gdrhs wherebinds              { GRHSs (reverse $1) $2 Nothing }
667
668 gdrhs :: { [RdrNameGRHS] }
669         : gdrhs gdrh                    { $2 : $1 }
670         | gdrh                          { [$1] }
671
672 gdrh :: { RdrNameGRHS }
673         : '|' srcloc quals '=' exp      { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
674
675 -----------------------------------------------------------------------------
676 -- Expressions
677
678 exp   :: { RdrNameHsExpr }
679         : infixexp '::' sigtype         { ExprWithTySig $1 $3 }
680         | infixexp 'with' dbinding      { HsWith $1 $3 }
681         | infixexp                      { $1 }
682
683 infixexp :: { RdrNameHsExpr }
684         : exp10                         { $1 }
685         | infixexp qop exp10            { OpApp $1 $2 (panic "fixity") $3 }
686
687 exp10 :: { RdrNameHsExpr }
688         : '\\' aexp aexps opt_asig '->' srcloc exp      
689                         {% checkPatterns ($2 : reverse $3) `thenP` \ ps -> 
690                            returnP (HsLam (Match [] ps $4 
691                                             (GRHSs (unguardedRHS $7 $6) 
692                                                    EmptyBinds Nothing))) }
693         | 'let' declbinds 'in' exp              { HsLet $2 $4 }
694         | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
695         | 'case' srcloc exp 'of' altslist       { HsCase $3 $5 $2 }
696         | '-' fexp                              { NegApp $2 (error "NegApp") }
697         | srcloc 'do' stmtlist                  { HsDo DoStmt $3 $1 }
698
699         | '_ccall_'    ccallid aexps0           { HsCCall $2 $3 False False cbot }
700         | '_ccall_GC_' ccallid aexps0           { HsCCall $2 $3 True  False cbot }
701         | '_casm_'     CLITLIT aexps0           { HsCCall $2 $3 False True  cbot }
702         | '_casm_GC_'  CLITLIT aexps0           { HsCCall $2 $3 True  True  cbot }
703
704         | '_scc_' STRING exp                    { if opt_SccProfilingOn
705                                                         then HsSCC $2 $3
706                                                         else HsPar $3 }
707
708         | fexp                                  { $1 }
709
710 ccallid :: { FAST_STRING }
711         :  VARID                                { $1 }
712         |  CONID                                { $1 }
713
714 fexp    :: { RdrNameHsExpr }
715         : fexp aexp                             { HsApp $1 $2 }
716         | aexp                                  { $1 }
717
718 aexps0  :: { [RdrNameHsExpr] }
719         : aexps                                 { reverse $1 }
720
721 aexps   :: { [RdrNameHsExpr] }
722         : aexps aexp                            { $2 : $1 }
723         | {- empty -}                           { [] }
724
725 aexp    :: { RdrNameHsExpr }
726         : aexp '{' fbinds '}'           {% mkRecConstrOrUpdate $1 (reverse $3) }
727         | aexp1                         { $1 }
728
729 aexp1   :: { RdrNameHsExpr }
730         : qvar                          { HsVar $1 }
731         | ipvar                         { HsIPVar $1 }
732         | gcon                          { HsVar $1 }
733         | literal                       { HsLit $1 }
734         | '(' exp ')'                   { HsPar $2 }
735         | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) Boxed}
736         | '(#' texps '#)'               { ExplicitTuple (reverse $2)      Unboxed }
737         | '[' list ']'                  { $2 }
738         | '(' infixexp qop ')'          { SectionL $2 $3  }
739         | '(' qopm infixexp ')'         { SectionR $2 $3 }
740         | qvar '@' aexp                 { EAsPat $1 $3 }
741         | '_'                           { EWildPat }
742         | '~' aexp1                     { ELazyPat $2 }
743
744 texps :: { [RdrNameHsExpr] }
745         : texps ',' exp                 { $3 : $1 }
746         | exp                           { [$1] }
747
748 -----------------------------------------------------------------------------
749 -- List expressions
750
751 -- The rules below are little bit contorted to keep lexps left-recursive while
752 -- avoiding another shift/reduce-conflict.
753
754 list :: { RdrNameHsExpr }
755         : exp                           { ExplicitList [$1] }
756         | lexps                         { ExplicitList (reverse $1) }
757         | exp '..'                      { ArithSeqIn (From $1) }
758         | exp ',' exp '..'              { ArithSeqIn (FromThen $1 $3) }
759         | exp '..' exp                  { ArithSeqIn (FromTo $1 $3) }
760         | exp ',' exp '..' exp          { ArithSeqIn (FromThenTo $1 $3 $5) }
761         | exp srcloc '|' quals                  { HsDo ListComp (reverse 
762                                                 (ReturnStmt $1 : $4)) $2 }
763
764 lexps :: { [RdrNameHsExpr] }
765         : lexps ',' exp                 { $3 : $1 }
766         | exp ',' exp                   { [$3,$1] }
767
768 -----------------------------------------------------------------------------
769 -- List Comprehensions
770
771 quals :: { [RdrNameStmt] }
772         : quals ',' qual                { $3 : $1 }
773         | qual                          { [$1] }
774
775 qual  :: { RdrNameStmt }
776         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
777                                            returnP (BindStmt p $4 $1) }
778         | srcloc exp                    { GuardStmt $2 $1 }
779         | srcloc 'let' declbinds        { LetStmt $3 }
780
781 -----------------------------------------------------------------------------
782 -- Case alternatives
783
784 altslist :: { [RdrNameMatch] }
785         : '{'            alts '}'       { reverse $2 }
786         |     layout_on  alts  close    { reverse $2 }
787
788 alts    :: { [RdrNameMatch] }
789         : alts1                         { $1 }
790         | ';' alts                      { $2 }
791
792 alts1   :: { [RdrNameMatch] }
793         : alts1 ';' alt                 { $3 : $1 }
794         | alts1 ';'                     { $1 }
795         | alt                           { [$1] }
796
797 alt     :: { RdrNameMatch }
798         : infixexp opt_sig ralt wherebinds
799                                         {% checkPattern $1 `thenP` \p ->
800                                            returnP (Match [] [p] $2
801                                                      (GRHSs $3 $4 Nothing)) }
802
803 ralt :: { [RdrNameGRHS] }
804         : '->' srcloc exp               { [GRHS [ExprStmt $3 $2] $2] }
805         | gdpats                        { (reverse $1) }
806
807 gdpats :: { [RdrNameGRHS] }
808         : gdpats gdpat                  { $2 : $1 }
809         | gdpat                         { [$1] }
810
811 gdpat   :: { RdrNameGRHS }
812         : srcloc '|' quals '->' exp     { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
813
814 -----------------------------------------------------------------------------
815 -- Statement sequences
816
817 stmtlist :: { [RdrNameStmt] }
818         : '{'                   stmts '}'       { reverse $2 }
819         |     layout_on_for_do  stmts close     { reverse $2 }
820
821 -- Stmt list should really end in an expression, but it's not
822 -- convenient to enforce this here, so we throw out erroneous
823 -- statement sequences in the renamer instead.
824
825 stmts :: { [RdrNameStmt] }
826         : ';' stmts1                    { $2 }
827         | stmts1                        { $1 }
828
829 stmts1 :: { [RdrNameStmt] }
830         : stmts1 ';' stmt               { $3 : $1 }
831         | stmts1 ';'                    { $1 }
832         | stmt                          { [$1] }
833
834 stmt  :: { RdrNameStmt }
835         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
836                                            returnP (BindStmt p $4 $1) }
837         | srcloc exp                    { ExprStmt $2 $1 }
838         | srcloc 'let' declbinds        { LetStmt $3 }
839
840 -----------------------------------------------------------------------------
841 -- Record Field Update/Construction
842
843 fbinds  :: { RdrNameHsRecordBinds }
844         : fbinds ',' fbind              { $3 : $1 }
845         | fbinds ','                    { $1 }
846         | fbind                         { [$1] }
847         | {- empty -}                   { [] }
848
849 fbind   :: { (RdrName, RdrNameHsExpr, Bool) }
850         : qvar '=' exp                  { ($1,$3,False) }
851
852 -----------------------------------------------------------------------------
853 -- Implicit Parameter Bindings
854
855 dbinding :: { [(RdrName, RdrNameHsExpr)] }
856         : '{' dbinds '}'                { $2 }
857         | layout_on dbinds close        { $2 }
858
859 dbinds  :: { [(RdrName, RdrNameHsExpr)] }
860         : dbinds ';' dbind              { $3 : $1 }
861         | dbinds ';'                    { $1 }
862         | dbind                         { [$1] }
863         | {- empty -}                   { [] }
864
865 dbind   :: { (RdrName, RdrNameHsExpr) }
866 dbind   : ipvar '=' exp                 { ($1, $3) }
867
868 -----------------------------------------------------------------------------
869 -- Variables, Constructors and Operators.
870
871 gtycon  :: { RdrName }
872         : qtycon                        { $1 }
873         | '(' ')'                       { unitTyCon_RDR }
874         | '(' '->' ')'                  { funTyCon_RDR }
875         | '[' ']'                       { listTyCon_RDR }
876         | '(' commas ')'                { tupleTyCon_RDR $2 }
877
878 gcon    :: { RdrName }
879         : '(' ')'               { unitCon_RDR }
880         | '[' ']'               { nilCon_RDR }
881         | '(' commas ')'        { tupleCon_RDR $2 }
882         | qcon                  { $1 }
883
884 var     :: { RdrName }
885         : varid                 { $1 }
886         | '(' varsym ')'        { $2 }
887
888 qvar    :: { RdrName }
889         : qvarid                { $1 }
890         | '(' varsym ')'        { $2 }
891         | '(' qvarsym1 ')'      { $2 }
892 -- We've inlined qvarsym here so that the decision about
893 -- whether it's a qvar or a var can be postponed until
894 -- *after* we see the close paren.
895
896 ipvar   :: { RdrName }
897         : IPVARID               { (mkSrcUnqual ipName (tailFS $1)) }
898
899 con     :: { RdrName }
900         : conid                 { $1 }
901         | '(' consym ')'        { $2 }
902
903 qcon    :: { RdrName }
904         : qconid                { $1 }
905         | '(' qconsym ')'       { $2 }
906
907 varop   :: { RdrName }
908         : varsym                { $1 }
909         | '`' varid '`'         { $2 }
910
911 qvarop :: { RdrName }
912         : qvarsym               { $1 }
913         | '`' qvarid '`'        { $2 }
914
915 qvaropm :: { RdrName }
916         : qvarsymm              { $1 }
917         | '`' qvarid '`'        { $2 }
918
919 conop :: { RdrName }
920         : consym                { $1 }  
921         | '`' conid '`'         { $2 }
922
923 qconop :: { RdrName }
924         : qconsym               { $1 }
925         | '`' qconid '`'        { $2 }
926
927 -----------------------------------------------------------------------------
928 -- Any operator
929
930 op      :: { RdrName }   -- used in infix decls
931         : varop                 { $1 }
932         | conop                 { $1 }
933
934 qop     :: { RdrNameHsExpr }   -- used in sections
935         : qvarop                { HsVar $1 }
936         | qconop                { HsVar $1 }
937
938 qopm    :: { RdrNameHsExpr }   -- used in sections
939         : qvaropm               { HsVar $1 }
940         | qconop                { HsVar $1 }
941
942 -----------------------------------------------------------------------------
943 -- VarIds
944
945 qvarid :: { RdrName }
946         : varid                 { $1 }
947         | QVARID                { case $1 of { (mod,n) ->
948                                   mkSrcQual varName mod n } }
949
950 varid :: { RdrName }
951         : VARID                 { mkSrcUnqual varName $1 }
952         | 'as'                  { as_var_RDR }
953         | 'qualified'           { qualified_var_RDR }
954         | 'hiding'              { hiding_var_RDR }
955         | 'forall'              { forall_var_RDR }
956         | 'export'              { export_var_RDR }
957         | 'label'               { label_var_RDR }
958         | 'dynamic'             { dynamic_var_RDR }
959         | 'unsafe'              { unsafe_var_RDR }
960         | 'stdcall'             { stdcall_var_RDR }
961         | 'ccall'               { ccall_var_RDR }
962
963 varid_no_unsafe :: { RdrName }
964         : VARID                 { mkSrcUnqual varName $1 }
965         | 'as'                  { as_var_RDR }
966         | 'qualified'           { qualified_var_RDR }
967         | 'hiding'              { hiding_var_RDR }
968         | 'forall'              { forall_var_RDR }
969         | 'export'              { export_var_RDR }
970         | 'label'               { label_var_RDR }
971         | 'dynamic'             { dynamic_var_RDR }
972         | 'stdcall'             { stdcall_var_RDR }
973         | 'ccall'               { ccall_var_RDR }
974
975 -----------------------------------------------------------------------------
976 -- ConIds
977
978 qconid :: { RdrName }
979         : conid                 { $1 }
980         | QCONID                { case $1 of { (mod,n) ->
981                                   mkSrcQual dataName mod n } }
982
983 conid   :: { RdrName }
984         : CONID                 { mkSrcUnqual dataName $1 }
985
986 -----------------------------------------------------------------------------
987 -- ConSyms
988
989 qconsym :: { RdrName }
990         : consym                { $1 }
991         | QCONSYM               { case $1 of { (mod,n) ->
992                                   mkSrcQual dataName mod n } }
993
994 consym :: { RdrName }
995         : CONSYM                { mkSrcUnqual dataName $1 }
996
997 -----------------------------------------------------------------------------
998 -- VarSyms
999
1000 qvarsym :: { RdrName }
1001         : varsym                { $1 }
1002         | qvarsym1              { $1 }
1003
1004 qvarsymm :: { RdrName }
1005         : varsymm               { $1 }
1006         | qvarsym1              { $1 }
1007
1008 varsym :: { RdrName }
1009         : VARSYM                { mkSrcUnqual varName $1 }
1010         | '-'                   { minus_RDR }
1011         | '!'                   { pling_RDR }
1012         | '.'                   { dot_RDR }
1013
1014 varsymm :: { RdrName } -- varsym not including '-'
1015         : VARSYM                { mkSrcUnqual varName $1 }
1016         | '!'                   { pling_RDR }
1017         | '.'                   { dot_RDR }
1018
1019 qvarsym1 :: { RdrName }
1020         : QVARSYM               { case $1 of { (mod,n) ->
1021                                   mkSrcQual varName mod n } }
1022
1023 literal :: { HsLit }
1024         : INTEGER               { HsInt    $1 }
1025         | CHAR                  { HsChar   $1 }
1026         | RATIONAL              { HsFrac   $1 }
1027         | STRING                { HsString $1 }
1028
1029         | PRIMINTEGER           { HsIntPrim    $1 }
1030         | PRIMCHAR              { HsCharPrim   $1 }
1031         | PRIMSTRING            { HsStringPrim $1 }
1032         | PRIMFLOAT             { HsFloatPrim  $1 }
1033         | PRIMDOUBLE            { HsDoublePrim $1 }
1034         | CLITLIT               { HsLitLit     $1 }
1035
1036 srcloc :: { SrcLoc }    :       {% getSrcLocP }
1037  
1038 -----------------------------------------------------------------------------
1039 -- Layout
1040
1041 close :: { () }
1042         : vccurly               { () } -- context popped in lexer.
1043         | error                 {% popContext }
1044
1045 layout_on         :: { () }     : {% layoutOn True{-strict-} }
1046 layout_on_for_do  :: { () }     : {% layoutOn False }
1047
1048 -----------------------------------------------------------------------------
1049 -- Miscellaneous (mostly renamings)
1050
1051 modid   :: { ModuleName }
1052         : CONID                 { mkSrcModuleFS $1 }
1053
1054 tycon   :: { RdrName }
1055         : CONID                 { mkSrcUnqual tcClsName $1 }
1056
1057 qtycon :: { RdrName }
1058         : tycon                 { $1 }
1059         | QCONID                { case $1 of { (mod,n) ->
1060                                   mkSrcQual tcClsName mod n } }
1061
1062 qtycls  :: { RdrName }
1063         : qtycon                { $1 }
1064
1065 tyvar   :: { RdrName }
1066         : VARID                 { mkSrcUnqual tvName $1 }
1067         | 'as'                  { as_tyvar_RDR }
1068         | 'qualified'           { qualified_tyvar_RDR }
1069         | 'hiding'              { hiding_tyvar_RDR }
1070         | 'export'              { export_tyvar_RDR }
1071         | 'label'               { label_tyvar_RDR }
1072         | 'dynamic'             { dynamic_tyvar_RDR }
1073         | 'unsafe'              { unsafe_tyvar_RDR }
1074         | 'stdcall'             { stdcall_tyvar_RDR }
1075         | 'ccall'               { ccall_tyvar_RDR }
1076         -- NOTE: no 'forall'
1077
1078 commas :: { Int }
1079         : commas ','                    { $1 + 1 }
1080         | ','                           { 2 }
1081
1082 -----------------------------------------------------------------------------
1083
1084 {
1085 happyError :: P a
1086 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
1087 }