[project @ 2005-11-12 21:41:12 by simonpj]
[ghc-hetmet.git] / ghc / 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 module Parser ( parseModule, parseStmt, parseIdentifier, parseType,
12                 parseHeader ) where
13
14 #define INCLUDE #include 
15 INCLUDE "HsVersions.h"
16
17 import HsSyn
18 import RdrHsSyn
19 import HscTypes         ( IsBootInterface, DeprecTxt )
20 import Lexer
21 import RdrName
22 import TysWiredIn       ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
23                           listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
24 import Type             ( funTyCon )
25 import ForeignCall      ( Safety(..), CExportSpec(..), CLabelString,
26                           CCallConv(..), CCallTarget(..), defaultCCallConv
27                         )
28 import OccName          ( UserFS, varName, dataName, tcClsName, tvName )
29 import DataCon          ( DataCon, dataConName )
30 import SrcLoc           ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
31                           SrcSpan, combineLocs, srcLocFile, 
32                           mkSrcLoc, mkSrcSpan )
33 import Module
34 import StaticFlags      ( opt_SccProfilingOn )
35 import Type             ( Kind, mkArrowKind, liftedTypeKind )
36 import BasicTypes       ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
37                           Activation(..), InlineSpec(..), defaultInlineSpec )
38 import OrdList
39 import Panic
40
41 import FastString
42 import Maybes           ( orElse )
43 import Outputable
44 import GLAEXTS
45 }
46
47 {-
48 -----------------------------------------------------------------------------
49 Conflicts: 36 shift/reduce (1.25)
50
51 10 for abiguity in 'if x then y else z + 1'             [State 178]
52         (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
53         10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
54
55 1 for ambiguity in 'if x then y else z :: T'            [State 178]
56         (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
57
58 4 for ambiguity in 'if x then y else z -< e'            [State 178]
59         (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
60         There are four such operators: -<, >-, -<<, >>-
61
62
63 2 for ambiguity in 'case v of { x :: T -> T ... } '     [States 11, 253]
64         Which of these two is intended?
65           case v of
66             (x::T) -> T         -- Rhs is T
67     or
68           case v of
69             (x::T -> T) -> ..   -- Rhs is ...
70
71 10 for ambiguity in 'e :: a `b` c'.  Does this mean     [States 11, 253]
72         (e::a) `b` c, or 
73         (e :: (a `b` c))
74     As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
75     Same duplication between states 11 and 253 as the previous case
76
77 1 for ambiguity in 'let ?x ...'                         [State 329]
78         the parser can't tell whether the ?x is the lhs of a normal binding or
79         an implicit binding.  Fortunately resolving as shift gives it the only
80         sensible meaning, namely the lhs of an implicit binding.
81
82 1 for ambiguity in '{-# RULES "name" [ ... #-}          [State 382]
83         we don't know whether the '[' starts the activation or not: it
84         might be the start of the declaration with the activation being
85         empty.  --SDM 1/4/2002
86
87 6 for conflicts between `fdecl' and `fdeclDEPRECATED',  [States 393,394]
88         which are resolved correctly, and moreover, 
89         should go away when `fdeclDEPRECATED' is removed.
90
91 1 for ambiguity in '{-# RULES "name" forall = ... #-}'  [State 474]
92         since 'forall' is a valid variable name, we don't know whether
93         to treat a forall on the input as the beginning of a quantifier
94         or the beginning of the rule itself.  Resolving to shift means
95         it's always treated as a quantifier, hence the above is disallowed.
96         This saves explicitly defining a grammar for the rule lhs that
97         doesn't include 'forall'.
98
99 -- ---------------------------------------------------------------------------
100 -- Adding location info
101
102 This is done in a stylised way using the three macros below, L0, L1
103 and LL.  Each of these macros can be thought of as having type
104
105    L0, L1, LL :: a -> Located a
106
107 They each add a SrcSpan to their argument.
108
109    L0   adds 'noSrcSpan', used for empty productions
110
111    L1   for a production with a single token on the lhs.  Grabs the SrcSpan
112         from that token.
113
114    LL   for a production with >1 token on the lhs.  Makes up a SrcSpan from
115         the first and last tokens.
116
117 These suffice for the majority of cases.  However, we must be
118 especially careful with empty productions: LL won't work if the first
119 or last token on the lhs can represent an empty span.  In these cases,
120 we have to calculate the span using more of the tokens from the lhs, eg.
121
122         | 'newtype' tycl_hdr '=' newconstr deriving
123                 { L (comb3 $1 $4 $5)
124                     (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
125
126 We provide comb3 and comb4 functions which are useful in such cases.
127
128 Be careful: there's no checking that you actually got this right, the
129 only symptom will be that the SrcSpans of your syntax will be
130 incorrect.
131
132 /*
133  * We must expand these macros *before* running Happy, which is why this file is
134  * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
135  */
136 #define L0   L noSrcSpan
137 #define L1   sL (getLoc $1)
138 #define LL   sL (comb2 $1 $>)
139
140 -- -----------------------------------------------------------------------------
141
142 -}
143
144 %token
145  '_'            { L _ ITunderscore }            -- Haskell keywords
146  'as'           { L _ ITas }
147  'case'         { L _ ITcase }          
148  'class'        { L _ ITclass } 
149  'data'         { L _ ITdata } 
150  'default'      { L _ ITdefault }
151  'deriving'     { L _ ITderiving }
152  'do'           { L _ ITdo }
153  'else'         { L _ ITelse }
154  'hiding'       { L _ IThiding }
155  'if'           { L _ ITif }
156  'import'       { L _ ITimport }
157  'in'           { L _ ITin }
158  'infix'        { L _ ITinfix }
159  'infixl'       { L _ ITinfixl }
160  'infixr'       { L _ ITinfixr }
161  'instance'     { L _ ITinstance }
162  'let'          { L _ ITlet }
163  'module'       { L _ ITmodule }
164  'newtype'      { L _ ITnewtype }
165  'of'           { L _ ITof }
166  'qualified'    { L _ ITqualified }
167  'then'         { L _ ITthen }
168  'type'         { L _ ITtype }
169  'where'        { L _ ITwhere }
170  '_scc_'        { L _ ITscc }         -- ToDo: remove
171
172  'forall'       { L _ ITforall }                        -- GHC extension keywords
173  'foreign'      { L _ ITforeign }
174  'export'       { L _ ITexport }
175  'label'        { L _ ITlabel } 
176  'dynamic'      { L _ ITdynamic }
177  'safe'         { L _ ITsafe }
178  'threadsafe'   { L _ ITthreadsafe }
179  'unsafe'       { L _ ITunsafe }
180  'mdo'          { L _ ITmdo }
181  'stdcall'      { L _ ITstdcallconv }
182  'ccall'        { L _ ITccallconv }
183  'dotnet'       { L _ ITdotnet }
184  'proc'         { L _ ITproc }          -- for arrow notation extension
185  'rec'          { L _ ITrec }           -- for arrow notation extension
186
187  '{-# INLINE'             { L _ (ITinline_prag _) }
188  '{-# SPECIALISE'         { L _ ITspec_prag }
189  '{-# SPECIALISE_INLINE'  { L _ (ITspec_inline_prag _) }
190  '{-# SOURCE'      { L _ ITsource_prag }
191  '{-# RULES'       { L _ ITrules_prag }
192  '{-# CORE'        { L _ ITcore_prag }              -- hdaume: annotated core
193  '{-# SCC'         { L _ ITscc_prag }
194  '{-# DEPRECATED'  { L _ ITdeprecated_prag }
195  '{-# UNPACK'      { L _ ITunpack_prag }
196  '#-}'             { L _ ITclose_prag }
197
198  '..'           { L _ ITdotdot }                        -- reserved symbols
199  ':'            { L _ ITcolon }
200  '::'           { L _ ITdcolon }
201  '='            { L _ ITequal }
202  '\\'           { L _ ITlam }
203  '|'            { L _ ITvbar }
204  '<-'           { L _ ITlarrow }
205  '->'           { L _ ITrarrow }
206  '@'            { L _ ITat }
207  '~'            { L _ ITtilde }
208  '=>'           { L _ ITdarrow }
209  '-'            { L _ ITminus }
210  '!'            { L _ ITbang }
211  '*'            { L _ ITstar }
212  '-<'           { L _ ITlarrowtail }            -- for arrow notation
213  '>-'           { L _ ITrarrowtail }            -- for arrow notation
214  '-<<'          { L _ ITLarrowtail }            -- for arrow notation
215  '>>-'          { L _ ITRarrowtail }            -- for arrow notation
216  '.'            { L _ ITdot }
217
218  '{'            { L _ ITocurly }                        -- special symbols
219  '}'            { L _ ITccurly }
220  '{|'           { L _ ITocurlybar }
221  '|}'           { L _ ITccurlybar }
222  vocurly        { L _ ITvocurly } -- virtual open curly (from layout)
223  vccurly        { L _ ITvccurly } -- virtual close curly (from layout)
224  '['            { L _ ITobrack }
225  ']'            { L _ ITcbrack }
226  '[:'           { L _ ITopabrack }
227  ':]'           { L _ ITcpabrack }
228  '('            { L _ IToparen }
229  ')'            { L _ ITcparen }
230  '(#'           { L _ IToubxparen }
231  '#)'           { L _ ITcubxparen }
232  '(|'           { L _ IToparenbar }
233  '|)'           { L _ ITcparenbar }
234  ';'            { L _ ITsemi }
235  ','            { L _ ITcomma }
236  '`'            { L _ ITbackquote }
237
238  VARID          { L _ (ITvarid    _) }          -- identifiers
239  CONID          { L _ (ITconid    _) }
240  VARSYM         { L _ (ITvarsym   _) }
241  CONSYM         { L _ (ITconsym   _) }
242  QVARID         { L _ (ITqvarid   _) }
243  QCONID         { L _ (ITqconid   _) }
244  QVARSYM        { L _ (ITqvarsym  _) }
245  QCONSYM        { L _ (ITqconsym  _) }
246
247  IPDUPVARID     { L _ (ITdupipvarid   _) }              -- GHC extension
248  IPSPLITVARID   { L _ (ITsplitipvarid _) }              -- GHC extension
249
250  CHAR           { L _ (ITchar     _) }
251  STRING         { L _ (ITstring   _) }
252  INTEGER        { L _ (ITinteger  _) }
253  RATIONAL       { L _ (ITrational _) }
254                     
255  PRIMCHAR       { L _ (ITprimchar   _) }
256  PRIMSTRING     { L _ (ITprimstring _) }
257  PRIMINTEGER    { L _ (ITprimint    _) }
258  PRIMFLOAT      { L _ (ITprimfloat  _) }
259  PRIMDOUBLE     { L _ (ITprimdouble _) }
260                     
261 -- Template Haskell 
262 '[|'            { L _ ITopenExpQuote  }       
263 '[p|'           { L _ ITopenPatQuote  }      
264 '[t|'           { L _ ITopenTypQuote  }      
265 '[d|'           { L _ ITopenDecQuote  }      
266 '|]'            { L _ ITcloseQuote    }
267 TH_ID_SPLICE    { L _ (ITidEscape _)  }     -- $x
268 '$('            { L _ ITparenEscape   }     -- $( exp )
269 TH_VAR_QUOTE    { L _ ITvarQuote      }     -- 'x
270 TH_TY_QUOTE     { L _ ITtyQuote       }      -- ''T
271
272 %monad { P } { >>= } { return }
273 %lexer { lexer } { L _ ITeof }
274 %name parseModule module
275 %name parseStmt   maybe_stmt
276 %name parseIdentifier  identifier
277 %name parseType ctype
278 %partial parseHeader header
279 %tokentype { Located Token }
280 %%
281
282 -----------------------------------------------------------------------------
283 -- Identifiers; one of the entry points
284 identifier :: { Located RdrName }
285         : qvar                          { $1 }
286         | qcon                          { $1 }
287         | qvarop                        { $1 }
288         | qconop                        { $1 }
289
290 -----------------------------------------------------------------------------
291 -- Module Header
292
293 -- The place for module deprecation is really too restrictive, but if it
294 -- was allowed at its natural place just before 'module', we get an ugly
295 -- s/r conflict with the second alternative. Another solution would be the
296 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
297 -- either, and DEPRECATED is only expected to be used by people who really
298 -- know what they are doing. :-)
299
300 module  :: { Located (HsModule RdrName) }
301         : 'module' modid maybemoddeprec maybeexports 'where' body 
302                 {% fileSrcSpan >>= \ loc ->
303                    return (L loc (HsModule (Just $2) $4 (fst $6) (snd $6) $3)) }
304         | missing_module_keyword top close
305                 {% fileSrcSpan >>= \ loc ->
306                    return (L loc (HsModule Nothing Nothing 
307                                 (fst $2) (snd $2) Nothing)) }
308
309 missing_module_keyword :: { () }
310         : {- empty -}                           {% pushCurrentContext }
311
312 maybemoddeprec :: { Maybe DeprecTxt }
313         : '{-# DEPRECATED' STRING '#-}'         { Just (getSTRING $2) }
314         |  {- empty -}                          { Nothing }
315
316 body    :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
317         :  '{'            top '}'               { $2 }
318         |      vocurly    top close             { $2 }
319
320 top     :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
321         : importdecls                           { (reverse $1,[]) }
322         | importdecls ';' cvtopdecls            { (reverse $1,$3) }
323         | cvtopdecls                            { ([],$1) }
324
325 cvtopdecls :: { [LHsDecl RdrName] }
326         : topdecls                              { cvTopDecls $1 }
327
328 -----------------------------------------------------------------------------
329 -- Module declaration & imports only
330
331 header  :: { Located (HsModule RdrName) }
332         : 'module' modid maybemoddeprec maybeexports 'where' header_body
333                 {% fileSrcSpan >>= \ loc ->
334                    return (L loc (HsModule (Just $2) $4 $6 [] $3)) }
335         | missing_module_keyword importdecls
336                 {% fileSrcSpan >>= \ loc ->
337                    return (L loc (HsModule Nothing Nothing $2 [] Nothing)) }
338
339 header_body :: { [LImportDecl RdrName] }
340         :  '{'            importdecls           { $2 }
341         |      vocurly    importdecls           { $2 }
342
343 -----------------------------------------------------------------------------
344 -- The Export List
345
346 maybeexports :: { Maybe [LIE RdrName] }
347         :  '(' exportlist ')'                   { Just $2 }
348         |  {- empty -}                          { Nothing }
349
350 exportlist :: { [LIE RdrName] }
351         :  exportlist ',' export                { $3 : $1 }
352         |  exportlist ','                       { $1 }
353         |  export                               { [$1]  }
354         |  {- empty -}                          { [] }
355
356    -- No longer allow things like [] and (,,,) to be exported
357    -- They are built in syntax, always available
358 export  :: { LIE RdrName }
359         :  qvar                         { L1 (IEVar (unLoc $1)) }
360         |  oqtycon                      { L1 (IEThingAbs (unLoc $1)) }
361         |  oqtycon '(' '..' ')'         { LL (IEThingAll (unLoc $1)) }
362         |  oqtycon '(' ')'              { LL (IEThingWith (unLoc $1) []) }
363         |  oqtycon '(' qcnames ')'      { LL (IEThingWith (unLoc $1) (reverse $3)) }
364         |  'module' modid               { LL (IEModuleContents (unLoc $2)) }
365
366 qcnames :: { [RdrName] }
367         :  qcnames ',' qcname                   { unLoc $3 : $1 }
368         |  qcname                               { [unLoc $1]  }
369
370 qcname  :: { Located RdrName }  -- Variable or data constructor
371         :  qvar                                 { $1 }
372         |  qcon                                 { $1 }
373
374 -----------------------------------------------------------------------------
375 -- Import Declarations
376
377 -- import decls can be *empty*, or even just a string of semicolons
378 -- whereas topdecls must contain at least one topdecl.
379
380 importdecls :: { [LImportDecl RdrName] }
381         : importdecls ';' importdecl            { $3 : $1 }
382         | importdecls ';'                       { $1 }
383         | importdecl                            { [ $1 ] }
384         | {- empty -}                           { [] }
385
386 importdecl :: { LImportDecl RdrName }
387         : 'import' maybe_src optqualified modid maybeas maybeimpspec 
388                 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
389
390 maybe_src :: { IsBootInterface }
391         : '{-# SOURCE' '#-}'                    { True }
392         | {- empty -}                           { False }
393
394 optqualified :: { Bool }
395         : 'qualified'                           { True  }
396         | {- empty -}                           { False }
397
398 maybeas :: { Located (Maybe Module) }
399         : 'as' modid                            { LL (Just (unLoc $2)) }
400         | {- empty -}                           { noLoc Nothing }
401
402 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
403         : impspec                               { L1 (Just (unLoc $1)) }
404         | {- empty -}                           { noLoc Nothing }
405
406 impspec :: { Located (Bool, [LIE RdrName]) }
407         :  '(' exportlist ')'                   { LL (False, reverse $2) }
408         |  'hiding' '(' exportlist ')'          { LL (True,  reverse $3) }
409
410 -----------------------------------------------------------------------------
411 -- Fixity Declarations
412
413 prec    :: { Int }
414         : {- empty -}           { 9 }
415         | INTEGER               {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
416
417 infix   :: { Located FixityDirection }
418         : 'infix'                               { L1 InfixN  }
419         | 'infixl'                              { L1 InfixL  }
420         | 'infixr'                              { L1 InfixR }
421
422 ops     :: { Located [Located RdrName] }
423         : ops ',' op                            { LL ($3 : unLoc $1) }
424         | op                                    { L1 [$1] }
425
426 -----------------------------------------------------------------------------
427 -- Top-Level Declarations
428
429 topdecls :: { OrdList (LHsDecl RdrName) }       -- Reversed
430         : topdecls ';' topdecl          { $1 `appOL` $3 }
431         | topdecls ';'                  { $1 }
432         | topdecl                       { $1 }
433
434 topdecl :: { OrdList (LHsDecl RdrName) }
435         : tycl_decl                     { unitOL (L1 (TyClD (unLoc $1))) }
436         | 'instance' inst_type where
437                 { let (binds,sigs) = cvBindsAndSigs (unLoc $3)
438                   in unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs))) }
439         | 'default' '(' comma_types0 ')'        { unitOL (LL $ DefD (DefaultDecl $3)) }
440         | 'foreign' fdecl                       { unitOL (LL (unLoc $2)) }
441         | '{-# DEPRECATED' deprecations '#-}'   { $2 }
442         | '{-# RULES' rules '#-}'               { $2 }
443         | '$(' exp ')'                          { unitOL (LL $ SpliceD (SpliceDecl $2)) }
444         | decl                                  { unLoc $1 }
445
446 tycl_decl :: { LTyClDecl RdrName }
447         : 'type' type '=' ctype 
448                 -- Note type on the left of the '='; this allows
449                 -- infix type constructors to be declared
450                 -- 
451                 -- Note ctype, not sigtype, on the right
452                 -- We allow an explicit for-all but we don't insert one
453                 -- in   type Foo a = (b,b)
454                 -- Instead we just say b is out of scope
455                 {% do { (tc,tvs) <- checkSynHdr $2
456                       ; return (LL (TySynonym tc tvs $4)) } }
457
458         | 'data' tycl_hdr constrs deriving
459                 { L (comb4 $1 $2 $3 $4) -- We need the location on tycl_hdr 
460                                         -- in case constrs and deriving are both empty
461                     (mkTyData DataType (unLoc $2) Nothing (reverse (unLoc $3)) (unLoc $4)) }
462
463         | 'data' tycl_hdr opt_kind_sig 
464                  'where' gadt_constrlist
465                  deriving
466                 { L (comb4 $1 $2 $4 $5)
467                     (mkTyData DataType (unLoc $2) $3 (reverse (unLoc $5)) (unLoc $6)) }
468
469         | 'newtype' tycl_hdr '=' newconstr deriving
470                 { L (comb3 $1 $4 $5)
471                     (mkTyData NewType (unLoc $2) Nothing [$4] (unLoc $5)) }
472
473         | 'class' tycl_hdr fds where
474                 { let 
475                         (binds,sigs) = cvBindsAndSigs (unLoc $4)
476                   in
477                   L (comb4 $1 $2 $3 $4) (mkClassDecl (unLoc $2) (unLoc $3) sigs 
478                                           binds) }
479
480 opt_kind_sig :: { Maybe Kind }
481         :                               { Nothing }
482         | '::' kind                     { Just $2 }
483
484 -- tycl_hdr parses the header of a type or class decl,
485 -- which takes the form
486 --      T a b
487 --      Eq a => T a
488 --      (Eq a, Ord b) => T a b
489 -- Rather a lot of inlining here, else we get reduce/reduce errors
490 tycl_hdr :: { Located (LHsContext RdrName, Located RdrName, [LHsTyVarBndr RdrName]) }
491         : context '=>' type             {% checkTyClHdr $1         $3 >>= return.LL }
492         | type                          {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
493
494 -----------------------------------------------------------------------------
495 -- Nested declarations
496
497 decls   :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
498         : decls ';' decl                { LL (unLoc $1 `appOL` unLoc $3) }
499         | decls ';'                     { LL (unLoc $1) }
500         | decl                          { $1 }
501         | {- empty -}                   { noLoc nilOL }
502
503
504 decllist :: { Located (OrdList (LHsDecl RdrName)) }     -- Reversed
505         : '{'            decls '}'      { LL (unLoc $2) }
506         |     vocurly    decls close    { $2 }
507
508 where   :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
509                                 -- No implicit parameters
510         : 'where' decllist              { LL (unLoc $2) }
511         | {- empty -}                   { noLoc nilOL }
512
513 binds   ::  { Located (HsLocalBinds RdrName) }          -- May have implicit parameters
514         : decllist                      { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
515         | '{'            dbinds '}'     { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
516         |     vocurly    dbinds close   { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
517
518 wherebinds :: { Located (HsLocalBinds RdrName) }        -- May have implicit parameters
519         : 'where' binds                 { LL (unLoc $2) }
520         | {- empty -}                   { noLoc emptyLocalBinds }
521
522
523 -----------------------------------------------------------------------------
524 -- Transformation Rules
525
526 rules   :: { OrdList (LHsDecl RdrName) }        -- Reversed
527         :  rules ';' rule                       { $1 `snocOL` $3 }
528         |  rules ';'                            { $1 }
529         |  rule                                 { unitOL $1 }
530         |  {- empty -}                          { nilOL }
531
532 rule    :: { LHsDecl RdrName }
533         : STRING activation rule_forall infixexp '=' exp
534              { LL $ RuleD (HsRule (getSTRING $1) 
535                                   ($2 `orElse` AlwaysActive) 
536                                   $3 $4 $6) }
537
538 activation :: { Maybe Activation } 
539         : {- empty -}                           { Nothing }
540         | explicit_activation                   { Just $1 }
541
542 explicit_activation :: { Activation }  -- In brackets
543         : '[' INTEGER ']'               { ActiveAfter  (fromInteger (getINTEGER $2)) }
544         | '[' '~' INTEGER ']'           { ActiveBefore (fromInteger (getINTEGER $3)) }
545
546 rule_forall :: { [RuleBndr RdrName] }
547         : 'forall' rule_var_list '.'            { $2 }
548         | {- empty -}                           { [] }
549
550 rule_var_list :: { [RuleBndr RdrName] }
551         : rule_var                              { [$1] }
552         | rule_var rule_var_list                { $1 : $2 }
553
554 rule_var :: { RuleBndr RdrName }
555         : varid                                 { RuleBndr $1 }
556         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
557
558 -----------------------------------------------------------------------------
559 -- Deprecations (c.f. rules)
560
561 deprecations :: { OrdList (LHsDecl RdrName) }   -- Reversed
562         : deprecations ';' deprecation          { $1 `appOL` $3 }
563         | deprecations ';'                      { $1 }
564         | deprecation                           { $1 }
565         | {- empty -}                           { nilOL }
566
567 -- SUP: TEMPORARY HACK, not checking for `module Foo'
568 deprecation :: { OrdList (LHsDecl RdrName) }
569         : depreclist STRING
570                 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2)) 
571                        | n <- unLoc $1 ] }
572
573
574 -----------------------------------------------------------------------------
575 -- Foreign import and export declarations
576
577 -- for the time being, the following accepts foreign declarations conforming
578 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
579 --
580 -- * a flag indicates whether pre-standard declarations have been used and
581 --   triggers a deprecation warning further down the road
582 --
583 -- NB: The first two rules could be combined into one by replacing `safety1'
584 --     with `safety'.  However, the combined rule conflicts with the
585 --     DEPRECATED rules.
586 --
587 fdecl :: { LHsDecl RdrName }
588 fdecl : 'import' callconv safety1 fspec
589                 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
590       | 'import' callconv         fspec         
591                 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
592                         return (LL d) } }
593       | 'export' callconv fspec
594                 {% mkExport $2 (unLoc $3) >>= return.LL }
595         -- the following syntax is DEPRECATED
596       | fdecl1DEPRECATED                        { L1 (ForD (unLoc $1)) }
597       | fdecl2DEPRECATED                        { L1 (unLoc $1) }
598
599 fdecl1DEPRECATED :: { LForeignDecl RdrName }
600 fdecl1DEPRECATED 
601   ----------- DEPRECATED label decls ------------
602   : 'label' ext_name varid '::' sigtype
603     { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS 
604                                    (CLabel ($2 `orElse` mkExtName (unLoc $3)))) True }
605
606   ----------- DEPRECATED ccall/stdcall decls ------------
607   --
608   -- NB: This business with the case expression below may seem overly
609   --     complicated, but it is necessary to avoid some conflicts.
610
611     -- DEPRECATED variant #1: lack of a calling convention specification
612     --                        (import) 
613   | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
614     { let
615         target = StaticTarget ($2 `orElse` mkExtName (unLoc $4))
616       in
617       LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS 
618                                    (CFunction target)) True }
619
620     -- DEPRECATED variant #2: external name consists of two separate strings
621     --                        (module name and function name) (import)
622   | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
623     {% case $2 of
624          DNCall      -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
625          CCall cconv -> return $
626            let
627              imp = CFunction (StaticTarget (getSTRING $4))
628            in
629            LL $ ForeignImport $6 $8 (CImport cconv $5 nilFS nilFS imp) True }
630
631     -- DEPRECATED variant #3: `unsafe' after entity
632   | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
633     {% case $2 of
634          DNCall      -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
635          CCall cconv -> return $
636            let
637              imp = CFunction (StaticTarget (getSTRING $3))
638            in
639            LL $ ForeignImport $5 $7 (CImport cconv PlayRisky nilFS nilFS imp) True }
640
641     -- DEPRECATED variant #4: use of the special identifier `dynamic' without
642     --                        an explicit calling convention (import)
643   | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
644     { LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS 
645                                    (CFunction DynamicTarget)) True }
646
647     -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
648   | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
649     {% case $2 of
650          DNCall      -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
651          CCall cconv -> return $
652            LL $ ForeignImport $5 $7 (CImport cconv $4 nilFS nilFS 
653                                         (CFunction DynamicTarget)) True }
654
655     -- DEPRECATED variant #6: lack of a calling convention specification
656     --                        (export) 
657   | 'export' {-no callconv-} ext_name varid '::' sigtype
658     { LL $ ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName (unLoc $3))
659                                    defaultCCallConv)) True }
660
661     -- DEPRECATED variant #7: external name consists of two separate strings
662     --                        (module name and function name) (export)
663   | 'export' callconv STRING STRING varid '::' sigtype
664     {% case $2 of
665          DNCall      -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
666          CCall cconv -> return $
667            LL $ ForeignExport $5 $7 
668                          (CExport (CExportStatic (getSTRING $4) cconv)) True }
669
670     -- DEPRECATED variant #8: use of the special identifier `dynamic' without
671     --                        an explicit calling convention (export)
672   | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
673     { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS 
674                                    CWrapper) True }
675
676     -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
677   | 'export' callconv 'dynamic' varid '::' sigtype
678     {% case $2 of
679          DNCall      -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
680          CCall cconv -> return $
681            LL $ ForeignImport $4 $6 
682                  (CImport cconv (PlaySafe False) nilFS nilFS CWrapper) True }
683
684   ----------- DEPRECATED .NET decls ------------
685   -- NB: removed the .NET call declaration, as it is entirely subsumed
686   --     by the new standard FFI declarations
687
688 fdecl2DEPRECATED :: { LHsDecl RdrName }
689 fdecl2DEPRECATED 
690   : 'import' 'dotnet' 'type' ext_name tycon { LL $ TyClD (ForeignType $5 $4 DNType) }
691     -- left this one unchanged for the moment as type imports are not
692     -- covered currently by the FFI standard -=chak
693
694
695 callconv :: { CallConv }
696           : 'stdcall'                   { CCall  StdCallConv }
697           | 'ccall'                     { CCall  CCallConv   }
698           | 'dotnet'                    { DNCall             }
699
700 safety :: { Safety }
701         : 'unsafe'                      { PlayRisky }
702         | 'safe'                        { PlaySafe False }
703         | 'threadsafe'                  { PlaySafe True  }
704         | {- empty -}                   { PlaySafe False }
705
706 safety1 :: { Safety }
707         : 'unsafe'                      { PlayRisky }
708         | 'safe'                        { PlaySafe  False }
709         | 'threadsafe'                  { PlaySafe  True }
710           -- only needed to avoid conflicts with the DEPRECATED rules
711
712 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
713        : STRING var '::' sigtype      { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
714        |        var '::' sigtype      { LL (noLoc nilFS, $1, $3) }
715          -- if the entity string is missing, it defaults to the empty string;
716          -- the meaning of an empty entity string depends on the calling
717          -- convention
718
719 -- DEPRECATED syntax
720 ext_name :: { Maybe CLabelString }
721         : STRING                { Just (getSTRING $1) }
722         | STRING STRING         { Just (getSTRING $2) } -- Ignore "module name" for now
723         | {- empty -}           { Nothing }
724
725
726 -----------------------------------------------------------------------------
727 -- Type signatures
728
729 opt_sig :: { Maybe (LHsType RdrName) }
730         : {- empty -}                   { Nothing }
731         | '::' sigtype                  { Just $2 }
732
733 opt_asig :: { Maybe (LHsType RdrName) }
734         : {- empty -}                   { Nothing }
735         | '::' atype                    { Just $2 }
736
737 sigtypes1 :: { [LHsType RdrName] }
738         : sigtype                       { [ $1 ] }
739         | sigtype ',' sigtypes1         { $1 : $3 }
740
741 sigtype :: { LHsType RdrName }
742         : ctype                         { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
743         -- Wrap an Implicit forall if there isn't one there already
744
745 sig_vars :: { Located [Located RdrName] }
746          : sig_vars ',' var             { LL ($3 : unLoc $1) }
747          | var                          { L1 [$1] }
748
749 -----------------------------------------------------------------------------
750 -- Types
751
752 strict_mark :: { Located HsBang }
753         : '!'                           { L1 HsStrict }
754         | '{-# UNPACK' '#-}' '!'        { LL HsUnbox }
755
756 -- A ctype is a for-all type
757 ctype   :: { LHsType RdrName }
758         : 'forall' tv_bndrs '.' ctype   { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
759         | context '=>' type             { LL $ mkImplicitHsForAllTy   $1 $3 }
760         -- A type of form (context => type) is an *implicit* HsForAllTy
761         | type                          { $1 }
762
763 -- We parse a context as a btype so that we don't get reduce/reduce
764 -- errors in ctype.  The basic problem is that
765 --      (Eq a, Ord a)
766 -- looks so much like a tuple type.  We can't tell until we find the =>
767 context :: { LHsContext RdrName }
768         : btype                         {% checkContext $1 }
769
770 type :: { LHsType RdrName }
771         : ipvar '::' gentype            { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
772         | gentype                       { $1 }
773
774 gentype :: { LHsType RdrName }
775         : btype                         { $1 }
776         | btype qtyconop gentype        { LL $ HsOpTy $1 $2 $3 }
777         | btype tyvarop  gentype        { LL $ HsOpTy $1 $2 $3 }
778         | btype '->' gentype            { LL $ HsFunTy $1 $3 }
779
780 btype :: { LHsType RdrName }
781         : btype atype                   { LL $ HsAppTy $1 $2 }
782         | atype                         { $1 }
783
784 atype :: { LHsType RdrName }
785         : gtycon                        { L1 (HsTyVar (unLoc $1)) }
786         | tyvar                         { L1 (HsTyVar (unLoc $1)) }
787         | strict_mark atype             { LL (HsBangTy (unLoc $1) $2) }
788         | '(' type ',' comma_types1 ')' { LL $ HsTupleTy Boxed  ($2:$4) }
789         | '(#' comma_types1 '#)'        { LL $ HsTupleTy Unboxed $2     }
790         | '[' type ']'                  { LL $ HsListTy  $2 }
791         | '[:' type ':]'                { LL $ HsPArrTy  $2 }
792         | '(' ctype ')'                 { LL $ HsParTy   $2 }
793         | '(' ctype '::' kind ')'       { LL $ HsKindSig $2 $4 }
794 -- Generics
795         | INTEGER                       { L1 (HsNumTy (getINTEGER $1)) }
796
797 -- An inst_type is what occurs in the head of an instance decl
798 --      e.g.  (Foo a, Gaz b) => Wibble a b
799 -- It's kept as a single type, with a MonoDictTy at the right
800 -- hand corner, for convenience.
801 inst_type :: { LHsType RdrName }
802         : sigtype                       {% checkInstType $1 }
803
804 inst_types1 :: { [LHsType RdrName] }
805         : inst_type                     { [$1] }
806         | inst_type ',' inst_types1     { $1 : $3 }
807
808 comma_types0  :: { [LHsType RdrName] }
809         : comma_types1                  { $1 }
810         | {- empty -}                   { [] }
811
812 comma_types1    :: { [LHsType RdrName] }
813         : type                          { [$1] }
814         | type  ',' comma_types1        { $1 : $3 }
815
816 tv_bndrs :: { [LHsTyVarBndr RdrName] }
817          : tv_bndr tv_bndrs             { $1 : $2 }
818          | {- empty -}                  { [] }
819
820 tv_bndr :: { LHsTyVarBndr RdrName }
821         : tyvar                         { L1 (UserTyVar (unLoc $1)) }
822         | '(' tyvar '::' kind ')'       { LL (KindedTyVar (unLoc $2) $4) }
823
824 fds :: { Located [Located ([RdrName], [RdrName])] }
825         : {- empty -}                   { noLoc [] }
826         | '|' fds1                      { LL (reverse (unLoc $2)) }
827
828 fds1 :: { Located [Located ([RdrName], [RdrName])] }
829         : fds1 ',' fd                   { LL ($3 : unLoc $1) }
830         | fd                            { L1 [$1] }
831
832 fd :: { Located ([RdrName], [RdrName]) }
833         : varids0 '->' varids0          { L (comb3 $1 $2 $3)
834                                            (reverse (unLoc $1), reverse (unLoc $3)) }
835
836 varids0 :: { Located [RdrName] }
837         : {- empty -}                   { noLoc [] }
838         | varids0 tyvar                 { LL (unLoc $2 : unLoc $1) }
839
840 -----------------------------------------------------------------------------
841 -- Kinds
842
843 kind    :: { Kind }
844         : akind                 { $1 }
845         | akind '->' kind       { mkArrowKind $1 $3 }
846
847 akind   :: { Kind }
848         : '*'                   { liftedTypeKind }
849         | '(' kind ')'          { $2 }
850
851
852 -----------------------------------------------------------------------------
853 -- Datatype declarations
854
855 newconstr :: { LConDecl RdrName }
856         : conid atype   { LL $ ConDecl $1 Explicit [] (noLoc []) (PrefixCon [$2]) ResTyH98 }
857         | conid '{' var '::' ctype '}'
858                         { LL $ ConDecl $1 Explicit [] (noLoc []) (RecCon [($3, $5)]) ResTyH98 }
859
860 gadt_constrlist :: { Located [LConDecl RdrName] }
861         : '{'            gadt_constrs '}'       { LL (unLoc $2) }
862         |     vocurly    gadt_constrs close     { $2 }
863
864 gadt_constrs :: { Located [LConDecl RdrName] }
865         : gadt_constrs ';' gadt_constr  { LL ($3 : unLoc $1) }
866         | gadt_constrs ';'              { $1 }
867         | gadt_constr                   { L1 [$1] } 
868
869 -- We allow the following forms:
870 --      C :: Eq a => a -> T a
871 --      C :: forall a. Eq a => !a -> T a
872 --      D { x,y :: a } :: T a
873 --      forall a. Eq a => D { x,y :: a } :: T a
874
875 gadt_constr :: { LConDecl RdrName }
876         : con '::' sigtype
877               { LL (mkGadtDecl $1 $3) } 
878         -- Syntax: Maybe merge the record stuff with the single-case above?
879         --         (to kill the mostly harmless reduce/reduce error)
880         -- XXX revisit autrijus
881         | constr_stuff_record '::' sigtype
882                 { let (con,details) = unLoc $1 in 
883                   LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
884 {-
885         | forall context '=>' constr_stuff_record '::' sigtype
886                 { let (con,details) = unLoc $4 in 
887                   LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
888         | forall constr_stuff_record '::' sigtype
889                 { let (con,details) = unLoc $2 in 
890                   LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
891 -}
892
893
894 constrs :: { Located [LConDecl RdrName] }
895         : {- empty; a GHC extension -}  { noLoc [] }
896         | '=' constrs1                  { LL (unLoc $2) }
897
898 constrs1 :: { Located [LConDecl RdrName] }
899         : constrs1 '|' constr           { LL ($3 : unLoc $1) }
900         | constr                        { L1 [$1] }
901
902 constr :: { LConDecl RdrName }
903         : forall context '=>' constr_stuff      
904                 { let (con,details) = unLoc $4 in 
905                   LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
906         | forall constr_stuff
907                 { let (con,details) = unLoc $2 in 
908                   LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
909
910 forall :: { Located [LHsTyVarBndr RdrName] }
911         : 'forall' tv_bndrs '.'         { LL $2 }
912         | {- empty -}                   { noLoc [] }
913
914 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
915 -- We parse the constructor declaration 
916 --      C t1 t2
917 -- as a btype (treating C as a type constructor) and then convert C to be
918 -- a data constructor.  Reason: it might continue like this:
919 --      C t1 t2 %: D Int
920 -- in which case C really would be a type constructor.  We can't resolve this
921 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
922         : btype                         {% mkPrefixCon $1 [] >>= return.LL }
923         | oqtycon '{' '}'               {% mkRecCon $1 [] >>= return.LL }
924         | oqtycon '{' fielddecls '}'    {% mkRecCon $1 $3 >>= return.LL }
925         | btype conop btype             { LL ($2, InfixCon $1 $3) }
926
927 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
928         : oqtycon '{' '}'               {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
929         | oqtycon '{' fielddecls '}'    {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
930
931 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
932         : fielddecl ',' fielddecls      { unLoc $1 : $3 }
933         | fielddecl                     { [unLoc $1] }
934
935 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
936         : sig_vars '::' ctype           { LL (reverse (unLoc $1), $3) }
937
938 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
939 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
940 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
941 -- We don't allow a context, but that's sorted out by the type checker.
942 deriving :: { Located (Maybe [LHsType RdrName]) }
943         : {- empty -}                           { noLoc Nothing }
944         | 'deriving' qtycon     {% do { let { L loc tv = $2 }
945                                       ; p <- checkInstType (L loc (HsTyVar tv))
946                                       ; return (LL (Just [p])) } }
947         | 'deriving' '(' ')'                    { LL (Just []) }
948         | 'deriving' '(' inst_types1 ')'        { LL (Just $3) }
949              -- Glasgow extension: allow partial 
950              -- applications in derivings
951
952 -----------------------------------------------------------------------------
953 -- Value definitions
954
955 {- There's an awkward overlap with a type signature.  Consider
956         f :: Int -> Int = ...rhs...
957    Then we can't tell whether it's a type signature or a value
958    definition with a result signature until we see the '='.
959    So we have to inline enough to postpone reductions until we know.
960 -}
961
962 {-
963   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
964   instead of qvar, we get another shift/reduce-conflict. Consider the
965   following programs:
966   
967      { (^^) :: Int->Int ; }          Type signature; only var allowed
968
969      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
970                                      qvar allowed (because of instance decls)
971   
972   We can't tell whether to reduce var to qvar until after we've read the signatures.
973 -}
974
975 decl    :: { Located (OrdList (LHsDecl RdrName)) }
976         : sigdecl                       { $1 }
977         | infixexp opt_sig rhs          {% do { r <- checkValDef $1 $2 $3;
978                                                 return (LL $ unitOL (LL $ ValD r)) } }
979
980 rhs     :: { Located (GRHSs RdrName) }
981         : '=' exp wherebinds    { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
982         | gdrhs wherebinds      { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
983
984 gdrhs :: { Located [LGRHS RdrName] }
985         : gdrhs gdrh            { LL ($2 : unLoc $1) }
986         | gdrh                  { L1 [$1] }
987
988 gdrh :: { LGRHS RdrName }
989         : '|' quals '=' exp     { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
990
991 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
992         : infixexp '::' sigtype
993                                 {% do s <- checkValSig $1 $3; 
994                                       return (LL $ unitOL (LL $ SigD s)) }
995                 -- See the above notes for why we need infixexp here
996         | var ',' sig_vars '::' sigtype 
997                                 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
998         | infix prec ops        { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
999                                              | n <- unLoc $3 ] }
1000         | '{-# INLINE'   activation qvar '#-}'        
1001                                 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1002         | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1003                                 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1004                                             | t <- $4] }
1005         | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1006                                 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1007                                             | t <- $5] }
1008         | '{-# SPECIALISE' 'instance' inst_type '#-}'
1009                                 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1010
1011 -----------------------------------------------------------------------------
1012 -- Expressions
1013
1014 exp   :: { LHsExpr RdrName }
1015         : infixexp '::' sigtype         { LL $ ExprWithTySig $1 $3 }
1016         | infixexp '-<' exp             { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1017         | infixexp '>-' exp             { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1018         | infixexp '-<<' exp            { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1019         | infixexp '>>-' exp            { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1020         | infixexp                      { $1 }
1021
1022 infixexp :: { LHsExpr RdrName }
1023         : exp10                         { $1 }
1024         | infixexp qop exp10            { LL (OpApp $1 $2 (panic "fixity") $3) }
1025
1026 exp10 :: { LHsExpr RdrName }
1027         : '\\' aexp aexps opt_asig '->' exp     
1028                         {% checkPatterns ($2 : reverse $3) >>= \ ps -> 
1029                            return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1030                                             (GRHSs (unguardedRHS $6) emptyLocalBinds
1031                                                         )])) }
1032         | 'let' binds 'in' exp                  { LL $ HsLet (unLoc $2) $4 }
1033         | 'if' exp 'then' exp 'else' exp        { LL $ HsIf $2 $4 $6 }
1034         | 'case' exp 'of' altslist              { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1035         | '-' fexp                              { LL $ mkHsNegApp $2 }
1036
1037         | 'do' stmtlist                 {% let loc = comb2 $1 $2 in
1038                                            checkDo loc (unLoc $2)  >>= \ (stmts,body) ->
1039                                            return (L loc (mkHsDo DoExpr stmts body)) }
1040         | 'mdo' stmtlist                {% let loc = comb2 $1 $2 in
1041                                            checkDo loc (unLoc $2)  >>= \ (stmts,body) ->
1042                                            return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1043         | scc_annot exp                         { LL $ if opt_SccProfilingOn
1044                                                         then HsSCC (unLoc $1) $2
1045                                                         else HsPar $2 }
1046
1047         | 'proc' aexp '->' exp  
1048                         {% checkPattern $2 >>= \ p -> 
1049                            return (LL $ HsProc p (LL $ HsCmdTop $4 [] 
1050                                                    placeHolderType undefined)) }
1051                                                 -- TODO: is LL right here?
1052
1053         | '{-# CORE' STRING '#-}' exp           { LL $ HsCoreAnn (getSTRING $2) $4 }
1054                                                     -- hdaume: core annotation
1055         | fexp                                  { $1 }
1056
1057 scc_annot :: { Located FastString }
1058         : '_scc_' STRING                        { LL $ getSTRING $2 }
1059         | '{-# SCC' STRING '#-}'                { LL $ getSTRING $2 }
1060
1061 fexp    :: { LHsExpr RdrName }
1062         : fexp aexp                             { LL $ HsApp $1 $2 }
1063         | aexp                                  { $1 }
1064
1065 aexps   :: { [LHsExpr RdrName] }
1066         : aexps aexp                            { $2 : $1 }
1067         | {- empty -}                           { [] }
1068
1069 aexp    :: { LHsExpr RdrName }
1070         : qvar '@' aexp                 { LL $ EAsPat $1 $3 }
1071         | '~' aexp                      { LL $ ELazyPat $2 }
1072         | aexp1                         { $1 }
1073
1074 aexp1   :: { LHsExpr RdrName }
1075         : aexp1 '{' fbinds '}'  {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) 
1076                                                         (reverse $3);
1077                                         return (LL r) }}
1078         | aexp2                 { $1 }
1079
1080 -- Here was the syntax for type applications that I was planning
1081 -- but there are difficulties (e.g. what order for type args)
1082 -- so it's not enabled yet.
1083 -- But this case *is* used for the left hand side of a generic definition,
1084 -- which is parsed as an expression before being munged into a pattern
1085         | qcname '{|' gentype '|}'      { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1086                                                      (sL (getLoc $3) (HsType $3)) }
1087
1088 aexp2   :: { LHsExpr RdrName }
1089         : ipvar                         { L1 (HsIPVar $! unLoc $1) }
1090         | qcname                        { L1 (HsVar   $! unLoc $1) }
1091         | literal                       { L1 (HsLit   $! unLoc $1) }
1092         | INTEGER                       { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1093         | RATIONAL                      { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1094         | '(' exp ')'                   { LL (HsPar $2) }
1095         | '(' exp ',' texps ')'         { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1096         | '(#' texps '#)'               { LL $ ExplicitTuple (reverse $2)      Unboxed }
1097         | '[' list ']'                  { LL (unLoc $2) }
1098         | '[:' parr ':]'                { LL (unLoc $2) }
1099         | '(' infixexp qop ')'          { LL $ SectionL $2 $3 }
1100         | '(' qopm infixexp ')'         { LL $ SectionR $2 $3 }
1101         | '_'                           { L1 EWildPat }
1102         
1103         -- MetaHaskell Extension
1104         | TH_ID_SPLICE          { L1 $ HsSpliceE (mkHsSplice 
1105                                         (L1 $ HsVar (mkUnqual varName 
1106                                                         (getTH_ID_SPLICE $1)))) } -- $x
1107         | '$(' exp ')'          { LL $ HsSpliceE (mkHsSplice $2) }               -- $( exp )
1108
1109         | TH_VAR_QUOTE qvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1110         | TH_VAR_QUOTE qcon     { LL $ HsBracket (VarBr (unLoc $2)) }
1111         | TH_TY_QUOTE tyvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1112         | TH_TY_QUOTE gtycon    { LL $ HsBracket (VarBr (unLoc $2)) }
1113         | '[|' exp '|]'         { LL $ HsBracket (ExpBr $2) }                       
1114         | '[t|' ctype '|]'      { LL $ HsBracket (TypBr $2) }                       
1115         | '[p|' infixexp '|]'   {% checkPattern $2 >>= \p ->
1116                                            return (LL $ HsBracket (PatBr p)) }
1117         | '[d|' cvtopbody '|]'  { LL $ HsBracket (DecBr (mkGroup $2)) }
1118
1119         -- arrow notation extension
1120         | '(|' aexp2 cmdargs '|)'       { LL $ HsArrForm $2 Nothing (reverse $3) }
1121
1122 cmdargs :: { [LHsCmdTop RdrName] }
1123         : cmdargs acmd                  { $2 : $1 }
1124         | {- empty -}                   { [] }
1125
1126 acmd    :: { LHsCmdTop RdrName }
1127         : aexp2                 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1128
1129 cvtopbody :: { [LHsDecl RdrName] }
1130         :  '{'            cvtopdecls0 '}'               { $2 }
1131         |      vocurly    cvtopdecls0 close             { $2 }
1132
1133 cvtopdecls0 :: { [LHsDecl RdrName] }
1134         : {- empty -}           { [] }
1135         | cvtopdecls            { $1 }
1136
1137 texps :: { [LHsExpr RdrName] }
1138         : texps ',' exp                 { $3 : $1 }
1139         | exp                           { [$1] }
1140
1141
1142 -----------------------------------------------------------------------------
1143 -- List expressions
1144
1145 -- The rules below are little bit contorted to keep lexps left-recursive while
1146 -- avoiding another shift/reduce-conflict.
1147
1148 list :: { LHsExpr RdrName }
1149         : exp                   { L1 $ ExplicitList placeHolderType [$1] }
1150         | lexps                 { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1151         | exp '..'              { LL $ ArithSeq noPostTcExpr (From $1) }
1152         | exp ',' exp '..'      { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1153         | exp '..' exp          { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1154         | exp ',' exp '..' exp  { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1155         | exp pquals            { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1156
1157 lexps :: { Located [LHsExpr RdrName] }
1158         : lexps ',' exp                 { LL ($3 : unLoc $1) }
1159         | exp ',' exp                   { LL [$3,$1] }
1160
1161 -----------------------------------------------------------------------------
1162 -- List Comprehensions
1163
1164 pquals :: { Located [LStmt RdrName] }   -- Either a singleton ParStmt, 
1165                                         -- or a reversed list of Stmts
1166         : pquals1                       { case unLoc $1 of
1167                                             [qs] -> L1 qs
1168                                             qss  -> L1 [L1 (ParStmt stmtss)]
1169                                                  where
1170                                                     stmtss = [ (reverse qs, undefined) 
1171                                                              | qs <- qss ]
1172                                         }
1173                         
1174 pquals1 :: { Located [[LStmt RdrName]] }
1175         : pquals1 '|' quals             { LL (unLoc $3 : unLoc $1) }
1176         | '|' quals                     { L (getLoc $2) [unLoc $2] }
1177
1178 quals :: { Located [LStmt RdrName] }
1179         : quals ',' qual                { LL ($3 : unLoc $1) }
1180         | qual                          { L1 [$1] }
1181
1182 -----------------------------------------------------------------------------
1183 -- Parallel array expressions
1184
1185 -- The rules below are little bit contorted; see the list case for details.
1186 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1187 -- Moreover, we allow explicit arrays with no element (represented by the nil
1188 -- constructor in the list case).
1189
1190 parr :: { LHsExpr RdrName }
1191         :                               { noLoc (ExplicitPArr placeHolderType []) }
1192         | exp                           { L1 $ ExplicitPArr placeHolderType [$1] }
1193         | lexps                         { L1 $ ExplicitPArr placeHolderType 
1194                                                        (reverse (unLoc $1)) }
1195         | exp '..' exp                  { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1196         | exp ',' exp '..' exp          { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1197         | exp pquals                    { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1198
1199 -- We are reusing `lexps' and `pquals' from the list case.
1200
1201 -----------------------------------------------------------------------------
1202 -- Case alternatives
1203
1204 altslist :: { Located [LMatch RdrName] }
1205         : '{'            alts '}'       { LL (reverse (unLoc $2)) }
1206         |     vocurly    alts  close    { L (getLoc $2) (reverse (unLoc $2)) }
1207
1208 alts    :: { Located [LMatch RdrName] }
1209         : alts1                         { L1 (unLoc $1) }
1210         | ';' alts                      { LL (unLoc $2) }
1211
1212 alts1   :: { Located [LMatch RdrName] }
1213         : alts1 ';' alt                 { LL ($3 : unLoc $1) }
1214         | alts1 ';'                     { LL (unLoc $1) }
1215         | alt                           { L1 [$1] }
1216
1217 alt     :: { LMatch RdrName }
1218         : infixexp opt_sig alt_rhs      {%  checkPattern $1 >>= \p ->
1219                                             return (LL (Match [p] $2 (unLoc $3))) }
1220
1221 alt_rhs :: { Located (GRHSs RdrName) }
1222         : ralt wherebinds               { LL (GRHSs (unLoc $1) (unLoc $2)) }
1223
1224 ralt :: { Located [LGRHS RdrName] }
1225         : '->' exp                      { LL (unguardedRHS $2) }
1226         | gdpats                        { L1 (reverse (unLoc $1)) }
1227
1228 gdpats :: { Located [LGRHS RdrName] }
1229         : gdpats gdpat                  { LL ($2 : unLoc $1) }
1230         | gdpat                         { L1 [$1] }
1231
1232 gdpat   :: { LGRHS RdrName }
1233         : '|' quals '->' exp            { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1234
1235 -----------------------------------------------------------------------------
1236 -- Statement sequences
1237
1238 stmtlist :: { Located [LStmt RdrName] }
1239         : '{'           stmts '}'       { LL (unLoc $2) }
1240         |     vocurly   stmts close     { $2 }
1241
1242 --      do { ;; s ; s ; ; s ;; }
1243 -- The last Stmt should be an expression, but that's hard to enforce
1244 -- here, because we need too much lookahead if we see do { e ; }
1245 -- So we use ExprStmts throughout, and switch the last one over
1246 -- in ParseUtils.checkDo instead
1247 stmts :: { Located [LStmt RdrName] }
1248         : stmt stmts_help               { LL ($1 : unLoc $2) }
1249         | ';' stmts                     { LL (unLoc $2) }
1250         | {- empty -}                   { noLoc [] }
1251
1252 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1253         : ';' stmts                     { LL (unLoc $2) }
1254         | {- empty -}                   { noLoc [] }
1255
1256 -- For typing stmts at the GHCi prompt, where 
1257 -- the input may consist of just comments.
1258 maybe_stmt :: { Maybe (LStmt RdrName) }
1259         : stmt                          { Just $1 }
1260         | {- nothing -}                 { Nothing }
1261
1262 stmt  :: { LStmt RdrName }
1263         : qual                          { $1 }
1264         | infixexp '->' exp             {% checkPattern $3 >>= \p ->
1265                                            return (LL $ mkBindStmt p $1) }
1266         | 'rec' stmtlist                { LL $ mkRecStmt (unLoc $2) }
1267
1268 qual  :: { LStmt RdrName }
1269         : infixexp '<-' exp             {% checkPattern $1 >>= \p ->
1270                                            return (LL $ mkBindStmt p $3) }
1271         | exp                           { L1 $ mkExprStmt $1 }
1272         | 'let' binds                   { LL $ LetStmt (unLoc $2) }
1273
1274 -----------------------------------------------------------------------------
1275 -- Record Field Update/Construction
1276
1277 fbinds  :: { HsRecordBinds RdrName }
1278         : fbinds1                       { $1 }
1279         | {- empty -}                   { [] }
1280
1281 fbinds1 :: { HsRecordBinds RdrName }
1282         : fbinds1 ',' fbind             { $3 : $1 }
1283         | fbind                         { [$1] }
1284   
1285 fbind   :: { (Located RdrName, LHsExpr RdrName) }
1286         : qvar '=' exp                  { ($1,$3) }
1287
1288 -----------------------------------------------------------------------------
1289 -- Implicit Parameter Bindings
1290
1291 dbinds  :: { Located [LIPBind RdrName] }
1292         : dbinds ';' dbind              { LL ($3 : unLoc $1) }
1293         | dbinds ';'                    { LL (unLoc $1) }
1294         | dbind                         { L1 [$1] }
1295 --      | {- empty -}                   { [] }
1296
1297 dbind   :: { LIPBind RdrName }
1298 dbind   : ipvar '=' exp                 { LL (IPBind (unLoc $1) $3) }
1299
1300 ipvar   :: { Located (IPName RdrName) }
1301         : IPDUPVARID            { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1302         | IPSPLITVARID          { L1 (Linear  (mkUnqual varName (getIPSPLITVARID $1))) }
1303
1304 -----------------------------------------------------------------------------
1305 -- Deprecations
1306
1307 depreclist :: { Located [RdrName] }
1308 depreclist : deprec_var                 { L1 [unLoc $1] }
1309            | deprec_var ',' depreclist  { LL (unLoc $1 : unLoc $3) }
1310
1311 deprec_var :: { Located RdrName }
1312 deprec_var : var                        { $1 }
1313            | con                        { $1 }
1314
1315 -----------------------------------------
1316 -- Data constructors
1317 qcon    :: { Located RdrName }
1318         : qconid                { $1 }
1319         | '(' qconsym ')'       { LL (unLoc $2) }
1320         | sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1321 -- The case of '[:' ':]' is part of the production `parr'
1322
1323 con     :: { Located RdrName }
1324         : conid                 { $1 }
1325         | '(' consym ')'        { LL (unLoc $2) }
1326         | sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1327
1328 sysdcon :: { Located DataCon }  -- Wired in data constructors
1329         : '(' ')'               { LL unitDataCon }
1330         | '(' commas ')'        { LL $ tupleCon Boxed $2 }
1331         | '[' ']'               { LL nilDataCon }
1332
1333 conop :: { Located RdrName }
1334         : consym                { $1 }  
1335         | '`' conid '`'         { LL (unLoc $2) }
1336
1337 qconop :: { Located RdrName }
1338         : qconsym               { $1 }
1339         | '`' qconid '`'        { LL (unLoc $2) }
1340
1341 -----------------------------------------------------------------------------
1342 -- Type constructors
1343
1344 gtycon  :: { Located RdrName }  -- A "general" qualified tycon
1345         : oqtycon                       { $1 }
1346         | '(' ')'                       { LL $ getRdrName unitTyCon }
1347         | '(' commas ')'                { LL $ getRdrName (tupleTyCon Boxed $2) }
1348         | '(' '->' ')'                  { LL $ getRdrName funTyCon }
1349         | '[' ']'                       { LL $ listTyCon_RDR }
1350         | '[:' ':]'                     { LL $ parrTyCon_RDR }
1351
1352 oqtycon :: { Located RdrName }  -- An "ordinary" qualified tycon
1353         : qtycon                        { $1 }
1354         | '(' qtyconsym ')'             { LL (unLoc $2) }
1355
1356 qtyconop :: { Located RdrName } -- Qualified or unqualified
1357         : qtyconsym                     { $1 }
1358         | '`' qtycon '`'                { LL (unLoc $2) }
1359
1360 qtycon :: { Located RdrName }   -- Qualified or unqualified
1361         : QCONID                        { L1 $! mkQual tcClsName (getQCONID $1) }
1362         | tycon                         { $1 }
1363
1364 tycon   :: { Located RdrName }  -- Unqualified
1365         : CONID                         { L1 $! mkUnqual tcClsName (getCONID $1) }
1366
1367 qtyconsym :: { Located RdrName }
1368         : QCONSYM                       { L1 $! mkQual tcClsName (getQCONSYM $1) }
1369         | tyconsym                      { $1 }
1370
1371 tyconsym :: { Located RdrName }
1372         : CONSYM                        { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1373
1374 -----------------------------------------------------------------------------
1375 -- Operators
1376
1377 op      :: { Located RdrName }   -- used in infix decls
1378         : varop                 { $1 }
1379         | conop                 { $1 }
1380
1381 varop   :: { Located RdrName }
1382         : varsym                { $1 }
1383         | '`' varid '`'         { LL (unLoc $2) }
1384
1385 qop     :: { LHsExpr RdrName }   -- used in sections
1386         : qvarop                { L1 $ HsVar (unLoc $1) }
1387         | qconop                { L1 $ HsVar (unLoc $1) }
1388
1389 qopm    :: { LHsExpr RdrName }   -- used in sections
1390         : qvaropm               { L1 $ HsVar (unLoc $1) }
1391         | qconop                { L1 $ HsVar (unLoc $1) }
1392
1393 qvarop :: { Located RdrName }
1394         : qvarsym               { $1 }
1395         | '`' qvarid '`'        { LL (unLoc $2) }
1396
1397 qvaropm :: { Located RdrName }
1398         : qvarsym_no_minus      { $1 }
1399         | '`' qvarid '`'        { LL (unLoc $2) }
1400
1401 -----------------------------------------------------------------------------
1402 -- Type variables
1403
1404 tyvar   :: { Located RdrName }
1405 tyvar   : tyvarid               { $1 }
1406         | '(' tyvarsym ')'      { LL (unLoc $2) }
1407
1408 tyvarop :: { Located RdrName }
1409 tyvarop : '`' tyvarid '`'       { LL (unLoc $2) }
1410         | tyvarsym              { $1 }
1411
1412 tyvarid :: { Located RdrName }
1413         : VARID                 { L1 $! mkUnqual tvName (getVARID $1) }
1414         | special_id            { L1 $! mkUnqual tvName (unLoc $1) }
1415         | 'unsafe'              { L1 $! mkUnqual tvName FSLIT("unsafe") }
1416         | 'safe'                { L1 $! mkUnqual tvName FSLIT("safe") }
1417         | 'threadsafe'          { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1418
1419 tyvarsym :: { Located RdrName }
1420 -- Does not include "!", because that is used for strictness marks
1421 --               or ".", because that separates the quantified type vars from the rest
1422 --               or "*", because that's used for kinds
1423 tyvarsym : VARSYM               { L1 $! mkUnqual tvName (getVARSYM $1) }
1424
1425 -----------------------------------------------------------------------------
1426 -- Variables 
1427
1428 var     :: { Located RdrName }
1429         : varid                 { $1 }
1430         | '(' varsym ')'        { LL (unLoc $2) }
1431
1432 qvar    :: { Located RdrName }
1433         : qvarid                { $1 }
1434         | '(' varsym ')'        { LL (unLoc $2) }
1435         | '(' qvarsym1 ')'      { LL (unLoc $2) }
1436 -- We've inlined qvarsym here so that the decision about
1437 -- whether it's a qvar or a var can be postponed until
1438 -- *after* we see the close paren.
1439
1440 qvarid :: { Located RdrName }
1441         : varid                 { $1 }
1442         | QVARID                { L1 $ mkQual varName (getQVARID $1) }
1443
1444 varid :: { Located RdrName }
1445         : varid_no_unsafe       { $1 }
1446         | 'unsafe'              { L1 $! mkUnqual varName FSLIT("unsafe") }
1447         | 'safe'                { L1 $! mkUnqual varName FSLIT("safe") }
1448         | 'threadsafe'          { L1 $! mkUnqual varName FSLIT("threadsafe") }
1449
1450 varid_no_unsafe :: { Located RdrName }
1451         : VARID                 { L1 $! mkUnqual varName (getVARID $1) }
1452         | special_id            { L1 $! mkUnqual varName (unLoc $1) }
1453         | 'forall'              { L1 $! mkUnqual varName FSLIT("forall") }
1454
1455 qvarsym :: { Located RdrName }
1456         : varsym                { $1 }
1457         | qvarsym1              { $1 }
1458
1459 qvarsym_no_minus :: { Located RdrName }
1460         : varsym_no_minus       { $1 }
1461         | qvarsym1              { $1 }
1462
1463 qvarsym1 :: { Located RdrName }
1464 qvarsym1 : QVARSYM              { L1 $ mkQual varName (getQVARSYM $1) }
1465
1466 varsym :: { Located RdrName }
1467         : varsym_no_minus       { $1 }
1468         | '-'                   { L1 $ mkUnqual varName FSLIT("-") }
1469
1470 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1471         : VARSYM                { L1 $ mkUnqual varName (getVARSYM $1) }
1472         | special_sym           { L1 $ mkUnqual varName (unLoc $1) }
1473
1474
1475 -- These special_ids are treated as keywords in various places, 
1476 -- but as ordinary ids elsewhere.   'special_id' collects all these
1477 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1478 special_id :: { Located UserFS }
1479 special_id
1480         : 'as'                  { L1 FSLIT("as") }
1481         | 'qualified'           { L1 FSLIT("qualified") }
1482         | 'hiding'              { L1 FSLIT("hiding") }
1483         | 'export'              { L1 FSLIT("export") }
1484         | 'label'               { L1 FSLIT("label")  }
1485         | 'dynamic'             { L1 FSLIT("dynamic") }
1486         | 'stdcall'             { L1 FSLIT("stdcall") }
1487         | 'ccall'               { L1 FSLIT("ccall") }
1488
1489 special_sym :: { Located UserFS }
1490 special_sym : '!'       { L1 FSLIT("!") }
1491             | '.'       { L1 FSLIT(".") }
1492             | '*'       { L1 FSLIT("*") }
1493
1494 -----------------------------------------------------------------------------
1495 -- Data constructors
1496
1497 qconid :: { Located RdrName }   -- Qualified or unqualified
1498         : conid                 { $1 }
1499         | QCONID                { L1 $ mkQual dataName (getQCONID $1) }
1500
1501 conid   :: { Located RdrName }
1502         : CONID                 { L1 $ mkUnqual dataName (getCONID $1) }
1503
1504 qconsym :: { Located RdrName }  -- Qualified or unqualified
1505         : consym                { $1 }
1506         | QCONSYM               { L1 $ mkQual dataName (getQCONSYM $1) }
1507
1508 consym :: { Located RdrName }
1509         : CONSYM                { L1 $ mkUnqual dataName (getCONSYM $1) }
1510
1511         -- ':' means only list cons
1512         | ':'                   { L1 $ consDataCon_RDR }
1513
1514
1515 -----------------------------------------------------------------------------
1516 -- Literals
1517
1518 literal :: { Located HsLit }
1519         : CHAR                  { L1 $ HsChar       $ getCHAR $1 }
1520         | STRING                { L1 $ HsString     $ getSTRING $1 }
1521         | PRIMINTEGER           { L1 $ HsIntPrim    $ getPRIMINTEGER $1 }
1522         | PRIMCHAR              { L1 $ HsCharPrim   $ getPRIMCHAR $1 }
1523         | PRIMSTRING            { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1524         | PRIMFLOAT             { L1 $ HsFloatPrim  $ getPRIMFLOAT $1 }
1525         | PRIMDOUBLE            { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1526
1527 -----------------------------------------------------------------------------
1528 -- Layout
1529
1530 close :: { () }
1531         : vccurly               { () } -- context popped in lexer.
1532         | error                 {% popContext }
1533
1534 -----------------------------------------------------------------------------
1535 -- Miscellaneous (mostly renamings)
1536
1537 modid   :: { Located Module }
1538         : CONID                 { L1 $ mkModuleFS (getCONID $1) }
1539         | QCONID                { L1 $ let (mod,c) = getQCONID $1 in
1540                                   mkModuleFS
1541                                    (mkFastString
1542                                      (unpackFS mod ++ '.':unpackFS c))
1543                                 }
1544
1545 commas :: { Int }
1546         : commas ','                    { $1 + 1 }
1547         | ','                           { 2 }
1548
1549 -----------------------------------------------------------------------------
1550
1551 {
1552 happyError :: P a
1553 happyError = srcParseFail
1554
1555 getVARID        (L _ (ITvarid    x)) = x
1556 getCONID        (L _ (ITconid    x)) = x
1557 getVARSYM       (L _ (ITvarsym   x)) = x
1558 getCONSYM       (L _ (ITconsym   x)) = x
1559 getQVARID       (L _ (ITqvarid   x)) = x
1560 getQCONID       (L _ (ITqconid   x)) = x
1561 getQVARSYM      (L _ (ITqvarsym  x)) = x
1562 getQCONSYM      (L _ (ITqconsym  x)) = x
1563 getIPDUPVARID   (L _ (ITdupipvarid   x)) = x
1564 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1565 getCHAR         (L _ (ITchar     x)) = x
1566 getSTRING       (L _ (ITstring   x)) = x
1567 getINTEGER      (L _ (ITinteger  x)) = x
1568 getRATIONAL     (L _ (ITrational x)) = x
1569 getPRIMCHAR     (L _ (ITprimchar   x)) = x
1570 getPRIMSTRING   (L _ (ITprimstring x)) = x
1571 getPRIMINTEGER  (L _ (ITprimint    x)) = x
1572 getPRIMFLOAT    (L _ (ITprimfloat  x)) = x
1573 getPRIMDOUBLE   (L _ (ITprimdouble x)) = x
1574 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1575 getINLINE       (L _ (ITinline_prag b)) = b
1576 getSPEC_INLINE  (L _ (ITspec_inline_prag b)) = b
1577
1578 -- Utilities for combining source spans
1579 comb2 :: Located a -> Located b -> SrcSpan
1580 comb2 = combineLocs
1581
1582 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1583 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1584
1585 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1586 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1587                 combineSrcSpans (getLoc c) (getLoc d)
1588
1589 -- strict constructor version:
1590 {-# INLINE sL #-}
1591 sL :: SrcSpan -> a -> Located a
1592 sL span a = span `seq` L span a
1593
1594 -- Make a source location for the file.  We're a bit lazy here and just
1595 -- make a point SrcSpan at line 1, column 0.  Strictly speaking we should
1596 -- try to find the span of the whole file (ToDo).
1597 fileSrcSpan :: P SrcSpan
1598 fileSrcSpan = do 
1599   l <- getSrcLoc; 
1600   let loc = mkSrcLoc (srcLocFile l) 1 0;
1601   return (mkSrcSpan loc loc)
1602 }