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