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