Use FractionalLit more extensively to improve other pretty printers
[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                 { L (comb2 $1 $2) (mkHsDo DoExpr  (unLoc $2)) }
1287         | 'mdo' stmtlist                { L (comb2 $1 $2) (mkHsDo MDoExpr (unLoc $2)) }
1288
1289         | scc_annot exp                         { LL $ if opt_SccProfilingOn
1290                                                         then HsSCC (unLoc $1) $2
1291                                                         else HsPar $2 }
1292         | hpc_annot exp                         { LL $ if opt_Hpc
1293                                                         then HsTickPragma (unLoc $1) $2
1294                                                         else HsPar $2 }
1295
1296         | 'proc' aexp '->' exp  
1297                         {% checkPattern $2 >>= \ p -> 
1298                            return (LL $ HsProc p (LL $ HsCmdTop $4 [] 
1299                                                    placeHolderType undefined)) }
1300                                                 -- TODO: is LL right here?
1301
1302         | '{-# CORE' STRING '#-}' exp           { LL $ HsCoreAnn (getSTRING $2) $4 }
1303                                                     -- hdaume: core annotation
1304         | fexp                                  { $1 }
1305
1306 optSemi :: { Bool }
1307         : ';'         { True }
1308         | {- empty -} { False }
1309
1310 scc_annot :: { Located FastString }
1311         : '_scc_' STRING                        {% (addWarning Opt_WarnWarningsDeprecations (getLoc $1) (text "_scc_ is deprecated; use an SCC pragma instead")) >>= \_ ->
1312                                    ( do scc <- getSCC $2; return $ LL scc ) }
1313         | '{-# SCC' STRING '#-}'                {% do scc <- getSCC $2; return $ LL scc }
1314
1315 hpc_annot :: { Located (FastString,(Int,Int),(Int,Int)) }
1316         : '{-# GENERATED' STRING INTEGER ':' INTEGER '-' INTEGER ':' INTEGER '#-}'
1317                                                 { LL $ (getSTRING $2
1318                                                        ,( fromInteger $ getINTEGER $3
1319                                                         , fromInteger $ getINTEGER $5
1320                                                         )
1321                                                        ,( fromInteger $ getINTEGER $7
1322                                                         , fromInteger $ getINTEGER $9
1323                                                         )
1324                                                        )
1325                                                  }
1326
1327 fexp    :: { LHsExpr RdrName }
1328         : fexp aexp                             { LL $ HsApp $1 $2 }
1329         | aexp                                  { $1 }
1330
1331 aexp    :: { LHsExpr RdrName }
1332         : qvar '@' aexp                 { LL $ EAsPat $1 $3 }
1333         | '~' aexp                      { LL $ ELazyPat $2 }
1334         | aexp1                 { $1 }
1335
1336 aexp1   :: { LHsExpr RdrName }
1337         : aexp1 '{' fbinds '}'  {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) $3
1338                                       ; return (LL r) }}
1339         | aexp2                 { $1 }
1340
1341 -- Here was the syntax for type applications that I was planning
1342 -- but there are difficulties (e.g. what order for type args)
1343 -- so it's not enabled yet.
1344 -- But this case *is* used for the left hand side of a generic definition,
1345 -- which is parsed as an expression before being munged into a pattern
1346         | qcname '{|' type '|}'         { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1347                                                      (sL (getLoc $3) (HsType $3)) }
1348
1349 aexp2   :: { LHsExpr RdrName }
1350         : ipvar                         { L1 (HsIPVar $! unLoc $1) }
1351         | qcname                        { L1 (HsVar   $! unLoc $1) }
1352         | literal                       { L1 (HsLit   $! unLoc $1) }
1353 -- This will enable overloaded strings permanently.  Normally the renamer turns HsString
1354 -- into HsOverLit when -foverloaded-strings is on.
1355 --      | STRING                        { sL (getLoc $1) (HsOverLit $! mkHsIsString (getSTRING $1) placeHolderType) }
1356         | INTEGER                       { sL (getLoc $1) (HsOverLit $! mkHsIntegral (getINTEGER $1) placeHolderType) }
1357         | RATIONAL                      { sL (getLoc $1) (HsOverLit $! mkHsFractional (getRATIONAL $1) placeHolderType) }
1358
1359         -- N.B.: sections get parsed by these next two productions.
1360         -- This allows you to write, e.g., '(+ 3, 4 -)', which isn't
1361         -- correct Haskell (you'd have to write '((+ 3), (4 -))')
1362         -- but the less cluttered version fell out of having texps.
1363         | '(' texp ')'                  { LL (HsPar $2) }
1364         | '(' tup_exprs ')'             { LL (ExplicitTuple $2 Boxed) }
1365
1366         | '(#' texp '#)'                { LL (ExplicitTuple [Present $2] Unboxed) }
1367         | '(#' tup_exprs '#)'           { LL (ExplicitTuple $2 Unboxed) }
1368
1369         | '[' list ']'                  { LL (unLoc $2) }
1370         | '[:' parr ':]'                { LL (unLoc $2) }
1371         | '_'                           { L1 EWildPat }
1372         
1373         -- Template Haskell Extension
1374         | TH_ID_SPLICE          { L1 $ HsSpliceE (mkHsSplice 
1375                                         (L1 $ HsVar (mkUnqual varName 
1376                                                         (getTH_ID_SPLICE $1)))) } 
1377         | '$(' exp ')'          { LL $ HsSpliceE (mkHsSplice $2) }               
1378
1379
1380         | TH_VAR_QUOTE qvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1381         | TH_VAR_QUOTE qcon     { LL $ HsBracket (VarBr (unLoc $2)) }
1382         | TH_TY_QUOTE tyvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1383         | TH_TY_QUOTE gtycon    { LL $ HsBracket (VarBr (unLoc $2)) }
1384         | '[|' exp '|]'         { LL $ HsBracket (ExpBr $2) }                       
1385         | '[t|' ctype '|]'      { LL $ HsBracket (TypBr $2) }                       
1386         | '[p|' infixexp '|]'   {% checkPattern $2 >>= \p ->
1387                                         return (LL $ HsBracket (PatBr p)) }
1388         | '[d|' cvtopbody '|]'  { LL $ HsBracket (DecBrL $2) }
1389         | quasiquote            { L1 (HsQuasiQuoteE (unLoc $1)) }
1390
1391         -- arrow notation extension
1392         | '(|' aexp2 cmdargs '|)'       { LL $ HsArrForm $2 Nothing (reverse $3) }
1393
1394 cmdargs :: { [LHsCmdTop RdrName] }
1395         : cmdargs acmd                  { $2 : $1 }
1396         | {- empty -}                   { [] }
1397
1398 acmd    :: { LHsCmdTop RdrName }
1399         : aexp2                 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1400
1401 cvtopbody :: { [LHsDecl RdrName] }
1402         :  '{'            cvtopdecls0 '}'               { $2 }
1403         |      vocurly    cvtopdecls0 close             { $2 }
1404
1405 cvtopdecls0 :: { [LHsDecl RdrName] }
1406         : {- empty -}           { [] }
1407         | cvtopdecls            { $1 }
1408
1409 -----------------------------------------------------------------------------
1410 -- Tuple expressions
1411
1412 -- "texp" is short for tuple expressions: 
1413 -- things that can appear unparenthesized as long as they're
1414 -- inside parens or delimitted by commas
1415 texp :: { LHsExpr RdrName }
1416         : exp                           { $1 }
1417
1418         -- Note [Parsing sections]
1419         -- ~~~~~~~~~~~~~~~~~~~~~~~
1420         -- We include left and right sections here, which isn't
1421         -- technically right according to the Haskell standard.
1422         -- For example (3 +, True) isn't legal.
1423         -- However, we want to parse bang patterns like
1424         --      (!x, !y)
1425         -- and it's convenient to do so here as a section
1426         -- Then when converting expr to pattern we unravel it again
1427         -- Meanwhile, the renamer checks that real sections appear
1428         -- inside parens.
1429         | infixexp qop  { LL $ SectionL $1 $2 }
1430         | qopm infixexp       { LL $ SectionR $1 $2 }
1431
1432        -- View patterns get parenthesized above
1433         | exp '->' texp   { LL $ EViewPat $1 $3 }
1434
1435 -- Always at least one comma
1436 tup_exprs :: { [HsTupArg RdrName] }
1437            : texp commas_tup_tail  { Present $1 : $2 }
1438            | commas tup_tail       { replicate $1 missingTupArg ++ $2 }
1439
1440 -- Always starts with commas; always follows an expr
1441 commas_tup_tail :: { [HsTupArg RdrName] }
1442 commas_tup_tail : commas tup_tail  { replicate ($1-1) missingTupArg ++ $2 }
1443
1444 -- Always follows a comma
1445 tup_tail :: { [HsTupArg RdrName] }
1446           : texp commas_tup_tail        { Present $1 : $2 }
1447           | texp                        { [Present $1] }
1448           | {- empty -}                 { [missingTupArg] }
1449
1450 -----------------------------------------------------------------------------
1451 -- List expressions
1452
1453 -- The rules below are little bit contorted to keep lexps left-recursive while
1454 -- avoiding another shift/reduce-conflict.
1455
1456 list :: { LHsExpr RdrName }
1457         : texp                  { L1 $ ExplicitList placeHolderType [$1] }
1458         | lexps                 { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1459         | texp '..'             { LL $ ArithSeq noPostTcExpr (From $1) }
1460         | texp ',' exp '..'     { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1461         | texp '..' exp         { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1462         | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1463         | texp '|' flattenedpquals      
1464              {% checkMonadComp >>= \ ctxt ->
1465                 return (sL (comb2 $1 $>) $ 
1466                         mkHsComp ctxt (unLoc $3) $1) }
1467
1468 lexps :: { Located [LHsExpr RdrName] }
1469         : lexps ',' texp                { LL (((:) $! $3) $! unLoc $1) }
1470         | texp ',' texp                 { LL [$3,$1] }
1471
1472 -----------------------------------------------------------------------------
1473 -- List Comprehensions
1474
1475 flattenedpquals :: { Located [LStmt RdrName] }
1476     : pquals   { case (unLoc $1) of
1477                     [qs] -> L1 qs
1478                     -- We just had one thing in our "parallel" list so 
1479                     -- we simply return that thing directly
1480                     
1481                     qss -> L1 [L1 $ ParStmt [(qs, undefined) | qs <- qss] noSyntaxExpr noSyntaxExpr noSyntaxExpr]
1482                     -- We actually found some actual parallel lists so
1483                     -- we wrap them into as a ParStmt
1484                 }
1485
1486 pquals :: { Located [[LStmt RdrName]] }
1487     : squals '|' pquals     { L (getLoc $2) (reverse (unLoc $1) : unLoc $3) }
1488     | squals                { L (getLoc $1) [reverse (unLoc $1)] }
1489
1490 squals :: { Located [LStmt RdrName] }   -- In reverse order, because the last 
1491                                         -- one can "grab" the earlier ones
1492     : squals ',' transformqual               { LL [L (getLoc $3) ((unLoc $3) (reverse (unLoc $1)))] }
1493     | squals ',' qual                        { LL ($3 : unLoc $1) }
1494     | transformqual                          { LL [L (getLoc $1) ((unLoc $1) [])] }
1495     | qual                                   { L1 [$1] }
1496 --  | transformquals1 ',' '{|' pquals '|}'   { LL ($4 : unLoc $1) }
1497 --  | '{|' pquals '|}'                       { L1 [$2] }
1498
1499
1500 -- It is possible to enable bracketing (associating) qualifier lists by uncommenting the lines with {| |}
1501 -- above. Due to a lack of consensus on the syntax, this feature is not being used until we get user
1502 -- demand. Note that the {| |} symbols are reused from -XGenerics and hence if you want to compile
1503 -- a program that makes use of this temporary syntax you must supply that flag to GHC
1504
1505 transformqual :: { Located ([LStmt RdrName] -> Stmt RdrName) }
1506                         -- Function is applied to a list of stmts *in order*
1507     : 'then' exp                { LL $ \leftStmts -> (mkTransformStmt leftStmts $2) }
1508     -- >>>
1509     | 'then' exp 'by' exp       { LL $ \leftStmts -> (mkTransformByStmt leftStmts $2 $4) }
1510     | 'then' 'group' 'by' exp   { LL $ \leftStmts -> (mkGroupByStmt leftStmts $4) }
1511     -- <<<
1512     -- These two productions deliberately have a shift-reduce conflict. I have made 'group' into a special_id,
1513     -- which means you can enable TransformListComp while still using Data.List.group. However, this makes the two
1514     -- productions ambiguous. I've set things up so that Happy chooses to resolve the conflict in that case by
1515     -- choosing the "group by" variant, which is what we want.
1516     --
1517     -- This is rather dubious: the user might be confused as to how to parse this statement. However, it is a good
1518     -- practical choice. NB: Data.List.group :: [a] -> [[a]], so using the first production would not even type check
1519     -- if /that/ is the group function we conflict with.
1520     | 'then' 'group' 'using' exp           { LL $ \leftStmts -> (mkGroupUsingStmt leftStmts $4) }
1521     | 'then' 'group' 'by' exp 'using' exp  { LL $ \leftStmts -> (mkGroupByUsingStmt leftStmts $4 $6) }
1522
1523 -----------------------------------------------------------------------------
1524 -- Parallel array expressions
1525
1526 -- The rules below are little bit contorted; see the list case for details.
1527 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1528 -- Moreover, we allow explicit arrays with no element (represented by the nil
1529 -- constructor in the list case).
1530
1531 parr :: { LHsExpr RdrName }
1532         :                               { noLoc (ExplicitPArr placeHolderType []) }
1533         | texp                          { L1 $ ExplicitPArr placeHolderType [$1] }
1534         | lexps                         { L1 $ ExplicitPArr placeHolderType 
1535                                                        (reverse (unLoc $1)) }
1536         | texp '..' exp                 { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1537         | texp ',' exp '..' exp         { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1538         | texp '|' flattenedpquals      { LL $ mkHsComp PArrComp (unLoc $3) $1 }
1539
1540 -- We are reusing `lexps' and `flattenedpquals' from the list case.
1541
1542 -----------------------------------------------------------------------------
1543 -- Guards
1544
1545 guardquals :: { Located [LStmt RdrName] }
1546     : guardquals1           { L (getLoc $1) (reverse (unLoc $1)) }
1547
1548 guardquals1 :: { Located [LStmt RdrName] }
1549     : guardquals1 ',' qual  { LL ($3 : unLoc $1) }
1550     | qual                  { L1 [$1] }
1551
1552 -----------------------------------------------------------------------------
1553 -- Case alternatives
1554
1555 altslist :: { Located [LMatch RdrName] }
1556         : '{'            alts '}'       { LL (reverse (unLoc $2)) }
1557         |     vocurly    alts  close    { L (getLoc $2) (reverse (unLoc $2)) }
1558
1559 alts    :: { Located [LMatch RdrName] }
1560         : alts1                         { L1 (unLoc $1) }
1561         | ';' alts                      { LL (unLoc $2) }
1562
1563 alts1   :: { Located [LMatch RdrName] }
1564         : alts1 ';' alt                 { LL ($3 : unLoc $1) }
1565         | alts1 ';'                     { LL (unLoc $1) }
1566         | alt                           { L1 [$1] }
1567
1568 alt     :: { LMatch RdrName }
1569         : pat opt_sig alt_rhs           { LL (Match [$1] $2 (unLoc $3)) }
1570
1571 alt_rhs :: { Located (GRHSs RdrName) }
1572         : ralt wherebinds               { LL (GRHSs (unLoc $1) (unLoc $2)) }
1573
1574 ralt :: { Located [LGRHS RdrName] }
1575         : '->' exp                      { LL (unguardedRHS $2) }
1576         | gdpats                        { L1 (reverse (unLoc $1)) }
1577
1578 gdpats :: { Located [LGRHS RdrName] }
1579         : gdpats gdpat                  { LL ($2 : unLoc $1) }
1580         | gdpat                         { L1 [$1] }
1581
1582 gdpat   :: { LGRHS RdrName }
1583         : '|' guardquals '->' exp               { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }
1584
1585 -- 'pat' recognises a pattern, including one with a bang at the top
1586 --      e.g.  "!x" or "!(x,y)" or "C a b" etc
1587 -- Bangs inside are parsed as infix operator applications, so that
1588 -- we parse them right when bang-patterns are off
1589 pat     :: { LPat RdrName }
1590 pat     :  exp                  {% checkPattern $1 }
1591         | '!' aexp              {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1592
1593 apat   :: { LPat RdrName }      
1594 apat    : aexp                  {% checkPattern $1 }
1595         | '!' aexp              {% checkPattern (LL (SectionR (L1 (HsVar bang_RDR)) $2)) }
1596
1597 apats  :: { [LPat RdrName] }
1598         : apat apats            { $1 : $2 }
1599         | {- empty -}           { [] }
1600
1601 -----------------------------------------------------------------------------
1602 -- Statement sequences
1603
1604 stmtlist :: { Located [LStmt RdrName] }
1605         : '{'           stmts '}'       { LL (unLoc $2) }
1606         |     vocurly   stmts close     { $2 }
1607
1608 --      do { ;; s ; s ; ; s ;; }
1609 -- The last Stmt should be an expression, but that's hard to enforce
1610 -- here, because we need too much lookahead if we see do { e ; }
1611 -- So we use ExprStmts throughout, and switch the last one over
1612 -- in ParseUtils.checkDo instead
1613 stmts :: { Located [LStmt RdrName] }
1614         : stmt stmts_help               { LL ($1 : unLoc $2) }
1615         | ';' stmts                     { LL (unLoc $2) }
1616         | {- empty -}                   { noLoc [] }
1617
1618 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1619         : ';' stmts                     { LL (unLoc $2) }
1620         | {- empty -}                   { noLoc [] }
1621
1622 -- For typing stmts at the GHCi prompt, where 
1623 -- the input may consist of just comments.
1624 maybe_stmt :: { Maybe (LStmt RdrName) }
1625         : stmt                          { Just $1 }
1626         | {- nothing -}                 { Nothing }
1627
1628 stmt  :: { LStmt RdrName }
1629         : qual                              { $1 }
1630         | 'rec' stmtlist                { LL $ mkRecStmt (unLoc $2) }
1631
1632 qual  :: { LStmt RdrName }
1633     : pat '<-' exp                      { LL $ mkBindStmt $1 $3 }
1634     | exp                                   { L1 $ mkExprStmt $1 }
1635     | 'let' binds                       { LL $ LetStmt (unLoc $2) }
1636
1637 -----------------------------------------------------------------------------
1638 -- Record Field Update/Construction
1639
1640 fbinds  :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1641         : fbinds1                       { $1 }
1642         | {- empty -}                   { ([], False) }
1643
1644 fbinds1 :: { ([HsRecField RdrName (LHsExpr RdrName)], Bool) }
1645         : fbind ',' fbinds1             { case $3 of (flds, dd) -> ($1 : flds, dd) } 
1646         | fbind                         { ([$1], False) }
1647         | '..'                          { ([],   True) }
1648   
1649 fbind   :: { HsRecField RdrName (LHsExpr RdrName) }
1650         : qvar '=' exp  { HsRecField $1 $3                False }
1651         | qvar          { HsRecField $1 placeHolderPunRhs True }
1652                         -- In the punning case, use a place-holder
1653                         -- The renamer fills in the final value
1654
1655 -----------------------------------------------------------------------------
1656 -- Implicit Parameter Bindings
1657
1658 dbinds  :: { Located [LIPBind RdrName] }
1659         : dbinds ';' dbind              { let { this = $3; rest = unLoc $1 }
1660                               in rest `seq` this `seq` LL (this : rest) }
1661         | dbinds ';'                    { LL (unLoc $1) }
1662         | dbind                         { let this = $1 in this `seq` L1 [this] }
1663 --      | {- empty -}                   { [] }
1664
1665 dbind   :: { LIPBind RdrName }
1666 dbind   : ipvar '=' exp                 { LL (IPBind (unLoc $1) $3) }
1667
1668 ipvar   :: { Located (IPName RdrName) }
1669         : IPDUPVARID            { L1 (IPName (mkUnqual varName (getIPDUPVARID $1))) }
1670
1671 -----------------------------------------------------------------------------
1672 -- Warnings and deprecations
1673
1674 namelist :: { Located [RdrName] }
1675 namelist : name_var              { L1 [unLoc $1] }
1676          | name_var ',' namelist { LL (unLoc $1 : unLoc $3) }
1677
1678 name_var :: { Located RdrName }
1679 name_var : var { $1 }
1680          | con { $1 }
1681
1682 -----------------------------------------
1683 -- Data constructors
1684 qcon    :: { Located RdrName }
1685         : qconid                { $1 }
1686         | '(' qconsym ')'       { LL (unLoc $2) }
1687         | sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1688 -- The case of '[:' ':]' is part of the production `parr'
1689
1690 con     :: { Located RdrName }
1691         : conid                 { $1 }
1692         | '(' consym ')'        { LL (unLoc $2) }
1693         | sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1694
1695 con_list :: { Located [Located RdrName] }
1696 con_list : con                  { L1 [$1] }
1697          | con ',' con_list     { LL ($1 : unLoc $3) }
1698
1699 sysdcon :: { Located DataCon }  -- Wired in data constructors
1700         : '(' ')'               { LL unitDataCon }
1701         | '(' commas ')'        { LL $ tupleCon Boxed ($2 + 1) }
1702         | '(#' '#)'             { LL $ unboxedSingletonDataCon }
1703         | '(#' commas '#)'      { LL $ tupleCon Unboxed ($2 + 1) }
1704         | '[' ']'               { LL nilDataCon }
1705
1706 conop :: { Located RdrName }
1707         : consym                { $1 }  
1708         | '`' conid '`'         { LL (unLoc $2) }
1709
1710 qconop :: { Located RdrName }
1711         : qconsym               { $1 }
1712         | '`' qconid '`'        { LL (unLoc $2) }
1713
1714 -----------------------------------------------------------------------------
1715 -- Type constructors
1716
1717 gtycon  :: { Located RdrName }  -- A "general" qualified tycon
1718         : oqtycon                       { $1 }
1719         | '(' ')'                       { LL $ getRdrName unitTyCon }
1720         | '(' commas ')'                { LL $ getRdrName (tupleTyCon Boxed ($2 + 1)) }
1721         | '(#' '#)'                     { LL $ getRdrName unboxedSingletonTyCon }
1722         | '(#' commas '#)'              { LL $ getRdrName (tupleTyCon Unboxed ($2 + 1)) }
1723         | '(' '->' ')'                  { LL $ getRdrName funTyCon }
1724         | '[' ']'                       { LL $ listTyCon_RDR }
1725         | '[:' ':]'                     { LL $ parrTyCon_RDR }
1726
1727 oqtycon :: { Located RdrName }  -- An "ordinary" qualified tycon
1728         : qtycon                        { $1 }
1729         | '(' qtyconsym ')'             { LL (unLoc $2) }
1730
1731 qtyconop :: { Located RdrName } -- Qualified or unqualified
1732         : qtyconsym                     { $1 }
1733         | '`' qtycon '`'                { LL (unLoc $2) }
1734
1735 qtycon :: { Located RdrName }   -- Qualified or unqualified
1736         : QCONID                        { L1 $! mkQual tcClsName (getQCONID $1) }
1737         | PREFIXQCONSYM                 { L1 $! mkQual tcClsName (getPREFIXQCONSYM $1) }
1738         | tycon                         { $1 }
1739
1740 tycon   :: { Located RdrName }  -- Unqualified
1741         : CONID                         { L1 $! mkUnqual tcClsName (getCONID $1) }
1742
1743 qtyconsym :: { Located RdrName }
1744         : QCONSYM                       { L1 $! mkQual tcClsName (getQCONSYM $1) }
1745         | tyconsym                      { $1 }
1746
1747 tyconsym :: { Located RdrName }
1748         : CONSYM                        { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1749
1750 -----------------------------------------------------------------------------
1751 -- Operators
1752
1753 op      :: { Located RdrName }   -- used in infix decls
1754         : varop                 { $1 }
1755         | conop                 { $1 }
1756
1757 varop   :: { Located RdrName }
1758         : varsym                { $1 }
1759         | '`' varid '`'         { LL (unLoc $2) }
1760
1761 qop     :: { LHsExpr RdrName }   -- used in sections
1762         : qvarop                { L1 $ HsVar (unLoc $1) }
1763         | qconop                { L1 $ HsVar (unLoc $1) }
1764
1765 qopm    :: { LHsExpr RdrName }   -- used in sections
1766         : qvaropm               { L1 $ HsVar (unLoc $1) }
1767         | qconop                { L1 $ HsVar (unLoc $1) }
1768
1769 qvarop :: { Located RdrName }
1770         : qvarsym               { $1 }
1771         | '`' qvarid '`'        { LL (unLoc $2) }
1772
1773 qvaropm :: { Located RdrName }
1774         : qvarsym_no_minus      { $1 }
1775         | '`' qvarid '`'        { LL (unLoc $2) }
1776
1777 -----------------------------------------------------------------------------
1778 -- Type variables
1779
1780 tyvar   :: { Located RdrName }
1781 tyvar   : tyvarid               { $1 }
1782         | '(' tyvarsym ')'      { LL (unLoc $2) }
1783
1784 tyvarop :: { Located RdrName }
1785 tyvarop : '`' tyvarid '`'       { LL (unLoc $2) }
1786         | tyvarsym              { $1 }
1787         | '.'                   {% parseErrorSDoc (getLoc $1) 
1788                                       (vcat [ptext (sLit "Illegal symbol '.' in type"), 
1789                                              ptext (sLit "Perhaps you intended -XRankNTypes or similar flag"),
1790                                              ptext (sLit "to enable explicit-forall syntax: forall <tvs>. <type>")])
1791                                 }
1792
1793 tyvarid :: { Located RdrName }
1794         : VARID                 { L1 $! mkUnqual tvName (getVARID $1) }
1795         | special_id            { L1 $! mkUnqual tvName (unLoc $1) }
1796         | 'unsafe'              { L1 $! mkUnqual tvName (fsLit "unsafe") }
1797         | 'safe'                { L1 $! mkUnqual tvName (fsLit "safe") }
1798         | 'interruptible'       { L1 $! mkUnqual tvName (fsLit "interruptible") }
1799         | 'threadsafe'          { L1 $! mkUnqual tvName (fsLit "threadsafe") }
1800
1801 tyvarsym :: { Located RdrName }
1802 -- Does not include "!", because that is used for strictness marks
1803 --               or ".", because that separates the quantified type vars from the rest
1804 --               or "*", because that's used for kinds
1805 tyvarsym : VARSYM               { L1 $! mkUnqual tvName (getVARSYM $1) }
1806
1807 -----------------------------------------------------------------------------
1808 -- Variables 
1809
1810 var     :: { Located RdrName }
1811         : varid                 { $1 }
1812         | '(' varsym ')'        { LL (unLoc $2) }
1813
1814 qvar    :: { Located RdrName }
1815         : qvarid                { $1 }
1816         | '(' varsym ')'        { LL (unLoc $2) }
1817         | '(' qvarsym1 ')'      { LL (unLoc $2) }
1818 -- We've inlined qvarsym here so that the decision about
1819 -- whether it's a qvar or a var can be postponed until
1820 -- *after* we see the close paren.
1821
1822 qvarid :: { Located RdrName }
1823         : varid                 { $1 }
1824         | QVARID                { L1 $! mkQual varName (getQVARID $1) }
1825         | PREFIXQVARSYM         { L1 $! mkQual varName (getPREFIXQVARSYM $1) }
1826
1827 varid :: { Located RdrName }
1828         : VARID                 { L1 $! mkUnqual varName (getVARID $1) }
1829         | special_id            { L1 $! mkUnqual varName (unLoc $1) }
1830         | 'unsafe'              { L1 $! mkUnqual varName (fsLit "unsafe") }
1831         | 'safe'                { L1 $! mkUnqual varName (fsLit "safe") }
1832         | 'interruptible'       { L1 $! mkUnqual varName (fsLit "interruptible") }
1833         | 'threadsafe'          { L1 $! mkUnqual varName (fsLit "threadsafe") }
1834         | 'forall'              { L1 $! mkUnqual varName (fsLit "forall") }
1835         | 'family'              { L1 $! mkUnqual varName (fsLit "family") }
1836
1837 qvarsym :: { Located RdrName }
1838         : varsym                { $1 }
1839         | qvarsym1              { $1 }
1840
1841 qvarsym_no_minus :: { Located RdrName }
1842         : varsym_no_minus       { $1 }
1843         | qvarsym1              { $1 }
1844
1845 qvarsym1 :: { Located RdrName }
1846 qvarsym1 : QVARSYM              { L1 $ mkQual varName (getQVARSYM $1) }
1847
1848 varsym :: { Located RdrName }
1849         : varsym_no_minus       { $1 }
1850         | '-'                   { L1 $ mkUnqual varName (fsLit "-") }
1851
1852 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1853         : VARSYM                { L1 $ mkUnqual varName (getVARSYM $1) }
1854         | special_sym           { L1 $ mkUnqual varName (unLoc $1) }
1855
1856
1857 -- These special_ids are treated as keywords in various places, 
1858 -- but as ordinary ids elsewhere.   'special_id' collects all these
1859 -- except 'unsafe', 'interruptible', 'forall', and 'family' whose treatment differs
1860 -- depending on context 
1861 special_id :: { Located FastString }
1862 special_id
1863         : 'as'                  { L1 (fsLit "as") }
1864         | 'qualified'           { L1 (fsLit "qualified") }
1865         | 'hiding'              { L1 (fsLit "hiding") }
1866         | 'export'              { L1 (fsLit "export") }
1867         | 'label'               { L1 (fsLit "label")  }
1868         | 'dynamic'             { L1 (fsLit "dynamic") }
1869         | 'stdcall'             { L1 (fsLit "stdcall") }
1870         | 'ccall'               { L1 (fsLit "ccall") }
1871         | 'prim'                { L1 (fsLit "prim") }
1872         | 'group'               { L1 (fsLit "group") }
1873
1874 special_sym :: { Located FastString }
1875 special_sym : '!'       { L1 (fsLit "!") }
1876             | '.'       { L1 (fsLit ".") }
1877             | '*'       { L1 (fsLit "*") }
1878
1879 -----------------------------------------------------------------------------
1880 -- Data constructors
1881
1882 qconid :: { Located RdrName }   -- Qualified or unqualified
1883         : conid                 { $1 }
1884         | QCONID                { L1 $! mkQual dataName (getQCONID $1) }
1885         | PREFIXQCONSYM         { L1 $! mkQual dataName (getPREFIXQCONSYM $1) }
1886
1887 conid   :: { Located RdrName }
1888         : CONID                 { L1 $ mkUnqual dataName (getCONID $1) }
1889
1890 qconsym :: { Located RdrName }  -- Qualified or unqualified
1891         : consym                { $1 }
1892         | QCONSYM               { L1 $ mkQual dataName (getQCONSYM $1) }
1893
1894 consym :: { Located RdrName }
1895         : CONSYM                { L1 $ mkUnqual dataName (getCONSYM $1) }
1896
1897         -- ':' means only list cons
1898         | ':'                   { L1 $ consDataCon_RDR }
1899
1900
1901 -----------------------------------------------------------------------------
1902 -- Literals
1903
1904 literal :: { Located HsLit }
1905         : CHAR                  { L1 $ HsChar       $ getCHAR $1 }
1906         | STRING                { L1 $ HsString     $ getSTRING $1 }
1907         | PRIMINTEGER           { L1 $ HsIntPrim    $ getPRIMINTEGER $1 }
1908         | PRIMWORD              { L1 $ HsWordPrim    $ getPRIMWORD $1 }
1909         | PRIMCHAR              { L1 $ HsCharPrim   $ getPRIMCHAR $1 }
1910         | PRIMSTRING            { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1911         | PRIMFLOAT             { L1 $ HsFloatPrim  $ getPRIMFLOAT $1 }
1912         | PRIMDOUBLE            { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1913
1914 -----------------------------------------------------------------------------
1915 -- Layout
1916
1917 close :: { () }
1918         : vccurly               { () } -- context popped in lexer.
1919         | error                 {% popContext }
1920
1921 -----------------------------------------------------------------------------
1922 -- Miscellaneous (mostly renamings)
1923
1924 modid   :: { Located ModuleName }
1925         : CONID                 { L1 $ mkModuleNameFS (getCONID $1) }
1926         | QCONID                { L1 $ let (mod,c) = getQCONID $1 in
1927                                   mkModuleNameFS
1928                                    (mkFastString
1929                                      (unpackFS mod ++ '.':unpackFS c))
1930                                 }
1931
1932 commas :: { Int }
1933         : commas ','                    { $1 + 1 }
1934         | ','                           { 1 }
1935
1936 -----------------------------------------------------------------------------
1937 -- Documentation comments
1938
1939 docnext :: { LHsDocString }
1940   : DOCNEXT {% return (L1 (HsDocString (mkFastString (getDOCNEXT $1)))) }
1941
1942 docprev :: { LHsDocString }
1943   : DOCPREV {% return (L1 (HsDocString (mkFastString (getDOCPREV $1)))) }
1944
1945 docnamed :: { Located (String, HsDocString) }
1946   : DOCNAMED {%
1947       let string = getDOCNAMED $1 
1948           (name, rest) = break isSpace string
1949       in return (L1 (name, HsDocString (mkFastString rest))) }
1950
1951 docsection :: { Located (Int, HsDocString) }
1952   : DOCSECTION {% let (n, doc) = getDOCSECTION $1 in
1953         return (L1 (n, HsDocString (mkFastString doc))) }
1954
1955 moduleheader :: { Maybe LHsDocString }
1956         : DOCNEXT {% let string = getDOCNEXT $1 in
1957                      return (Just (L1 (HsDocString (mkFastString string)))) }
1958
1959 maybe_docprev :: { Maybe LHsDocString }
1960         : docprev                       { Just $1 }
1961         | {- empty -}                   { Nothing }
1962
1963 maybe_docnext :: { Maybe LHsDocString }
1964         : docnext                       { Just $1 }
1965         | {- empty -}                   { Nothing }
1966
1967 {
1968 happyError :: P a
1969 happyError = srcParseFail
1970
1971 getVARID        (L _ (ITvarid    x)) = x
1972 getCONID        (L _ (ITconid    x)) = x
1973 getVARSYM       (L _ (ITvarsym   x)) = x
1974 getCONSYM       (L _ (ITconsym   x)) = x
1975 getQVARID       (L _ (ITqvarid   x)) = x
1976 getQCONID       (L _ (ITqconid   x)) = x
1977 getQVARSYM      (L _ (ITqvarsym  x)) = x
1978 getQCONSYM      (L _ (ITqconsym  x)) = x
1979 getPREFIXQVARSYM (L _ (ITprefixqvarsym  x)) = x
1980 getPREFIXQCONSYM (L _ (ITprefixqconsym  x)) = x
1981 getIPDUPVARID   (L _ (ITdupipvarid   x)) = x
1982 getCHAR         (L _ (ITchar     x)) = x
1983 getSTRING       (L _ (ITstring   x)) = x
1984 getINTEGER      (L _ (ITinteger  x)) = x
1985 getRATIONAL     (L _ (ITrational x)) = x
1986 getPRIMCHAR     (L _ (ITprimchar   x)) = x
1987 getPRIMSTRING   (L _ (ITprimstring x)) = x
1988 getPRIMINTEGER  (L _ (ITprimint    x)) = x
1989 getPRIMWORD     (L _ (ITprimword x)) = x
1990 getPRIMFLOAT    (L _ (ITprimfloat  x)) = x
1991 getPRIMDOUBLE   (L _ (ITprimdouble x)) = x
1992 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1993 getINLINE       (L _ (ITinline_prag inl conl)) = (inl,conl)
1994 getSPEC_INLINE  (L _ (ITspec_inline_prag True))  = (Inline,  FunLike)
1995 getSPEC_INLINE  (L _ (ITspec_inline_prag False)) = (NoInline,FunLike)
1996
1997 getDOCNEXT (L _ (ITdocCommentNext x)) = x
1998 getDOCPREV (L _ (ITdocCommentPrev x)) = x
1999 getDOCNAMED (L _ (ITdocCommentNamed x)) = x
2000 getDOCSECTION (L _ (ITdocSection n x)) = (n, x)
2001
2002 getSCC :: Located Token -> P FastString
2003 getSCC lt = do let s = getSTRING lt
2004                    err = "Spaces are not allowed in SCCs"
2005                -- We probably actually want to be more restrictive than this
2006                if ' ' `elem` unpackFS s
2007                    then failSpanMsgP (getLoc lt) (text err)
2008                    else return s
2009
2010 -- Utilities for combining source spans
2011 comb2 :: Located a -> Located b -> SrcSpan
2012 comb2 a b = a `seq` b `seq` combineLocs a b
2013
2014 comb3 :: Located a -> Located b -> Located c -> SrcSpan
2015 comb3 a b c = a `seq` b `seq` c `seq`
2016     combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
2017
2018 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
2019 comb4 a b c d = a `seq` b `seq` c `seq` d `seq`
2020     (combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
2021                 combineSrcSpans (getLoc c) (getLoc d))
2022
2023 -- strict constructor version:
2024 {-# INLINE sL #-}
2025 sL :: SrcSpan -> a -> Located a
2026 sL span a = span `seq` a `seq` L span a
2027
2028 -- Make a source location for the file.  We're a bit lazy here and just
2029 -- make a point SrcSpan at line 1, column 0.  Strictly speaking we should
2030 -- try to find the span of the whole file (ToDo).
2031 fileSrcSpan :: P SrcSpan
2032 fileSrcSpan = do 
2033   l <- getSrcLoc; 
2034   let loc = mkSrcLoc (srcLocFile l) 1 1;
2035   return (mkSrcSpan loc loc)
2036 }