3 -- The above warning supression flag is a temporary kludge.
4 -- While working on this module you are encouraged to remove it and fix
5 -- any warnings in the module. See
6 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
9 module ParserCore ( parseCore ) where
18 liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
19 argTypeKindTyCon, ubxTupleKindTyCon, mkArrowKind, mkTyConApp
21 import Name( Name, nameOccName, nameModule, mkExternalName )
23 import ParserCoreUtils
27 import TysPrim( wordPrimTyCon, intPrimTyCon, charPrimTyCon,
28 floatPrimTyCon, doublePrimTyCon, addrPrimTyCon )
29 import TyCon ( TyCon, tyConName )
35 #include "../HsVersions.h"
43 '%module' { TKmodule }
45 '%newtype' { TKnewtype }
46 '%forall' { TKforall }
54 '%external' { TKexternal }
65 ':=:' { TKcoloneqcolon }
75 INTEGER { TKinteger $$ }
76 RATIONAL { TKrational $$ }
77 STRING { TKstring $$ }
80 %monad { P } { thenP } { returnP }
81 %lexer { lexer } { TKEOF }
85 module :: { HsExtCore RdrName }
86 -- : '%module' modid tdefs vdefgs { HsExtCore $2 $3 $4 }
87 : '%module' modid tdefs vdefgs { HsExtCore $2 [] [] }
90 -------------------------------------------------------------
91 -- Names: the trickiest bit in here
93 -- A name of the form A.B.C could be:
95 -- dcon C in module A.B
96 -- tcon C in module A.B
98 : NAME ':' mparts { undefined }
100 q_dc_name :: { Name }
101 : NAME ':' mparts { undefined }
103 q_tc_name :: { Name }
104 : NAME ':' mparts { undefined }
106 q_var_occ :: { Name }
107 : NAME ':' vparts { undefined }
109 mparts :: { [String] }
111 | CNAME '.' mparts { $1:$3 }
113 vparts :: { [String] }
115 | CNAME '.' vparts { $1:$3 }
117 -------------------------------------------------------------
118 -- Type and newtype declarations are in HsSyn syntax
120 tdefs :: { [TyClDecl RdrName] }
124 tdef :: { TyClDecl RdrName }
125 : '%data' q_tc_name tv_bndrs '=' '{' cons '}' ';'
126 { mkTyData DataType ( noLoc []
127 , noLoc (ifaceExtRdrName $2)
130 ) Nothing $6 Nothing }
131 | '%newtype' q_tc_name tv_bndrs trep ';'
132 { let tc_rdr = ifaceExtRdrName $2 in
133 mkTyData NewType ( noLoc []
137 ) Nothing ($4 (rdrNameOcc tc_rdr)) Nothing }
139 -- For a newtype we have to invent a fake data constructor name
140 -- It doesn't matter what it is, because it won't be used
141 trep :: { OccName -> [LConDecl RdrName] }
142 : {- empty -} { (\ tc_occ -> []) }
143 | '=' ty { (\ tc_occ -> let { dc_name = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
144 con_info = PrefixCon [toHsType $2] }
145 in [noLoc $ ConDecl (noLoc dc_name) Explicit []
146 (noLoc []) con_info ResTyH98 Nothing]) }
148 cons :: { [LConDecl RdrName] }
149 : {- empty -} { [] } -- 20060420 Empty data types allowed. jds
151 | con ';' cons { $1:$3 }
153 con :: { LConDecl RdrName }
154 : d_pat_occ attv_bndrs hs_atys
155 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) Explicit $2 (noLoc []) (PrefixCon $3) ResTyH98 Nothing }
157 -- XXX - audreyt - $3 needs to be split into argument and return types!
158 -- also not sure whether the [] below (quantified vars) appears.
159 -- also the "PrefixCon []" is wrong.
160 -- also we want to munge $3 somehow.
161 -- extractWhatEver to unpack ty into the parts to ConDecl
162 -- XXX - define it somewhere in RdrHsSyn
163 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) Explicit [] (noLoc []) (PrefixCon []) (undefined $3) Nothing }
165 attv_bndrs :: { [LHsTyVarBndr RdrName] }
167 | '@' tv_bndr attv_bndrs { toHsTvBndr $2 : $3 }
169 hs_atys :: { [LHsType RdrName] }
170 : atys { map toHsType $1 }
173 ---------------------------------------
175 ---------------------------------------
177 atys :: { [IfaceType] }
182 : fs_var_occ { IfaceTyVar $1 }
183 | q_tc_name { IfaceTyConApp (IfaceTc $1) [] }
187 : fs_var_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
188 | q_var_occ atys { undefined }
189 | q_tc_name atys { IfaceTyConApp (IfaceTc $1) $2 }
194 | bty '->' ty { IfaceFunTy $1 $3 }
195 | '%forall' tv_bndrs '.' ty { foldr IfaceForAllTy $4 $2 }
197 ----------------------------------------------
198 -- Bindings are in Iface syntax
200 vdefgs :: { [IfaceBinding] }
202 | let_bind ';' vdefgs { $1 : $3 }
204 let_bind :: { IfaceBinding }
205 : '%rec' '{' vdefs1 '}' { IfaceRec $3 } -- Can be empty. Do we care?
206 | vdef { let (b,r) = $1
209 vdefs1 :: { [(IfaceLetBndr, IfaceExpr)] }
211 | vdef ';' vdefs1 { $1:$3 }
213 vdef :: { (IfaceLetBndr, IfaceExpr) }
214 : fs_var_occ '::' ty '=' exp { (IfLetBndr $1 $3 NoInfo, $5) }
215 | '%local' vdef { $2 }
217 -- NB: qd_occ includes data constructors, because
218 -- we allow data-constructor wrappers at top level
219 -- But we discard the module name, because it must be the
220 -- same as the module being compiled, and Iface syntax only
221 -- has OccNames in binding positions. Ah, but it has Names now!
223 ---------------------------------------
225 bndr :: { IfaceBndr }
226 : '@' tv_bndr { IfaceTvBndr $2 }
227 | id_bndr { IfaceIdBndr $1 }
229 bndrs :: { [IfaceBndr] }
231 | bndr bndrs { $1:$2 }
233 id_bndr :: { IfaceIdBndr }
234 : '(' fs_var_occ '::' ty ')' { ($2,$4) }
236 tv_bndr :: { IfaceTvBndr }
237 : fs_var_occ { ($1, ifaceLiftedTypeKind) }
238 | '(' fs_var_occ '::' akind ')' { ($2, $4) }
240 tv_bndrs :: { [IfaceTvBndr] }
242 | tv_bndr tv_bndrs { $1:$2 }
244 akind :: { IfaceKind }
245 : '*' { ifaceLiftedTypeKind }
246 | '#' { ifaceUnliftedTypeKind }
247 | '?' { ifaceOpenTypeKind }
248 | '(' kind ')' { $2 }
250 kind :: { IfaceKind }
252 | akind '->' kind { ifaceArrow $1 $3 }
253 | ty ':=:' ty { ifaceEq $1 $3 }
255 -----------------------------------------
258 aexp :: { IfaceExpr }
259 : fs_var_occ { IfaceLcl $1 }
260 | q_var_occ { IfaceExt $1 }
261 | q_dc_name { IfaceExt $1 }
262 | lit { IfaceLit $1 }
265 fexp :: { IfaceExpr }
266 : fexp aexp { IfaceApp $1 $2 }
267 | fexp '@' aty { IfaceApp $1 (IfaceType $3) }
272 | '\\' bndrs '->' exp { foldr IfaceLam $4 $2 }
273 | '%let' let_bind '%in' exp { IfaceLet $2 $4 }
275 | '%case' '(' ty ')' aexp '%of' id_bndr
276 '{' alts1 '}' { IfaceCase $5 (fst $7) $3 $9 }
277 | '%cast' aexp aty { IfaceCast $2 $3 }
280 --"SCC" -> IfaceNote (IfaceSCC "scc") $3
281 "InlineMe" -> IfaceNote IfaceInlineMe $3
283 | '%external' STRING aty { IfaceFCall (ForeignCall.CCall
284 (CCallSpec (StaticTarget (mkFastString $2))
285 CCallConv (PlaySafe False)))
288 alts1 :: { [IfaceAlt] }
290 | alt ';' alts1 { $1:$3 }
293 : q_dc_name bndrs '->' exp
294 { (IfaceDataAlt $1, map ifaceBndrName $2, $4) }
295 -- The external syntax currently includes the types of the
296 -- the args, but they aren't needed internally
297 -- Nor is the module qualifier
299 { (IfaceDataAlt $1, [], $3) }
301 { (IfaceLitAlt $1, [], $3) }
303 { (IfaceDefault, [], $3) }
306 : '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
307 | '(' RATIONAL '::' aty ')' { convRatLit $2 $4 }
308 | '(' CHAR '::' aty ')' { MachChar $2 }
309 | '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
311 fs_var_occ :: { FastString }
312 : NAME { mkFastString $1 }
314 var_occ :: { String }
318 -- Data constructor in a pattern or data type declaration; use the dataName,
319 -- because that's what we expect in Core case patterns
320 d_pat_occ :: { OccName }
321 : CNAME { mkOccName dataName $1 }
325 ifaceKind kc = IfaceTyConApp kc []
327 ifaceBndrName (IfaceIdBndr (n,_)) = n
328 ifaceBndrName (IfaceTvBndr (n,_)) = n
330 convIntLit :: Integer -> IfaceType -> Literal
331 convIntLit i (IfaceTyConApp tc [])
332 | tc `eqTc` intPrimTyCon = MachInt i
333 | tc `eqTc` wordPrimTyCon = MachWord i
334 | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
335 | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
337 = pprPanic "Unknown integer literal type" (ppr aty)
339 convRatLit :: Rational -> IfaceType -> Literal
340 convRatLit r (IfaceTyConApp tc [])
341 | tc `eqTc` floatPrimTyCon = MachFloat r
342 | tc `eqTc` doublePrimTyCon = MachDouble r
344 = pprPanic "Unknown rational literal type" (ppr aty)
346 eqTc :: IfaceTyCon -> TyCon -> Bool -- Ugh!
347 eqTc (IfaceTc name) tycon = name == tyConName tycon
349 -- Tiresomely, we have to generate both HsTypes (in type/class decls)
350 -- and IfaceTypes (in Core expressions). So we parse them as IfaceTypes,
351 -- and convert to HsTypes here. But the IfaceTypes we can see here
352 -- are very limited (see the productions for 'ty', so the translation
354 toHsType :: IfaceType -> LHsType RdrName
355 toHsType (IfaceTyVar v) = noLoc $ HsTyVar (mkRdrUnqual (mkTyVarOcc v))
356 toHsType (IfaceAppTy t1 t2) = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
357 toHsType (IfaceFunTy t1 t2) = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
358 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts)
359 toHsType (IfaceForAllTy tv t) = add_forall (toHsTvBndr tv) (toHsType t)
361 -- We also need to convert IfaceKinds to Kinds (now that they are different).
362 -- Only a limited form of kind will be encountered... hopefully
363 toKind :: IfaceKind -> Kind
364 toKind (IfaceFunTy ifK1 ifK2) = mkArrowKind (toKind ifK1) (toKind ifK2)
365 toKind (IfaceTyConApp ifKc []) = mkTyConApp (toKindTc ifKc) []
366 toKind other = pprPanic "toKind" (ppr other)
368 toKindTc :: IfaceTyCon -> TyCon
369 toKindTc IfaceLiftedTypeKindTc = liftedTypeKindTyCon
370 toKindTc IfaceOpenTypeKindTc = openTypeKindTyCon
371 toKindTc IfaceUnliftedTypeKindTc = unliftedTypeKindTyCon
372 toKindTc IfaceUbxTupleKindTc = ubxTupleKindTyCon
373 toKindTc IfaceArgTypeKindTc = argTypeKindTyCon
374 toKindTc other = pprPanic "toKindTc" (ppr other)
376 ifaceTcType ifTc = IfaceTyConApp ifTc []
378 ifaceLiftedTypeKind = ifaceTcType IfaceLiftedTypeKindTc
379 ifaceOpenTypeKind = ifaceTcType IfaceOpenTypeKindTc
380 ifaceUnliftedTypeKind = ifaceTcType IfaceUnliftedTypeKindTc
382 ifaceArrow ifT1 ifT2 = IfaceFunTy ifT1 ifT2
384 ifaceEq ifT1 ifT2 = IfacePredTy (IfaceEqPred ifT1 ifT2)
386 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
387 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual (mkTyVarOcc tv)) (toKind k)
389 ifaceExtRdrName :: Name -> RdrName
390 ifaceExtRdrName name = mkOrig (nameModule name) (nameOccName name)
391 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
393 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
394 = noLoc $ HsForAllTy exp (tv:tvs) cxt t
396 = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
399 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l