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