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