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