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