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