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