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