18565a95478b7f59d1f6d7fe9701ed8c722c5729
[ghc-hetmet.git] / compiler / parser / Parser.y.pp
1 --                                                              -*-haskell-*-
2 -- ---------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1997-2003
4 ---
5 -- The GHC grammar.
6 --
7 -- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -- ---------------------------------------------------------------------------
9
10 {
11 module Parser ( parseModule, parseStmt, parseIdentifier, parseType,
12                 parseHeader ) where
13
14 #define INCLUDE #include 
15 INCLUDE "HsVersions.h"
16
17 import HsSyn
18 import RdrHsSyn
19 import HscTypes         ( IsBootInterface, DeprecTxt )
20 import Lexer
21 import RdrName
22 import TysWiredIn       ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
23                           listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
24 import Type             ( funTyCon )
25 import ForeignCall      ( Safety(..), CExportSpec(..), CLabelString,
26                           CCallConv(..), CCallTarget(..), defaultCCallConv
27                         )
28 import OccName          ( varName, dataName, tcClsName, tvName )
29 import DataCon          ( DataCon, dataConName )
30 import SrcLoc           ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
31                           SrcSpan, combineLocs, srcLocFile, 
32                           mkSrcLoc, mkSrcSpan )
33 import Module
34 import StaticFlags      ( opt_SccProfilingOn )
35 import Type             ( Kind, mkArrowKind, liftedTypeKind, unliftedTypeKind )
36 import BasicTypes       ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
37                           Activation(..), defaultInlineSpec )
38 import OrdList
39
40 import FastString
41 import Maybes           ( orElse )
42 import Monad            ( when )
43 import Outputable
44 import GLAEXTS
45 }
46
47 {-
48 -----------------------------------------------------------------------------
49 26 July 2006
50
51 Conflicts: 37 shift/reduce
52            1 reduce/reduce
53
54 The reduce/reduce conflict is weird.  It's between tyconsym and consym, and I
55 would think the two should never occur in the same context.
56
57   -=chak
58
59 -----------------------------------------------------------------------------
60 Conflicts: 36 shift/reduce (1.25)
61
62 10 for abiguity in 'if x then y else z + 1'             [State 178]
63         (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
64         10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
65
66 1 for ambiguity in 'if x then y else z :: T'            [State 178]
67         (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
68
69 4 for ambiguity in 'if x then y else z -< e'            [State 178]
70         (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
71         There are four such operators: -<, >-, -<<, >>-
72
73
74 2 for ambiguity in 'case v of { x :: T -> T ... } '     [States 11, 253]
75         Which of these two is intended?
76           case v of
77             (x::T) -> T         -- Rhs is T
78     or
79           case v of
80             (x::T -> T) -> ..   -- Rhs is ...
81
82 10 for ambiguity in 'e :: a `b` c'.  Does this mean     [States 11, 253]
83         (e::a) `b` c, or 
84         (e :: (a `b` c))
85     As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
86     Same duplication between states 11 and 253 as the previous case
87
88 1 for ambiguity in 'let ?x ...'                         [State 329]
89         the parser can't tell whether the ?x is the lhs of a normal binding or
90         an implicit binding.  Fortunately resolving as shift gives it the only
91         sensible meaning, namely the lhs of an implicit binding.
92
93 1 for ambiguity in '{-# RULES "name" [ ... #-}          [State 382]
94         we don't know whether the '[' starts the activation or not: it
95         might be the start of the declaration with the activation being
96         empty.  --SDM 1/4/2002
97
98 1 for ambiguity in '{-# RULES "name" forall = ... #-}'  [State 474]
99         since 'forall' is a valid variable name, we don't know whether
100         to treat a forall on the input as the beginning of a quantifier
101         or the beginning of the rule itself.  Resolving to shift means
102         it's always treated as a quantifier, hence the above is disallowed.
103         This saves explicitly defining a grammar for the rule lhs that
104         doesn't include 'forall'.
105
106 -- ---------------------------------------------------------------------------
107 -- Adding location info
108
109 This is done in a stylised way using the three macros below, L0, L1
110 and LL.  Each of these macros can be thought of as having type
111
112    L0, L1, LL :: a -> Located a
113
114 They each add a SrcSpan to their argument.
115
116    L0   adds 'noSrcSpan', used for empty productions
117      -- This doesn't seem to work anymore -=chak
118
119    L1   for a production with a single token on the lhs.  Grabs the SrcSpan
120         from that token.
121
122    LL   for a production with >1 token on the lhs.  Makes up a SrcSpan from
123         the first and last tokens.
124
125 These suffice for the majority of cases.  However, we must be
126 especially careful with empty productions: LL won't work if the first
127 or last token on the lhs can represent an empty span.  In these cases,
128 we have to calculate the span using more of the tokens from the lhs, eg.
129
130         | 'newtype' tycl_hdr '=' newconstr deriving
131                 { L (comb3 $1 $4 $5)
132                     (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
133
134 We provide comb3 and comb4 functions which are useful in such cases.
135
136 Be careful: there's no checking that you actually got this right, the
137 only symptom will be that the SrcSpans of your syntax will be
138 incorrect.
139
140 /*
141  * We must expand these macros *before* running Happy, which is why this file is
142  * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
143  */
144 #define L0   L noSrcSpan
145 #define L1   sL (getLoc $1)
146 #define LL   sL (comb2 $1 $>)
147
148 -- -----------------------------------------------------------------------------
149
150 -}
151
152 %token
153  '_'            { L _ ITunderscore }            -- Haskell keywords
154  'as'           { L _ ITas }
155  'case'         { L _ ITcase }          
156  'class'        { L _ ITclass } 
157  'data'         { L _ ITdata } 
158  'default'      { L _ ITdefault }
159  'deriving'     { L _ ITderiving }
160  'do'           { L _ ITdo }
161  'else'         { L _ ITelse }
162  'for'          { L _ ITfor }
163  'hiding'       { L _ IThiding }
164  'if'           { L _ ITif }
165  'import'       { L _ ITimport }
166  'in'           { L _ ITin }
167  'infix'        { L _ ITinfix }
168  'infixl'       { L _ ITinfixl }
169  'infixr'       { L _ ITinfixr }
170  'instance'     { L _ ITinstance }
171  'let'          { L _ ITlet }
172  'module'       { L _ ITmodule }
173  'newtype'      { L _ ITnewtype }
174  'of'           { L _ ITof }
175  'qualified'    { L _ ITqualified }
176  'then'         { L _ ITthen }
177  'type'         { L _ ITtype }
178  'where'        { L _ ITwhere }
179  '_scc_'        { L _ ITscc }         -- ToDo: remove
180
181  'forall'       { L _ ITforall }                -- GHC extension keywords
182  'foreign'      { L _ ITforeign }
183  'export'       { L _ ITexport }
184  'label'        { L _ ITlabel } 
185  'dynamic'      { L _ ITdynamic }
186  'safe'         { L _ ITsafe }
187  'threadsafe'   { L _ ITthreadsafe }
188  'unsafe'       { L _ ITunsafe }
189  'mdo'          { L _ ITmdo }
190  'iso'          { L _ ITiso }
191  'family'       { L _ ITfamily }
192  'stdcall'      { L _ ITstdcallconv }
193  'ccall'        { L _ ITccallconv }
194  'dotnet'       { L _ ITdotnet }
195  'proc'         { L _ ITproc }          -- for arrow notation extension
196  'rec'          { L _ ITrec }           -- for arrow notation extension
197
198  '{-# INLINE'             { L _ (ITinline_prag _) }
199  '{-# SPECIALISE'         { L _ ITspec_prag }
200  '{-# SPECIALISE_INLINE'  { L _ (ITspec_inline_prag _) }
201  '{-# SOURCE'      { L _ ITsource_prag }
202  '{-# RULES'       { L _ ITrules_prag }
203  '{-# CORE'        { L _ ITcore_prag }              -- hdaume: annotated core
204  '{-# SCC'         { L _ ITscc_prag }
205  '{-# DEPRECATED'  { L _ ITdeprecated_prag }
206  '{-# UNPACK'      { L _ ITunpack_prag }
207  '#-}'             { L _ ITclose_prag }
208
209  '..'           { L _ ITdotdot }                        -- reserved symbols
210  ':'            { L _ ITcolon }
211  '::'           { L _ ITdcolon }
212  '='            { L _ ITequal }
213  '\\'           { L _ ITlam }
214  '|'            { L _ ITvbar }
215  '<-'           { L _ ITlarrow }
216  '->'           { L _ ITrarrow }
217  '@'            { L _ ITat }
218  '~'            { L _ ITtilde }
219  '=>'           { L _ ITdarrow }
220  '-'            { L _ ITminus }
221  '!'            { L _ ITbang }
222  '*'            { L _ ITstar }
223  '-<'           { L _ ITlarrowtail }            -- for arrow notation
224  '>-'           { L _ ITrarrowtail }            -- for arrow notation
225  '-<<'          { L _ ITLarrowtail }            -- for arrow notation
226  '>>-'          { L _ ITRarrowtail }            -- for arrow notation
227  '.'            { L _ ITdot }
228
229  '{'            { L _ ITocurly }                        -- special symbols
230  '}'            { L _ ITccurly }
231  '{|'           { L _ ITocurlybar }
232  '|}'           { L _ ITccurlybar }
233  vocurly        { L _ ITvocurly } -- virtual open curly (from layout)
234  vccurly        { L _ ITvccurly } -- virtual close curly (from layout)
235  '['            { L _ ITobrack }
236  ']'            { L _ ITcbrack }
237  '[:'           { L _ ITopabrack }
238  ':]'           { L _ ITcpabrack }
239  '('            { L _ IToparen }
240  ')'            { L _ ITcparen }
241  '(#'           { L _ IToubxparen }
242  '#)'           { L _ ITcubxparen }
243  '(|'           { L _ IToparenbar }
244  '|)'           { L _ ITcparenbar }
245  ';'            { L _ ITsemi }
246  ','            { L _ ITcomma }
247  '`'            { L _ ITbackquote }
248
249  VARID          { L _ (ITvarid    _) }          -- identifiers
250  CONID          { L _ (ITconid    _) }
251  VARSYM         { L _ (ITvarsym   _) }
252  CONSYM         { L _ (ITconsym   _) }
253  QVARID         { L _ (ITqvarid   _) }
254  QCONID         { L _ (ITqconid   _) }
255  QVARSYM        { L _ (ITqvarsym  _) }
256  QCONSYM        { L _ (ITqconsym  _) }
257
258  IPDUPVARID     { L _ (ITdupipvarid   _) }              -- GHC extension
259
260  CHAR           { L _ (ITchar     _) }
261  STRING         { L _ (ITstring   _) }
262  INTEGER        { L _ (ITinteger  _) }
263  RATIONAL       { L _ (ITrational _) }
264                     
265  PRIMCHAR       { L _ (ITprimchar   _) }
266  PRIMSTRING     { L _ (ITprimstring _) }
267  PRIMINTEGER    { L _ (ITprimint    _) }
268  PRIMFLOAT      { L _ (ITprimfloat  _) }
269  PRIMDOUBLE     { L _ (ITprimdouble _) }
270                     
271 -- Template Haskell 
272 '[|'            { L _ ITopenExpQuote  }       
273 '[p|'           { L _ ITopenPatQuote  }      
274 '[t|'           { L _ ITopenTypQuote  }      
275 '[d|'           { L _ ITopenDecQuote  }      
276 '|]'            { L _ ITcloseQuote    }
277 TH_ID_SPLICE    { L _ (ITidEscape _)  }     -- $x
278 '$('            { L _ ITparenEscape   }     -- $( exp )
279 TH_VAR_QUOTE    { L _ ITvarQuote      }     -- 'x
280 TH_TY_QUOTE     { L _ ITtyQuote       }      -- ''T
281
282 %monad { P } { >>= } { return }
283 %lexer { lexer } { L _ ITeof }
284 %name parseModule module
285 %name parseStmt   maybe_stmt
286 %name parseIdentifier  identifier
287 %name parseType ctype
288 %partial parseHeader header
289 %tokentype { (Located Token) }
290 %%
291
292 -----------------------------------------------------------------------------
293 -- Identifiers; one of the entry points
294 identifier :: { Located RdrName }
295         : qvar                          { $1 }
296         | qcon                          { $1 }
297         | qvarop                        { $1 }
298         | qconop                        { $1 }
299
300 -----------------------------------------------------------------------------
301 -- Module Header
302
303 -- The place for module deprecation is really too restrictive, but if it
304 -- was allowed at its natural place just before 'module', we get an ugly
305 -- s/r conflict with the second alternative. Another solution would be the
306 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
307 -- either, and DEPRECATED is only expected to be used by people who really
308 -- know what they are doing. :-)
309
310 module  :: { Located (HsModule RdrName) }
311         : 'module' modid maybemoddeprec maybeexports 'where' body 
312                 {% fileSrcSpan >>= \ loc ->
313                    return (L loc (HsModule (Just $2) $4 (fst $6) (snd $6) $3)) }
314         | missing_module_keyword top close
315                 {% fileSrcSpan >>= \ loc ->
316                    return (L loc (HsModule Nothing Nothing 
317                                 (fst $2) (snd $2) Nothing)) }
318
319 missing_module_keyword :: { () }
320         : {- empty -}                           {% pushCurrentContext }
321
322 maybemoddeprec :: { Maybe DeprecTxt }
323         : '{-# DEPRECATED' STRING '#-}'         { Just (getSTRING $2) }
324         |  {- empty -}                          { Nothing }
325
326 body    :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
327         :  '{'            top '}'               { $2 }
328         |      vocurly    top close             { $2 }
329
330 top     :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
331         : importdecls                           { (reverse $1,[]) }
332         | importdecls ';' cvtopdecls            { (reverse $1,$3) }
333         | cvtopdecls                            { ([],$1) }
334
335 cvtopdecls :: { [LHsDecl RdrName] }
336         : topdecls                              { cvTopDecls $1 }
337
338 -----------------------------------------------------------------------------
339 -- Module declaration & imports only
340
341 header  :: { Located (HsModule RdrName) }
342         : 'module' modid maybemoddeprec maybeexports 'where' header_body
343                 {% fileSrcSpan >>= \ loc ->
344                    return (L loc (HsModule (Just $2) $4 $6 [] $3)) }
345         | missing_module_keyword importdecls
346                 {% fileSrcSpan >>= \ loc ->
347                    return (L loc (HsModule Nothing Nothing $2 [] Nothing)) }
348
349 header_body :: { [LImportDecl RdrName] }
350         :  '{'            importdecls           { $2 }
351         |      vocurly    importdecls           { $2 }
352
353 -----------------------------------------------------------------------------
354 -- The Export List
355
356 maybeexports :: { Maybe [LIE RdrName] }
357         :  '(' exportlist ')'                   { Just $2 }
358         |  {- empty -}                          { Nothing }
359
360 exportlist  :: { [LIE RdrName] }
361         : ','                                   { [] }
362         | exportlist1                           { $1 }
363
364 exportlist1 :: { [LIE RdrName] }
365         :  export                               { [$1] }
366         |  export ',' exportlist                { $1 : $3 }
367         |  {- empty -}                          { [] }
368
369    -- No longer allow things like [] and (,,,) to be exported
370    -- They are built in syntax, always available
371 export  :: { LIE RdrName }
372         :  qvar                         { L1 (IEVar (unLoc $1)) }
373         |  oqtycon                      { L1 (IEThingAbs (unLoc $1)) }
374         |  oqtycon '(' '..' ')'         { LL (IEThingAll (unLoc $1)) }
375         |  oqtycon '(' ')'              { LL (IEThingWith (unLoc $1) []) }
376         |  oqtycon '(' qcnames ')'      { LL (IEThingWith (unLoc $1) (reverse $3)) }
377         |  'module' modid               { LL (IEModuleContents (unLoc $2)) }
378
379 qcnames :: { [RdrName] }
380         :  qcnames ',' qcname_ext       { unLoc $3 : $1 }
381         |  qcname_ext                   { [unLoc $1]  }
382
383 qcname_ext :: { Located RdrName }       -- Variable or data constructor
384                                         -- or tagged type constructor
385         :  qcname                       { $1 }
386         |  'type' qcon                  { sL (comb2 $1 $2) 
387                                              (setRdrNameSpace (unLoc $2) 
388                                                               tcClsName)  }
389
390 -- Cannot pull into qcname_ext, as qcname is also used in expression.
391 qcname  :: { Located RdrName }  -- Variable or data constructor
392         :  qvar                         { $1 }
393         |  qcon                         { $1 }
394
395 -----------------------------------------------------------------------------
396 -- Import Declarations
397
398 -- import decls can be *empty*, or even just a string of semicolons
399 -- whereas topdecls must contain at least one topdecl.
400
401 importdecls :: { [LImportDecl RdrName] }
402         : importdecls ';' importdecl            { $3 : $1 }
403         | importdecls ';'                       { $1 }
404         | importdecl                            { [ $1 ] }
405         | {- empty -}                           { [] }
406
407 importdecl :: { LImportDecl RdrName }
408         : 'import' maybe_src optqualified modid maybeas maybeimpspec 
409                 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
410
411 maybe_src :: { IsBootInterface }
412         : '{-# SOURCE' '#-}'                    { True }
413         | {- empty -}                           { False }
414
415 optqualified :: { Bool }
416         : 'qualified'                           { True  }
417         | {- empty -}                           { False }
418
419 maybeas :: { Located (Maybe ModuleName) }
420         : 'as' modid                            { LL (Just (unLoc $2)) }
421         | {- empty -}                           { noLoc Nothing }
422
423 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
424         : impspec                               { L1 (Just (unLoc $1)) }
425         | {- empty -}                           { noLoc Nothing }
426
427 impspec :: { Located (Bool, [LIE RdrName]) }
428         :  '(' exportlist ')'                   { LL (False, $2) }
429         |  'hiding' '(' exportlist ')'          { LL (True,  $3) }
430
431 -----------------------------------------------------------------------------
432 -- Fixity Declarations
433
434 prec    :: { Int }
435         : {- empty -}           { 9 }
436         | INTEGER               {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
437
438 infix   :: { Located FixityDirection }
439         : 'infix'                               { L1 InfixN  }
440         | 'infixl'                              { L1 InfixL  }
441         | 'infixr'                              { L1 InfixR }
442
443 ops     :: { Located [Located RdrName] }
444         : ops ',' op                            { LL ($3 : unLoc $1) }
445         | op                                    { L1 [$1] }
446
447 -----------------------------------------------------------------------------
448 -- Top-Level Declarations
449
450 topdecls :: { OrdList (LHsDecl RdrName) }
451         : topdecls ';' topdecl          { $1 `appOL` $3 }
452         | topdecls ';'                  { $1 }
453         | topdecl                       { $1 }
454
455 topdecl :: { OrdList (LHsDecl RdrName) }
456         : cl_decl                       { unitOL (L1 (TyClD (unLoc $1))) }
457         | ty_decl                       { unitOL (L1 (TyClD (unLoc $1))) }
458         | 'instance' inst_type where
459                 { let (binds, sigs, ats) = cvBindsAndSigs (unLoc $3)
460                   in unitOL (L (comb3 $1 $2 $3) 
461                             (InstD (InstDecl $2 binds sigs ats))) }
462         | stand_alone_deriving                  { unitOL (LL (DerivD (unLoc $1))) }
463         | 'default' '(' comma_types0 ')'        { unitOL (LL $ DefD (DefaultDecl $3)) }
464         | 'foreign' fdecl                       { unitOL (LL (unLoc $2)) }
465         | '{-# DEPRECATED' deprecations '#-}'   { $2 }
466         | '{-# RULES' rules '#-}'               { $2 }
467         | decl                                  { unLoc $1 }
468
469         -- Template Haskell Extension
470         | '$(' exp ')'                          { unitOL (LL $ SpliceD (SpliceDecl $2)) }
471         | TH_ID_SPLICE                          { unitOL (LL $ SpliceD (SpliceDecl $
472                                                         L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
473                                                   )) }
474
475 -- Type classes
476 --
477 cl_decl :: { LTyClDecl RdrName }
478         : 'class' tycl_hdr fds where
479                 {% do { let { (binds, sigs, ats)           = 
480                                 cvBindsAndSigs (unLoc $4)
481                             ; (ctxt, tc, tvs, tparms) = unLoc $2}
482                       ; checkTyVars tparms      -- only type vars allowed
483                       ; checkKindSigs ats
484                       ; return $ L (comb4 $1 $2 $3 $4) 
485                                    (mkClassDecl (ctxt, tc, tvs) 
486                                                 (unLoc $3) sigs binds ats) } }
487
488 -- Type declarations (toplevel)
489 --
490 ty_decl :: { LTyClDecl RdrName }
491            -- ordinary type synonyms
492         : 'type' type '=' ctype
493                 -- Note ctype, not sigtype, on the right of '='
494                 -- We allow an explicit for-all but we don't insert one
495                 -- in   type Foo a = (b,b)
496                 -- Instead we just say b is out of scope
497                 --
498                 -- Note the use of type for the head; this allows
499                 -- infix type constructors to be declared 
500                 {% do { (tc, tvs, _) <- checkSynHdr $2 False
501                       ; return (L (comb2 $1 $4) 
502                                   (TySynonym tc tvs Nothing $4)) 
503                       } }
504
505            -- type family declarations
506         | 'type' 'family' type opt_kind_sig 
507                 -- Note the use of type for the head; this allows
508                 -- infix type constructors to be declared
509                 --
510                 {% do { (tc, tvs, _) <- checkSynHdr $3 False
511                       ; let kind = case unLoc $4 of
512                                      Nothing -> liftedTypeKind
513                                      Just ki -> ki
514                       ; return (L (comb3 $1 $3 $4) 
515                                   (TyFunction tc tvs False kind))
516                       } }
517
518            -- type instance declarations
519         | 'type' 'instance' type '=' ctype
520                 -- Note the use of type for the head; this allows
521                 -- infix type constructors and type patterns
522                 --
523                 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
524                       ; return (L (comb2 $1 $5) 
525                                   (TySynonym tc tvs (Just typats) $5)) 
526                       } }
527
528           -- ordinary data type or newtype declaration
529         | data_or_newtype tycl_hdr constrs deriving
530                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
531                       ; checkTyVars tparms    -- no type pattern
532                       ; return $
533                           L (comb4 $1 $2 $3 $4)
534                                    -- We need the location on tycl_hdr in case 
535                                    -- constrs and deriving are both empty
536                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing) 
537                                Nothing (reverse (unLoc $3)) (unLoc $4)) } }
538
539           -- ordinary GADT declaration
540         | data_or_newtype tycl_hdr opt_kind_sig 
541                  'where' gadt_constrlist
542                  deriving
543                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
544                       ; checkTyVars tparms    -- can have type pats
545                       ; return $
546                           L (comb4 $1 $2 $4 $5)
547                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing) 
548                               (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
549
550           -- data/newtype family
551         | data_or_newtype 'family' tycl_hdr opt_kind_sig
552                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
553                       ; checkTyVars tparms    -- no type pattern
554                       ; let kind = case unLoc $4 of
555                                      Nothing -> liftedTypeKind
556                                      Just ki -> ki
557                       ; return $
558                           L (comb3 $1 $2 $4)
559                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing) 
560                               (Just kind) [] Nothing) } }
561
562           -- data/newtype instance declaration
563         | data_or_newtype 'instance' tycl_hdr constrs deriving
564                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
565                                              -- can have type pats
566                       ; return $
567                           L (comb4 $1 $3 $4 $5)
568                                    -- We need the location on tycl_hdr in case 
569                                    -- constrs and deriving are both empty
570                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms) 
571                               Nothing (reverse (unLoc $4)) (unLoc $5)) } }
572
573           -- GADT instance declaration
574         | data_or_newtype 'instance' tycl_hdr opt_kind_sig 
575                  'where' gadt_constrlist
576                  deriving
577                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
578                                              -- can have type pats
579                       ; return $
580                           L (comb4 $1 $3 $6 $7)
581                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms) 
582                                (unLoc $4) (reverse (unLoc $6)) (unLoc $7)) } }
583
584 -- Associate type declarations
585 --
586 at_decl :: { LTyClDecl RdrName }
587            -- type family declarations
588         : 'type' type opt_kind_sig
589                 -- Note the use of type for the head; this allows
590                 -- infix type constructors to be declared
591                 --
592                 {% do { (tc, tvs, _) <- checkSynHdr $2 False
593                       ; let kind = case unLoc $3 of
594                                      Nothing -> liftedTypeKind
595                                      Just ki -> ki
596                       ; return (L (comb3 $1 $2 $3) 
597                                   (TyFunction tc tvs False kind))
598                       } }
599
600            -- type instance declarations
601         | 'type' type '=' ctype
602                 -- Note the use of type for the head; this allows
603                 -- infix type constructors and type patterns
604                 --
605                 {% do { (tc, tvs, typats) <- checkSynHdr $2 True
606                       ; return (L (comb2 $1 $4) 
607                                   (TySynonym tc tvs (Just typats) $4)) 
608                       } }
609
610           -- data/newtype family
611         | data_or_newtype tycl_hdr '::' kind
612                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
613                       ; checkTyVars tparms    -- no type pattern
614                       ; return $
615                           L (comb3 $1 $2 $4)
616                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing) 
617                               (Just (unLoc $4)) [] Nothing) } }
618
619         -- data/newtype instance declaration
620         | data_or_newtype tycl_hdr constrs deriving
621                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
622                                              -- can have type pats
623                       ; return $
624                           L (comb4 $1 $2 $3 $4)
625                                    -- We need the location on tycl_hdr in case 
626                                    -- constrs and deriving are both empty
627                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms) 
628                               Nothing (reverse (unLoc $3)) (unLoc $4)) } }
629
630         -- GADT instance declaration
631         | data_or_newtype tycl_hdr opt_kind_sig 
632                  'where' gadt_constrlist
633                  deriving
634                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
635                                              -- can have type pats
636                       ; return $
637                           L (comb4 $1 $2 $5 $6)
638                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms) 
639                              (unLoc $3) (reverse (unLoc $5)) (unLoc $6)) } }
640
641 opt_iso :: { Bool }
642         :       { False }
643         | 'iso' { True  }
644
645 data_or_newtype :: { Located NewOrData }
646         : 'data'        { L1 DataType }
647         | 'newtype'     { L1 NewType }
648
649 opt_kind_sig :: { Located (Maybe Kind) }
650         :                               { noLoc Nothing }
651         | '::' kind                     { LL (Just (unLoc $2)) }
652
653 -- tycl_hdr parses the header of a class or data type decl,
654 -- which takes the form
655 --      T a b
656 --      Eq a => T a
657 --      (Eq a, Ord b) => T a b
658 --      T Int [a]                       -- for associated types
659 -- Rather a lot of inlining here, else we get reduce/reduce errors
660 tycl_hdr :: { Located (LHsContext RdrName, 
661                        Located RdrName, 
662                        [LHsTyVarBndr RdrName],
663                        [LHsType RdrName]) }
664         : context '=>' type             {% checkTyClHdr $1         $3 >>= return.LL }
665         | type                          {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
666
667 -----------------------------------------------------------------------------
668 -- Stand-alone deriving
669
670 -- Glasgow extension: stand-alone deriving declarations
671 stand_alone_deriving :: { LDerivDecl RdrName }
672         : 'deriving' qtycon            'for' qtycon  {% do { p <- checkInstType (fmap HsTyVar $2)
673                                                            ; checkDerivDecl (LL (DerivDecl p $4)) } }
674
675         | 'deriving' '(' inst_type ')' 'for' qtycon  {% checkDerivDecl (LL (DerivDecl $3 $6)) }
676
677 -----------------------------------------------------------------------------
678 -- Nested declarations
679
680 -- Type declaration or value declaration
681 --
682 tydecl  :: { Located (OrdList (LHsDecl RdrName)) }
683 tydecl  : at_decl                       { LL (unitOL (L1 (TyClD (unLoc $1)))) }
684         | decl                          { $1 }
685
686 tydecls :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
687         : tydecls ';' tydecl            { LL (unLoc $1 `appOL` unLoc $3) }
688         | tydecls ';'                   { LL (unLoc $1) }
689         | tydecl                        { $1 }
690         | {- empty -}                   { noLoc nilOL }
691
692
693 tydecllist 
694         :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
695         : '{'            tydecls '}'    { LL (unLoc $2) }
696         |     vocurly    tydecls close  { $2 }
697
698 -- Form of the body of class and instance declarations
699 --
700 where   :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
701                                 -- No implicit parameters
702                                 -- May have type declarations
703         : 'where' tydecllist            { LL (unLoc $2) }
704         | {- empty -}                   { noLoc nilOL }
705
706 decls   :: { Located (OrdList (LHsDecl RdrName)) }      
707         : decls ';' decl                { LL (unLoc $1 `appOL` unLoc $3) }
708         | decls ';'                     { LL (unLoc $1) }
709         | decl                          { $1 }
710         | {- empty -}                   { noLoc nilOL }
711
712
713 decllist :: { Located (OrdList (LHsDecl RdrName)) }
714         : '{'            decls '}'      { LL (unLoc $2) }
715         |     vocurly    decls close    { $2 }
716
717 -- Binding groups other than those of class and instance declarations
718 --
719 binds   ::  { Located (HsLocalBinds RdrName) }          -- May have implicit parameters
720                                                 -- No type declarations
721         : decllist                      { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
722         | '{'            dbinds '}'     { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
723         |     vocurly    dbinds close   { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
724
725 wherebinds :: { Located (HsLocalBinds RdrName) }        -- May have implicit parameters
726                                                 -- No type declarations
727         : 'where' binds                 { LL (unLoc $2) }
728         | {- empty -}                   { noLoc emptyLocalBinds }
729
730
731 -----------------------------------------------------------------------------
732 -- Transformation Rules
733
734 rules   :: { OrdList (LHsDecl RdrName) }
735         :  rules ';' rule                       { $1 `snocOL` $3 }
736         |  rules ';'                            { $1 }
737         |  rule                                 { unitOL $1 }
738         |  {- empty -}                          { nilOL }
739
740 rule    :: { LHsDecl RdrName }
741         : STRING activation rule_forall infixexp '=' exp
742              { LL $ RuleD (HsRule (getSTRING $1) 
743                                   ($2 `orElse` AlwaysActive) 
744                                   $3 $4 placeHolderNames $6 placeHolderNames) }
745
746 activation :: { Maybe Activation } 
747         : {- empty -}                           { Nothing }
748         | explicit_activation                   { Just $1 }
749
750 explicit_activation :: { Activation }  -- In brackets
751         : '[' INTEGER ']'               { ActiveAfter  (fromInteger (getINTEGER $2)) }
752         | '[' '~' INTEGER ']'           { ActiveBefore (fromInteger (getINTEGER $3)) }
753
754 rule_forall :: { [RuleBndr RdrName] }
755         : 'forall' rule_var_list '.'            { $2 }
756         | {- empty -}                           { [] }
757
758 rule_var_list :: { [RuleBndr RdrName] }
759         : rule_var                              { [$1] }
760         | rule_var rule_var_list                { $1 : $2 }
761
762 rule_var :: { RuleBndr RdrName }
763         : varid                                 { RuleBndr $1 }
764         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
765
766 -----------------------------------------------------------------------------
767 -- Deprecations (c.f. rules)
768
769 deprecations :: { OrdList (LHsDecl RdrName) }
770         : deprecations ';' deprecation          { $1 `appOL` $3 }
771         | deprecations ';'                      { $1 }
772         | deprecation                           { $1 }
773         | {- empty -}                           { nilOL }
774
775 -- SUP: TEMPORARY HACK, not checking for `module Foo'
776 deprecation :: { OrdList (LHsDecl RdrName) }
777         : depreclist STRING
778                 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2)) 
779                        | n <- unLoc $1 ] }
780
781
782 -----------------------------------------------------------------------------
783 -- Foreign import and export declarations
784
785 fdecl :: { LHsDecl RdrName }
786 fdecl : 'import' callconv safety fspec
787                 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
788       | 'import' callconv        fspec          
789                 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
790                         return (LL d) } }
791       | 'export' callconv fspec
792                 {% mkExport $2 (unLoc $3) >>= return.LL }
793
794 callconv :: { CallConv }
795           : 'stdcall'                   { CCall  StdCallConv }
796           | 'ccall'                     { CCall  CCallConv   }
797           | 'dotnet'                    { DNCall             }
798
799 safety :: { Safety }
800         : 'unsafe'                      { PlayRisky }
801         | 'safe'                        { PlaySafe  False }
802         | 'threadsafe'                  { PlaySafe  True }
803
804 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
805        : STRING var '::' sigtype      { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
806        |        var '::' sigtype      { LL (noLoc nilFS, $1, $3) }
807          -- if the entity string is missing, it defaults to the empty string;
808          -- the meaning of an empty entity string depends on the calling
809          -- convention
810
811 -----------------------------------------------------------------------------
812 -- Type signatures
813
814 opt_sig :: { Maybe (LHsType RdrName) }
815         : {- empty -}                   { Nothing }
816         | '::' sigtype                  { Just $2 }
817
818 opt_asig :: { Maybe (LHsType RdrName) }
819         : {- empty -}                   { Nothing }
820         | '::' atype                    { Just $2 }
821
822 sigtypes1 :: { [LHsType RdrName] }
823         : sigtype                       { [ $1 ] }
824         | sigtype ',' sigtypes1         { $1 : $3 }
825
826 sigtype :: { LHsType RdrName }
827         : ctype                         { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
828         -- Wrap an Implicit forall if there isn't one there already
829
830 sig_vars :: { Located [Located RdrName] }
831          : sig_vars ',' var             { LL ($3 : unLoc $1) }
832          | var                          { L1 [$1] }
833
834 -----------------------------------------------------------------------------
835 -- Types
836
837 strict_mark :: { Located HsBang }
838         : '!'                           { L1 HsStrict }
839         | '{-# UNPACK' '#-}' '!'        { LL HsUnbox }
840
841 -- A ctype is a for-all type
842 ctype   :: { LHsType RdrName }
843         : 'forall' tv_bndrs '.' ctype   { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
844         | context '=>' type             { LL $ mkImplicitHsForAllTy   $1 $3 }
845         -- A type of form (context => type) is an *implicit* HsForAllTy
846         | type                          { $1 }
847
848 -- We parse a context as a btype so that we don't get reduce/reduce
849 -- errors in ctype.  The basic problem is that
850 --      (Eq a, Ord a)
851 -- looks so much like a tuple type.  We can't tell until we find the =>
852 context :: { LHsContext RdrName }
853         : btype                         {% checkContext $1 }
854
855 type :: { LHsType RdrName }
856         : ipvar '::' gentype            { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
857         | gentype                       { $1 }
858
859 gentype :: { LHsType RdrName }
860         : btype                         { $1 }
861         | btype qtyconop gentype        { LL $ HsOpTy $1 $2 $3 }
862         | btype tyvarop  gentype        { LL $ HsOpTy $1 $2 $3 }
863         | btype '->' ctype              { LL $ HsFunTy $1 $3 }
864
865 btype :: { LHsType RdrName }
866         : btype atype                   { LL $ HsAppTy $1 $2 }
867         | atype                         { $1 }
868
869 atype :: { LHsType RdrName }
870         : gtycon                        { L1 (HsTyVar (unLoc $1)) }
871         | tyvar                         { L1 (HsTyVar (unLoc $1)) }
872         | strict_mark atype             { LL (HsBangTy (unLoc $1) $2) }
873         | '(' ctype ',' comma_types1 ')'  { LL $ HsTupleTy Boxed  ($2:$4) }
874         | '(#' comma_types1 '#)'        { LL $ HsTupleTy Unboxed $2     }
875         | '[' ctype ']'                 { LL $ HsListTy  $2 }
876         | '[:' ctype ':]'               { LL $ HsPArrTy  $2 }
877         | '(' ctype ')'                 { LL $ HsParTy   $2 }
878         | '(' ctype '::' kind ')'       { LL $ HsKindSig $2 (unLoc $4) }
879 -- Generics
880         | INTEGER                       { L1 (HsNumTy (getINTEGER $1)) }
881
882 -- An inst_type is what occurs in the head of an instance decl
883 --      e.g.  (Foo a, Gaz b) => Wibble a b
884 -- It's kept as a single type, with a MonoDictTy at the right
885 -- hand corner, for convenience.
886 inst_type :: { LHsType RdrName }
887         : sigtype                       {% checkInstType $1 }
888
889 inst_types1 :: { [LHsType RdrName] }
890         : inst_type                     { [$1] }
891         | inst_type ',' inst_types1     { $1 : $3 }
892
893 comma_types0  :: { [LHsType RdrName] }
894         : comma_types1                  { $1 }
895         | {- empty -}                   { [] }
896
897 comma_types1    :: { [LHsType RdrName] }
898         : ctype                         { [$1] }
899         | ctype  ',' comma_types1       { $1 : $3 }
900
901 tv_bndrs :: { [LHsTyVarBndr RdrName] }
902          : tv_bndr tv_bndrs             { $1 : $2 }
903          | {- empty -}                  { [] }
904
905 tv_bndr :: { LHsTyVarBndr RdrName }
906         : tyvar                         { L1 (UserTyVar (unLoc $1)) }
907         | '(' tyvar '::' kind ')'       { LL (KindedTyVar (unLoc $2) 
908                                                           (unLoc $4)) }
909
910 fds :: { Located [Located ([RdrName], [RdrName])] }
911         : {- empty -}                   { noLoc [] }
912         | '|' fds1                      { LL (reverse (unLoc $2)) }
913
914 fds1 :: { Located [Located ([RdrName], [RdrName])] }
915         : fds1 ',' fd                   { LL ($3 : unLoc $1) }
916         | fd                            { L1 [$1] }
917
918 fd :: { Located ([RdrName], [RdrName]) }
919         : varids0 '->' varids0          { L (comb3 $1 $2 $3)
920                                            (reverse (unLoc $1), reverse (unLoc $3)) }
921
922 varids0 :: { Located [RdrName] }
923         : {- empty -}                   { noLoc [] }
924         | varids0 tyvar                 { LL (unLoc $2 : unLoc $1) }
925
926 -----------------------------------------------------------------------------
927 -- Kinds
928
929 kind    :: { Located Kind }
930         : akind                 { $1 }
931         | akind '->' kind       { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
932
933 akind   :: { Located Kind }
934         : '*'                   { L1 liftedTypeKind }
935         | '!'                   { L1 unliftedTypeKind }
936         | '(' kind ')'          { LL (unLoc $2) }
937
938
939 -----------------------------------------------------------------------------
940 -- Datatype declarations
941
942 gadt_constrlist :: { Located [LConDecl RdrName] }
943         : '{'            gadt_constrs '}'       { LL (unLoc $2) }
944         |     vocurly    gadt_constrs close     { $2 }
945
946 gadt_constrs :: { Located [LConDecl RdrName] }
947         : gadt_constrs ';' gadt_constr  { LL ($3 : unLoc $1) }
948         | gadt_constrs ';'              { $1 }
949         | gadt_constr                   { L1 [$1] } 
950
951 -- We allow the following forms:
952 --      C :: Eq a => a -> T a
953 --      C :: forall a. Eq a => !a -> T a
954 --      D { x,y :: a } :: T a
955 --      forall a. Eq a => D { x,y :: a } :: T a
956
957 gadt_constr :: { LConDecl RdrName }
958         : con '::' sigtype
959               { LL (mkGadtDecl $1 $3) } 
960         -- Syntax: Maybe merge the record stuff with the single-case above?
961         --         (to kill the mostly harmless reduce/reduce error)
962         -- XXX revisit audreyt
963         | constr_stuff_record '::' sigtype
964                 { let (con,details) = unLoc $1 in 
965                   LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
966 {-
967         | forall context '=>' constr_stuff_record '::' sigtype
968                 { let (con,details) = unLoc $4 in 
969                   LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
970         | forall constr_stuff_record '::' sigtype
971                 { let (con,details) = unLoc $2 in 
972                   LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
973 -}
974
975
976 constrs :: { Located [LConDecl RdrName] }
977         : {- empty; a GHC extension -}  { noLoc [] }
978         | '=' constrs1                  { LL (unLoc $2) }
979
980 constrs1 :: { Located [LConDecl RdrName] }
981         : constrs1 '|' constr           { LL ($3 : unLoc $1) }
982         | constr                        { L1 [$1] }
983
984 constr :: { LConDecl RdrName }
985         : forall context '=>' constr_stuff      
986                 { let (con,details) = unLoc $4 in 
987                   LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
988         | forall constr_stuff
989                 { let (con,details) = unLoc $2 in 
990                   LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
991
992 forall :: { Located [LHsTyVarBndr RdrName] }
993         : 'forall' tv_bndrs '.'         { LL $2 }
994         | {- empty -}                   { noLoc [] }
995
996 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
997 -- We parse the constructor declaration 
998 --      C t1 t2
999 -- as a btype (treating C as a type constructor) and then convert C to be
1000 -- a data constructor.  Reason: it might continue like this:
1001 --      C t1 t2 %: D Int
1002 -- in which case C really would be a type constructor.  We can't resolve this
1003 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1004         : btype                         {% mkPrefixCon $1 [] >>= return.LL }
1005         | oqtycon '{' '}'               {% mkRecCon $1 [] >>= return.LL }
1006         | oqtycon '{' fielddecls '}'    {% mkRecCon $1 $3 >>= return.LL }
1007         | btype conop btype             { LL ($2, InfixCon $1 $3) }
1008
1009 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
1010         : oqtycon '{' '}'               {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
1011         | oqtycon '{' fielddecls '}'    {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
1012
1013 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
1014         : fielddecl ',' fielddecls      { unLoc $1 : $3 }
1015         | fielddecl                     { [unLoc $1] }
1016
1017 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
1018         : sig_vars '::' ctype           { LL (reverse (unLoc $1), $3) }
1019
1020 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1021 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1022 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1023 -- We don't allow a context, but that's sorted out by the type checker.
1024 deriving :: { Located (Maybe [LHsType RdrName]) }
1025         : {- empty -}                           { noLoc Nothing }
1026         | 'deriving' qtycon     {% do { let { L loc tv = $2 }
1027                                       ; p <- checkInstType (L loc (HsTyVar tv))
1028                                       ; return (LL (Just [p])) } }
1029         | 'deriving' '(' ')'                    { LL (Just []) }
1030         | 'deriving' '(' inst_types1 ')'        { LL (Just $3) }
1031              -- Glasgow extension: allow partial 
1032              -- applications in derivings
1033
1034 -----------------------------------------------------------------------------
1035 -- Value definitions
1036
1037 {- There's an awkward overlap with a type signature.  Consider
1038         f :: Int -> Int = ...rhs...
1039    Then we can't tell whether it's a type signature or a value
1040    definition with a result signature until we see the '='.
1041    So we have to inline enough to postpone reductions until we know.
1042 -}
1043
1044 {-
1045   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1046   instead of qvar, we get another shift/reduce-conflict. Consider the
1047   following programs:
1048   
1049      { (^^) :: Int->Int ; }          Type signature; only var allowed
1050
1051      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
1052                                      qvar allowed (because of instance decls)
1053   
1054   We can't tell whether to reduce var to qvar until after we've read the signatures.
1055 -}
1056
1057 decl    :: { Located (OrdList (LHsDecl RdrName)) }
1058         : sigdecl                       { $1 }
1059         | '!' infixexp rhs              {% do { pat <- checkPattern $2;
1060                                                 return (LL $ unitOL $ LL $ ValD $ 
1061                                                         PatBind (LL $ BangPat pat) (unLoc $3)
1062                                                                 placeHolderType placeHolderNames) } }
1063         | infixexp opt_sig rhs          {% do { r <- checkValDef $1 $2 $3;
1064                                                 return (LL $ unitOL (LL $ ValD r)) } }
1065
1066 rhs     :: { Located (GRHSs RdrName) }
1067         : '=' exp wherebinds    { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1068         | gdrhs wherebinds      { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1069
1070 gdrhs :: { Located [LGRHS RdrName] }
1071         : gdrhs gdrh            { LL ($2 : unLoc $1) }
1072         | gdrh                  { L1 [$1] }
1073
1074 gdrh :: { LGRHS RdrName }
1075         : '|' quals '=' exp     { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1076
1077 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1078         : infixexp '::' sigtype
1079                                 {% do s <- checkValSig $1 $3; 
1080                                       return (LL $ unitOL (LL $ SigD s)) }
1081                 -- See the above notes for why we need infixexp here
1082         | var ',' sig_vars '::' sigtype 
1083                                 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1084         | infix prec ops        { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1085                                              | n <- unLoc $3 ] }
1086         | '{-# INLINE'   activation qvar '#-}'        
1087                                 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1088         | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1089                                 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1090                                             | t <- $4] }
1091         | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1092                                 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1093                                             | t <- $5] }
1094         | '{-# SPECIALISE' 'instance' inst_type '#-}'
1095                                 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1096
1097 -----------------------------------------------------------------------------
1098 -- Expressions
1099
1100 exp   :: { LHsExpr RdrName }
1101         : infixexp '::' sigtype         { LL $ ExprWithTySig $1 $3 }
1102         | infixexp '-<' exp             { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1103         | infixexp '>-' exp             { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1104         | infixexp '-<<' exp            { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1105         | infixexp '>>-' exp            { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1106         | infixexp                      { $1 }
1107
1108 infixexp :: { LHsExpr RdrName }
1109         : exp10                         { $1 }
1110         | infixexp qop exp10            { LL (OpApp $1 $2 (panic "fixity") $3) }
1111
1112 exp10 :: { LHsExpr RdrName }
1113         : '\\' aexp aexps opt_asig '->' exp     
1114                         {% checkPatterns ($2 : reverse $3) >>= \ ps -> 
1115                            return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1116                                             (GRHSs (unguardedRHS $6) emptyLocalBinds
1117                                                         )])) }
1118         | 'let' binds 'in' exp                  { LL $ HsLet (unLoc $2) $4 }
1119         | 'if' exp 'then' exp 'else' exp        { LL $ HsIf $2 $4 $6 }
1120         | 'case' exp 'of' altslist              { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1121         | '-' fexp                              { LL $ mkHsNegApp $2 }
1122
1123         | 'do' stmtlist                 {% let loc = comb2 $1 $2 in
1124                                            checkDo loc (unLoc $2)  >>= \ (stmts,body) ->
1125                                            return (L loc (mkHsDo DoExpr stmts body)) }
1126         | 'mdo' stmtlist                {% let loc = comb2 $1 $2 in
1127                                            checkDo loc (unLoc $2)  >>= \ (stmts,body) ->
1128                                            return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1129         | scc_annot exp                         { LL $ if opt_SccProfilingOn
1130                                                         then HsSCC (unLoc $1) $2
1131                                                         else HsPar $2 }
1132
1133         | 'proc' aexp '->' exp  
1134                         {% checkPattern $2 >>= \ p -> 
1135                            return (LL $ HsProc p (LL $ HsCmdTop $4 [] 
1136                                                    placeHolderType undefined)) }
1137                                                 -- TODO: is LL right here?
1138
1139         | '{-# CORE' STRING '#-}' exp           { LL $ HsCoreAnn (getSTRING $2) $4 }
1140                                                     -- hdaume: core annotation
1141         | fexp                                  { $1 }
1142
1143 scc_annot :: { Located FastString }
1144         : '_scc_' STRING                        { LL $ getSTRING $2 }
1145         | '{-# SCC' STRING '#-}'                { LL $ getSTRING $2 }
1146
1147 fexp    :: { LHsExpr RdrName }
1148         : fexp aexp                             { LL $ HsApp $1 $2 }
1149         | aexp                                  { $1 }
1150
1151 aexps   :: { [LHsExpr RdrName] }
1152         : aexps aexp                            { $2 : $1 }
1153         | {- empty -}                           { [] }
1154
1155 aexp    :: { LHsExpr RdrName }
1156         : qvar '@' aexp                 { LL $ EAsPat $1 $3 }
1157         | '~' aexp                      { LL $ ELazyPat $2 }
1158 --      | '!' aexp                      { LL $ EBangPat $2 }
1159         | aexp1                         { $1 }
1160
1161 aexp1   :: { LHsExpr RdrName }
1162         : aexp1 '{' fbinds '}'  {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) 
1163                                                         (reverse $3);
1164                                         return (LL r) }}
1165         | aexp2                 { $1 }
1166
1167 -- Here was the syntax for type applications that I was planning
1168 -- but there are difficulties (e.g. what order for type args)
1169 -- so it's not enabled yet.
1170 -- But this case *is* used for the left hand side of a generic definition,
1171 -- which is parsed as an expression before being munged into a pattern
1172         | qcname '{|' gentype '|}'      { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1173                                                      (sL (getLoc $3) (HsType $3)) }
1174
1175 aexp2   :: { LHsExpr RdrName }
1176         : ipvar                         { L1 (HsIPVar $! unLoc $1) }
1177         | qcname                        { L1 (HsVar   $! unLoc $1) }
1178         | literal                       { L1 (HsLit   $! unLoc $1) }
1179         | INTEGER                       { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1180         | RATIONAL                      { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1181         | '(' exp ')'                   { LL (HsPar $2) }
1182         | '(' texp ',' texps ')'        { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1183         | '(#' texps '#)'               { LL $ ExplicitTuple (reverse $2)      Unboxed }
1184         | '[' list ']'                  { LL (unLoc $2) }
1185         | '[:' parr ':]'                { LL (unLoc $2) }
1186         | '(' infixexp qop ')'          { LL $ SectionL $2 $3 }
1187         | '(' qopm infixexp ')'         { LL $ SectionR $2 $3 }
1188         | '_'                           { L1 EWildPat }
1189         
1190         -- Template Haskell Extension
1191         | TH_ID_SPLICE          { L1 $ HsSpliceE (mkHsSplice 
1192                                         (L1 $ HsVar (mkUnqual varName 
1193                                                         (getTH_ID_SPLICE $1)))) } -- $x
1194         | '$(' exp ')'          { LL $ HsSpliceE (mkHsSplice $2) }               -- $( exp )
1195
1196         | TH_VAR_QUOTE qvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1197         | TH_VAR_QUOTE qcon     { LL $ HsBracket (VarBr (unLoc $2)) }
1198         | TH_TY_QUOTE tyvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1199         | TH_TY_QUOTE gtycon    { LL $ HsBracket (VarBr (unLoc $2)) }
1200         | '[|' exp '|]'         { LL $ HsBracket (ExpBr $2) }                       
1201         | '[t|' ctype '|]'      { LL $ HsBracket (TypBr $2) }                       
1202         | '[p|' infixexp '|]'   {% checkPattern $2 >>= \p ->
1203                                            return (LL $ HsBracket (PatBr p)) }
1204         | '[d|' cvtopbody '|]'  { LL $ HsBracket (DecBr (mkGroup $2)) }
1205
1206         -- arrow notation extension
1207         | '(|' aexp2 cmdargs '|)'       { LL $ HsArrForm $2 Nothing (reverse $3) }
1208
1209 cmdargs :: { [LHsCmdTop RdrName] }
1210         : cmdargs acmd                  { $2 : $1 }
1211         | {- empty -}                   { [] }
1212
1213 acmd    :: { LHsCmdTop RdrName }
1214         : aexp2                 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1215
1216 cvtopbody :: { [LHsDecl RdrName] }
1217         :  '{'            cvtopdecls0 '}'               { $2 }
1218         |      vocurly    cvtopdecls0 close             { $2 }
1219
1220 cvtopdecls0 :: { [LHsDecl RdrName] }
1221         : {- empty -}           { [] }
1222         | cvtopdecls            { $1 }
1223
1224 texp :: { LHsExpr RdrName }
1225         : exp                           { $1 }
1226         | qopm infixexp                 { LL $ SectionR $1 $2 }
1227         -- The second production is really here only for bang patterns
1228         -- but 
1229
1230 texps :: { [LHsExpr RdrName] }
1231         : texps ',' texp                { $3 : $1 }
1232         | texp                          { [$1] }
1233
1234
1235 -----------------------------------------------------------------------------
1236 -- List expressions
1237
1238 -- The rules below are little bit contorted to keep lexps left-recursive while
1239 -- avoiding another shift/reduce-conflict.
1240
1241 list :: { LHsExpr RdrName }
1242         : texp                  { L1 $ ExplicitList placeHolderType [$1] }
1243         | lexps                 { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1244         | texp '..'             { LL $ ArithSeq noPostTcExpr (From $1) }
1245         | texp ',' exp '..'     { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1246         | texp '..' exp         { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1247         | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1248         | texp pquals           { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1249
1250 lexps :: { Located [LHsExpr RdrName] }
1251         : lexps ',' texp                { LL ($3 : unLoc $1) }
1252         | texp ',' texp                 { LL [$3,$1] }
1253
1254 -----------------------------------------------------------------------------
1255 -- List Comprehensions
1256
1257 pquals :: { Located [LStmt RdrName] }   -- Either a singleton ParStmt, 
1258                                         -- or a reversed list of Stmts
1259         : pquals1                       { case unLoc $1 of
1260                                             [qs] -> L1 qs
1261                                             qss  -> L1 [L1 (ParStmt stmtss)]
1262                                                  where
1263                                                     stmtss = [ (reverse qs, undefined) 
1264                                                              | qs <- qss ]
1265                                         }
1266                         
1267 pquals1 :: { Located [[LStmt RdrName]] }
1268         : pquals1 '|' quals             { LL (unLoc $3 : unLoc $1) }
1269         | '|' quals                     { L (getLoc $2) [unLoc $2] }
1270
1271 quals :: { Located [LStmt RdrName] }
1272         : quals ',' qual                { LL ($3 : unLoc $1) }
1273         | qual                          { L1 [$1] }
1274
1275 -----------------------------------------------------------------------------
1276 -- Parallel array expressions
1277
1278 -- The rules below are little bit contorted; see the list case for details.
1279 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1280 -- Moreover, we allow explicit arrays with no element (represented by the nil
1281 -- constructor in the list case).
1282
1283 parr :: { LHsExpr RdrName }
1284         :                               { noLoc (ExplicitPArr placeHolderType []) }
1285         | exp                           { L1 $ ExplicitPArr placeHolderType [$1] }
1286         | lexps                         { L1 $ ExplicitPArr placeHolderType 
1287                                                        (reverse (unLoc $1)) }
1288         | exp '..' exp                  { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1289         | exp ',' exp '..' exp          { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1290         | exp pquals                    { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1291
1292 -- We are reusing `lexps' and `pquals' from the list case.
1293
1294 -----------------------------------------------------------------------------
1295 -- Case alternatives
1296
1297 altslist :: { Located [LMatch RdrName] }
1298         : '{'            alts '}'       { LL (reverse (unLoc $2)) }
1299         |     vocurly    alts  close    { L (getLoc $2) (reverse (unLoc $2)) }
1300
1301 alts    :: { Located [LMatch RdrName] }
1302         : alts1                         { L1 (unLoc $1) }
1303         | ';' alts                      { LL (unLoc $2) }
1304
1305 alts1   :: { Located [LMatch RdrName] }
1306         : alts1 ';' alt                 { LL ($3 : unLoc $1) }
1307         | alts1 ';'                     { LL (unLoc $1) }
1308         | alt                           { L1 [$1] }
1309
1310 alt     :: { LMatch RdrName }
1311         : infixexp opt_sig alt_rhs      {%  checkPattern $1 >>= \p ->
1312                                             return (LL (Match [p] $2 (unLoc $3))) }
1313         | '!' infixexp opt_sig alt_rhs  {%  checkPattern $2 >>= \p ->
1314                                             return (LL (Match [LL $ BangPat p] $3 (unLoc $4))) }
1315
1316 alt_rhs :: { Located (GRHSs RdrName) }
1317         : ralt wherebinds               { LL (GRHSs (unLoc $1) (unLoc $2)) }
1318
1319 ralt :: { Located [LGRHS RdrName] }
1320         : '->' exp                      { LL (unguardedRHS $2) }
1321         | gdpats                        { L1 (reverse (unLoc $1)) }
1322
1323 gdpats :: { Located [LGRHS RdrName] }
1324         : gdpats gdpat                  { LL ($2 : unLoc $1) }
1325         | gdpat                         { L1 [$1] }
1326
1327 gdpat   :: { LGRHS RdrName }
1328         : '|' quals '->' exp            { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1329
1330 -----------------------------------------------------------------------------
1331 -- Statement sequences
1332
1333 stmtlist :: { Located [LStmt RdrName] }
1334         : '{'           stmts '}'       { LL (unLoc $2) }
1335         |     vocurly   stmts close     { $2 }
1336
1337 --      do { ;; s ; s ; ; s ;; }
1338 -- The last Stmt should be an expression, but that's hard to enforce
1339 -- here, because we need too much lookahead if we see do { e ; }
1340 -- So we use ExprStmts throughout, and switch the last one over
1341 -- in ParseUtils.checkDo instead
1342 stmts :: { Located [LStmt RdrName] }
1343         : stmt stmts_help               { LL ($1 : unLoc $2) }
1344         | ';' stmts                     { LL (unLoc $2) }
1345         | {- empty -}                   { noLoc [] }
1346
1347 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1348         : ';' stmts                     { LL (unLoc $2) }
1349         | {- empty -}                   { noLoc [] }
1350
1351 -- For typing stmts at the GHCi prompt, where 
1352 -- the input may consist of just comments.
1353 maybe_stmt :: { Maybe (LStmt RdrName) }
1354         : stmt                          { Just $1 }
1355         | {- nothing -}                 { Nothing }
1356
1357 stmt  :: { LStmt RdrName }
1358         : qual                          { $1 }
1359         | infixexp '->' exp             {% checkPattern $3 >>= \p ->
1360                                            return (LL $ mkBindStmt p $1) }
1361         | 'rec' stmtlist                { LL $ mkRecStmt (unLoc $2) }
1362
1363 qual  :: { LStmt RdrName }
1364         : exp '<-' exp                  {% checkPattern $1 >>= \p ->
1365                                            return (LL $ mkBindStmt p $3) }
1366         | exp                           { L1 $ mkExprStmt $1 }
1367         | 'let' binds                   { LL $ LetStmt (unLoc $2) }
1368
1369 -----------------------------------------------------------------------------
1370 -- Record Field Update/Construction
1371
1372 fbinds  :: { HsRecordBinds RdrName }
1373         : fbinds1                       { $1 }
1374         | {- empty -}                   { [] }
1375
1376 fbinds1 :: { HsRecordBinds RdrName }
1377         : fbinds1 ',' fbind             { $3 : $1 }
1378         | fbind                         { [$1] }
1379   
1380 fbind   :: { (Located RdrName, LHsExpr RdrName) }
1381         : qvar '=' exp                  { ($1,$3) }
1382
1383 -----------------------------------------------------------------------------
1384 -- Implicit Parameter Bindings
1385
1386 dbinds  :: { Located [LIPBind RdrName] }
1387         : dbinds ';' dbind              { LL ($3 : unLoc $1) }
1388         | dbinds ';'                    { LL (unLoc $1) }
1389         | dbind                         { L1 [$1] }
1390 --      | {- empty -}                   { [] }
1391
1392 dbind   :: { LIPBind RdrName }
1393 dbind   : ipvar '=' exp                 { LL (IPBind (unLoc $1) $3) }
1394
1395 ipvar   :: { Located (IPName RdrName) }
1396         : IPDUPVARID            { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1397
1398 -----------------------------------------------------------------------------
1399 -- Deprecations
1400
1401 depreclist :: { Located [RdrName] }
1402 depreclist : deprec_var                 { L1 [unLoc $1] }
1403            | deprec_var ',' depreclist  { LL (unLoc $1 : unLoc $3) }
1404
1405 deprec_var :: { Located RdrName }
1406 deprec_var : var                        { $1 }
1407            | con                        { $1 }
1408
1409 -----------------------------------------
1410 -- Data constructors
1411 qcon    :: { Located RdrName }
1412         : qconid                { $1 }
1413         | '(' qconsym ')'       { LL (unLoc $2) }
1414         | sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1415 -- The case of '[:' ':]' is part of the production `parr'
1416
1417 con     :: { Located RdrName }
1418         : conid                 { $1 }
1419         | '(' consym ')'        { LL (unLoc $2) }
1420         | sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1421
1422 sysdcon :: { Located DataCon }  -- Wired in data constructors
1423         : '(' ')'               { LL unitDataCon }
1424         | '(' commas ')'        { LL $ tupleCon Boxed $2 }
1425         | '[' ']'               { LL nilDataCon }
1426
1427 conop :: { Located RdrName }
1428         : consym                { $1 }  
1429         | '`' conid '`'         { LL (unLoc $2) }
1430
1431 qconop :: { Located RdrName }
1432         : qconsym               { $1 }
1433         | '`' qconid '`'        { LL (unLoc $2) }
1434
1435 -----------------------------------------------------------------------------
1436 -- Type constructors
1437
1438 gtycon  :: { Located RdrName }  -- A "general" qualified tycon
1439         : oqtycon                       { $1 }
1440         | '(' ')'                       { LL $ getRdrName unitTyCon }
1441         | '(' commas ')'                { LL $ getRdrName (tupleTyCon Boxed $2) }
1442         | '(' '->' ')'                  { LL $ getRdrName funTyCon }
1443         | '[' ']'                       { LL $ listTyCon_RDR }
1444         | '[:' ':]'                     { LL $ parrTyCon_RDR }
1445
1446 oqtycon :: { Located RdrName }  -- An "ordinary" qualified tycon
1447         : qtycon                        { $1 }
1448         | '(' qtyconsym ')'             { LL (unLoc $2) }
1449
1450 qtyconop :: { Located RdrName } -- Qualified or unqualified
1451         : qtyconsym                     { $1 }
1452         | '`' qtycon '`'                { LL (unLoc $2) }
1453
1454 qtycon :: { Located RdrName }   -- Qualified or unqualified
1455         : QCONID                        { L1 $! mkQual tcClsName (getQCONID $1) }
1456         | tycon                         { $1 }
1457
1458 tycon   :: { Located RdrName }  -- Unqualified
1459         : CONID                         { L1 $! mkUnqual tcClsName (getCONID $1) }
1460
1461 qtyconsym :: { Located RdrName }
1462         : QCONSYM                       { L1 $! mkQual tcClsName (getQCONSYM $1) }
1463         | tyconsym                      { $1 }
1464
1465 tyconsym :: { Located RdrName }
1466         : CONSYM                        { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1467
1468 -----------------------------------------------------------------------------
1469 -- Operators
1470
1471 op      :: { Located RdrName }   -- used in infix decls
1472         : varop                 { $1 }
1473         | conop                 { $1 }
1474
1475 varop   :: { Located RdrName }
1476         : varsym                { $1 }
1477         | '`' varid '`'         { LL (unLoc $2) }
1478
1479 qop     :: { LHsExpr RdrName }   -- used in sections
1480         : qvarop                { L1 $ HsVar (unLoc $1) }
1481         | qconop                { L1 $ HsVar (unLoc $1) }
1482
1483 qopm    :: { LHsExpr RdrName }   -- used in sections
1484         : qvaropm               { L1 $ HsVar (unLoc $1) }
1485         | qconop                { L1 $ HsVar (unLoc $1) }
1486
1487 qvarop :: { Located RdrName }
1488         : qvarsym               { $1 }
1489         | '`' qvarid '`'        { LL (unLoc $2) }
1490
1491 qvaropm :: { Located RdrName }
1492         : qvarsym_no_minus      { $1 }
1493         | '`' qvarid '`'        { LL (unLoc $2) }
1494
1495 -----------------------------------------------------------------------------
1496 -- Type variables
1497
1498 tyvar   :: { Located RdrName }
1499 tyvar   : tyvarid               { $1 }
1500         | '(' tyvarsym ')'      { LL (unLoc $2) }
1501
1502 tyvarop :: { Located RdrName }
1503 tyvarop : '`' tyvarid '`'       { LL (unLoc $2) }
1504         | tyvarsym              { $1 }
1505
1506 tyvarid :: { Located RdrName }
1507         : VARID                 { L1 $! mkUnqual tvName (getVARID $1) }
1508         | special_id            { L1 $! mkUnqual tvName (unLoc $1) }
1509         | 'unsafe'              { L1 $! mkUnqual tvName FSLIT("unsafe") }
1510         | 'safe'                { L1 $! mkUnqual tvName FSLIT("safe") }
1511         | 'threadsafe'          { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1512
1513 tyvarsym :: { Located RdrName }
1514 -- Does not include "!", because that is used for strictness marks
1515 --               or ".", because that separates the quantified type vars from the rest
1516 --               or "*", because that's used for kinds
1517 tyvarsym : VARSYM               { L1 $! mkUnqual tvName (getVARSYM $1) }
1518
1519 -----------------------------------------------------------------------------
1520 -- Variables 
1521
1522 var     :: { Located RdrName }
1523         : varid                 { $1 }
1524         | '(' varsym ')'        { LL (unLoc $2) }
1525
1526 qvar    :: { Located RdrName }
1527         : qvarid                { $1 }
1528         | '(' varsym ')'        { LL (unLoc $2) }
1529         | '(' qvarsym1 ')'      { LL (unLoc $2) }
1530 -- We've inlined qvarsym here so that the decision about
1531 -- whether it's a qvar or a var can be postponed until
1532 -- *after* we see the close paren.
1533
1534 qvarid :: { Located RdrName }
1535         : varid                 { $1 }
1536         | QVARID                { L1 $ mkQual varName (getQVARID $1) }
1537
1538 varid :: { Located RdrName }
1539         : varid_no_unsafe       { $1 }
1540         | 'unsafe'              { L1 $! mkUnqual varName FSLIT("unsafe") }
1541         | 'safe'                { L1 $! mkUnqual varName FSLIT("safe") }
1542         | 'threadsafe'          { L1 $! mkUnqual varName FSLIT("threadsafe") }
1543
1544 varid_no_unsafe :: { Located RdrName }
1545         : VARID                 { L1 $! mkUnqual varName (getVARID $1) }
1546         | special_id            { L1 $! mkUnqual varName (unLoc $1) }
1547         | 'forall'              { L1 $! mkUnqual varName FSLIT("forall") }
1548         | 'iso'                 { L1 $! mkUnqual varName FSLIT("iso") }
1549         | 'family'              { L1 $! mkUnqual varName FSLIT("family") }
1550
1551 qvarsym :: { Located RdrName }
1552         : varsym                { $1 }
1553         | qvarsym1              { $1 }
1554
1555 qvarsym_no_minus :: { Located RdrName }
1556         : varsym_no_minus       { $1 }
1557         | qvarsym1              { $1 }
1558
1559 qvarsym1 :: { Located RdrName }
1560 qvarsym1 : QVARSYM              { L1 $ mkQual varName (getQVARSYM $1) }
1561
1562 varsym :: { Located RdrName }
1563         : varsym_no_minus       { $1 }
1564         | '-'                   { L1 $ mkUnqual varName FSLIT("-") }
1565
1566 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1567         : VARSYM                { L1 $ mkUnqual varName (getVARSYM $1) }
1568         | special_sym           { L1 $ mkUnqual varName (unLoc $1) }
1569
1570
1571 -- These special_ids are treated as keywords in various places, 
1572 -- but as ordinary ids elsewhere.   'special_id' collects all these
1573 -- except 'unsafe', 'forall', 'family', and 'iso' whose treatment differs
1574 -- depending on context 
1575 special_id :: { Located FastString }
1576 special_id
1577         : 'as'                  { L1 FSLIT("as") }
1578         | 'qualified'           { L1 FSLIT("qualified") }
1579         | 'hiding'              { L1 FSLIT("hiding") }
1580         | 'for'                 { L1 FSLIT("for") }
1581         | 'export'              { L1 FSLIT("export") }
1582         | 'label'               { L1 FSLIT("label")  }
1583         | 'dynamic'             { L1 FSLIT("dynamic") }
1584         | 'stdcall'             { L1 FSLIT("stdcall") }
1585         | 'ccall'               { L1 FSLIT("ccall") }
1586
1587 special_sym :: { Located FastString }
1588 special_sym : '!'       { L1 FSLIT("!") }
1589             | '.'       { L1 FSLIT(".") }
1590             | '*'       { L1 FSLIT("*") }
1591
1592 -----------------------------------------------------------------------------
1593 -- Data constructors
1594
1595 qconid :: { Located RdrName }   -- Qualified or unqualified
1596         : conid                 { $1 }
1597         | QCONID                { L1 $ mkQual dataName (getQCONID $1) }
1598
1599 conid   :: { Located RdrName }
1600         : CONID                 { L1 $ mkUnqual dataName (getCONID $1) }
1601
1602 qconsym :: { Located RdrName }  -- Qualified or unqualified
1603         : consym                { $1 }
1604         | QCONSYM               { L1 $ mkQual dataName (getQCONSYM $1) }
1605
1606 consym :: { Located RdrName }
1607         : CONSYM                { L1 $ mkUnqual dataName (getCONSYM $1) }
1608
1609         -- ':' means only list cons
1610         | ':'                   { L1 $ consDataCon_RDR }
1611
1612
1613 -----------------------------------------------------------------------------
1614 -- Literals
1615
1616 literal :: { Located HsLit }
1617         : CHAR                  { L1 $ HsChar       $ getCHAR $1 }
1618         | STRING                { L1 $ HsString     $ getSTRING $1 }
1619         | PRIMINTEGER           { L1 $ HsIntPrim    $ getPRIMINTEGER $1 }
1620         | PRIMCHAR              { L1 $ HsCharPrim   $ getPRIMCHAR $1 }
1621         | PRIMSTRING            { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1622         | PRIMFLOAT             { L1 $ HsFloatPrim  $ getPRIMFLOAT $1 }
1623         | PRIMDOUBLE            { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1624
1625 -----------------------------------------------------------------------------
1626 -- Layout
1627
1628 close :: { () }
1629         : vccurly               { () } -- context popped in lexer.
1630         | error                 {% popContext }
1631
1632 -----------------------------------------------------------------------------
1633 -- Miscellaneous (mostly renamings)
1634
1635 modid   :: { Located ModuleName }
1636         : CONID                 { L1 $ mkModuleNameFS (getCONID $1) }
1637         | QCONID                { L1 $ let (mod,c) = getQCONID $1 in
1638                                   mkModuleNameFS
1639                                    (mkFastString
1640                                      (unpackFS mod ++ '.':unpackFS c))
1641                                 }
1642
1643 commas :: { Int }
1644         : commas ','                    { $1 + 1 }
1645         | ','                           { 2 }
1646
1647 -----------------------------------------------------------------------------
1648
1649 {
1650 happyError :: P a
1651 happyError = srcParseFail
1652
1653 getVARID        (L _ (ITvarid    x)) = x
1654 getCONID        (L _ (ITconid    x)) = x
1655 getVARSYM       (L _ (ITvarsym   x)) = x
1656 getCONSYM       (L _ (ITconsym   x)) = x
1657 getQVARID       (L _ (ITqvarid   x)) = x
1658 getQCONID       (L _ (ITqconid   x)) = x
1659 getQVARSYM      (L _ (ITqvarsym  x)) = x
1660 getQCONSYM      (L _ (ITqconsym  x)) = x
1661 getIPDUPVARID   (L _ (ITdupipvarid   x)) = x
1662 getCHAR         (L _ (ITchar     x)) = x
1663 getSTRING       (L _ (ITstring   x)) = x
1664 getINTEGER      (L _ (ITinteger  x)) = x
1665 getRATIONAL     (L _ (ITrational x)) = x
1666 getPRIMCHAR     (L _ (ITprimchar   x)) = x
1667 getPRIMSTRING   (L _ (ITprimstring x)) = x
1668 getPRIMINTEGER  (L _ (ITprimint    x)) = x
1669 getPRIMFLOAT    (L _ (ITprimfloat  x)) = x
1670 getPRIMDOUBLE   (L _ (ITprimdouble x)) = x
1671 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1672 getINLINE       (L _ (ITinline_prag b)) = b
1673 getSPEC_INLINE  (L _ (ITspec_inline_prag b)) = b
1674
1675 -- Utilities for combining source spans
1676 comb2 :: Located a -> Located b -> SrcSpan
1677 comb2 = combineLocs
1678
1679 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1680 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1681
1682 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1683 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1684                 combineSrcSpans (getLoc c) (getLoc d)
1685
1686 -- strict constructor version:
1687 {-# INLINE sL #-}
1688 sL :: SrcSpan -> a -> Located a
1689 sL span a = span `seq` L span a
1690
1691 -- Make a source location for the file.  We're a bit lazy here and just
1692 -- make a point SrcSpan at line 1, column 0.  Strictly speaking we should
1693 -- try to find the span of the whole file (ToDo).
1694 fileSrcSpan :: P SrcSpan
1695 fileSrcSpan = do 
1696   l <- getSrcLoc; 
1697   let loc = mkSrcLoc (srcLocFile l) 1 0;
1698   return (mkSrcSpan loc loc)
1699 }