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