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