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