Add a new flag XDefaultSignatures to enable just the signatures on the default method...
[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 {-# LANGUAGE BangPatterns #-} -- required for versions of Happy before 1.18.6
12 {-# OPTIONS -Wwarn -w #-}
13 -- The above warning supression flag is a temporary kludge.
14 -- While working on this module you are encouraged to remove it and fix
15 -- any warnings in the module. See
16 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
17 -- for details
18
19 {-# OPTIONS_GHC -O0 -fno-ignore-interface-pragmas #-}
20 {-
21 Careful optimisation of the parser: we don't want to throw everything
22 at it, because that takes too long and doesn't buy much, but we do want
23 to inline certain key external functions, so we instruct GHC not to
24 throw away inlinings as it would normally do in -O0 mode.
25 -}
26
27 module Parser ( parseModule, parseStmt, parseIdentifier, parseType,
28                 parseHeader ) where
29
30 import HsSyn
31 import RdrHsSyn
32 import HscTypes         ( IsBootInterface, WarningTxt(..) )
33 import Lexer
34 import RdrName
35 import TysWiredIn       ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
36                           unboxedSingletonTyCon, unboxedSingletonDataCon,
37                           listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
38 import Type             ( funTyCon )
39 import ForeignCall      ( Safety(..), CExportSpec(..), CLabelString,
40                           CCallConv(..), CCallTarget(..), defaultCCallConv
41                         )
42 import OccName          ( varName, dataName, tcClsName, tvName )
43 import DataCon          ( DataCon, dataConName )
44 import SrcLoc           ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
45                           SrcSpan, combineLocs, srcLocFile, 
46                           mkSrcLoc, mkSrcSpan )
47 import Module
48 import StaticFlags      ( opt_SccProfilingOn, opt_Hpc )
49 import Type             ( Kind, liftedTypeKind, unliftedTypeKind )
50 import Coercion         ( mkArrowKind )
51 import Class            ( FunDep )
52 import BasicTypes
53 import DynFlags
54 import OrdList
55 import HaddockUtils
56
57 import FastString
58 import Maybes           ( orElse )
59 import Outputable
60
61 import Control.Monad    ( unless )
62 import GHC.Exts
63 import Data.Char
64 import Control.Monad    ( mplus )
65 }
66
67 {-
68 -----------------------------------------------------------------------------
69 24 Februar 2006
70
71 Conflicts: 33 shift/reduce
72            1 reduce/reduce
73
74 The reduce/reduce conflict is weird.  It's between tyconsym and consym, and I
75 would think the two should never occur in the same context.
76
77   -=chak
78
79 -----------------------------------------------------------------------------
80 31 December 2006
81
82 Conflicts: 34 shift/reduce
83            1 reduce/reduce
84
85 The reduce/reduce conflict is weird.  It's between tyconsym and consym, and I
86 would think the two should never occur in the same context.
87
88   -=chak
89
90 -----------------------------------------------------------------------------
91 6 December 2006
92
93 Conflicts: 32 shift/reduce
94            1 reduce/reduce
95
96 The reduce/reduce conflict is weird.  It's between tyconsym and consym, and I
97 would think the two should never occur in the same context.
98
99   -=chak
100
101 -----------------------------------------------------------------------------
102 26 July 2006
103
104 Conflicts: 37 shift/reduce
105            1 reduce/reduce
106
107 The reduce/reduce conflict is weird.  It's between tyconsym and consym, and I
108 would think the two should never occur in the same context.
109
110   -=chak
111
112 -----------------------------------------------------------------------------
113 Conflicts: 38 shift/reduce (1.25)
114
115 10 for abiguity in 'if x then y else z + 1'             [State 178]
116         (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
117         10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
118
119 1 for ambiguity in 'if x then y else z :: T'            [State 178]
120         (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
121
122 4 for ambiguity in 'if x then y else z -< e'            [State 178]
123         (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
124         There are four such operators: -<, >-, -<<, >>-
125
126
127 2 for ambiguity in 'case v of { x :: T -> T ... } '     [States 11, 253]
128         Which of these two is intended?
129           case v of
130             (x::T) -> T         -- Rhs is T
131     or
132           case v of
133             (x::T -> T) -> ..   -- Rhs is ...
134
135 10 for ambiguity in 'e :: a `b` c'.  Does this mean     [States 11, 253]
136         (e::a) `b` c, or 
137         (e :: (a `b` c))
138     As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
139     Same duplication between states 11 and 253 as the previous case
140
141 1 for ambiguity in 'let ?x ...'                         [State 329]
142         the parser can't tell whether the ?x is the lhs of a normal binding or
143         an implicit binding.  Fortunately resolving as shift gives it the only
144         sensible meaning, namely the lhs of an implicit binding.
145
146 1 for ambiguity in '{-# RULES "name" [ ... #-}          [State 382]
147         we don't know whether the '[' starts the activation or not: it
148         might be the start of the declaration with the activation being
149         empty.  --SDM 1/4/2002
150
151 1 for ambiguity in '{-# RULES "name" forall = ... #-}'  [State 474]
152         since 'forall' is a valid variable name, we don't know whether
153         to treat a forall on the input as the beginning of a quantifier
154         or the beginning of the rule itself.  Resolving to shift means
155         it's always treated as a quantifier, hence the above is disallowed.
156         This saves explicitly defining a grammar for the rule lhs that
157         doesn't include 'forall'.
158
159 1 for ambiguity when the source file starts with "-- | doc". We need another
160   token of lookahead to determine if a top declaration or the 'module' keyword
161   follows. Shift parses as if the 'module' keyword follows.   
162
163 -- ---------------------------------------------------------------------------
164 -- Adding location info
165
166 This is done in a stylised way using the three macros below, L0, L1
167 and LL.  Each of these macros can be thought of as having type
168
169    L0, L1, LL :: a -> Located a
170
171 They each add a SrcSpan to their argument.
172
173    L0   adds 'noSrcSpan', used for empty productions
174      -- This doesn't seem to work anymore -=chak
175
176    L1   for a production with a single token on the lhs.  Grabs the SrcSpan
177         from that token.
178
179    LL   for a production with >1 token on the lhs.  Makes up a SrcSpan from
180         the first and last tokens.
181
182 These suffice for the majority of cases.  However, we must be
183 especially careful with empty productions: LL won't work if the first
184 or last token on the lhs can represent an empty span.  In these cases,
185 we have to calculate the span using more of the tokens from the lhs, eg.
186
187         | 'newtype' tycl_hdr '=' newconstr deriving
188                 { L (comb3 $1 $4 $5)
189                     (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
190
191 We provide comb3 and comb4 functions which are useful in such cases.
192
193 Be careful: there's no checking that you actually got this right, the
194 only symptom will be that the SrcSpans of your syntax will be
195 incorrect.
196
197 /*
198  * We must expand these macros *before* running Happy, which is why this file is
199  * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
200  */
201 #define L0   L noSrcSpan
202 #define L1   sL (getLoc $1)
203 #define LL   sL (comb2 $1 $>)
204
205 -- -----------------------------------------------------------------------------
206
207 -}
208
209 %token
210  '_'            { L _ ITunderscore }            -- Haskell keywords
211  'as'           { L _ ITas }
212  'case'         { L _ ITcase }          
213  'class'        { L _ ITclass } 
214  'data'         { L _ ITdata } 
215  'default'      { L _ ITdefault }
216  'deriving'     { L _ ITderiving }
217  'do'           { L _ ITdo }
218  'else'         { L _ ITelse }
219  'hiding'       { L _ IThiding }
220  'if'           { L _ ITif }
221  'import'       { L _ ITimport }
222  'in'           { L _ ITin }
223  'infix'        { L _ ITinfix }
224  'infixl'       { L _ ITinfixl }
225  'infixr'       { L _ ITinfixr }
226  'instance'     { L _ ITinstance }
227  'let'          { L _ ITlet }
228  'module'       { L _ ITmodule }
229  'newtype'      { L _ ITnewtype }
230  'of'           { L _ ITof }
231  'qualified'    { L _ ITqualified }
232  'then'         { L _ ITthen }
233  'type'         { L _ ITtype }
234  'where'        { L _ ITwhere }
235  '_scc_'        { L _ ITscc }         -- ToDo: remove
236
237  'forall'       { L _ ITforall }                -- GHC extension keywords
238  'foreign'      { L _ ITforeign }
239  'export'       { L _ ITexport }
240  'label'        { L _ ITlabel } 
241  'dynamic'      { L _ ITdynamic }
242  'safe'         { L _ ITsafe }
243  'threadsafe'   { L _ ITthreadsafe }  -- ToDo: remove deprecated alias
244  'interruptible' { L _ ITinterruptible }
245  'unsafe'       { L _ ITunsafe }
246  'mdo'          { L _ ITmdo }
247  'family'       { L _ ITfamily }
248  'stdcall'      { L _ ITstdcallconv }
249  'ccall'        { L _ ITccallconv }
250  'prim'         { L _ ITprimcallconv }
251  'proc'         { L _ ITproc }          -- for arrow notation extension
252  'rec'          { L _ ITrec }           -- for arrow notation extension
253  'group'    { L _ ITgroup }     -- for list transform extension
254  'by'       { L _ ITby }        -- for list transform extension
255  'using'    { L _ ITusing }     -- for list transform extension
256
257  '{-# INLINE'             { L _ (ITinline_prag _ _) }
258  '{-# SPECIALISE'         { L _ ITspec_prag }
259  '{-# SPECIALISE_INLINE'  { L _ (ITspec_inline_prag _) }
260  '{-# SOURCE'      { L _ ITsource_prag }
261  '{-# RULES'       { L _ ITrules_prag }
262  '{-# CORE'        { L _ ITcore_prag }              -- hdaume: annotated core
263  '{-# SCC'         { L _ ITscc_prag }
264  '{-# GENERATED'   { L _ ITgenerated_prag }
265  '{-# DEPRECATED'  { L _ ITdeprecated_prag }
266  '{-# WARNING'     { L _ ITwarning_prag }
267  '{-# UNPACK'      { L _ ITunpack_prag }
268  '{-# ANN'         { L _ ITann_prag }
269  '{-# VECTORISE'          { L _ ITvect_prag }
270  '{-# VECTORISE_SCALAR'   { L _ ITvect_scalar_prag }
271  '#-}'             { L _ ITclose_prag }
272
273  '..'           { L _ ITdotdot }                        -- reserved symbols
274  ':'            { L _ ITcolon }
275  '::'           { L _ ITdcolon }
276  '='            { L _ ITequal }
277  '\\'           { L _ ITlam }
278  '|'            { L _ ITvbar }
279  '<-'           { L _ ITlarrow }
280  '->'           { L _ ITrarrow }
281  '@'            { L _ ITat }
282  '~'            { L _ ITtilde }
283  '=>'           { L _ ITdarrow }
284  '-'            { L _ ITminus }
285  '!'            { L _ ITbang }
286  '*'            { L _ ITstar }
287  '-<'           { L _ ITlarrowtail }            -- for arrow notation
288  '>-'           { L _ ITrarrowtail }            -- for arrow notation
289  '-<<'          { L _ ITLarrowtail }            -- for arrow notation
290  '>>-'          { L _ ITRarrowtail }            -- for arrow notation
291  '.'            { L _ ITdot }
292
293  '{'            { L _ ITocurly }                        -- special symbols
294  '}'            { L _ ITccurly }
295  '{|'           { L _ ITocurlybar }
296  '|}'           { L _ ITccurlybar }
297  vocurly        { L _ ITvocurly } -- virtual open curly (from layout)
298  vccurly        { L _ ITvccurly } -- virtual close curly (from layout)
299  '['            { L _ ITobrack }
300  ']'            { L _ ITcbrack }
301  '[:'           { L _ ITopabrack }
302  ':]'           { L _ ITcpabrack }
303  '('            { L _ IToparen }
304  ')'            { L _ ITcparen }
305  '(#'           { L _ IToubxparen }
306  '#)'           { L _ ITcubxparen }
307  '(|'           { L _ IToparenbar }
308  '|)'           { L _ ITcparenbar }
309  ';'            { L _ ITsemi }
310  ','            { L _ ITcomma }
311  '`'            { L _ ITbackquote }
312
313  VARID          { L _ (ITvarid    _) }          -- identifiers
314  CONID          { L _ (ITconid    _) }
315  VARSYM         { L _ (ITvarsym   _) }
316  CONSYM         { L _ (ITconsym   _) }
317  QVARID         { L _ (ITqvarid   _) }
318  QCONID         { L _ (ITqconid   _) }
319  QVARSYM        { L _ (ITqvarsym  _) }
320  QCONSYM        { L _ (ITqconsym  _) }
321  PREFIXQVARSYM  { L _ (ITprefixqvarsym  _) }
322  PREFIXQCONSYM  { L _ (ITprefixqconsym  _) }
323
324  IPDUPVARID     { L _ (ITdupipvarid   _) }              -- GHC extension
325
326  CHAR           { L _ (ITchar     _) }
327  STRING         { L _ (ITstring   _) }
328  INTEGER        { L _ (ITinteger  _) }
329  RATIONAL       { L _ (ITrational _) }
330                     
331  PRIMCHAR       { L _ (ITprimchar   _) }
332  PRIMSTRING     { L _ (ITprimstring _) }
333  PRIMINTEGER    { L _ (ITprimint    _) }
334  PRIMWORD       { L _ (ITprimword  _) }
335  PRIMFLOAT      { L _ (ITprimfloat  _) }
336  PRIMDOUBLE     { L _ (ITprimdouble _) }
337
338  DOCNEXT        { L _ (ITdocCommentNext _) }
339  DOCPREV        { L _ (ITdocCommentPrev _) }
340  DOCNAMED       { L _ (ITdocCommentNamed _) }
341  DOCSECTION     { L _ (ITdocSection _ _) }
342
343 -- Template Haskell 
344 '[|'            { L _ ITopenExpQuote  }       
345 '[p|'           { L _ ITopenPatQuote  }      
346 '[t|'           { L _ ITopenTypQuote  }      
347 '[d|'           { L _ ITopenDecQuote  }      
348 '|]'            { L _ ITcloseQuote    }
349 TH_ID_SPLICE    { L _ (ITidEscape _)  }     -- $x
350 '$('            { L _ ITparenEscape   }     -- $( exp )
351 TH_VAR_QUOTE    { L _ ITvarQuote      }     -- 'x
352 TH_TY_QUOTE     { L _ ITtyQuote       }      -- ''T
353 TH_QUASIQUOTE   { L _ (ITquasiQuote _) }
354
355 %monad { P } { >>= } { return }
356 %lexer { lexer } { L _ ITeof }
357 %name parseModule module
358 %name parseStmt   maybe_stmt
359 %name parseIdentifier  identifier
360 %name parseType ctype
361 %partial parseHeader header
362 %tokentype { (Located Token) }
363 %%
364
365 -----------------------------------------------------------------------------
366 -- Identifiers; one of the entry points
367 identifier :: { Located RdrName }
368         : qvar                          { $1 }
369         | qcon                          { $1 }
370         | qvarop                        { $1 }
371         | qconop                        { $1 }
372     | '(' '->' ')'      { LL $ getRdrName funTyCon }
373
374 -----------------------------------------------------------------------------
375 -- Module Header
376
377 -- The place for module deprecation is really too restrictive, but if it
378 -- was allowed at its natural place just before 'module', we get an ugly
379 -- s/r conflict with the second alternative. Another solution would be the
380 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
381 -- either, and DEPRECATED is only expected to be used by people who really
382 -- know what they are doing. :-)
383
384 module  :: { Located (HsModule RdrName) }
385         : maybedocheader 'module' modid maybemodwarning maybeexports 'where' body
386                 {% fileSrcSpan >>= \ loc ->
387                    return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4 $1
388                           ) )}
389         | body2
390                 {% fileSrcSpan >>= \ loc ->
391                    return (L loc (HsModule Nothing Nothing
392                           (fst $1) (snd $1) Nothing Nothing
393                           )) }
394
395 maybedocheader :: { Maybe LHsDocString }
396         : moduleheader            { $1 }
397         | {- empty -}             { Nothing }
398
399 missing_module_keyword :: { () }
400         : {- empty -}                           {% pushCurrentContext }
401
402 maybemodwarning :: { Maybe WarningTxt }
403     : '{-# DEPRECATED' strings '#-}' { Just (DeprecatedTxt $ unLoc $2) }
404     | '{-# WARNING' strings '#-}'    { Just (WarningTxt $ unLoc $2) }
405     |  {- empty -}                  { Nothing }
406
407 body    :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
408         :  '{'            top '}'               { $2 }
409         |      vocurly    top close             { $2 }
410
411 body2   :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
412         :  '{' top '}'                          { $2 }
413         |  missing_module_keyword top close     { $2 }
414
415 top     :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
416         : importdecls                           { (reverse $1,[]) }
417         | importdecls ';' cvtopdecls            { (reverse $1,$3) }
418         | cvtopdecls                            { ([],$1) }
419
420 cvtopdecls :: { [LHsDecl RdrName] }
421         : topdecls                              { cvTopDecls $1 }
422
423 -----------------------------------------------------------------------------
424 -- Module declaration & imports only
425
426 header  :: { Located (HsModule RdrName) }
427         : maybedocheader 'module' modid maybemodwarning maybeexports 'where' header_body
428                 {% fileSrcSpan >>= \ loc ->
429                    return (L loc (HsModule (Just $3) $5 $7 [] $4 $1
430                           ))}
431         | missing_module_keyword importdecls
432                 {% fileSrcSpan >>= \ loc ->
433                    return (L loc (HsModule Nothing Nothing $2 [] Nothing
434                           Nothing)) }
435
436 header_body :: { [LImportDecl RdrName] }
437         :  '{'            importdecls           { $2 }
438         |      vocurly    importdecls           { $2 }
439
440 -----------------------------------------------------------------------------
441 -- The Export List
442
443 maybeexports :: { Maybe [LIE RdrName] }
444         :  '(' exportlist ')'                   { Just $2 }
445         |  {- empty -}                          { Nothing }
446
447 exportlist :: { [LIE RdrName] }
448         : expdoclist ',' expdoclist             { $1 ++ $3 }
449         | exportlist1                           { $1 }
450
451 exportlist1 :: { [LIE RdrName] }
452         : expdoclist export expdoclist ',' exportlist  { $1 ++ ($2 : $3) ++ $5 }
453         | expdoclist export expdoclist                 { $1 ++ ($2 : $3) }
454         | expdoclist                                   { $1 }
455
456 expdoclist :: { [LIE RdrName] }
457         : exp_doc expdoclist                           { $1 : $2 }
458         | {- empty -}                                  { [] }
459
460 exp_doc :: { LIE RdrName }                                                   
461         : docsection    { L1 (case (unLoc $1) of (n, doc) -> IEGroup n doc) }
462         | docnamed      { L1 (IEDocNamed ((fst . unLoc) $1)) } 
463         | docnext       { L1 (IEDoc (unLoc $1)) }       
464                        
465    -- No longer allow things like [] and (,,,) to be exported
466    -- They are built in syntax, always available
467 export  :: { LIE RdrName }
468         :  qvar                         { L1 (IEVar (unLoc $1)) }
469         |  oqtycon                      { L1 (IEThingAbs (unLoc $1)) }
470         |  oqtycon '(' '..' ')'         { LL (IEThingAll (unLoc $1)) }
471         |  oqtycon '(' ')'              { LL (IEThingWith (unLoc $1) []) }
472         |  oqtycon '(' qcnames ')'      { LL (IEThingWith (unLoc $1) (reverse $3)) }
473         |  'module' modid               { LL (IEModuleContents (unLoc $2)) }
474
475 qcnames :: { [RdrName] }
476         :  qcnames ',' qcname_ext       { unLoc $3 : $1 }
477         |  qcname_ext                   { [unLoc $1]  }
478
479 qcname_ext :: { Located RdrName }       -- Variable or data constructor
480                                         -- or tagged type constructor
481         :  qcname                       { $1 }
482         |  'type' qcon                  { sL (comb2 $1 $2) 
483                                              (setRdrNameSpace (unLoc $2) 
484                                                               tcClsName)  }
485
486 -- Cannot pull into qcname_ext, as qcname is also used in expression.
487 qcname  :: { Located RdrName }  -- Variable or data constructor
488         :  qvar                         { $1 }
489         |  qcon                         { $1 }
490
491 -----------------------------------------------------------------------------
492 -- Import Declarations
493
494 -- import decls can be *empty*, or even just a string of semicolons
495 -- whereas topdecls must contain at least one topdecl.
496
497 importdecls :: { [LImportDecl RdrName] }
498         : importdecls ';' importdecl            { $3 : $1 }
499         | importdecls ';'                       { $1 }
500         | importdecl                            { [ $1 ] }
501         | {- empty -}                           { [] }
502
503 importdecl :: { LImportDecl RdrName }
504         : 'import' maybe_src optqualified maybe_pkg modid maybeas maybeimpspec 
505                 { L (comb4 $1 $5 $6 $7) (ImportDecl $5 $4 $2 $3 (unLoc $6) (unLoc $7)) }
506
507 maybe_src :: { IsBootInterface }
508         : '{-# SOURCE' '#-}'                    { True }
509         | {- empty -}                           { False }
510
511 maybe_pkg :: { Maybe FastString }
512         : STRING                                { Just (getSTRING $1) }
513         | {- empty -}                           { Nothing }
514
515 optqualified :: { Bool }
516         : 'qualified'                           { True  }
517         | {- empty -}                           { False }
518
519 maybeas :: { Located (Maybe ModuleName) }
520         : 'as' modid                            { LL (Just (unLoc $2)) }
521         | {- empty -}                           { noLoc Nothing }
522
523 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
524         : impspec                               { L1 (Just (unLoc $1)) }
525         | {- empty -}                           { noLoc Nothing }
526
527 impspec :: { Located (Bool, [LIE RdrName]) }
528         :  '(' exportlist ')'                   { LL (False, $2) }
529         |  'hiding' '(' exportlist ')'          { LL (True,  $3) }
530
531 -----------------------------------------------------------------------------
532 -- Fixity Declarations
533
534 prec    :: { Int }
535         : {- empty -}           { 9 }
536         | INTEGER               {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
537
538 infix   :: { Located FixityDirection }
539         : 'infix'                               { L1 InfixN  }
540         | 'infixl'                              { L1 InfixL  }
541         | 'infixr'                              { L1 InfixR }
542
543 ops     :: { Located [Located RdrName] }
544         : ops ',' op                            { LL ($3 : unLoc $1) }
545         | op                                    { L1 [$1] }
546
547 -----------------------------------------------------------------------------
548 -- Top-Level Declarations
549
550 topdecls :: { OrdList (LHsDecl RdrName) }
551         : topdecls ';' topdecl                  { $1 `appOL` $3 }
552         | topdecls ';'                          { $1 }
553         | topdecl                               { $1 }
554
555 topdecl :: { OrdList (LHsDecl RdrName) }
556         : cl_decl                       { unitOL (L1 (TyClD (unLoc $1))) }
557         | ty_decl                       { unitOL (L1 (TyClD (unLoc $1))) }
558         | 'instance' inst_type where_inst
559             { let (binds, sigs, ats, _) = cvBindsAndSigs (unLoc $3)
560               in 
561               unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs ats)))}
562         | stand_alone_deriving                  { unitOL (LL (DerivD (unLoc $1))) }
563         | 'default' '(' comma_types0 ')'        { unitOL (LL $ DefD (DefaultDecl $3)) }
564         | 'foreign' fdecl                       { unitOL (LL (unLoc $2)) }
565         | '{-# DEPRECATED' deprecations '#-}'   { $2 }
566         | '{-# WARNING' warnings '#-}'          { $2 }
567         | '{-# RULES' rules '#-}'               { $2 }
568         | '{-# VECTORISE_SCALAR' qvar '#-}'     { unitOL $ LL $ VectD (HsVect $2 Nothing) }
569         | '{-# VECTORISE' qvar '=' exp '#-}'    { unitOL $ LL $ VectD (HsVect $2 (Just $4)) }
570         | annotation { unitOL $1 }
571         | decl                                  { unLoc $1 }
572
573         -- Template Haskell Extension
574         -- The $(..) form is one possible form of infixexp
575         -- but we treat an arbitrary expression just as if 
576         -- it had a $(..) wrapped around it
577         | infixexp                              { unitOL (LL $ mkTopSpliceDecl $1) } 
578
579 -- Type classes
580 --
581 cl_decl :: { LTyClDecl RdrName }
582         : 'class' tycl_hdr fds where_cls        {% mkClassDecl (comb4 $1 $2 $3 $4) $2 $3 $4 }
583
584 -- Type declarations (toplevel)
585 --
586 ty_decl :: { LTyClDecl RdrName }
587            -- ordinary type synonyms
588         : 'type' type '=' ctypedoc
589                 -- Note ctype, not sigtype, on the right of '='
590                 -- We allow an explicit for-all but we don't insert one
591                 -- in   type Foo a = (b,b)
592                 -- Instead we just say b is out of scope
593                 --
594                 -- Note the use of type for the head; this allows
595                 -- infix type constructors to be declared 
596                 {% mkTySynonym (comb2 $1 $4) False $2 $4 }
597
598            -- type family declarations
599         | 'type' 'family' type opt_kind_sig 
600                 -- Note the use of type for the head; this allows
601                 -- infix type constructors to be declared
602                 {% mkTyFamily (comb3 $1 $3 $4) TypeFamily $3 (unLoc $4) }
603
604            -- type instance declarations
605         | 'type' 'instance' type '=' ctype
606                 -- Note the use of type for the head; this allows
607                 -- infix type constructors and type patterns
608                 {% mkTySynonym (comb2 $1 $5) True $3 $5 }
609
610           -- ordinary data type or newtype declaration
611         | data_or_newtype tycl_hdr constrs deriving
612                 {% mkTyData (comb4 $1 $2 $3 $4) (unLoc $1) False $2 
613                             Nothing (reverse (unLoc $3)) (unLoc $4) }
614                                    -- We need the location on tycl_hdr in case 
615                                    -- constrs and deriving are both empty
616
617           -- ordinary GADT declaration
618         | data_or_newtype tycl_hdr opt_kind_sig 
619                  gadt_constrlist
620                  deriving
621                 {% mkTyData (comb4 $1 $2 $4 $5) (unLoc $1) False $2 
622                             (unLoc $3) (unLoc $4) (unLoc $5) }
623                                    -- We need the location on tycl_hdr in case 
624                                    -- constrs and deriving are both empty
625
626           -- data/newtype family
627         | 'data' 'family' type opt_kind_sig
628                 {% mkTyFamily (comb3 $1 $2 $4) DataFamily $3 (unLoc $4) }
629
630           -- data/newtype instance declaration
631         | data_or_newtype 'instance' tycl_hdr constrs deriving
632                 {% mkTyData (comb4 $1 $3 $4 $5) (unLoc $1) True $3
633                             Nothing (reverse (unLoc $4)) (unLoc $5) }
634
635           -- GADT instance declaration
636         | data_or_newtype 'instance' tycl_hdr opt_kind_sig 
637                  gadt_constrlist
638                  deriving
639                 {% mkTyData (comb4 $1 $3 $5 $6) (unLoc $1) True $3
640                             (unLoc $4) (unLoc $5) (unLoc $6) }
641
642 -- Associated type family declarations
643 --
644 -- * They have a different syntax than on the toplevel (no family special
645 --   identifier).
646 --
647 -- * They also need to be separate from instances; otherwise, data family
648 --   declarations without a kind signature cause parsing conflicts with empty
649 --   data declarations. 
650 --
651 at_decl_cls :: { LTyClDecl RdrName }
652            -- type family declarations
653         : 'type' type opt_kind_sig
654                 -- Note the use of type for the head; this allows
655                 -- infix type constructors to be declared
656                 {% mkTyFamily (comb3 $1 $2 $3) TypeFamily $2 (unLoc $3) }
657
658            -- default type instance
659         | 'type' type '=' ctype
660                 -- Note the use of type for the head; this allows
661                 -- infix type constructors and type patterns
662                 {% mkTySynonym (comb2 $1 $4) True $2 $4 }
663
664           -- data/newtype family declaration
665         | 'data' type opt_kind_sig
666                 {% mkTyFamily (comb3 $1 $2 $3) DataFamily $2 (unLoc $3) }
667
668 -- Associated type instances
669 --
670 at_decl_inst :: { LTyClDecl RdrName }
671            -- type instance declarations
672         : 'type' type '=' ctype
673                 -- Note the use of type for the head; this allows
674                 -- infix type constructors and type patterns
675                 {% mkTySynonym (comb2 $1 $4) True $2 $4 }
676
677         -- data/newtype instance declaration
678         | data_or_newtype tycl_hdr constrs deriving
679                 {% mkTyData (comb4 $1 $2 $3 $4) (unLoc $1) True $2 
680                             Nothing (reverse (unLoc $3)) (unLoc $4) }
681
682         -- GADT instance declaration
683         | data_or_newtype tycl_hdr opt_kind_sig 
684                  gadt_constrlist
685                  deriving
686                 {% mkTyData (comb4 $1 $2 $4 $5) (unLoc $1) True $2 
687                             (unLoc $3) (unLoc $4) (unLoc $5) }
688
689 data_or_newtype :: { Located NewOrData }
690         : 'data'        { L1 DataType }
691         | 'newtype'     { L1 NewType }
692
693 opt_kind_sig :: { Located (Maybe Kind) }
694         :                               { noLoc Nothing }
695         | '::' kind                     { LL (Just (unLoc $2)) }
696
697 -- tycl_hdr parses the header of a class or data type decl,
698 -- which takes the form
699 --      T a b
700 --      Eq a => T a
701 --      (Eq a, Ord b) => T a b
702 --      T Int [a]                       -- for associated types
703 -- Rather a lot of inlining here, else we get reduce/reduce errors
704 tycl_hdr :: { Located (Maybe (LHsContext RdrName), LHsType RdrName) }
705         : context '=>' type             { LL (Just $1, $3) }
706         | type                          { L1 (Nothing, $1) }
707
708 -----------------------------------------------------------------------------
709 -- Stand-alone deriving
710
711 -- Glasgow extension: stand-alone deriving declarations
712 stand_alone_deriving :: { LDerivDecl RdrName }
713         : 'deriving' 'instance' inst_type { LL (DerivDecl $3) }
714
715 -----------------------------------------------------------------------------
716 -- Nested declarations
717
718 -- Declaration in class bodies
719 --
720 decl_cls  :: { Located (OrdList (LHsDecl RdrName)) }
721 decl_cls  : at_decl_cls                 { LL (unitOL (L1 (TyClD (unLoc $1)))) }
722           | decl                        { $1 }
723
724           -- A 'default' signature used with the generic-programming extension
725           | 'default' infixexp '::' sigtypedoc
726                     {% do { (TypeSig l ty) <- checkValSig $2 $4
727                           ; return (LL $ unitOL (LL $ SigD (GenericSig l ty))) } }
728
729 decls_cls :: { Located (OrdList (LHsDecl RdrName)) }    -- Reversed
730           : decls_cls ';' decl_cls      { LL (unLoc $1 `appOL` unLoc $3) }
731           | decls_cls ';'               { LL (unLoc $1) }
732           | decl_cls                    { $1 }
733           | {- empty -}                 { noLoc nilOL }
734
735
736 decllist_cls
737         :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
738         : '{'         decls_cls '}'     { LL (unLoc $2) }
739         |     vocurly decls_cls close   { $2 }
740
741 -- Class body
742 --
743 where_cls :: { Located (OrdList (LHsDecl RdrName)) }    -- Reversed
744                                 -- No implicit parameters
745                                 -- May have type declarations
746         : 'where' decllist_cls          { LL (unLoc $2) }
747         | {- empty -}                   { noLoc nilOL }
748
749 -- Declarations in instance bodies
750 --
751 decl_inst  :: { Located (OrdList (LHsDecl RdrName)) }
752 decl_inst  : at_decl_inst               { LL (unitOL (L1 (TyClD (unLoc $1)))) }
753            | decl                       { $1 }
754
755 decls_inst :: { Located (OrdList (LHsDecl RdrName)) }   -- Reversed
756            : decls_inst ';' decl_inst   { LL (unLoc $1 `appOL` unLoc $3) }
757            | decls_inst ';'             { LL (unLoc $1) }
758            | decl_inst                  { $1 }
759            | {- empty -}                { noLoc nilOL }
760
761 decllist_inst 
762         :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
763         : '{'         decls_inst '}'    { LL (unLoc $2) }
764         |     vocurly decls_inst close  { $2 }
765
766 -- Instance body
767 --
768 where_inst :: { Located (OrdList (LHsDecl RdrName)) }   -- Reversed
769                                 -- No implicit parameters
770                                 -- May have type declarations
771         : 'where' decllist_inst         { LL (unLoc $2) }
772         | {- empty -}                   { noLoc nilOL }
773
774 -- Declarations in binding groups other than classes and instances
775 --
776 decls   :: { Located (OrdList (LHsDecl RdrName)) }      
777         : decls ';' decl                { let { this = unLoc $3;
778                                     rest = unLoc $1;
779                                     these = rest `appOL` this }
780                               in rest `seq` this `seq` these `seq`
781                                     LL these }
782         | decls ';'                     { LL (unLoc $1) }
783         | decl                          { $1 }
784         | {- empty -}                   { noLoc nilOL }
785
786 decllist :: { Located (OrdList (LHsDecl RdrName)) }
787         : '{'            decls '}'      { LL (unLoc $2) }
788         |     vocurly    decls close    { $2 }
789
790 -- Binding groups other than those of class and instance declarations
791 --
792 binds   ::  { Located (HsLocalBinds RdrName) }          -- May have implicit parameters
793                                                 -- No type declarations
794         : decllist                      { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
795         | '{'            dbinds '}'     { LL (HsIPBinds (IPBinds (unLoc $2) emptyTcEvBinds)) }
796         |     vocurly    dbinds close   { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyTcEvBinds)) }
797
798 wherebinds :: { Located (HsLocalBinds RdrName) }        -- May have implicit parameters
799                                                 -- No type declarations
800         : 'where' binds                 { LL (unLoc $2) }
801         | {- empty -}                   { noLoc emptyLocalBinds }
802
803
804 -----------------------------------------------------------------------------
805 -- Transformation Rules
806
807 rules   :: { OrdList (LHsDecl RdrName) }
808         :  rules ';' rule                       { $1 `snocOL` $3 }
809         |  rules ';'                            { $1 }
810         |  rule                                 { unitOL $1 }
811         |  {- empty -}                          { nilOL }
812
813 rule    :: { LHsDecl RdrName }
814         : STRING activation rule_forall infixexp '=' exp
815              { LL $ RuleD (HsRule (getSTRING $1) 
816                                   ($2 `orElse` AlwaysActive) 
817                                   $3 $4 placeHolderNames $6 placeHolderNames) }
818
819 activation :: { Maybe Activation } 
820         : {- empty -}                           { Nothing }
821         | explicit_activation                   { Just $1 }
822
823 explicit_activation :: { Activation }  -- In brackets
824         : '[' INTEGER ']'               { ActiveAfter  (fromInteger (getINTEGER $2)) }
825         | '[' '~' INTEGER ']'           { ActiveBefore (fromInteger (getINTEGER $3)) }
826
827 rule_forall :: { [RuleBndr RdrName] }
828         : 'forall' rule_var_list '.'            { $2 }
829         | {- empty -}                           { [] }
830
831 rule_var_list :: { [RuleBndr RdrName] }
832         : rule_var                              { [$1] }
833         | rule_var rule_var_list                { $1 : $2 }
834
835 rule_var :: { RuleBndr RdrName }
836         : varid                                 { RuleBndr $1 }
837         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
838
839 -----------------------------------------------------------------------------
840 -- Warnings and deprecations (c.f. rules)
841
842 warnings :: { OrdList (LHsDecl RdrName) }
843         : warnings ';' warning          { $1 `appOL` $3 }
844         | warnings ';'                  { $1 }
845         | warning                               { $1 }
846         | {- empty -}                           { nilOL }
847
848 -- SUP: TEMPORARY HACK, not checking for `module Foo'
849 warning :: { OrdList (LHsDecl RdrName) }
850         : namelist strings
851                 { toOL [ LL $ WarningD (Warning n (WarningTxt $ unLoc $2))
852                        | n <- unLoc $1 ] }
853
854 deprecations :: { OrdList (LHsDecl RdrName) }
855         : deprecations ';' deprecation          { $1 `appOL` $3 }
856         | deprecations ';'                      { $1 }
857         | deprecation                           { $1 }
858         | {- empty -}                           { nilOL }
859
860 -- SUP: TEMPORARY HACK, not checking for `module Foo'
861 deprecation :: { OrdList (LHsDecl RdrName) }
862         : namelist strings
863                 { toOL [ LL $ WarningD (Warning n (DeprecatedTxt $ unLoc $2))
864                        | n <- unLoc $1 ] }
865
866 strings :: { Located [FastString] }
867     : STRING { L1 [getSTRING $1] }
868     | '[' stringlist ']' { LL $ fromOL (unLoc $2) }
869
870 stringlist :: { Located (OrdList FastString) }
871     : stringlist ',' STRING { LL (unLoc $1 `snocOL` getSTRING $3) }
872     | STRING                { LL (unitOL (getSTRING $1)) }
873
874 -----------------------------------------------------------------------------
875 -- Annotations
876 annotation :: { LHsDecl RdrName }
877     : '{-# ANN' name_var aexp '#-}'      { LL (AnnD $ HsAnnotation (ValueAnnProvenance (unLoc $2)) $3) }
878     | '{-# ANN' 'type' tycon aexp '#-}'  { LL (AnnD $ HsAnnotation (TypeAnnProvenance (unLoc $3)) $4) }
879     | '{-# ANN' 'module' aexp '#-}'      { LL (AnnD $ HsAnnotation ModuleAnnProvenance $3) }
880
881
882 -----------------------------------------------------------------------------
883 -- Foreign import and export declarations
884
885 fdecl :: { LHsDecl RdrName }
886 fdecl : 'import' callconv safety fspec
887                 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
888       | 'import' callconv        fspec          
889                 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
890                         return (LL d) } }
891       | 'export' callconv fspec
892                 {% mkExport $2 (unLoc $3) >>= return.LL }
893
894 callconv :: { CCallConv }
895           : 'stdcall'                   { StdCallConv }
896           | 'ccall'                     { CCallConv   }
897           | 'prim'                      { PrimCallConv}
898
899 safety :: { Safety }
900         : 'unsafe'                      { PlayRisky }
901         | 'safe'                        { PlaySafe  False }
902         | 'interruptible'               { PlayInterruptible }
903         | 'threadsafe'                  { PlaySafe  True } -- deprecated alias
904
905 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
906        : STRING var '::' sigtypedoc     { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
907        |        var '::' sigtypedoc     { LL (noLoc nilFS, $1, $3) }
908          -- if the entity string is missing, it defaults to the empty string;
909          -- the meaning of an empty entity string depends on the calling
910          -- convention
911
912 -----------------------------------------------------------------------------
913 -- Type signatures
914
915 opt_sig :: { Maybe (LHsType RdrName) }
916         : {- empty -}                   { Nothing }
917         | '::' sigtype                  { Just $2 }
918
919 opt_asig :: { Maybe (LHsType RdrName) }
920         : {- empty -}                   { Nothing }
921         | '::' atype                    { Just $2 }
922
923 sigtype :: { LHsType RdrName }          -- Always a HsForAllTy,
924                                         -- to tell the renamer where to generalise
925         : ctype                         { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
926         -- Wrap an Implicit forall if there isn't one there already
927
928 sigtypedoc :: { LHsType RdrName }       -- Always a HsForAllTy
929         : ctypedoc                      { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
930         -- Wrap an Implicit forall if there isn't one there already
931
932 sig_vars :: { Located [Located RdrName] }
933          : sig_vars ',' var             { LL ($3 : unLoc $1) }
934          | var                          { L1 [$1] }
935
936 sigtypes1 :: { [LHsType RdrName] }      -- Always HsForAllTys
937         : sigtype                       { [ $1 ] }
938         | sigtype ',' sigtypes1         { $1 : $3 }
939
940 -----------------------------------------------------------------------------
941 -- Types
942
943 infixtype :: { LHsType RdrName }
944         : btype qtyconop type         { LL $ HsOpTy $1 $2 $3 }
945         | btype tyvarop  type    { LL $ HsOpTy $1 $2 $3 }
946
947 strict_mark :: { Located HsBang }
948         : '!'                           { L1 HsStrict }
949         | '{-# UNPACK' '#-}' '!'        { LL HsUnpack }
950
951 -- A ctype is a for-all type
952 ctype   :: { LHsType RdrName }
953         : 'forall' tv_bndrs '.' ctype   { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
954         | context '=>' ctype            { LL $ mkImplicitHsForAllTy   $1 $3 }
955         -- A type of form (context => type) is an *implicit* HsForAllTy
956         | ipvar '::' type               { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
957         | type                          { $1 }
958
959 ----------------------
960 -- Notes for 'ctypedoc'
961 -- It would have been nice to simplify the grammar by unifying `ctype` and 
962 -- ctypedoc` into one production, allowing comments on types everywhere (and
963 -- rejecting them after parsing, where necessary).  This is however not possible
964 -- since it leads to ambiguity. The reason is the support for comments on record
965 -- fields: 
966 --         data R = R { field :: Int -- ^ comment on the field }
967 -- If we allow comments on types here, it's not clear if the comment applies
968 -- to 'field' or to 'Int'. So we must use `ctype` to describe the type.
969
970 ctypedoc :: { LHsType RdrName }
971         : 'forall' tv_bndrs '.' ctypedoc        { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
972         | context '=>' ctypedoc         { LL $ mkImplicitHsForAllTy   $1 $3 }
973         -- A type of form (context => type) is an *implicit* HsForAllTy
974         | ipvar '::' type               { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
975         | typedoc                       { $1 }
976
977 ----------------------
978 -- Notes for 'context'
979 -- We parse a context as a btype so that we don't get reduce/reduce
980 -- errors in ctype.  The basic problem is that
981 --      (Eq a, Ord a)
982 -- looks so much like a tuple type.  We can't tell until we find the =>
983
984 -- We have the t1 ~ t2 form both in 'context' and in type, 
985 -- to permit an individual equational constraint without parenthesis.
986 -- Thus for some reason we allow    f :: a~b => blah
987 -- but not                          f :: ?x::Int => blah
988 context :: { LHsContext RdrName }
989         : btype '~'      btype          {% checkContext
990                                              (LL $ HsPredTy (HsEqualP $1 $3)) }
991         | btype                         {% checkContext $1 }
992
993 type :: { LHsType RdrName }
994         : btype                         { $1 }
995         | btype qtyconop type           { LL $ HsOpTy $1 $2 $3 }
996         | btype tyvarop  type           { LL $ HsOpTy $1 $2 $3 }
997         | btype '->'     ctype          { LL $ HsFunTy $1 $3 }
998         | btype '~'      btype          { LL $ HsPredTy (HsEqualP $1 $3) }
999
1000 typedoc :: { LHsType RdrName }
1001         : btype                          { $1 }
1002         | btype docprev                  { LL $ HsDocTy $1 $2 }
1003         | btype qtyconop type            { LL $ HsOpTy $1 $2 $3 }
1004         | btype qtyconop type docprev    { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
1005         | btype tyvarop  type            { LL $ HsOpTy $1 $2 $3 }
1006         | btype tyvarop  type docprev    { LL $ HsDocTy (L (comb3 $1 $2 $3) (HsOpTy $1 $2 $3)) $4 }
1007         | btype '->'     ctypedoc        { LL $ HsFunTy $1 $3 }
1008         | btype docprev '->' ctypedoc    { LL $ HsFunTy (L (comb2 $1 $2) (HsDocTy $1 $2)) $4 }
1009         | btype '~'      btype           { LL $ HsPredTy (HsEqualP $1 $3) }
1010
1011 btype :: { LHsType RdrName }
1012         : btype atype                   { LL $ HsAppTy $1 $2 }
1013         | atype                         { $1 }
1014
1015 atype :: { LHsType RdrName }
1016         : gtycon                        { L1 (HsTyVar (unLoc $1)) }
1017         | tyvar                         { L1 (HsTyVar (unLoc $1)) }
1018         | strict_mark atype             { LL (HsBangTy (unLoc $1) $2) }  -- Constructor sigs only
1019         | '{' fielddecls '}'            { LL $ HsRecTy $2 }              -- Constructor sigs only
1020         | '(' ctype ',' comma_types1 ')'  { LL $ HsTupleTy Boxed  ($2:$4) }
1021         | '(#' comma_types1 '#)'        { LL $ HsTupleTy Unboxed $2     }
1022         | '[' ctype ']'                 { LL $ HsListTy  $2 }
1023         | '[:' ctype ':]'               { LL $ HsPArrTy  $2 }
1024         | '(' ctype ')'                 { LL $ HsParTy   $2 }
1025         | '(' ctype '::' kind ')'       { LL $ HsKindSig $2 (unLoc $4) }
1026         | quasiquote                    { L1 (HsQuasiQuoteTy (unLoc $1)) }
1027         | '$(' exp ')'                  { LL $ mkHsSpliceTy $2 }
1028         | TH_ID_SPLICE                  { LL $ mkHsSpliceTy $ L1 $ HsVar $ 
1029                                           mkUnqual varName (getTH_ID_SPLICE $1) }
1030 -- Generics
1031         | INTEGER                       { L1 (HsNumTy (getINTEGER $1)) }
1032
1033 -- An inst_type is what occurs in the head of an instance decl
1034 --      e.g.  (Foo a, Gaz b) => Wibble a b
1035 -- It's kept as a single type, with a MonoDictTy at the right
1036 -- hand corner, for convenience.
1037 inst_type :: { LHsType RdrName }
1038         : sigtype                       {% checkInstType $1 }
1039
1040 inst_types1 :: { [LHsType RdrName] }
1041         : inst_type                     { [$1] }
1042         | inst_type ',' inst_types1     { $1 : $3 }
1043
1044 comma_types0  :: { [LHsType RdrName] }
1045         : comma_types1                  { $1 }
1046         | {- empty -}                   { [] }
1047
1048 comma_types1    :: { [LHsType RdrName] }
1049         : ctype                         { [$1] }
1050         | ctype  ',' comma_types1       { $1 : $3 }
1051
1052 tv_bndrs :: { [LHsTyVarBndr RdrName] }
1053          : tv_bndr tv_bndrs             { $1 : $2 }
1054          | {- empty -}                  { [] }
1055
1056 tv_bndr :: { LHsTyVarBndr RdrName }
1057         : tyvar                         { L1 (UserTyVar (unLoc $1) placeHolderKind) }
1058         | '(' tyvar '::' kind ')'       { LL (KindedTyVar (unLoc $2) 
1059                                                           (unLoc $4)) }
1060
1061 fds :: { Located [Located (FunDep RdrName)] }
1062         : {- empty -}                   { noLoc [] }
1063         | '|' fds1                      { LL (reverse (unLoc $2)) }
1064
1065 fds1 :: { Located [Located (FunDep RdrName)] }
1066         : fds1 ',' fd                   { LL ($3 : unLoc $1) }
1067         | fd                            { L1 [$1] }
1068
1069 fd :: { Located (FunDep RdrName) }
1070         : varids0 '->' varids0          { L (comb3 $1 $2 $3)
1071                                            (reverse (unLoc $1), reverse (unLoc $3)) }
1072
1073 varids0 :: { Located [RdrName] }
1074         : {- empty -}                   { noLoc [] }
1075         | varids0 tyvar                 { LL (unLoc $2 : unLoc $1) }
1076
1077 -----------------------------------------------------------------------------
1078 -- Kinds
1079
1080 kind    :: { Located Kind }
1081         : akind                 { $1 }
1082         | akind '->' kind       { LL (mkArrowKind (unLoc $1) (unLoc $3)) }
1083
1084 akind   :: { Located Kind }
1085         : '*'                   { L1 liftedTypeKind }
1086         | '!'                   { L1 unliftedTypeKind }
1087         | '(' kind ')'          { LL (unLoc $2) }
1088
1089
1090 -----------------------------------------------------------------------------
1091 -- Datatype declarations
1092
1093 gadt_constrlist :: { Located [LConDecl RdrName] }       -- Returned in order
1094         : 'where' '{'        gadt_constrs '}'      { L (comb2 $1 $3) (unLoc $3) }
1095         | 'where' vocurly    gadt_constrs close    { L (comb2 $1 $3) (unLoc $3) }
1096         | {- empty -}                              { noLoc [] }
1097
1098 gadt_constrs :: { Located [LConDecl RdrName] }
1099         : gadt_constr ';' gadt_constrs  { L (comb2 (head $1) $3) ($1 ++ unLoc $3) }
1100         | gadt_constr                   { L (getLoc (head $1)) $1 }
1101         | {- empty -}                   { noLoc [] }
1102
1103 -- We allow the following forms:
1104 --      C :: Eq a => a -> T a
1105 --      C :: forall a. Eq a => !a -> T a
1106 --      D { x,y :: a } :: T a
1107 --      forall a. Eq a => D { x,y :: a } :: T a
1108
1109 gadt_constr :: { [LConDecl RdrName] }   -- Returns a list because of:   C,D :: ty
1110         : con_list '::' sigtype
1111                 { map (sL (comb2 $1 $3)) (mkGadtDecl (unLoc $1) $3) } 
1112
1113                 -- Deprecated syntax for GADT record declarations
1114         | oqtycon '{' fielddecls '}' '::' sigtype
1115                 {% do { cd <- mkDeprecatedGadtRecordDecl (comb2 $1 $6) $1 $3 $6
1116                       ; return [cd] } }
1117
1118 constrs :: { Located [LConDecl RdrName] }
1119         : maybe_docnext '=' constrs1    { L (comb2 $2 $3) (addConDocs (unLoc $3) $1) }
1120
1121 constrs1 :: { Located [LConDecl RdrName] }
1122         : constrs1 maybe_docnext '|' maybe_docprev constr { LL (addConDoc $5 $2 : addConDocFirst (unLoc $1) $4) }
1123         | constr                                          { L1 [$1] }
1124
1125 constr :: { LConDecl RdrName }
1126         : maybe_docnext forall context '=>' constr_stuff maybe_docprev  
1127                 { let (con,details) = unLoc $5 in 
1128                   addConDoc (L (comb4 $2 $3 $4 $5) (mkSimpleConDecl con (unLoc $2) $3 details))
1129                             ($1 `mplus` $6) }
1130         | maybe_docnext forall constr_stuff maybe_docprev
1131                 { let (con,details) = unLoc $3 in 
1132                   addConDoc (L (comb2 $2 $3) (mkSimpleConDecl con (unLoc $2) (noLoc []) details))
1133                             ($1 `mplus` $4) }
1134
1135 forall :: { Located [LHsTyVarBndr RdrName] }
1136         : 'forall' tv_bndrs '.'         { LL $2 }
1137         | {- empty -}                   { noLoc [] }
1138
1139 constr_stuff :: { Located (Located RdrName, HsConDeclDetails RdrName) }
1140 -- We parse the constructor declaration 
1141 --      C t1 t2
1142 -- as a btype (treating C as a type constructor) and then convert C to be
1143 -- a data constructor.  Reason: it might continue like this:
1144 --      C t1 t2 %: D Int
1145 -- in which case C really would be a type constructor.  We can't resolve this
1146 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
1147         : btype                         {% splitCon $1 >>= return.LL }
1148         | btype conop btype             {  LL ($2, InfixCon $1 $3) }
1149
1150 fielddecls :: { [ConDeclField RdrName] }
1151         : {- empty -}     { [] }
1152         | fielddecls1     { $1 }
1153
1154 fielddecls1 :: { [ConDeclField RdrName] }
1155         : fielddecl maybe_docnext ',' maybe_docprev fielddecls1
1156                       { [ addFieldDoc f $4 | f <- $1 ] ++ addFieldDocs $5 $2 }
1157                              -- This adds the doc $4 to each field separately
1158         | fielddecl   { $1 }
1159
1160 fielddecl :: { [ConDeclField RdrName] }    -- A list because of   f,g :: Int
1161         : maybe_docnext sig_vars '::' ctype maybe_docprev      { [ ConDeclField fld $4 ($1 `mplus` $5) 
1162                                                                  | fld <- reverse (unLoc $2) ] }
1163
1164 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
1165 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
1166 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
1167 -- We don't allow a context, but that's sorted out by the type checker.
1168 deriving :: { Located (Maybe [LHsType RdrName]) }
1169         : {- empty -}                           { noLoc Nothing }
1170         | 'deriving' qtycon     {% do { let { L loc tv = $2 }
1171                                       ; p <- checkInstType (L loc (HsTyVar tv))
1172                                       ; return (LL (Just [p])) } }
1173         | 'deriving' '(' ')'                    { LL (Just []) }
1174         | 'deriving' '(' inst_types1 ')'        { LL (Just $3) }
1175              -- Glasgow extension: allow partial 
1176              -- applications in derivings
1177
1178 -----------------------------------------------------------------------------
1179 -- Value definitions
1180
1181 {- Note [Declaration/signature overlap]
1182 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1183 There's an awkward overlap with a type signature.  Consider
1184         f :: Int -> Int = ...rhs...
1185    Then we can't tell whether it's a type signature or a value
1186    definition with a result signature until we see the '='.
1187    So we have to inline enough to postpone reductions until we know.
1188 -}
1189
1190 {-
1191   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
1192   instead of qvar, we get another shift/reduce-conflict. Consider the
1193   following programs:
1194   
1195      { (^^) :: Int->Int ; }          Type signature; only var allowed
1196
1197      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
1198                                      qvar allowed (because of instance decls)
1199   
1200   We can't tell whether to reduce var to qvar until after we've read the signatures.
1201 -}
1202
1203 docdecl :: { LHsDecl RdrName }
1204         : docdecld { L1 (DocD (unLoc $1)) }
1205
1206 docdecld :: { LDocDecl }
1207         : docnext                               { L1 (DocCommentNext (unLoc $1)) }
1208         | docprev                               { L1 (DocCommentPrev (unLoc $1)) }
1209         | docnamed                              { L1 (case (unLoc $1) of (n, doc) -> DocCommentNamed n doc) }
1210         | docsection                            { L1 (case (unLoc $1) of (n, doc) -> DocGroup n doc) }
1211
1212 decl    :: { Located (OrdList (LHsDecl RdrName)) }
1213         : sigdecl               { $1 }
1214
1215         | '!' aexp rhs          {% do { let { e = LL (SectionR (LL (HsVar bang_RDR)) $2) };
1216                                         pat <- checkPattern e;
1217                                         return $ LL $ unitOL $ LL $ ValD $
1218                                                PatBind pat (unLoc $3)
1219                                                        placeHolderType placeHolderNames } }
1220                                 -- Turn it all into an expression so that
1221                                 -- checkPattern can check that bangs are enabled
1222
1223         | infixexp opt_sig rhs  {% do { r <- checkValDef $1 $2 $3;
1224                                         let { l = comb2 $1 $> };
1225                                         return $! (sL l (unitOL $! (sL l $ ValD r))) } }
1226         | docdecl               { LL $ unitOL $1 }
1227
1228 rhs     :: { Located (GRHSs RdrName) }
1229         : '=' exp wherebinds    { sL (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
1230         | gdrhs wherebinds      { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
1231
1232 gdrhs :: { Located [LGRHS RdrName] }
1233         : gdrhs gdrh            { LL ($2 : unLoc $1) }
1234         | gdrh                  { L1 [$1] }
1235
1236 gdrh :: { LGRHS RdrName }
1237         : '|' guardquals '=' exp        { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1238
1239 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
1240         : 
1241         -- See Note [Declaration/signature overlap] for why we need infixexp here
1242           infixexp '::' sigtypedoc
1243                         {% do s <- checkValSig $1 $3 
1244                         ; return (LL $ unitOL (LL $ SigD s)) }
1245         | var ',' sig_vars '::' sigtypedoc
1246                                 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1247         | infix prec ops        { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1248                                              | n <- unLoc $3 ] }
1249         | '{-# INLINE'   activation qvar '#-}'        
1250                 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlinePragma (getINLINE $1) $2))) }
1251         | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1252                 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlinePragma) 
1253                                             | t <- $4] }
1254         | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1255                 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlinePragma (getSPEC_INLINE $1) $2))
1256                                             | t <- $5] }
1257         | '{-# SPECIALISE' 'instance' inst_type '#-}'
1258                 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1259
1260 -----------------------------------------------------------------------------
1261 -- Expressions
1262
1263 quasiquote :: { Located (HsQuasiQuote RdrName) }
1264         : TH_QUASIQUOTE   { let { loc = getLoc $1
1265                                 ; ITquasiQuote (quoter, quote, quoteSpan) = unLoc $1
1266                                 ; quoterId = mkUnqual varName quoter }
1267                             in L1 (mkHsQuasiQuote quoterId quoteSpan quote) }
1268
1269 exp   :: { LHsExpr RdrName }
1270         : infixexp '::' sigtype         { LL $ ExprWithTySig $1 $3 }
1271         | infixexp '-<' exp             { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1272         | infixexp '>-' exp             { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1273         | infixexp '-<<' exp            { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1274         | infixexp '>>-' exp            { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1275         | infixexp                      { $1 }
1276
1277 infixexp :: { LHsExpr RdrName }
1278         : exp10                         { $1 }
1279         | infixexp qop exp10            { LL (OpApp $1 $2 (panic "fixity") $3) }
1280
1281 exp10 :: { LHsExpr RdrName }
1282         : '\\' apat apats opt_asig '->' exp     
1283                         { LL $ HsLam (mkMatchGroup [LL $ Match ($2:$3) $4
1284                                                                 (unguardedGRHSs $6)
1285                                                             ]) }
1286         | 'let' binds 'in' exp                  { LL $ HsLet (unLoc $2) $4 }
1287         | 'if' exp optSemi 'then' exp optSemi 'else' exp
1288                                         {% checkDoAndIfThenElse $2 $3 $5 $6 $8 >>
1289                                            return (LL $ mkHsIf $2 $5 $8) }
1290         | 'case' exp 'of' altslist              { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1291         | '-' fexp                              { LL $ NegApp $2 noSyntaxExpr }
1292
1293         | 'do' stmtlist                 {% let loc = comb2 $1 $2 in
1294                                            checkDo loc (unLoc $2)  >>= \ (stmts,body) ->
1295                                            return (L loc (mkHsDo DoExpr stmts body)) }
1296         | 'mdo' stmtlist                {% let loc = comb2 $1 $2 in
1297                                            checkDo loc (unLoc $2)  >>= \ (stmts,body) ->
1298                                            return (L loc (mkHsDo MDoExpr
1299                                                                  [L loc (mkRecStmt stmts)]
1300                                                                  body)) }
1301         | scc_annot exp                         { LL $ if opt_SccProfilingOn
1302                                                         then HsSCC (unLoc $1) $2
1303                                                         else HsPar $2 }
1304         | hpc_annot exp                         { LL $ if opt_Hpc
1305                                                         then HsTickPragma (unLoc $1) $2
1306                                                         else HsPar $2 }
1307
1308         | 'proc' aexp '->' exp  
1309                         {% checkPattern $2 >>= \ p -> 
1310                            return (LL $ HsProc p (LL $ HsCmdTop $4 [] 
1311                                                    placeHolderType undefined)) }
1312                                                 -- TODO: is LL right here?
1313
1314         | '{-# CORE' STRING '#-}' exp           { LL $ HsCoreAnn (getSTRING $2) $4 }
1315                                                     -- hdaume: core annotation
1316         | fexp                                  { $1 }
1317
1318 optSemi :: { Bool }
1319         : ';'         { True }
1320         | {- empty -} { False }
1321
1322 scc_annot :: { Located FastString }
1323         : '_scc_' STRING                        {% (addWarning Opt_WarnWarningsDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1324                                    ( do scc <- getSCC $2; return $ LL scc ) }
1325         | '{-# SCC' STRING '#-}'                {% do scc <- getSCC $2; return $ LL scc }
1326
1327 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1328         : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1329                                                 { LL $ (getSTRING $2
1330                                                        ,( fromInteger $ getINTEGER $3
1331                                                         , fromInteger $ getINTEGER $5
1332                                                         )
1333                                                        ,( fromInteger $ getINTEGER $7
1334                                                         , fromInteger $ getINTEGER $9
1335                                                         )
1336                                                        )
1337                                                  }
1338
1339 fexp    :: { LHsExpr RdrName }
1340         : fexp aexp                             { LL $ HsApp $1 $2 }
1341         | aexp                                  { $1 }
1342
1343 aexp    :: { LHsExpr RdrName }
1344         : qvar '@' aexp                 { LL $ EAsPat $1 $3 }
1345         | '~' aexp                      { LL $ ELazyPat $2 }
1346         | aexp1                 { $1 }
1347
1348 aexp1   :: { LHsExpr RdrName }
1349         : aexp1 '{' fbinds '}'  {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1350                                       ; return (LL r) }}
1351         | aexp2                 { $1 }
1352
1353 -- Here was the syntax for type applications that I was planning
1354 -- but there are difficulties (e.g. what order for type args)
1355 -- so it's not enabled yet.
1356 -- But this case *is* used for the left hand side of a generic definition,
1357 -- which is parsed as an expression before being munged into a pattern
1358         | qcname '{|' type '|}'         { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1359                                                      (sL (getLoc $3) (HsType $3)) }
1360
1361 aexp2   :: { LHsExpr RdrName }
1362         : ipvar                         { L1 (HsIPVar $! unLoc $1) }
1363         | qcname                        { L1 (HsVar   $! unLoc $1) }
1364         | literal                       { L1 (HsLit   $! unLoc $1) }
1365 -- This will enable overloaded strings permanently.  Normally the renamer turns HsString
1366 -- into HsOverLit when -foverloaded-strings is on.
1367 --      | STRING                        { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1368         | INTEGER                       { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1369         | RATIONAL                      { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1370
1371         -- N.B.: sections get parsed by these next two productions.
1372         -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't
1373         -- correct Haskell (you'd have to write '((+ 3), (4 -))')
1374         -- but the less cluttered version fell out of having texps.
1375         | '(' texp ')'                  { LL (HsPar $2) }
1376         | '(' tup_exprs ')'             { LL (ExplicitTuple $2 Boxed) }
1377
1378         | '(#' texp '#)'                { LL (ExplicitTuple [Present $2] Unboxed) }
1379         | '(#' tup_exprs '#)'           { LL (ExplicitTuple $2 Unboxed) }
1380
1381         | '[' list ']'                  { LL (unLoc $2) }
1382         | '[:' parr ':]'                { LL (unLoc $2) }
1383         | '_'                           { L1 EWildPat }
1384         
1385         -- Template Haskell Extension
1386         | TH_ID_SPLICE          { L1 $ HsSpliceE (mkHsSplice 
1387                                         (L1 $ HsVar (mkUnqual varName 
1388                                                         (getTH_ID_SPLICE $1)))) } 
1389         | '$(' exp ')'          { LL $ HsSpliceE (mkHsSplice $2) }               
1390
1391
1392         | TH_VAR_QUOTE qvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1393         | TH_VAR_QUOTE qcon     { LL $ HsBracket (VarBr (unLoc $2)) }
1394         | TH_TY_QUOTE tyvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1395         | TH_TY_QUOTE gtycon    { LL $ HsBracket (VarBr (unLoc $2)) }
1396         | '[|' exp '|]'         { LL $ HsBracket (ExpBr $2) }                       
1397         | '[t|' ctype '|]'      { LL $ HsBracket (TypBr $2) }                       
1398         | '[p|' infixexp '|]'   {% checkPattern $2 >>= \p ->
1399                                         return (LL $ HsBracket (PatBr p)) }
1400         | '[d|' cvtopbody '|]'  { LL $ HsBracket (DecBrL $2) }
1401         | quasiquote            { L1 (HsQuasiQuoteE (unLoc $1)) }
1402
1403         -- arrow notation extension
1404         | '(|' aexp2 cmdargs '|)'       { LL $ HsArrForm $2 Nothing (reverse $3) }
1405
1406 cmdargs :: { [LHsCmdTop RdrName] }
1407         : cmdargs acmd                  { $2 : $1 }
1408         | {- empty -}                   { [] }
1409
1410 acmd    :: { LHsCmdTop RdrName }
1411         : aexp2                 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1412
1413 cvtopbody :: { [LHsDecl RdrName] }
1414         :  '{'            cvtopdecls0 '}'               { $2 }
1415         |      vocurly    cvtopdecls0 close             { $2 }
1416
1417 cvtopdecls0 :: { [LHsDecl RdrName] }
1418         : {- empty -}           { [] }
1419         | cvtopdecls            { $1 }
1420
1421 -----------------------------------------------------------------------------
1422 -- Tuple expressions
1423
1424 -- "texp" is short for tuple expressions: 
1425 -- things that can appear unparenthesized as long as they're
1426 -- inside parens or delimitted by commas
1427 texp :: { LHsExpr RdrName }
1428         : exp                           { $1 }
1429
1430         -- Note [Parsing sections]
1431         -- ~~~~~~~~~~~~~~~~~~~~~~~
1432         -- We include left and right sections here, which isn't
1433         -- technically right according to the Haskell standard.
1434         -- For example (3 +, True) isn't legal.
1435         -- However, we want to parse bang patterns like
1436         --      (!x, !y)
1437         -- and it's convenient to do so here as a section
1438         -- Then when converting expr to pattern we unravel it again
1439         -- Meanwhile, the renamer checks that real sections appear
1440         -- inside parens.
1441         | infixexp qop  { LL $ SectionL $1 $2 }
1442         | qopm infixexp       { LL $ SectionR $1 $2 }
1443
1444        -- View patterns get parenthesized above
1445         | exp '->' texp   { LL $ EViewPat $1 $3 }
1446
1447 -- Always at least one comma
1448 tup_exprs :: { [HsTupArg RdrName] }
1449            : texp commas_tup_tail  { Present $1 : $2 }
1450            | commas tup_tail       { replicate $1 missingTupArg ++ $2 }
1451
1452 -- Always starts with commas; always follows an expr
1453 commas_tup_tail :: { [HsTupArg RdrName] }
1454 commas_tup_tail : commas tup_tail  { replicate ($1-1) missingTupArg ++ $2 }
1455
1456 -- Always follows a comma
1457 tup_tail :: { [HsTupArg RdrName] }
1458           : texp commas_tup_tail        { Present $1 : $2 }
1459           | texp                        { [Present $1] }
1460           | {- empty -}                 { [missingTupArg] }
1461
1462 -----------------------------------------------------------------------------
1463 -- List expressions
1464
1465 -- The rules below are little bit contorted to keep lexps left-recursive while
1466 -- avoiding another shift/reduce-conflict.
1467
1468 list :: { LHsExpr RdrName }
1469         : texp                  { L1 $ ExplicitList placeHolderType [$1] }
1470         | lexps                 { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1471         | texp '..'             { LL $ ArithSeq noPostTcExpr (From $1) }
1472         | texp ',' exp '..'     { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1473         | texp '..' exp         { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1474         | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1475         | texp '|' flattenedpquals      { sL (comb2 $1 $>) $ mkHsDo ListComp (unLoc $3) $1 }
1476
1477 lexps :: { Located [LHsExpr RdrName] }
1478         : lexps ',' texp                { LL (((:) $! $3) $! unLoc $1) }
1479         | texp ',' texp                 { LL [$3,$1] }
1480
1481 -----------------------------------------------------------------------------
1482 -- List Comprehensions
1483
1484 flattenedpquals :: { Located [LStmt RdrName] }
1485     : pquals   { case (unLoc $1) of
1486                     [qs] -> L1 qs
1487                     -- We just had one thing in our "parallel" list so 
1488                     -- we simply return that thing directly
1489                     
1490                     qss -> L1 [L1 $ ParStmt [(qs, undefined) | qs <- qss]]
1491                     -- We actually found some actual parallel lists so
1492                     -- we wrap them into as a ParStmt
1493                 }
1494
1495 pquals :: { Located [[LStmt RdrName]] }
1496     : squals '|' pquals     { L (getLoc $2) (reverse (unLoc $1) : unLoc $3) }
1497     | squals                { L (getLoc $1) [reverse (unLoc $1)] }
1498
1499 squals :: { Located [LStmt RdrName] }   -- In reverse order, because the last 
1500                                         -- one can "grab" the earlier ones
1501     : squals ',' transformqual               { LL [L (getLoc $3) ((unLoc $3) (reverse (unLoc $1)))] }
1502     | squals ',' qual                        { LL ($3 : unLoc $1) }
1503     | transformqual                          { LL [L (getLoc $1) ((unLoc $1) [])] }
1504     | qual                                   { L1 [$1] }
1505 --  | transformquals1 ',' '{|' pquals '|}'   { LL ($4 : unLoc $1) }
1506 --  | '{|' pquals '|}'                       { L1 [$2] }
1507
1508
1509 -- It is possible to enable bracketing (associating) qualifier lists by uncommenting the lines with {| |}
1510 -- above. Due to a lack of consensus on the syntax, this feature is not being used until we get user
1511 -- demand. Note that the {| |} symbols are reused from -XGenerics and hence if you want to compile
1512 -- a program that makes use of this temporary syntax you must supply that flag to GHC
1513
1514 transformqual :: { Located ([LStmt RdrName] -> Stmt RdrName) }
1515                         -- Function is applied to a list of stmts *in order*
1516     : 'then' exp                { LL $ \leftStmts -> (mkTransformStmt leftStmts $2) }
1517     -- >>>
1518     | 'then' exp 'by' exp       { LL $ \leftStmts -> (mkTransformByStmt leftStmts $2 $4) }
1519     | 'then' 'group' 'by' exp   { LL $ \leftStmts -> (mkGroupByStmt leftStmts $4) }
1520     -- <<<
1521     -- These two productions deliberately have a shift-reduce conflict. I have made 'group' into a special_id,
1522     -- which means you can enable TransformListComp while still using Data.List.group. However, this makes the two
1523     -- productions ambiguous. I've set things up so that Happy chooses to resolve the conflict in that case by
1524     -- choosing the "group by" variant, which is what we want.
1525     --
1526     -- This is rather dubious: the user might be confused as to how to parse this statement. However, it is a good
1527     -- practical choice. NB: Data.List.group :: [a] -> [[a]], so using the first production would not even type check
1528     -- if /that/ is the group function we conflict with.
1529     | 'then' 'group' 'using' exp           { LL $ \leftStmts -> (mkGroupUsingStmt leftStmts $4) }
1530     | 'then' 'group' 'by' exp 'using' exp  { LL $ \leftStmts -> (mkGroupByUsingStmt leftStmts $4 $6) }
1531
1532 -----------------------------------------------------------------------------
1533 -- Parallel array expressions
1534
1535 -- The rules below are little bit contorted; see the list case for details.
1536 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1537 -- Moreover, we allow explicit arrays with no element (represented by the nil
1538 -- constructor in the list case).
1539
1540 parr :: { LHsExpr RdrName }
1541         :                               { noLoc (ExplicitPArr placeHolderType []) }
1542         | texp                          { L1 $ ExplicitPArr placeHolderType [$1] }
1543         | lexps                         { L1 $ ExplicitPArr placeHolderType 
1544                                                        (reverse (unLoc $1)) }
1545         | texp '..' exp                 { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1546         | texp ',' exp '..' exp         { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1547         | texp '|' flattenedpquals      { LL $ mkHsDo PArrComp (unLoc $3) $1 }
1548
1549 -- We are reusing `lexps' and `flattenedpquals' from the list case.
1550
1551 -----------------------------------------------------------------------------
1552 -- Guards
1553
1554 guardquals :: { Located [LStmt RdrName] }
1555     : guardquals1           { L (getLoc $1) (reverse (unLoc $1)) }
1556
1557 guardquals1 :: { Located [LStmt RdrName] }
1558     : guardquals1 ',' qual  { LL ($3 : unLoc $1) }
1559     | qual                  { L1 [$1] }
1560
1561 -----------------------------------------------------------------------------
1562 -- Case alternatives
1563
1564 altslist :: { Located [LMatch RdrName] }
1565         : '{'            alts '}'       { LL (reverse (unLoc $2)) }
1566         |     vocurly    alts  close    { L (getLoc $2) (reverse (unLoc $2)) }
1567
1568 alts    :: { Located [LMatch RdrName] }
1569         : alts1                         { L1 (unLoc $1) }
1570         | ';' alts                      { LL (unLoc $2) }
1571
1572 alts1   :: { Located [LMatch RdrName] }
1573         : alts1 ';' alt                 { LL ($3 : unLoc $1) }
1574         | alts1 ';'                     { LL (unLoc $1) }
1575         | alt                           { L1 [$1] }
1576
1577 alt     :: { LMatch RdrName }
1578         : pat opt_sig alt_rhs           { LL (Match [$1] $2 (unLoc $3)) }
1579
1580 alt_rhs :: { Located (GRHSs RdrName) }
1581         : ralt wherebinds               { LL (GRHSs (unLoc $1) (unLoc $2)) }
1582
1583 ralt :: { Located [LGRHS RdrName] }
1584         : '->' exp                      { LL (unguardedRHS $2) }
1585         | gdpats                        { L1 (reverse (unLoc $1)) }
1586
1587 gdpats :: { Located [LGRHS RdrName] }
1588         : gdpats gdpat                  { LL ($2 : unLoc $1) }
1589         | gdpat                         { L1 [$1] }
1590
1591 gdpat   :: { LGRHS RdrName }
1592         : '|' guardquals '->' exp               { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1593
1594 -- 'pat' recognises a pattern, including one with a bang at the top
1595 --      e.g.  "!x" or "!(x,y)" or "C a b" etc
1596 -- Bangs inside are parsed as infix operator applications, so that
1597 -- we parse them right when bang-patterns are off
1598 pat     :: { LPat RdrName }
1599 pat     :  exp                  {% checkPattern $1 }
1600         | '!' aexp              {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1601
1602 apat   :: { LPat RdrName }      
1603 apat    : aexp                  {% checkPattern $1 }
1604         | '!' aexp              {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1605
1606 apats  :: { [LPat RdrName] }
1607         : apat apats            { $1 : $2 }
1608         | {- empty -}           { [] }
1609
1610 -----------------------------------------------------------------------------
1611 -- Statement sequences
1612
1613 stmtlist :: { Located [LStmt RdrName] }
1614         : '{'           stmts '}'       { LL (unLoc $2) }
1615         |     vocurly   stmts close     { $2 }
1616
1617 --      do { ;; s ; s ; ; s ;; }
1618 -- The last Stmt should be an expression, but that's hard to enforce
1619 -- here, because we need too much lookahead if we see do { e ; }
1620 -- So we use ExprStmts throughout, and switch the last one over
1621 -- in ParseUtils.checkDo instead
1622 stmts :: { Located [LStmt RdrName] }
1623         : stmt stmts_help               { LL ($1 : unLoc $2) }
1624         | ';' stmts                     { LL (unLoc $2) }
1625         | {- empty -}                   { noLoc [] }
1626
1627 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1628         : ';' stmts                     { LL (unLoc $2) }
1629         | {- empty -}                   { noLoc [] }
1630
1631 -- For typing stmts at the GHCi prompt, where 
1632 -- the input may consist of just comments.
1633 maybe_stmt :: { Maybe (LStmt RdrName) }
1634         : stmt                          { Just $1 }
1635         | {- nothing -}                 { Nothing }
1636
1637 stmt  :: { LStmt RdrName }
1638         : qual                              { $1 }
1639         | 'rec' stmtlist                { LL $ mkRecStmt (unLoc $2) }
1640
1641 qual  :: { LStmt RdrName }
1642     : pat '<-' exp                      { LL $ mkBindStmt $1 $3 }
1643     | exp                                   { L1 $ mkExprStmt $1 }
1644     | 'let' binds                       { LL $ LetStmt (unLoc $2) }
1645
1646 -----------------------------------------------------------------------------
1647 -- Record Field Update/Construction
1648
1649 fbinds  :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1650         : fbinds1                       { $1 }
1651         | {- empty -}                   { ([], False) }
1652
1653 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1654         : fbind ',' fbinds1             { case $3 of (flds, dd) -> ($1 : flds, dd) } 
1655         | fbind                         { ([$1], False) }
1656         | '..'                          { ([],   True) }
1657   
1658 fbind   :: { HsRecField RdrName (LHsExpr RdrName) }
1659         : qvar '=' exp  { HsRecField $1 $3                False }
1660         | qvar          { HsRecField $1 placeHolderPunRhs True }
1661                         -- In the punning case, use a place-holder
1662                         -- The renamer fills in the final value
1663
1664 -----------------------------------------------------------------------------
1665 -- Implicit Parameter Bindings
1666
1667 dbinds  :: { Located [LIPBind RdrName] }
1668         : dbinds ';' dbind              { let { this = $3; rest = unLoc $1 }
1669                               in rest `seq` this `seq` LL (this : rest) }
1670         | dbinds ';'                    { LL (unLoc $1) }
1671         | dbind                         { let this = $1 in this `seq` L1 [this] }
1672 --      | {- empty -}                   { [] }
1673
1674 dbind   :: { LIPBind RdrName }
1675 dbind   : ipvar '=' exp                 { LL (IPBind (unLoc $1) $3) }
1676
1677 ipvar   :: { Located (IPName RdrName) }
1678         : IPDUPVARID            { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1679
1680 -----------------------------------------------------------------------------
1681 -- Warnings and deprecations
1682
1683 namelist :: { Located [RdrName] }
1684 namelist : name_var              { L1 [unLoc $1] }
1685          | name_var ',' namelist { LL (unLoc $1 : unLoc $3) }
1686
1687 name_var :: { Located RdrName }
1688 name_var : var { $1 }
1689          | con { $1 }
1690
1691 -----------------------------------------
1692 -- Data constructors
1693 qcon    :: { Located RdrName }
1694         : qconid                { $1 }
1695         | '(' qconsym ')'       { LL (unLoc $2) }
1696         | sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1697 -- The case of '[:' ':]' is part of the production `parr'
1698
1699 con     :: { Located RdrName }
1700         : conid                 { $1 }
1701         | '(' consym ')'        { LL (unLoc $2) }
1702         | sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1703
1704 con_list :: { Located [Located RdrName] }
1705 con_list : con                  { L1 [$1] }
1706          | con ',' con_list     { LL ($1 : unLoc $3) }
1707
1708 sysdcon :: { Located DataCon }  -- Wired in data constructors
1709         : '(' ')'               { LL unitDataCon }
1710         | '(' commas ')'        { LL $ tupleCon Boxed ($2 + 1) }
1711         | '(#' '#)'             { LL $ unboxedSingletonDataCon }
1712         | '(#' commas '#)'      { LL $ tupleCon Unboxed ($2 + 1) }
1713         | '[' ']'               { LL nilDataCon }
1714
1715 conop :: { Located RdrName }
1716         : consym                { $1 }  
1717         | '`' conid '`'         { LL (unLoc $2) }
1718
1719 qconop :: { Located RdrName }
1720         : qconsym               { $1 }
1721         | '`' qconid '`'        { LL (unLoc $2) }
1722
1723 -----------------------------------------------------------------------------
1724 -- Type constructors
1725
1726 gtycon  :: { Located RdrName }  -- A "general" qualified tycon
1727         : oqtycon                       { $1 }
1728         | '(' ')'                       { LL $ getRdrName unitTyCon }
1729         | '(' commas ')'                { LL $ getRdrName (tupleTyCon Boxed ($2 + 1)) }
1730         | '(#' '#)'                     { LL $ getRdrName unboxedSingletonTyCon }
1731         | '(#' commas '#)'              { LL $ getRdrName (tupleTyCon Unboxed ($2 + 1)) }
1732         | '(' '->' ')'                  { LL $ getRdrName funTyCon }
1733         | '[' ']'                       { LL $ listTyCon_RDR }
1734         | '[:' ':]'                     { LL $ parrTyCon_RDR }
1735
1736 oqtycon :: { Located RdrName }  -- An "ordinary" qualified tycon
1737         : qtycon                        { $1 }
1738         | '(' qtyconsym ')'             { LL (unLoc $2) }
1739
1740 qtyconop :: { Located RdrName } -- Qualified or unqualified
1741         : qtyconsym                     { $1 }
1742         | '`' qtycon '`'                { LL (unLoc $2) }
1743
1744 qtycon :: { Located RdrName }   -- Qualified or unqualified
1745         : QCONID                        { L1 $! mkQual tcClsName (getQCONID $1) }
1746         | PREFIXQCONSYM                 { L1 $! mkQual tcClsName (getPREFIXQCONSYM $1) }
1747         | tycon                         { $1 }
1748
1749 tycon   :: { Located RdrName }  -- Unqualified
1750         : CONID                         { L1 $! mkUnqual tcClsName (getCONID $1) }
1751
1752 qtyconsym :: { Located RdrName }
1753         : QCONSYM                       { L1 $! mkQual tcClsName (getQCONSYM $1) }
1754         | tyconsym                      { $1 }
1755
1756 tyconsym :: { Located RdrName }
1757         : CONSYM                        { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1758
1759 -----------------------------------------------------------------------------
1760 -- Operators
1761
1762 op      :: { Located RdrName }   -- used in infix decls
1763         : varop                 { $1 }
1764         | conop                 { $1 }
1765
1766 varop   :: { Located RdrName }
1767         : varsym                { $1 }
1768         | '`' varid '`'         { LL (unLoc $2) }
1769
1770 qop     :: { LHsExpr RdrName }   -- used in sections
1771         : qvarop                { L1 $ HsVar (unLoc $1) }
1772         | qconop                { L1 $ HsVar (unLoc $1) }
1773
1774 qopm    :: { LHsExpr RdrName }   -- used in sections
1775         : qvaropm               { L1 $ HsVar (unLoc $1) }
1776         | qconop                { L1 $ HsVar (unLoc $1) }
1777
1778 qvarop :: { Located RdrName }
1779         : qvarsym               { $1 }
1780         | '`' qvarid '`'        { LL (unLoc $2) }
1781
1782 qvaropm :: { Located RdrName }
1783         : qvarsym_no_minus      { $1 }
1784         | '`' qvarid '`'        { LL (unLoc $2) }
1785
1786 -----------------------------------------------------------------------------
1787 -- Type variables
1788
1789 tyvar   :: { Located RdrName }
1790 tyvar   : tyvarid               { $1 }
1791         | '(' tyvarsym ')'      { LL (unLoc $2) }
1792
1793 tyvarop :: { Located RdrName }
1794 tyvarop : '`' tyvarid '`'       { LL (unLoc $2) }
1795         | tyvarsym              { $1 }
1796         | '.'                   {% parseErrorSDoc (getLoc $1) 
1797                                       (vcat [ptext (sLit "Illegal symbol '.' in type"), 
1798                                              ptext (sLit "Perhaps you intended -XRankNTypes or similar flag"),
1799                                              ptext (sLit "to enable explicit-forall syntax: forall <tvs>. <type>")])
1800                                 }
1801
1802 tyvarid :: { Located RdrName }
1803         : VARID                 { L1 $! mkUnqual tvName (getVARID $1) }
1804         | special_id            { L1 $! mkUnqual tvName (unLoc $1) }
1805         | 'unsafe'              { L1 $! mkUnqual tvName (fsLit "unsafe") }
1806         | 'safe'                { L1 $! mkUnqual tvName (fsLit "safe") }
1807         | 'interruptible'       { L1 $! mkUnqual tvName (fsLit "interruptible") }
1808         | 'threadsafe'          { L1 $! mkUnqual tvName (fsLit "threadsafe") }
1809
1810 tyvarsym :: { Located RdrName }
1811 -- Does not include "!", because that is used for strictness marks
1812 --               or ".", because that separates the quantified type vars from the rest
1813 --               or "*", because that's used for kinds
1814 tyvarsym : VARSYM               { L1 $! mkUnqual tvName (getVARSYM $1) }
1815
1816 -----------------------------------------------------------------------------
1817 -- Variables 
1818
1819 var     :: { Located RdrName }
1820         : varid                 { $1 }
1821         | '(' varsym ')'        { LL (unLoc $2) }
1822
1823 qvar    :: { Located RdrName }
1824         : qvarid                { $1 }
1825         | '(' varsym ')'        { LL (unLoc $2) }
1826         | '(' qvarsym1 ')'      { LL (unLoc $2) }
1827 -- We've inlined qvarsym here so that the decision about
1828 -- whether it's a qvar or a var can be postponed until
1829 -- *after* we see the close paren.
1830
1831 qvarid :: { Located RdrName }
1832         : varid                 { $1 }
1833         | QVARID                { L1 $! mkQual varName (getQVARID $1) }
1834         | PREFIXQVARSYM         { L1 $! mkQual varName (getPREFIXQVARSYM $1) }
1835
1836 varid :: { Located RdrName }
1837         : VARID                 { L1 $! mkUnqual varName (getVARID $1) }
1838         | special_id            { L1 $! mkUnqual varName (unLoc $1) }
1839         | 'unsafe'              { L1 $! mkUnqual varName (fsLit "unsafe") }
1840         | 'safe'                { L1 $! mkUnqual varName (fsLit "safe") }
1841         | 'interruptible'       { L1 $! mkUnqual varName (fsLit "interruptible") }
1842         | 'threadsafe'          { L1 $! mkUnqual varName (fsLit "threadsafe") }
1843         | 'forall'              { L1 $! mkUnqual varName (fsLit "forall") }
1844         | 'family'              { L1 $! mkUnqual varName (fsLit "family") }
1845
1846 qvarsym :: { Located RdrName }
1847         : varsym                { $1 }
1848         | qvarsym1              { $1 }
1849
1850 qvarsym_no_minus :: { Located RdrName }
1851         : varsym_no_minus       { $1 }
1852         | qvarsym1              { $1 }
1853
1854 qvarsym1 :: { Located RdrName }
1855 qvarsym1 : QVARSYM              { L1 $ mkQual varName (getQVARSYM $1) }
1856
1857 varsym :: { Located RdrName }
1858         : varsym_no_minus       { $1 }
1859         | '-'                   { L1 $ mkUnqual varName (fsLit "-") }
1860
1861 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1862         : VARSYM                { L1 $ mkUnqual varName (getVARSYM $1) }
1863         | special_sym           { L1 $ mkUnqual varName (unLoc $1) }
1864
1865
1866 -- These special_ids are treated as keywords in various places, 
1867 -- but as ordinary ids elsewhere.   'special_id' collects all these
1868 -- except 'unsafe', 'interruptible', 'forall', and 'family' whose treatment differs
1869 -- depending on context 
1870 special_id :: { Located FastString }
1871 special_id
1872         : 'as'                  { L1 (fsLit "as") }
1873         | 'qualified'           { L1 (fsLit "qualified") }
1874         | 'hiding'              { L1 (fsLit "hiding") }
1875         | 'export'              { L1 (fsLit "export") }
1876         | 'label'               { L1 (fsLit "label")  }
1877         | 'dynamic'             { L1 (fsLit "dynamic") }
1878         | 'stdcall'             { L1 (fsLit "stdcall") }
1879         | 'ccall'               { L1 (fsLit "ccall") }
1880         | 'prim'                { L1 (fsLit "prim") }
1881         | 'group'               { L1 (fsLit "group") }
1882
1883 special_sym :: { Located FastString }
1884 special_sym : '!'       { L1 (fsLit "!") }
1885             | '.'       { L1 (fsLit ".") }
1886             | '*'       { L1 (fsLit "*") }
1887
1888 -----------------------------------------------------------------------------
1889 -- Data constructors
1890
1891 qconid :: { Located RdrName }   -- Qualified or unqualified
1892         : conid                 { $1 }
1893         | QCONID                { L1 $! mkQual dataName (getQCONID $1) }
1894         | PREFIXQCONSYM         { L1 $! mkQual dataName (getPREFIXQCONSYM $1) }
1895
1896 conid   :: { Located RdrName }
1897         : CONID                 { L1 $ mkUnqual dataName (getCONID $1) }
1898
1899 qconsym :: { Located RdrName }  -- Qualified or unqualified
1900         : consym                { $1 }
1901         | QCONSYM               { L1 $ mkQual dataName (getQCONSYM $1) }
1902
1903 consym :: { Located RdrName }
1904         : CONSYM                { L1 $ mkUnqual dataName (getCONSYM $1) }
1905
1906         -- ':' means only list cons
1907         | ':'                   { L1 $ consDataCon_RDR }
1908
1909
1910 -----------------------------------------------------------------------------
1911 -- Literals
1912
1913 literal :: { Located HsLit }
1914         : CHAR                  { L1 $ HsChar       $ getCHAR $1 }
1915         | STRING                { L1 $ HsString     $ getSTRING $1 }
1916         | PRIMINTEGER           { L1 $ HsIntPrim    $ getPRIMINTEGER $1 }
1917         | PRIMWORD              { L1 $ HsWordPrim    $ getPRIMWORD $1 }
1918         | PRIMCHAR              { L1 $ HsCharPrim   $ getPRIMCHAR $1 }
1919         | PRIMSTRING            { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1920         | PRIMFLOAT             { L1 $ HsFloatPrim  $ getPRIMFLOAT $1 }
1921         | PRIMDOUBLE            { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1922
1923 -----------------------------------------------------------------------------
1924 -- Layout
1925
1926 close :: { () }
1927         : vccurly               { () } -- context popped in lexer.
1928         | error                 {% popContext }
1929
1930 -----------------------------------------------------------------------------
1931 -- Miscellaneous (mostly renamings)
1932
1933 modid   :: { Located ModuleName }
1934         : CONID                 { L1 $ mkModuleNameFS (getCONID $1) }
1935         | QCONID                { L1 $ let (mod,c) = getQCONID $1 in
1936                                   mkModuleNameFS
1937                                    (mkFastString
1938                                      (unpackFS mod ++ '.':unpackFS c))
1939                                 }
1940
1941 commas :: { Int }
1942         : commas ','                    { $1 + 1 }
1943         | ','                           { 1 }
1944
1945 -----------------------------------------------------------------------------
1946 -- Documentation comments
1947
1948 docnext :: { LHsDocString }
1949   : DOCNEXT {% return (L1 (HsDocString (mkFastString (getDOCNEXT $1)))) }
1950
1951 docprev :: { LHsDocString }
1952   : DOCPREV {% return (L1 (HsDocString (mkFastString (getDOCPREV $1)))) }
1953
1954 docnamed :: { Located (String, HsDocString) }
1955   : DOCNAMED {%
1956       let string = getDOCNAMED $1 
1957           (name, rest) = break isSpace string
1958       in return (L1 (name, HsDocString (mkFastString rest))) }
1959
1960 docsection :: { Located (Int, HsDocString) }
1961   : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1962         return (L1 (n, HsDocString (mkFastString doc))) }
1963
1964 moduleheader :: { Maybe LHsDocString }
1965         : DOCNEXT {% let string = getDOCNEXT $1 in
1966                      return (Just (L1 (HsDocString (mkFastString string)))) }
1967
1968 maybe_docprev :: { Maybe LHsDocString }
1969         : docprev                       { Just $1 }
1970         | {- empty -}                   { Nothing }
1971
1972 maybe_docnext :: { Maybe LHsDocString }
1973         : docnext                       { Just $1 }
1974         | {- empty -}                   { Nothing }
1975
1976 {
1977 happyError :: P a
1978 happyError = srcParseFail
1979
1980 getVARID        (L _ (ITvarid    x)) = x
1981 getCONID        (L _ (ITconid    x)) = x
1982 getVARSYM       (L _ (ITvarsym   x)) = x
1983 getCONSYM       (L _ (ITconsym   x)) = x
1984 getQVARID       (L _ (ITqvarid   x)) = x
1985 getQCONID       (L _ (ITqconid   x)) = x
1986 getQVARSYM      (L _ (ITqvarsym  x)) = x
1987 getQCONSYM      (L _ (ITqconsym  x)) = x
1988 getPREFIXQVARSYM (L _ (ITprefixqvarsym  x)) = x
1989 getPREFIXQCONSYM (L _ (ITprefixqconsym  x)) = x
1990 getIPDUPVARID   (L _ (ITdupipvarid   x)) = x
1991 getCHAR         (L _ (ITchar     x)) = x
1992 getSTRING       (L _ (ITstring   x)) = x
1993 getINTEGER      (L _ (ITinteger  x)) = x
1994 getRATIONAL     (L _ (ITrational x)) = x
1995 getPRIMCHAR     (L _ (ITprimchar   x)) = x
1996 getPRIMSTRING   (L _ (ITprimstring x)) = x
1997 getPRIMINTEGER  (L _ (ITprimint    x)) = x
1998 getPRIMWORD     (L _ (ITprimword x)) = x
1999 getPRIMFLOAT    (L _ (ITprimfloat  x)) = x
2000 getPRIMDOUBLE   (L _ (ITprimdouble x)) = x
2001 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
2002 getINLINE       (L _ (ITinline_prag inl conl)) = (inl,conl)
2003 getSPEC_INLINE  (L _ (ITspec_inline_prag True))  = (Inline,  FunLike)
2004 getSPEC_INLINE  (L _ (ITspec_inline_prag False)) = (NoInline,FunLike)
2005
2006 getDOCNEXT (L _ (ITdocCommentNext x)) = x
2007 getDOCPREV (L _ (ITdocCommentPrev x)) = x
2008 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
2009 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
2010
2011 getSCC :: Located Token -> P FastString
2012 getSCC lt = do let s = getSTRING lt
2013                    err = "Spaces are not allowed in SCCs"
2014                -- We probably actually want to be more restrictive than this
2015                if ' ' `elem` unpackFS s
2016                    then failSpanMsgP (getLoc lt) (text err)
2017                    else return s
2018
2019 -- Utilities for combining source spans
2020 comb2 :: Located a -> Located b -> SrcSpan
2021 comb2 a b = a `seq` b `seq` combineLocs a b
2022
2023 comb3 :: Located a -> Located b -> Located c -> SrcSpan
2024 comb3 a b c = a `seq` b `seq` c `seq`
2025     combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
2026
2027 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
2028 comb4 a b c d = a `seq` b `seq` c `seq` d `seq`
2029     (combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
2030                 combineSrcSpans (getLoc c) (getLoc d))
2031
2032 -- strict constructor version:
2033 {-# INLINE sL #-}
2034 sL :: SrcSpan -> a -> Located a
2035 sL span a = span `seq` a `seq` L span a
2036
2037 -- Make a source location for the file.  We're a bit lazy here and just
2038 -- make a point SrcSpan at line 1, column 0.  Strictly speaking we should
2039 -- try to find the span of the whole file (ToDo).
2040 fileSrcSpan :: P SrcSpan
2041 fileSrcSpan = do 
2042   l <- getSrcLoc; 
2043   let loc = mkSrcLoc (srcLocFile l) 1 1;
2044   return (mkSrcSpan loc loc)
2045 }