2
{-# OPTIONS -Wwarn -w -XNoMonomorphismRestriction #-}
3
-- The NoMonomorphismRestriction deals with a Happy infelicity
4
-- With OutsideIn's more conservativ monomorphism restriction
5
-- we aren't generalising
6
-- notHappyAtAll = error "urk"
7
-- which is terrible. Switching off the restriction allows
8
-- the generalisation. Better would be to make Happy generate
9
-- an appropriate signature.
11
-- The above warning supression flag is a temporary kludge.
12
-- While working on this module you are encouraged to remove it and fix
13
-- any warnings in the module. See
14
-- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
17
module ParserCore ( parseCore ) where
26
liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
27
argTypeKindTyCon, ubxTupleKindTyCon, mkTyConApp
29
import Coercion( mkArrowKind )
30
import Name( Name, nameOccName, nameModule, mkExternalName )
32
import ParserCoreUtils
36
import TysPrim( wordPrimTyCon, intPrimTyCon, charPrimTyCon,
37
floatPrimTyCon, doublePrimTyCon, addrPrimTyCon )
38
import TyCon ( TyCon, tyConName )
44
#include "../HsVersions.h"
53
'%module' { TKmodule }
55
'%newtype' { TKnewtype }
56
'%forall' { TKforall }
64
'%external' { TKexternal }
75
':=:' { TKcoloneqcolon }
85
INTEGER { TKinteger $$ }
86
RATIONAL { TKrational $$ }
87
STRING { TKstring $$ }
90
%monad { P } { thenP } { returnP }
91
%lexer { lexer } { TKEOF }
95
module :: { HsExtCore RdrName }
96
-- : '%module' modid tdefs vdefgs { HsExtCore $2 $3 $4 }
97
: '%module' modid tdefs vdefgs { HsExtCore $2 [] [] }
100
-------------------------------------------------------------
101
-- Names: the trickiest bit in here
103
-- A name of the form A.B.C could be:
105
-- dcon C in module A.B
106
-- tcon C in module A.B
108
: NAME ':' mparts { undefined }
110
q_dc_name :: { Name }
111
: NAME ':' mparts { undefined }
113
q_tc_name :: { Name }
114
: NAME ':' mparts { undefined }
116
q_var_occ :: { Name }
117
: NAME ':' vparts { undefined }
119
mparts :: { [String] }
121
| CNAME '.' mparts { $1:$3 }
123
vparts :: { [String] }
125
| CNAME '.' vparts { $1:$3 }
127
-------------------------------------------------------------
128
-- Type and newtype declarations are in HsSyn syntax
130
tdefs :: { [TyClDecl RdrName] }
134
tdef :: { TyClDecl RdrName }
135
: '%data' q_tc_name tv_bndrs '=' '{' cons '}' ';'
136
{ TyData { tcdND = DataType, tcdCtxt = noLoc []
137
, tcdLName = noLoc (ifaceExtRdrName $2)
138
, tcdTyVars = map toHsTvBndr $3
139
, tcdTyPats = Nothing, tcdKindSig = Nothing
140
, tcdCons = $6, tcdDerivs = Nothing } }
141
| '%newtype' q_tc_name tv_bndrs trep ';'
142
{ let tc_rdr = ifaceExtRdrName $2 in
143
TyData { tcdND = NewType, tcdCtxt = noLoc []
144
, tcdLName = noLoc tc_rdr
145
, tcdTyVars = map toHsTvBndr $3
146
, tcdTyPats = Nothing, tcdKindSig = Nothing
147
, tcdCons = $4 (rdrNameOcc tc_rdr), tcdDerivs = Nothing } }
149
-- For a newtype we have to invent a fake data constructor name
150
-- It doesn't matter what it is, because it won't be used
151
trep :: { OccName -> [LConDecl RdrName] }
152
: {- empty -} { (\ tc_occ -> []) }
153
| '=' ty { (\ tc_occ -> let { dc_name = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
154
con_info = PrefixCon [toHsType $2] }
155
in [noLoc $ mkSimpleConDecl (noLoc dc_name) []
156
(noLoc []) con_info]) }
158
cons :: { [LConDecl RdrName] }
159
: {- empty -} { [] } -- 20060420 Empty data types allowed. jds
161
| con ';' cons { $1:$3 }
163
con :: { LConDecl RdrName }
164
: d_pat_occ attv_bndrs hs_atys
165
{ noLoc $ mkSimpleConDecl (noLoc (mkRdrUnqual $1)) $2 (noLoc []) (PrefixCon $3) }
166
-- ToDo: parse record-style declarations
168
attv_bndrs :: { [LHsTyVarBndr RdrName] }
170
| '@' tv_bndr attv_bndrs { toHsTvBndr $2 : $3 }
172
hs_atys :: { [LHsType RdrName] }
173
: atys { map toHsType $1 }
176
---------------------------------------
178
---------------------------------------
180
atys :: { [IfaceType] }
185
: fs_var_occ { IfaceTyVar $1 }
186
| q_tc_name { IfaceTyConApp (IfaceTc $1) [] }
190
: fs_var_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
191
| q_var_occ atys { undefined }
192
| q_tc_name atys { IfaceTyConApp (IfaceTc $1) $2 }
197
| bty '->' ty { IfaceFunTy $1 $3 }
198
| '%forall' tv_bndrs '.' ty { foldr IfaceForAllTy $4 $2 }
200
----------------------------------------------
201
-- Bindings are in Iface syntax
203
vdefgs :: { [IfaceBinding] }
205
| let_bind ';' vdefgs { $1 : $3 }
207
let_bind :: { IfaceBinding }
208
: '%rec' '{' vdefs1 '}' { IfaceRec $3 } -- Can be empty. Do we care?
209
| vdef { let (b,r) = $1
212
vdefs1 :: { [(IfaceLetBndr, IfaceExpr)] }
214
| vdef ';' vdefs1 { $1:$3 }
216
vdef :: { (IfaceLetBndr, IfaceExpr) }
217
: fs_var_occ '::' ty '=' exp { (IfLetBndr $1 $3 NoInfo, $5) }
218
| '%local' vdef { $2 }
220
-- NB: qd_occ includes data constructors, because
221
-- we allow data-constructor wrappers at top level
222
-- But we discard the module name, because it must be the
223
-- same as the module being compiled, and Iface syntax only
224
-- has OccNames in binding positions. Ah, but it has Names now!
226
---------------------------------------
228
bndr :: { IfaceBndr }
229
: '@' tv_bndr { IfaceTvBndr $2 }
230
| id_bndr { IfaceIdBndr $1 }
232
bndrs :: { [IfaceBndr] }
234
| bndr bndrs { $1:$2 }
236
id_bndr :: { IfaceIdBndr }
237
: '(' fs_var_occ '::' ty ')' { ($2,$4) }
239
tv_bndr :: { IfaceTvBndr }
240
: fs_var_occ { ($1, ifaceLiftedTypeKind) }
241
| '(' fs_var_occ '::' akind ')' { ($2, $4) }
243
tv_bndrs :: { [IfaceTvBndr] }
245
| tv_bndr tv_bndrs { $1:$2 }
247
akind :: { IfaceKind }
248
: '*' { ifaceLiftedTypeKind }
249
| '#' { ifaceUnliftedTypeKind }
250
| '?' { ifaceOpenTypeKind }
251
| '(' kind ')' { $2 }
253
kind :: { IfaceKind }
255
| akind '->' kind { ifaceArrow $1 $3 }
256
| ty ':=:' ty { ifaceEq $1 $3 }
258
-----------------------------------------
261
aexp :: { IfaceExpr }
262
: fs_var_occ { IfaceLcl $1 }
263
| q_var_occ { IfaceExt $1 }
264
| q_dc_name { IfaceExt $1 }
265
| lit { IfaceLit $1 }
268
fexp :: { IfaceExpr }
269
: fexp aexp { IfaceApp $1 $2 }
270
| fexp '@' aty { IfaceApp $1 (IfaceType $3) }
275
| '\\' bndrs '->' exp { foldr IfaceLam $4 $2 }
276
| '%let' let_bind '%in' exp { IfaceLet $2 $4 }
278
| '%case' '(' ty ')' aexp '%of' id_bndr
279
'{' alts1 '}' { IfaceCase $5 (fst $7) $3 $9 }
280
| '%cast' aexp aty { IfaceCast $2 $3 }
281
-- No InlineMe any more
282
-- | '%note' STRING exp
284
-- --"SCC" -> IfaceNote (IfaceSCC "scc") $3
285
-- "InlineMe" -> IfaceNote IfaceInlineMe $3
287
| '%external' STRING aty { IfaceFCall (ForeignCall.CCall
288
(CCallSpec (StaticTarget (mkFastString $2) Nothing)
289
CCallConv (PlaySafe False)))
292
alts1 :: { [IfaceAlt] }
294
| alt ';' alts1 { $1:$3 }
297
: q_dc_name bndrs '->' exp
298
{ (IfaceDataAlt $1, map ifaceBndrName $2, $4) }
299
-- The external syntax currently includes the types of the
300
-- the args, but they aren't needed internally
301
-- Nor is the module qualifier
303
{ (IfaceDataAlt $1, [], $3) }
305
{ (IfaceLitAlt $1, [], $3) }
307
{ (IfaceDefault, [], $3) }
310
: '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
311
| '(' RATIONAL '::' aty ')' { convRatLit $2 $4 }
312
| '(' CHAR '::' aty ')' { MachChar $2 }
313
| '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
315
fs_var_occ :: { FastString }
316
: NAME { mkFastString $1 }
318
var_occ :: { String }
322
-- Data constructor in a pattern or data type declaration; use the dataName,
323
-- because that's what we expect in Core case patterns
324
d_pat_occ :: { OccName }
325
: CNAME { mkOccName dataName $1 }
329
ifaceKind kc = IfaceTyConApp kc []
331
ifaceBndrName (IfaceIdBndr (n,_)) = n
332
ifaceBndrName (IfaceTvBndr (n,_)) = n
334
convIntLit :: Integer -> IfaceType -> Literal
335
convIntLit i (IfaceTyConApp tc [])
336
| tc `eqTc` intPrimTyCon = MachInt i
337
| tc `eqTc` wordPrimTyCon = MachWord i
338
| tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
339
| tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
341
= pprPanic "Unknown integer literal type" (ppr aty)
343
convRatLit :: Rational -> IfaceType -> Literal
344
convRatLit r (IfaceTyConApp tc [])
345
| tc `eqTc` floatPrimTyCon = MachFloat r
346
| tc `eqTc` doublePrimTyCon = MachDouble r
348
= pprPanic "Unknown rational literal type" (ppr aty)
350
eqTc :: IfaceTyCon -> TyCon -> Bool -- Ugh!
351
eqTc (IfaceTc name) tycon = name == tyConName tycon
353
-- Tiresomely, we have to generate both HsTypes (in type/class decls)
354
-- and IfaceTypes (in Core expressions). So we parse them as IfaceTypes,
355
-- and convert to HsTypes here. But the IfaceTypes we can see here
356
-- are very limited (see the productions for 'ty', so the translation
358
toHsType :: IfaceType -> LHsType RdrName
359
toHsType (IfaceTyVar v) = noLoc $ HsTyVar (mkRdrUnqual (mkTyVarOccFS v))
360
toHsType (IfaceAppTy t1 t2) = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
361
toHsType (IfaceFunTy t1 t2) = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
362
toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts)
363
toHsType (IfaceForAllTy tv t) = add_forall (toHsTvBndr tv) (toHsType t)
365
-- We also need to convert IfaceKinds to Kinds (now that they are different).
366
-- Only a limited form of kind will be encountered... hopefully
367
toKind :: IfaceKind -> Kind
368
toKind (IfaceFunTy ifK1 ifK2) = mkArrowKind (toKind ifK1) (toKind ifK2)
369
toKind (IfaceTyConApp ifKc []) = mkTyConApp (toKindTc ifKc) []
370
toKind other = pprPanic "toKind" (ppr other)
372
toKindTc :: IfaceTyCon -> TyCon
373
toKindTc IfaceLiftedTypeKindTc = liftedTypeKindTyCon
374
toKindTc IfaceOpenTypeKindTc = openTypeKindTyCon
375
toKindTc IfaceUnliftedTypeKindTc = unliftedTypeKindTyCon
376
toKindTc IfaceUbxTupleKindTc = ubxTupleKindTyCon
377
toKindTc IfaceArgTypeKindTc = argTypeKindTyCon
378
toKindTc other = pprPanic "toKindTc" (ppr other)
380
ifaceTcType ifTc = IfaceTyConApp ifTc []
382
ifaceLiftedTypeKind = ifaceTcType IfaceLiftedTypeKindTc
383
ifaceOpenTypeKind = ifaceTcType IfaceOpenTypeKindTc
384
ifaceUnliftedTypeKind = ifaceTcType IfaceUnliftedTypeKindTc
386
ifaceArrow ifT1 ifT2 = IfaceFunTy ifT1 ifT2
388
ifaceEq ifT1 ifT2 = IfacePredTy (IfaceEqPred ifT1 ifT2)
390
toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
391
toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual (mkTyVarOccFS tv)) (toKind k)
393
ifaceExtRdrName :: Name -> RdrName
394
ifaceExtRdrName name = mkOrig (nameModule name) (nameOccName name)
395
ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
397
add_forall tv (L _ (HsForAllTy exp tvs cxt t))
398
= noLoc $ HsForAllTy exp (tv:tvs) cxt t
400
= noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
403
happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l