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