Extend Class.Class to include the TyCons of ATs
[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  'hiding'       { L _ IThiding }
163  'if'           { L _ ITif }
164  'import'       { L _ ITimport }
165  'in'           { L _ ITin }
166  'infix'        { L _ ITinfix }
167  'infixl'       { L _ ITinfixl }
168  'infixr'       { L _ ITinfixr }
169  'instance'     { L _ ITinstance }
170  'let'          { L _ ITlet }
171  'module'       { L _ ITmodule }
172  'newtype'      { L _ ITnewtype }
173  'of'           { L _ ITof }
174  'qualified'    { L _ ITqualified }
175  'then'         { L _ ITthen }
176  'type'         { L _ ITtype }
177  'where'        { L _ ITwhere }
178  '_scc_'        { L _ ITscc }         -- ToDo: remove
179
180  'forall'       { L _ ITforall }                -- GHC extension keywords
181  'foreign'      { L _ ITforeign }
182  'export'       { L _ ITexport }
183  'label'        { L _ ITlabel } 
184  'dynamic'      { L _ ITdynamic }
185  'safe'         { L _ ITsafe }
186  'threadsafe'   { L _ ITthreadsafe }
187  'unsafe'       { L _ ITunsafe }
188  'mdo'          { L _ ITmdo }
189  'iso'          { L _ ITiso }
190  'family'       { L _ ITfamily }
191  'stdcall'      { L _ ITstdcallconv }
192  'ccall'        { L _ ITccallconv }
193  'dotnet'       { L _ ITdotnet }
194  'proc'         { L _ ITproc }          -- for arrow notation extension
195  'rec'          { L _ ITrec }           -- for arrow notation extension
196
197  '{-# INLINE'             { L _ (ITinline_prag _) }
198  '{-# SPECIALISE'         { L _ ITspec_prag }
199  '{-# SPECIALISE_INLINE'  { L _ (ITspec_inline_prag _) }
200  '{-# SOURCE'      { L _ ITsource_prag }
201  '{-# RULES'       { L _ ITrules_prag }
202  '{-# CORE'        { L _ ITcore_prag }              -- hdaume: annotated core
203  '{-# SCC'         { L _ ITscc_prag }
204  '{-# DEPRECATED'  { L _ ITdeprecated_prag }
205  '{-# UNPACK'      { L _ ITunpack_prag }
206  '#-}'             { L _ ITclose_prag }
207
208  '..'           { L _ ITdotdot }                        -- reserved symbols
209  ':'            { L _ ITcolon }
210  '::'           { L _ ITdcolon }
211  '='            { L _ ITequal }
212  '\\'           { L _ ITlam }
213  '|'            { L _ ITvbar }
214  '<-'           { L _ ITlarrow }
215  '->'           { L _ ITrarrow }
216  '@'            { L _ ITat }
217  '~'            { L _ ITtilde }
218  '=>'           { L _ ITdarrow }
219  '-'            { L _ ITminus }
220  '!'            { L _ ITbang }
221  '*'            { L _ ITstar }
222  '-<'           { L _ ITlarrowtail }            -- for arrow notation
223  '>-'           { L _ ITrarrowtail }            -- for arrow notation
224  '-<<'          { L _ ITLarrowtail }            -- for arrow notation
225  '>>-'          { L _ ITRarrowtail }            -- for arrow notation
226  '.'            { L _ ITdot }
227
228  '{'            { L _ ITocurly }                        -- special symbols
229  '}'            { L _ ITccurly }
230  '{|'           { L _ ITocurlybar }
231  '|}'           { L _ ITccurlybar }
232  vocurly        { L _ ITvocurly } -- virtual open curly (from layout)
233  vccurly        { L _ ITvccurly } -- virtual close curly (from layout)
234  '['            { L _ ITobrack }
235  ']'            { L _ ITcbrack }
236  '[:'           { L _ ITopabrack }
237  ':]'           { L _ ITcpabrack }
238  '('            { L _ IToparen }
239  ')'            { L _ ITcparen }
240  '(#'           { L _ IToubxparen }
241  '#)'           { L _ ITcubxparen }
242  '(|'           { L _ IToparenbar }
243  '|)'           { L _ ITcparenbar }
244  ';'            { L _ ITsemi }
245  ','            { L _ ITcomma }
246  '`'            { L _ ITbackquote }
247
248  VARID          { L _ (ITvarid    _) }          -- identifiers
249  CONID          { L _ (ITconid    _) }
250  VARSYM         { L _ (ITvarsym   _) }
251  CONSYM         { L _ (ITconsym   _) }
252  QVARID         { L _ (ITqvarid   _) }
253  QCONID         { L _ (ITqconid   _) }
254  QVARSYM        { L _ (ITqvarsym  _) }
255  QCONSYM        { L _ (ITqconsym  _) }
256
257  IPDUPVARID     { L _ (ITdupipvarid   _) }              -- GHC extension
258  IPSPLITVARID   { L _ (ITsplitipvarid _) }              -- 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         | 'default' '(' comma_types0 ')'        { unitOL (LL $ DefD (DefaultDecl $3)) }
463         | 'foreign' fdecl                       { unitOL (LL (unLoc $2)) }
464         | '{-# DEPRECATED' deprecations '#-}'   { $2 }
465         | '{-# RULES' rules '#-}'               { $2 }
466         | decl                                  { unLoc $1 }
467
468         -- Template Haskell Extension
469         | '$(' exp ')'                          { unitOL (LL $ SpliceD (SpliceDecl $2)) }
470         | TH_ID_SPLICE                          { unitOL (LL $ SpliceD (SpliceDecl $
471                                                         L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
472                                                   )) }
473
474 -- Type classes
475 --
476 cl_decl :: { LTyClDecl RdrName }
477         : 'class' tycl_hdr fds where
478                 {% do { let { (binds, sigs, ats)           = 
479                                 cvBindsAndSigs (unLoc $4)
480                             ; (ctxt, tc, tvs, tparms) = unLoc $2}
481                       ; checkTyVars tparms      -- only type vars allowed
482                       ; checkKindSigs ats
483                       ; return $ L (comb4 $1 $2 $3 $4) 
484                                    (mkClassDecl (ctxt, tc, tvs) 
485                                                 (unLoc $3) sigs binds ats) } }
486
487 -- Type declarations (toplevel)
488 --
489 ty_decl :: { LTyClDecl RdrName }
490            -- ordinary type synonyms
491         : 'type' type '=' ctype
492                 -- Note ctype, not sigtype, on the right of '='
493                 -- We allow an explicit for-all but we don't insert one
494                 -- in   type Foo a = (b,b)
495                 -- Instead we just say b is out of scope
496                 --
497                 -- Note the use of type for the head; this allows
498                 -- infix type constructors to be declared 
499                 {% do { (tc, tvs, _) <- checkSynHdr $2 False
500                       ; return (L (comb2 $1 $4) 
501                                   (TySynonym tc tvs Nothing $4)) 
502                       } }
503
504            -- type family declarations
505         | 'type' 'family' opt_iso type '::' kind
506                 -- Note the use of type for the head; this allows
507                 -- infix type constructors to be declared
508                 --
509                 {% do { (tc, tvs, _) <- checkSynHdr $4 False
510                       ; return (L (comb3 $1 $4 $6) 
511                                   (TyFunction tc tvs $3 (unLoc $6)))
512                       } }
513
514            -- type instance declarations
515         | 'type' 'instance' type '=' ctype
516                 -- Note the use of type for the head; this allows
517                 -- infix type constructors and type patterns
518                 --
519                 {% do { (tc, tvs, typats) <- checkSynHdr $3 True
520                       ; return (L (comb2 $1 $5) 
521                                   (TySynonym tc tvs (Just typats) $5)) 
522                       } }
523
524           -- ordinary data type or newtype declaration
525         | data_or_newtype tycl_hdr constrs deriving
526                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
527                       ; checkTyVars tparms    -- no type pattern
528                       ; return $
529                           L (comb4 $1 $2 $3 $4)
530                                    -- We need the location on tycl_hdr in case 
531                                    -- constrs and deriving are both empty
532                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing) 
533                                Nothing (reverse (unLoc $3)) (unLoc $4)) } }
534
535           -- ordinary GADT declaration
536         | data_or_newtype tycl_hdr opt_kind_sig 
537                  'where' gadt_constrlist
538                  deriving
539                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
540                       ; checkTyVars tparms    -- can have type pats
541                       ; return $
542                           L (comb4 $1 $2 $4 $5)
543                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing) $3
544                               (reverse (unLoc $5)) (unLoc $6)) } }
545
546           -- data/newtype family
547         | data_or_newtype 'family' tycl_hdr '::' kind
548                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
549                       ; checkTyVars tparms    -- no type pattern
550                       ; return $
551                           L (comb3 $1 $2 $5)
552                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing) 
553                               (Just (unLoc $5)) [] Nothing) } }
554
555           -- data/newtype instance declaration
556         | data_or_newtype 'instance' tycl_hdr constrs deriving
557                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
558                                              -- can have type pats
559                       ; return $
560                           L (comb4 $1 $3 $4 $5)
561                                    -- We need the location on tycl_hdr in case 
562                                    -- constrs and deriving are both empty
563                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms) 
564                               Nothing (reverse (unLoc $4)) (unLoc $5)) } }
565
566           -- GADT instance declaration
567         | data_or_newtype 'instance' tycl_hdr opt_kind_sig 
568                  'where' gadt_constrlist
569                  deriving
570                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $3}
571                                              -- can have type pats
572                       ; return $
573                           L (comb4 $1 $3 $6 $7)
574                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms) 
575                                $4 (reverse (unLoc $6)) (unLoc $7)) } }
576
577 -- Associate type declarations
578 --
579 at_decl :: { LTyClDecl RdrName }
580            -- type family declarations
581         : 'type' opt_iso type '::' kind
582                 -- Note the use of type for the head; this allows
583                 -- infix type constructors to be declared
584                 --
585                 {% do { (tc, tvs, _) <- checkSynHdr $3 False
586                       ; return (L (comb3 $1 $3 $5) 
587                                   (TyFunction tc tvs $2 (unLoc $5)))
588                       } }
589
590            -- type instance declarations
591         | 'type' opt_iso type '=' ctype
592                 -- Note the use of type for the head; this allows
593                 -- infix type constructors and type patterns
594                 --
595                 {% do { when $2 $ 
596                           parseError (comb2 $1 $>) "Misplaced iso keyword"
597                       ; (tc, tvs, typats) <- checkSynHdr $3 True
598                       ; return (L (comb2 $1 $5) 
599                                   (TySynonym tc tvs (Just typats) $5)) 
600                       } }
601
602           -- data/newtype family
603         | data_or_newtype tycl_hdr '::' kind
604                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
605                       ; checkTyVars tparms    -- no type pattern
606                       ; return $
607                           L (comb3 $1 $2 $4)
608                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Nothing) 
609                               (Just (unLoc $4)) [] Nothing) } }
610
611         -- data/newtype instance declaration
612         | data_or_newtype tycl_hdr constrs deriving
613                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
614                                              -- can have type pats
615                       ; return $
616                           L (comb4 $1 $2 $3 $4)
617                                    -- We need the location on tycl_hdr in case 
618                                    -- constrs and deriving are both empty
619                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms) 
620                               Nothing (reverse (unLoc $3)) (unLoc $4)) } }
621
622         -- GADT instance declaration
623         | data_or_newtype tycl_hdr opt_kind_sig 
624                  'where' gadt_constrlist
625                  deriving
626                 {% do { let {(ctxt, tc, tvs, tparms) = unLoc $2}
627                                              -- can have type pats
628                       ; return $
629                           L (comb4 $1 $2 $5 $6)
630                             (mkTyData (unLoc $1) (ctxt, tc, tvs, Just tparms) 
631                              $3 (reverse (unLoc $5)) (unLoc $6)) } }
632
633 opt_iso :: { Bool }
634         :       { False }
635         | 'iso' { True  }
636
637 data_or_newtype :: { Located NewOrData }
638         : 'data'        { L1 DataType }
639         | 'newtype'     { L1 NewType }
640
641 opt_kind_sig :: { Maybe Kind }
642         :                               { Nothing }
643         | '::' kind                     { Just (unLoc $2) }
644
645 -- tycl_hdr parses the header of a class or data type decl,
646 -- which takes the form
647 --      T a b
648 --      Eq a => T a
649 --      (Eq a, Ord b) => T a b
650 --      T Int [a]                       -- for associated types
651 -- Rather a lot of inlining here, else we get reduce/reduce errors
652 tycl_hdr :: { Located (LHsContext RdrName, 
653                        Located RdrName, 
654                        [LHsTyVarBndr RdrName],
655                        [LHsType RdrName]) }
656         : context '=>' type             {% checkTyClHdr $1         $3 >>= return.LL }
657         | type                          {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
658
659 -----------------------------------------------------------------------------
660 -- Nested declarations
661
662 -- Type declaration or value declaration
663 --
664 tydecl  :: { Located (OrdList (LHsDecl RdrName)) }
665 tydecl  : at_decl                       { LL (unitOL (L1 (TyClD (unLoc $1)))) }
666         | decl                          { $1 }
667
668 tydecls :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
669         : tydecls ';' tydecl            { LL (unLoc $1 `appOL` unLoc $3) }
670         | tydecls ';'                   { LL (unLoc $1) }
671         | tydecl                        { $1 }
672         | {- empty -}                   { noLoc nilOL }
673
674
675 tydecllist 
676         :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
677         : '{'            tydecls '}'    { LL (unLoc $2) }
678         |     vocurly    tydecls close  { $2 }
679
680 -- Form of the body of class and instance declarations
681 --
682 where   :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
683                                 -- No implicit parameters
684                                 -- May have type declarations
685         : 'where' tydecllist            { LL (unLoc $2) }
686         | {- empty -}                   { noLoc nilOL }
687
688 decls   :: { Located (OrdList (LHsDecl RdrName)) }      
689         : decls ';' decl                { LL (unLoc $1 `appOL` unLoc $3) }
690         | decls ';'                     { LL (unLoc $1) }
691         | decl                          { $1 }
692         | {- empty -}                   { noLoc nilOL }
693
694
695 decllist :: { Located (OrdList (LHsDecl RdrName)) }
696         : '{'            decls '}'      { LL (unLoc $2) }
697         |     vocurly    decls close    { $2 }
698
699 -- Binding groups other than those of class and instance declarations
700 --
701 binds   ::  { Located (HsLocalBinds RdrName) }          -- May have implicit parameters
702                                                 -- No type declarations
703         : decllist                      { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
704         | '{'            dbinds '}'     { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
705         |     vocurly    dbinds close   { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
706
707 wherebinds :: { Located (HsLocalBinds RdrName) }        -- May have implicit parameters
708                                                 -- No type declarations
709         : 'where' binds                 { LL (unLoc $2) }
710         | {- empty -}                   { noLoc emptyLocalBinds }
711
712
713 -----------------------------------------------------------------------------
714 -- Transformation Rules
715
716 rules   :: { OrdList (LHsDecl RdrName) }
717         :  rules ';' rule                       { $1 `snocOL` $3 }
718         |  rules ';'                            { $1 }
719         |  rule                                 { unitOL $1 }
720         |  {- empty -}                          { nilOL }
721
722 rule    :: { LHsDecl RdrName }
723         : STRING activation rule_forall infixexp '=' exp
724              { LL $ RuleD (HsRule (getSTRING $1) 
725                                   ($2 `orElse` AlwaysActive) 
726                                   $3 $4 placeHolderNames $6 placeHolderNames) }
727
728 activation :: { Maybe Activation } 
729         : {- empty -}                           { Nothing }
730         | explicit_activation                   { Just $1 }
731
732 explicit_activation :: { Activation }  -- In brackets
733         : '[' INTEGER ']'               { ActiveAfter  (fromInteger (getINTEGER $2)) }
734         | '[' '~' INTEGER ']'           { ActiveBefore (fromInteger (getINTEGER $3)) }
735
736 rule_forall :: { [RuleBndr RdrName] }
737         : 'forall' rule_var_list '.'            { $2 }
738         | {- empty -}                           { [] }
739
740 rule_var_list :: { [RuleBndr RdrName] }
741         : rule_var                              { [$1] }
742         | rule_var rule_var_list                { $1 : $2 }
743
744 rule_var :: { RuleBndr RdrName }
745         : varid                                 { RuleBndr $1 }
746         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
747
748 -----------------------------------------------------------------------------
749 -- Deprecations (c.f. rules)
750
751 deprecations :: { OrdList (LHsDecl RdrName) }
752         : deprecations ';' deprecation          { $1 `appOL` $3 }
753         | deprecations ';'                      { $1 }
754         | deprecation                           { $1 }
755         | {- empty -}                           { nilOL }
756
757 -- SUP: TEMPORARY HACK, not checking for `module Foo'
758 deprecation :: { OrdList (LHsDecl RdrName) }
759         : depreclist STRING
760                 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2)) 
761                        | n <- unLoc $1 ] }
762
763
764 -----------------------------------------------------------------------------
765 -- Foreign import and export declarations
766
767 fdecl :: { LHsDecl RdrName }
768 fdecl : 'import' callconv safety fspec
769                 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
770       | 'import' callconv        fspec          
771                 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
772                         return (LL d) } }
773       | 'export' callconv fspec
774                 {% mkExport $2 (unLoc $3) >>= return.LL }
775
776 callconv :: { CallConv }
777           : 'stdcall'                   { CCall  StdCallConv }
778           | 'ccall'                     { CCall  CCallConv   }
779           | 'dotnet'                    { DNCall             }
780
781 safety :: { Safety }
782         : 'unsafe'                      { PlayRisky }
783         | 'safe'                        { PlaySafe  False }
784         | 'threadsafe'                  { PlaySafe  True }
785
786 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
787        : STRING var '::' sigtype      { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
788        |        var '::' sigtype      { LL (noLoc nilFS, $1, $3) }
789          -- if the entity string is missing, it defaults to the empty string;
790          -- the meaning of an empty entity string depends on the calling
791          -- convention
792
793 -----------------------------------------------------------------------------
794 -- Type signatures
795
796 opt_sig :: { Maybe (LHsType RdrName) }
797         : {- empty -}                   { Nothing }
798         | '::' sigtype                  { Just $2 }
799
800 opt_asig :: { Maybe (LHsType RdrName) }
801         : {- empty -}                   { Nothing }
802         | '::' atype                    { Just $2 }
803
804 sigtypes1 :: { [LHsType RdrName] }
805         : sigtype                       { [ $1 ] }
806         | sigtype ',' sigtypes1         { $1 : $3 }
807
808 sigtype :: { LHsType RdrName }
809         : ctype                         { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
810         -- Wrap an Implicit forall if there isn't one there already
811
812 sig_vars :: { Located [Located RdrName] }
813          : sig_vars ',' var             { LL ($3 : unLoc $1) }
814          | var                          { L1 [$1] }
815
816 -----------------------------------------------------------------------------
817 -- Types
818
819 strict_mark :: { Located HsBang }
820         : '!'                           { L1 HsStrict }
821         | '{-# UNPACK' '#-}' '!'        { LL HsUnbox }
822
823 -- A ctype is a for-all type
824 ctype   :: { LHsType RdrName }
825         : 'forall' tv_bndrs '.' ctype   { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
826         | context '=>' type             { LL $ mkImplicitHsForAllTy   $1 $3 }
827         -- A type of form (context => type) is an *implicit* HsForAllTy
828         | type                          { $1 }
829
830 -- We parse a context as a btype so that we don't get reduce/reduce
831 -- errors in ctype.  The basic problem is that
832 --      (Eq a, Ord a)
833 -- looks so much like a tuple type.  We can't tell until we find the =>
834 context :: { LHsContext RdrName }
835         : btype                         {% checkContext $1 }
836
837 type :: { LHsType RdrName }
838         : ipvar '::' gentype            { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
839         | gentype                       { $1 }
840
841 gentype :: { LHsType RdrName }
842         : btype                         { $1 }
843         | btype qtyconop gentype        { LL $ HsOpTy $1 $2 $3 }
844         | btype tyvarop  gentype        { LL $ HsOpTy $1 $2 $3 }
845         | btype '->' ctype              { LL $ HsFunTy $1 $3 }
846
847 btype :: { LHsType RdrName }
848         : btype atype                   { LL $ HsAppTy $1 $2 }
849         | atype                         { $1 }
850
851 atype :: { LHsType RdrName }
852         : gtycon                        { L1 (HsTyVar (unLoc $1)) }
853         | tyvar                         { L1 (HsTyVar (unLoc $1)) }
854         | strict_mark atype             { LL (HsBangTy (unLoc $1) $2) }
855         | '(' ctype ',' comma_types1 ')'  { LL $ HsTupleTy Boxed  ($2:$4) }
856         | '(#' comma_types1 '#)'        { LL $ HsTupleTy Unboxed $2     }
857         | '[' ctype ']'                 { LL $ HsListTy  $2 }
858         | '[:' ctype ':]'               { LL $ HsPArrTy  $2 }
859         | '(' ctype ')'                 { LL $ HsParTy   $2 }
860         | '(' ctype '::' kind ')'       { LL $ HsKindSig $2 (unLoc $4) }
861 -- Generics
862         | INTEGER                       { L1 (HsNumTy (getINTEGER $1)) }
863
864 -- An inst_type is what occurs in the head of an instance decl
865 --      e.g.  (Foo a, Gaz b) => Wibble a b
866 -- It's kept as a single type, with a MonoDictTy at the right
867 -- hand corner, for convenience.
868 inst_type :: { LHsType RdrName }
869         : sigtype                       {% checkInstType $1 }
870
871 inst_types1 :: { [LHsType RdrName] }
872         : inst_type                     { [$1] }
873         | inst_type ',' inst_types1     { $1 : $3 }
874
875 comma_types0  :: { [LHsType RdrName] }
876         : comma_types1                  { $1 }
877         | {- empty -}                   { [] }
878
879 comma_types1    :: { [LHsType RdrName] }
880         : ctype                         { [$1] }
881         | ctype  ',' comma_types1       { $1 : $3 }
882
883 tv_bndrs :: { [LHsTyVarBndr RdrName] }
884          : tv_bndr tv_bndrs             { $1 : $2 }
885          | {- empty -}                  { [] }
886
887 tv_bndr :: { LHsTyVarBndr RdrName }
888         : tyvar                         { L1 (UserTyVar (unLoc $1)) }
889         | '(' tyvar '::' kind ')'       { LL (KindedTyVar (unLoc $2) 
890                                                           (unLoc $4)) }
891
892 fds :: { Located [Located ([RdrName], [RdrName])] }
893         : {- empty -}                   { noLoc [] }
894         | '|' fds1                      { LL (reverse (unLoc $2)) }
895
896 fds1 :: { Located [Located ([RdrName], [RdrName])] }
897         : fds1 ',' fd                   { LL ($3 : unLoc $1) }
898         | fd                            { L1 [$1] }
899
900 fd :: { Located ([RdrName], [RdrName]) }
901         : varids0 '->' varids0          { L (comb3 $1 $2 $3)
902                                            (reverse (unLoc $1), reverse (unLoc $3)) }
903
904 varids0 :: { Located [RdrName] }
905         : {- empty -}                   { noLoc [] }
906         | varids0 tyvar                 { LL (unLoc $2 : unLoc $1) }
907
908 -----------------------------------------------------------------------------
909 -- Kinds
910
911 kind    :: { Located Kind }
912         : akind                 { $1 }
913         | akind '->' kind       { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
914
915 akind   :: { Located Kind }
916         : '*'                   { L1 liftedTypeKind }
917         | '!'                   { L1 unliftedTypeKind }
918         | '(' kind ')'          { LL (unLoc $2) }
919
920
921 -----------------------------------------------------------------------------
922 -- Datatype declarations
923
924 gadt_constrlist :: { Located [LConDecl RdrName] }
925         : '{'            gadt_constrs '}'       { LL (unLoc $2) }
926         |     vocurly    gadt_constrs close     { $2 }
927
928 gadt_constrs :: { Located [LConDecl RdrName] }
929         : gadt_constrs ';' gadt_constr  { LL ($3 : unLoc $1) }
930         | gadt_constrs ';'              { $1 }
931         | gadt_constr                   { L1 [$1] } 
932
933 -- We allow the following forms:
934 --      C :: Eq a => a -> T a
935 --      C :: forall a. Eq a => !a -> T a
936 --      D { x,y :: a } :: T a
937 --      forall a. Eq a => D { x,y :: a } :: T a
938
939 gadt_constr :: { LConDecl RdrName }
940         : con '::' sigtype
941               { LL (mkGadtDecl $1 $3) } 
942         -- Syntax: Maybe merge the record stuff with the single-case above?
943         --         (to kill the mostly harmless reduce/reduce error)
944         -- XXX revisit audreyt
945         | constr_stuff_record '::' sigtype
946                 { let (con,details) = unLoc $1 in 
947                   LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
948 {-
949         | forall context '=>' constr_stuff_record '::' sigtype
950                 { let (con,details) = unLoc $4 in 
951                   LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
952         | forall constr_stuff_record '::' sigtype
953                 { let (con,details) = unLoc $2 in 
954                   LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
955 -}
956
957
958 constrs :: { Located [LConDecl RdrName] }
959         : {- empty; a GHC extension -}  { noLoc [] }
960         | '=' constrs1                  { LL (unLoc $2) }
961
962 constrs1 :: { Located [LConDecl RdrName] }
963         : constrs1 '|' constr           { LL ($3 : unLoc $1) }
964         | constr                        { L1 [$1] }
965
966 constr :: { LConDecl RdrName }
967         : forall context '=>' constr_stuff      
968                 { let (con,details) = unLoc $4 in 
969                   LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
970         | forall constr_stuff
971                 { let (con,details) = unLoc $2 in 
972                   LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
973
974 forall :: { Located [LHsTyVarBndr RdrName] }
975         : 'forall' tv_bndrs '.'         { LL $2 }
976         | {- empty -}                   { noLoc [] }
977
978 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
979 -- We parse the constructor declaration 
980 --      C t1 t2
981 -- as a btype (treating C as a type constructor) and then convert C to be
982 -- a data constructor.  Reason: it might continue like this:
983 --      C t1 t2 %: D Int
984 -- in which case C really would be a type constructor.  We can't resolve this
985 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
986         : btype                         {% mkPrefixCon $1 [] >>= return.LL }
987         | oqtycon '{' '}'               {% mkRecCon $1 [] >>= return.LL }
988         | oqtycon '{' fielddecls '}'    {% mkRecCon $1 $3 >>= return.LL }
989         | btype conop btype             { LL ($2, InfixCon $1 $3) }
990
991 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
992         : oqtycon '{' '}'               {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
993         | oqtycon '{' fielddecls '}'    {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
994
995 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
996         : fielddecl ',' fielddecls      { unLoc $1 : $3 }
997         | fielddecl                     { [unLoc $1] }
998
999 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
1000         : sig_vars '::' ctype           { LL (reverse (unLoc $1), $3) }
1001
1002 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1003 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1004 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1005 -- We don't allow a context, but that's sorted out by the type checker.
1006 deriving :: { Located (Maybe [LHsType RdrName]) }
1007         : {- empty -}                           { noLoc Nothing }
1008         | 'deriving' qtycon     {% do { let { L loc tv = $2 }
1009                                       ; p <- checkInstType (L loc (HsTyVar tv))
1010                                       ; return (LL (Just [p])) } }
1011         | 'deriving' '(' ')'                    { LL (Just []) }
1012         | 'deriving' '(' inst_types1 ')'        { LL (Just $3) }
1013              -- Glasgow extension: allow partial 
1014              -- applications in derivings
1015
1016 -----------------------------------------------------------------------------
1017 -- Value definitions
1018
1019 {- There's an awkward overlap with a type signature.  Consider
1020         f :: Int -> Int = ...rhs...
1021    Then we can't tell whether it's a type signature or a value
1022    definition with a result signature until we see the '='.
1023    So we have to inline enough to postpone reductions until we know.
1024 -}
1025
1026 {-
1027   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1028   instead of qvar, we get another shift/reduce-conflict. Consider the
1029   following programs:
1030   
1031      { (^^) :: Int->Int ; }          Type signature; only var allowed
1032
1033      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
1034                                      qvar allowed (because of instance decls)
1035   
1036   We can't tell whether to reduce var to qvar until after we've read the signatures.
1037 -}
1038
1039 decl    :: { Located (OrdList (LHsDecl RdrName)) }
1040         : sigdecl                       { $1 }
1041         | '!' infixexp rhs              {% do { pat <- checkPattern $2;
1042                                                 return (LL $ unitOL $ LL $ ValD $ 
1043                                                         PatBind (LL $ BangPat pat) (unLoc $3)
1044                                                                 placeHolderType placeHolderNames) } }
1045         | infixexp opt_sig rhs          {% do { r <- checkValDef $1 $2 $3;
1046                                                 return (LL $ unitOL (LL $ ValD r)) } }
1047
1048 rhs     :: { Located (GRHSs RdrName) }
1049         : '=' exp wherebinds    { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1050         | gdrhs wherebinds      { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1051
1052 gdrhs :: { Located [LGRHS RdrName] }
1053         : gdrhs gdrh            { LL ($2 : unLoc $1) }
1054         | gdrh                  { L1 [$1] }
1055
1056 gdrh :: { LGRHS RdrName }
1057         : '|' quals '=' exp     { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1058
1059 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1060         : infixexp '::' sigtype
1061                                 {% do s <- checkValSig $1 $3; 
1062                                       return (LL $ unitOL (LL $ SigD s)) }
1063                 -- See the above notes for why we need infixexp here
1064         | var ',' sig_vars '::' sigtype 
1065                                 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1066         | infix prec ops        { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1067                                              | n <- unLoc $3 ] }
1068         | '{-# INLINE'   activation qvar '#-}'        
1069                                 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1070         | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1071                                 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1072                                             | t <- $4] }
1073         | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1074                                 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1075                                             | t <- $5] }
1076         | '{-# SPECIALISE' 'instance' inst_type '#-}'
1077                                 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1078
1079 -----------------------------------------------------------------------------
1080 -- Expressions
1081
1082 exp   :: { LHsExpr RdrName }
1083         : infixexp '::' sigtype         { LL $ ExprWithTySig $1 $3 }
1084         | infixexp '-<' exp             { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1085         | infixexp '>-' exp             { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1086         | infixexp '-<<' exp            { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1087         | infixexp '>>-' exp            { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1088         | infixexp                      { $1 }
1089
1090 infixexp :: { LHsExpr RdrName }
1091         : exp10                         { $1 }
1092         | infixexp qop exp10            { LL (OpApp $1 $2 (panic "fixity") $3) }
1093
1094 exp10 :: { LHsExpr RdrName }
1095         : '\\' aexp aexps opt_asig '->' exp     
1096                         {% checkPatterns ($2 : reverse $3) >>= \ ps -> 
1097                            return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1098                                             (GRHSs (unguardedRHS $6) emptyLocalBinds
1099                                                         )])) }
1100         | 'let' binds 'in' exp                  { LL $ HsLet (unLoc $2) $4 }
1101         | 'if' exp 'then' exp 'else' exp        { LL $ HsIf $2 $4 $6 }
1102         | 'case' exp 'of' altslist              { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1103         | '-' fexp                              { LL $ mkHsNegApp $2 }
1104
1105         | 'do' stmtlist                 {% let loc = comb2 $1 $2 in
1106                                            checkDo loc (unLoc $2)  >>= \ (stmts,body) ->
1107                                            return (L loc (mkHsDo DoExpr stmts body)) }
1108         | 'mdo' stmtlist                {% let loc = comb2 $1 $2 in
1109                                            checkDo loc (unLoc $2)  >>= \ (stmts,body) ->
1110                                            return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1111         | scc_annot exp                         { LL $ if opt_SccProfilingOn
1112                                                         then HsSCC (unLoc $1) $2
1113                                                         else HsPar $2 }
1114
1115         | 'proc' aexp '->' exp  
1116                         {% checkPattern $2 >>= \ p -> 
1117                            return (LL $ HsProc p (LL $ HsCmdTop $4 [] 
1118                                                    placeHolderType undefined)) }
1119                                                 -- TODO: is LL right here?
1120
1121         | '{-# CORE' STRING '#-}' exp           { LL $ HsCoreAnn (getSTRING $2) $4 }
1122                                                     -- hdaume: core annotation
1123         | fexp                                  { $1 }
1124
1125 scc_annot :: { Located FastString }
1126         : '_scc_' STRING                        { LL $ getSTRING $2 }
1127         | '{-# SCC' STRING '#-}'                { LL $ getSTRING $2 }
1128
1129 fexp    :: { LHsExpr RdrName }
1130         : fexp aexp                             { LL $ HsApp $1 $2 }
1131         | aexp                                  { $1 }
1132
1133 aexps   :: { [LHsExpr RdrName] }
1134         : aexps aexp                            { $2 : $1 }
1135         | {- empty -}                           { [] }
1136
1137 aexp    :: { LHsExpr RdrName }
1138         : qvar '@' aexp                 { LL $ EAsPat $1 $3 }
1139         | '~' aexp                      { LL $ ELazyPat $2 }
1140 --      | '!' aexp                      { LL $ EBangPat $2 }
1141         | aexp1                         { $1 }
1142
1143 aexp1   :: { LHsExpr RdrName }
1144         : aexp1 '{' fbinds '}'  {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) 
1145                                                         (reverse $3);
1146                                         return (LL r) }}
1147         | aexp2                 { $1 }
1148
1149 -- Here was the syntax for type applications that I was planning
1150 -- but there are difficulties (e.g. what order for type args)
1151 -- so it's not enabled yet.
1152 -- But this case *is* used for the left hand side of a generic definition,
1153 -- which is parsed as an expression before being munged into a pattern
1154         | qcname '{|' gentype '|}'      { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1155                                                      (sL (getLoc $3) (HsType $3)) }
1156
1157 aexp2   :: { LHsExpr RdrName }
1158         : ipvar                         { L1 (HsIPVar $! unLoc $1) }
1159         | qcname                        { L1 (HsVar   $! unLoc $1) }
1160         | literal                       { L1 (HsLit   $! unLoc $1) }
1161         | INTEGER                       { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1162         | RATIONAL                      { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1163         | '(' exp ')'                   { LL (HsPar $2) }
1164         | '(' texp ',' texps ')'        { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1165         | '(#' texps '#)'               { LL $ ExplicitTuple (reverse $2)      Unboxed }
1166         | '[' list ']'                  { LL (unLoc $2) }
1167         | '[:' parr ':]'                { LL (unLoc $2) }
1168         | '(' infixexp qop ')'          { LL $ SectionL $2 $3 }
1169         | '(' qopm infixexp ')'         { LL $ SectionR $2 $3 }
1170         | '_'                           { L1 EWildPat }
1171         
1172         -- Template Haskell Extension
1173         | TH_ID_SPLICE          { L1 $ HsSpliceE (mkHsSplice 
1174                                         (L1 $ HsVar (mkUnqual varName 
1175                                                         (getTH_ID_SPLICE $1)))) } -- $x
1176         | '$(' exp ')'          { LL $ HsSpliceE (mkHsSplice $2) }               -- $( exp )
1177
1178         | TH_VAR_QUOTE qvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1179         | TH_VAR_QUOTE qcon     { LL $ HsBracket (VarBr (unLoc $2)) }
1180         | TH_TY_QUOTE tyvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1181         | TH_TY_QUOTE gtycon    { LL $ HsBracket (VarBr (unLoc $2)) }
1182         | '[|' exp '|]'         { LL $ HsBracket (ExpBr $2) }                       
1183         | '[t|' ctype '|]'      { LL $ HsBracket (TypBr $2) }                       
1184         | '[p|' infixexp '|]'   {% checkPattern $2 >>= \p ->
1185                                            return (LL $ HsBracket (PatBr p)) }
1186         | '[d|' cvtopbody '|]'  { LL $ HsBracket (DecBr (mkGroup $2)) }
1187
1188         -- arrow notation extension
1189         | '(|' aexp2 cmdargs '|)'       { LL $ HsArrForm $2 Nothing (reverse $3) }
1190
1191 cmdargs :: { [LHsCmdTop RdrName] }
1192         : cmdargs acmd                  { $2 : $1 }
1193         | {- empty -}                   { [] }
1194
1195 acmd    :: { LHsCmdTop RdrName }
1196         : aexp2                 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1197
1198 cvtopbody :: { [LHsDecl RdrName] }
1199         :  '{'            cvtopdecls0 '}'               { $2 }
1200         |      vocurly    cvtopdecls0 close             { $2 }
1201
1202 cvtopdecls0 :: { [LHsDecl RdrName] }
1203         : {- empty -}           { [] }
1204         | cvtopdecls            { $1 }
1205
1206 texp :: { LHsExpr RdrName }
1207         : exp                           { $1 }
1208         | qopm infixexp                 { LL $ SectionR $1 $2 }
1209         -- The second production is really here only for bang patterns
1210         -- but 
1211
1212 texps :: { [LHsExpr RdrName] }
1213         : texps ',' texp                { $3 : $1 }
1214         | texp                          { [$1] }
1215
1216
1217 -----------------------------------------------------------------------------
1218 -- List expressions
1219
1220 -- The rules below are little bit contorted to keep lexps left-recursive while
1221 -- avoiding another shift/reduce-conflict.
1222
1223 list :: { LHsExpr RdrName }
1224         : texp                  { L1 $ ExplicitList placeHolderType [$1] }
1225         | lexps                 { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1226         | texp '..'             { LL $ ArithSeq noPostTcExpr (From $1) }
1227         | texp ',' exp '..'     { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1228         | texp '..' exp         { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1229         | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1230         | texp pquals           { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1231
1232 lexps :: { Located [LHsExpr RdrName] }
1233         : lexps ',' texp                { LL ($3 : unLoc $1) }
1234         | texp ',' texp                 { LL [$3,$1] }
1235
1236 -----------------------------------------------------------------------------
1237 -- List Comprehensions
1238
1239 pquals :: { Located [LStmt RdrName] }   -- Either a singleton ParStmt, 
1240                                         -- or a reversed list of Stmts
1241         : pquals1                       { case unLoc $1 of
1242                                             [qs] -> L1 qs
1243                                             qss  -> L1 [L1 (ParStmt stmtss)]
1244                                                  where
1245                                                     stmtss = [ (reverse qs, undefined) 
1246                                                              | qs <- qss ]
1247                                         }
1248                         
1249 pquals1 :: { Located [[LStmt RdrName]] }
1250         : pquals1 '|' quals             { LL (unLoc $3 : unLoc $1) }
1251         | '|' quals                     { L (getLoc $2) [unLoc $2] }
1252
1253 quals :: { Located [LStmt RdrName] }
1254         : quals ',' qual                { LL ($3 : unLoc $1) }
1255         | qual                          { L1 [$1] }
1256
1257 -----------------------------------------------------------------------------
1258 -- Parallel array expressions
1259
1260 -- The rules below are little bit contorted; see the list case for details.
1261 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1262 -- Moreover, we allow explicit arrays with no element (represented by the nil
1263 -- constructor in the list case).
1264
1265 parr :: { LHsExpr RdrName }
1266         :                               { noLoc (ExplicitPArr placeHolderType []) }
1267         | exp                           { L1 $ ExplicitPArr placeHolderType [$1] }
1268         | lexps                         { L1 $ ExplicitPArr placeHolderType 
1269                                                        (reverse (unLoc $1)) }
1270         | exp '..' exp                  { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1271         | exp ',' exp '..' exp          { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1272         | exp pquals                    { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1273
1274 -- We are reusing `lexps' and `pquals' from the list case.
1275
1276 -----------------------------------------------------------------------------
1277 -- Case alternatives
1278
1279 altslist :: { Located [LMatch RdrName] }
1280         : '{'            alts '}'       { LL (reverse (unLoc $2)) }
1281         |     vocurly    alts  close    { L (getLoc $2) (reverse (unLoc $2)) }
1282
1283 alts    :: { Located [LMatch RdrName] }
1284         : alts1                         { L1 (unLoc $1) }
1285         | ';' alts                      { LL (unLoc $2) }
1286
1287 alts1   :: { Located [LMatch RdrName] }
1288         : alts1 ';' alt                 { LL ($3 : unLoc $1) }
1289         | alts1 ';'                     { LL (unLoc $1) }
1290         | alt                           { L1 [$1] }
1291
1292 alt     :: { LMatch RdrName }
1293         : infixexp opt_sig alt_rhs      {%  checkPattern $1 >>= \p ->
1294                                             return (LL (Match [p] $2 (unLoc $3))) }
1295         | '!' infixexp opt_sig alt_rhs  {%  checkPattern $2 >>= \p ->
1296                                             return (LL (Match [LL $ BangPat p] $3 (unLoc $4))) }
1297
1298 alt_rhs :: { Located (GRHSs RdrName) }
1299         : ralt wherebinds               { LL (GRHSs (unLoc $1) (unLoc $2)) }
1300
1301 ralt :: { Located [LGRHS RdrName] }
1302         : '->' exp                      { LL (unguardedRHS $2) }
1303         | gdpats                        { L1 (reverse (unLoc $1)) }
1304
1305 gdpats :: { Located [LGRHS RdrName] }
1306         : gdpats gdpat                  { LL ($2 : unLoc $1) }
1307         | gdpat                         { L1 [$1] }
1308
1309 gdpat   :: { LGRHS RdrName }
1310         : '|' quals '->' exp            { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1311
1312 -----------------------------------------------------------------------------
1313 -- Statement sequences
1314
1315 stmtlist :: { Located [LStmt RdrName] }
1316         : '{'           stmts '}'       { LL (unLoc $2) }
1317         |     vocurly   stmts close     { $2 }
1318
1319 --      do { ;; s ; s ; ; s ;; }
1320 -- The last Stmt should be an expression, but that's hard to enforce
1321 -- here, because we need too much lookahead if we see do { e ; }
1322 -- So we use ExprStmts throughout, and switch the last one over
1323 -- in ParseUtils.checkDo instead
1324 stmts :: { Located [LStmt RdrName] }
1325         : stmt stmts_help               { LL ($1 : unLoc $2) }
1326         | ';' stmts                     { LL (unLoc $2) }
1327         | {- empty -}                   { noLoc [] }
1328
1329 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1330         : ';' stmts                     { LL (unLoc $2) }
1331         | {- empty -}                   { noLoc [] }
1332
1333 -- For typing stmts at the GHCi prompt, where 
1334 -- the input may consist of just comments.
1335 maybe_stmt :: { Maybe (LStmt RdrName) }
1336         : stmt                          { Just $1 }
1337         | {- nothing -}                 { Nothing }
1338
1339 stmt  :: { LStmt RdrName }
1340         : qual                          { $1 }
1341         | infixexp '->' exp             {% checkPattern $3 >>= \p ->
1342                                            return (LL $ mkBindStmt p $1) }
1343         | 'rec' stmtlist                { LL $ mkRecStmt (unLoc $2) }
1344
1345 qual  :: { LStmt RdrName }
1346         : exp '<-' exp                  {% checkPattern $1 >>= \p ->
1347                                            return (LL $ mkBindStmt p $3) }
1348         | exp                           { L1 $ mkExprStmt $1 }
1349         | 'let' binds                   { LL $ LetStmt (unLoc $2) }
1350
1351 -----------------------------------------------------------------------------
1352 -- Record Field Update/Construction
1353
1354 fbinds  :: { HsRecordBinds RdrName }
1355         : fbinds1                       { $1 }
1356         | {- empty -}                   { [] }
1357
1358 fbinds1 :: { HsRecordBinds RdrName }
1359         : fbinds1 ',' fbind             { $3 : $1 }
1360         | fbind                         { [$1] }
1361   
1362 fbind   :: { (Located RdrName, LHsExpr RdrName) }
1363         : qvar '=' exp                  { ($1,$3) }
1364
1365 -----------------------------------------------------------------------------
1366 -- Implicit Parameter Bindings
1367
1368 dbinds  :: { Located [LIPBind RdrName] }
1369         : dbinds ';' dbind              { LL ($3 : unLoc $1) }
1370         | dbinds ';'                    { LL (unLoc $1) }
1371         | dbind                         { L1 [$1] }
1372 --      | {- empty -}                   { [] }
1373
1374 dbind   :: { LIPBind RdrName }
1375 dbind   : ipvar '=' exp                 { LL (IPBind (unLoc $1) $3) }
1376
1377 ipvar   :: { Located (IPName RdrName) }
1378         : IPDUPVARID            { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1379         | IPSPLITVARID          { L1 (Linear  (mkUnqual varName (getIPSPLITVARID $1))) }
1380
1381 -----------------------------------------------------------------------------
1382 -- Deprecations
1383
1384 depreclist :: { Located [RdrName] }
1385 depreclist : deprec_var                 { L1 [unLoc $1] }
1386            | deprec_var ',' depreclist  { LL (unLoc $1 : unLoc $3) }
1387
1388 deprec_var :: { Located RdrName }
1389 deprec_var : var                        { $1 }
1390            | con                        { $1 }
1391
1392 -----------------------------------------
1393 -- Data constructors
1394 qcon    :: { Located RdrName }
1395         : qconid                { $1 }
1396         | '(' qconsym ')'       { LL (unLoc $2) }
1397         | sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1398 -- The case of '[:' ':]' is part of the production `parr'
1399
1400 con     :: { Located RdrName }
1401         : conid                 { $1 }
1402         | '(' consym ')'        { LL (unLoc $2) }
1403         | sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1404
1405 sysdcon :: { Located DataCon }  -- Wired in data constructors
1406         : '(' ')'               { LL unitDataCon }
1407         | '(' commas ')'        { LL $ tupleCon Boxed $2 }
1408         | '[' ']'               { LL nilDataCon }
1409
1410 conop :: { Located RdrName }
1411         : consym                { $1 }  
1412         | '`' conid '`'         { LL (unLoc $2) }
1413
1414 qconop :: { Located RdrName }
1415         : qconsym               { $1 }
1416         | '`' qconid '`'        { LL (unLoc $2) }
1417
1418 -----------------------------------------------------------------------------
1419 -- Type constructors
1420
1421 gtycon  :: { Located RdrName }  -- A "general" qualified tycon
1422         : oqtycon                       { $1 }
1423         | '(' ')'                       { LL $ getRdrName unitTyCon }
1424         | '(' commas ')'                { LL $ getRdrName (tupleTyCon Boxed $2) }
1425         | '(' '->' ')'                  { LL $ getRdrName funTyCon }
1426         | '[' ']'                       { LL $ listTyCon_RDR }
1427         | '[:' ':]'                     { LL $ parrTyCon_RDR }
1428
1429 oqtycon :: { Located RdrName }  -- An "ordinary" qualified tycon
1430         : qtycon                        { $1 }
1431         | '(' qtyconsym ')'             { LL (unLoc $2) }
1432
1433 qtyconop :: { Located RdrName } -- Qualified or unqualified
1434         : qtyconsym                     { $1 }
1435         | '`' qtycon '`'                { LL (unLoc $2) }
1436
1437 qtycon :: { Located RdrName }   -- Qualified or unqualified
1438         : QCONID                        { L1 $! mkQual tcClsName (getQCONID $1) }
1439         | tycon                         { $1 }
1440
1441 tycon   :: { Located RdrName }  -- Unqualified
1442         : CONID                         { L1 $! mkUnqual tcClsName (getCONID $1) }
1443
1444 qtyconsym :: { Located RdrName }
1445         : QCONSYM                       { L1 $! mkQual tcClsName (getQCONSYM $1) }
1446         | tyconsym                      { $1 }
1447
1448 tyconsym :: { Located RdrName }
1449         : CONSYM                        { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1450
1451 -----------------------------------------------------------------------------
1452 -- Operators
1453
1454 op      :: { Located RdrName }   -- used in infix decls
1455         : varop                 { $1 }
1456         | conop                 { $1 }
1457
1458 varop   :: { Located RdrName }
1459         : varsym                { $1 }
1460         | '`' varid '`'         { LL (unLoc $2) }
1461
1462 qop     :: { LHsExpr RdrName }   -- used in sections
1463         : qvarop                { L1 $ HsVar (unLoc $1) }
1464         | qconop                { L1 $ HsVar (unLoc $1) }
1465
1466 qopm    :: { LHsExpr RdrName }   -- used in sections
1467         : qvaropm               { L1 $ HsVar (unLoc $1) }
1468         | qconop                { L1 $ HsVar (unLoc $1) }
1469
1470 qvarop :: { Located RdrName }
1471         : qvarsym               { $1 }
1472         | '`' qvarid '`'        { LL (unLoc $2) }
1473
1474 qvaropm :: { Located RdrName }
1475         : qvarsym_no_minus      { $1 }
1476         | '`' qvarid '`'        { LL (unLoc $2) }
1477
1478 -----------------------------------------------------------------------------
1479 -- Type variables
1480
1481 tyvar   :: { Located RdrName }
1482 tyvar   : tyvarid               { $1 }
1483         | '(' tyvarsym ')'      { LL (unLoc $2) }
1484
1485 tyvarop :: { Located RdrName }
1486 tyvarop : '`' tyvarid '`'       { LL (unLoc $2) }
1487         | tyvarsym              { $1 }
1488
1489 tyvarid :: { Located RdrName }
1490         : VARID                 { L1 $! mkUnqual tvName (getVARID $1) }
1491         | special_id            { L1 $! mkUnqual tvName (unLoc $1) }
1492         | 'unsafe'              { L1 $! mkUnqual tvName FSLIT("unsafe") }
1493         | 'safe'                { L1 $! mkUnqual tvName FSLIT("safe") }
1494         | 'threadsafe'          { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1495
1496 tyvarsym :: { Located RdrName }
1497 -- Does not include "!", because that is used for strictness marks
1498 --               or ".", because that separates the quantified type vars from the rest
1499 --               or "*", because that's used for kinds
1500 tyvarsym : VARSYM               { L1 $! mkUnqual tvName (getVARSYM $1) }
1501
1502 -----------------------------------------------------------------------------
1503 -- Variables 
1504
1505 var     :: { Located RdrName }
1506         : varid                 { $1 }
1507         | '(' varsym ')'        { LL (unLoc $2) }
1508
1509 qvar    :: { Located RdrName }
1510         : qvarid                { $1 }
1511         | '(' varsym ')'        { LL (unLoc $2) }
1512         | '(' qvarsym1 ')'      { LL (unLoc $2) }
1513 -- We've inlined qvarsym here so that the decision about
1514 -- whether it's a qvar or a var can be postponed until
1515 -- *after* we see the close paren.
1516
1517 qvarid :: { Located RdrName }
1518         : varid                 { $1 }
1519         | QVARID                { L1 $ mkQual varName (getQVARID $1) }
1520
1521 varid :: { Located RdrName }
1522         : varid_no_unsafe       { $1 }
1523         | 'unsafe'              { L1 $! mkUnqual varName FSLIT("unsafe") }
1524         | 'safe'                { L1 $! mkUnqual varName FSLIT("safe") }
1525         | 'threadsafe'          { L1 $! mkUnqual varName FSLIT("threadsafe") }
1526
1527 varid_no_unsafe :: { Located RdrName }
1528         : VARID                 { L1 $! mkUnqual varName (getVARID $1) }
1529         | special_id            { L1 $! mkUnqual varName (unLoc $1) }
1530         | 'forall'              { L1 $! mkUnqual varName FSLIT("forall") }
1531         | 'iso'                 { L1 $! mkUnqual varName FSLIT("iso") }
1532         | 'family'              { L1 $! mkUnqual varName FSLIT("family") }
1533
1534 qvarsym :: { Located RdrName }
1535         : varsym                { $1 }
1536         | qvarsym1              { $1 }
1537
1538 qvarsym_no_minus :: { Located RdrName }
1539         : varsym_no_minus       { $1 }
1540         | qvarsym1              { $1 }
1541
1542 qvarsym1 :: { Located RdrName }
1543 qvarsym1 : QVARSYM              { L1 $ mkQual varName (getQVARSYM $1) }
1544
1545 varsym :: { Located RdrName }
1546         : varsym_no_minus       { $1 }
1547         | '-'                   { L1 $ mkUnqual varName FSLIT("-") }
1548
1549 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1550         : VARSYM                { L1 $ mkUnqual varName (getVARSYM $1) }
1551         | special_sym           { L1 $ mkUnqual varName (unLoc $1) }
1552
1553
1554 -- These special_ids are treated as keywords in various places, 
1555 -- but as ordinary ids elsewhere.   'special_id' collects all these
1556 -- except 'unsafe', 'forall', 'family', and 'iso' whose treatment differs
1557 -- depending on context 
1558 special_id :: { Located FastString }
1559 special_id
1560         : 'as'                  { L1 FSLIT("as") }
1561         | 'qualified'           { L1 FSLIT("qualified") }
1562         | 'hiding'              { L1 FSLIT("hiding") }
1563         | 'export'              { L1 FSLIT("export") }
1564         | 'label'               { L1 FSLIT("label")  }
1565         | 'dynamic'             { L1 FSLIT("dynamic") }
1566         | 'stdcall'             { L1 FSLIT("stdcall") }
1567         | 'ccall'               { L1 FSLIT("ccall") }
1568
1569 special_sym :: { Located FastString }
1570 special_sym : '!'       { L1 FSLIT("!") }
1571             | '.'       { L1 FSLIT(".") }
1572             | '*'       { L1 FSLIT("*") }
1573
1574 -----------------------------------------------------------------------------
1575 -- Data constructors
1576
1577 qconid :: { Located RdrName }   -- Qualified or unqualified
1578         : conid                 { $1 }
1579         | QCONID                { L1 $ mkQual dataName (getQCONID $1) }
1580
1581 conid   :: { Located RdrName }
1582         : CONID                 { L1 $ mkUnqual dataName (getCONID $1) }
1583
1584 qconsym :: { Located RdrName }  -- Qualified or unqualified
1585         : consym                { $1 }
1586         | QCONSYM               { L1 $ mkQual dataName (getQCONSYM $1) }
1587
1588 consym :: { Located RdrName }
1589         : CONSYM                { L1 $ mkUnqual dataName (getCONSYM $1) }
1590
1591         -- ':' means only list cons
1592         | ':'                   { L1 $ consDataCon_RDR }
1593
1594
1595 -----------------------------------------------------------------------------
1596 -- Literals
1597
1598 literal :: { Located HsLit }
1599         : CHAR                  { L1 $ HsChar       $ getCHAR $1 }
1600         | STRING                { L1 $ HsString     $ getSTRING $1 }
1601         | PRIMINTEGER           { L1 $ HsIntPrim    $ getPRIMINTEGER $1 }
1602         | PRIMCHAR              { L1 $ HsCharPrim   $ getPRIMCHAR $1 }
1603         | PRIMSTRING            { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1604         | PRIMFLOAT             { L1 $ HsFloatPrim  $ getPRIMFLOAT $1 }
1605         | PRIMDOUBLE            { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1606
1607 -----------------------------------------------------------------------------
1608 -- Layout
1609
1610 close :: { () }
1611         : vccurly               { () } -- context popped in lexer.
1612         | error                 {% popContext }
1613
1614 -----------------------------------------------------------------------------
1615 -- Miscellaneous (mostly renamings)
1616
1617 modid   :: { Located ModuleName }
1618         : CONID                 { L1 $ mkModuleNameFS (getCONID $1) }
1619         | QCONID                { L1 $ let (mod,c) = getQCONID $1 in
1620                                   mkModuleNameFS
1621                                    (mkFastString
1622                                      (unpackFS mod ++ '.':unpackFS c))
1623                                 }
1624
1625 commas :: { Int }
1626         : commas ','                    { $1 + 1 }
1627         | ','                           { 2 }
1628
1629 -----------------------------------------------------------------------------
1630
1631 {
1632 happyError :: P a
1633 happyError = srcParseFail
1634
1635 getVARID        (L _ (ITvarid    x)) = x
1636 getCONID        (L _ (ITconid    x)) = x
1637 getVARSYM       (L _ (ITvarsym   x)) = x
1638 getCONSYM       (L _ (ITconsym   x)) = x
1639 getQVARID       (L _ (ITqvarid   x)) = x
1640 getQCONID       (L _ (ITqconid   x)) = x
1641 getQVARSYM      (L _ (ITqvarsym  x)) = x
1642 getQCONSYM      (L _ (ITqconsym  x)) = x
1643 getIPDUPVARID   (L _ (ITdupipvarid   x)) = x
1644 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1645 getCHAR         (L _ (ITchar     x)) = x
1646 getSTRING       (L _ (ITstring   x)) = x
1647 getINTEGER      (L _ (ITinteger  x)) = x
1648 getRATIONAL     (L _ (ITrational x)) = x
1649 getPRIMCHAR     (L _ (ITprimchar   x)) = x
1650 getPRIMSTRING   (L _ (ITprimstring x)) = x
1651 getPRIMINTEGER  (L _ (ITprimint    x)) = x
1652 getPRIMFLOAT    (L _ (ITprimfloat  x)) = x
1653 getPRIMDOUBLE   (L _ (ITprimdouble x)) = x
1654 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1655 getINLINE       (L _ (ITinline_prag b)) = b
1656 getSPEC_INLINE  (L _ (ITspec_inline_prag b)) = b
1657
1658 -- Utilities for combining source spans
1659 comb2 :: Located a -> Located b -> SrcSpan
1660 comb2 = combineLocs
1661
1662 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1663 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1664
1665 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1666 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1667                 combineSrcSpans (getLoc c) (getLoc d)
1668
1669 -- strict constructor version:
1670 {-# INLINE sL #-}
1671 sL :: SrcSpan -> a -> Located a
1672 sL span a = span `seq` L span a
1673
1674 -- Make a source location for the file.  We're a bit lazy here and just
1675 -- make a point SrcSpan at line 1, column 0.  Strictly speaking we should
1676 -- try to find the span of the whole file (ToDo).
1677 fileSrcSpan :: P SrcSpan
1678 fileSrcSpan = do 
1679   l <- getSrcLoc; 
1680   let loc = mkSrcLoc (srcLocFile l) 1 0;
1681   return (mkSrcSpan loc loc)
1682 }