3210583f9651cfa70247dadca7adafb2fa17fd42
[ghc-hetmet.git] / compiler / parser / ParserCore.y
1 {
2 module ParserCore ( parseCore ) where
3
4 import IfaceSyn
5 import ForeignCall
6 import RdrHsSyn
7 import HsSyn
8 import RdrName
9 import OccName
10 import Kind( Kind(..) )
11 import Name( nameOccName, nameModule )
12 import Module
13 import ParserCoreUtils
14 import LexCore
15 import Literal
16 import SrcLoc
17 import TysPrim( wordPrimTyCon, intPrimTyCon, charPrimTyCon, 
18                 floatPrimTyCon, doublePrimTyCon, addrPrimTyCon )
19 import TyCon ( TyCon, tyConName )
20 import FastString
21 import Outputable
22 import Char
23
24 #include "../HsVersions.h"
25
26 }
27
28 %name parseCore
29 %tokentype { Token }
30
31 %token
32  '%module'      { TKmodule }
33  '%data'        { TKdata }
34  '%newtype'     { TKnewtype }
35  '%forall'      { TKforall }
36  '%rec'         { TKrec }
37  '%let'         { TKlet }
38  '%in'          { TKin }
39  '%case'        { TKcase }
40  '%of'          { TKof }
41  '%coerce'      { TKcoerce }
42  '%note'        { TKnote }
43  '%external'    { TKexternal }
44  '%_'           { TKwild }
45  '('            { TKoparen }
46  ')'            { TKcparen }
47  '{'            { TKobrace }
48  '}'            { TKcbrace }
49  '#'            { TKhash}
50  '='            { TKeq }
51  '::'           { TKcoloncolon }
52  '*'            { TKstar }
53  '->'           { TKrarrow }
54  '\\'           { TKlambda}
55  '@'            { TKat }
56  '.'            { TKdot }
57  '?'            { TKquestion}
58  ';'            { TKsemicolon }
59  NAME           { TKname $$ }
60  CNAME          { TKcname $$ }
61  INTEGER        { TKinteger $$ }
62  RATIONAL       { TKrational $$ }
63  STRING         { TKstring $$ }
64  CHAR           { TKchar $$ }
65
66 %monad { P } { thenP } { returnP }
67 %lexer { lexer } { TKEOF }
68
69 %%
70
71 module  :: { HsExtCore RdrName }
72          : '%module' modid tdefs vdefgs { HsExtCore $2 $3 $4 }
73
74 modid   :: { Module }
75         : CNAME                  { mkModuleFS (mkFastString $1) }
76
77 -------------------------------------------------------------
78 --     Type and newtype declarations are in HsSyn syntax
79
80 tdefs   :: { [TyClDecl RdrName] }
81         : {- empty -}   {[]}
82         | tdef ';' tdefs        {$1:$3}
83
84 tdef    :: { TyClDecl RdrName }
85         : '%data' q_tc_name tv_bndrs '=' '{' cons1 '}'
86                 { mkTyData DataType (noLoc [], noLoc (ifaceExtRdrName $2), map toHsTvBndr $3) Nothing $6 Nothing }
87         | '%newtype' q_tc_name tv_bndrs trep 
88                 { let tc_rdr = ifaceExtRdrName $2 in
89                   mkTyData NewType (noLoc [], noLoc tc_rdr, map toHsTvBndr $3) Nothing ($4 (rdrNameOcc tc_rdr)) Nothing }
90
91 -- For a newtype we have to invent a fake data constructor name
92 -- It doesn't matter what it is, because it won't be used
93 trep    :: { OccName -> [LConDecl RdrName] }
94         : {- empty -}   { (\ tc_occ -> []) }
95         | '=' ty        { (\ tc_occ -> let { dc_name  = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
96                                              con_info = PrefixCon [toHsType $2] }
97                                         in [noLoc $ ConDecl (noLoc dc_name) Explicit []
98                                            (noLoc []) con_info ResTyH98]) }
99
100 cons1   :: { [LConDecl RdrName] }
101         : con           { [$1] }
102         | con ';' cons1 { $1:$3 }
103
104 con     :: { LConDecl RdrName }
105         : d_pat_occ attv_bndrs hs_atys 
106                 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) Explicit $2 (noLoc []) (PrefixCon $3) ResTyH98}
107         | d_pat_occ '::' ty
108                 -- XXX - autrijus - $3 needs to be split into argument and return types!
109                 -- also not sure whether the [] below (quantified vars) appears.
110                 -- also the "PrefixCon []" is wrong.
111                 -- also we want to munge $3 somehow.
112                 -- extractWhatEver to unpack ty into the parts to ConDecl
113                 -- XXX - define it somewhere in RdrHsSyn
114                 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) Explicit [] (noLoc []) (PrefixCon []) (undefined $3) }
115
116 attv_bndrs :: { [LHsTyVarBndr RdrName] }
117         : {- empty -}            { [] }
118         | '@' tv_bndr attv_bndrs {  toHsTvBndr $2 : $3 }
119
120 hs_atys :: { [LHsType RdrName] }
121          : atys               { map toHsType $1 }
122
123
124 ---------------------------------------
125 --                 Types
126 ---------------------------------------
127
128 atys    :: { [IfaceType] }
129         : {- empty -}   { [] }
130         | aty atys      { $1:$2 }
131
132 aty     :: { IfaceType }
133         : tv_occ    { IfaceTyVar $1 }
134         | q_tc_name  { IfaceTyConApp (IfaceTc $1) [] }
135         | '(' ty ')' { $2 }
136
137 bty     :: { IfaceType }
138         : tv_occ atys    { foldl IfaceAppTy (IfaceTyVar $1) $2 }
139         | q_tc_name atys  { IfaceTyConApp (IfaceTc $1) $2 }
140         | '(' ty ')' { $2 }
141
142 ty      :: { IfaceType }
143         : bty                        { $1 }
144         | bty '->' ty                { IfaceFunTy $1 $3 }
145         | '%forall' tv_bndrs '.' ty  { foldr IfaceForAllTy $4 $2 }
146
147 ----------------------------------------------
148 --        Bindings are in Iface syntax
149
150 vdefgs  :: { [IfaceBinding] }
151         : {- empty -}           { [] }
152         | let_bind ';' vdefgs   { $1 : $3 }
153
154 let_bind :: { IfaceBinding }
155         : '%rec' '{' vdefs1 '}' { IfaceRec $3 }
156         |  vdef                 { let (b,r) = $1
157                                   in IfaceNonRec b r }
158
159 vdefs1  :: { [(IfaceIdBndr, IfaceExpr)] }
160         : vdef                  { [$1] }
161         | vdef ';' vdefs1       { $1:$3 }
162
163 vdef    :: { (IfaceIdBndr, IfaceExpr) }
164         : qd_occ '::' ty '=' exp { (($1, $3), $5) }
165   -- NB: qd_occ includes data constructors, because
166   --     we allow data-constructor wrappers at top level
167   -- But we discard the module name, because it must be the
168   -- same as the module being compiled, and Iface syntax only
169   -- has OccNames in binding positions
170
171 qd_occ :: { OccName }
172         : var_occ { $1 }
173         | d_occ   { $1 }
174
175 ---------------------------------------
176 --  Binders
177 bndr    :: { IfaceBndr }
178         : '@' tv_bndr   { IfaceTvBndr $2 }
179         | id_bndr       { IfaceIdBndr $1 }
180
181 bndrs   :: { [IfaceBndr] }
182         : bndr          { [$1] }
183         | bndr bndrs    { $1:$2 }
184
185 id_bndr :: { IfaceIdBndr }
186         : '(' var_occ '::' ty ')'       { ($2,$4) }
187
188 id_bndrs :: { [IfaceIdBndr] }
189         : {-empty -}            { [] }
190         | id_bndr id_bndrs      { $1:$2 }
191
192 tv_bndr :: { IfaceTvBndr }
193         :  tv_occ                    { ($1, LiftedTypeKind) }
194         |  '(' tv_occ '::' akind ')' { ($2, $4) }
195
196 tv_bndrs        :: { [IfaceTvBndr] }
197         : {- empty -}   { [] }
198         | tv_bndr tv_bndrs      { $1:$2 }
199
200 akind   :: { IfaceKind }
201         : '*'              { LiftedTypeKind   } 
202         | '#'              { UnliftedTypeKind }
203         | '?'              { OpenTypeKind     }
204         | '(' kind ')'     { $2 }
205
206 kind    :: { IfaceKind }
207         : akind            { $1 }
208         | akind '->' kind  { FunKind $1 $3 }
209
210 -----------------------------------------
211 --             Expressions
212
213 aexp    :: { IfaceExpr }
214         : var_occ                { IfaceLcl $1 }
215         | modid '.' qd_occ       { IfaceExt (ExtPkg $1 $3) }
216         | lit           { IfaceLit $1 }
217         | '(' exp ')'   { $2 }
218
219 fexp    :: { IfaceExpr }
220         : fexp aexp     { IfaceApp $1 $2 }
221         | fexp '@' aty  { IfaceApp $1 (IfaceType $3) }
222         | aexp          { $1 }
223
224 exp     :: { IfaceExpr }
225         : fexp                        { $1 }
226         | '\\' bndrs '->' exp         { foldr IfaceLam $4 $2 }
227         | '%let' let_bind '%in' exp   { IfaceLet $2 $4 }
228 -- gaw 2004
229         | '%case' '(' ty ')' aexp '%of' id_bndr
230           '{' alts1 '}'               { IfaceCase $5 (fst $7) $3 $9 }
231         | '%coerce' aty exp           { IfaceNote (IfaceCoerce $2) $3 }
232         | '%note' STRING exp       
233             { case $2 of
234                --"SCC"      -> IfaceNote (IfaceSCC "scc") $3
235                "InlineCall" -> IfaceNote IfaceInlineCall $3
236                "InlineMe"   -> IfaceNote IfaceInlineMe $3
237             }
238         | '%external' STRING aty   { IfaceFCall (ForeignCall.CCall 
239                                                     (CCallSpec (StaticTarget (mkFastString $2)) 
240                                                                CCallConv (PlaySafe False))) 
241                                                  $3 }
242
243 alts1   :: { [IfaceAlt] }
244         : alt           { [$1] }
245         | alt ';' alts1 { $1:$3 }
246
247 alt     :: { IfaceAlt }
248         : modid '.' d_pat_occ bndrs '->' exp 
249                 { (IfaceDataAlt $3, map ifaceBndrName $4, $6) } 
250                        -- The external syntax currently includes the types of the
251                        -- the args, but they aren't needed internally
252                        -- Nor is the module qualifier
253         | lit '->' exp
254                 { (IfaceLitAlt $1, [], $3) }
255         | '%_' '->' exp
256                 { (IfaceDefault, [], $3) }
257
258 lit     :: { Literal }
259         : '(' INTEGER '::' aty ')'      { convIntLit $2 $4 }
260         | '(' RATIONAL '::' aty ')'     { convRatLit $2 $4 }
261         | '(' CHAR '::' aty ')'         { MachChar $2 }
262         | '(' STRING '::' aty ')'       { MachStr (mkFastString $2) }
263
264 tv_occ  :: { OccName }
265         : NAME  { mkOccName tvName $1 }
266
267 var_occ :: { OccName }
268         : NAME  { mkVarOcc $1 }
269
270
271 -- Type constructor
272 q_tc_name       :: { IfaceExtName }
273         : modid '.' CNAME       { ExtPkg $1 (mkOccName tcName $3) }
274
275 -- Data constructor in a pattern or data type declaration; use the dataName, 
276 -- because that's what we expect in Core case patterns
277 d_pat_occ :: { OccName }
278         : CNAME      { mkOccName dataName $1 }
279
280 -- Data constructor occurrence in an expression;
281 -- use the varName because that's the worker Id
282 d_occ :: { OccName }
283        : CNAME { mkVarOcc $1 }
284
285 {
286
287 ifaceBndrName (IfaceIdBndr (n,_)) = n
288 ifaceBndrName (IfaceTvBndr (n,_)) = n
289
290 convIntLit :: Integer -> IfaceType -> Literal
291 convIntLit i (IfaceTyConApp tc [])
292   | tc `eqTc` intPrimTyCon  = MachInt  i  
293   | tc `eqTc` wordPrimTyCon = MachWord i
294   | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
295   | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
296 convIntLit i aty
297   = pprPanic "Unknown integer literal type" (ppr aty)
298
299 convRatLit :: Rational -> IfaceType -> Literal
300 convRatLit r (IfaceTyConApp tc [])
301   | tc `eqTc` floatPrimTyCon  = MachFloat  r
302   | tc `eqTc` doublePrimTyCon = MachDouble r
303 convRatLit i aty
304   = pprPanic "Unknown rational literal type" (ppr aty)
305
306 eqTc :: IfaceTyCon -> TyCon -> Bool   -- Ugh!
307 eqTc (IfaceTc (ExtPkg mod occ)) tycon
308   = mod == nameModule nm && occ == nameOccName nm
309   where
310     nm = tyConName tycon
311
312 -- Tiresomely, we have to generate both HsTypes (in type/class decls) 
313 -- and IfaceTypes (in Core expressions).  So we parse them as IfaceTypes,
314 -- and convert to HsTypes here.  But the IfaceTypes we can see here
315 -- are very limited (see the productions for 'ty', so the translation
316 -- isn't hard
317 toHsType :: IfaceType -> LHsType RdrName
318 toHsType (IfaceTyVar v)                  = noLoc $ HsTyVar (mkRdrUnqual v)
319 toHsType (IfaceAppTy t1 t2)              = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
320 toHsType (IfaceFunTy t1 t2)              = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
321 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts) 
322 toHsType (IfaceForAllTy tv t)            = add_forall (toHsTvBndr tv) (toHsType t)
323
324 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
325 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual tv) k
326
327 ifaceExtRdrName :: IfaceExtName -> RdrName
328 ifaceExtRdrName (ExtPkg mod occ) = mkOrig mod occ
329 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
330
331 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
332   = noLoc $ HsForAllTy exp (tv:tvs) cxt t
333 add_forall tv t
334   = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
335   
336 happyError :: P a 
337 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l
338 }
339