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