2 -- ---------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1997-2003
7 -- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -- ---------------------------------------------------------------------------
11 module Parser ( parseModule, parseStmt, parseIdentifier, parseIface ) where
13 #define INCLUDE #include
14 INCLUDE "HsVersions.h"
18 import HscTypes ( ModIface, IsBootInterface, DeprecTxt )
21 import TysWiredIn ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
22 listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
23 import Type ( funTyCon )
24 import ForeignCall ( Safety(..), CExportSpec(..),
25 CCallConv(..), CCallTarget(..), defaultCCallConv
27 import OccName ( UserFS, varName, dataName, tcClsName, tvName )
28 import DataCon ( DataCon, dataConName )
29 import SrcLoc ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
30 SrcSpan, combineLocs, mkGeneralSrcSpan, srcLocFile )
32 import CmdLineOpts ( opt_SccProfilingOn )
33 import Type ( Kind, mkArrowKind, liftedTypeKind )
34 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
35 NewOrData(..), Activation(..) )
36 import Bag ( emptyBag )
40 import CStrings ( CLabelString )
42 import Maybes ( orElse )
47 -----------------------------------------------------------------------------
48 Conflicts: 29 shift/reduce, [SDM 19/9/2002]
50 10 for abiguity in 'if x then y else z + 1' [State 136]
51 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
52 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
54 1 for ambiguity in 'if x then y else z with ?x=3' [State 136]
55 (shift parses as 'if x then y else (z with ?x=3)'
57 1 for ambiguity in 'if x then y else z :: T' [State 136]
58 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
60 8 for ambiguity in 'e :: a `b` c'. Does this mean [States 160,246]
64 1 for ambiguity in 'let ?x ...' [State 268]
65 the parser can't tell whether the ?x is the lhs of a normal binding or
66 an implicit binding. Fortunately resolving as shift gives it the only
67 sensible meaning, namely the lhs of an implicit binding.
69 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 332]
70 we don't know whether the '[' starts the activation or not: it
71 might be the start of the declaration with the activation being
74 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 394]
75 since 'forall' is a valid variable name, we don't know whether
76 to treat a forall on the input as the beginning of a quantifier
77 or the beginning of the rule itself. Resolving to shift means
78 it's always treated as a quantifier, hence the above is disallowed.
79 This saves explicitly defining a grammar for the rule lhs that
80 doesn't include 'forall'.
82 6 for conflicts between `fdecl' and `fdeclDEPRECATED', [States 384,385]
83 which are resolved correctly, and moreover,
84 should go away when `fdeclDEPRECATED' is removed.
86 -- ---------------------------------------------------------------------------
87 -- Adding location info
89 This is done in a stylised way using the three macros below, L0, L1
90 and LL. Each of these macros can be thought of as having type
92 L0, L1, LL :: a -> Located a
94 They each add a SrcSpan to their argument.
96 L0 adds 'noSrcSpan', used for empty productions
98 L1 for a production with a single token on the lhs. Grabs the SrcSpan
101 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
102 the first and last tokens.
104 These suffice for the majority of cases. However, we must be
105 especially careful with empty productions: LL won't work if the first
106 or last token on the lhs can represent an empty span. In these cases,
107 we have to calculate the span using more of the tokens from the lhs, eg.
109 | 'newtype' tycl_hdr '=' newconstr deriving
111 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
113 We provide comb3 and comb4 functions which are useful in such cases.
115 Be careful: there's no checking that you actually got this right, the
116 only symptom will be that the SrcSpans of your syntax will be
120 * We must expand these macros *before* running Happy, which is why this file is
121 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
123 #define L0 L noSrcSpan
124 #define L1 sL (getLoc $1)
125 #define LL sL (comb2 $1 $>)
127 -- -----------------------------------------------------------------------------
132 '_' { L _ ITunderscore } -- Haskell keywords
134 'case' { L _ ITcase }
135 'class' { L _ ITclass }
136 'data' { L _ ITdata }
137 'default' { L _ ITdefault }
138 'deriving' { L _ ITderiving }
140 'else' { L _ ITelse }
141 'hiding' { L _ IThiding }
143 'import' { L _ ITimport }
145 'infix' { L _ ITinfix }
146 'infixl' { L _ ITinfixl }
147 'infixr' { L _ ITinfixr }
148 'instance' { L _ ITinstance }
150 'module' { L _ ITmodule }
151 'newtype' { L _ ITnewtype }
153 'qualified' { L _ ITqualified }
154 'then' { L _ ITthen }
155 'type' { L _ ITtype }
156 'where' { L _ ITwhere }
157 '_scc_' { L _ ITscc } -- ToDo: remove
159 'forall' { L _ ITforall } -- GHC extension keywords
160 'foreign' { L _ ITforeign }
161 'export' { L _ ITexport }
162 'label' { L _ ITlabel }
163 'dynamic' { L _ ITdynamic }
164 'safe' { L _ ITsafe }
165 'threadsafe' { L _ ITthreadsafe }
166 'unsafe' { L _ ITunsafe }
168 'stdcall' { L _ ITstdcallconv }
169 'ccall' { L _ ITccallconv }
170 'dotnet' { L _ ITdotnet }
171 'proc' { L _ ITproc } -- for arrow notation extension
172 'rec' { L _ ITrec } -- for arrow notation extension
174 '{-# SPECIALISE' { L _ ITspecialise_prag }
175 '{-# SOURCE' { L _ ITsource_prag }
176 '{-# INLINE' { L _ ITinline_prag }
177 '{-# NOINLINE' { L _ ITnoinline_prag }
178 '{-# RULES' { L _ ITrules_prag }
179 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
180 '{-# SCC' { L _ ITscc_prag }
181 '{-# DEPRECATED' { L _ ITdeprecated_prag }
182 '{-# UNPACK' { L _ ITunpack_prag }
183 '#-}' { L _ ITclose_prag }
185 '..' { L _ ITdotdot } -- reserved symbols
187 '::' { L _ ITdcolon }
191 '<-' { L _ ITlarrow }
192 '->' { L _ ITrarrow }
195 '=>' { L _ ITdarrow }
199 '-<' { L _ ITlarrowtail } -- for arrow notation
200 '>-' { L _ ITrarrowtail } -- for arrow notation
201 '-<<' { L _ ITLarrowtail } -- for arrow notation
202 '>>-' { L _ ITRarrowtail } -- for arrow notation
205 '{' { L _ ITocurly } -- special symbols
207 '{|' { L _ ITocurlybar }
208 '|}' { L _ ITccurlybar }
209 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
210 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
213 '[:' { L _ ITopabrack }
214 ':]' { L _ ITcpabrack }
217 '(#' { L _ IToubxparen }
218 '#)' { L _ ITcubxparen }
219 '(|' { L _ IToparenbar }
220 '|)' { L _ ITcparenbar }
223 '`' { L _ ITbackquote }
225 VARID { L _ (ITvarid _) } -- identifiers
226 CONID { L _ (ITconid _) }
227 VARSYM { L _ (ITvarsym _) }
228 CONSYM { L _ (ITconsym _) }
229 QVARID { L _ (ITqvarid _) }
230 QCONID { L _ (ITqconid _) }
231 QVARSYM { L _ (ITqvarsym _) }
232 QCONSYM { L _ (ITqconsym _) }
234 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
235 IPSPLITVARID { L _ (ITsplitipvarid _) } -- GHC extension
237 CHAR { L _ (ITchar _) }
238 STRING { L _ (ITstring _) }
239 INTEGER { L _ (ITinteger _) }
240 RATIONAL { L _ (ITrational _) }
242 PRIMCHAR { L _ (ITprimchar _) }
243 PRIMSTRING { L _ (ITprimstring _) }
244 PRIMINTEGER { L _ (ITprimint _) }
245 PRIMFLOAT { L _ (ITprimfloat _) }
246 PRIMDOUBLE { L _ (ITprimdouble _) }
249 '[|' { L _ ITopenExpQuote }
250 '[p|' { L _ ITopenPatQuote }
251 '[t|' { L _ ITopenTypQuote }
252 '[d|' { L _ ITopenDecQuote }
253 '|]' { L _ ITcloseQuote }
254 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
255 '$(' { L _ ITparenEscape } -- $( exp )
256 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
257 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
259 %monad { P } { >>= } { return }
260 %lexer { lexer } { L _ ITeof }
261 %name parseModule module
262 %name parseStmt maybe_stmt
263 %name parseIdentifier identifier
264 %name parseIface iface
265 %tokentype { Located Token }
268 -----------------------------------------------------------------------------
271 -- The place for module deprecation is really too restrictive, but if it
272 -- was allowed at its natural place just before 'module', we get an ugly
273 -- s/r conflict with the second alternative. Another solution would be the
274 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
275 -- either, and DEPRECATED is only expected to be used by people who really
276 -- know what they are doing. :-)
278 module :: { Located (HsModule RdrName) }
279 : 'module' modid maybemoddeprec maybeexports 'where' body
280 {% fileSrcSpan >>= \ loc ->
281 return (L loc (HsModule (Just (L (getLoc $2)
282 (mkHomeModule (unLoc $2))))
283 $4 (fst $6) (snd $6) $3)) }
284 | missing_module_keyword top close
285 {% fileSrcSpan >>= \ loc ->
286 return (L loc (HsModule Nothing Nothing
287 (fst $2) (snd $2) Nothing)) }
289 missing_module_keyword :: { () }
290 : {- empty -} {% pushCurrentContext }
292 maybemoddeprec :: { Maybe DeprecTxt }
293 : '{-# DEPRECATED' STRING '#-}' { Just (getSTRING $2) }
294 | {- empty -} { Nothing }
296 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
298 | vocurly top close { $2 }
300 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
301 : importdecls { (reverse $1,[]) }
302 | importdecls ';' cvtopdecls { (reverse $1,$3) }
303 | cvtopdecls { ([],$1) }
305 cvtopdecls :: { [LHsDecl RdrName] }
306 : topdecls { cvTopDecls $1 }
308 -----------------------------------------------------------------------------
309 -- Interfaces (.hi-boot files)
311 iface :: { ModIface }
312 : 'module' modid 'where' ifacebody { mkBootIface (unLoc $2) $4 }
314 ifacebody :: { [HsDecl RdrName] }
315 : '{' ifacedecls '}' { $2 }
316 | vocurly ifacedecls close { $2 }
318 ifacedecls :: { [HsDecl RdrName] }
319 : ifacedecl ';' ifacedecls { $1 : $3 }
320 | ';' ifacedecls { $2 }
324 ifacedecl :: { HsDecl RdrName }
327 | 'type' syn_hdr '=' ctype
328 { let (tc,tvs) = $2 in TyClD (TySynonym tc tvs $4) }
330 { TyClD (mkTyData DataType (unLoc $2) [] Nothing) }
332 { TyClD (mkTyData NewType (unLoc $2) [] Nothing) }
333 | 'class' tycl_hdr fds
334 { TyClD (mkClassDecl (unLoc $2) (unLoc $3) [] emptyBag) }
336 -----------------------------------------------------------------------------
339 maybeexports :: { Maybe [LIE RdrName] }
340 : '(' exportlist ')' { Just $2 }
341 | {- empty -} { Nothing }
343 exportlist :: { [LIE RdrName] }
344 : exportlist ',' export { $3 : $1 }
345 | exportlist ',' { $1 }
349 -- No longer allow things like [] and (,,,) to be exported
350 -- They are built in syntax, always available
351 export :: { LIE RdrName }
352 : qvar { L1 (IEVar (unLoc $1)) }
353 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
354 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
355 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
356 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
357 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
359 qcnames :: { [RdrName] }
360 : qcnames ',' qcname { unLoc $3 : $1 }
361 | qcname { [unLoc $1] }
363 qcname :: { Located RdrName } -- Variable or data constructor
367 -----------------------------------------------------------------------------
368 -- Import Declarations
370 -- import decls can be *empty*, or even just a string of semicolons
371 -- whereas topdecls must contain at least one topdecl.
373 importdecls :: { [LImportDecl RdrName] }
374 : importdecls ';' importdecl { $3 : $1 }
375 | importdecls ';' { $1 }
376 | importdecl { [ $1 ] }
379 importdecl :: { LImportDecl RdrName }
380 : 'import' maybe_src optqualified modid maybeas maybeimpspec
381 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
383 maybe_src :: { IsBootInterface }
384 : '{-# SOURCE' '#-}' { True }
385 | {- empty -} { False }
387 optqualified :: { Bool }
388 : 'qualified' { True }
389 | {- empty -} { False }
391 maybeas :: { Located (Maybe ModuleName) }
392 : 'as' modid { LL (Just (unLoc $2)) }
393 | {- empty -} { noLoc Nothing }
395 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
396 : impspec { L1 (Just (unLoc $1)) }
397 | {- empty -} { noLoc Nothing }
399 impspec :: { Located (Bool, [LIE RdrName]) }
400 : '(' exportlist ')' { LL (False, reverse $2) }
401 | 'hiding' '(' exportlist ')' { LL (True, reverse $3) }
403 -----------------------------------------------------------------------------
404 -- Fixity Declarations
408 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
410 infix :: { Located FixityDirection }
411 : 'infix' { L1 InfixN }
412 | 'infixl' { L1 InfixL }
413 | 'infixr' { L1 InfixR }
415 ops :: { Located [Located RdrName] }
416 : ops ',' op { LL ($3 : unLoc $1) }
419 -----------------------------------------------------------------------------
420 -- Top-Level Declarations
422 topdecls :: { [RdrBinding] } -- Reversed
423 : topdecls ';' topdecl { $3 : $1 }
424 | topdecls ';' { $1 }
427 topdecl :: { RdrBinding }
428 : tycl_decl { RdrHsDecl (L1 (TyClD (unLoc $1))) }
429 | 'instance' inst_type where
430 { let (binds,sigs) = cvBindsAndSigs (unLoc $3)
431 in RdrHsDecl (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs))) }
432 | 'default' '(' comma_types0 ')' { RdrHsDecl (LL $ DefD (DefaultDecl $3)) }
433 | 'foreign' fdecl { RdrHsDecl (LL (unLoc $2)) }
434 | '{-# DEPRECATED' deprecations '#-}' { RdrBindings (reverse $2) }
435 | '{-# RULES' rules '#-}' { RdrBindings (reverse $2) }
436 | '$(' exp ')' { RdrHsDecl (LL $ SpliceD (SpliceDecl $2)) }
439 tycl_decl :: { LTyClDecl RdrName }
440 : 'type' syn_hdr '=' ctype
441 -- Note ctype, not sigtype.
442 -- We allow an explicit for-all but we don't insert one
443 -- in type Foo a = (b,b)
444 -- Instead we just say b is out of scope
445 { LL $ let (tc,tvs) = $2 in TySynonym tc tvs $4 }
447 | 'data' tycl_hdr constrs deriving
448 { L (comb4 $1 $2 $3 $4)
449 (mkTyData DataType (unLoc $2) (reverse (unLoc $3)) (unLoc $4)) }
451 | 'newtype' tycl_hdr '=' newconstr deriving
453 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
455 | 'class' tycl_hdr fds where
457 (binds,sigs) = cvBindsAndSigs (unLoc $4)
459 L (comb4 $1 $2 $3 $4) (mkClassDecl (unLoc $2) (unLoc $3) sigs
462 syn_hdr :: { (Located RdrName, [LHsTyVarBndr RdrName]) }
463 -- We don't retain the syntax of an infix
464 -- type synonym declaration. Oh well.
465 : tycon tv_bndrs { ($1, $2) }
466 | tv_bndr tyconop tv_bndr { ($2, [$1,$3]) }
468 -- tycl_hdr parses the header of a type or class decl,
469 -- which takes the form
472 -- (Eq a, Ord b) => T a b
473 -- Rather a lot of inlining here, else we get reduce/reduce errors
474 tycl_hdr :: { Located (LHsContext RdrName, Located RdrName, [LHsTyVarBndr RdrName]) }
475 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
476 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
478 -----------------------------------------------------------------------------
479 -- Nested declarations
481 decls :: { Located [RdrBinding] } -- Reversed
482 : decls ';' decl { LL (unLoc $3 : unLoc $1) }
483 | decls ';' { LL (unLoc $1) }
484 | decl { L1 [unLoc $1] }
485 | {- empty -} { noLoc [] }
488 decllist :: { Located [RdrBinding] } -- Reversed
489 : '{' decls '}' { LL (unLoc $2) }
490 | vocurly decls close { $2 }
492 where :: { Located [RdrBinding] } -- Reversed
493 -- No implicit parameters
494 : 'where' decllist { LL (unLoc $2) }
495 | {- empty -} { noLoc [] }
497 binds :: { Located [HsBindGroup RdrName] } -- May have implicit parameters
498 : decllist { L1 [cvBindGroup (unLoc $1)] }
499 | '{' dbinds '}' { LL [HsIPBinds (unLoc $2)] }
500 | vocurly dbinds close { L (getLoc $2) [HsIPBinds (unLoc $2)] }
502 wherebinds :: { Located [HsBindGroup RdrName] } -- May have implicit parameters
503 : 'where' binds { LL (unLoc $2) }
504 | {- empty -} { noLoc [] }
507 -----------------------------------------------------------------------------
508 -- Transformation Rules
510 rules :: { [RdrBinding] } -- Reversed
511 : rules ';' rule { $3 : $1 }
516 rule :: { RdrBinding }
517 : STRING activation rule_forall infixexp '=' exp
518 { RdrHsDecl (LL $ RuleD (HsRule (getSTRING $1) $2 $3 $4 $6)) }
520 activation :: { Activation } -- Omitted means AlwaysActive
521 : {- empty -} { AlwaysActive }
522 | explicit_activation { $1 }
524 inverse_activation :: { Activation } -- Omitted means NeverActive
525 : {- empty -} { NeverActive }
526 | explicit_activation { $1 }
528 explicit_activation :: { Activation } -- In brackets
529 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
530 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
532 rule_forall :: { [RuleBndr RdrName] }
533 : 'forall' rule_var_list '.' { $2 }
536 rule_var_list :: { [RuleBndr RdrName] }
538 | rule_var rule_var_list { $1 : $2 }
540 rule_var :: { RuleBndr RdrName }
541 : varid { RuleBndr $1 }
542 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
544 -----------------------------------------------------------------------------
545 -- Deprecations (c.f. rules)
547 deprecations :: { [RdrBinding] } -- Reversed
548 : deprecations ';' deprecation { $3 : $1 }
549 | deprecations ';' { $1 }
550 | deprecation { [$1] }
553 -- SUP: TEMPORARY HACK, not checking for `module Foo'
554 deprecation :: { RdrBinding }
556 { RdrBindings [ RdrHsDecl (LL $ DeprecD (Deprecation n (getSTRING $2))) | n <- unLoc $1 ] }
559 -----------------------------------------------------------------------------
560 -- Foreign import and export declarations
562 -- for the time being, the following accepts foreign declarations conforming
563 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
565 -- * a flag indicates whether pre-standard declarations have been used and
566 -- triggers a deprecation warning further down the road
568 -- NB: The first two rules could be combined into one by replacing `safety1'
569 -- with `safety'. However, the combined rule conflicts with the
572 fdecl :: { LHsDecl RdrName }
573 fdecl : 'import' callconv safety1 fspec
574 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
575 | 'import' callconv fspec
576 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
578 | 'export' callconv fspec
579 {% mkExport $2 (unLoc $3) >>= return.LL }
580 -- the following syntax is DEPRECATED
581 | fdecl1DEPRECATED { L1 (ForD (unLoc $1)) }
582 | fdecl2DEPRECATED { L1 (unLoc $1) }
584 fdecl1DEPRECATED :: { LForeignDecl RdrName }
586 ----------- DEPRECATED label decls ------------
587 : 'label' ext_name varid '::' sigtype
588 { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS
589 (CLabel ($2 `orElse` mkExtName (unLoc $3)))) True }
591 ----------- DEPRECATED ccall/stdcall decls ------------
593 -- NB: This business with the case expression below may seem overly
594 -- complicated, but it is necessary to avoid some conflicts.
596 -- DEPRECATED variant #1: lack of a calling convention specification
598 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
600 target = StaticTarget ($2 `orElse` mkExtName (unLoc $4))
602 LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS
603 (CFunction target)) True }
605 -- DEPRECATED variant #2: external name consists of two separate strings
606 -- (module name and function name) (import)
607 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
609 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
610 CCall cconv -> return $
612 imp = CFunction (StaticTarget (getSTRING $4))
614 LL $ ForeignImport $6 $8 (CImport cconv $5 nilFS nilFS imp) True }
616 -- DEPRECATED variant #3: `unsafe' after entity
617 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
619 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
620 CCall cconv -> return $
622 imp = CFunction (StaticTarget (getSTRING $3))
624 LL $ ForeignImport $5 $7 (CImport cconv PlayRisky nilFS nilFS imp) True }
626 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
627 -- an explicit calling convention (import)
628 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
629 { LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS
630 (CFunction DynamicTarget)) True }
632 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
633 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
635 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
636 CCall cconv -> return $
637 LL $ ForeignImport $5 $7 (CImport cconv $4 nilFS nilFS
638 (CFunction DynamicTarget)) True }
640 -- DEPRECATED variant #6: lack of a calling convention specification
642 | 'export' {-no callconv-} ext_name varid '::' sigtype
643 { LL $ ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName (unLoc $3))
644 defaultCCallConv)) True }
646 -- DEPRECATED variant #7: external name consists of two separate strings
647 -- (module name and function name) (export)
648 | 'export' callconv STRING STRING varid '::' sigtype
650 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
651 CCall cconv -> return $
652 LL $ ForeignExport $5 $7
653 (CExport (CExportStatic (getSTRING $4) cconv)) True }
655 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
656 -- an explicit calling convention (export)
657 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
658 { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS
661 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
662 | 'export' callconv 'dynamic' varid '::' sigtype
664 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
665 CCall cconv -> return $
666 LL $ ForeignImport $4 $6
667 (CImport cconv (PlaySafe False) nilFS nilFS CWrapper) True }
669 ----------- DEPRECATED .NET decls ------------
670 -- NB: removed the .NET call declaration, as it is entirely subsumed
671 -- by the new standard FFI declarations
673 fdecl2DEPRECATED :: { LHsDecl RdrName }
675 : 'import' 'dotnet' 'type' ext_name tycon { LL $ TyClD (ForeignType $5 $4 DNType) }
676 -- left this one unchanged for the moment as type imports are not
677 -- covered currently by the FFI standard -=chak
680 callconv :: { CallConv }
681 : 'stdcall' { CCall StdCallConv }
682 | 'ccall' { CCall CCallConv }
683 | 'dotnet' { DNCall }
686 : 'unsafe' { PlayRisky }
687 | 'safe' { PlaySafe False }
688 | 'threadsafe' { PlaySafe True }
689 | {- empty -} { PlaySafe False }
691 safety1 :: { Safety }
692 : 'unsafe' { PlayRisky }
693 | 'safe' { PlaySafe False }
694 | 'threadsafe' { PlaySafe True }
695 -- only needed to avoid conflicts with the DEPRECATED rules
697 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
698 : STRING var '::' sigtype { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
699 | var '::' sigtype { LL (noLoc nilFS, $1, $3) }
700 -- if the entity string is missing, it defaults to the empty string;
701 -- the meaning of an empty entity string depends on the calling
705 ext_name :: { Maybe CLabelString }
706 : STRING { Just (getSTRING $1) }
707 | STRING STRING { Just (getSTRING $2) } -- Ignore "module name" for now
708 | {- empty -} { Nothing }
711 -----------------------------------------------------------------------------
714 opt_sig :: { Maybe (LHsType RdrName) }
715 : {- empty -} { Nothing }
716 | '::' sigtype { Just $2 }
718 opt_asig :: { Maybe (LHsType RdrName) }
719 : {- empty -} { Nothing }
720 | '::' atype { Just $2 }
722 sigtypes :: { [LHsType RdrName] }
724 | sigtypes ',' sigtype { $3 : $1 }
726 sigtype :: { LHsType RdrName }
727 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
728 -- Wrap an Implicit forall if there isn't one there already
730 sig_vars :: { Located [Located RdrName] }
731 : sig_vars ',' var { LL ($3 : unLoc $1) }
734 -----------------------------------------------------------------------------
737 -- A ctype is a for-all type
738 ctype :: { LHsType RdrName }
739 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
740 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
741 -- A type of form (context => type) is an *implicit* HsForAllTy
744 -- We parse a context as a btype so that we don't get reduce/reduce
745 -- errors in ctype. The basic problem is that
747 -- looks so much like a tuple type. We can't tell until we find the =>
748 context :: { LHsContext RdrName }
749 : btype {% checkContext $1 }
751 type :: { LHsType RdrName }
752 : ipvar '::' gentype { LL (HsPredTy (LL $ HsIParam (unLoc $1) $3)) }
755 gentype :: { LHsType RdrName }
757 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
758 | btype '`' tyvar '`' gentype { LL $ HsOpTy $1 $3 $5 }
759 | btype '->' gentype { LL $ HsFunTy $1 $3 }
761 btype :: { LHsType RdrName }
762 : btype atype { LL $ HsAppTy $1 $2 }
765 atype :: { LHsType RdrName }
766 : gtycon { L1 (HsTyVar (unLoc $1)) }
767 | tyvar { L1 (HsTyVar (unLoc $1)) }
768 | '(' type ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
769 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
770 | '[' type ']' { LL $ HsListTy $2 }
771 | '[:' type ':]' { LL $ HsPArrTy $2 }
772 | '(' ctype ')' { LL $ HsParTy $2 }
773 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 $4 }
775 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
777 -- An inst_type is what occurs in the head of an instance decl
778 -- e.g. (Foo a, Gaz b) => Wibble a b
779 -- It's kept as a single type, with a MonoDictTy at the right
780 -- hand corner, for convenience.
781 inst_type :: { LHsType RdrName }
782 : ctype {% checkInstType $1 }
784 comma_types0 :: { [LHsType RdrName] }
785 : comma_types1 { $1 }
788 comma_types1 :: { [LHsType RdrName] }
790 | type ',' comma_types1 { $1 : $3 }
792 tv_bndrs :: { [LHsTyVarBndr RdrName] }
793 : tv_bndr tv_bndrs { $1 : $2 }
796 tv_bndr :: { LHsTyVarBndr RdrName }
797 : tyvar { L1 (UserTyVar (unLoc $1)) }
798 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2) $4) }
800 fds :: { Located [Located ([RdrName], [RdrName])] }
801 : {- empty -} { noLoc [] }
802 | '|' fds1 { LL (reverse (unLoc $2)) }
804 fds1 :: { Located [Located ([RdrName], [RdrName])] }
805 : fds1 ',' fd { LL ($3 : unLoc $1) }
808 fd :: { Located ([RdrName], [RdrName]) }
809 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
810 (reverse (unLoc $1), reverse (unLoc $3)) }
812 varids0 :: { Located [RdrName] }
813 : {- empty -} { noLoc [] }
814 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
816 -----------------------------------------------------------------------------
821 | akind '->' kind { mkArrowKind $1 $3 }
824 : '*' { liftedTypeKind }
825 | '(' kind ')' { $2 }
828 -----------------------------------------------------------------------------
829 -- Datatype declarations
831 newconstr :: { LConDecl RdrName }
832 : conid atype { LL $ ConDecl $1 [] (noLoc [])
833 (PrefixCon [(unbangedType $2)]) }
834 | conid '{' var '::' ctype '}'
835 { LL $ ConDecl $1 [] (noLoc [])
836 (RecCon [($3, (unbangedType $5))]) }
838 constrs :: { Located [LConDecl RdrName] }
839 : {- empty; a GHC extension -} { noLoc [] }
840 | '=' constrs1 { LL (unLoc $2) }
842 constrs1 :: { Located [LConDecl RdrName] }
843 : constrs1 '|' constr { LL ($3 : unLoc $1) }
846 constr :: { LConDecl RdrName }
847 : forall context '=>' constr_stuff
848 { let (con,details) = unLoc $4 in
849 LL (ConDecl con (unLoc $1) $2 details) }
850 | forall constr_stuff
851 { let (con,details) = unLoc $2 in
852 LL (ConDecl con (unLoc $1) (noLoc []) details) }
854 forall :: { Located [LHsTyVarBndr RdrName] }
855 : 'forall' tv_bndrs '.' { LL $2 }
856 | {- empty -} { noLoc [] }
858 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
859 : btype {% mkPrefixCon $1 [] >>= return.LL }
860 | btype bang_atype satypes {% do { r <- mkPrefixCon $1 ($2 : unLoc $3);
861 return (L (comb3 $1 $2 $3) r) } }
862 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
863 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
864 | sbtype conop sbtype { LL ($2, InfixCon $1 $3) }
866 bang_atype :: { LBangType RdrName }
867 : strict_mark atype { LL (BangType (unLoc $1) $2) }
869 satypes :: { Located [LBangType RdrName] }
870 : atype satypes { LL (unbangedType $1 : unLoc $2) }
871 | bang_atype satypes { LL ($1 : unLoc $2) }
872 | {- empty -} { noLoc [] }
874 sbtype :: { LBangType RdrName }
875 : btype { unbangedType $1 }
876 | strict_mark atype { LL (BangType (unLoc $1) $2) }
878 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
879 : fielddecl ',' fielddecls { unLoc $1 : $3 }
880 | fielddecl { [unLoc $1] }
882 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
883 : sig_vars '::' stype { LL (reverse (unLoc $1), $3) }
885 stype :: { LBangType RdrName }
886 : ctype { unbangedType $1 }
887 | strict_mark atype { LL (BangType (unLoc $1) $2) }
889 strict_mark :: { Located HsBang }
890 : '!' { L1 HsStrict }
891 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
893 deriving :: { Located (Maybe (LHsContext RdrName)) }
894 : {- empty -} { noLoc Nothing }
895 | 'deriving' context { LL (Just $2) }
896 -- Glasgow extension: allow partial
897 -- applications in derivings
899 -----------------------------------------------------------------------------
902 {- There's an awkward overlap with a type signature. Consider
903 f :: Int -> Int = ...rhs...
904 Then we can't tell whether it's a type signature or a value
905 definition with a result signature until we see the '='.
906 So we have to inline enough to postpone reductions until we know.
910 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
911 instead of qvar, we get another shift/reduce-conflict. Consider the
914 { (^^) :: Int->Int ; } Type signature; only var allowed
916 { (^^) :: Int->Int = ... ; } Value defn with result signature;
917 qvar allowed (because of instance decls)
919 We can't tell whether to reduce var to qvar until after we've read the signatures.
922 decl :: { Located RdrBinding }
924 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 (unLoc $3);
925 return (LL $ RdrValBinding (LL r)) } }
927 rhs :: { Located (GRHSs RdrName) }
928 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) placeHolderType }
929 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) placeHolderType }
931 gdrhs :: { Located [LGRHS RdrName] }
932 : gdrhs gdrh { LL ($2 : unLoc $1) }
935 gdrh :: { LGRHS RdrName }
936 : '|' quals '=' exp { LL $ GRHS (reverse (L (getLoc $4) (ResultStmt $4) :
939 sigdecl :: { Located RdrBinding }
940 : infixexp '::' sigtype
941 {% do s <- checkValSig $1 $3;
942 return (LL $ RdrHsDecl (LL $ SigD s)) }
943 -- See the above notes for why we need infixexp here
944 | var ',' sig_vars '::' sigtype
945 { LL $ mkSigDecls [ LL $ Sig n $5 | n <- $1 : unLoc $3 ] }
946 | infix prec ops { LL $ mkSigDecls [ LL $ FixSig (FixitySig n (Fixity $2 (unLoc $1)))
948 | '{-# INLINE' activation qvar '#-}'
949 { LL $ RdrHsDecl (LL $ SigD (InlineSig True $3 $2)) }
950 | '{-# NOINLINE' inverse_activation qvar '#-}'
951 { LL $ RdrHsDecl (LL $ SigD (InlineSig False $3 $2)) }
952 | '{-# SPECIALISE' qvar '::' sigtypes '#-}'
953 { LL $ mkSigDecls [ LL $ SpecSig $2 t | t <- $4] }
954 | '{-# SPECIALISE' 'instance' inst_type '#-}'
955 { LL $ RdrHsDecl (LL $ SigD (SpecInstSig $3)) }
957 -----------------------------------------------------------------------------
960 exp :: { LHsExpr RdrName }
961 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
962 | fexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
963 | fexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
964 | fexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
965 | fexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
968 infixexp :: { LHsExpr RdrName }
970 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
972 exp10 :: { LHsExpr RdrName }
973 : '\\' aexp aexps opt_asig '->' exp
974 {% checkPatterns ($2 : reverse $3) >>= \ ps ->
975 return (LL $ HsLam (LL $ Match ps $4
976 (GRHSs (unguardedRHS $6) []
978 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
979 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
980 | 'case' exp 'of' altslist { LL $ HsCase $2 (unLoc $4) }
981 | '-' fexp { LL $ mkHsNegApp $2 }
983 | 'do' stmtlist {% let loc = comb2 $1 $2 in
984 checkDo loc (unLoc $2) >>= \ stmts ->
985 return (L loc (mkHsDo DoExpr stmts)) }
986 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
987 checkMDo loc (unLoc $2) >>= \ stmts ->
988 return (L loc (mkHsDo MDoExpr stmts)) }
990 | scc_annot exp { LL $ if opt_SccProfilingOn
991 then HsSCC (unLoc $1) $2
994 | 'proc' aexp '->' exp
995 {% checkPattern $2 >>= \ p ->
996 return (LL $ HsProc p (LL $ HsCmdTop $4 []
997 placeHolderType undefined)) }
998 -- TODO: is LL right here?
1000 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1001 -- hdaume: core annotation
1004 scc_annot :: { Located FastString }
1005 : '_scc_' STRING { LL $ getSTRING $2 }
1006 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
1008 fexp :: { LHsExpr RdrName }
1009 : fexp aexp { LL $ HsApp $1 $2 }
1012 aexps :: { [LHsExpr RdrName] }
1013 : aexps aexp { $2 : $1 }
1014 | {- empty -} { [] }
1016 aexp :: { LHsExpr RdrName }
1017 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1018 | '~' aexp { LL $ ELazyPat $2 }
1021 aexp1 :: { LHsExpr RdrName }
1022 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1027 -- Here was the syntax for type applications that I was planning
1028 -- but there are difficulties (e.g. what order for type args)
1029 -- so it's not enabled yet.
1030 -- But this case *is* used for the left hand side of a generic definition,
1031 -- which is parsed as an expression before being munged into a pattern
1032 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1033 (sL (getLoc $3) (HsType $3)) }
1035 aexp2 :: { LHsExpr RdrName }
1036 : ipvar { L1 (HsIPVar $! unLoc $1) }
1037 | qcname { L1 (HsVar $! unLoc $1) }
1038 | literal { L1 (HsLit $! unLoc $1) }
1039 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1040 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1041 | '(' exp ')' { LL (HsPar $2) }
1042 | '(' exp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1043 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1044 | '[' list ']' { LL (unLoc $2) }
1045 | '[:' parr ':]' { LL (unLoc $2) }
1046 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1047 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1048 | '_' { L1 EWildPat }
1050 -- MetaHaskell Extension
1051 | TH_ID_SPLICE { L1 $ mkHsSplice
1052 (L1 $ HsVar (mkUnqual varName
1053 (getTH_ID_SPLICE $1))) } -- $x
1054 | '$(' exp ')' { LL $ mkHsSplice $2 } -- $( exp )
1055 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1056 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1057 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1058 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1059 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1060 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1061 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1062 return (LL $ HsBracket (PatBr p)) }
1063 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBr (mkGroup $2)) }
1065 -- arrow notation extension
1066 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1068 cmdargs :: { [LHsCmdTop RdrName] }
1069 : cmdargs acmd { $2 : $1 }
1070 | {- empty -} { [] }
1072 acmd :: { LHsCmdTop RdrName }
1073 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1075 cvtopbody :: { [LHsDecl RdrName] }
1076 : '{' cvtopdecls '}' { $2 }
1077 | vocurly cvtopdecls close { $2 }
1079 texps :: { [LHsExpr RdrName] }
1080 : texps ',' exp { $3 : $1 }
1084 -----------------------------------------------------------------------------
1087 -- The rules below are little bit contorted to keep lexps left-recursive while
1088 -- avoiding another shift/reduce-conflict.
1090 list :: { LHsExpr RdrName }
1091 : exp { L1 $ ExplicitList placeHolderType [$1] }
1092 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1093 | exp '..' { LL $ ArithSeqIn (From $1) }
1094 | exp ',' exp '..' { LL $ ArithSeqIn (FromThen $1 $3) }
1095 | exp '..' exp { LL $ ArithSeqIn (FromTo $1 $3) }
1096 | exp ',' exp '..' exp { LL $ ArithSeqIn (FromThenTo $1 $3 $5) }
1097 | exp pquals { LL $ mkHsDo ListComp
1098 (reverse (L (getLoc $1) (ResultStmt $1) :
1101 lexps :: { Located [LHsExpr RdrName] }
1102 : lexps ',' exp { LL ($3 : unLoc $1) }
1103 | exp ',' exp { LL [$3,$1] }
1105 -----------------------------------------------------------------------------
1106 -- List Comprehensions
1108 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1109 -- or a reversed list of Stmts
1110 : pquals1 { case unLoc $1 of
1112 qss -> L1 [L1 (ParStmt stmtss)]
1114 stmtss = [ (reverse qs, undefined)
1118 pquals1 :: { Located [[LStmt RdrName]] }
1119 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1120 | '|' quals { L (getLoc $2) [unLoc $2] }
1122 quals :: { Located [LStmt RdrName] }
1123 : quals ',' qual { LL ($3 : unLoc $1) }
1126 -----------------------------------------------------------------------------
1127 -- Parallel array expressions
1129 -- The rules below are little bit contorted; see the list case for details.
1130 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1131 -- Moreover, we allow explicit arrays with no element (represented by the nil
1132 -- constructor in the list case).
1134 parr :: { LHsExpr RdrName }
1135 : { noLoc (ExplicitPArr placeHolderType []) }
1136 | exp { L1 $ ExplicitPArr placeHolderType [$1] }
1137 | lexps { L1 $ ExplicitPArr placeHolderType
1138 (reverse (unLoc $1)) }
1139 | exp '..' exp { LL $ PArrSeqIn (FromTo $1 $3) }
1140 | exp ',' exp '..' exp { LL $ PArrSeqIn (FromThenTo $1 $3 $5) }
1141 | exp pquals { LL $ mkHsDo PArrComp
1142 (reverse (L (getLoc $1) (ResultStmt $1) :
1146 -- We are reusing `lexps' and `pquals' from the list case.
1148 -----------------------------------------------------------------------------
1149 -- Case alternatives
1151 altslist :: { Located [LMatch RdrName] }
1152 : '{' alts '}' { LL (reverse (unLoc $2)) }
1153 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1155 alts :: { Located [LMatch RdrName] }
1156 : alts1 { L1 (unLoc $1) }
1157 | ';' alts { LL (unLoc $2) }
1159 alts1 :: { Located [LMatch RdrName] }
1160 : alts1 ';' alt { LL ($3 : unLoc $1) }
1161 | alts1 ';' { LL (unLoc $1) }
1164 alt :: { LMatch RdrName }
1165 : infixexp opt_sig alt_rhs {% checkPattern $1 >>= \p ->
1166 return (LL (Match [p] $2 (unLoc $3))) }
1168 alt_rhs :: { Located (GRHSs RdrName) }
1169 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)
1172 ralt :: { Located [LGRHS RdrName] }
1173 : '->' exp { LL (unguardedRHS $2) }
1174 | gdpats { L1 (reverse (unLoc $1)) }
1176 gdpats :: { Located [LGRHS RdrName] }
1177 : gdpats gdpat { LL ($2 : unLoc $1) }
1180 gdpat :: { LGRHS RdrName }
1181 : '|' quals '->' exp { let r = L (getLoc $4) (ResultStmt $4)
1182 in LL $ GRHS (reverse (r : unLoc $2)) }
1184 -----------------------------------------------------------------------------
1185 -- Statement sequences
1187 stmtlist :: { Located [LStmt RdrName] }
1188 : '{' stmts '}' { LL (unLoc $2) }
1189 | vocurly stmts close { $2 }
1191 -- do { ;; s ; s ; ; s ;; }
1192 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1193 -- here, because we need too much lookahead if we see do { e ; }
1194 -- So we use ExprStmts throughout, and switch the last one over
1195 -- in ParseUtils.checkDo instead
1196 stmts :: { Located [LStmt RdrName] }
1197 : stmt stmts_help { LL ($1 : unLoc $2) }
1198 | ';' stmts { LL (unLoc $2) }
1199 | {- empty -} { noLoc [] }
1201 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1202 : ';' stmts { LL (unLoc $2) }
1203 | {- empty -} { noLoc [] }
1205 -- For typing stmts at the GHCi prompt, where
1206 -- the input may consist of just comments.
1207 maybe_stmt :: { Maybe (LStmt RdrName) }
1209 | {- nothing -} { Nothing }
1211 stmt :: { LStmt RdrName }
1213 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1214 return (LL $ BindStmt p $1) }
1215 | 'rec' stmtlist { LL $ RecStmt (unLoc $2) undefined undefined undefined }
1217 qual :: { LStmt RdrName }
1218 : infixexp '<-' exp {% checkPattern $1 >>= \p ->
1219 return (LL $ BindStmt p $3) }
1220 | exp { L1 $ ExprStmt $1 placeHolderType }
1221 | 'let' binds { LL $ LetStmt (unLoc $2) }
1223 -----------------------------------------------------------------------------
1224 -- Record Field Update/Construction
1226 fbinds :: { HsRecordBinds RdrName }
1228 | {- empty -} { [] }
1230 fbinds1 :: { HsRecordBinds RdrName }
1231 : fbinds1 ',' fbind { $3 : $1 }
1234 fbind :: { (Located RdrName, LHsExpr RdrName) }
1235 : qvar '=' exp { ($1,$3) }
1237 -----------------------------------------------------------------------------
1238 -- Implicit Parameter Bindings
1240 dbinds :: { Located [LIPBind RdrName] }
1241 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1242 | dbinds ';' { LL (unLoc $1) }
1244 -- | {- empty -} { [] }
1246 dbind :: { LIPBind RdrName }
1247 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1249 -----------------------------------------------------------------------------
1250 -- Variables, Constructors and Operators.
1252 identifier :: { Located RdrName }
1258 depreclist :: { Located [RdrName] }
1259 depreclist : deprec_var { L1 [unLoc $1] }
1260 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1262 deprec_var :: { Located RdrName }
1263 deprec_var : var { $1 }
1266 gcon :: { Located RdrName } -- Data constructor namespace
1267 : sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1269 -- the case of '[:' ':]' is part of the production `parr'
1271 sysdcon :: { Located DataCon } -- Wired in data constructors
1272 : '(' ')' { LL unitDataCon }
1273 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1274 | '[' ']' { LL nilDataCon }
1276 var :: { Located RdrName }
1278 | '(' varsym ')' { LL (unLoc $2) }
1280 qvar :: { Located RdrName }
1282 | '(' varsym ')' { LL (unLoc $2) }
1283 | '(' qvarsym1 ')' { LL (unLoc $2) }
1284 -- We've inlined qvarsym here so that the decision about
1285 -- whether it's a qvar or a var can be postponed until
1286 -- *after* we see the close paren.
1288 ipvar :: { Located (IPName RdrName) }
1289 : IPDUPVARID { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1290 | IPSPLITVARID { L1 (Linear (mkUnqual varName (getIPSPLITVARID $1))) }
1292 qcon :: { Located RdrName }
1294 | '(' qconsym ')' { LL (unLoc $2) }
1296 varop :: { Located RdrName }
1298 | '`' varid '`' { LL (unLoc $2) }
1300 qvarop :: { Located RdrName }
1302 | '`' qvarid '`' { LL (unLoc $2) }
1304 qvaropm :: { Located RdrName }
1305 : qvarsym_no_minus { $1 }
1306 | '`' qvarid '`' { LL (unLoc $2) }
1308 conop :: { Located RdrName }
1310 | '`' conid '`' { LL (unLoc $2) }
1312 qconop :: { Located RdrName }
1314 | '`' qconid '`' { LL (unLoc $2) }
1316 -----------------------------------------------------------------------------
1317 -- Type constructors
1319 gtycon :: { Located RdrName } -- A "general" qualified tycon
1321 | '(' ')' { LL $ getRdrName unitTyCon }
1322 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1323 | '(' '->' ')' { LL $ getRdrName funTyCon }
1324 | '[' ']' { LL $ listTyCon_RDR }
1325 | '[:' ':]' { LL $ parrTyCon_RDR }
1327 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1329 | '(' qtyconsym ')' { LL (unLoc $2) }
1331 qtyconop :: { Located RdrName } -- Qualified or unqualified
1333 | '`' qtycon '`' { LL (unLoc $2) }
1335 tyconop :: { Located RdrName } -- Unqualified
1337 | '`' tycon '`' { LL (unLoc $2) }
1339 qtycon :: { Located RdrName } -- Qualified or unqualified
1340 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1343 tycon :: { Located RdrName } -- Unqualified
1344 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1346 qtyconsym :: { Located RdrName }
1347 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1350 tyconsym :: { Located RdrName }
1351 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1353 -----------------------------------------------------------------------------
1356 op :: { Located RdrName } -- used in infix decls
1360 qop :: { LHsExpr RdrName } -- used in sections
1361 : qvarop { L1 $ HsVar (unLoc $1) }
1362 | qconop { L1 $ HsVar (unLoc $1) }
1364 qopm :: { LHsExpr RdrName } -- used in sections
1365 : qvaropm { L1 $ HsVar (unLoc $1) }
1366 | qconop { L1 $ HsVar (unLoc $1) }
1368 -----------------------------------------------------------------------------
1371 qvarid :: { Located RdrName }
1373 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1375 varid :: { Located RdrName }
1376 : varid_no_unsafe { $1 }
1377 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1378 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1379 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1381 varid_no_unsafe :: { Located RdrName }
1382 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1383 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1384 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1386 tyvar :: { Located RdrName }
1387 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1388 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1389 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1390 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1391 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1393 -- These special_ids are treated as keywords in various places,
1394 -- but as ordinary ids elsewhere. 'special_id' collects all these
1395 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1396 special_id :: { Located UserFS }
1398 : 'as' { L1 FSLIT("as") }
1399 | 'qualified' { L1 FSLIT("qualified") }
1400 | 'hiding' { L1 FSLIT("hiding") }
1401 | 'export' { L1 FSLIT("export") }
1402 | 'label' { L1 FSLIT("label") }
1403 | 'dynamic' { L1 FSLIT("dynamic") }
1404 | 'stdcall' { L1 FSLIT("stdcall") }
1405 | 'ccall' { L1 FSLIT("ccall") }
1407 -----------------------------------------------------------------------------
1410 qvarsym :: { Located RdrName }
1414 qvarsym_no_minus :: { Located RdrName }
1415 : varsym_no_minus { $1 }
1418 qvarsym1 :: { Located RdrName }
1419 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1421 varsym :: { Located RdrName }
1422 : varsym_no_minus { $1 }
1423 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1425 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1426 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1427 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1430 -- See comments with special_id
1431 special_sym :: { Located UserFS }
1432 special_sym : '!' { L1 FSLIT("!") }
1433 | '.' { L1 FSLIT(".") }
1434 | '*' { L1 FSLIT("*") }
1436 -----------------------------------------------------------------------------
1437 -- Data constructors
1439 qconid :: { Located RdrName } -- Qualified or unqualifiedb
1441 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1443 conid :: { Located RdrName }
1444 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1446 qconsym :: { Located RdrName } -- Qualified or unqualified
1448 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1450 consym :: { Located RdrName }
1451 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1453 -- ':' means only list cons
1454 | ':' { L1 $ consDataCon_RDR }
1457 -----------------------------------------------------------------------------
1460 literal :: { Located HsLit }
1461 : CHAR { L1 $ HsChar $ getCHAR $1 }
1462 | STRING { L1 $ HsString $ getSTRING $1 }
1463 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1464 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1465 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1466 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1467 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1469 -----------------------------------------------------------------------------
1473 : vccurly { () } -- context popped in lexer.
1474 | error {% popContext }
1476 -----------------------------------------------------------------------------
1477 -- Miscellaneous (mostly renamings)
1479 modid :: { Located ModuleName }
1480 : CONID { L1 $ mkModuleNameFS (getCONID $1) }
1481 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1484 (unpackFS mod ++ '.':unpackFS c))
1488 : commas ',' { $1 + 1 }
1491 -----------------------------------------------------------------------------
1495 happyError = srcParseFail
1497 getVARID (L _ (ITvarid x)) = x
1498 getCONID (L _ (ITconid x)) = x
1499 getVARSYM (L _ (ITvarsym x)) = x
1500 getCONSYM (L _ (ITconsym x)) = x
1501 getQVARID (L _ (ITqvarid x)) = x
1502 getQCONID (L _ (ITqconid x)) = x
1503 getQVARSYM (L _ (ITqvarsym x)) = x
1504 getQCONSYM (L _ (ITqconsym x)) = x
1505 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1506 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1507 getCHAR (L _ (ITchar x)) = x
1508 getSTRING (L _ (ITstring x)) = x
1509 getINTEGER (L _ (ITinteger x)) = x
1510 getRATIONAL (L _ (ITrational x)) = x
1511 getPRIMCHAR (L _ (ITprimchar x)) = x
1512 getPRIMSTRING (L _ (ITprimstring x)) = x
1513 getPRIMINTEGER (L _ (ITprimint x)) = x
1514 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1515 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1516 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1518 -- Utilities for combining source spans
1519 comb2 :: Located a -> Located b -> SrcSpan
1522 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1523 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1525 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1526 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1527 combineSrcSpans (getLoc c) (getLoc d)
1529 -- strict constructor version:
1531 sL :: SrcSpan -> a -> Located a
1532 sL span a = span `seq` L span a
1534 -- Make a source location that is just the filename. This seems slightly
1535 -- neater than trying to construct the span of the text within the file.
1536 fileSrcSpan :: P SrcSpan
1537 fileSrcSpan = do l <- getSrcLoc; return (mkGeneralSrcSpan (srcLocFile l))