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