[project @ 2005-03-09 16:58:15 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 CmdLineOpts      ( opt_SccProfilingOn )
35 import Type             ( Kind, mkArrowKind, liftedTypeKind )
36 import BasicTypes       ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
37                           Activation(..) )
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: 34 shift/reduce (1.15)
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  '{-# SPECIALISE'  { L _ ITspecialise_prag }
188  '{-# SOURCE'      { L _ ITsource_prag }
189  '{-# INLINE'      { L _ ITinline_prag }
190  '{-# NOINLINE'    { L _ ITnoinline_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 -- Module Header
284
285 -- The place for module deprecation is really too restrictive, but if it
286 -- was allowed at its natural place just before 'module', we get an ugly
287 -- s/r conflict with the second alternative. Another solution would be the
288 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
289 -- either, and DEPRECATED is only expected to be used by people who really
290 -- know what they are doing. :-)
291
292 module  :: { Located (HsModule RdrName) }
293         : 'module' modid maybemoddeprec maybeexports 'where' body 
294                 {% fileSrcSpan >>= \ loc ->
295                    return (L loc (HsModule (Just $2) $4 (fst $6) (snd $6) $3)) }
296         | missing_module_keyword top close
297                 {% fileSrcSpan >>= \ loc ->
298                    return (L loc (HsModule Nothing Nothing 
299                                 (fst $2) (snd $2) Nothing)) }
300
301 missing_module_keyword :: { () }
302         : {- empty -}                           {% pushCurrentContext }
303
304 maybemoddeprec :: { Maybe DeprecTxt }
305         : '{-# DEPRECATED' STRING '#-}'         { Just (getSTRING $2) }
306         |  {- empty -}                          { Nothing }
307
308 body    :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
309         :  '{'            top '}'               { $2 }
310         |      vocurly    top close             { $2 }
311
312 top     :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
313         : importdecls                           { (reverse $1,[]) }
314         | importdecls ';' cvtopdecls            { (reverse $1,$3) }
315         | cvtopdecls                            { ([],$1) }
316
317 cvtopdecls :: { [LHsDecl RdrName] }
318         : topdecls                              { cvTopDecls $1 }
319
320 -----------------------------------------------------------------------------
321 -- Module declaration & imports only
322
323 header  :: { Located (HsModule RdrName) }
324         : 'module' modid maybemoddeprec maybeexports 'where' header_body
325                 {% fileSrcSpan >>= \ loc ->
326                    return (L loc (HsModule (Just $2) $4 $6 [] $3)) }
327         | missing_module_keyword importdecls
328                 {% fileSrcSpan >>= \ loc ->
329                    return (L loc (HsModule Nothing Nothing $2 [] Nothing)) }
330
331 header_body :: { [LImportDecl RdrName] }
332         :  '{'            importdecls           { $2 }
333         |      vocurly    importdecls           { $2 }
334
335 -----------------------------------------------------------------------------
336 -- The Export List
337
338 maybeexports :: { Maybe [LIE RdrName] }
339         :  '(' exportlist ')'                   { Just $2 }
340         |  {- empty -}                          { Nothing }
341
342 exportlist :: { [LIE RdrName] }
343         :  exportlist ',' export                { $3 : $1 }
344         |  exportlist ','                       { $1 }
345         |  export                               { [$1]  }
346         |  {- empty -}                          { [] }
347
348    -- No longer allow things like [] and (,,,) to be exported
349    -- They are built in syntax, always available
350 export  :: { LIE RdrName }
351         :  qvar                         { L1 (IEVar (unLoc $1)) }
352         |  oqtycon                      { L1 (IEThingAbs (unLoc $1)) }
353         |  oqtycon '(' '..' ')'         { LL (IEThingAll (unLoc $1)) }
354         |  oqtycon '(' ')'              { LL (IEThingWith (unLoc $1) []) }
355         |  oqtycon '(' qcnames ')'      { LL (IEThingWith (unLoc $1) (reverse $3)) }
356         |  'module' modid               { LL (IEModuleContents (unLoc $2)) }
357
358 qcnames :: { [RdrName] }
359         :  qcnames ',' qcname                   { unLoc $3 : $1 }
360         |  qcname                               { [unLoc $1]  }
361
362 qcname  :: { Located RdrName }  -- Variable or data constructor
363         :  qvar                                 { $1 }
364         |  gcon                                 { $1 }
365
366 -----------------------------------------------------------------------------
367 -- Import Declarations
368
369 -- import decls can be *empty*, or even just a string of semicolons
370 -- whereas topdecls must contain at least one topdecl.
371
372 importdecls :: { [LImportDecl RdrName] }
373         : importdecls ';' importdecl            { $3 : $1 }
374         | importdecls ';'                       { $1 }
375         | importdecl                            { [ $1 ] }
376         | {- empty -}                           { [] }
377
378 importdecl :: { LImportDecl RdrName }
379         : 'import' maybe_src optqualified modid maybeas maybeimpspec 
380                 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
381
382 maybe_src :: { IsBootInterface }
383         : '{-# SOURCE' '#-}'                    { True }
384         | {- empty -}                           { False }
385
386 optqualified :: { Bool }
387         : 'qualified'                           { True  }
388         | {- empty -}                           { False }
389
390 maybeas :: { Located (Maybe Module) }
391         : 'as' modid                            { LL (Just (unLoc $2)) }
392         | {- empty -}                           { noLoc Nothing }
393
394 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
395         : impspec                               { L1 (Just (unLoc $1)) }
396         | {- empty -}                           { noLoc Nothing }
397
398 impspec :: { Located (Bool, [LIE RdrName]) }
399         :  '(' exportlist ')'                   { LL (False, reverse $2) }
400         |  'hiding' '(' exportlist ')'          { LL (True,  reverse $3) }
401
402 -----------------------------------------------------------------------------
403 -- Fixity Declarations
404
405 prec    :: { Int }
406         : {- empty -}           { 9 }
407         | INTEGER               {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
408
409 infix   :: { Located FixityDirection }
410         : 'infix'                               { L1 InfixN  }
411         | 'infixl'                              { L1 InfixL  }
412         | 'infixr'                              { L1 InfixR }
413
414 ops     :: { Located [Located RdrName] }
415         : ops ',' op                            { LL ($3 : unLoc $1) }
416         | op                                    { L1 [$1] }
417
418 -----------------------------------------------------------------------------
419 -- Top-Level Declarations
420
421 topdecls :: { OrdList (LHsDecl RdrName) }       -- Reversed
422         : topdecls ';' topdecl          { $1 `appOL` $3 }
423         | topdecls ';'                  { $1 }
424         | topdecl                       { $1 }
425
426 topdecl :: { OrdList (LHsDecl RdrName) }
427         : tycl_decl                     { unitOL (L1 (TyClD (unLoc $1))) }
428         | 'instance' inst_type where
429                 { let (binds,sigs) = cvBindsAndSigs (unLoc $3)
430                   in unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs))) }
431         | 'default' '(' comma_types0 ')'        { unitOL (LL $ DefD (DefaultDecl $3)) }
432         | 'foreign' fdecl                       { unitOL (LL (unLoc $2)) }
433         | '{-# DEPRECATED' deprecations '#-}'   { $2 }
434         | '{-# RULES' rules '#-}'               { $2 }
435         | '$(' exp ')'                          { unitOL (LL $ SpliceD (SpliceDecl $2)) }
436         | decl                                  { unLoc $1 }
437
438 tycl_decl :: { LTyClDecl RdrName }
439         : 'type' type '=' ctype 
440                 -- Note type on the left of the '='; this allows
441                 -- infix type constructors to be declared
442                 -- 
443                 -- Note ctype, not sigtype, on the right
444                 -- We allow an explicit for-all but we don't insert one
445                 -- in   type Foo a = (b,b)
446                 -- Instead we just say b is out of scope
447                 {% do { (tc,tvs) <- checkSynHdr $2
448                       ; return (LL (TySynonym tc tvs $4)) } }
449
450         | 'data' tycl_hdr constrs deriving
451                 { L (comb4 $1 $2 $3 $4) -- We need the location on tycl_hdr 
452                                         -- in case constrs and deriving are both empty
453                     (mkTyData DataType $2 Nothing (reverse (unLoc $3)) (unLoc $4)) }
454
455         | 'data' tycl_hdr opt_kind_sig 'where' gadt_constrlist  -- No deriving for GADTs
456                 { L (comb4 $1 $2 $4 $5)
457                     (mkTyData DataType $2 $3 (reverse (unLoc $5)) Nothing) }
458
459         | 'newtype' tycl_hdr '=' newconstr deriving
460                 { L (comb3 $1 $4 $5)
461                     (mkTyData NewType $2 Nothing [$4] (unLoc $5)) }
462
463         | 'class' tycl_hdr fds where
464                 { let 
465                         (binds,sigs) = cvBindsAndSigs (unLoc $4)
466                   in
467                   L (comb4 $1 $2 $3 $4) (mkClassDecl (unLoc $2) (unLoc $3) sigs 
468                                           binds) }
469
470 opt_kind_sig :: { Maybe Kind }
471         :                               { Nothing }
472         | '::' kind                     { Just $2 }
473
474 -- tycl_hdr parses the header of a type or class decl,
475 -- which takes the form
476 --      T a b
477 --      Eq a => T a
478 --      (Eq a, Ord b) => T a b
479 -- Rather a lot of inlining here, else we get reduce/reduce errors
480 tycl_hdr :: { Located (LHsContext RdrName, Located RdrName, [LHsTyVarBndr RdrName]) }
481         : context '=>' type             {% checkTyClHdr $1         $3 >>= return.LL }
482         | type                          {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
483
484 -----------------------------------------------------------------------------
485 -- Nested declarations
486
487 decls   :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
488         : decls ';' decl                { LL (unLoc $1 `appOL` unLoc $3) }
489         | decls ';'                     { LL (unLoc $1) }
490         | decl                          { $1 }
491         | {- empty -}                   { noLoc nilOL }
492
493
494 decllist :: { Located (OrdList (LHsDecl RdrName)) }     -- Reversed
495         : '{'            decls '}'      { LL (unLoc $2) }
496         |     vocurly    decls close    { $2 }
497
498 where   :: { Located (OrdList (LHsDecl RdrName)) }      -- Reversed
499                                 -- No implicit parameters
500         : 'where' decllist              { LL (unLoc $2) }
501         | {- empty -}                   { noLoc nilOL }
502
503 binds   ::  { Located [HsBindGroup RdrName] }   -- May have implicit parameters
504         : decllist                      { L1 [cvBindGroup (unLoc $1)] }
505         | '{'            dbinds '}'     { LL [HsIPBinds (unLoc $2)] }
506         |     vocurly    dbinds close   { L (getLoc $2) [HsIPBinds (unLoc $2)] }
507
508 wherebinds :: { Located [HsBindGroup RdrName] } -- May have implicit parameters
509         : 'where' binds                 { LL (unLoc $2) }
510         | {- empty -}                   { noLoc [] }
511
512
513 -----------------------------------------------------------------------------
514 -- Transformation Rules
515
516 rules   :: { OrdList (LHsDecl RdrName) }        -- Reversed
517         :  rules ';' rule                       { $1 `snocOL` $3 }
518         |  rules ';'                            { $1 }
519         |  rule                                 { unitOL $1 }
520         |  {- empty -}                          { nilOL }
521
522 rule    :: { LHsDecl RdrName }
523         : STRING activation rule_forall infixexp '=' exp
524              { LL $ RuleD (HsRule (getSTRING $1) $2 $3 $4 $6) }
525
526 activation :: { Activation }           -- Omitted means AlwaysActive
527         : {- empty -}                           { AlwaysActive }
528         | explicit_activation                   { $1 }
529
530 inverse_activation :: { Activation }   -- Omitted means NeverActive
531         : {- empty -}                           { NeverActive }
532         | explicit_activation                   { $1 }
533
534 explicit_activation :: { Activation }  -- In brackets
535         : '[' INTEGER ']'               { ActiveAfter  (fromInteger (getINTEGER $2)) }
536         | '[' '~' INTEGER ']'           { ActiveBefore (fromInteger (getINTEGER $3)) }
537
538 rule_forall :: { [RuleBndr RdrName] }
539         : 'forall' rule_var_list '.'            { $2 }
540         | {- empty -}                           { [] }
541
542 rule_var_list :: { [RuleBndr RdrName] }
543         : rule_var                              { [$1] }
544         | rule_var rule_var_list                { $1 : $2 }
545
546 rule_var :: { RuleBndr RdrName }
547         : varid                                 { RuleBndr $1 }
548         | '(' varid '::' ctype ')'              { RuleBndrSig $2 $4 }
549
550 -----------------------------------------------------------------------------
551 -- Deprecations (c.f. rules)
552
553 deprecations :: { OrdList (LHsDecl RdrName) }   -- Reversed
554         : deprecations ';' deprecation          { $1 `appOL` $3 }
555         | deprecations ';'                      { $1 }
556         | deprecation                           { $1 }
557         | {- empty -}                           { nilOL }
558
559 -- SUP: TEMPORARY HACK, not checking for `module Foo'
560 deprecation :: { OrdList (LHsDecl RdrName) }
561         : depreclist STRING
562                 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2)) 
563                        | n <- unLoc $1 ] }
564
565
566 -----------------------------------------------------------------------------
567 -- Foreign import and export declarations
568
569 -- for the time being, the following accepts foreign declarations conforming
570 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
571 --
572 -- * a flag indicates whether pre-standard declarations have been used and
573 --   triggers a deprecation warning further down the road
574 --
575 -- NB: The first two rules could be combined into one by replacing `safety1'
576 --     with `safety'.  However, the combined rule conflicts with the
577 --     DEPRECATED rules.
578 --
579 fdecl :: { LHsDecl RdrName }
580 fdecl : 'import' callconv safety1 fspec
581                 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
582       | 'import' callconv         fspec         
583                 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
584                         return (LL d) } }
585       | 'export' callconv fspec
586                 {% mkExport $2 (unLoc $3) >>= return.LL }
587         -- the following syntax is DEPRECATED
588       | fdecl1DEPRECATED                        { L1 (ForD (unLoc $1)) }
589       | fdecl2DEPRECATED                        { L1 (unLoc $1) }
590
591 fdecl1DEPRECATED :: { LForeignDecl RdrName }
592 fdecl1DEPRECATED 
593   ----------- DEPRECATED label decls ------------
594   : 'label' ext_name varid '::' sigtype
595     { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS 
596                                    (CLabel ($2 `orElse` mkExtName (unLoc $3)))) True }
597
598   ----------- DEPRECATED ccall/stdcall decls ------------
599   --
600   -- NB: This business with the case expression below may seem overly
601   --     complicated, but it is necessary to avoid some conflicts.
602
603     -- DEPRECATED variant #1: lack of a calling convention specification
604     --                        (import) 
605   | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
606     { let
607         target = StaticTarget ($2 `orElse` mkExtName (unLoc $4))
608       in
609       LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS 
610                                    (CFunction target)) True }
611
612     -- DEPRECATED variant #2: external name consists of two separate strings
613     --                        (module name and function name) (import)
614   | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
615     {% case $2 of
616          DNCall      -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
617          CCall cconv -> return $
618            let
619              imp = CFunction (StaticTarget (getSTRING $4))
620            in
621            LL $ ForeignImport $6 $8 (CImport cconv $5 nilFS nilFS imp) True }
622
623     -- DEPRECATED variant #3: `unsafe' after entity
624   | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
625     {% case $2 of
626          DNCall      -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
627          CCall cconv -> return $
628            let
629              imp = CFunction (StaticTarget (getSTRING $3))
630            in
631            LL $ ForeignImport $5 $7 (CImport cconv PlayRisky nilFS nilFS imp) True }
632
633     -- DEPRECATED variant #4: use of the special identifier `dynamic' without
634     --                        an explicit calling convention (import)
635   | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
636     { LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS 
637                                    (CFunction DynamicTarget)) True }
638
639     -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
640   | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
641     {% case $2 of
642          DNCall      -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
643          CCall cconv -> return $
644            LL $ ForeignImport $5 $7 (CImport cconv $4 nilFS nilFS 
645                                         (CFunction DynamicTarget)) True }
646
647     -- DEPRECATED variant #6: lack of a calling convention specification
648     --                        (export) 
649   | 'export' {-no callconv-} ext_name varid '::' sigtype
650     { LL $ ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName (unLoc $3))
651                                    defaultCCallConv)) True }
652
653     -- DEPRECATED variant #7: external name consists of two separate strings
654     --                        (module name and function name) (export)
655   | 'export' callconv STRING STRING varid '::' sigtype
656     {% case $2 of
657          DNCall      -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
658          CCall cconv -> return $
659            LL $ ForeignExport $5 $7 
660                          (CExport (CExportStatic (getSTRING $4) cconv)) True }
661
662     -- DEPRECATED variant #8: use of the special identifier `dynamic' without
663     --                        an explicit calling convention (export)
664   | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
665     { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS 
666                                    CWrapper) True }
667
668     -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
669   | 'export' callconv 'dynamic' varid '::' sigtype
670     {% case $2 of
671          DNCall      -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
672          CCall cconv -> return $
673            LL $ ForeignImport $4 $6 
674                  (CImport cconv (PlaySafe False) nilFS nilFS CWrapper) True }
675
676   ----------- DEPRECATED .NET decls ------------
677   -- NB: removed the .NET call declaration, as it is entirely subsumed
678   --     by the new standard FFI declarations
679
680 fdecl2DEPRECATED :: { LHsDecl RdrName }
681 fdecl2DEPRECATED 
682   : 'import' 'dotnet' 'type' ext_name tycon { LL $ TyClD (ForeignType $5 $4 DNType) }
683     -- left this one unchanged for the moment as type imports are not
684     -- covered currently by the FFI standard -=chak
685
686
687 callconv :: { CallConv }
688           : 'stdcall'                   { CCall  StdCallConv }
689           | 'ccall'                     { CCall  CCallConv   }
690           | 'dotnet'                    { DNCall             }
691
692 safety :: { Safety }
693         : 'unsafe'                      { PlayRisky }
694         | 'safe'                        { PlaySafe False }
695         | 'threadsafe'                  { PlaySafe True  }
696         | {- empty -}                   { PlaySafe False }
697
698 safety1 :: { Safety }
699         : 'unsafe'                      { PlayRisky }
700         | 'safe'                        { PlaySafe  False }
701         | 'threadsafe'                  { PlaySafe  True }
702           -- only needed to avoid conflicts with the DEPRECATED rules
703
704 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
705        : STRING var '::' sigtype      { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
706        |        var '::' sigtype      { LL (noLoc nilFS, $1, $3) }
707          -- if the entity string is missing, it defaults to the empty string;
708          -- the meaning of an empty entity string depends on the calling
709          -- convention
710
711 -- DEPRECATED syntax
712 ext_name :: { Maybe CLabelString }
713         : STRING                { Just (getSTRING $1) }
714         | STRING STRING         { Just (getSTRING $2) } -- Ignore "module name" for now
715         | {- empty -}           { Nothing }
716
717
718 -----------------------------------------------------------------------------
719 -- Type signatures
720
721 opt_sig :: { Maybe (LHsType RdrName) }
722         : {- empty -}                   { Nothing }
723         | '::' sigtype                  { Just $2 }
724
725 opt_asig :: { Maybe (LHsType RdrName) }
726         : {- empty -}                   { Nothing }
727         | '::' atype                    { Just $2 }
728
729 sigtypes1 :: { [LHsType RdrName] }
730         : sigtype                       { [ $1 ] }
731         | sigtype ',' sigtypes1         { $1 : $3 }
732
733 sigtype :: { LHsType RdrName }
734         : ctype                         { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
735         -- Wrap an Implicit forall if there isn't one there already
736
737 sig_vars :: { Located [Located RdrName] }
738          : sig_vars ',' var             { LL ($3 : unLoc $1) }
739          | var                          { L1 [$1] }
740
741 -----------------------------------------------------------------------------
742 -- Types
743
744 strict_mark :: { Located HsBang }
745         : '!'                           { L1 HsStrict }
746         | '{-# UNPACK' '#-}' '!'        { LL HsUnbox }
747
748 -- A ctype is a for-all type
749 ctype   :: { LHsType RdrName }
750         : 'forall' tv_bndrs '.' ctype   { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
751         | context '=>' type             { LL $ mkImplicitHsForAllTy   $1 $3 }
752         -- A type of form (context => type) is an *implicit* HsForAllTy
753         | type                          { $1 }
754
755 -- We parse a context as a btype so that we don't get reduce/reduce
756 -- errors in ctype.  The basic problem is that
757 --      (Eq a, Ord a)
758 -- looks so much like a tuple type.  We can't tell until we find the =>
759 context :: { LHsContext RdrName }
760         : btype                         {% checkContext $1 }
761
762 type :: { LHsType RdrName }
763         : ipvar '::' gentype            { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
764         | gentype                       { $1 }
765
766 gentype :: { LHsType RdrName }
767         : btype                         { $1 }
768         | btype qtyconop gentype        { LL $ HsOpTy $1 $2 $3 }
769         | btype tyvarop  gentype        { LL $ HsOpTy $1 $2 $3 }
770         | btype '->' gentype            { LL $ HsFunTy $1 $3 }
771
772 btype :: { LHsType RdrName }
773         : btype atype                   { LL $ HsAppTy $1 $2 }
774         | atype                         { $1 }
775
776 atype :: { LHsType RdrName }
777         : gtycon                        { L1 (HsTyVar (unLoc $1)) }
778         | tyvarid                       { L1 (HsTyVar (unLoc $1)) }
779         | strict_mark atype             { LL (HsBangTy (unLoc $1) $2) }
780         | '(' type ',' comma_types1 ')' { LL $ HsTupleTy Boxed  ($2:$4) }
781         | '(#' comma_types1 '#)'        { LL $ HsTupleTy Unboxed $2     }
782         | '[' type ']'                  { LL $ HsListTy  $2 }
783         | '[:' type ':]'                { LL $ HsPArrTy  $2 }
784         | '(' ctype ')'                 { LL $ HsParTy   $2 }
785         | '(' ctype '::' kind ')'       { LL $ HsKindSig $2 $4 }
786 -- Generics
787         | INTEGER                       { L1 (HsNumTy (getINTEGER $1)) }
788
789 -- An inst_type is what occurs in the head of an instance decl
790 --      e.g.  (Foo a, Gaz b) => Wibble a b
791 -- It's kept as a single type, with a MonoDictTy at the right
792 -- hand corner, for convenience.
793 inst_type :: { LHsType RdrName }
794         : sigtype                       {% checkInstType $1 }
795
796 inst_types1 :: { [LHsType RdrName] }
797         : inst_type                     { [$1] }
798         | inst_type ',' inst_types1     { $1 : $3 }
799
800 comma_types0  :: { [LHsType RdrName] }
801         : comma_types1                  { $1 }
802         | {- empty -}                   { [] }
803
804 comma_types1    :: { [LHsType RdrName] }
805         : type                          { [$1] }
806         | type  ',' comma_types1        { $1 : $3 }
807
808 tv_bndrs :: { [LHsTyVarBndr RdrName] }
809          : tv_bndr tv_bndrs             { $1 : $2 }
810          | {- empty -}                  { [] }
811
812 tv_bndr :: { LHsTyVarBndr RdrName }
813         : tyvar                         { L1 (UserTyVar (unLoc $1)) }
814         | '(' tyvar '::' kind ')'       { LL (KindedTyVar (unLoc $2) $4) }
815
816 fds :: { Located [Located ([RdrName], [RdrName])] }
817         : {- empty -}                   { noLoc [] }
818         | '|' fds1                      { LL (reverse (unLoc $2)) }
819
820 fds1 :: { Located [Located ([RdrName], [RdrName])] }
821         : fds1 ',' fd                   { LL ($3 : unLoc $1) }
822         | fd                            { L1 [$1] }
823
824 fd :: { Located ([RdrName], [RdrName]) }
825         : varids0 '->' varids0          { L (comb3 $1 $2 $3)
826                                            (reverse (unLoc $1), reverse (unLoc $3)) }
827
828 varids0 :: { Located [RdrName] }
829         : {- empty -}                   { noLoc [] }
830         | varids0 tyvar                 { LL (unLoc $2 : unLoc $1) }
831
832 -----------------------------------------------------------------------------
833 -- Kinds
834
835 kind    :: { Kind }
836         : akind                 { $1 }
837         | akind '->' kind       { mkArrowKind $1 $3 }
838
839 akind   :: { Kind }
840         : '*'                   { liftedTypeKind }
841         | '(' kind ')'          { $2 }
842
843
844 -----------------------------------------------------------------------------
845 -- Datatype declarations
846
847 newconstr :: { LConDecl RdrName }
848         : conid atype   { LL $ ConDecl $1 [] (noLoc []) (PrefixCon [$2]) }
849         | conid '{' var '::' ctype '}'
850                         { LL $ ConDecl $1 [] (noLoc []) (RecCon [($3, $5)]) }
851
852 gadt_constrlist :: { Located [LConDecl RdrName] }
853         : '{'            gadt_constrs '}'       { LL (unLoc $2) }
854         |     vocurly    gadt_constrs close     { $2 }
855
856 gadt_constrs :: { Located [LConDecl RdrName] }
857         : gadt_constrs ';' gadt_constr  { LL ($3 : unLoc $1) }
858         | gadt_constrs ';'              { $1 }
859         | gadt_constr                   { L1 [$1] } 
860
861 gadt_constr :: { LConDecl RdrName }
862         : qcon '::' sigtype
863               { LL (GadtDecl $1 $3) } 
864
865 constrs :: { Located [LConDecl RdrName] }
866         : {- empty; a GHC extension -}  { noLoc [] }
867         | '=' constrs1                  { LL (unLoc $2) }
868
869 constrs1 :: { Located [LConDecl RdrName] }
870         : constrs1 '|' constr           { LL ($3 : unLoc $1) }
871         | constr                        { L1 [$1] }
872
873 constr :: { LConDecl RdrName }
874         : forall context '=>' constr_stuff      
875                 { let (con,details) = unLoc $4 in 
876                   LL (ConDecl con (unLoc $1) $2 details) }
877         | forall constr_stuff
878                 { let (con,details) = unLoc $2 in 
879                   LL (ConDecl con (unLoc $1) (noLoc []) details) }
880
881 forall :: { Located [LHsTyVarBndr RdrName] }
882         : 'forall' tv_bndrs '.'         { LL $2 }
883         | {- empty -}                   { noLoc [] }
884
885 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
886 -- We parse the constructor declaration 
887 --      C t1 t2
888 -- as a btype (treating C as a type constructor) and then convert C to be
889 -- a data constructor.  Reason: it might continue like this:
890 --      C t1 t2 %: D Int
891 -- in which case C really would be a type constructor.  We can't resolve this
892 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
893         : btype                         {% mkPrefixCon $1 [] >>= return.LL }
894         | oqtycon '{' '}'               {% mkRecCon $1 [] >>= return.LL }
895         | oqtycon '{' fielddecls '}'    {% mkRecCon $1 $3 >>= return.LL }
896         | btype conop btype             { LL ($2, InfixCon $1 $3) }
897
898 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
899         : fielddecl ',' fielddecls      { unLoc $1 : $3 }
900         | fielddecl                     { [unLoc $1] }
901
902 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
903         : sig_vars '::' ctype           { LL (reverse (unLoc $1), $3) }
904
905 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
906 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
907 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
908 -- We don't allow a context, but that's sorted out by the type checker.
909 deriving :: { Located (Maybe [LHsType RdrName]) }
910         : {- empty -}                           { noLoc Nothing }
911         | 'deriving' qtycon     {% do { let { L loc tv = $2 }
912                                       ; p <- checkInstType (L loc (HsTyVar tv))
913                                       ; return (LL (Just [p])) } }
914         | 'deriving' '(' ')'                    { LL (Just []) }
915         | 'deriving' '(' inst_types1 ')'        { LL (Just $3) }
916              -- Glasgow extension: allow partial 
917              -- applications in derivings
918
919 -----------------------------------------------------------------------------
920 -- Value definitions
921
922 {- There's an awkward overlap with a type signature.  Consider
923         f :: Int -> Int = ...rhs...
924    Then we can't tell whether it's a type signature or a value
925    definition with a result signature until we see the '='.
926    So we have to inline enough to postpone reductions until we know.
927 -}
928
929 {-
930   ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
931   instead of qvar, we get another shift/reduce-conflict. Consider the
932   following programs:
933   
934      { (^^) :: Int->Int ; }          Type signature; only var allowed
935
936      { (^^) :: Int->Int = ... ; }    Value defn with result signature;
937                                      qvar allowed (because of instance decls)
938   
939   We can't tell whether to reduce var to qvar until after we've read the signatures.
940 -}
941
942 decl    :: { Located (OrdList (LHsDecl RdrName)) }
943         : sigdecl                       { $1 }
944         | infixexp opt_sig rhs          {% do { r <- checkValDef $1 $2 $3;
945                                                 return (LL $ unitOL (LL $ ValD r)) } }
946
947 rhs     :: { Located (GRHSs RdrName) }
948         : '=' exp wherebinds    { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
949         | gdrhs wherebinds      { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
950
951 gdrhs :: { Located [LGRHS RdrName] }
952         : gdrhs gdrh            { LL ($2 : unLoc $1) }
953         | gdrh                  { L1 [$1] }
954
955 gdrh :: { LGRHS RdrName }
956         : '|' quals '=' exp     { LL $ GRHS (reverse (L (getLoc $4) (ResultStmt $4) : 
957                                                         unLoc $2)) }
958
959 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
960         : infixexp '::' sigtype
961                                 {% do s <- checkValSig $1 $3; 
962                                       return (LL $ unitOL (LL $ SigD s)) }
963                 -- See the above notes for why we need infixexp here
964         | var ',' sig_vars '::' sigtype 
965                                 { LL $ toOL [ LL $ SigD (Sig n $5) | n <- $1 : unLoc $3 ] }
966         | infix prec ops        { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
967                                              | n <- unLoc $3 ] }
968         | '{-# INLINE'   activation qvar '#-}'        
969                                 { LL $ unitOL (LL $ SigD (InlineSig True  $3 $2)) }
970         | '{-# NOINLINE' inverse_activation qvar '#-}' 
971                                 { LL $ unitOL (LL $ SigD (InlineSig False $3 $2)) }
972         | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
973                                 { LL $ toOL [ LL $ SigD (SpecSig $2 t)
974                                             | t <- $4] }
975         | '{-# SPECIALISE' 'instance' inst_type '#-}'
976                                 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
977
978 -----------------------------------------------------------------------------
979 -- Expressions
980
981 exp   :: { LHsExpr RdrName }
982         : infixexp '::' sigtype         { LL $ ExprWithTySig $1 $3 }
983         | infixexp '-<' exp             { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
984         | infixexp '>-' exp             { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
985         | infixexp '-<<' exp            { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
986         | infixexp '>>-' exp            { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
987         | infixexp                      { $1 }
988
989 infixexp :: { LHsExpr RdrName }
990         : exp10                         { $1 }
991         | infixexp qop exp10            { LL (OpApp $1 $2 (panic "fixity") $3) }
992
993 exp10 :: { LHsExpr RdrName }
994         : '\\' aexp aexps opt_asig '->' exp     
995                         {% checkPatterns ($2 : reverse $3) >>= \ ps -> 
996                            return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
997                                             (GRHSs (unguardedRHS $6) []
998                                                         )])) }
999         | 'let' binds 'in' exp                  { LL $ HsLet (unLoc $2) $4 }
1000         | 'if' exp 'then' exp 'else' exp        { LL $ HsIf $2 $4 $6 }
1001         | 'case' exp 'of' altslist              { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1002         | '-' fexp                              { LL $ mkHsNegApp $2 }
1003
1004         | 'do' stmtlist                 {% let loc = comb2 $1 $2 in
1005                                            checkDo loc (unLoc $2)  >>= \ stmts ->
1006                                            return (L loc (mkHsDo DoExpr stmts)) }
1007         | 'mdo' stmtlist                {% let loc = comb2 $1 $2 in
1008                                            checkMDo loc (unLoc $2)  >>= \ stmts ->
1009                                            return (L loc (mkHsDo MDoExpr stmts)) }
1010
1011         | scc_annot exp                         { LL $ if opt_SccProfilingOn
1012                                                         then HsSCC (unLoc $1) $2
1013                                                         else HsPar $2 }
1014
1015         | 'proc' aexp '->' exp  
1016                         {% checkPattern $2 >>= \ p -> 
1017                            return (LL $ HsProc p (LL $ HsCmdTop $4 [] 
1018                                                    placeHolderType undefined)) }
1019                                                 -- TODO: is LL right here?
1020
1021         | '{-# CORE' STRING '#-}' exp           { LL $ HsCoreAnn (getSTRING $2) $4 }
1022                                                     -- hdaume: core annotation
1023         | fexp                                  { $1 }
1024
1025 scc_annot :: { Located FastString }
1026         : '_scc_' STRING                        { LL $ getSTRING $2 }
1027         | '{-# SCC' STRING '#-}'                { LL $ getSTRING $2 }
1028
1029 fexp    :: { LHsExpr RdrName }
1030         : fexp aexp                             { LL $ HsApp $1 $2 }
1031         | aexp                                  { $1 }
1032
1033 aexps   :: { [LHsExpr RdrName] }
1034         : aexps aexp                            { $2 : $1 }
1035         | {- empty -}                           { [] }
1036
1037 aexp    :: { LHsExpr RdrName }
1038         : qvar '@' aexp                 { LL $ EAsPat $1 $3 }
1039         | '~' aexp                      { LL $ ELazyPat $2 }
1040         | aexp1                         { $1 }
1041
1042 aexp1   :: { LHsExpr RdrName }
1043         : aexp1 '{' fbinds '}'  {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4) 
1044                                                         (reverse $3);
1045                                         return (LL r) }}
1046         | aexp2                 { $1 }
1047
1048 -- Here was the syntax for type applications that I was planning
1049 -- but there are difficulties (e.g. what order for type args)
1050 -- so it's not enabled yet.
1051 -- But this case *is* used for the left hand side of a generic definition,
1052 -- which is parsed as an expression before being munged into a pattern
1053         | qcname '{|' gentype '|}'      { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1054                                                      (sL (getLoc $3) (HsType $3)) }
1055
1056 aexp2   :: { LHsExpr RdrName }
1057         : ipvar                         { L1 (HsIPVar $! unLoc $1) }
1058         | qcname                        { L1 (HsVar   $! unLoc $1) }
1059         | literal                       { L1 (HsLit   $! unLoc $1) }
1060         | INTEGER                       { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1061         | RATIONAL                      { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1062         | '(' exp ')'                   { LL (HsPar $2) }
1063         | '(' exp ',' texps ')'         { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1064         | '(#' texps '#)'               { LL $ ExplicitTuple (reverse $2)      Unboxed }
1065         | '[' list ']'                  { LL (unLoc $2) }
1066         | '[:' parr ':]'                { LL (unLoc $2) }
1067         | '(' infixexp qop ')'          { LL $ SectionL $2 $3 }
1068         | '(' qopm infixexp ')'         { LL $ SectionR $2 $3 }
1069         | '_'                           { L1 EWildPat }
1070         
1071         -- MetaHaskell Extension
1072         | TH_ID_SPLICE          { L1 $ HsSpliceE (mkHsSplice 
1073                                         (L1 $ HsVar (mkUnqual varName 
1074                                                         (getTH_ID_SPLICE $1)))) } -- $x
1075         | '$(' exp ')'          { LL $ HsSpliceE (mkHsSplice $2) }               -- $( exp )
1076
1077         | TH_VAR_QUOTE qvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1078         | TH_VAR_QUOTE gcon     { LL $ HsBracket (VarBr (unLoc $2)) }
1079         | TH_TY_QUOTE tyvar     { LL $ HsBracket (VarBr (unLoc $2)) }
1080         | TH_TY_QUOTE gtycon    { LL $ HsBracket (VarBr (unLoc $2)) }
1081         | '[|' exp '|]'         { LL $ HsBracket (ExpBr $2) }                       
1082         | '[t|' ctype '|]'      { LL $ HsBracket (TypBr $2) }                       
1083         | '[p|' infixexp '|]'   {% checkPattern $2 >>= \p ->
1084                                            return (LL $ HsBracket (PatBr p)) }
1085         | '[d|' cvtopbody '|]'  { LL $ HsBracket (DecBr (mkGroup $2)) }
1086
1087         -- arrow notation extension
1088         | '(|' aexp2 cmdargs '|)'       { LL $ HsArrForm $2 Nothing (reverse $3) }
1089
1090 cmdargs :: { [LHsCmdTop RdrName] }
1091         : cmdargs acmd                  { $2 : $1 }
1092         | {- empty -}                   { [] }
1093
1094 acmd    :: { LHsCmdTop RdrName }
1095         : aexp2                 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1096
1097 cvtopbody :: { [LHsDecl RdrName] }
1098         :  '{'            cvtopdecls0 '}'               { $2 }
1099         |      vocurly    cvtopdecls0 close             { $2 }
1100
1101 cvtopdecls0 :: { [LHsDecl RdrName] }
1102         : {- empty -}           { [] }
1103         | cvtopdecls            { $1 }
1104
1105 texps :: { [LHsExpr RdrName] }
1106         : texps ',' exp                 { $3 : $1 }
1107         | exp                           { [$1] }
1108
1109
1110 -----------------------------------------------------------------------------
1111 -- List expressions
1112
1113 -- The rules below are little bit contorted to keep lexps left-recursive while
1114 -- avoiding another shift/reduce-conflict.
1115
1116 list :: { LHsExpr RdrName }
1117         : exp                   { L1 $ ExplicitList placeHolderType [$1] }
1118         | lexps                 { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1119         | exp '..'              { LL $ ArithSeqIn (From $1) }
1120         | exp ',' exp '..'      { LL $ ArithSeqIn (FromThen $1 $3) }
1121         | exp '..' exp          { LL $ ArithSeqIn (FromTo $1 $3) }
1122         | exp ',' exp '..' exp  { LL $ ArithSeqIn (FromThenTo $1 $3 $5) }
1123         | exp pquals            { LL $ mkHsDo ListComp 
1124                                         (reverse (L (getLoc $1) (ResultStmt $1) : 
1125                                            unLoc $2)) }
1126
1127 lexps :: { Located [LHsExpr RdrName] }
1128         : lexps ',' exp                 { LL ($3 : unLoc $1) }
1129         | exp ',' exp                   { LL [$3,$1] }
1130
1131 -----------------------------------------------------------------------------
1132 -- List Comprehensions
1133
1134 pquals :: { Located [LStmt RdrName] }   -- Either a singleton ParStmt, 
1135                                         -- or a reversed list of Stmts
1136         : pquals1                       { case unLoc $1 of
1137                                             [qs] -> L1 qs
1138                                             qss  -> L1 [L1 (ParStmt stmtss)]
1139                                                  where
1140                                                     stmtss = [ (reverse qs, undefined) 
1141                                                              | qs <- qss ]
1142                                         }
1143                         
1144 pquals1 :: { Located [[LStmt RdrName]] }
1145         : pquals1 '|' quals             { LL (unLoc $3 : unLoc $1) }
1146         | '|' quals                     { L (getLoc $2) [unLoc $2] }
1147
1148 quals :: { Located [LStmt RdrName] }
1149         : quals ',' qual                { LL ($3 : unLoc $1) }
1150         | qual                          { L1 [$1] }
1151
1152 -----------------------------------------------------------------------------
1153 -- Parallel array expressions
1154
1155 -- The rules below are little bit contorted; see the list case for details.
1156 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1157 -- Moreover, we allow explicit arrays with no element (represented by the nil
1158 -- constructor in the list case).
1159
1160 parr :: { LHsExpr RdrName }
1161         :                               { noLoc (ExplicitPArr placeHolderType []) }
1162         | exp                           { L1 $ ExplicitPArr placeHolderType [$1] }
1163         | lexps                         { L1 $ ExplicitPArr placeHolderType 
1164                                                        (reverse (unLoc $1)) }
1165         | exp '..' exp                  { LL $ PArrSeqIn (FromTo $1 $3) }
1166         | exp ',' exp '..' exp          { LL $ PArrSeqIn (FromThenTo $1 $3 $5) }
1167         | exp pquals                    { LL $ mkHsDo PArrComp 
1168                                             (reverse (L (getLoc $1) (ResultStmt $1) :
1169                                                  unLoc $2))
1170                                         }
1171
1172 -- We are reusing `lexps' and `pquals' from the list case.
1173
1174 -----------------------------------------------------------------------------
1175 -- Case alternatives
1176
1177 altslist :: { Located [LMatch RdrName] }
1178         : '{'            alts '}'       { LL (reverse (unLoc $2)) }
1179         |     vocurly    alts  close    { L (getLoc $2) (reverse (unLoc $2)) }
1180
1181 alts    :: { Located [LMatch RdrName] }
1182         : alts1                         { L1 (unLoc $1) }
1183         | ';' alts                      { LL (unLoc $2) }
1184
1185 alts1   :: { Located [LMatch RdrName] }
1186         : alts1 ';' alt                 { LL ($3 : unLoc $1) }
1187         | alts1 ';'                     { LL (unLoc $1) }
1188         | alt                           { L1 [$1] }
1189
1190 alt     :: { LMatch RdrName }
1191         : infixexp opt_sig alt_rhs      {%  checkPattern $1 >>= \p ->
1192                                             return (LL (Match [p] $2 (unLoc $3))) }
1193
1194 alt_rhs :: { Located (GRHSs RdrName) }
1195         : ralt wherebinds               { LL (GRHSs (unLoc $1) (unLoc $2)) }
1196
1197 ralt :: { Located [LGRHS RdrName] }
1198         : '->' exp                      { LL (unguardedRHS $2) }
1199         | gdpats                        { L1 (reverse (unLoc $1)) }
1200
1201 gdpats :: { Located [LGRHS RdrName] }
1202         : gdpats gdpat                  { LL ($2 : unLoc $1) }
1203         | gdpat                         { L1 [$1] }
1204
1205 gdpat   :: { LGRHS RdrName }
1206         : '|' quals '->' exp            { let r = L (getLoc $4) (ResultStmt $4)
1207                                           in LL $ GRHS (reverse (r : unLoc $2)) }
1208
1209 -----------------------------------------------------------------------------
1210 -- Statement sequences
1211
1212 stmtlist :: { Located [LStmt RdrName] }
1213         : '{'           stmts '}'       { LL (unLoc $2) }
1214         |     vocurly   stmts close     { $2 }
1215
1216 --      do { ;; s ; s ; ; s ;; }
1217 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1218 -- here, because we need too much lookahead if we see do { e ; }
1219 -- So we use ExprStmts throughout, and switch the last one over
1220 -- in ParseUtils.checkDo instead
1221 stmts :: { Located [LStmt RdrName] }
1222         : stmt stmts_help               { LL ($1 : unLoc $2) }
1223         | ';' stmts                     { LL (unLoc $2) }
1224         | {- empty -}                   { noLoc [] }
1225
1226 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1227         : ';' stmts                     { LL (unLoc $2) }
1228         | {- empty -}                   { noLoc [] }
1229
1230 -- For typing stmts at the GHCi prompt, where 
1231 -- the input may consist of just comments.
1232 maybe_stmt :: { Maybe (LStmt RdrName) }
1233         : stmt                          { Just $1 }
1234         | {- nothing -}                 { Nothing }
1235
1236 stmt  :: { LStmt RdrName }
1237         : qual                          { $1 }
1238         | infixexp '->' exp             {% checkPattern $3 >>= \p ->
1239                                            return (LL $ BindStmt p $1) }
1240         | 'rec' stmtlist                { LL $ RecStmt (unLoc $2) undefined undefined undefined }
1241
1242 qual  :: { LStmt RdrName }
1243         : infixexp '<-' exp             {% checkPattern $1 >>= \p ->
1244                                            return (LL $ BindStmt p $3) }
1245         | exp                           { L1 $ ExprStmt $1 placeHolderType }
1246         | 'let' binds                   { LL $ LetStmt (unLoc $2) }
1247
1248 -----------------------------------------------------------------------------
1249 -- Record Field Update/Construction
1250
1251 fbinds  :: { HsRecordBinds RdrName }
1252         : fbinds1                       { $1 }
1253         | {- empty -}                   { [] }
1254
1255 fbinds1 :: { HsRecordBinds RdrName }
1256         : fbinds1 ',' fbind             { $3 : $1 }
1257         | fbind                         { [$1] }
1258   
1259 fbind   :: { (Located RdrName, LHsExpr RdrName) }
1260         : qvar '=' exp                  { ($1,$3) }
1261
1262 -----------------------------------------------------------------------------
1263 -- Implicit Parameter Bindings
1264
1265 dbinds  :: { Located [LIPBind RdrName] }
1266         : dbinds ';' dbind              { LL ($3 : unLoc $1) }
1267         | dbinds ';'                    { LL (unLoc $1) }
1268         | dbind                         { L1 [$1] }
1269 --      | {- empty -}                   { [] }
1270
1271 dbind   :: { LIPBind RdrName }
1272 dbind   : ipvar '=' exp                 { LL (IPBind (unLoc $1) $3) }
1273
1274 -----------------------------------------------------------------------------
1275 -- Variables, Constructors and Operators.
1276
1277 identifier :: { Located RdrName }
1278         : qvar                          { $1 }
1279         | gcon                          { $1 }
1280         | qvarop                        { $1 }
1281         | qconop                        { $1 }
1282
1283 depreclist :: { Located [RdrName] }
1284 depreclist : deprec_var                 { L1 [unLoc $1] }
1285            | deprec_var ',' depreclist  { LL (unLoc $1 : unLoc $3) }
1286
1287 deprec_var :: { Located RdrName }
1288 deprec_var : var                        { $1 }
1289            | tycon                      { $1 }
1290
1291 gcon    :: { Located RdrName }  -- Data constructor namespace
1292         : sysdcon               { L1 $ nameRdrName (dataConName (unLoc $1)) }
1293         | qcon                  { $1 }
1294 -- the case of '[:' ':]' is part of the production `parr'
1295
1296 sysdcon :: { Located DataCon }  -- Wired in data constructors
1297         : '(' ')'               { LL unitDataCon }
1298         | '(' commas ')'        { LL $ tupleCon Boxed $2 }
1299         | '[' ']'               { LL nilDataCon }
1300
1301 var     :: { Located RdrName }
1302         : varid                 { $1 }
1303         | '(' varsym ')'        { LL (unLoc $2) }
1304
1305 qvar    :: { Located RdrName }
1306         : qvarid                { $1 }
1307         | '(' varsym ')'        { LL (unLoc $2) }
1308         | '(' qvarsym1 ')'      { LL (unLoc $2) }
1309 -- We've inlined qvarsym here so that the decision about
1310 -- whether it's a qvar or a var can be postponed until
1311 -- *after* we see the close paren.
1312
1313 ipvar   :: { Located (IPName RdrName) }
1314         : IPDUPVARID            { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1315         | IPSPLITVARID          { L1 (Linear  (mkUnqual varName (getIPSPLITVARID $1))) }
1316
1317 qcon    :: { Located RdrName }
1318         : qconid                { $1 }
1319         | '(' qconsym ')'       { LL (unLoc $2) }
1320
1321 varop   :: { Located RdrName }
1322         : varsym                { $1 }
1323         | '`' varid '`'         { LL (unLoc $2) }
1324
1325 qvarop :: { Located RdrName }
1326         : qvarsym               { $1 }
1327         | '`' qvarid '`'        { LL (unLoc $2) }
1328
1329 qvaropm :: { Located RdrName }
1330         : qvarsym_no_minus      { $1 }
1331         | '`' qvarid '`'        { LL (unLoc $2) }
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 -- Any operator
1376
1377 op      :: { Located RdrName }   -- used in infix decls
1378         : varop                 { $1 }
1379         | conop                 { $1 }
1380
1381 qop     :: { LHsExpr RdrName }   -- used in sections
1382         : qvarop                { L1 $ HsVar (unLoc $1) }
1383         | qconop                { L1 $ HsVar (unLoc $1) }
1384
1385 qopm    :: { LHsExpr RdrName }   -- used in sections
1386         : qvaropm               { L1 $ HsVar (unLoc $1) }
1387         | qconop                { L1 $ HsVar (unLoc $1) }
1388
1389 -----------------------------------------------------------------------------
1390 -- VarIds
1391
1392 qvarid :: { Located RdrName }
1393         : varid                 { $1 }
1394         | QVARID                { L1 $ mkQual varName (getQVARID $1) }
1395
1396 varid :: { Located RdrName }
1397         : varid_no_unsafe       { $1 }
1398         | 'unsafe'              { L1 $! mkUnqual varName FSLIT("unsafe") }
1399         | 'safe'                { L1 $! mkUnqual varName FSLIT("safe") }
1400         | 'threadsafe'          { L1 $! mkUnqual varName FSLIT("threadsafe") }
1401
1402 varid_no_unsafe :: { Located RdrName }
1403         : VARID                 { L1 $! mkUnqual varName (getVARID $1) }
1404         | special_id            { L1 $! mkUnqual varName (unLoc $1) }
1405         | 'forall'              { L1 $! mkUnqual varName FSLIT("forall") }
1406
1407 tyvar   :: { Located RdrName }
1408 tyvar   : tyvarid               { $1 }
1409         | '(' tyvarsym ')'      { LL (unLoc $2) }
1410
1411 tyvarop :: { Located RdrName }
1412 tyvarop : '`' tyvarid '`'       { LL (unLoc $2) }
1413         | tyvarsym              { $1 }
1414
1415 tyvarid :: { Located RdrName }
1416         : VARID                 { L1 $! mkUnqual tvName (getVARID $1) }
1417         | special_id            { L1 $! mkUnqual tvName (unLoc $1) }
1418         | 'unsafe'              { L1 $! mkUnqual tvName FSLIT("unsafe") }
1419         | 'safe'                { L1 $! mkUnqual tvName FSLIT("safe") }
1420         | 'threadsafe'          { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1421
1422 tyvarsym :: { Located RdrName }
1423 -- Does not include "!", because that is used for strictness marks
1424 --               or ".", because that separates the quantified type vars from the rest
1425 --               or "*", because that's used for kinds
1426 tyvarsym : VARSYM               { L1 $! mkUnqual tvName (getVARSYM $1) }
1427
1428 -- These special_ids are treated as keywords in various places, 
1429 -- but as ordinary ids elsewhere.   'special_id' collects all these
1430 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1431 special_id :: { Located UserFS }
1432 special_id
1433         : 'as'                  { L1 FSLIT("as") }
1434         | 'qualified'           { L1 FSLIT("qualified") }
1435         | 'hiding'              { L1 FSLIT("hiding") }
1436         | 'export'              { L1 FSLIT("export") }
1437         | 'label'               { L1 FSLIT("label")  }
1438         | 'dynamic'             { L1 FSLIT("dynamic") }
1439         | 'stdcall'             { L1 FSLIT("stdcall") }
1440         | 'ccall'               { L1 FSLIT("ccall") }
1441
1442 -----------------------------------------------------------------------------
1443 -- Variables 
1444
1445 qvarsym :: { Located RdrName }
1446         : varsym                { $1 }
1447         | qvarsym1              { $1 }
1448
1449 qvarsym_no_minus :: { Located RdrName }
1450         : varsym_no_minus       { $1 }
1451         | qvarsym1              { $1 }
1452
1453 qvarsym1 :: { Located RdrName }
1454 qvarsym1 : QVARSYM              { L1 $ mkQual varName (getQVARSYM $1) }
1455
1456 varsym :: { Located RdrName }
1457         : varsym_no_minus       { $1 }
1458         | '-'                   { L1 $ mkUnqual varName FSLIT("-") }
1459
1460 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1461         : VARSYM                { L1 $ mkUnqual varName (getVARSYM $1) }
1462         | special_sym           { L1 $ mkUnqual varName (unLoc $1) }
1463
1464
1465 -- See comments with special_id
1466 special_sym :: { Located UserFS }
1467 special_sym : '!'       { L1 FSLIT("!") }
1468             | '.'       { L1 FSLIT(".") }
1469             | '*'       { L1 FSLIT("*") }
1470
1471 -----------------------------------------------------------------------------
1472 -- Data constructors
1473
1474 qconid :: { Located RdrName }   -- Qualified or unqualified
1475         : conid                 { $1 }
1476         | QCONID                { L1 $ mkQual dataName (getQCONID $1) }
1477
1478 conid   :: { Located RdrName }
1479         : CONID                 { L1 $ mkUnqual dataName (getCONID $1) }
1480
1481 qconsym :: { Located RdrName }  -- Qualified or unqualified
1482         : consym                { $1 }
1483         | QCONSYM               { L1 $ mkQual dataName (getQCONSYM $1) }
1484
1485 consym :: { Located RdrName }
1486         : CONSYM                { L1 $ mkUnqual dataName (getCONSYM $1) }
1487
1488         -- ':' means only list cons
1489         | ':'                   { L1 $ consDataCon_RDR }
1490
1491
1492 -----------------------------------------------------------------------------
1493 -- Literals
1494
1495 literal :: { Located HsLit }
1496         : CHAR                  { L1 $ HsChar       $ getCHAR $1 }
1497         | STRING                { L1 $ HsString     $ getSTRING $1 }
1498         | PRIMINTEGER           { L1 $ HsIntPrim    $ getPRIMINTEGER $1 }
1499         | PRIMCHAR              { L1 $ HsCharPrim   $ getPRIMCHAR $1 }
1500         | PRIMSTRING            { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1501         | PRIMFLOAT             { L1 $ HsFloatPrim  $ getPRIMFLOAT $1 }
1502         | PRIMDOUBLE            { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1503
1504 -----------------------------------------------------------------------------
1505 -- Layout
1506
1507 close :: { () }
1508         : vccurly               { () } -- context popped in lexer.
1509         | error                 {% popContext }
1510
1511 -----------------------------------------------------------------------------
1512 -- Miscellaneous (mostly renamings)
1513
1514 modid   :: { Located Module }
1515         : CONID                 { L1 $ mkModuleFS (getCONID $1) }
1516         | QCONID                { L1 $ let (mod,c) = getQCONID $1 in
1517                                   mkModuleFS
1518                                    (mkFastString
1519                                      (unpackFS mod ++ '.':unpackFS c))
1520                                 }
1521
1522 commas :: { Int }
1523         : commas ','                    { $1 + 1 }
1524         | ','                           { 2 }
1525
1526 -----------------------------------------------------------------------------
1527
1528 {
1529 happyError :: P a
1530 happyError = srcParseFail
1531
1532 getVARID        (L _ (ITvarid    x)) = x
1533 getCONID        (L _ (ITconid    x)) = x
1534 getVARSYM       (L _ (ITvarsym   x)) = x
1535 getCONSYM       (L _ (ITconsym   x)) = x
1536 getQVARID       (L _ (ITqvarid   x)) = x
1537 getQCONID       (L _ (ITqconid   x)) = x
1538 getQVARSYM      (L _ (ITqvarsym  x)) = x
1539 getQCONSYM      (L _ (ITqconsym  x)) = x
1540 getIPDUPVARID   (L _ (ITdupipvarid   x)) = x
1541 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1542 getCHAR         (L _ (ITchar     x)) = x
1543 getSTRING       (L _ (ITstring   x)) = x
1544 getINTEGER      (L _ (ITinteger  x)) = x
1545 getRATIONAL     (L _ (ITrational x)) = x
1546 getPRIMCHAR     (L _ (ITprimchar   x)) = x
1547 getPRIMSTRING   (L _ (ITprimstring x)) = x
1548 getPRIMINTEGER  (L _ (ITprimint    x)) = x
1549 getPRIMFLOAT    (L _ (ITprimfloat  x)) = x
1550 getPRIMDOUBLE   (L _ (ITprimdouble x)) = x
1551 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1552
1553 -- Utilities for combining source spans
1554 comb2 :: Located a -> Located b -> SrcSpan
1555 comb2 = combineLocs
1556
1557 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1558 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1559
1560 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1561 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1562                 combineSrcSpans (getLoc c) (getLoc d)
1563
1564 -- strict constructor version:
1565 {-# INLINE sL #-}
1566 sL :: SrcSpan -> a -> Located a
1567 sL span a = span `seq` L span a
1568
1569 -- Make a source location for the file.  We're a bit lazy here and just
1570 -- make a point SrcSpan at line 1, column 0.  Strictly speaking we should
1571 -- try to find the span of the whole file (ToDo).
1572 fileSrcSpan :: P SrcSpan
1573 fileSrcSpan = do 
1574   l <- getSrcLoc; 
1575   let loc = mkSrcLoc (srcLocFile l) 1 0;
1576   return (mkSrcSpan loc loc)
1577 }