[project @ 2000-09-22 15:56:12 by simonpj]
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
1 {-
2 -----------------------------------------------------------------------------
3 $Id: Parser.y,v 1.36 2000/09/22 15:56:13 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          ( UserFS, 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  '{'            { 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  CHAR           { ITchar     $$ }
184  STRING         { ITstring   $$ }
185  INTEGER        { ITinteger  $$ }
186  RATIONAL       { ITrational $$ }
187
188  PRIMCHAR       { ITprimchar   $$ }
189  PRIMSTRING     { ITprimstring $$ }
190  PRIMINTEGER    { ITprimint    $$ }
191  PRIMFLOAT      { ITprimfloat  $$ }
192  PRIMDOUBLE     { ITprimdouble $$ }
193  CLITLIT        { ITlitlit     $$ }
194
195 %monad { P } { thenP } { returnP }
196 %lexer { lexer } { ITeof }
197 %name parse
198 %tokentype { Token }
199 %%
200
201 -----------------------------------------------------------------------------
202 -- Module Header
203
204 -- The place for module deprecation is really too restrictive, but if it
205 -- was allowed at its natural place just before 'module', we get an ugly
206 -- s/r conflict with the second alternative. Another solution would be the
207 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
208 -- either, and DEPRECATED is only expected to be used by people who really
209 -- know what they are doing. :-)
210
211 module  :: { RdrNameHsModule }
212         : srcloc 'module' modid maybemoddeprec maybeexports 'where' body 
213                 { HsModule $3 Nothing $5 (fst $7) (snd $7) $4 $1 }
214         | srcloc body
215                 { HsModule mAIN_Name Nothing Nothing (fst $2) (snd $2) Nothing $1 }
216
217 maybemoddeprec :: { Maybe DeprecTxt }
218         : '{-# DEPRECATED' STRING '#-}'         { Just $2 }
219         |  {- empty -}                          { Nothing }
220
221 body    :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
222         :  '{'            top '}'               { $2 }
223         |      layout_on  top close             { $2 }
224
225 top     :: { ([RdrNameImportDecl], [RdrNameHsDecl]) }
226         : importdecls                           { (reverse $1,[]) }
227         | importdecls ';' cvtopdecls            { (reverse $1,$3) }
228         | cvtopdecls                            { ([],$1) }
229
230 cvtopdecls :: { [RdrNameHsDecl] }
231         : topdecls                              { cvTopDecls (groupBindings $1)}
232
233 -----------------------------------------------------------------------------
234 -- The Export List
235
236 maybeexports :: { Maybe [RdrNameIE] }
237         :  '(' exportlist ')'                   { Just $2 }
238         |  {- empty -}                          { Nothing }
239
240 exportlist :: { [RdrNameIE] }
241         :  exportlist ',' export                { $3 : $1 }
242         |  exportlist ','                       { $1 }
243         |  export                               { [$1]  }
244         |  {- empty -}                          { [] }
245
246    -- GHC extension: we allow things like [] and (,,,) to be exported
247 export  :: { RdrNameIE }
248         :  qvar                                 { IEVar $1 }
249         |  gtycon                               { IEThingAbs $1 }
250         |  gtycon '(' '..' ')'                  { IEThingAll $1 }
251         |  gtycon '(' ')'                       { IEThingWith $1 [] }
252         |  gtycon '(' qcnames ')'               { IEThingWith $1 (reverse $3) }
253         |  'module' modid                       { IEModuleContents $2 }
254
255 qcnames :: { [RdrName] }
256         :  qcnames ',' qcname                   { $3 : $1 }
257         |  qcname                               { [$1]  }
258
259 qcname  :: { RdrName }
260         :  qvar                                 { $1 }
261         |  gcon                                 { $1 }
262
263 -----------------------------------------------------------------------------
264 -- Import Declarations
265
266 -- import decls can be *empty*, or even just a string of semicolons
267 -- whereas topdecls must contain at least one topdecl.
268
269 importdecls :: { [RdrNameImportDecl] }
270         : importdecls ';' importdecl            { $3 : $1 }
271         | importdecls ';'                       { $1 }
272         | importdecl                            { [ $1 ] }
273         | {- empty -}                           { [] }
274
275 importdecl :: { RdrNameImportDecl }
276         : 'import' srcloc maybe_src optqualified CONID maybeas maybeimpspec 
277                 { ImportDecl (mkSrcModuleFS $5) $3 $4 $6 $7 $2 }
278
279 maybe_src :: { WhereFrom }
280         : '{-# SOURCE' '#-}'                    { ImportByUserSource }
281         | {- empty -}                           { ImportByUser }
282
283 optqualified :: { Bool }
284         : 'qualified'                           { True  }
285         | {- empty -}                           { False }
286
287 maybeas :: { Maybe ModuleName }
288         : 'as' modid                            { Just $2 }
289         | {- empty -}                           { Nothing }
290
291 maybeimpspec :: { Maybe (Bool, [RdrNameIE]) }
292         : impspec                               { Just $1 }
293         | {- empty -}                           { Nothing }
294
295 impspec :: { (Bool, [RdrNameIE]) }
296         :  '(' exportlist ')'                   { (False, reverse $2) }
297         |  'hiding' '(' exportlist ')'          { (True,  reverse $3) }
298
299 -----------------------------------------------------------------------------
300 -- Fixity Declarations
301
302 prec    :: { Int }
303         : {- empty -}                           { 9 }
304         | INTEGER                               {%  checkPrec $1 `thenP_`
305                                                     returnP (fromInteger $1) }
306
307 infix   :: { FixityDirection }
308         : 'infix'                               { InfixN  }
309         | 'infixl'                              { InfixL  }
310         | 'infixr'                              { InfixR }
311
312 ops     :: { [RdrName] }
313         : ops ',' op                            { $3 : $1 }
314         | op                                    { [$1] }
315
316 -----------------------------------------------------------------------------
317 -- Top-Level Declarations
318
319 topdecls :: { [RdrBinding] }
320         : topdecls ';' topdecl          { ($3 : $1) }
321         | topdecls ';'                  { $1 }
322         | topdecl                       { [$1] }
323
324 topdecl :: { RdrBinding }
325         : srcloc 'type' simpletype '=' sigtype  
326                 { RdrHsDecl (TyClD (TySynonym (fst $3) (snd $3) $5 $1)) }
327
328         | srcloc 'data' ctype '=' constrs deriving
329                 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
330                    returnP (RdrHsDecl (TyClD
331                       (TyData DataType cs c ts (reverse $5) (length $5) $6
332                         NoDataPragmas $1))) }
333
334         | srcloc 'newtype' ctype '=' newconstr deriving
335                 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
336                    returnP (RdrHsDecl (TyClD
337                       (TyData NewType cs c ts [$5] 1 $6
338                         NoDataPragmas $1))) }
339
340         | srcloc 'class' ctype fds where
341                 {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
342                    let 
343                         (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5) 
344                    in
345                    returnP (RdrHsDecl (TyClD
346                       (mkClassDecl cs c ts $4 sigs binds 
347                         NoClassPragmas $1))) }
348
349         | srcloc 'instance' inst_type where
350                 { let (binds,sigs) 
351                         = cvMonoBindsAndSigs cvInstDeclSig 
352                                 (groupBindings $4)
353                   in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
354
355         | srcloc 'default' '(' types0 ')'
356                 { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
357
358         | srcloc 'foreign' 'import' callconv ext_name 
359           unsafe_flag varid_no_unsafe '::' sigtype
360                 { RdrHsDecl (ForD (ForeignDecl $7 (FoImport $6) $9 (mkExtName $5 $7) $4 $1)) }
361
362         | srcloc 'foreign' 'export' callconv ext_name varid '::' sigtype
363                 { RdrHsDecl (ForD (ForeignDecl $6 FoExport $8 (mkExtName $5 $6) $4 $1)) }
364
365         | srcloc 'foreign' 'label' ext_name varid '::' sigtype
366                 { RdrHsDecl (ForD (ForeignDecl $5 FoLabel $7 (mkExtName $4 $5)
367                                         defaultCallConv $1)) }
368
369         | '{-# DEPRECATED' deprecations '#-}'           { $2 }
370         | '{-# RULES' rules '#-}'                       { $2 }
371         | decl                                          { $1 }
372
373 decls   :: { [RdrBinding] }
374         : decls ';' decl                { $3 : $1 }
375         | decls ';'                     { $1 }
376         | decl                          { [$1] }
377         | {- empty -}                   { [] }
378
379 decl    :: { RdrBinding }
380         : fixdecl                       { $1 }
381         | valdef                        { $1 }
382         | '{-# INLINE'   srcloc opt_phase qvar '#-}'    { RdrSig (InlineSig $4 $3 $2) }
383         | '{-# NOINLINE' srcloc opt_phase qvar '#-}'    { RdrSig (NoInlineSig $4 $3 $2) }
384         | '{-# SPECIALISE' srcloc qvar '::' sigtypes '#-}'
385                 { foldr1 RdrAndBindings 
386                     (map (\t -> RdrSig (SpecSig $3 t $2)) $5) }
387         | '{-# SPECIALISE' srcloc 'instance' inst_type '#-}'
388                 { RdrSig (SpecInstSig $4 $2) }
389
390 opt_phase :: { Maybe Int }
391           : INTEGER                     { Just (fromInteger $1) }
392           | {- empty -}                 { Nothing }
393
394 wherebinds :: { RdrNameHsBinds }
395         : where                 { cvBinds cvValSig (groupBindings $1) }
396
397 where   :: { [RdrBinding] }
398         : 'where' decllist              { $2 }
399         | {- empty -}                   { [] }
400
401 declbinds :: { RdrNameHsBinds }
402         : decllist                      { cvBinds cvValSig (groupBindings $1) }
403
404 decllist :: { [RdrBinding] }
405         : '{'            decls '}'      { $2 }
406         |     layout_on  decls close    { $2 }
407
408 fixdecl :: { RdrBinding }
409         : srcloc infix prec ops         { foldr1 RdrAndBindings
410                                             [ RdrSig (FixSig (FixitySig n 
411                                                             (Fixity $3 $2) $1))
412                                             | n <- $4 ] }
413
414 -----------------------------------------------------------------------------
415 -- Transformation Rules
416
417 rules   :: { RdrBinding }
418         :  rules ';' rule                       { $1 `RdrAndBindings` $3 }
419         |  rules ';'                            { $1 }
420         |  rule                                 { $1 }
421         |  {- empty -}                          { RdrNullBind }
422
423 rule    :: { RdrBinding }
424         : STRING rule_forall fexp '=' srcloc exp
425              { RdrHsDecl (RuleD (HsRule $1 [] $2 $3 $6 $5)) }
426
427 rule_forall :: { [RdrNameRuleBndr] }
428         : 'forall' rule_var_list '.'            { $2 }
429         | {- empty -}                           { [] }
430
431 rule_var_list :: { [RdrNameRuleBndr] }
432         : rule_var                              { [$1] }
433         | rule_var rule_var_list                { $1 : $2 }
434
435 rule_var :: { RdrNameRuleBndr }
436         : varid                                 { RuleBndr $1 }
437         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
438
439 -----------------------------------------------------------------------------
440 -- Deprecations
441
442 deprecations :: { RdrBinding }
443         : deprecations ';' deprecation          { $1 `RdrAndBindings` $3 }
444         | deprecations ';'                      { $1 }
445         | deprecation                           { $1 }
446         | {- empty -}                           { RdrNullBind }
447
448 -- SUP: TEMPORARY HACK, not checking for `module Foo'
449 deprecation :: { RdrBinding }
450         : srcloc exportlist STRING
451                 { foldr RdrAndBindings RdrNullBind 
452                         [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
453
454 -----------------------------------------------------------------------------
455 -- Foreign import/export
456
457 callconv :: { Int }
458         : 'stdcall'             { stdCallConv }
459         | 'ccall'               { cCallConv }
460         | {- empty -}           { defaultCallConv }
461
462 unsafe_flag :: { Bool }
463         : 'unsafe'              { True }
464         | {- empty -}           { False }
465
466 ext_name :: { Maybe ExtName }
467         : 'dynamic'             { Just Dynamic }
468         | STRING                { Just (ExtName $1 Nothing)   }
469         | STRING STRING         { Just (ExtName $2 (Just $1)) }
470         | {- empty -}           { Nothing }
471
472
473 -----------------------------------------------------------------------------
474 -- Type signatures
475
476 opt_sig :: { Maybe RdrNameHsType }
477         : {- empty -}                   { Nothing }
478         | '::' sigtype                  { Just $2 }
479
480 opt_asig :: { Maybe RdrNameHsType }
481         : {- empty -}                   { Nothing }
482         | '::' atype                    { Just $2 }
483
484 sigtypes :: { [RdrNameHsType] }
485         : sigtype                       { [ $1 ] }
486         | sigtypes ',' sigtype          { $3 : $1 }
487
488 sigtype :: { RdrNameHsType }
489         : ctype                         { mkHsForAllTy Nothing [] $1 }
490
491 sig_vars :: { [RdrName] }
492          : sig_vars ',' var             { $3 : $1 }
493          | var                          { [ $1 ] }
494
495 -----------------------------------------------------------------------------
496 -- Types
497
498 -- A ctype is a for-all type
499 ctype   :: { RdrNameHsType }
500         : 'forall' tyvars '.' ctype     { mkHsForAllTy (Just $2) [] $4 }
501         | context type                  { mkHsForAllTy Nothing   $1 $2 }
502                 -- A type of form (context => type) is an *implicit* HsForAllTy
503         | type                          { $1 }
504
505 type :: { RdrNameHsType }
506         : btype '->' type               { HsFunTy $1 $3 }
507         | ipvar '::' type               { mkHsIParamTy $1 $3 }
508         | btype                         { $1 }
509
510 btype :: { RdrNameHsType }
511         : btype atype                   { HsAppTy $1 $2 }
512         | atype                         { $1 }
513
514 atype :: { RdrNameHsType }
515         : gtycon                        { HsTyVar $1 }
516         | tyvar                         { HsTyVar $1 }
517         | '(' type ',' types ')'        { HsTupleTy (mkHsTupCon tcName Boxed  ($2:$4)) ($2 : reverse $4) }
518         | '(#' types '#)'               { HsTupleTy (mkHsTupCon tcName Unboxed     $2) (reverse $2)      }
519         | '[' type ']'                  { HsListTy $2 }
520         | '(' ctype ')'                 { $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 [] [] (VanillaCon [Unbanged $3]) $1 }
586         | srcloc conid '{' var '::' type '}'
587                                 { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $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         | '(' consym ')'                { ($2,[]) }
597
598 satype :: { RdrNameBangType }
599         : atype                         { Unbanged $1 }
600         | '!' atype                     { Banged   $2 }
601
602 sbtype :: { RdrNameBangType }
603         : btype                         { Unbanged $1 }
604         | '!' atype                     { Banged   $2 }
605
606 fielddecls :: { [([RdrName],RdrNameBangType)] }
607         : fielddecls ',' fielddecl      { $3 : $1 }
608         | fielddecl                     { [$1] }
609
610 fielddecl :: { ([RdrName],RdrNameBangType) }
611         : sig_vars '::' stype           { (reverse $1, $3) }
612
613 stype :: { RdrNameBangType }
614         : ctype                         { Unbanged $1 } 
615         | '!' atype                     { Banged   $2 }
616
617 deriving :: { Maybe [RdrName] }
618         : {- empty -}                   { Nothing }
619         | 'deriving' qtycls             { Just [$2] }
620         | 'deriving' '('          ')'   { Just [] }
621         | 'deriving' '(' dclasses ')'   { Just (reverse $3) }
622
623 dclasses :: { [RdrName] }
624         : dclasses ',' qtycls           { $3 : $1 }
625         | qtycls                        { [$1] }
626
627 -----------------------------------------------------------------------------
628 -- Value definitions
629
630 {- There's an awkward overlap with a type signature.  Consider
631         f :: Int -> Int = ...rhs...
632    Then we can't tell whether it's a type signature or a value
633    definition with a result signature until we see the '='.
634    So we have to inline enough to postpone reductions until we know.
635 -}
636
637 {-
638   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
639   instead of qvar, we get another shift/reduce-conflict. Consider the
640   following programs:
641   
642      { (^^) :: Int->Int ; }          Type signature; only var allowed
643
644      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
645                                      qvar allowed (because of instance decls)
646   
647   We can't tell whether to reduce var to qvar until after we've read the signatures.
648 -}
649
650 valdef :: { RdrBinding }
651         : infixexp srcloc opt_sig rhs           {% checkValDef $1 $3 $4 $2 }
652         | infixexp srcloc '::' sigtype          {% checkValSig $1 $4 $2 }
653         | var ',' sig_vars srcloc '::' sigtype  { foldr1 RdrAndBindings 
654                                                          [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
655                                                 }
656
657 rhs     :: { RdrNameGRHSs }
658         : '=' srcloc exp wherebinds     { GRHSs (unguardedRHS $3 $2) 
659                                                                 $4 Nothing}
660         | gdrhs wherebinds              { GRHSs (reverse $1) $2 Nothing }
661
662 gdrhs :: { [RdrNameGRHS] }
663         : gdrhs gdrh                    { $2 : $1 }
664         | gdrh                          { [$1] }
665
666 gdrh :: { RdrNameGRHS }
667         : '|' srcloc quals '=' exp      { GRHS (reverse (ExprStmt $5 $2 : $3)) $2 }
668
669 -----------------------------------------------------------------------------
670 -- Expressions
671
672 exp   :: { RdrNameHsExpr }
673         : infixexp '::' sigtype         { ExprWithTySig $1 $3 }
674         | infixexp 'with' dbinding      { HsWith $1 $3 }
675         | infixexp                      { $1 }
676
677 infixexp :: { RdrNameHsExpr }
678         : exp10                         { $1 }
679         | infixexp qop exp10            { OpApp $1 $2 (panic "fixity") $3 }
680
681 exp10 :: { RdrNameHsExpr }
682         : '\\' aexp aexps opt_asig '->' srcloc exp      
683                         {% checkPatterns ($2 : reverse $3) `thenP` \ ps -> 
684                            returnP (HsLam (Match [] ps $4 
685                                             (GRHSs (unguardedRHS $7 $6) 
686                                                    EmptyBinds Nothing))) }
687         | 'let' declbinds 'in' exp              { HsLet $2 $4 }
688         | 'if' srcloc exp 'then' exp 'else' exp { HsIf $3 $5 $7 $2 }
689         | 'case' srcloc exp 'of' altslist       { HsCase $3 $5 $2 }
690         | '-' fexp                              { mkHsNegApp $2 }
691         | srcloc 'do' stmtlist                  { HsDo DoStmt $3 $1 }
692
693         | '_ccall_'    ccallid aexps0           { HsCCall $2 $3 False False cbot }
694         | '_ccall_GC_' ccallid aexps0           { HsCCall $2 $3 True  False cbot }
695         | '_casm_'     CLITLIT aexps0           { HsCCall $2 $3 False True  cbot }
696         | '_casm_GC_'  CLITLIT aexps0           { HsCCall $2 $3 True  True  cbot }
697
698         | '_scc_' STRING exp                    { if opt_SccProfilingOn
699                                                         then HsSCC $2 $3
700                                                         else HsPar $3 }
701
702         | fexp                                  { $1 }
703
704 ccallid :: { FAST_STRING }
705         :  VARID                                { $1 }
706         |  CONID                                { $1 }
707
708 fexp    :: { RdrNameHsExpr }
709         : fexp aexp                             { HsApp $1 $2 }
710         | aexp                                  { $1 }
711
712 aexps0  :: { [RdrNameHsExpr] }
713         : aexps                                 { reverse $1 }
714
715 aexps   :: { [RdrNameHsExpr] }
716         : aexps aexp                            { $2 : $1 }
717         | {- empty -}                           { [] }
718
719 aexp    :: { RdrNameHsExpr }
720         : aexp '{' fbinds '}'           {% mkRecConstrOrUpdate $1 (reverse $3) }
721         | aexp1                         { $1 }
722
723 aexp1   :: { RdrNameHsExpr }
724         : qvar                          { HsVar $1 }
725         | ipvar                         { HsIPVar $1 }
726         | gcon                          { HsVar $1 }
727         | literal                       { HsLit $1 }
728         | INTEGER                       { HsOverLit (mkHsIntegralLit $1) }
729         | RATIONAL                      { HsOverLit (mkHsFractionalLit $1) }
730         | '(' exp ')'                   { HsPar $2 }
731         | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) Boxed}
732         | '(#' texps '#)'               { ExplicitTuple (reverse $2)      Unboxed }
733         | '[' list ']'                  { $2 }
734         | '(' infixexp qop ')'          { SectionL $2 $3  }
735         | '(' qopm infixexp ')'         { SectionR $2 $3 }
736         | qvar '@' aexp                 { EAsPat $1 $3 }
737         | '_'                           { EWildPat }
738         | '~' aexp1                     { ELazyPat $2 }
739
740 texps :: { [RdrNameHsExpr] }
741         : texps ',' exp                 { $3 : $1 }
742         | exp                           { [$1] }
743
744 -----------------------------------------------------------------------------
745 -- List expressions
746
747 -- The rules below are little bit contorted to keep lexps left-recursive while
748 -- avoiding another shift/reduce-conflict.
749
750 list :: { RdrNameHsExpr }
751         : exp                           { ExplicitList [$1] }
752         | lexps                         { ExplicitList (reverse $1) }
753         | exp '..'                      { ArithSeqIn (From $1) }
754         | exp ',' exp '..'              { ArithSeqIn (FromThen $1 $3) }
755         | exp '..' exp                  { ArithSeqIn (FromTo $1 $3) }
756         | exp ',' exp '..' exp          { ArithSeqIn (FromThenTo $1 $3 $5) }
757         | exp srcloc '|' quals                  { HsDo ListComp (reverse 
758                                                 (ReturnStmt $1 : $4)) $2 }
759
760 lexps :: { [RdrNameHsExpr] }
761         : lexps ',' exp                 { $3 : $1 }
762         | exp ',' exp                   { [$3,$1] }
763
764 -----------------------------------------------------------------------------
765 -- List Comprehensions
766
767 quals :: { [RdrNameStmt] }
768         : quals ',' qual                { $3 : $1 }
769         | qual                          { [$1] }
770
771 qual  :: { RdrNameStmt }
772         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
773                                            returnP (BindStmt p $4 $1) }
774         | srcloc exp                    { GuardStmt $2 $1 }
775         | srcloc 'let' declbinds        { LetStmt $3 }
776
777 -----------------------------------------------------------------------------
778 -- Case alternatives
779
780 altslist :: { [RdrNameMatch] }
781         : '{'            alts '}'       { reverse $2 }
782         |     layout_on  alts  close    { reverse $2 }
783
784 alts    :: { [RdrNameMatch] }
785         : alts1                         { $1 }
786         | ';' alts                      { $2 }
787
788 alts1   :: { [RdrNameMatch] }
789         : alts1 ';' alt                 { $3 : $1 }
790         | alts1 ';'                     { $1 }
791         | alt                           { [$1] }
792
793 alt     :: { RdrNameMatch }
794         : infixexp opt_sig ralt wherebinds
795                                         {% checkPattern $1 `thenP` \p ->
796                                            returnP (Match [] [p] $2
797                                                      (GRHSs $3 $4 Nothing)) }
798
799 ralt :: { [RdrNameGRHS] }
800         : '->' srcloc exp               { [GRHS [ExprStmt $3 $2] $2] }
801         | gdpats                        { (reverse $1) }
802
803 gdpats :: { [RdrNameGRHS] }
804         : gdpats gdpat                  { $2 : $1 }
805         | gdpat                         { [$1] }
806
807 gdpat   :: { RdrNameGRHS }
808         : srcloc '|' quals '->' exp     { GRHS (reverse (ExprStmt $5 $1:$3)) $1}
809
810 -----------------------------------------------------------------------------
811 -- Statement sequences
812
813 stmtlist :: { [RdrNameStmt] }
814         : '{'                   stmts '}'       { reverse $2 }
815         |     layout_on_for_do  stmts close     { reverse $2 }
816
817 -- Stmt list should really end in an expression, but it's not
818 -- convenient to enforce this here, so we throw out erroneous
819 -- statement sequences in the renamer instead.
820
821 stmts :: { [RdrNameStmt] }
822         : ';' stmts1                    { $2 }
823         | stmts1                        { $1 }
824
825 stmts1 :: { [RdrNameStmt] }
826         : stmts1 ';' stmt               { $3 : $1 }
827         | stmts1 ';'                    { $1 }
828         | stmt                          { [$1] }
829
830 stmt  :: { RdrNameStmt }
831         : srcloc infixexp '<-' exp      {% checkPattern $2 `thenP` \p ->
832                                            returnP (BindStmt p $4 $1) }
833         | srcloc exp                    { ExprStmt $2 $1 }
834         | srcloc 'let' declbinds        { LetStmt $3 }
835
836 -----------------------------------------------------------------------------
837 -- Record Field Update/Construction
838
839 fbinds  :: { RdrNameHsRecordBinds }
840         : fbinds ',' fbind              { $3 : $1 }
841         | fbinds ','                    { $1 }
842         | fbind                         { [$1] }
843         | {- empty -}                   { [] }
844
845 fbind   :: { (RdrName, RdrNameHsExpr, Bool) }
846         : qvar '=' exp                  { ($1,$3,False) }
847
848 -----------------------------------------------------------------------------
849 -- Implicit Parameter Bindings
850
851 dbinding :: { [(RdrName, RdrNameHsExpr)] }
852         : '{' dbinds '}'                { $2 }
853         | layout_on dbinds close        { $2 }
854
855 dbinds  :: { [(RdrName, RdrNameHsExpr)] }
856         : dbinds ';' dbind              { $3 : $1 }
857         | dbinds ';'                    { $1 }
858         | dbind                         { [$1] }
859         | {- empty -}                   { [] }
860
861 dbind   :: { (RdrName, RdrNameHsExpr) }
862 dbind   : ipvar '=' exp                 { ($1, $3) }
863
864 -----------------------------------------------------------------------------
865 -- Variables, Constructors and Operators.
866
867 gtycon  :: { RdrName }
868         : qtycon                        { $1 }
869         | '(' ')'                       { unitTyCon_RDR }
870         | '(' '->' ')'                  { funTyCon_RDR }
871         | '[' ']'                       { listTyCon_RDR }
872         | '(' commas ')'                { tupleTyCon_RDR $2 }
873
874 gcon    :: { RdrName }
875         : '(' ')'               { unitCon_RDR }
876         | '[' ']'               { nilCon_RDR }
877         | '(' commas ')'        { tupleCon_RDR $2 }
878         | qcon                  { $1 }
879
880 var     :: { RdrName }
881         : varid                 { $1 }
882         | '(' varsym ')'        { $2 }
883
884 qvar    :: { RdrName }
885         : qvarid                { $1 }
886         | '(' varsym ')'        { $2 }
887         | '(' qvarsym1 ')'      { $2 }
888 -- We've inlined qvarsym here so that the decision about
889 -- whether it's a qvar or a var can be postponed until
890 -- *after* we see the close paren.
891
892 ipvar   :: { RdrName }
893         : IPVARID               { (mkSrcUnqual ipName (tailFS $1)) }
894
895 con     :: { RdrName }
896         : conid                 { $1 }
897         | '(' consym ')'        { $2 }
898
899 qcon    :: { RdrName }
900         : qconid                { $1 }
901         | '(' qconsym ')'       { $2 }
902
903 varop   :: { RdrName }
904         : varsym                { $1 }
905         | '`' varid '`'         { $2 }
906
907 qvarop :: { RdrName }
908         : qvarsym               { $1 }
909         | '`' qvarid '`'        { $2 }
910
911 qvaropm :: { RdrName }
912         : qvarsym_no_minus      { $1 }
913         | '`' qvarid '`'        { $2 }
914
915 conop :: { RdrName }
916         : consym                { $1 }  
917         | '`' conid '`'         { $2 }
918
919 qconop :: { RdrName }
920         : qconsym               { $1 }
921         | '`' qconid '`'        { $2 }
922
923 -----------------------------------------------------------------------------
924 -- Any operator
925
926 op      :: { RdrName }   -- used in infix decls
927         : varop                 { $1 }
928         | conop                 { $1 }
929
930 qop     :: { RdrNameHsExpr }   -- used in sections
931         : qvarop                { HsVar $1 }
932         | qconop                { HsVar $1 }
933
934 qopm    :: { RdrNameHsExpr }   -- used in sections
935         : qvaropm               { HsVar $1 }
936         | qconop                { HsVar $1 }
937
938 -----------------------------------------------------------------------------
939 -- VarIds
940
941 qvarid :: { RdrName }
942         : varid                 { $1 }
943         | QVARID                { mkSrcQual varName $1 }
944
945 varid :: { RdrName }
946         : varid_no_unsafe       { $1 }
947         | 'unsafe'              { mkSrcUnqual varName SLIT("unsafe") }
948
949 varid_no_unsafe :: { RdrName }
950         : VARID                 { mkSrcUnqual varName $1 }
951         | special_id            { mkSrcUnqual varName $1 }
952         | 'forall'              { mkSrcUnqual varName SLIT("forall") }
953
954 tyvar   :: { RdrName }
955         : VARID                 { mkSrcUnqual tvName $1 }
956         | special_id            { mkSrcUnqual tvName $1 }
957         | 'unsafe'              { mkSrcUnqual tvName SLIT("unsafe") }
958
959 -- These special_ids are treated as keywords in various places, 
960 -- but as ordinary ids elsewhere.   A special_id collects all thsee
961 -- except 'unsafe' and 'forall' whose treatment differs depending on context
962 special_id :: { UserFS }
963 special_id
964         : 'as'                  { SLIT("as") }
965         | 'qualified'           { SLIT("qualified") }
966         | 'hiding'              { SLIT("hiding") }
967         | 'export'              { SLIT("export") }
968         | 'label'               { SLIT("label")  }
969         | 'dynamic'             { SLIT("dynamic") }
970         | 'stdcall'             { SLIT("stdcall") }
971         | 'ccall'               { SLIT("ccall") }
972
973 -----------------------------------------------------------------------------
974 -- ConIds
975
976 qconid :: { RdrName }
977         : conid                 { $1 }
978         | QCONID                { mkSrcQual dataName $1 }
979
980 conid   :: { RdrName }
981         : CONID                 { mkSrcUnqual dataName $1 }
982
983 -----------------------------------------------------------------------------
984 -- ConSyms
985
986 qconsym :: { RdrName }
987         : consym                { $1 }
988         | QCONSYM               { mkSrcQual dataName $1 }
989
990 consym :: { RdrName }
991         : CONSYM                { mkSrcUnqual dataName $1 }
992
993 -----------------------------------------------------------------------------
994 -- VarSyms
995
996 qvarsym :: { RdrName }
997         : varsym                { $1 }
998         | qvarsym1              { $1 }
999
1000 qvarsym_no_minus :: { RdrName }
1001         : varsym_no_minus       { $1 }
1002         | qvarsym1              { $1 }
1003
1004 qvarsym1 :: { RdrName }
1005 qvarsym1 : QVARSYM              { mkSrcQual varName $1 }
1006
1007 varsym :: { RdrName }
1008         : varsym_no_minus       { $1 }
1009         | '-'                   { mkSrcUnqual varName SLIT("-") }
1010
1011 varsym_no_minus :: { RdrName } -- varsym not including '-'
1012         : VARSYM                { mkSrcUnqual varName $1 }
1013         | special_sym           { mkSrcUnqual varName $1 }
1014
1015
1016 -- See comments with special_id
1017 special_sym :: { UserFS }
1018 special_sym : '!'       { SLIT("!") }
1019             | '.'       { SLIT(".") }
1020
1021 -----------------------------------------------------------------------------
1022 -- Literals
1023
1024 literal :: { HsLit }
1025         : CHAR                  { HsChar       $1 }
1026         | STRING                { HsString     $1 }
1027         | PRIMINTEGER           { HsIntPrim    $1 }
1028         | PRIMCHAR              { HsCharPrim   $1 }
1029         | PRIMSTRING            { HsStringPrim $1 }
1030         | PRIMFLOAT             { HsFloatPrim  $1 }
1031         | PRIMDOUBLE            { HsDoublePrim $1 }
1032         | CLITLIT               { HsLitLit     $1 (error "Parser.y: CLITLIT") }
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                { mkSrcQual tcClsName $1 }
1058
1059 qtycls  :: { RdrName }
1060         : qtycon                { $1 }
1061
1062 commas :: { Int }
1063         : commas ','                    { $1 + 1 }
1064         | ','                           { 2 }
1065
1066 -----------------------------------------------------------------------------
1067
1068 {
1069 happyError :: P a
1070 happyError buf PState{ loc = loc } = PFailed (srcParseErr buf loc)
1071 }