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