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