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 }
442 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
445 tycl_decl :: { LTyClDecl RdrName }
446 : 'type' type '=' ctype
447 -- Note type on the left of the '='; this allows
448 -- infix type constructors to be declared
450 -- Note ctype, not sigtype, on the right
451 -- We allow an explicit for-all but we don't insert one
452 -- in type Foo a = (b,b)
453 -- Instead we just say b is out of scope
454 {% do { (tc,tvs) <- checkSynHdr $2
455 ; return (LL (TySynonym tc tvs $4)) } }
457 | data_or_newtype tycl_hdr constrs deriving
458 { L (comb4 $1 $2 $3 $4) -- We need the location on tycl_hdr
459 -- in case constrs and deriving are both empty
460 (mkTyData (unLoc $1) (unLoc $2) Nothing (reverse (unLoc $3)) (unLoc $4)) }
462 | data_or_newtype tycl_hdr opt_kind_sig
463 'where' gadt_constrlist
465 { L (comb4 $1 $2 $4 $5)
466 (mkTyData (unLoc $1) (unLoc $2) $3 (reverse (unLoc $5)) (unLoc $6)) }
468 | 'class' tycl_hdr fds where
470 (binds,sigs) = cvBindsAndSigs (unLoc $4)
472 L (comb4 $1 $2 $3 $4) (mkClassDecl (unLoc $2) (unLoc $3) sigs
475 data_or_newtype :: { Located NewOrData }
476 : 'data' { L1 DataType }
477 | 'newtype' { L1 NewType }
479 opt_kind_sig :: { Maybe Kind }
481 | '::' kind { Just $2 }
483 -- tycl_hdr parses the header of a type or class decl,
484 -- which takes the form
487 -- (Eq a, Ord b) => T a b
488 -- Rather a lot of inlining here, else we get reduce/reduce errors
489 tycl_hdr :: { Located (LHsContext RdrName, Located RdrName, [LHsTyVarBndr RdrName]) }
490 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
491 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
493 -----------------------------------------------------------------------------
494 -- Nested declarations
496 decls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
497 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
498 | decls ';' { LL (unLoc $1) }
500 | {- empty -} { noLoc nilOL }
503 decllist :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
504 : '{' decls '}' { LL (unLoc $2) }
505 | vocurly decls close { $2 }
507 where :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
508 -- No implicit parameters
509 : 'where' decllist { LL (unLoc $2) }
510 | {- empty -} { noLoc nilOL }
512 binds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
513 : decllist { L1 (HsValBinds (cvBindGroup (unLoc $1))) }
514 | '{' dbinds '}' { LL (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
515 | vocurly dbinds close { L (getLoc $2) (HsIPBinds (IPBinds (unLoc $2) emptyLHsBinds)) }
517 wherebinds :: { Located (HsLocalBinds RdrName) } -- May have implicit parameters
518 : 'where' binds { LL (unLoc $2) }
519 | {- empty -} { noLoc emptyLocalBinds }
522 -----------------------------------------------------------------------------
523 -- Transformation Rules
525 rules :: { OrdList (LHsDecl RdrName) } -- Reversed
526 : rules ';' rule { $1 `snocOL` $3 }
529 | {- empty -} { nilOL }
531 rule :: { LHsDecl RdrName }
532 : STRING activation rule_forall infixexp '=' exp
533 { LL $ RuleD (HsRule (getSTRING $1)
534 ($2 `orElse` AlwaysActive)
535 $3 $4 placeHolderNames $6 placeHolderNames) }
537 activation :: { Maybe Activation }
538 : {- empty -} { Nothing }
539 | explicit_activation { Just $1 }
541 explicit_activation :: { Activation } -- In brackets
542 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
543 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
545 rule_forall :: { [RuleBndr RdrName] }
546 : 'forall' rule_var_list '.' { $2 }
549 rule_var_list :: { [RuleBndr RdrName] }
551 | rule_var rule_var_list { $1 : $2 }
553 rule_var :: { RuleBndr RdrName }
554 : varid { RuleBndr $1 }
555 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
557 -----------------------------------------------------------------------------
558 -- Deprecations (c.f. rules)
560 deprecations :: { OrdList (LHsDecl RdrName) } -- Reversed
561 : deprecations ';' deprecation { $1 `appOL` $3 }
562 | deprecations ';' { $1 }
564 | {- empty -} { nilOL }
566 -- SUP: TEMPORARY HACK, not checking for `module Foo'
567 deprecation :: { OrdList (LHsDecl RdrName) }
569 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
573 -----------------------------------------------------------------------------
574 -- Foreign import and export declarations
576 -- for the time being, the following accepts foreign declarations conforming
577 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
579 -- * a flag indicates whether pre-standard declarations have been used and
580 -- triggers a deprecation warning further down the road
582 -- NB: The first two rules could be combined into one by replacing `safety1'
583 -- with `safety'. However, the combined rule conflicts with the
586 fdecl :: { LHsDecl RdrName }
587 fdecl : 'import' callconv safety1 fspec
588 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
589 | 'import' callconv fspec
590 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
592 | 'export' callconv fspec
593 {% mkExport $2 (unLoc $3) >>= return.LL }
594 -- the following syntax is DEPRECATED
595 | fdecl1DEPRECATED { L1 (ForD (unLoc $1)) }
596 | fdecl2DEPRECATED { L1 (unLoc $1) }
598 fdecl1DEPRECATED :: { LForeignDecl RdrName }
600 ----------- DEPRECATED label decls ------------
601 : 'label' ext_name varid '::' sigtype
602 { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS
603 (CLabel ($2 `orElse` mkExtName (unLoc $3)))) True }
605 ----------- DEPRECATED ccall/stdcall decls ------------
607 -- NB: This business with the case expression below may seem overly
608 -- complicated, but it is necessary to avoid some conflicts.
610 -- DEPRECATED variant #1: lack of a calling convention specification
612 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
614 target = StaticTarget ($2 `orElse` mkExtName (unLoc $4))
616 LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS
617 (CFunction target)) True }
619 -- DEPRECATED variant #2: external name consists of two separate strings
620 -- (module name and function name) (import)
621 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
623 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
624 CCall cconv -> return $
626 imp = CFunction (StaticTarget (getSTRING $4))
628 LL $ ForeignImport $6 $8 (CImport cconv $5 nilFS nilFS imp) True }
630 -- DEPRECATED variant #3: `unsafe' after entity
631 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
633 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
634 CCall cconv -> return $
636 imp = CFunction (StaticTarget (getSTRING $3))
638 LL $ ForeignImport $5 $7 (CImport cconv PlayRisky nilFS nilFS imp) True }
640 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
641 -- an explicit calling convention (import)
642 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
643 { LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS
644 (CFunction DynamicTarget)) True }
646 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
647 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
649 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
650 CCall cconv -> return $
651 LL $ ForeignImport $5 $7 (CImport cconv $4 nilFS nilFS
652 (CFunction DynamicTarget)) True }
654 -- DEPRECATED variant #6: lack of a calling convention specification
656 | 'export' {-no callconv-} ext_name varid '::' sigtype
657 { LL $ ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName (unLoc $3))
658 defaultCCallConv)) True }
660 -- DEPRECATED variant #7: external name consists of two separate strings
661 -- (module name and function name) (export)
662 | 'export' callconv STRING STRING varid '::' sigtype
664 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
665 CCall cconv -> return $
666 LL $ ForeignExport $5 $7
667 (CExport (CExportStatic (getSTRING $4) cconv)) True }
669 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
670 -- an explicit calling convention (export)
671 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
672 { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS
675 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
676 | 'export' callconv 'dynamic' varid '::' sigtype
678 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
679 CCall cconv -> return $
680 LL $ ForeignImport $4 $6
681 (CImport cconv (PlaySafe False) nilFS nilFS CWrapper) True }
683 ----------- DEPRECATED .NET decls ------------
684 -- NB: removed the .NET call declaration, as it is entirely subsumed
685 -- by the new standard FFI declarations
687 fdecl2DEPRECATED :: { LHsDecl RdrName }
689 : 'import' 'dotnet' 'type' ext_name tycon { LL $ TyClD (ForeignType $5 $4 DNType) }
690 -- left this one unchanged for the moment as type imports are not
691 -- covered currently by the FFI standard -=chak
694 callconv :: { CallConv }
695 : 'stdcall' { CCall StdCallConv }
696 | 'ccall' { CCall CCallConv }
697 | 'dotnet' { DNCall }
700 : 'unsafe' { PlayRisky }
701 | 'safe' { PlaySafe False }
702 | 'threadsafe' { PlaySafe True }
703 | {- empty -} { PlaySafe False }
705 safety1 :: { Safety }
706 : 'unsafe' { PlayRisky }
707 | 'safe' { PlaySafe False }
708 | 'threadsafe' { PlaySafe True }
709 -- only needed to avoid conflicts with the DEPRECATED rules
711 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
712 : STRING var '::' sigtype { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
713 | var '::' sigtype { LL (noLoc nilFS, $1, $3) }
714 -- if the entity string is missing, it defaults to the empty string;
715 -- the meaning of an empty entity string depends on the calling
719 ext_name :: { Maybe CLabelString }
720 : STRING { Just (getSTRING $1) }
721 | STRING STRING { Just (getSTRING $2) } -- Ignore "module name" for now
722 | {- empty -} { Nothing }
725 -----------------------------------------------------------------------------
728 opt_sig :: { Maybe (LHsType RdrName) }
729 : {- empty -} { Nothing }
730 | '::' sigtype { Just $2 }
732 opt_asig :: { Maybe (LHsType RdrName) }
733 : {- empty -} { Nothing }
734 | '::' atype { Just $2 }
736 sigtypes1 :: { [LHsType RdrName] }
738 | sigtype ',' sigtypes1 { $1 : $3 }
740 sigtype :: { LHsType RdrName }
741 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
742 -- Wrap an Implicit forall if there isn't one there already
744 sig_vars :: { Located [Located RdrName] }
745 : sig_vars ',' var { LL ($3 : unLoc $1) }
748 -----------------------------------------------------------------------------
751 strict_mark :: { Located HsBang }
752 : '!' { L1 HsStrict }
753 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
755 -- A ctype is a for-all type
756 ctype :: { LHsType RdrName }
757 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
758 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
759 -- A type of form (context => type) is an *implicit* HsForAllTy
762 -- We parse a context as a btype so that we don't get reduce/reduce
763 -- errors in ctype. The basic problem is that
765 -- looks so much like a tuple type. We can't tell until we find the =>
766 context :: { LHsContext RdrName }
767 : btype {% checkContext $1 }
769 type :: { LHsType RdrName }
770 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
773 gentype :: { LHsType RdrName }
775 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
776 | btype tyvarop gentype { LL $ HsOpTy $1 $2 $3 }
777 | btype '->' ctype { LL $ HsFunTy $1 $3 }
779 btype :: { LHsType RdrName }
780 : btype atype { LL $ HsAppTy $1 $2 }
783 atype :: { LHsType RdrName }
784 : gtycon { L1 (HsTyVar (unLoc $1)) }
785 | tyvar { L1 (HsTyVar (unLoc $1)) }
786 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
787 | '(' ctype ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
788 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
789 | '[' ctype ']' { LL $ HsListTy $2 }
790 | '[:' ctype ':]' { LL $ HsPArrTy $2 }
791 | '(' ctype ')' { LL $ HsParTy $2 }
792 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 $4 }
794 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
796 -- An inst_type is what occurs in the head of an instance decl
797 -- e.g. (Foo a, Gaz b) => Wibble a b
798 -- It's kept as a single type, with a MonoDictTy at the right
799 -- hand corner, for convenience.
800 inst_type :: { LHsType RdrName }
801 : sigtype {% checkInstType $1 }
803 inst_types1 :: { [LHsType RdrName] }
805 | inst_type ',' inst_types1 { $1 : $3 }
807 comma_types0 :: { [LHsType RdrName] }
808 : comma_types1 { $1 }
811 comma_types1 :: { [LHsType RdrName] }
813 | ctype ',' comma_types1 { $1 : $3 }
815 tv_bndrs :: { [LHsTyVarBndr RdrName] }
816 : tv_bndr tv_bndrs { $1 : $2 }
819 tv_bndr :: { LHsTyVarBndr RdrName }
820 : tyvar { L1 (UserTyVar (unLoc $1)) }
821 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2) $4) }
823 fds :: { Located [Located ([RdrName], [RdrName])] }
824 : {- empty -} { noLoc [] }
825 | '|' fds1 { LL (reverse (unLoc $2)) }
827 fds1 :: { Located [Located ([RdrName], [RdrName])] }
828 : fds1 ',' fd { LL ($3 : unLoc $1) }
831 fd :: { Located ([RdrName], [RdrName]) }
832 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
833 (reverse (unLoc $1), reverse (unLoc $3)) }
835 varids0 :: { Located [RdrName] }
836 : {- empty -} { noLoc [] }
837 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
839 -----------------------------------------------------------------------------
844 | akind '->' kind { mkArrowKind $1 $3 }
847 : '*' { liftedTypeKind }
848 | '(' kind ')' { $2 }
851 -----------------------------------------------------------------------------
852 -- Datatype declarations
854 gadt_constrlist :: { Located [LConDecl RdrName] }
855 : '{' gadt_constrs '}' { LL (unLoc $2) }
856 | vocurly gadt_constrs close { $2 }
858 gadt_constrs :: { Located [LConDecl RdrName] }
859 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
860 | gadt_constrs ';' { $1 }
861 | gadt_constr { L1 [$1] }
863 -- We allow the following forms:
864 -- C :: Eq a => a -> T a
865 -- C :: forall a. Eq a => !a -> T a
866 -- D { x,y :: a } :: T a
867 -- forall a. Eq a => D { x,y :: a } :: T a
869 gadt_constr :: { LConDecl RdrName }
871 { LL (mkGadtDecl $1 $3) }
872 -- Syntax: Maybe merge the record stuff with the single-case above?
873 -- (to kill the mostly harmless reduce/reduce error)
874 -- XXX revisit autrijus
875 | constr_stuff_record '::' sigtype
876 { let (con,details) = unLoc $1 in
877 LL (ConDecl con Implicit [] (noLoc []) details (ResTyGADT $3)) }
879 | forall context '=>' constr_stuff_record '::' sigtype
880 { let (con,details) = unLoc $4 in
881 LL (ConDecl con Implicit (unLoc $1) $2 details (ResTyGADT $6)) }
882 | forall constr_stuff_record '::' sigtype
883 { let (con,details) = unLoc $2 in
884 LL (ConDecl con Implicit (unLoc $1) (noLoc []) details (ResTyGADT $4)) }
888 constrs :: { Located [LConDecl RdrName] }
889 : {- empty; a GHC extension -} { noLoc [] }
890 | '=' constrs1 { LL (unLoc $2) }
892 constrs1 :: { Located [LConDecl RdrName] }
893 : constrs1 '|' constr { LL ($3 : unLoc $1) }
896 constr :: { LConDecl RdrName }
897 : forall context '=>' constr_stuff
898 { let (con,details) = unLoc $4 in
899 LL (ConDecl con Explicit (unLoc $1) $2 details ResTyH98) }
900 | forall constr_stuff
901 { let (con,details) = unLoc $2 in
902 LL (ConDecl con Explicit (unLoc $1) (noLoc []) details ResTyH98) }
904 forall :: { Located [LHsTyVarBndr RdrName] }
905 : 'forall' tv_bndrs '.' { LL $2 }
906 | {- empty -} { noLoc [] }
908 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
909 -- We parse the constructor declaration
911 -- as a btype (treating C as a type constructor) and then convert C to be
912 -- a data constructor. Reason: it might continue like this:
914 -- in which case C really would be a type constructor. We can't resolve this
915 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
916 : btype {% mkPrefixCon $1 [] >>= return.LL }
917 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
918 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
919 | btype conop btype { LL ($2, InfixCon $1 $3) }
921 constr_stuff_record :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
922 : oqtycon '{' '}' {% mkRecCon $1 [] >>= return.sL (comb2 $1 $>) }
923 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.sL (comb2 $1 $>) }
925 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
926 : fielddecl ',' fielddecls { unLoc $1 : $3 }
927 | fielddecl { [unLoc $1] }
929 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
930 : sig_vars '::' ctype { LL (reverse (unLoc $1), $3) }
932 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
933 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
934 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
935 -- We don't allow a context, but that's sorted out by the type checker.
936 deriving :: { Located (Maybe [LHsType RdrName]) }
937 : {- empty -} { noLoc Nothing }
938 | 'deriving' qtycon {% do { let { L loc tv = $2 }
939 ; p <- checkInstType (L loc (HsTyVar tv))
940 ; return (LL (Just [p])) } }
941 | 'deriving' '(' ')' { LL (Just []) }
942 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
943 -- Glasgow extension: allow partial
944 -- applications in derivings
946 -----------------------------------------------------------------------------
949 {- There's an awkward overlap with a type signature. Consider
950 f :: Int -> Int = ...rhs...
951 Then we can't tell whether it's a type signature or a value
952 definition with a result signature until we see the '='.
953 So we have to inline enough to postpone reductions until we know.
957 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
958 instead of qvar, we get another shift/reduce-conflict. Consider the
961 { (^^) :: Int->Int ; } Type signature; only var allowed
963 { (^^) :: Int->Int = ... ; } Value defn with result signature;
964 qvar allowed (because of instance decls)
966 We can't tell whether to reduce var to qvar until after we've read the signatures.
969 decl :: { Located (OrdList (LHsDecl RdrName)) }
971 | '!' infixexp rhs {% do { pat <- checkPattern $2;
972 return (LL $ unitOL $ LL $ ValD $
973 PatBind (LL $ BangPat pat) (unLoc $3)
974 placeHolderType placeHolderNames) } }
975 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
976 return (LL $ unitOL (LL $ ValD r)) } }
978 rhs :: { Located (GRHSs RdrName) }
979 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
980 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
982 gdrhs :: { Located [LGRHS RdrName] }
983 : gdrhs gdrh { LL ($2 : unLoc $1) }
986 gdrh :: { LGRHS RdrName }
987 : '|' quals '=' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
989 sigdecl :: { Located (OrdList (LHsDecl RdrName)) }
990 : infixexp '::' sigtype
991 {% do s <- checkValSig $1 $3;
992 return (LL $ unitOL (LL $ SigD s)) }
993 -- See the above notes for why we need infixexp here
994 | var ',' sig_vars '::' sigtype
995 { LL $ toOL [ LL $ SigD (TypeSig n $5) | n <- $1 : unLoc $3 ] }
996 | infix prec ops { LL $ toOL [ LL $ SigD (FixSig (FixitySig n (Fixity $2 (unLoc $1))))
998 | '{-# INLINE' activation qvar '#-}'
999 { LL $ unitOL (LL $ SigD (InlineSig $3 (mkInlineSpec $2 (getINLINE $1)))) }
1000 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1001 { LL $ toOL [ LL $ SigD (SpecSig $2 t defaultInlineSpec)
1003 | '{-# SPECIALISE_INLINE' activation qvar '::' sigtypes1 '#-}'
1004 { LL $ toOL [ LL $ SigD (SpecSig $3 t (mkInlineSpec $2 (getSPEC_INLINE $1)))
1006 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1007 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1009 -----------------------------------------------------------------------------
1012 exp :: { LHsExpr RdrName }
1013 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1014 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1015 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1016 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1017 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1020 infixexp :: { LHsExpr RdrName }
1022 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1024 exp10 :: { LHsExpr RdrName }
1025 : '\\' aexp aexps opt_asig '->' exp
1026 {% checkPatterns ($2 : reverse $3) >>= \ ps ->
1027 return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1028 (GRHSs (unguardedRHS $6) emptyLocalBinds
1030 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1031 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1032 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1033 | '-' fexp { LL $ mkHsNegApp $2 }
1035 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1036 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1037 return (L loc (mkHsDo DoExpr stmts body)) }
1038 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1039 checkDo loc (unLoc $2) >>= \ (stmts,body) ->
1040 return (L loc (mkHsDo (MDoExpr noPostTcTable) stmts body)) }
1041 | scc_annot exp { LL $ if opt_SccProfilingOn
1042 then HsSCC (unLoc $1) $2
1045 | 'proc' aexp '->' exp
1046 {% checkPattern $2 >>= \ p ->
1047 return (LL $ HsProc p (LL $ HsCmdTop $4 []
1048 placeHolderType undefined)) }
1049 -- TODO: is LL right here?
1051 | '{-# CORE' STRING '#-}' exp { LL $ HsCoreAnn (getSTRING $2) $4 }
1052 -- hdaume: core annotation
1055 scc_annot :: { Located FastString }
1056 : '_scc_' STRING { LL $ getSTRING $2 }
1057 | '{-# SCC' STRING '#-}' { LL $ getSTRING $2 }
1059 fexp :: { LHsExpr RdrName }
1060 : fexp aexp { LL $ HsApp $1 $2 }
1063 aexps :: { [LHsExpr RdrName] }
1064 : aexps aexp { $2 : $1 }
1065 | {- empty -} { [] }
1067 aexp :: { LHsExpr RdrName }
1068 : qvar '@' aexp { LL $ EAsPat $1 $3 }
1069 | '~' aexp { LL $ ELazyPat $2 }
1070 -- | '!' aexp { LL $ EBangPat $2 }
1073 aexp1 :: { LHsExpr RdrName }
1074 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1079 -- Here was the syntax for type applications that I was planning
1080 -- but there are difficulties (e.g. what order for type args)
1081 -- so it's not enabled yet.
1082 -- But this case *is* used for the left hand side of a generic definition,
1083 -- which is parsed as an expression before being munged into a pattern
1084 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1085 (sL (getLoc $3) (HsType $3)) }
1087 aexp2 :: { LHsExpr RdrName }
1088 : ipvar { L1 (HsIPVar $! unLoc $1) }
1089 | qcname { L1 (HsVar $! unLoc $1) }
1090 | literal { L1 (HsLit $! unLoc $1) }
1091 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1092 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1093 | '(' exp ')' { LL (HsPar $2) }
1094 | '(' texp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1095 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1096 | '[' list ']' { LL (unLoc $2) }
1097 | '[:' parr ':]' { LL (unLoc $2) }
1098 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1099 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1100 | '_' { L1 EWildPat }
1102 -- MetaHaskell Extension
1103 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1104 (L1 $ HsVar (mkUnqual varName
1105 (getTH_ID_SPLICE $1)))) } -- $x
1106 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1108 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1109 | TH_VAR_QUOTE qcon { LL $ HsBracket (VarBr (unLoc $2)) }
1110 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1111 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1112 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1113 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1114 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1115 return (LL $ HsBracket (PatBr p)) }
1116 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBr (mkGroup $2)) }
1118 -- arrow notation extension
1119 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1121 cmdargs :: { [LHsCmdTop RdrName] }
1122 : cmdargs acmd { $2 : $1 }
1123 | {- empty -} { [] }
1125 acmd :: { LHsCmdTop RdrName }
1126 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1128 cvtopbody :: { [LHsDecl RdrName] }
1129 : '{' cvtopdecls0 '}' { $2 }
1130 | vocurly cvtopdecls0 close { $2 }
1132 cvtopdecls0 :: { [LHsDecl RdrName] }
1133 : {- empty -} { [] }
1136 texp :: { LHsExpr RdrName }
1138 | qopm infixexp { LL $ SectionR $1 $2 }
1139 -- The second production is really here only for bang patterns
1142 texps :: { [LHsExpr RdrName] }
1143 : texps ',' texp { $3 : $1 }
1147 -----------------------------------------------------------------------------
1150 -- The rules below are little bit contorted to keep lexps left-recursive while
1151 -- avoiding another shift/reduce-conflict.
1153 list :: { LHsExpr RdrName }
1154 : texp { L1 $ ExplicitList placeHolderType [$1] }
1155 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1156 | texp '..' { LL $ ArithSeq noPostTcExpr (From $1) }
1157 | texp ',' exp '..' { LL $ ArithSeq noPostTcExpr (FromThen $1 $3) }
1158 | texp '..' exp { LL $ ArithSeq noPostTcExpr (FromTo $1 $3) }
1159 | texp ',' exp '..' exp { LL $ ArithSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1160 | texp pquals { sL (comb2 $1 $>) $ mkHsDo ListComp (reverse (unLoc $2)) $1 }
1162 lexps :: { Located [LHsExpr RdrName] }
1163 : lexps ',' texp { LL ($3 : unLoc $1) }
1164 | texp ',' texp { LL [$3,$1] }
1166 -----------------------------------------------------------------------------
1167 -- List Comprehensions
1169 pquals :: { Located [LStmt RdrName] } -- Either a singleton ParStmt,
1170 -- or a reversed list of Stmts
1171 : pquals1 { case unLoc $1 of
1173 qss -> L1 [L1 (ParStmt stmtss)]
1175 stmtss = [ (reverse qs, undefined)
1179 pquals1 :: { Located [[LStmt RdrName]] }
1180 : pquals1 '|' quals { LL (unLoc $3 : unLoc $1) }
1181 | '|' quals { L (getLoc $2) [unLoc $2] }
1183 quals :: { Located [LStmt RdrName] }
1184 : quals ',' qual { LL ($3 : unLoc $1) }
1187 -----------------------------------------------------------------------------
1188 -- Parallel array expressions
1190 -- The rules below are little bit contorted; see the list case for details.
1191 -- Note that, in contrast to lists, we only have finite arithmetic sequences.
1192 -- Moreover, we allow explicit arrays with no element (represented by the nil
1193 -- constructor in the list case).
1195 parr :: { LHsExpr RdrName }
1196 : { noLoc (ExplicitPArr placeHolderType []) }
1197 | exp { L1 $ ExplicitPArr placeHolderType [$1] }
1198 | lexps { L1 $ ExplicitPArr placeHolderType
1199 (reverse (unLoc $1)) }
1200 | exp '..' exp { LL $ PArrSeq noPostTcExpr (FromTo $1 $3) }
1201 | exp ',' exp '..' exp { LL $ PArrSeq noPostTcExpr (FromThenTo $1 $3 $5) }
1202 | exp pquals { sL (comb2 $1 $>) $ mkHsDo PArrComp (reverse (unLoc $2)) $1 }
1204 -- We are reusing `lexps' and `pquals' from the list case.
1206 -----------------------------------------------------------------------------
1207 -- Case alternatives
1209 altslist :: { Located [LMatch RdrName] }
1210 : '{' alts '}' { LL (reverse (unLoc $2)) }
1211 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1213 alts :: { Located [LMatch RdrName] }
1214 : alts1 { L1 (unLoc $1) }
1215 | ';' alts { LL (unLoc $2) }
1217 alts1 :: { Located [LMatch RdrName] }
1218 : alts1 ';' alt { LL ($3 : unLoc $1) }
1219 | alts1 ';' { LL (unLoc $1) }
1222 alt :: { LMatch RdrName }
1223 : infixexp opt_sig alt_rhs {% checkPattern $1 >>= \p ->
1224 return (LL (Match [p] $2 (unLoc $3))) }
1226 alt_rhs :: { Located (GRHSs RdrName) }
1227 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1229 ralt :: { Located [LGRHS RdrName] }
1230 : '->' exp { LL (unguardedRHS $2) }
1231 | gdpats { L1 (reverse (unLoc $1)) }
1233 gdpats :: { Located [LGRHS RdrName] }
1234 : gdpats gdpat { LL ($2 : unLoc $1) }
1237 gdpat :: { LGRHS RdrName }
1238 : '|' quals '->' exp { sL (comb2 $1 $>) $ GRHS (reverse (unLoc $2)) $4 }
1240 -----------------------------------------------------------------------------
1241 -- Statement sequences
1243 stmtlist :: { Located [LStmt RdrName] }
1244 : '{' stmts '}' { LL (unLoc $2) }
1245 | vocurly stmts close { $2 }
1247 -- do { ;; s ; s ; ; s ;; }
1248 -- The last Stmt should be an expression, but that's hard to enforce
1249 -- here, because we need too much lookahead if we see do { e ; }
1250 -- So we use ExprStmts throughout, and switch the last one over
1251 -- in ParseUtils.checkDo instead
1252 stmts :: { Located [LStmt RdrName] }
1253 : stmt stmts_help { LL ($1 : unLoc $2) }
1254 | ';' stmts { LL (unLoc $2) }
1255 | {- empty -} { noLoc [] }
1257 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1258 : ';' stmts { LL (unLoc $2) }
1259 | {- empty -} { noLoc [] }
1261 -- For typing stmts at the GHCi prompt, where
1262 -- the input may consist of just comments.
1263 maybe_stmt :: { Maybe (LStmt RdrName) }
1265 | {- nothing -} { Nothing }
1267 stmt :: { LStmt RdrName }
1269 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1270 return (LL $ mkBindStmt p $1) }
1271 | 'rec' stmtlist { LL $ mkRecStmt (unLoc $2) }
1273 qual :: { LStmt RdrName }
1274 : exp '<-' exp {% checkPattern $1 >>= \p ->
1275 return (LL $ mkBindStmt p $3) }
1276 | exp { L1 $ mkExprStmt $1 }
1277 | 'let' binds { LL $ LetStmt (unLoc $2) }
1279 -----------------------------------------------------------------------------
1280 -- Record Field Update/Construction
1282 fbinds :: { HsRecordBinds RdrName }
1284 | {- empty -} { [] }
1286 fbinds1 :: { HsRecordBinds RdrName }
1287 : fbinds1 ',' fbind { $3 : $1 }
1290 fbind :: { (Located RdrName, LHsExpr RdrName) }
1291 : qvar '=' exp { ($1,$3) }
1293 -----------------------------------------------------------------------------
1294 -- Implicit Parameter Bindings
1296 dbinds :: { Located [LIPBind RdrName] }
1297 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1298 | dbinds ';' { LL (unLoc $1) }
1300 -- | {- empty -} { [] }
1302 dbind :: { LIPBind RdrName }
1303 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1305 ipvar :: { Located (IPName RdrName) }
1306 : IPDUPVARID { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1307 | IPSPLITVARID { L1 (Linear (mkUnqual varName (getIPSPLITVARID $1))) }
1309 -----------------------------------------------------------------------------
1312 depreclist :: { Located [RdrName] }
1313 depreclist : deprec_var { L1 [unLoc $1] }
1314 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1316 deprec_var :: { Located RdrName }
1317 deprec_var : var { $1 }
1320 -----------------------------------------
1321 -- Data constructors
1322 qcon :: { Located RdrName }
1324 | '(' qconsym ')' { LL (unLoc $2) }
1325 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1326 -- The case of '[:' ':]' is part of the production `parr'
1328 con :: { Located RdrName }
1330 | '(' consym ')' { LL (unLoc $2) }
1331 | sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1333 sysdcon :: { Located DataCon } -- Wired in data constructors
1334 : '(' ')' { LL unitDataCon }
1335 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1336 | '[' ']' { LL nilDataCon }
1338 conop :: { Located RdrName }
1340 | '`' conid '`' { LL (unLoc $2) }
1342 qconop :: { Located RdrName }
1344 | '`' qconid '`' { LL (unLoc $2) }
1346 -----------------------------------------------------------------------------
1347 -- Type constructors
1349 gtycon :: { Located RdrName } -- A "general" qualified tycon
1351 | '(' ')' { LL $ getRdrName unitTyCon }
1352 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1353 | '(' '->' ')' { LL $ getRdrName funTyCon }
1354 | '[' ']' { LL $ listTyCon_RDR }
1355 | '[:' ':]' { LL $ parrTyCon_RDR }
1357 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1359 | '(' qtyconsym ')' { LL (unLoc $2) }
1361 qtyconop :: { Located RdrName } -- Qualified or unqualified
1363 | '`' qtycon '`' { LL (unLoc $2) }
1365 qtycon :: { Located RdrName } -- Qualified or unqualified
1366 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1369 tycon :: { Located RdrName } -- Unqualified
1370 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1372 qtyconsym :: { Located RdrName }
1373 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1376 tyconsym :: { Located RdrName }
1377 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1379 -----------------------------------------------------------------------------
1382 op :: { Located RdrName } -- used in infix decls
1386 varop :: { Located RdrName }
1388 | '`' varid '`' { LL (unLoc $2) }
1390 qop :: { LHsExpr RdrName } -- used in sections
1391 : qvarop { L1 $ HsVar (unLoc $1) }
1392 | qconop { L1 $ HsVar (unLoc $1) }
1394 qopm :: { LHsExpr RdrName } -- used in sections
1395 : qvaropm { L1 $ HsVar (unLoc $1) }
1396 | qconop { L1 $ HsVar (unLoc $1) }
1398 qvarop :: { Located RdrName }
1400 | '`' qvarid '`' { LL (unLoc $2) }
1402 qvaropm :: { Located RdrName }
1403 : qvarsym_no_minus { $1 }
1404 | '`' qvarid '`' { LL (unLoc $2) }
1406 -----------------------------------------------------------------------------
1409 tyvar :: { Located RdrName }
1410 tyvar : tyvarid { $1 }
1411 | '(' tyvarsym ')' { LL (unLoc $2) }
1413 tyvarop :: { Located RdrName }
1414 tyvarop : '`' tyvarid '`' { LL (unLoc $2) }
1417 tyvarid :: { Located RdrName }
1418 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1419 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1420 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1421 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1422 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1424 tyvarsym :: { Located RdrName }
1425 -- Does not include "!", because that is used for strictness marks
1426 -- or ".", because that separates the quantified type vars from the rest
1427 -- or "*", because that's used for kinds
1428 tyvarsym : VARSYM { L1 $! mkUnqual tvName (getVARSYM $1) }
1430 -----------------------------------------------------------------------------
1433 var :: { Located RdrName }
1435 | '(' varsym ')' { LL (unLoc $2) }
1437 qvar :: { Located RdrName }
1439 | '(' varsym ')' { LL (unLoc $2) }
1440 | '(' qvarsym1 ')' { LL (unLoc $2) }
1441 -- We've inlined qvarsym here so that the decision about
1442 -- whether it's a qvar or a var can be postponed until
1443 -- *after* we see the close paren.
1445 qvarid :: { Located RdrName }
1447 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1449 varid :: { Located RdrName }
1450 : varid_no_unsafe { $1 }
1451 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1452 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1453 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1455 varid_no_unsafe :: { Located RdrName }
1456 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1457 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1458 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1460 qvarsym :: { Located RdrName }
1464 qvarsym_no_minus :: { Located RdrName }
1465 : varsym_no_minus { $1 }
1468 qvarsym1 :: { Located RdrName }
1469 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1471 varsym :: { Located RdrName }
1472 : varsym_no_minus { $1 }
1473 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1475 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1476 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1477 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1480 -- These special_ids are treated as keywords in various places,
1481 -- but as ordinary ids elsewhere. 'special_id' collects all these
1482 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1483 special_id :: { Located FastString }
1485 : 'as' { L1 FSLIT("as") }
1486 | 'qualified' { L1 FSLIT("qualified") }
1487 | 'hiding' { L1 FSLIT("hiding") }
1488 | 'export' { L1 FSLIT("export") }
1489 | 'label' { L1 FSLIT("label") }
1490 | 'dynamic' { L1 FSLIT("dynamic") }
1491 | 'stdcall' { L1 FSLIT("stdcall") }
1492 | 'ccall' { L1 FSLIT("ccall") }
1494 special_sym :: { Located FastString }
1495 special_sym : '!' { L1 FSLIT("!") }
1496 | '.' { L1 FSLIT(".") }
1497 | '*' { L1 FSLIT("*") }
1499 -----------------------------------------------------------------------------
1500 -- Data constructors
1502 qconid :: { Located RdrName } -- Qualified or unqualified
1504 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1506 conid :: { Located RdrName }
1507 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1509 qconsym :: { Located RdrName } -- Qualified or unqualified
1511 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1513 consym :: { Located RdrName }
1514 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1516 -- ':' means only list cons
1517 | ':' { L1 $ consDataCon_RDR }
1520 -----------------------------------------------------------------------------
1523 literal :: { Located HsLit }
1524 : CHAR { L1 $ HsChar $ getCHAR $1 }
1525 | STRING { L1 $ HsString $ getSTRING $1 }
1526 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1527 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1528 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1529 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1530 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1532 -----------------------------------------------------------------------------
1536 : vccurly { () } -- context popped in lexer.
1537 | error {% popContext }
1539 -----------------------------------------------------------------------------
1540 -- Miscellaneous (mostly renamings)
1542 modid :: { Located Module }
1543 : CONID { L1 $ mkModuleFS (getCONID $1) }
1544 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1547 (unpackFS mod ++ '.':unpackFS c))
1551 : commas ',' { $1 + 1 }
1554 -----------------------------------------------------------------------------
1558 happyError = srcParseFail
1560 getVARID (L _ (ITvarid x)) = x
1561 getCONID (L _ (ITconid x)) = x
1562 getVARSYM (L _ (ITvarsym x)) = x
1563 getCONSYM (L _ (ITconsym x)) = x
1564 getQVARID (L _ (ITqvarid x)) = x
1565 getQCONID (L _ (ITqconid x)) = x
1566 getQVARSYM (L _ (ITqvarsym x)) = x
1567 getQCONSYM (L _ (ITqconsym x)) = x
1568 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1569 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1570 getCHAR (L _ (ITchar x)) = x
1571 getSTRING (L _ (ITstring x)) = x
1572 getINTEGER (L _ (ITinteger x)) = x
1573 getRATIONAL (L _ (ITrational x)) = x
1574 getPRIMCHAR (L _ (ITprimchar x)) = x
1575 getPRIMSTRING (L _ (ITprimstring x)) = x
1576 getPRIMINTEGER (L _ (ITprimint x)) = x
1577 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1578 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1579 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1580 getINLINE (L _ (ITinline_prag b)) = b
1581 getSPEC_INLINE (L _ (ITspec_inline_prag b)) = b
1583 -- Utilities for combining source spans
1584 comb2 :: Located a -> Located b -> SrcSpan
1587 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1588 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1590 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1591 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1592 combineSrcSpans (getLoc c) (getLoc d)
1594 -- strict constructor version:
1596 sL :: SrcSpan -> a -> Located a
1597 sL span a = span `seq` L span a
1599 -- Make a source location for the file. We're a bit lazy here and just
1600 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1601 -- try to find the span of the whole file (ToDo).
1602 fileSrcSpan :: P SrcSpan
1605 let loc = mkSrcLoc (srcLocFile l) 1 0;
1606 return (mkSrcSpan loc loc)