2 -- ---------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1997-2003
7 -- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
8 -- ---------------------------------------------------------------------------
11 module Parser ( parseModule, parseStmt, parseIdentifier, parseIface, parseType,
14 #define INCLUDE #include
15 INCLUDE "HsVersions.h"
19 import HscTypes ( ModIface, 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 ( UserFS, varName, dataName, tcClsName, tvName )
29 import DataCon ( DataCon, dataConName )
30 import SrcLoc ( Located(..), unLoc, getLoc, noLoc, combineSrcSpans,
31 SrcSpan, combineLocs, srcLocFile,
34 import CmdLineOpts ( opt_SccProfilingOn )
35 import Type ( Kind, mkArrowKind, liftedTypeKind )
36 import BasicTypes ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
39 import Bag ( emptyBag )
43 import Maybes ( orElse )
49 -----------------------------------------------------------------------------
50 Conflicts: 34 shift/reduce (1.15)
52 10 for abiguity in 'if x then y else z + 1' [State 178]
53 (shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
54 10 because op might be: : - ! * . `x` VARSYM CONSYM QVARSYM QCONSYM
56 1 for ambiguity in 'if x then y else z :: T' [State 178]
57 (shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
59 4 for ambiguity in 'if x then y else z -< e' [State 178]
60 (shift parses as 'if x then y else (z -< T)', as per longest-parse rule)
61 There are four such operators: -<, >-, -<<, >>-
64 2 for ambiguity in 'case v of { x :: T -> T ... } ' [States 11, 253]
65 Which of these two is intended?
67 (x::T) -> T -- Rhs is T
70 (x::T -> T) -> .. -- Rhs is ...
72 8 for ambiguity in 'e :: a `b` c'. Does this mean [States 11, 253]
75 As well as `b` we can have !, QCONSYM, and CONSYM, hence 3 cases
76 Same duplication between states 11 and 253 as the previous case
78 1 for ambiguity in 'let ?x ...' [State 329]
79 the parser can't tell whether the ?x is the lhs of a normal binding or
80 an implicit binding. Fortunately resolving as shift gives it the only
81 sensible meaning, namely the lhs of an implicit binding.
83 1 for ambiguity in '{-# RULES "name" [ ... #-} [State 382]
84 we don't know whether the '[' starts the activation or not: it
85 might be the start of the declaration with the activation being
88 6 for conflicts between `fdecl' and `fdeclDEPRECATED', [States 393,394]
89 which are resolved correctly, and moreover,
90 should go away when `fdeclDEPRECATED' is removed.
92 1 for ambiguity in '{-# RULES "name" forall = ... #-}' [State 474]
93 since 'forall' is a valid variable name, we don't know whether
94 to treat a forall on the input as the beginning of a quantifier
95 or the beginning of the rule itself. Resolving to shift means
96 it's always treated as a quantifier, hence the above is disallowed.
97 This saves explicitly defining a grammar for the rule lhs that
98 doesn't include 'forall'.
100 -- ---------------------------------------------------------------------------
101 -- Adding location info
103 This is done in a stylised way using the three macros below, L0, L1
104 and LL. Each of these macros can be thought of as having type
106 L0, L1, LL :: a -> Located a
108 They each add a SrcSpan to their argument.
110 L0 adds 'noSrcSpan', used for empty productions
112 L1 for a production with a single token on the lhs. Grabs the SrcSpan
115 LL for a production with >1 token on the lhs. Makes up a SrcSpan from
116 the first and last tokens.
118 These suffice for the majority of cases. However, we must be
119 especially careful with empty productions: LL won't work if the first
120 or last token on the lhs can represent an empty span. In these cases,
121 we have to calculate the span using more of the tokens from the lhs, eg.
123 | 'newtype' tycl_hdr '=' newconstr deriving
125 (mkTyData NewType (unLoc $2) [$4] (unLoc $5)) }
127 We provide comb3 and comb4 functions which are useful in such cases.
129 Be careful: there's no checking that you actually got this right, the
130 only symptom will be that the SrcSpans of your syntax will be
134 * We must expand these macros *before* running Happy, which is why this file is
135 * Parser.y.pp rather than just Parser.y - we run the C pre-processor first.
137 #define L0 L noSrcSpan
138 #define L1 sL (getLoc $1)
139 #define LL sL (comb2 $1 $>)
141 -- -----------------------------------------------------------------------------
146 '_' { L _ ITunderscore } -- Haskell keywords
148 'case' { L _ ITcase }
149 'class' { L _ ITclass }
150 'data' { L _ ITdata }
151 'default' { L _ ITdefault }
152 'deriving' { L _ ITderiving }
154 'else' { L _ ITelse }
155 'hiding' { L _ IThiding }
157 'import' { L _ ITimport }
159 'infix' { L _ ITinfix }
160 'infixl' { L _ ITinfixl }
161 'infixr' { L _ ITinfixr }
162 'instance' { L _ ITinstance }
164 'module' { L _ ITmodule }
165 'newtype' { L _ ITnewtype }
167 'qualified' { L _ ITqualified }
168 'then' { L _ ITthen }
169 'type' { L _ ITtype }
170 'where' { L _ ITwhere }
171 '_scc_' { L _ ITscc } -- ToDo: remove
173 'forall' { L _ ITforall } -- GHC extension keywords
174 'foreign' { L _ ITforeign }
175 'export' { L _ ITexport }
176 'label' { L _ ITlabel }
177 'dynamic' { L _ ITdynamic }
178 'safe' { L _ ITsafe }
179 'threadsafe' { L _ ITthreadsafe }
180 'unsafe' { L _ ITunsafe }
182 'stdcall' { L _ ITstdcallconv }
183 'ccall' { L _ ITccallconv }
184 'dotnet' { L _ ITdotnet }
185 'proc' { L _ ITproc } -- for arrow notation extension
186 'rec' { L _ ITrec } -- for arrow notation extension
188 '{-# SPECIALISE' { L _ ITspecialise_prag }
189 '{-# SOURCE' { L _ ITsource_prag }
190 '{-# INLINE' { L _ ITinline_prag }
191 '{-# NOINLINE' { L _ ITnoinline_prag }
192 '{-# RULES' { L _ ITrules_prag }
193 '{-# CORE' { L _ ITcore_prag } -- hdaume: annotated core
194 '{-# SCC' { L _ ITscc_prag }
195 '{-# DEPRECATED' { L _ ITdeprecated_prag }
196 '{-# UNPACK' { L _ ITunpack_prag }
197 '#-}' { L _ ITclose_prag }
199 '..' { L _ ITdotdot } -- reserved symbols
201 '::' { L _ ITdcolon }
205 '<-' { L _ ITlarrow }
206 '->' { L _ ITrarrow }
209 '=>' { L _ ITdarrow }
213 '-<' { L _ ITlarrowtail } -- for arrow notation
214 '>-' { L _ ITrarrowtail } -- for arrow notation
215 '-<<' { L _ ITLarrowtail } -- for arrow notation
216 '>>-' { L _ ITRarrowtail } -- for arrow notation
219 '{' { L _ ITocurly } -- special symbols
221 '{|' { L _ ITocurlybar }
222 '|}' { L _ ITccurlybar }
223 vocurly { L _ ITvocurly } -- virtual open curly (from layout)
224 vccurly { L _ ITvccurly } -- virtual close curly (from layout)
227 '[:' { L _ ITopabrack }
228 ':]' { L _ ITcpabrack }
231 '(#' { L _ IToubxparen }
232 '#)' { L _ ITcubxparen }
233 '(|' { L _ IToparenbar }
234 '|)' { L _ ITcparenbar }
237 '`' { L _ ITbackquote }
239 VARID { L _ (ITvarid _) } -- identifiers
240 CONID { L _ (ITconid _) }
241 VARSYM { L _ (ITvarsym _) }
242 CONSYM { L _ (ITconsym _) }
243 QVARID { L _ (ITqvarid _) }
244 QCONID { L _ (ITqconid _) }
245 QVARSYM { L _ (ITqvarsym _) }
246 QCONSYM { L _ (ITqconsym _) }
248 IPDUPVARID { L _ (ITdupipvarid _) } -- GHC extension
249 IPSPLITVARID { L _ (ITsplitipvarid _) } -- GHC extension
251 CHAR { L _ (ITchar _) }
252 STRING { L _ (ITstring _) }
253 INTEGER { L _ (ITinteger _) }
254 RATIONAL { L _ (ITrational _) }
256 PRIMCHAR { L _ (ITprimchar _) }
257 PRIMSTRING { L _ (ITprimstring _) }
258 PRIMINTEGER { L _ (ITprimint _) }
259 PRIMFLOAT { L _ (ITprimfloat _) }
260 PRIMDOUBLE { L _ (ITprimdouble _) }
263 '[|' { L _ ITopenExpQuote }
264 '[p|' { L _ ITopenPatQuote }
265 '[t|' { L _ ITopenTypQuote }
266 '[d|' { L _ ITopenDecQuote }
267 '|]' { L _ ITcloseQuote }
268 TH_ID_SPLICE { L _ (ITidEscape _) } -- $x
269 '$(' { L _ ITparenEscape } -- $( exp )
270 TH_VAR_QUOTE { L _ ITvarQuote } -- 'x
271 TH_TY_QUOTE { L _ ITtyQuote } -- ''T
273 %monad { P } { >>= } { return }
274 %lexer { lexer } { L _ ITeof }
275 %name parseModule module
276 %name parseStmt maybe_stmt
277 %name parseIdentifier identifier
278 %name parseIface iface
279 %name parseType ctype
280 %partial parseHeader header
281 %tokentype { Located Token }
284 -----------------------------------------------------------------------------
287 -- The place for module deprecation is really too restrictive, but if it
288 -- was allowed at its natural place just before 'module', we get an ugly
289 -- s/r conflict with the second alternative. Another solution would be the
290 -- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
291 -- either, and DEPRECATED is only expected to be used by people who really
292 -- know what they are doing. :-)
294 module :: { Located (HsModule RdrName) }
295 : 'module' modid maybemoddeprec maybeexports 'where' body
296 {% fileSrcSpan >>= \ loc ->
297 return (L loc (HsModule (Just $2) $4 (fst $6) (snd $6) $3)) }
298 | missing_module_keyword top close
299 {% fileSrcSpan >>= \ loc ->
300 return (L loc (HsModule Nothing Nothing
301 (fst $2) (snd $2) Nothing)) }
303 missing_module_keyword :: { () }
304 : {- empty -} {% pushCurrentContext }
306 maybemoddeprec :: { Maybe DeprecTxt }
307 : '{-# DEPRECATED' STRING '#-}' { Just (getSTRING $2) }
308 | {- empty -} { Nothing }
310 body :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
312 | vocurly top close { $2 }
314 top :: { ([LImportDecl RdrName], [LHsDecl RdrName]) }
315 : importdecls { (reverse $1,[]) }
316 | importdecls ';' cvtopdecls { (reverse $1,$3) }
317 | cvtopdecls { ([],$1) }
319 cvtopdecls :: { [LHsDecl RdrName] }
320 : topdecls { cvTopDecls $1 }
322 -----------------------------------------------------------------------------
323 -- Module declaration & imports only
325 header :: { Located (HsModule RdrName) }
326 : 'module' modid maybemoddeprec maybeexports 'where' header_body
327 {% fileSrcSpan >>= \ loc ->
328 return (L loc (HsModule (Just $2) $4 $6 [] $3)) }
329 | missing_module_keyword importdecls
330 {% fileSrcSpan >>= \ loc ->
331 return (L loc (HsModule Nothing Nothing $2 [] Nothing)) }
333 header_body :: { [LImportDecl RdrName] }
334 : '{' importdecls { $2 }
335 | vocurly importdecls { $2 }
337 -----------------------------------------------------------------------------
338 -- Interfaces (.hi-boot files)
340 iface :: { ModIface }
341 : 'module' modid 'where' ifacebody { mkBootIface (unLoc $2) $4 }
343 ifacebody :: { [HsDecl RdrName] }
344 : '{' ifacedecls '}' { $2 }
345 | vocurly ifacedecls close { $2 }
347 ifacedecls :: { [HsDecl RdrName] }
348 : ifacedecl ';' ifacedecls { $1 : $3 }
349 | ';' ifacedecls { $2 }
353 ifacedecl :: { HsDecl RdrName }
356 | 'type' syn_hdr '=' ctype
357 { let (tc,tvs) = $2 in TyClD (TySynonym tc tvs $4) }
358 | 'data' tycl_hdr constrs -- No deriving in hi-boot
359 { TyClD (mkTyData DataType $2 Nothing (reverse (unLoc $3)) Nothing) }
360 | 'data' tycl_hdr 'where' gadt_constrlist
361 { TyClD (mkTyData DataType $2 Nothing (reverse (unLoc $4)) Nothing) }
362 | 'newtype' tycl_hdr -- Constructor is optional
363 { TyClD (mkTyData NewType $2 Nothing [] Nothing) }
364 | 'newtype' tycl_hdr '=' newconstr
365 { TyClD (mkTyData NewType $2 Nothing [$4] Nothing) }
366 | 'class' tycl_hdr fds
367 { TyClD (mkClassDecl (unLoc $2) (unLoc $3) [] emptyBag) }
369 -----------------------------------------------------------------------------
372 maybeexports :: { Maybe [LIE RdrName] }
373 : '(' exportlist ')' { Just $2 }
374 | {- empty -} { Nothing }
376 exportlist :: { [LIE RdrName] }
377 : exportlist ',' export { $3 : $1 }
378 | exportlist ',' { $1 }
382 -- No longer allow things like [] and (,,,) to be exported
383 -- They are built in syntax, always available
384 export :: { LIE RdrName }
385 : qvar { L1 (IEVar (unLoc $1)) }
386 | oqtycon { L1 (IEThingAbs (unLoc $1)) }
387 | oqtycon '(' '..' ')' { LL (IEThingAll (unLoc $1)) }
388 | oqtycon '(' ')' { LL (IEThingWith (unLoc $1) []) }
389 | oqtycon '(' qcnames ')' { LL (IEThingWith (unLoc $1) (reverse $3)) }
390 | 'module' modid { LL (IEModuleContents (unLoc $2)) }
392 qcnames :: { [RdrName] }
393 : qcnames ',' qcname { unLoc $3 : $1 }
394 | qcname { [unLoc $1] }
396 qcname :: { Located RdrName } -- Variable or data constructor
400 -----------------------------------------------------------------------------
401 -- Import Declarations
403 -- import decls can be *empty*, or even just a string of semicolons
404 -- whereas topdecls must contain at least one topdecl.
406 importdecls :: { [LImportDecl RdrName] }
407 : importdecls ';' importdecl { $3 : $1 }
408 | importdecls ';' { $1 }
409 | importdecl { [ $1 ] }
412 importdecl :: { LImportDecl RdrName }
413 : 'import' maybe_src optqualified modid maybeas maybeimpspec
414 { L (comb4 $1 $4 $5 $6) (ImportDecl $4 $2 $3 (unLoc $5) (unLoc $6)) }
416 maybe_src :: { IsBootInterface }
417 : '{-# SOURCE' '#-}' { True }
418 | {- empty -} { False }
420 optqualified :: { Bool }
421 : 'qualified' { True }
422 | {- empty -} { False }
424 maybeas :: { Located (Maybe Module) }
425 : 'as' modid { LL (Just (unLoc $2)) }
426 | {- empty -} { noLoc Nothing }
428 maybeimpspec :: { Located (Maybe (Bool, [LIE RdrName])) }
429 : impspec { L1 (Just (unLoc $1)) }
430 | {- empty -} { noLoc Nothing }
432 impspec :: { Located (Bool, [LIE RdrName]) }
433 : '(' exportlist ')' { LL (False, reverse $2) }
434 | 'hiding' '(' exportlist ')' { LL (True, reverse $3) }
436 -----------------------------------------------------------------------------
437 -- Fixity Declarations
441 | INTEGER {% checkPrecP (L1 (fromInteger (getINTEGER $1))) }
443 infix :: { Located FixityDirection }
444 : 'infix' { L1 InfixN }
445 | 'infixl' { L1 InfixL }
446 | 'infixr' { L1 InfixR }
448 ops :: { Located [Located RdrName] }
449 : ops ',' op { LL ($3 : unLoc $1) }
452 -----------------------------------------------------------------------------
453 -- Top-Level Declarations
455 topdecls :: { OrdList (LHsDecl RdrName) } -- Reversed
456 : topdecls ';' topdecl { $1 `appOL` $3 }
457 | topdecls ';' { $1 }
460 topdecl :: { OrdList (LHsDecl RdrName) }
461 : tycl_decl { unitOL (L1 (TyClD (unLoc $1))) }
462 | 'instance' inst_type where
463 { let (binds,sigs) = cvBindsAndSigs (unLoc $3)
464 in unitOL (L (comb3 $1 $2 $3) (InstD (InstDecl $2 binds sigs))) }
465 | 'default' '(' comma_types0 ')' { unitOL (LL $ DefD (DefaultDecl $3)) }
466 | 'foreign' fdecl { unitOL (LL (unLoc $2)) }
467 | '{-# DEPRECATED' deprecations '#-}' { $2 }
468 | '{-# RULES' rules '#-}' { $2 }
469 | '$(' exp ')' { unitOL (LL $ SpliceD (SpliceDecl $2)) }
472 tycl_decl :: { LTyClDecl RdrName }
473 : 'type' syn_hdr '=' ctype
474 -- Note ctype, not sigtype.
475 -- We allow an explicit for-all but we don't insert one
476 -- in type Foo a = (b,b)
477 -- Instead we just say b is out of scope
478 { LL $ let (tc,tvs) = $2 in TySynonym tc tvs $4 }
480 | 'data' tycl_hdr constrs deriving
481 { L (comb4 $1 $2 $3 $4)
482 (mkTyData DataType $2 Nothing (reverse (unLoc $3)) (unLoc $4)) }
484 | 'data' tycl_hdr opt_kind_sig 'where' gadt_constrlist -- No deriving for GADTs
485 { L (comb4 $1 $2 $4 $5)
486 (mkTyData DataType $2 $3 (reverse (unLoc $5)) Nothing) }
488 | 'newtype' tycl_hdr '=' newconstr deriving
490 (mkTyData NewType $2 Nothing [$4] (unLoc $5)) }
492 | 'class' tycl_hdr fds where
494 (binds,sigs) = cvBindsAndSigs (unLoc $4)
496 L (comb4 $1 $2 $3 $4) (mkClassDecl (unLoc $2) (unLoc $3) sigs
499 opt_kind_sig :: { Maybe Kind }
501 | '::' kind { Just $2 }
503 syn_hdr :: { (Located RdrName, [LHsTyVarBndr RdrName]) }
504 -- We don't retain the syntax of an infix
505 -- type synonym declaration. Oh well.
506 : tycon tv_bndrs { ($1, $2) }
507 | tv_bndr tyconop tv_bndr { ($2, [$1,$3]) }
509 -- tycl_hdr parses the header of a type or class decl,
510 -- which takes the form
513 -- (Eq a, Ord b) => T a b
514 -- Rather a lot of inlining here, else we get reduce/reduce errors
515 tycl_hdr :: { Located (LHsContext RdrName, Located RdrName, [LHsTyVarBndr RdrName]) }
516 : context '=>' type {% checkTyClHdr $1 $3 >>= return.LL }
517 | type {% checkTyClHdr (noLoc []) $1 >>= return.L1 }
519 -----------------------------------------------------------------------------
520 -- Nested declarations
522 decls :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
523 : decls ';' decl { LL (unLoc $1 `appOL` unLoc $3) }
524 | decls ';' { LL (unLoc $1) }
526 | {- empty -} { noLoc nilOL }
529 decllist :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
530 : '{' decls '}' { LL (unLoc $2) }
531 | vocurly decls close { $2 }
533 where :: { Located (OrdList (LHsDecl RdrName)) } -- Reversed
534 -- No implicit parameters
535 : 'where' decllist { LL (unLoc $2) }
536 | {- empty -} { noLoc nilOL }
538 binds :: { Located [HsBindGroup RdrName] } -- May have implicit parameters
539 : decllist { L1 [cvBindGroup (unLoc $1)] }
540 | '{' dbinds '}' { LL [HsIPBinds (unLoc $2)] }
541 | vocurly dbinds close { L (getLoc $2) [HsIPBinds (unLoc $2)] }
543 wherebinds :: { Located [HsBindGroup RdrName] } -- May have implicit parameters
544 : 'where' binds { LL (unLoc $2) }
545 | {- empty -} { noLoc [] }
548 -----------------------------------------------------------------------------
549 -- Transformation Rules
551 rules :: { OrdList (LHsDecl RdrName) } -- Reversed
552 : rules ';' rule { $1 `snocOL` $3 }
555 | {- empty -} { nilOL }
557 rule :: { LHsDecl RdrName }
558 : STRING activation rule_forall infixexp '=' exp
559 { LL $ RuleD (HsRule (getSTRING $1) $2 $3 $4 $6) }
561 activation :: { Activation } -- Omitted means AlwaysActive
562 : {- empty -} { AlwaysActive }
563 | explicit_activation { $1 }
565 inverse_activation :: { Activation } -- Omitted means NeverActive
566 : {- empty -} { NeverActive }
567 | explicit_activation { $1 }
569 explicit_activation :: { Activation } -- In brackets
570 : '[' INTEGER ']' { ActiveAfter (fromInteger (getINTEGER $2)) }
571 | '[' '~' INTEGER ']' { ActiveBefore (fromInteger (getINTEGER $3)) }
573 rule_forall :: { [RuleBndr RdrName] }
574 : 'forall' rule_var_list '.' { $2 }
577 rule_var_list :: { [RuleBndr RdrName] }
579 | rule_var rule_var_list { $1 : $2 }
581 rule_var :: { RuleBndr RdrName }
582 : varid { RuleBndr $1 }
583 | '(' varid '::' ctype ')' { RuleBndrSig $2 $4 }
585 -----------------------------------------------------------------------------
586 -- Deprecations (c.f. rules)
588 deprecations :: { OrdList (LHsDecl RdrName) } -- Reversed
589 : deprecations ';' deprecation { $1 `appOL` $3 }
590 | deprecations ';' { $1 }
592 | {- empty -} { nilOL }
594 -- SUP: TEMPORARY HACK, not checking for `module Foo'
595 deprecation :: { OrdList (LHsDecl RdrName) }
597 { toOL [ LL $ DeprecD (Deprecation n (getSTRING $2))
601 -----------------------------------------------------------------------------
602 -- Foreign import and export declarations
604 -- for the time being, the following accepts foreign declarations conforming
605 -- to the FFI Addendum, Version 1.0 as well as pre-standard declarations
607 -- * a flag indicates whether pre-standard declarations have been used and
608 -- triggers a deprecation warning further down the road
610 -- NB: The first two rules could be combined into one by replacing `safety1'
611 -- with `safety'. However, the combined rule conflicts with the
614 fdecl :: { LHsDecl RdrName }
615 fdecl : 'import' callconv safety1 fspec
616 {% mkImport $2 $3 (unLoc $4) >>= return.LL }
617 | 'import' callconv fspec
618 {% do { d <- mkImport $2 (PlaySafe False) (unLoc $3);
620 | 'export' callconv fspec
621 {% mkExport $2 (unLoc $3) >>= return.LL }
622 -- the following syntax is DEPRECATED
623 | fdecl1DEPRECATED { L1 (ForD (unLoc $1)) }
624 | fdecl2DEPRECATED { L1 (unLoc $1) }
626 fdecl1DEPRECATED :: { LForeignDecl RdrName }
628 ----------- DEPRECATED label decls ------------
629 : 'label' ext_name varid '::' sigtype
630 { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS
631 (CLabel ($2 `orElse` mkExtName (unLoc $3)))) True }
633 ----------- DEPRECATED ccall/stdcall decls ------------
635 -- NB: This business with the case expression below may seem overly
636 -- complicated, but it is necessary to avoid some conflicts.
638 -- DEPRECATED variant #1: lack of a calling convention specification
640 | 'import' {-no callconv-} ext_name safety varid_no_unsafe '::' sigtype
642 target = StaticTarget ($2 `orElse` mkExtName (unLoc $4))
644 LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS
645 (CFunction target)) True }
647 -- DEPRECATED variant #2: external name consists of two separate strings
648 -- (module name and function name) (import)
649 | 'import' callconv STRING STRING safety varid_no_unsafe '::' sigtype
651 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
652 CCall cconv -> return $
654 imp = CFunction (StaticTarget (getSTRING $4))
656 LL $ ForeignImport $6 $8 (CImport cconv $5 nilFS nilFS imp) True }
658 -- DEPRECATED variant #3: `unsafe' after entity
659 | 'import' callconv STRING 'unsafe' varid_no_unsafe '::' sigtype
661 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
662 CCall cconv -> return $
664 imp = CFunction (StaticTarget (getSTRING $3))
666 LL $ ForeignImport $5 $7 (CImport cconv PlayRisky nilFS nilFS imp) True }
668 -- DEPRECATED variant #4: use of the special identifier `dynamic' without
669 -- an explicit calling convention (import)
670 | 'import' {-no callconv-} 'dynamic' safety varid_no_unsafe '::' sigtype
671 { LL $ ForeignImport $4 $6 (CImport defaultCCallConv $3 nilFS nilFS
672 (CFunction DynamicTarget)) True }
674 -- DEPRECATED variant #5: use of the special identifier `dynamic' (import)
675 | 'import' callconv 'dynamic' safety varid_no_unsafe '::' sigtype
677 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
678 CCall cconv -> return $
679 LL $ ForeignImport $5 $7 (CImport cconv $4 nilFS nilFS
680 (CFunction DynamicTarget)) True }
682 -- DEPRECATED variant #6: lack of a calling convention specification
684 | 'export' {-no callconv-} ext_name varid '::' sigtype
685 { LL $ ForeignExport $3 $5 (CExport (CExportStatic ($2 `orElse` mkExtName (unLoc $3))
686 defaultCCallConv)) True }
688 -- DEPRECATED variant #7: external name consists of two separate strings
689 -- (module name and function name) (export)
690 | 'export' callconv STRING STRING varid '::' sigtype
692 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
693 CCall cconv -> return $
694 LL $ ForeignExport $5 $7
695 (CExport (CExportStatic (getSTRING $4) cconv)) True }
697 -- DEPRECATED variant #8: use of the special identifier `dynamic' without
698 -- an explicit calling convention (export)
699 | 'export' {-no callconv-} 'dynamic' varid '::' sigtype
700 { LL $ ForeignImport $3 $5 (CImport defaultCCallConv (PlaySafe False) nilFS nilFS
703 -- DEPRECATED variant #9: use of the special identifier `dynamic' (export)
704 | 'export' callconv 'dynamic' varid '::' sigtype
706 DNCall -> parseError (comb2 $1 $>) "Illegal format of .NET foreign import"
707 CCall cconv -> return $
708 LL $ ForeignImport $4 $6
709 (CImport cconv (PlaySafe False) nilFS nilFS CWrapper) True }
711 ----------- DEPRECATED .NET decls ------------
712 -- NB: removed the .NET call declaration, as it is entirely subsumed
713 -- by the new standard FFI declarations
715 fdecl2DEPRECATED :: { LHsDecl RdrName }
717 : 'import' 'dotnet' 'type' ext_name tycon { LL $ TyClD (ForeignType $5 $4 DNType) }
718 -- left this one unchanged for the moment as type imports are not
719 -- covered currently by the FFI standard -=chak
722 callconv :: { CallConv }
723 : 'stdcall' { CCall StdCallConv }
724 | 'ccall' { CCall CCallConv }
725 | 'dotnet' { DNCall }
728 : 'unsafe' { PlayRisky }
729 | 'safe' { PlaySafe False }
730 | 'threadsafe' { PlaySafe True }
731 | {- empty -} { PlaySafe False }
733 safety1 :: { Safety }
734 : 'unsafe' { PlayRisky }
735 | 'safe' { PlaySafe False }
736 | 'threadsafe' { PlaySafe True }
737 -- only needed to avoid conflicts with the DEPRECATED rules
739 fspec :: { Located (Located FastString, Located RdrName, LHsType RdrName) }
740 : STRING var '::' sigtype { LL (L (getLoc $1) (getSTRING $1), $2, $4) }
741 | var '::' sigtype { LL (noLoc nilFS, $1, $3) }
742 -- if the entity string is missing, it defaults to the empty string;
743 -- the meaning of an empty entity string depends on the calling
747 ext_name :: { Maybe CLabelString }
748 : STRING { Just (getSTRING $1) }
749 | STRING STRING { Just (getSTRING $2) } -- Ignore "module name" for now
750 | {- empty -} { Nothing }
753 -----------------------------------------------------------------------------
756 opt_sig :: { Maybe (LHsType RdrName) }
757 : {- empty -} { Nothing }
758 | '::' sigtype { Just $2 }
760 opt_asig :: { Maybe (LHsType RdrName) }
761 : {- empty -} { Nothing }
762 | '::' atype { Just $2 }
764 sigtypes1 :: { [LHsType RdrName] }
766 | sigtype ',' sigtypes1 { $1 : $3 }
768 sigtype :: { LHsType RdrName }
769 : ctype { L1 (mkImplicitHsForAllTy (noLoc []) $1) }
770 -- Wrap an Implicit forall if there isn't one there already
772 sig_vars :: { Located [Located RdrName] }
773 : sig_vars ',' var { LL ($3 : unLoc $1) }
776 -----------------------------------------------------------------------------
779 strict_mark :: { Located HsBang }
780 : '!' { L1 HsStrict }
781 | '{-# UNPACK' '#-}' '!' { LL HsUnbox }
783 -- A ctype is a for-all type
784 ctype :: { LHsType RdrName }
785 : 'forall' tv_bndrs '.' ctype { LL $ mkExplicitHsForAllTy $2 (noLoc []) $4 }
786 | context '=>' type { LL $ mkImplicitHsForAllTy $1 $3 }
787 -- A type of form (context => type) is an *implicit* HsForAllTy
790 -- We parse a context as a btype so that we don't get reduce/reduce
791 -- errors in ctype. The basic problem is that
793 -- looks so much like a tuple type. We can't tell until we find the =>
794 context :: { LHsContext RdrName }
795 : btype {% checkContext $1 }
797 type :: { LHsType RdrName }
798 : ipvar '::' gentype { LL (HsPredTy (HsIParam (unLoc $1) $3)) }
801 gentype :: { LHsType RdrName }
803 | btype qtyconop gentype { LL $ HsOpTy $1 $2 $3 }
804 | btype '`' tyvar '`' gentype { LL $ HsOpTy $1 $3 $5 }
805 | btype '->' gentype { LL $ HsFunTy $1 $3 }
807 btype :: { LHsType RdrName }
808 : btype atype { LL $ HsAppTy $1 $2 }
811 atype :: { LHsType RdrName }
812 : gtycon { L1 (HsTyVar (unLoc $1)) }
813 | tyvar { L1 (HsTyVar (unLoc $1)) }
814 | strict_mark atype { LL (HsBangTy (unLoc $1) $2) }
815 | '(' type ',' comma_types1 ')' { LL $ HsTupleTy Boxed ($2:$4) }
816 | '(#' comma_types1 '#)' { LL $ HsTupleTy Unboxed $2 }
817 | '[' type ']' { LL $ HsListTy $2 }
818 | '[:' type ':]' { LL $ HsPArrTy $2 }
819 | '(' ctype ')' { LL $ HsParTy $2 }
820 | '(' ctype '::' kind ')' { LL $ HsKindSig $2 $4 }
822 | INTEGER { L1 (HsNumTy (getINTEGER $1)) }
824 -- An inst_type is what occurs in the head of an instance decl
825 -- e.g. (Foo a, Gaz b) => Wibble a b
826 -- It's kept as a single type, with a MonoDictTy at the right
827 -- hand corner, for convenience.
828 inst_type :: { LHsType RdrName }
829 : sigtype {% checkInstType $1 }
831 inst_types1 :: { [LHsType RdrName] }
833 | inst_type ',' inst_types1 { $1 : $3 }
835 comma_types0 :: { [LHsType RdrName] }
836 : comma_types1 { $1 }
839 comma_types1 :: { [LHsType RdrName] }
841 | type ',' comma_types1 { $1 : $3 }
843 tv_bndrs :: { [LHsTyVarBndr RdrName] }
844 : tv_bndr tv_bndrs { $1 : $2 }
847 tv_bndr :: { LHsTyVarBndr RdrName }
848 : tyvar { L1 (UserTyVar (unLoc $1)) }
849 | '(' tyvar '::' kind ')' { LL (KindedTyVar (unLoc $2) $4) }
851 fds :: { Located [Located ([RdrName], [RdrName])] }
852 : {- empty -} { noLoc [] }
853 | '|' fds1 { LL (reverse (unLoc $2)) }
855 fds1 :: { Located [Located ([RdrName], [RdrName])] }
856 : fds1 ',' fd { LL ($3 : unLoc $1) }
859 fd :: { Located ([RdrName], [RdrName]) }
860 : varids0 '->' varids0 { L (comb3 $1 $2 $3)
861 (reverse (unLoc $1), reverse (unLoc $3)) }
863 varids0 :: { Located [RdrName] }
864 : {- empty -} { noLoc [] }
865 | varids0 tyvar { LL (unLoc $2 : unLoc $1) }
867 -----------------------------------------------------------------------------
872 | akind '->' kind { mkArrowKind $1 $3 }
875 : '*' { liftedTypeKind }
876 | '(' kind ')' { $2 }
879 -----------------------------------------------------------------------------
880 -- Datatype declarations
882 newconstr :: { LConDecl RdrName }
883 : conid atype { LL $ ConDecl $1 [] (noLoc []) (PrefixCon [$2]) }
884 | conid '{' var '::' ctype '}'
885 { LL $ ConDecl $1 [] (noLoc []) (RecCon [($3, $5)]) }
887 gadt_constrlist :: { Located [LConDecl RdrName] }
888 : '{' gadt_constrs '}' { LL (unLoc $2) }
889 | vocurly gadt_constrs close { $2 }
891 gadt_constrs :: { Located [LConDecl RdrName] }
892 : gadt_constrs ';' gadt_constr { LL ($3 : unLoc $1) }
893 | gadt_constrs ';' { $1 }
894 | gadt_constr { L1 [$1] }
896 gadt_constr :: { LConDecl RdrName }
898 { LL (GadtDecl $1 $3) }
900 constrs :: { Located [LConDecl RdrName] }
901 : {- empty; a GHC extension -} { noLoc [] }
902 | '=' constrs1 { LL (unLoc $2) }
904 constrs1 :: { Located [LConDecl RdrName] }
905 : constrs1 '|' constr { LL ($3 : unLoc $1) }
908 constr :: { LConDecl RdrName }
909 : forall context '=>' constr_stuff
910 { let (con,details) = unLoc $4 in
911 LL (ConDecl con (unLoc $1) $2 details) }
912 | forall constr_stuff
913 { let (con,details) = unLoc $2 in
914 LL (ConDecl con (unLoc $1) (noLoc []) details) }
916 forall :: { Located [LHsTyVarBndr RdrName] }
917 : 'forall' tv_bndrs '.' { LL $2 }
918 | {- empty -} { noLoc [] }
920 constr_stuff :: { Located (Located RdrName, HsConDetails RdrName (LBangType RdrName)) }
921 -- We parse the constructor declaration
923 -- as a btype (treating C as a type constructor) and then convert C to be
924 -- a data constructor. Reason: it might continue like this:
926 -- in which case C really would be a type constructor. We can't resolve this
927 -- ambiguity till we come across the constructor oprerator :% (or not, more usually)
928 : btype {% mkPrefixCon $1 [] >>= return.LL }
929 | oqtycon '{' '}' {% mkRecCon $1 [] >>= return.LL }
930 | oqtycon '{' fielddecls '}' {% mkRecCon $1 $3 >>= return.LL }
931 | btype conop btype { LL ($2, InfixCon $1 $3) }
933 fielddecls :: { [([Located RdrName], LBangType RdrName)] }
934 : fielddecl ',' fielddecls { unLoc $1 : $3 }
935 | fielddecl { [unLoc $1] }
937 fielddecl :: { Located ([Located RdrName], LBangType RdrName) }
938 : sig_vars '::' ctype { LL (reverse (unLoc $1), $3) }
940 -- We allow the odd-looking 'inst_type' in a deriving clause, so that
941 -- we can do deriving( forall a. C [a] ) in a newtype (GHC extension).
942 -- The 'C [a]' part is converted to an HsPredTy by checkInstType
943 -- We don't allow a context, but that's sorted out by the type checker.
944 deriving :: { Located (Maybe [LHsType RdrName]) }
945 : {- empty -} { noLoc Nothing }
946 | 'deriving' qtycon {% do { let { L loc tv = $2 }
947 ; p <- checkInstType (L loc (HsTyVar tv))
948 ; return (LL (Just [p])) } }
949 | 'deriving' '(' ')' { LL (Just []) }
950 | 'deriving' '(' inst_types1 ')' { LL (Just $3) }
951 -- Glasgow extension: allow partial
952 -- applications in derivings
954 -----------------------------------------------------------------------------
957 {- There's an awkward overlap with a type signature. Consider
958 f :: Int -> Int = ...rhs...
959 Then we can't tell whether it's a type signature or a value
960 definition with a result signature until we see the '='.
961 So we have to inline enough to postpone reductions until we know.
965 ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is var
966 instead of qvar, we get another shift/reduce-conflict. Consider the
969 { (^^) :: Int->Int ; } Type signature; only var allowed
971 { (^^) :: Int->Int = ... ; } Value defn with result signature;
972 qvar allowed (because of instance decls)
974 We can't tell whether to reduce var to qvar until after we've read the signatures.
977 decl :: { Located (OrdList (LHsDecl RdrName)) }
979 | infixexp opt_sig rhs {% do { r <- checkValDef $1 $2 $3;
980 return (LL $ unitOL (LL $ ValD r)) } }
982 rhs :: { Located (GRHSs RdrName) }
983 : '=' exp wherebinds { L (comb3 $1 $2 $3) $ GRHSs (unguardedRHS $2) (unLoc $3) }
984 | gdrhs wherebinds { LL $ GRHSs (reverse (unLoc $1)) (unLoc $2) }
986 gdrhs :: { Located [LGRHS RdrName] }
987 : gdrhs gdrh { LL ($2 : unLoc $1) }
990 gdrh :: { LGRHS RdrName }
991 : '|' quals '=' exp { LL $ GRHS (reverse (L (getLoc $4) (ResultStmt $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 (Sig 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 True $3 $2)) }
1005 | '{-# NOINLINE' inverse_activation qvar '#-}'
1006 { LL $ unitOL (LL $ SigD (InlineSig False $3 $2)) }
1007 | '{-# SPECIALISE' qvar '::' sigtypes1 '#-}'
1008 { LL $ toOL [ LL $ SigD (SpecSig $2 t)
1010 | '{-# SPECIALISE' 'instance' inst_type '#-}'
1011 { LL $ unitOL (LL $ SigD (SpecInstSig $3)) }
1013 -----------------------------------------------------------------------------
1016 exp :: { LHsExpr RdrName }
1017 : infixexp '::' sigtype { LL $ ExprWithTySig $1 $3 }
1018 | infixexp '-<' exp { LL $ HsArrApp $1 $3 placeHolderType HsFirstOrderApp True }
1019 | infixexp '>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsFirstOrderApp False }
1020 | infixexp '-<<' exp { LL $ HsArrApp $1 $3 placeHolderType HsHigherOrderApp True }
1021 | infixexp '>>-' exp { LL $ HsArrApp $3 $1 placeHolderType HsHigherOrderApp False}
1024 infixexp :: { LHsExpr RdrName }
1026 | infixexp qop exp10 { LL (OpApp $1 $2 (panic "fixity") $3) }
1028 exp10 :: { LHsExpr RdrName }
1029 : '\\' aexp aexps opt_asig '->' exp
1030 {% checkPatterns ($2 : reverse $3) >>= \ ps ->
1031 return (LL $ HsLam (mkMatchGroup [LL $ Match ps $4
1032 (GRHSs (unguardedRHS $6) []
1034 | 'let' binds 'in' exp { LL $ HsLet (unLoc $2) $4 }
1035 | 'if' exp 'then' exp 'else' exp { LL $ HsIf $2 $4 $6 }
1036 | 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
1037 | '-' fexp { LL $ mkHsNegApp $2 }
1039 | 'do' stmtlist {% let loc = comb2 $1 $2 in
1040 checkDo loc (unLoc $2) >>= \ stmts ->
1041 return (L loc (mkHsDo DoExpr stmts)) }
1042 | 'mdo' stmtlist {% let loc = comb2 $1 $2 in
1043 checkMDo loc (unLoc $2) >>= \ stmts ->
1044 return (L loc (mkHsDo MDoExpr stmts)) }
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 }
1077 aexp1 :: { LHsExpr RdrName }
1078 : aexp1 '{' fbinds '}' {% do { r <- mkRecConstrOrUpdate $1 (comb2 $2 $4)
1083 -- Here was the syntax for type applications that I was planning
1084 -- but there are difficulties (e.g. what order for type args)
1085 -- so it's not enabled yet.
1086 -- But this case *is* used for the left hand side of a generic definition,
1087 -- which is parsed as an expression before being munged into a pattern
1088 | qcname '{|' gentype '|}' { LL $ HsApp (sL (getLoc $1) (HsVar (unLoc $1)))
1089 (sL (getLoc $3) (HsType $3)) }
1091 aexp2 :: { LHsExpr RdrName }
1092 : ipvar { L1 (HsIPVar $! unLoc $1) }
1093 | qcname { L1 (HsVar $! unLoc $1) }
1094 | literal { L1 (HsLit $! unLoc $1) }
1095 | INTEGER { L1 (HsOverLit $! mkHsIntegral (getINTEGER $1)) }
1096 | RATIONAL { L1 (HsOverLit $! mkHsFractional (getRATIONAL $1)) }
1097 | '(' exp ')' { LL (HsPar $2) }
1098 | '(' exp ',' texps ')' { LL $ ExplicitTuple ($2 : reverse $4) Boxed }
1099 | '(#' texps '#)' { LL $ ExplicitTuple (reverse $2) Unboxed }
1100 | '[' list ']' { LL (unLoc $2) }
1101 | '[:' parr ':]' { LL (unLoc $2) }
1102 | '(' infixexp qop ')' { LL $ SectionL $2 $3 }
1103 | '(' qopm infixexp ')' { LL $ SectionR $2 $3 }
1104 | '_' { L1 EWildPat }
1106 -- MetaHaskell Extension
1107 | TH_ID_SPLICE { L1 $ HsSpliceE (mkHsSplice
1108 (L1 $ HsVar (mkUnqual varName
1109 (getTH_ID_SPLICE $1)))) } -- $x
1110 | '$(' exp ')' { LL $ HsSpliceE (mkHsSplice $2) } -- $( exp )
1112 | TH_VAR_QUOTE qvar { LL $ HsBracket (VarBr (unLoc $2)) }
1113 | TH_VAR_QUOTE gcon { LL $ HsBracket (VarBr (unLoc $2)) }
1114 | TH_TY_QUOTE tyvar { LL $ HsBracket (VarBr (unLoc $2)) }
1115 | TH_TY_QUOTE gtycon { LL $ HsBracket (VarBr (unLoc $2)) }
1116 | '[|' exp '|]' { LL $ HsBracket (ExpBr $2) }
1117 | '[t|' ctype '|]' { LL $ HsBracket (TypBr $2) }
1118 | '[p|' infixexp '|]' {% checkPattern $2 >>= \p ->
1119 return (LL $ HsBracket (PatBr p)) }
1120 | '[d|' cvtopbody '|]' { LL $ HsBracket (DecBr (mkGroup $2)) }
1122 -- arrow notation extension
1123 | '(|' aexp2 cmdargs '|)' { LL $ HsArrForm $2 Nothing (reverse $3) }
1125 cmdargs :: { [LHsCmdTop RdrName] }
1126 : cmdargs acmd { $2 : $1 }
1127 | {- empty -} { [] }
1129 acmd :: { LHsCmdTop RdrName }
1130 : aexp2 { L1 $ HsCmdTop $1 [] placeHolderType undefined }
1132 cvtopbody :: { [LHsDecl RdrName] }
1133 : '{' cvtopdecls0 '}' { $2 }
1134 | vocurly cvtopdecls0 close { $2 }
1136 cvtopdecls0 :: { [LHsDecl RdrName] }
1137 : {- empty -} { [] }
1140 texps :: { [LHsExpr RdrName] }
1141 : texps ',' exp { $3 : $1 }
1145 -----------------------------------------------------------------------------
1148 -- The rules below are little bit contorted to keep lexps left-recursive while
1149 -- avoiding another shift/reduce-conflict.
1151 list :: { LHsExpr RdrName }
1152 : exp { L1 $ ExplicitList placeHolderType [$1] }
1153 | lexps { L1 $ ExplicitList placeHolderType (reverse (unLoc $1)) }
1154 | exp '..' { LL $ ArithSeqIn (From $1) }
1155 | exp ',' exp '..' { LL $ ArithSeqIn (FromThen $1 $3) }
1156 | exp '..' exp { LL $ ArithSeqIn (FromTo $1 $3) }
1157 | exp ',' exp '..' exp { LL $ ArithSeqIn (FromThenTo $1 $3 $5) }
1158 | exp pquals { LL $ mkHsDo ListComp
1159 (reverse (L (getLoc $1) (ResultStmt $1) :
1162 lexps :: { Located [LHsExpr RdrName] }
1163 : lexps ',' exp { LL ($3 : unLoc $1) }
1164 | exp ',' exp { 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 $ PArrSeqIn (FromTo $1 $3) }
1201 | exp ',' exp '..' exp { LL $ PArrSeqIn (FromThenTo $1 $3 $5) }
1202 | exp pquals { LL $ mkHsDo PArrComp
1203 (reverse (L (getLoc $1) (ResultStmt $1) :
1207 -- We are reusing `lexps' and `pquals' from the list case.
1209 -----------------------------------------------------------------------------
1210 -- Case alternatives
1212 altslist :: { Located [LMatch RdrName] }
1213 : '{' alts '}' { LL (reverse (unLoc $2)) }
1214 | vocurly alts close { L (getLoc $2) (reverse (unLoc $2)) }
1216 alts :: { Located [LMatch RdrName] }
1217 : alts1 { L1 (unLoc $1) }
1218 | ';' alts { LL (unLoc $2) }
1220 alts1 :: { Located [LMatch RdrName] }
1221 : alts1 ';' alt { LL ($3 : unLoc $1) }
1222 | alts1 ';' { LL (unLoc $1) }
1225 alt :: { LMatch RdrName }
1226 : infixexp opt_sig alt_rhs {% checkPattern $1 >>= \p ->
1227 return (LL (Match [p] $2 (unLoc $3))) }
1229 alt_rhs :: { Located (GRHSs RdrName) }
1230 : ralt wherebinds { LL (GRHSs (unLoc $1) (unLoc $2)) }
1232 ralt :: { Located [LGRHS RdrName] }
1233 : '->' exp { LL (unguardedRHS $2) }
1234 | gdpats { L1 (reverse (unLoc $1)) }
1236 gdpats :: { Located [LGRHS RdrName] }
1237 : gdpats gdpat { LL ($2 : unLoc $1) }
1240 gdpat :: { LGRHS RdrName }
1241 : '|' quals '->' exp { let r = L (getLoc $4) (ResultStmt $4)
1242 in LL $ GRHS (reverse (r : unLoc $2)) }
1244 -----------------------------------------------------------------------------
1245 -- Statement sequences
1247 stmtlist :: { Located [LStmt RdrName] }
1248 : '{' stmts '}' { LL (unLoc $2) }
1249 | vocurly stmts close { $2 }
1251 -- do { ;; s ; s ; ; s ;; }
1252 -- The last Stmt should be a ResultStmt, but that's hard to enforce
1253 -- here, because we need too much lookahead if we see do { e ; }
1254 -- So we use ExprStmts throughout, and switch the last one over
1255 -- in ParseUtils.checkDo instead
1256 stmts :: { Located [LStmt RdrName] }
1257 : stmt stmts_help { LL ($1 : unLoc $2) }
1258 | ';' stmts { LL (unLoc $2) }
1259 | {- empty -} { noLoc [] }
1261 stmts_help :: { Located [LStmt RdrName] } -- might be empty
1262 : ';' stmts { LL (unLoc $2) }
1263 | {- empty -} { noLoc [] }
1265 -- For typing stmts at the GHCi prompt, where
1266 -- the input may consist of just comments.
1267 maybe_stmt :: { Maybe (LStmt RdrName) }
1269 | {- nothing -} { Nothing }
1271 stmt :: { LStmt RdrName }
1273 | infixexp '->' exp {% checkPattern $3 >>= \p ->
1274 return (LL $ BindStmt p $1) }
1275 | 'rec' stmtlist { LL $ RecStmt (unLoc $2) undefined undefined undefined }
1277 qual :: { LStmt RdrName }
1278 : infixexp '<-' exp {% checkPattern $1 >>= \p ->
1279 return (LL $ BindStmt p $3) }
1280 | exp { L1 $ ExprStmt $1 placeHolderType }
1281 | 'let' binds { LL $ LetStmt (unLoc $2) }
1283 -----------------------------------------------------------------------------
1284 -- Record Field Update/Construction
1286 fbinds :: { HsRecordBinds RdrName }
1288 | {- empty -} { [] }
1290 fbinds1 :: { HsRecordBinds RdrName }
1291 : fbinds1 ',' fbind { $3 : $1 }
1294 fbind :: { (Located RdrName, LHsExpr RdrName) }
1295 : qvar '=' exp { ($1,$3) }
1297 -----------------------------------------------------------------------------
1298 -- Implicit Parameter Bindings
1300 dbinds :: { Located [LIPBind RdrName] }
1301 : dbinds ';' dbind { LL ($3 : unLoc $1) }
1302 | dbinds ';' { LL (unLoc $1) }
1304 -- | {- empty -} { [] }
1306 dbind :: { LIPBind RdrName }
1307 dbind : ipvar '=' exp { LL (IPBind (unLoc $1) $3) }
1309 -----------------------------------------------------------------------------
1310 -- Variables, Constructors and Operators.
1312 identifier :: { Located RdrName }
1318 depreclist :: { Located [RdrName] }
1319 depreclist : deprec_var { L1 [unLoc $1] }
1320 | deprec_var ',' depreclist { LL (unLoc $1 : unLoc $3) }
1322 deprec_var :: { Located RdrName }
1323 deprec_var : var { $1 }
1326 gcon :: { Located RdrName } -- Data constructor namespace
1327 : sysdcon { L1 $ nameRdrName (dataConName (unLoc $1)) }
1329 -- the case of '[:' ':]' is part of the production `parr'
1331 sysdcon :: { Located DataCon } -- Wired in data constructors
1332 : '(' ')' { LL unitDataCon }
1333 | '(' commas ')' { LL $ tupleCon Boxed $2 }
1334 | '[' ']' { LL nilDataCon }
1336 var :: { Located RdrName }
1338 | '(' varsym ')' { LL (unLoc $2) }
1340 qvar :: { Located RdrName }
1342 | '(' varsym ')' { LL (unLoc $2) }
1343 | '(' qvarsym1 ')' { LL (unLoc $2) }
1344 -- We've inlined qvarsym here so that the decision about
1345 -- whether it's a qvar or a var can be postponed until
1346 -- *after* we see the close paren.
1348 ipvar :: { Located (IPName RdrName) }
1349 : IPDUPVARID { L1 (Dupable (mkUnqual varName (getIPDUPVARID $1))) }
1350 | IPSPLITVARID { L1 (Linear (mkUnqual varName (getIPSPLITVARID $1))) }
1352 qcon :: { Located RdrName }
1354 | '(' qconsym ')' { LL (unLoc $2) }
1356 varop :: { Located RdrName }
1358 | '`' varid '`' { LL (unLoc $2) }
1360 qvarop :: { Located RdrName }
1362 | '`' qvarid '`' { LL (unLoc $2) }
1364 qvaropm :: { Located RdrName }
1365 : qvarsym_no_minus { $1 }
1366 | '`' qvarid '`' { LL (unLoc $2) }
1368 conop :: { Located RdrName }
1370 | '`' conid '`' { LL (unLoc $2) }
1372 qconop :: { Located RdrName }
1374 | '`' qconid '`' { LL (unLoc $2) }
1376 -----------------------------------------------------------------------------
1377 -- Type constructors
1379 gtycon :: { Located RdrName } -- A "general" qualified tycon
1381 | '(' ')' { LL $ getRdrName unitTyCon }
1382 | '(' commas ')' { LL $ getRdrName (tupleTyCon Boxed $2) }
1383 | '(' '->' ')' { LL $ getRdrName funTyCon }
1384 | '[' ']' { LL $ listTyCon_RDR }
1385 | '[:' ':]' { LL $ parrTyCon_RDR }
1387 oqtycon :: { Located RdrName } -- An "ordinary" qualified tycon
1389 | '(' qtyconsym ')' { LL (unLoc $2) }
1391 qtyconop :: { Located RdrName } -- Qualified or unqualified
1393 | '`' qtycon '`' { LL (unLoc $2) }
1395 tyconop :: { Located RdrName } -- Unqualified
1397 | '`' tycon '`' { LL (unLoc $2) }
1399 qtycon :: { Located RdrName } -- Qualified or unqualified
1400 : QCONID { L1 $! mkQual tcClsName (getQCONID $1) }
1403 tycon :: { Located RdrName } -- Unqualified
1404 : CONID { L1 $! mkUnqual tcClsName (getCONID $1) }
1406 qtyconsym :: { Located RdrName }
1407 : QCONSYM { L1 $! mkQual tcClsName (getQCONSYM $1) }
1410 tyconsym :: { Located RdrName }
1411 : CONSYM { L1 $! mkUnqual tcClsName (getCONSYM $1) }
1413 -----------------------------------------------------------------------------
1416 op :: { Located RdrName } -- used in infix decls
1420 qop :: { LHsExpr RdrName } -- used in sections
1421 : qvarop { L1 $ HsVar (unLoc $1) }
1422 | qconop { L1 $ HsVar (unLoc $1) }
1424 qopm :: { LHsExpr RdrName } -- used in sections
1425 : qvaropm { L1 $ HsVar (unLoc $1) }
1426 | qconop { L1 $ HsVar (unLoc $1) }
1428 -----------------------------------------------------------------------------
1431 qvarid :: { Located RdrName }
1433 | QVARID { L1 $ mkQual varName (getQVARID $1) }
1435 varid :: { Located RdrName }
1436 : varid_no_unsafe { $1 }
1437 | 'unsafe' { L1 $! mkUnqual varName FSLIT("unsafe") }
1438 | 'safe' { L1 $! mkUnqual varName FSLIT("safe") }
1439 | 'threadsafe' { L1 $! mkUnqual varName FSLIT("threadsafe") }
1441 varid_no_unsafe :: { Located RdrName }
1442 : VARID { L1 $! mkUnqual varName (getVARID $1) }
1443 | special_id { L1 $! mkUnqual varName (unLoc $1) }
1444 | 'forall' { L1 $! mkUnqual varName FSLIT("forall") }
1446 tyvar :: { Located RdrName }
1447 : VARID { L1 $! mkUnqual tvName (getVARID $1) }
1448 | special_id { L1 $! mkUnqual tvName (unLoc $1) }
1449 | 'unsafe' { L1 $! mkUnqual tvName FSLIT("unsafe") }
1450 | 'safe' { L1 $! mkUnqual tvName FSLIT("safe") }
1451 | 'threadsafe' { L1 $! mkUnqual tvName FSLIT("threadsafe") }
1453 -- These special_ids are treated as keywords in various places,
1454 -- but as ordinary ids elsewhere. 'special_id' collects all these
1455 -- except 'unsafe' and 'forall' whose treatment differs depending on context
1456 special_id :: { Located UserFS }
1458 : 'as' { L1 FSLIT("as") }
1459 | 'qualified' { L1 FSLIT("qualified") }
1460 | 'hiding' { L1 FSLIT("hiding") }
1461 | 'export' { L1 FSLIT("export") }
1462 | 'label' { L1 FSLIT("label") }
1463 | 'dynamic' { L1 FSLIT("dynamic") }
1464 | 'stdcall' { L1 FSLIT("stdcall") }
1465 | 'ccall' { L1 FSLIT("ccall") }
1467 -----------------------------------------------------------------------------
1470 qvarsym :: { Located RdrName }
1474 qvarsym_no_minus :: { Located RdrName }
1475 : varsym_no_minus { $1 }
1478 qvarsym1 :: { Located RdrName }
1479 qvarsym1 : QVARSYM { L1 $ mkQual varName (getQVARSYM $1) }
1481 varsym :: { Located RdrName }
1482 : varsym_no_minus { $1 }
1483 | '-' { L1 $ mkUnqual varName FSLIT("-") }
1485 varsym_no_minus :: { Located RdrName } -- varsym not including '-'
1486 : VARSYM { L1 $ mkUnqual varName (getVARSYM $1) }
1487 | special_sym { L1 $ mkUnqual varName (unLoc $1) }
1490 -- See comments with special_id
1491 special_sym :: { Located UserFS }
1492 special_sym : '!' { L1 FSLIT("!") }
1493 | '.' { L1 FSLIT(".") }
1494 | '*' { L1 FSLIT("*") }
1496 -----------------------------------------------------------------------------
1497 -- Data constructors
1499 qconid :: { Located RdrName } -- Qualified or unqualified
1501 | QCONID { L1 $ mkQual dataName (getQCONID $1) }
1503 conid :: { Located RdrName }
1504 : CONID { L1 $ mkUnqual dataName (getCONID $1) }
1506 qconsym :: { Located RdrName } -- Qualified or unqualified
1508 | QCONSYM { L1 $ mkQual dataName (getQCONSYM $1) }
1510 consym :: { Located RdrName }
1511 : CONSYM { L1 $ mkUnqual dataName (getCONSYM $1) }
1513 -- ':' means only list cons
1514 | ':' { L1 $ consDataCon_RDR }
1517 -----------------------------------------------------------------------------
1520 literal :: { Located HsLit }
1521 : CHAR { L1 $ HsChar $ getCHAR $1 }
1522 | STRING { L1 $ HsString $ getSTRING $1 }
1523 | PRIMINTEGER { L1 $ HsIntPrim $ getPRIMINTEGER $1 }
1524 | PRIMCHAR { L1 $ HsCharPrim $ getPRIMCHAR $1 }
1525 | PRIMSTRING { L1 $ HsStringPrim $ getPRIMSTRING $1 }
1526 | PRIMFLOAT { L1 $ HsFloatPrim $ getPRIMFLOAT $1 }
1527 | PRIMDOUBLE { L1 $ HsDoublePrim $ getPRIMDOUBLE $1 }
1529 -----------------------------------------------------------------------------
1533 : vccurly { () } -- context popped in lexer.
1534 | error {% popContext }
1536 -----------------------------------------------------------------------------
1537 -- Miscellaneous (mostly renamings)
1539 modid :: { Located Module }
1540 : CONID { L1 $ mkModuleFS (getCONID $1) }
1541 | QCONID { L1 $ let (mod,c) = getQCONID $1 in
1544 (unpackFS mod ++ '.':unpackFS c))
1548 : commas ',' { $1 + 1 }
1551 -----------------------------------------------------------------------------
1555 happyError = srcParseFail
1557 getVARID (L _ (ITvarid x)) = x
1558 getCONID (L _ (ITconid x)) = x
1559 getVARSYM (L _ (ITvarsym x)) = x
1560 getCONSYM (L _ (ITconsym x)) = x
1561 getQVARID (L _ (ITqvarid x)) = x
1562 getQCONID (L _ (ITqconid x)) = x
1563 getQVARSYM (L _ (ITqvarsym x)) = x
1564 getQCONSYM (L _ (ITqconsym x)) = x
1565 getIPDUPVARID (L _ (ITdupipvarid x)) = x
1566 getIPSPLITVARID (L _ (ITsplitipvarid x)) = x
1567 getCHAR (L _ (ITchar x)) = x
1568 getSTRING (L _ (ITstring x)) = x
1569 getINTEGER (L _ (ITinteger x)) = x
1570 getRATIONAL (L _ (ITrational x)) = x
1571 getPRIMCHAR (L _ (ITprimchar x)) = x
1572 getPRIMSTRING (L _ (ITprimstring x)) = x
1573 getPRIMINTEGER (L _ (ITprimint x)) = x
1574 getPRIMFLOAT (L _ (ITprimfloat x)) = x
1575 getPRIMDOUBLE (L _ (ITprimdouble x)) = x
1576 getTH_ID_SPLICE (L _ (ITidEscape x)) = x
1578 -- Utilities for combining source spans
1579 comb2 :: Located a -> Located b -> SrcSpan
1582 comb3 :: Located a -> Located b -> Located c -> SrcSpan
1583 comb3 a b c = combineSrcSpans (getLoc a) (combineSrcSpans (getLoc b) (getLoc c))
1585 comb4 :: Located a -> Located b -> Located c -> Located d -> SrcSpan
1586 comb4 a b c d = combineSrcSpans (getLoc a) $ combineSrcSpans (getLoc b) $
1587 combineSrcSpans (getLoc c) (getLoc d)
1589 -- strict constructor version:
1591 sL :: SrcSpan -> a -> Located a
1592 sL span a = span `seq` L span a
1594 -- Make a source location for the file. We're a bit lazy here and just
1595 -- make a point SrcSpan at line 1, column 0. Strictly speaking we should
1596 -- try to find the span of the whole file (ToDo).
1597 fileSrcSpan :: P SrcSpan
1600 let loc = mkSrcLoc (srcLocFile l) 1 0;
1601 return (mkSrcSpan loc loc)