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