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