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, parseType,
14 #define INCLUDE #include
15 INCLUDE "HsVersions.h"
19 import HscTypes ( IsBootInterface, DeprecTxt )
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
28 import OccName ( varName, dataName, tcClsName, tvName )
29 import DataCon ( DataCon, dataConName )
30 import SrcLoc ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
31 SrcSpan, combineLocs, srcLocFile,
34 import StaticFlags ( opt_SccProfilingOn )
35 import Type ( Kind, mkArrowKind, liftedTypeKind )
36 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
37 Activation(..), defaultInlineSpec )
41 import Maybes ( orElse )
47 -----------------------------------------------------------------------------
48 Conflicts: 36 shift/reduce (1.25)
50 10 for abiguity in 'if x then y else z + 1' [State 178]
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 :: T' [State 178]
55 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
57 4 for ambiguity in 'if x then y else z -< e' [State 178]
58 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
59 There are four such operators: -<, >-, -<<, >>-
62 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
63 Which of these two is intended?
65 (x::T) -> T -- Rhs is T
68 (x::T -> T) -> .. -- Rhs is ...
70 10 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
73 As well as `b` we can have !, VARSYM, QCONSYM, and CONSYM, hence 5 cases
74 Same duplication between states 11 and 253 as the previous case
76 1 for ambiguity in 'let ?x ...' [State 329]
77 the parser can't tell whether the ?x is the lhs of a normal binding or
78 an implicit binding. Fortunately resolving as shift gives it the only
79 sensible meaning, namely the lhs of an implicit binding.
81 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
82 we don't know whether the '[' starts the activation or not: it
83 might be the start of the declaration with the activation being
86 6 for conflicts between `fdecl' and `fdeclDEPRECATED', [States 393,394]
87 which are resolved correctly, and moreover,
88 should go away when `fdeclDEPRECATED' is removed.
90 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
91 since 'forall' is a valid variable name, we don't know whether
92 to treat a forall on the input as the beginning of a quantifier
93 or the beginning of the rule itself. Resolving to shift means
94 it's always treated as a quantifier, hence the above is disallowed.
95 This saves explicitly defining a grammar for the rule lhs that
96 doesn't include 'forall'.
98 -- ---------------------------------------------------------------------------
99 -- Adding location info
101 This is done in a stylised way using the three macros below, L0, L1
102 and LL. Each of these macros can be thought of as having type
104 L0, L1, LL :: a -> Located a
106 They each add a SrcSpan to their argument.
108 L0 adds 'noSrcSpan', used for empty productions
110 L1 for a production with a single token on the lhs. Grabs the SrcSpan
113 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
114 the first and last tokens.
116 These suffice for the majority of cases. However, we must be
117 especially careful with empty productions: LL won't work if the first
118 or last token on the lhs can represent an empty span. In these cases,
119 we have to calculate the span using more of the tokens from the lhs, eg.
121 | 'newtype' tycl_hdr '=' newconstr deriving
123 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
125 We provide comb3 and comb4 functions which are useful in such cases.
127 Be careful: there's no checking that you actually got this right, the
128 only symptom will be that the SrcSpans of your syntax will be
132 * We must expand these macros *before* running Happy, which is why this file is
133 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
135 #define L0 L noSrcSpan
136 #define L1 sL (getLoc $1)
137 #define LL sL (comb2 $1 $>)
139 -- -----------------------------------------------------------------------------
144 '_' { L _ ITunderscore } -- Haskell keywords
146 'case' { L _ ITcase }
147 'class' { L _ ITclass }
148 'data' { L _ ITdata }
149 'default' { L _ ITdefault }
150 'deriving' { L _ ITderiving }
152 'else' { L _ ITelse }
153 'hiding' { L _ IThiding }
155 'import' { L _ ITimport }
157 'infix' { L _ ITinfix }
158 'infixl' { L _ ITinfixl }
159 'infixr' { L _ ITinfixr }
160 'instance' { L _ ITinstance }
162 'module' { L _ ITmodule }
163 'newtype' { L _ ITnewtype }
165 'qualified' { L _ ITqualified }
166 'then' { L _ ITthen }
167 'type' { L _ ITtype }
168 'where' { L _ ITwhere }
169 '_scc_' { L _ ITscc } -- ToDo: remove
171 'forall' { L _ ITforall } -- GHC extension keywords
172 'foreign' { L _ ITforeign }
173 'export' { L _ ITexport }
174 'label' { L _ ITlabel }
175 'dynamic' { L _ ITdynamic }
176 'safe' { L _ ITsafe }
177 'threadsafe' { L _ ITthreadsafe }
178 'unsafe' { L _ ITunsafe }
180 'stdcall' { L _ ITstdcallconv }
181 'ccall' { L _ ITccallconv }
182 'dotnet' { L _ ITdotnet }
183 'proc' { L _ ITproc } -- for arrow notation extension
184 'rec' { L _ ITrec } -- for arrow notation extension
186 '{-# INLINE' { L _ (ITinline_prag _) }
187 '{-# SPECIALISE' { L _ ITspec_prag }
188 '{-# SPECIALISE_INLINE' { L _ (ITspec_inline_prag _) }
189 '{-# SOURCE' { L _ ITsource_prag }
190 '{-# RULES' { L _ ITrules_prag }
191 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
192 '{-# SCC' { L _ ITscc_prag }
193 '{-# DEPRECATED' { L _ ITdeprecated_prag }
194 '{-# UNPACK' { L _ ITunpack_prag }
195 '#-}' { L _ ITclose_prag }
197 '..' { L _ ITdotdot } -- reserved symbols
199 '::' { L _ ITdcolon }
203 '<-' { L _ ITlarrow }
204 '->' { L _ ITrarrow }
207 '=>' { L _ ITdarrow }
211 '-<' { L _ ITlarrowtail } -- for arrow notation
212 '>-' { L _ ITrarrowtail } -- for arrow notation
213 '-<<' { L _ ITLarrowtail } -- for arrow notation
214 '>>-' { L _ ITRarrowtail } -- for arrow notation
217 '{' { L _ ITocurly } -- special symbols
219 '{|' { L _ ITocurlybar }
220 '|}' { L _ ITccurlybar }
221 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
222 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
225 '[:' { L _ ITopabrack }
226 ':]' { L _ ITcpabrack }
229 '(#' { L _ IToubxparen }
230 '#)' { L _ ITcubxparen }
231 '(|' { L _ IToparenbar }
232 '|)' { L _ ITcparenbar }
235 '`' { L _ ITbackquote }
237 VARID { L _ (ITvarid _) } -- identifiers
238 CONID { L _ (ITconid _) }
239 VARSYM { L _ (ITvarsym _) }
240 CONSYM { L _ (ITconsym _) }
241 QVARID { L _ (ITqvarid _) }
242 QCONID { L _ (ITqconid _) }
243 QVARSYM { L _ (ITqvarsym _) }
244 QCONSYM { L _ (ITqconsym _) }
246 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
247 IPSPLITVARID { L _ (ITsplitipvarid _) } -- GHC extension
249 CHAR { L _ (ITchar _) }
250 STRING { L _ (ITstring _) }
251 INTEGER { L _ (ITinteger _) }
252 RATIONAL { L _ (ITrational _) }
254 PRIMCHAR { L _ (ITprimchar _) }
255 PRIMSTRING { L _ (ITprimstring _) }
256 PRIMINTEGER { L _ (ITprimint _) }
257 PRIMFLOAT { L _ (ITprimfloat _) }
258 PRIMDOUBLE { L _ (ITprimdouble _) }
261 '[|' { L _ ITopenExpQuote }
262 '[p|' { L _ ITopenPatQuote }
263 '[t|' { L _ ITopenTypQuote }
264 '[d|' { L _ ITopenDecQuote }
265 '|]' { L _ ITcloseQuote }
266 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
267 '$(' { L _ ITparenEscape } -- $( exp )
268 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
269 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
271 %monad { P } { >>= } { return }
272 %lexer { lexer } { L _ ITeof }
273 %name parseModule module
274 %name parseStmt maybe_stmt
275 %name parseIdentifier identifier
276 %name parseType ctype
277 %partial parseHeader header
278 %tokentype { (Located Token) }
281 -----------------------------------------------------------------------------
282 -- Identifiers; one of the entry points
283 identifier :: { Located RdrName }
289 -----------------------------------------------------------------------------
292 -- The place for module deprecation is really too restrictive, but if it
293 -- was allowed at its natural place just before 'module', we get an ugly
294 -- s/r conflict with the second alternative. Another solution would be the
295 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
296 -- either, and DEPRECATED is only expected to be used by people who really
297 -- know what they are doing. :-)
299 module :: { Located (HsModule RdrName) }
300 : 'module' modid maybemoddeprec maybeexports 'where' body
301 {% fileSrcSpan >>= \ loc ->
302 return (L loc (HsModule (Just $2) $4 (fst $6) (snd $6) $3)) }
303 | missing_module_keyword top close
304 {% fileSrcSpan >>= \ loc ->
305 return (L loc (HsModule Nothing Nothing
306 (fst $2) (snd $2) Nothing)) }
308 missing_module_keyword :: { () }
309 : {- empty -} {% pushCurrentContext }
311 maybemoddeprec :: { Maybe DeprecTxt }
312 : '{-# DEPRECATED' STRING '#-}' { Just (getSTRING $2) }
313 | {- empty -} { Nothing }
315 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
317 | vocurly top close { $2 }
319 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
320 : importdecls { (reverse $1,[]) }
321 | importdecls ';' cvtopdecls { (reverse $1,$3) }
322 | cvtopdecls { ([],$1) }
324 cvtopdecls :: { [LHsDecl RdrName] }
325 : topdecls { cvTopDecls $1 }
327 -----------------------------------------------------------------------------
328 -- Module declaration & imports only
330 header :: { Located (HsModule RdrName) }
331 : 'module' modid maybemoddeprec maybeexports 'where' header_body
332 {% fileSrcSpan >>= \ loc ->
333 return (L loc (HsModule (Just $2) $4 $6 [] $3)) }
334 | missing_module_keyword importdecls
335 {% fileSrcSpan >>= \ loc ->
336 return (L loc (HsModule Nothing Nothing $2 [] Nothing)) }
338 header_body :: { [LImportDecl RdrName] }
339 : '{' importdecls { $2 }
340 | vocurly importdecls { $2 }
342 -----------------------------------------------------------------------------
345 maybeexports :: { Maybe [LIE RdrName] }
346 : '(' exportlist ')' { Just $2 }
347 | {- empty -} { Nothing }
349 exportlist :: { [LIE RdrName] }
350 : exportlist ',' export { $3 : $1 }
351 | exportlist ',' { $1 }
355 -- No longer allow things like [] and (,,,) to be exported
356 -- They are built in syntax, always available
357 export :: { LIE RdrName }
358 : qvar { L1 (IEVar (unLoc $1)) }
359 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
360 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
361 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
362 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
363 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
365 qcnames :: { [RdrName] }
366 : qcnames ',' qcname { unLoc $3 : $1 }
367 | qcname { [unLoc $1] }
369 qcname :: { Located RdrName } -- Variable or data constructor
373 -----------------------------------------------------------------------------
374 -- Import Declarations
376 -- import decls can be *empty*, or even just a string of semicolons
377 -- whereas topdecls must contain at least one topdecl.
379 importdecls :: { [LImportDecl RdrName] }
380 : importdecls ';' importdecl { $3 : $1 }
381 | importdecls ';' { $1 }
382 | importdecl { [ $1 ] }
385 importdecl :: { LImportDecl RdrName }
386 : 'import' maybe_src optqualified modid maybeas maybeimpspec
387 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
389 maybe_src :: { IsBootInterface }
390 : '{-# SOURCE' '#-}' { True }
391 | {- empty -} { False }
393 optqualified :: { Bool }
394 : 'qualified' { True }
395 | {- empty -} { False }
397 maybeas :: { Located (Maybe Module) }
398 : 'as' modid { LL (Just (unLoc $2)) }
399 | {- empty -} { noLoc Nothing }
401 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
402 : impspec { L1 (Just (unLoc $1)) }
403 | {- empty -} { noLoc Nothing }
405 impspec :: { Located (Bool, [LIE RdrName]) }
406 : '(' exportlist ')' { LL (False, reverse $2) }
407 | 'hiding' '(' exportlist ')' { LL (True, reverse $3) }
409 -----------------------------------------------------------------------------
410 -- Fixity Declarations
414 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
416 infix :: { Located FixityDirection }
417 : 'infix' { L1 InfixN }
418 | 'infixl' { L1 InfixL }
419 | 'infixr' { L1 InfixR }
421 ops :: { Located [Located RdrName] }
422 : ops ',' op { LL ($3 : unLoc $1) }
425 -----------------------------------------------------------------------------
426 -- Top-Level Declarations
428 topdecls :: { OrdList (LHsDecl RdrName) } -- Reversed
429 : topdecls ';' topdecl { $1 `appOL` $3 }
430 | topdecls ';' { $1 }
433 topdecl :: { OrdList (LHsDecl RdrName) }
434 : tycl_decl { unitOL (L1 (TyClD (unLoc $1))) }
435 | 'instance' inst_type where
436 { let (binds,sigs) = cvBindsAndSigs (unLoc $3)
437 in unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs))) }
438 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
439 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
440 | '{-# DEPRECATED' deprecations '#-}' { $2 }
441 | '{-# RULES' rules '#-}' { $2 }
444 -- Template Haskell Extension
445 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
446 | TH_ID_SPLICE { unitOL (LL $ SpliceD (SpliceDecl $
447 L1 $ HsVar (mkUnqual varName (getTH_ID_SPLICE $1))
450 tycl_decl :: { LTyClDecl RdrName }
451 : 'type' type '=' ctype
452 -- Note type on the left of the '='; this allows
453 -- infix type constructors to be declared
455 -- Note ctype, not sigtype, on the right
456 -- We allow an explicit for-all but we don't insert one
457 -- in type Foo a = (b,b)
458 -- Instead we just say b is out of scope
459 {% do { (tc,tvs) <- checkSynHdr $2
460 ; return (LL (TySynonym tc tvs $4)) } }
462 | data_or_newtype tycl_hdr constrs deriving
463 { L (comb4 $1 $2 $3 $4) -- We need the location on tycl_hdr
464 -- in case constrs and deriving are both empty
465 (mkTyData (unLoc $1) (unLoc $2) Nothing (reverse (unLoc $3)) (unLoc $4)) }
467 | data_or_newtype tycl_hdr opt_kind_sig
468 'where' gadt_constrlist
470 { L (comb4 $1 $2 $4 $5)
471 (mkTyData (unLoc $1) (unLoc $2) $3 (reverse (unLoc $5)) (unLoc $6)) }
473 | 'class' tycl_hdr fds where
475 (binds,sigs) = cvBindsAndSigs (unLoc $4)
477 L (comb4 $1 $2 $3 $4) (mkClassDecl (unLoc $2) (unLoc $3) sigs
480 data_or_newtype :: { Located NewOrData }
481 : 'data' { L1 DataType }
482 | 'newtype' { L1 NewType }
484 opt_kind_sig :: { Maybe Kind }
486 | '::' kind { Just $2 }
488 -- tycl_hdr parses the header of a type or class decl,
489 -- which takes the form
492 -- (Eq a, Ord b) => T a b
493 -- Rather a lot of inlining here, else we get reduce/reduce errors
494 tycl_hdr :: { Located (LHsContext RdrName, Located RdrName, [LHsTyVarBndr RdrName]) }
495 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
496 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
498 -----------------------------------------------------------------------------
499 -- Nested declarations
501 decls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
502 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
503 | decls ';' { LL (unLoc $1) }
505 | {- empty -} { noLoc nilOL }
508 decllist :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
509 : '{' decls '}' { LL (unLoc $2) }
510 | vocurly decls close { $2 }
512 where :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
513 -- No implicit parameters
514 : 'where' decllist { LL (unLoc $2) }
515 | {- empty -} { noLoc nilOL }
517 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
518 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
519 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
520 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
522 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
523 : 'where' binds { LL (unLoc $2) }
524 | {- empty -} { noLoc emptyLocalBinds }
527 -----------------------------------------------------------------------------
528 -- Transformation Rules
530 rules :: { OrdList (LHsDecl RdrName) } -- Reversed
531 : rules ';' rule { $1 `snocOL` $3 }
534 | {- empty -} { nilOL }
536 rule :: { LHsDecl RdrName }
537 : STRING activation rule_forall infixexp '=' exp
538 { LL $ RuleD (HsRule (getSTRING $1)
539 ($2 `orElse` AlwaysActive)
540 $3 $4 placeHolderNames $6 placeHolderNames) }
542 activation :: { Maybe Activation }
543 : {- empty -} { Nothing }
544 | explicit_activation { Just $1 }
546 explicit_activation :: { Activation } -- In brackets
547 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
548 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
550 rule_forall :: { [RuleBndr RdrName] }
551 : 'forall' rule_var_list '.' { $2 }
554 rule_var_list :: { [RuleBndr RdrName] }
556 | rule_var rule_var_list { $1 : $2 }
558 rule_var :: { RuleBndr RdrName }
559 : varid { RuleBndr $1 }
560 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
562 -----------------------------------------------------------------------------
563 -- Deprecations (c.f. rules)
565 deprecations :: { OrdList (LHsDecl RdrName) } -- Reversed
566 : deprecations ';' deprecation { $1 `appOL` $3 }
567 | deprecations ';' { $1 }
569 | {- empty -} { nilOL }
571 -- SUP: TEMPORARY HACK, not checking for `module Foo'
572 deprecation :: { OrdList (LHsDecl RdrName) }
574 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
578 -----------------------------------------------------------------------------
579 -- Foreign import and export declarations
581 -- for the time being, the following accepts foreign declarations conforming
582 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
584 -- * a flag indicates whether pre-standard declarations have been used and
585 -- triggers a deprecation warning further down the road
587 -- NB: The first two rules could be combined into one by replacing `safety1'
588 -- with `safety'. However, the combined rule conflicts with the
591 fdecl :: { LHsDecl RdrName }
592 fdecl : 'import' callconv safety1 fspec
593 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
594 | 'import' callconv fspec
595 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
597 | 'export' callconv fspec
598 {% mkExport $2 (unLoc $3) >>= return.LL }
599 -- the following syntax is DEPRECATED
600 | fdecl1DEPRECATED { L1 (ForD (unLoc $1)) }
601 | fdecl2DEPRECATED { L1 (unLoc $1) }
603 fdecl1DEPRECATED :: { LForeignDecl RdrName }
605 ----------- DEPRECATED label decls ------------
606 : 'label' ext_name varid '::' sigtype
607 { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS
608 (CLabel ($2 `orElse` mkExtName (unLoc $3)))) True }
610 ----------- DEPRECATED ccall/stdcall decls ------------
612 -- NB: This business with the case expression below may seem overly
613 -- complicated, but it is necessary to avoid some conflicts.
615 -- DEPRECATED variant #1: lack of a calling convention specification
617 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
619 target = StaticTarget ($2 `orElse` mkExtName (unLoc $4))
621 LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS
622 (CFunction target)) True }
624 -- DEPRECATED variant #2: external name consists of two separate strings
625 -- (module name and function name) (import)
626 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
628 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
629 CCall cconv -> return $
631 imp = CFunction (StaticTarget (getSTRING $4))
633 LL $ ForeignImport $6 $8 (CImport cconv $5 nilFS nilFS imp) True }
635 -- DEPRECATED variant #3: `unsafe' after entity
636 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
638 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
639 CCall cconv -> return $
641 imp = CFunction (StaticTarget (getSTRING $3))
643 LL $ ForeignImport $5 $7 (CImport cconv PlayRisky nilFS nilFS imp) True }
645 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
646 -- an explicit calling convention (import)
647 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
648 { LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS
649 (CFunction DynamicTarget)) True }
651 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
652 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
654 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
655 CCall cconv -> return $
656 LL $ ForeignImport $5 $7 (CImport cconv $4 nilFS nilFS
657 (CFunction DynamicTarget)) True }
659 -- DEPRECATED variant #6: lack of a calling convention specification
661 | 'export' {-no callconv-} ext_name varid '::' sigtype
662 { LL $ ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName (unLoc $3))
663 defaultCCallConv)) True }
665 -- DEPRECATED variant #7: external name consists of two separate strings
666 -- (module name and function name) (export)
667 | 'export' callconv STRING STRING varid '::' sigtype
669 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
670 CCall cconv -> return $
671 LL $ ForeignExport $5 $7
672 (CExport (CExportStatic (getSTRING $4) cconv)) True }
674 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
675 -- an explicit calling convention (export)
676 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
677 { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS
680 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
681 | 'export' callconv 'dynamic' varid '::' sigtype
683 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
684 CCall cconv -> return $
685 LL $ ForeignImport $4 $6
686 (CImport cconv (PlaySafe False) nilFS nilFS CWrapper) True }
688 ----------- DEPRECATED .NET decls ------------
689 -- NB: removed the .NET call declaration, as it is entirely subsumed
690 -- by the new standard FFI declarations
692 fdecl2DEPRECATED :: { LHsDecl RdrName }
694 : 'import' 'dotnet' 'type' ext_name tycon { LL $ TyClD (ForeignType $5 $4 DNType) }
695 -- left this one unchanged for the moment as type imports are not
696 -- covered currently by the FFI standard -=chak
699 callconv :: { CallConv }
700 : 'stdcall' { CCall StdCallConv }
701 | 'ccall' { CCall CCallConv }
702 | 'dotnet' { DNCall }
705 : 'unsafe' { PlayRisky }
706 | 'safe' { PlaySafe False }
707 | 'threadsafe' { PlaySafe True }
708 | {- empty -} { PlaySafe False }
710 safety1 :: { Safety }
711 : 'unsafe' { PlayRisky }
712 | 'safe' { PlaySafe False }
713 | 'threadsafe' { PlaySafe True }
714 -- only needed to avoid conflicts with the DEPRECATED rules
716 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
717 : STRING var '::' sigtype { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
718 | var '::' sigtype { LL (noLoc nilFS, $1, $3) }
719 -- if the entity string is missing, it defaults to the empty string;
720 -- the meaning of an empty entity string depends on the calling
724 ext_name :: { Maybe CLabelString }
725 : STRING { Just (getSTRING $1) }
726 | STRING STRING { Just (getSTRING $2) } -- Ignore "module name" for now
727 | {- empty -} { Nothing }
730 -----------------------------------------------------------------------------
733 opt_sig :: { Maybe (LHsType RdrName) }
734 : {- empty -} { Nothing }
735 | '::' sigtype { Just $2 }
737 opt_asig :: { Maybe (LHsType RdrName) }
738 : {- empty -} { Nothing }
739 | '::' atype { Just $2 }
741 sigtypes1 :: { [LHsType RdrName] }
743 | sigtype ',' sigtypes1 { $1 : $3 }
745 sigtype :: { LHsType RdrName }
746 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
747 -- Wrap an Implicit forall if there isn't one there already
749 sig_vars :: { Located [Located RdrName] }
750 : sig_vars ',' var { LL ($3 : unLoc $1) }
753 -----------------------------------------------------------------------------
756 strict_mark :: { Located HsBang }
757 : '!' { L1 HsStrict }
758 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
760 -- A ctype is a for-all type
761 ctype :: { LHsType RdrName }
762 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
763 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
764 -- A type of form (context => type) is an *implicit* HsForAllTy
767 -- We parse a context as a btype so that we don't get reduce/reduce
768 -- errors in ctype. The basic problem is that
770 -- looks so much like a tuple type. We can't tell until we find the =>
771 context :: { LHsContext RdrName }
772 : btype {% checkContext $1 }
774 type :: { LHsType RdrName }
775 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
778 gentype :: { LHsType RdrName }
780 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
781 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
782 | btype '->' ctype { LL $ HsFunTy $1 $3 }
784 btype :: { LHsType RdrName }
785 : btype atype { LL $ HsAppTy $1 $2 }
788 atype :: { LHsType RdrName }
789 : gtycon { L1 (HsTyVar (unLoc $1)) }
790 | tyvar { L1 (HsTyVar (unLoc $1)) }
791 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
792 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
793 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
794 | '[' ctype ']' { LL $ HsListTy $2 }
795 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
796 | '(' ctype ')' { LL $ HsParTy $2 }
797 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 $4 }
799 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
801 -- An inst_type is what occurs in the head of an instance decl
802 -- e.g. (Foo a, Gaz b) => Wibble a b
803 -- It's kept as a single type, with a MonoDictTy at the right
804 -- hand corner, for convenience.
805 inst_type :: { LHsType RdrName }
806 : sigtype {% checkInstType $1 }
808 inst_types1 :: { [LHsType RdrName] }
810 | inst_type ',' inst_types1 { $1 : $3 }
812 comma_types0 :: { [LHsType RdrName] }
813 : comma_types1 { $1 }
816 comma_types1 :: { [LHsType RdrName] }
818 | ctype ',' comma_types1 { $1 : $3 }
820 tv_bndrs :: { [LHsTyVarBndr RdrName] }
821 : tv_bndr tv_bndrs { $1 : $2 }
824 tv_bndr :: { LHsTyVarBndr RdrName }
825 : tyvar { L1 (UserTyVar (unLoc $1)) }
826 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2) $4) }
828 fds :: { Located [Located ([RdrName], [RdrName])] }
829 : {- empty -} { noLoc [] }
830 | '|' fds1 { LL (reverse (unLoc $2)) }
832 fds1 :: { Located [Located ([RdrName], [RdrName])] }
833 : fds1 ',' fd { LL ($3 : unLoc $1) }
836 fd :: { Located ([RdrName], [RdrName]) }
837 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
838 (reverse (unLoc $1), reverse (unLoc $3)) }
840 varids0 :: { Located [RdrName] }
841 : {- empty -} { noLoc [] }
842 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
844 -----------------------------------------------------------------------------
849 | akind '->' kind { mkArrowKind $1 $3 }
852 : '*' { liftedTypeKind }
853 | '(' kind ')' { $2 }
856 -----------------------------------------------------------------------------
857 -- Datatype declarations
859 gadt_constrlist :: { Located [LConDecl RdrName] }
860 : '{' gadt_constrs '}' { LL (unLoc $2) }
861 | vocurly gadt_constrs close { $2 }
863 gadt_constrs :: { Located [LConDecl RdrName] }
864 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
865 | gadt_constrs ';' { $1 }
866 | gadt_constr { L1 [$1] }
868 -- We allow the following forms:
869 -- C :: Eq a => a -> T a
870 -- C :: forall a. Eq a => !a -> T a
871 -- D { x,y :: a } :: T a
872 -- forall a. Eq a => D { x,y :: a } :: T a
874 gadt_constr :: { LConDecl RdrName }
876 { LL (mkGadtDecl $1 $3) }
877 -- Syntax: Maybe merge the record stuff with the single-case above?
878 -- (to kill the mostly harmless reduce/reduce error)
879 -- XXX revisit autrijus
880 | constr_stuff_record '::' sigtype
881 { let (con,details) = unLoc $1 in
882 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
884 | forall context '=>' constr_stuff_record '::' sigtype
885 { let (con,details) = unLoc $4 in
886 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
887 | forall constr_stuff_record '::' sigtype
888 { let (con,details) = unLoc $2 in
889 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
893 constrs :: { Located [LConDecl RdrName] }
894 : {- empty; a GHC extension -} { noLoc [] }
895 | '=' constrs1 { LL (unLoc $2) }
897 constrs1 :: { Located [LConDecl RdrName] }
898 : constrs1 '|' constr { LL ($3 : unLoc $1) }
901 constr :: { LConDecl RdrName }
902 : forall context '=>' constr_stuff
903 { let (con,details) = unLoc $4 in
904 LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
905 | forall constr_stuff
906 { let (con,details) = unLoc $2 in
907 LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
909 forall :: { Located [LHsTyVarBndr RdrName] }
910 : 'forall' tv_bndrs '.' { LL $2 }
911 | {- empty -} { noLoc [] }
913 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
914 -- We parse the constructor declaration
916 -- as a btype (treating C as a type constructor) and then convert C to be
917 -- a data constructor. Reason: it might continue like this:
919 -- in which case C really would be a type constructor. We can't resolve this
920 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
921 : btype {% mkPrefixCon $1 [] >>= return.LL }
922 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
923 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
924 | btype conop btype { LL ($2, InfixCon $1 $3) }
926 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
927 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
928 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
930 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
931 : fielddecl ',' fielddecls { unLoc $1 : $3 }
932 | fielddecl { [unLoc $1] }
934 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
935 : sig_vars '::' ctype { LL (reverse (unLoc $1), $3) }
937 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
938 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
939 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
940 -- We don't allow a context, but that's sorted out by the type checker.
941 deriving :: { Located (Maybe [LHsType RdrName]) }
942 : {- empty -} { noLoc Nothing }
943 | 'deriving' qtycon {% do { let { L loc tv = $2 }
944 ; p <- checkInstType (L loc (HsTyVar tv))
945 ; return (LL (Just [p])) } }
946 | 'deriving' '(' ')' { LL (Just []) }
947 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
948 -- Glasgow extension: allow partial
949 -- applications in derivings
951 -----------------------------------------------------------------------------
954 {- There's an awkward overlap with a type signature. Consider
955 f :: Int -> Int = ...rhs...
956 Then we can't tell whether it's a type signature or a value
957 definition with a result signature until we see the '='.
958 So we have to inline enough to postpone reductions until we know.
962 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
963 instead of qvar, we get another shift/reduce-conflict. Consider the
966 { (^^) :: Int->Int ; } Type signature; only var allowed
968 { (^^) :: Int->Int = ... ; } Value defn with result signature;
969 qvar allowed (because of instance decls)
971 We can't tell whether to reduce var to qvar until after we've read the signatures.
974 decl :: { Located (OrdList (LHsDecl RdrName)) }
976 | '!' infixexp rhs {% do { pat <- checkPattern $2;
977 return (LL $ unitOL $ LL $ ValD $
978 PatBind (LL $ BangPat pat) (unLoc $3)
979 placeHolderType placeHolderNames) } }
980 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
981 return (LL $ unitOL (LL $ ValD r)) } }
983 rhs :: { Located (GRHSs RdrName) }
984 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
985 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
987 gdrhs :: { Located [LGRHS RdrName] }
988 : gdrhs gdrh { LL ($2 : unLoc $1) }
991 gdrh :: { LGRHS RdrName }
992 : '|' quals '=' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
994 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
995 : infixexp '::' sigtype
996 {% do s <- checkValSig $1 $3;
997 return (LL $ unitOL (LL $ SigD s)) }
998 -- See the above notes for why we need infixexp here
999 | var ',' sig_vars '::' sigtype
1000 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
1001 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
1003 | '{-# INLINE' activation qvar '#-}'
1004 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1005 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1006 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1008 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1009 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1011 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1012 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1014 -----------------------------------------------------------------------------
1017 exp :: { LHsExpr RdrName }
1018 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1019 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1020 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1021 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1022 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1025 infixexp :: { LHsExpr RdrName }
1027 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1029 exp10 :: { LHsExpr RdrName }
1030 : '\\' aexp aexps opt_asig '->' exp
1031 {% checkPatterns ($2 : reverse $3) >>= \ ps ->
1032 return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1033 (GRHSs (unguardedRHS $6) emptyLocalBinds
1035 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1036 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1037 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1038 | '-' fexp { LL $ mkHsNegApp $2 }
1040 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1041 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1042 return (L loc (mkHsDo DoExpr stmts body)) }
1043 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1044 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1045 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1046 | scc_annot exp { LL $ if opt_SccProfilingOn
1047 then HsSCC (unLoc $1) $2
1050 | 'proc' aexp '->' exp
1051 {% checkPattern $2 >>= \ p ->
1052 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1053 placeHolderType undefined)) }
1054 -- TODO: is LL right here?
1056 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1057 -- hdaume: core annotation
1060 scc_annot :: { Located FastString }
1061 : '_scc_' STRING { LL $ getSTRING $2 }
1062 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
1064 fexp :: { LHsExpr RdrName }
1065 : fexp aexp { LL $ HsApp $1 $2 }
1068 aexps :: { [LHsExpr RdrName] }
1069 : aexps aexp { $2 : $1 }
1070 | {- empty -} { [] }
1072 aexp :: { LHsExpr RdrName }
1073 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1074 | '~' aexp { LL $ ELazyPat $2 }
1075 -- | '!' aexp { LL $ EBangPat $2 }
1078 aexp1 :: { LHsExpr RdrName }
1079 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1084 -- Here was the syntax for type applications that I was planning
1085 -- but there are difficulties (e.g. what order for type args)
1086 -- so it's not enabled yet.
1087 -- But this case *is* used for the left hand side of a generic definition,
1088 -- which is parsed as an expression before being munged into a pattern
1089 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1090 (sL (getLoc $3) (HsType $3)) }
1092 aexp2 :: { LHsExpr RdrName }
1093 : ipvar { L1 (HsIPVar $! unLoc $1) }
1094 | qcname { L1 (HsVar $! unLoc $1) }
1095 | literal { L1 (HsLit $! unLoc $1) }
1096 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1097 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1098 | '(' exp ')' { LL (HsPar $2) }
1099 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1100 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1101 | '[' list ']' { LL (unLoc $2) }
1102 | '[:' parr ':]' { LL (unLoc $2) }
1103 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1104 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1105 | '_' { L1 EWildPat }
1107 -- Template Haskell Extension
1108 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1109 (L1 $ HsVar (mkUnqual varName
1110 (getTH_ID_SPLICE $1)))) } -- $x
1111 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1113 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1114 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1115 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1116 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1117 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1118 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1119 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1120 return (LL $ HsBracket (PatBr p)) }
1121 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBr (mkGroup $2)) }
1123 -- arrow notation extension
1124 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1126 cmdargs :: { [LHsCmdTop RdrName] }
1127 : cmdargs acmd { $2 : $1 }
1128 | {- empty -} { [] }
1130 acmd :: { LHsCmdTop RdrName }
1131 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1133 cvtopbody :: { [LHsDecl RdrName] }
1134 : '{' cvtopdecls0 '}' { $2 }
1135 | vocurly cvtopdecls0 close { $2 }
1137 cvtopdecls0 :: { [LHsDecl RdrName] }
1138 : {- empty -} { [] }
1141 texp :: { LHsExpr RdrName }
1143 | qopm infixexp { LL $ SectionR $1 $2 }
1144 -- The second production is really here only for bang patterns
1147 texps :: { [LHsExpr RdrName] }
1148 : texps ',' texp { $3 : $1 }
1152 -----------------------------------------------------------------------------
1155 -- The rules below are little bit contorted to keep lexps left-recursive while
1156 -- avoiding another shift/reduce-conflict.
1158 list :: { LHsExpr RdrName }
1159 : texp { L1 $ ExplicitList placeHolderType [$1] }
1160 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1161 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1162 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1163 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1164 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1165 | texp pquals { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1167 lexps :: { Located [LHsExpr RdrName] }
1168 : lexps ',' texp { LL ($3 : unLoc $1) }
1169 | texp ',' texp { LL [$3,$1] }
1171 -----------------------------------------------------------------------------
1172 -- List Comprehensions
1174 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1175 -- or a reversed list of Stmts
1176 : pquals1 { case unLoc $1 of
1178 qss -> L1 [L1 (ParStmt stmtss)]
1180 stmtss = [ (reverse qs, undefined)
1184 pquals1 :: { Located [[LStmt RdrName]] }
1185 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1186 | '|' quals { L (getLoc $2) [unLoc $2] }
1188 quals :: { Located [LStmt RdrName] }
1189 : quals ',' qual { LL ($3 : unLoc $1) }
1192 -----------------------------------------------------------------------------
1193 -- Parallel array expressions
1195 -- The rules below are little bit contorted; see the list case for details.
1196 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1197 -- Moreover, we allow explicit arrays with no element (represented by the nil
1198 -- constructor in the list case).
1200 parr :: { LHsExpr RdrName }
1201 : { noLoc (ExplicitPArr placeHolderType []) }
1202 | exp { L1 $ ExplicitPArr placeHolderType [$1] }
1203 | lexps { L1 $ ExplicitPArr placeHolderType
1204 (reverse (unLoc $1)) }
1205 | exp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1206 | exp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1207 | exp pquals { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1209 -- We are reusing `lexps' and `pquals' from the list case.
1211 -----------------------------------------------------------------------------
1212 -- Case alternatives
1214 altslist :: { Located [LMatch RdrName] }
1215 : '{' alts '}' { LL (reverse (unLoc $2)) }
1216 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1218 alts :: { Located [LMatch RdrName] }
1219 : alts1 { L1 (unLoc $1) }
1220 | ';' alts { LL (unLoc $2) }
1222 alts1 :: { Located [LMatch RdrName] }
1223 : alts1 ';' alt { LL ($3 : unLoc $1) }
1224 | alts1 ';' { LL (unLoc $1) }
1227 alt :: { LMatch RdrName }
1228 : infixexp opt_sig alt_rhs {% checkPattern $1 >>= \p ->
1229 return (LL (Match [p] $2 (unLoc $3))) }
1231 alt_rhs :: { Located (GRHSs RdrName) }
1232 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1234 ralt :: { Located [LGRHS RdrName] }
1235 : '->' exp { LL (unguardedRHS $2) }
1236 | gdpats { L1 (reverse (unLoc $1)) }
1238 gdpats :: { Located [LGRHS RdrName] }
1239 : gdpats gdpat { LL ($2 : unLoc $1) }
1242 gdpat :: { LGRHS RdrName }
1243 : '|' quals '->' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1245 -----------------------------------------------------------------------------
1246 -- Statement sequences
1248 stmtlist :: { Located [LStmt RdrName] }
1249 : '{' stmts '}' { LL (unLoc $2) }
1250 | vocurly stmts close { $2 }
1252 -- do { ;; s ; s ; ; s ;; }
1253 -- The last Stmt should be an expression, but that's hard to enforce
1254 -- here, because we need too much lookahead if we see do { e ; }
1255 -- So we use ExprStmts throughout, and switch the last one over
1256 -- in ParseUtils.checkDo instead
1257 stmts :: { Located [LStmt RdrName] }
1258 : stmt stmts_help { LL ($1 : unLoc $2) }
1259 | ';' stmts { LL (unLoc $2) }
1260 | {- empty -} { noLoc [] }
1262 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1263 : ';' stmts { LL (unLoc $2) }
1264 | {- empty -} { noLoc [] }
1266 -- For typing stmts at the GHCi prompt, where
1267 -- the input may consist of just comments.
1268 maybe_stmt :: { Maybe (LStmt RdrName) }
1270 | {- nothing -} { Nothing }
1272 stmt :: { LStmt RdrName }
1274 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1275 return (LL $ mkBindStmt p $1) }
1276 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1278 qual :: { LStmt RdrName }
1279 : exp '<-' exp {% checkPattern $1 >>= \p ->
1280 return (LL $ mkBindStmt p $3) }
1281 | exp { L1 $ mkExprStmt $1 }
1282 | 'let' binds { LL $ LetStmt (unLoc $2) }
1284 -----------------------------------------------------------------------------
1285 -- Record Field Update/Construction
1287 fbinds :: { HsRecordBinds RdrName }
1289 | {- empty -} { [] }
1291 fbinds1 :: { HsRecordBinds RdrName }
1292 : fbinds1 ',' fbind { $3 : $1 }
1295 fbind :: { (Located RdrName, LHsExpr RdrName) }
1296 : qvar '=' exp { ($1,$3) }
1298 -----------------------------------------------------------------------------
1299 -- Implicit Parameter Bindings
1301 dbinds :: { Located [LIPBind RdrName] }
1302 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1303 | dbinds ';' { LL (unLoc $1) }
1305 -- | {- empty -} { [] }
1307 dbind :: { LIPBind RdrName }
1308 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1310 ipvar :: { Located (IPName RdrName) }
1311 : IPDUPVARID { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1312 | IPSPLITVARID { L1 (Linear (mkUnqual varName (getIPSPLITVARID $1))) }
1314 -----------------------------------------------------------------------------
1317 depreclist :: { Located [RdrName] }
1318 depreclist : deprec_var { L1 [unLoc $1] }
1319 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1321 deprec_var :: { Located RdrName }
1322 deprec_var : var { $1 }
1325 -----------------------------------------
1326 -- Data constructors
1327 qcon :: { Located RdrName }
1329 | '(' qconsym ')' { LL (unLoc $2) }
1330 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1331 -- The case of '[:' ':]' is part of the production `parr'
1333 con :: { Located RdrName }
1335 | '(' consym ')' { LL (unLoc $2) }
1336 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1338 sysdcon :: { Located DataCon } -- Wired in data constructors
1339 : '(' ')' { LL unitDataCon }
1340 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1341 | '[' ']' { LL nilDataCon }
1343 conop :: { Located RdrName }
1345 | '`' conid '`' { LL (unLoc $2) }
1347 qconop :: { Located RdrName }
1349 | '`' qconid '`' { LL (unLoc $2) }
1351 -----------------------------------------------------------------------------
1352 -- Type constructors
1354 gtycon :: { Located RdrName } -- A "general" qualified tycon
1356 | '(' ')' { LL $ getRdrName unitTyCon }
1357 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1358 | '(' '->' ')' { LL $ getRdrName funTyCon }
1359 | '[' ']' { LL $ listTyCon_RDR }
1360 | '[:' ':]' { LL $ parrTyCon_RDR }
1362 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1364 | '(' qtyconsym ')' { LL (unLoc $2) }
1366 qtyconop :: { Located RdrName } -- Qualified or unqualified
1368 | '`' qtycon '`' { LL (unLoc $2) }
1370 qtycon :: { Located RdrName } -- Qualified or unqualified
1371 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1374 tycon :: { Located RdrName } -- Unqualified
1375 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1377 qtyconsym :: { Located RdrName }
1378 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1381 tyconsym :: { Located RdrName }
1382 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1384 -----------------------------------------------------------------------------
1387 op :: { Located RdrName } -- used in infix decls
1391 varop :: { Located RdrName }
1393 | '`' varid '`' { LL (unLoc $2) }
1395 qop :: { LHsExpr RdrName } -- used in sections
1396 : qvarop { L1 $ HsVar (unLoc $1) }
1397 | qconop { L1 $ HsVar (unLoc $1) }
1399 qopm :: { LHsExpr RdrName } -- used in sections
1400 : qvaropm { L1 $ HsVar (unLoc $1) }
1401 | qconop { L1 $ HsVar (unLoc $1) }
1403 qvarop :: { Located RdrName }
1405 | '`' qvarid '`' { LL (unLoc $2) }
1407 qvaropm :: { Located RdrName }
1408 : qvarsym_no_minus { $1 }
1409 | '`' qvarid '`' { LL (unLoc $2) }
1411 -----------------------------------------------------------------------------
1414 tyvar :: { Located RdrName }
1415 tyvar : tyvarid { $1 }
1416 | '(' tyvarsym ')' { LL (unLoc $2) }
1418 tyvarop :: { Located RdrName }
1419 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1422 tyvarid :: { Located RdrName }
1423 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1424 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1425 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1426 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1427 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1429 tyvarsym :: { Located RdrName }
1430 -- Does not include "!", because that is used for strictness marks
1431 -- or ".", because that separates the quantified type vars from the rest
1432 -- or "*", because that's used for kinds
1433 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1435 -----------------------------------------------------------------------------
1438 var :: { Located RdrName }
1440 | '(' varsym ')' { LL (unLoc $2) }
1442 qvar :: { Located RdrName }
1444 | '(' varsym ')' { LL (unLoc $2) }
1445 | '(' qvarsym1 ')' { LL (unLoc $2) }
1446 -- We've inlined qvarsym here so that the decision about
1447 -- whether it's a qvar or a var can be postponed until
1448 -- *after* we see the close paren.
1450 qvarid :: { Located RdrName }
1452 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1454 varid :: { Located RdrName }
1455 : varid_no_unsafe { $1 }
1456 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1457 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1458 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1460 varid_no_unsafe :: { Located RdrName }
1461 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1462 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1463 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1465 qvarsym :: { Located RdrName }
1469 qvarsym_no_minus :: { Located RdrName }
1470 : varsym_no_minus { $1 }
1473 qvarsym1 :: { Located RdrName }
1474 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1476 varsym :: { Located RdrName }
1477 : varsym_no_minus { $1 }
1478 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1480 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1481 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1482 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1485 -- These special_ids are treated as keywords in various places,
1486 -- but as ordinary ids elsewhere. 'special_id' collects all these
1487 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1488 special_id :: { Located FastString }
1490 : 'as' { L1 FSLIT("as") }
1491 | 'qualified' { L1 FSLIT("qualified") }
1492 | 'hiding' { L1 FSLIT("hiding") }
1493 | 'export' { L1 FSLIT("export") }
1494 | 'label' { L1 FSLIT("label") }
1495 | 'dynamic' { L1 FSLIT("dynamic") }
1496 | 'stdcall' { L1 FSLIT("stdcall") }
1497 | 'ccall' { L1 FSLIT("ccall") }
1499 special_sym :: { Located FastString }
1500 special_sym : '!' { L1 FSLIT("!") }
1501 | '.' { L1 FSLIT(".") }
1502 | '*' { L1 FSLIT("*") }
1504 -----------------------------------------------------------------------------
1505 -- Data constructors
1507 qconid :: { Located RdrName } -- Qualified or unqualified
1509 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1511 conid :: { Located RdrName }
1512 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1514 qconsym :: { Located RdrName } -- Qualified or unqualified
1516 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1518 consym :: { Located RdrName }
1519 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1521 -- ':' means only list cons
1522 | ':' { L1 $ consDataCon_RDR }
1525 -----------------------------------------------------------------------------
1528 literal :: { Located HsLit }
1529 : CHAR { L1 $ HsChar $ getCHAR $1 }
1530 | STRING { L1 $ HsString $ getSTRING $1 }
1531 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1532 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1533 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1534 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1535 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1537 -----------------------------------------------------------------------------
1541 : vccurly { () } -- context popped in lexer.
1542 | error {% popContext }
1544 -----------------------------------------------------------------------------
1545 -- Miscellaneous (mostly renamings)
1547 modid :: { Located Module }
1548 : CONID { L1 $ mkModuleFS (getCONID $1) }
1549 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1552 (unpackFS mod ++ '.':unpackFS c))
1556 : commas ',' { $1 + 1 }
1559 -----------------------------------------------------------------------------
1563 happyError = srcParseFail
1565 getVARID (L _ (ITvarid x)) = x
1566 getCONID (L _ (ITconid x)) = x
1567 getVARSYM (L _ (ITvarsym x)) = x
1568 getCONSYM (L _ (ITconsym x)) = x
1569 getQVARID (L _ (ITqvarid x)) = x
1570 getQCONID (L _ (ITqconid x)) = x
1571 getQVARSYM (L _ (ITqvarsym x)) = x
1572 getQCONSYM (L _ (ITqconsym x)) = x
1573 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1574 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1575 getCHAR (L _ (ITchar x)) = x
1576 getSTRING (L _ (ITstring x)) = x
1577 getINTEGER (L _ (ITinteger x)) = x
1578 getRATIONAL (L _ (ITrational x)) = x
1579 getPRIMCHAR (L _ (ITprimchar x)) = x
1580 getPRIMSTRING (L _ (ITprimstring x)) = x
1581 getPRIMINTEGER (L _ (ITprimint x)) = x
1582 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1583 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1584 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1585 getINLINE (L _ (ITinline_prag b)) = b
1586 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1588 -- Utilities for combining source spans
1589 comb2 :: Located a -> Located b -> SrcSpan
1592 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1593 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1595 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1596 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1597 combineSrcSpans (getLoc c) (getLoc d)
1599 -- strict constructor version:
1601 sL :: SrcSpan -> a -> Located a
1602 sL span a = span `seq` L span a
1604 -- Make a source location for the file. We're a bit lazy here and just
1605 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1606 -- try to find the span of the whole file (ToDo).
1607 fileSrcSpan :: P SrcSpan
1610 let loc = mkSrcLoc (srcLocFile l) 1 0;
1611 return (mkSrcSpan loc loc)