2 {-# OPTIONS -Wwarn -w #-}
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"
44 '%module' { TKmodule }
46 '%newtype' { TKnewtype }
47 '%forall' { TKforall }
55 '%external' { TKexternal }
66 ':=:' { TKcoloneqcolon }
76 INTEGER { TKinteger $$ }
77 RATIONAL { TKrational $$ }
78 STRING { TKstring $$ }
81 %monad { P } { thenP } { returnP }
82 %lexer { lexer } { TKEOF }
86 module :: { HsExtCore RdrName }
87 -- : '%module' modid tdefs vdefgs { HsExtCore $2 $3 $4 }
88 : '%module' modid tdefs vdefgs { HsExtCore $2 [] [] }
91 -------------------------------------------------------------
92 -- Names: the trickiest bit in here
94 -- A name of the form A.B.C could be:
96 -- dcon C in module A.B
97 -- tcon C in module A.B
99 : NAME ':' mparts { undefined }
101 q_dc_name :: { Name }
102 : NAME ':' mparts { undefined }
104 q_tc_name :: { Name }
105 : NAME ':' mparts { undefined }
107 q_var_occ :: { Name }
108 : NAME ':' vparts { undefined }
110 mparts :: { [String] }
112 | CNAME '.' mparts { $1:$3 }
114 vparts :: { [String] }
116 | CNAME '.' vparts { $1:$3 }
118 -------------------------------------------------------------
119 -- Type and newtype declarations are in HsSyn syntax
121 tdefs :: { [TyClDecl RdrName] }
125 tdef :: { TyClDecl RdrName }
126 : '%data' q_tc_name tv_bndrs '=' '{' cons '}' ';'
127 { TyData { tcdND = DataType, tcdCtxt = noLoc []
128 , tcdLName = noLoc (ifaceExtRdrName $2)
129 , tcdTyVars = map toHsTvBndr $3
130 , tcdTyPats = Nothing, tcdKindSig = Nothing
131 , tcdCons = $6, tcdDerivs = Nothing } }
132 | '%newtype' q_tc_name tv_bndrs trep ';'
133 { let tc_rdr = ifaceExtRdrName $2 in
134 TyData { tcdND = NewType, tcdCtxt = noLoc []
135 , tcdLName = noLoc tc_rdr
136 , tcdTyVars = map toHsTvBndr $3
137 , tcdTyPats = Nothing, tcdKindSig = Nothing
138 , tcdCons = $4 (rdrNameOcc tc_rdr), tcdDerivs = Nothing } }
140 -- For a newtype we have to invent a fake data constructor name
141 -- It doesn't matter what it is, because it won't be used
142 trep :: { OccName -> [LConDecl RdrName] }
143 : {- empty -} { (\ tc_occ -> []) }
144 | '=' ty { (\ tc_occ -> let { dc_name = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
145 con_info = PrefixCon [toHsType $2] }
146 in [noLoc $ mkSimpleConDecl (noLoc dc_name) []
147 (noLoc []) con_info]) }
149 cons :: { [LConDecl RdrName] }
150 : {- empty -} { [] } -- 20060420 Empty data types allowed. jds
152 | con ';' cons { $1:$3 }
154 con :: { LConDecl RdrName }
155 : d_pat_occ attv_bndrs hs_atys
156 { noLoc $ mkSimpleConDecl (noLoc (mkRdrUnqual $1)) $2 (noLoc []) (PrefixCon $3) }
157 -- ToDo: parse record-style declarations
159 attv_bndrs :: { [LHsTyVarBndr RdrName] }
161 | '@' tv_bndr attv_bndrs { toHsTvBndr $2 : $3 }
163 hs_atys :: { [LHsType RdrName] }
164 : atys { map toHsType $1 }
167 ---------------------------------------
169 ---------------------------------------
171 atys :: { [IfaceType] }
176 : fs_var_occ { IfaceTyVar $1 }
177 | q_tc_name { IfaceTyConApp (IfaceTc $1) [] }
181 : fs_var_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
182 | q_var_occ atys { undefined }
183 | q_tc_name atys { IfaceTyConApp (IfaceTc $1) $2 }
188 | bty '->' ty { IfaceFunTy $1 $3 }
189 | '%forall' tv_bndrs '.' ty { foldr IfaceForAllTy $4 $2 }
191 ----------------------------------------------
192 -- Bindings are in Iface syntax
194 vdefgs :: { [IfaceBinding] }
196 | let_bind ';' vdefgs { $1 : $3 }
198 let_bind :: { IfaceBinding }
199 : '%rec' '{' vdefs1 '}' { IfaceRec $3 } -- Can be empty. Do we care?
200 | vdef { let (b,r) = $1
203 vdefs1 :: { [(IfaceLetBndr, IfaceExpr)] }
205 | vdef ';' vdefs1 { $1:$3 }
207 vdef :: { (IfaceLetBndr, IfaceExpr) }
208 : fs_var_occ '::' ty '=' exp { (IfLetBndr $1 $3 NoInfo, $5) }
209 | '%local' vdef { $2 }
211 -- NB: qd_occ includes data constructors, because
212 -- we allow data-constructor wrappers at top level
213 -- But we discard the module name, because it must be the
214 -- same as the module being compiled, and Iface syntax only
215 -- has OccNames in binding positions. Ah, but it has Names now!
217 ---------------------------------------
219 bndr :: { IfaceBndr }
220 : '@' tv_bndr { IfaceTvBndr $2 }
221 | id_bndr { IfaceIdBndr $1 }
223 bndrs :: { [IfaceBndr] }
225 | bndr bndrs { $1:$2 }
227 id_bndr :: { IfaceIdBndr }
228 : '(' fs_var_occ '::' ty ')' { ($2,$4) }
230 tv_bndr :: { IfaceTvBndr }
231 : fs_var_occ { ($1, ifaceLiftedTypeKind) }
232 | '(' fs_var_occ '::' akind ')' { ($2, $4) }
234 tv_bndrs :: { [IfaceTvBndr] }
236 | tv_bndr tv_bndrs { $1:$2 }
238 akind :: { IfaceKind }
239 : '*' { ifaceLiftedTypeKind }
240 | '#' { ifaceUnliftedTypeKind }
241 | '?' { ifaceOpenTypeKind }
242 | '(' kind ')' { $2 }
244 kind :: { IfaceKind }
246 | akind '->' kind { ifaceArrow $1 $3 }
247 | ty ':=:' ty { ifaceEq $1 $3 }
249 -----------------------------------------
252 aexp :: { IfaceExpr }
253 : fs_var_occ { IfaceLcl $1 }
254 | q_var_occ { IfaceExt $1 }
255 | q_dc_name { IfaceExt $1 }
256 | lit { IfaceLit $1 }
259 fexp :: { IfaceExpr }
260 : fexp aexp { IfaceApp $1 $2 }
261 | fexp '@' aty { IfaceApp $1 (IfaceType $3) }
266 | '\\' bndrs '->' exp { foldr IfaceLam $4 $2 }
267 | '%let' let_bind '%in' exp { IfaceLet $2 $4 }
269 | '%case' '(' ty ')' aexp '%of' id_bndr
270 '{' alts1 '}' { IfaceCase $5 (fst $7) $3 $9 }
271 | '%cast' aexp aty { IfaceCast $2 $3 }
272 -- No InlineMe any more
273 -- | '%note' STRING exp
275 -- --"SCC" -> IfaceNote (IfaceSCC "scc") $3
276 -- "InlineMe" -> IfaceNote IfaceInlineMe $3
278 | '%external' STRING aty { IfaceFCall (ForeignCall.CCall
279 (CCallSpec (StaticTarget (mkFastString $2))
280 CCallConv (PlaySafe False)))
283 alts1 :: { [IfaceAlt] }
285 | alt ';' alts1 { $1:$3 }
288 : q_dc_name bndrs '->' exp
289 { (IfaceDataAlt $1, map ifaceBndrName $2, $4) }
290 -- The external syntax currently includes the types of the
291 -- the args, but they aren't needed internally
292 -- Nor is the module qualifier
294 { (IfaceDataAlt $1, [], $3) }
296 { (IfaceLitAlt $1, [], $3) }
298 { (IfaceDefault, [], $3) }
301 : '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
302 | '(' RATIONAL '::' aty ')' { convRatLit $2 $4 }
303 | '(' CHAR '::' aty ')' { MachChar $2 }
304 | '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
306 fs_var_occ :: { FastString }
307 : NAME { mkFastString $1 }
309 var_occ :: { String }
313 -- Data constructor in a pattern or data type declaration; use the dataName,
314 -- because that's what we expect in Core case patterns
315 d_pat_occ :: { OccName }
316 : CNAME { mkOccName dataName $1 }
320 ifaceKind kc = IfaceTyConApp kc []
322 ifaceBndrName (IfaceIdBndr (n,_)) = n
323 ifaceBndrName (IfaceTvBndr (n,_)) = n
325 convIntLit :: Integer -> IfaceType -> Literal
326 convIntLit i (IfaceTyConApp tc [])
327 | tc `eqTc` intPrimTyCon = MachInt i
328 | tc `eqTc` wordPrimTyCon = MachWord i
329 | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
330 | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
332 = pprPanic "Unknown integer literal type" (ppr aty)
334 convRatLit :: Rational -> IfaceType -> Literal
335 convRatLit r (IfaceTyConApp tc [])
336 | tc `eqTc` floatPrimTyCon = MachFloat r
337 | tc `eqTc` doublePrimTyCon = MachDouble r
339 = pprPanic "Unknown rational literal type" (ppr aty)
341 eqTc :: IfaceTyCon -> TyCon -> Bool -- Ugh!
342 eqTc (IfaceTc name) tycon = name == tyConName tycon
344 -- Tiresomely, we have to generate both HsTypes (in type/class decls)
345 -- and IfaceTypes (in Core expressions). So we parse them as IfaceTypes,
346 -- and convert to HsTypes here. But the IfaceTypes we can see here
347 -- are very limited (see the productions for 'ty', so the translation
349 toHsType :: IfaceType -> LHsType RdrName
350 toHsType (IfaceTyVar v) = noLoc $ HsTyVar (mkRdrUnqual (mkTyVarOccFS v))
351 toHsType (IfaceAppTy t1 t2) = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
352 toHsType (IfaceFunTy t1 t2) = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
353 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts)
354 toHsType (IfaceForAllTy tv t) = add_forall (toHsTvBndr tv) (toHsType t)
356 -- We also need to convert IfaceKinds to Kinds (now that they are different).
357 -- Only a limited form of kind will be encountered... hopefully
358 toKind :: IfaceKind -> Kind
359 toKind (IfaceFunTy ifK1 ifK2) = mkArrowKind (toKind ifK1) (toKind ifK2)
360 toKind (IfaceTyConApp ifKc []) = mkTyConApp (toKindTc ifKc) []
361 toKind other = pprPanic "toKind" (ppr other)
363 toKindTc :: IfaceTyCon -> TyCon
364 toKindTc IfaceLiftedTypeKindTc = liftedTypeKindTyCon
365 toKindTc IfaceOpenTypeKindTc = openTypeKindTyCon
366 toKindTc IfaceUnliftedTypeKindTc = unliftedTypeKindTyCon
367 toKindTc IfaceUbxTupleKindTc = ubxTupleKindTyCon
368 toKindTc IfaceArgTypeKindTc = argTypeKindTyCon
369 toKindTc other = pprPanic "toKindTc" (ppr other)
371 ifaceTcType ifTc = IfaceTyConApp ifTc []
373 ifaceLiftedTypeKind = ifaceTcType IfaceLiftedTypeKindTc
374 ifaceOpenTypeKind = ifaceTcType IfaceOpenTypeKindTc
375 ifaceUnliftedTypeKind = ifaceTcType IfaceUnliftedTypeKindTc
377 ifaceArrow ifT1 ifT2 = IfaceFunTy ifT1 ifT2
379 ifaceEq ifT1 ifT2 = IfacePredTy (IfaceEqPred ifT1 ifT2)
381 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
382 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual (mkTyVarOccFS tv)) (toKind k)
384 ifaceExtRdrName :: Name -> RdrName
385 ifaceExtRdrName name = mkOrig (nameModule name) (nameOccName name)
386 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
388 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
389 = noLoc $ HsForAllTy exp (tv:tvs) cxt t
391 = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
394 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l