75f7b1b3656936cf13cc96c7662e53d1d59462ce
[ghc-hetmet.git] / ghc / 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, nameModuleName )
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
73                 { HsExtCore (mkHomeModule $2) $3 $4 }
74
75 modid   :: { ModuleName }
76         : CNAME                  { mkSysModuleNameFS (mkFastString $1) }
77
78 -------------------------------------------------------------
79 --     Type and newtype declarations are in HsSyn syntax
80
81 tdefs   :: { [TyClDecl RdrName] }
82         : {- empty -}   {[]}
83         | tdef ';' tdefs        {$1:$3}
84
85 tdef    :: { TyClDecl RdrName }
86         : '%data' q_tc_name tv_bndrs '=' '{' cons1 '}'
87                 { mkTyData DataType (noLoc (noLoc [], noLoc (ifaceExtRdrName $2), map toHsTvBndr $3)) Nothing $6 Nothing }
88         | '%newtype' q_tc_name tv_bndrs trep 
89                 { let tc_rdr = ifaceExtRdrName $2 in
90                   mkTyData NewType (noLoc (noLoc [], noLoc tc_rdr, map toHsTvBndr $3)) Nothing ($4 (rdrNameOcc tc_rdr)) Nothing }
91
92 -- For a newtype we have to invent a fake data constructor name
93 -- It doesn't matter what it is, because it won't be used
94 trep    :: { OccName -> [LConDecl RdrName] }
95         : {- empty -}   { (\ tc_occ -> []) }
96         | '=' ty        { (\ tc_occ -> let { dc_name  = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
97                                              con_info = PrefixCon [toHsType $2] }
98                                         in [noLoc $ ConDecl (noLoc dc_name) []
99                                            (noLoc []) con_info]) }
100
101 cons1   :: { [LConDecl RdrName] }
102         : con           { [$1] }
103         | con ';' cons1 { $1:$3 }
104
105 con     :: { LConDecl RdrName }
106         : d_pat_occ attv_bndrs hs_atys 
107                 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) $2 (noLoc []) (PrefixCon $3)}
108         | d_pat_occ '::' ty
109                 { noLoc $ GadtDecl (noLoc (mkRdrUnqual $1)) (toHsType $3) } 
110
111 attv_bndrs :: { [LHsTyVarBndr RdrName] }
112         : {- empty -}            { [] }
113         | '@' tv_bndr attv_bndrs {  toHsTvBndr $2 : $3 }
114
115 hs_atys :: { [LHsType RdrName] }
116          : atys               { map toHsType $1 }
117
118
119 ---------------------------------------
120 --                 Types
121 ---------------------------------------
122
123 atys    :: { [IfaceType] }
124         : {- empty -}   { [] }
125         | aty atys      { $1:$2 }
126
127 aty     :: { IfaceType }
128         : tv_occ    { IfaceTyVar $1 }
129         | q_tc_name  { IfaceTyConApp (IfaceTc $1) [] }
130         | '(' ty ')' { $2 }
131
132 bty     :: { IfaceType }
133         : tv_occ atys    { foldl IfaceAppTy (IfaceTyVar $1) $2 }
134         | q_tc_name atys  { IfaceTyConApp (IfaceTc $1) $2 }
135
136 ty      :: { IfaceType }
137         : bty                        { $1 }
138         | bty '->' ty                { IfaceFunTy $1 $3 }
139         | '%forall' tv_bndrs '.' ty  { foldr IfaceForAllTy $4 $2 }
140
141 ----------------------------------------------
142 --        Bindings are in Iface syntax
143
144 vdefgs  :: { [IfaceBinding] }
145         : {- empty -}           { [] }
146         | let_bind ';' vdefgs   { $1 : $3 }
147
148 let_bind :: { IfaceBinding }
149         : '%rec' '{' vdefs1 '}' { IfaceRec $3 }
150         |  vdef                 { let (b,r) = $1
151                                   in IfaceNonRec b r }
152
153 vdefs1  :: { [(IfaceIdBndr, IfaceExpr)] }
154         : vdef                  { [$1] }
155         | vdef ';' vdefs1       { $1:$3 }
156
157 vdef    :: { (IfaceIdBndr, IfaceExpr) }
158         : qd_occ '::' ty '=' exp { (($1, $3), $5) }
159   -- NB: qd_occ includes data constructors, because
160   --     we allow data-constructor wrappers at top level
161   -- But we discard the module name, because it must be the
162   -- same as the module being compiled, and Iface syntax only
163   -- has OccNames in binding positions
164
165 qd_occ :: { OccName }
166         : var_occ { $1 }
167         | d_occ   { $1 }
168
169 ---------------------------------------
170 --  Binders
171 bndr    :: { IfaceBndr }
172         : '@' tv_bndr   { IfaceTvBndr $2 }
173         | id_bndr       { IfaceIdBndr $1 }
174
175 bndrs   :: { [IfaceBndr] }
176         : bndr          { [$1] }
177         | bndr bndrs    { $1:$2 }
178
179 id_bndr :: { IfaceIdBndr }
180         : '(' var_occ '::' ty ')'       { ($2,$4) }
181
182 id_bndrs :: { [IfaceIdBndr] }
183         : {-empty -}            { [] }
184         | id_bndr id_bndrs      { $1:$2 }
185
186 tv_bndr :: { IfaceTvBndr }
187         :  tv_occ                    { ($1, LiftedTypeKind) }
188         |  '(' tv_occ '::' akind ')' { ($2, $4) }
189
190 tv_bndrs        :: { [IfaceTvBndr] }
191         : {- empty -}   { [] }
192         | tv_bndr tv_bndrs      { $1:$2 }
193
194 akind   :: { IfaceKind }
195         : '*'              { LiftedTypeKind   } 
196         | '#'              { UnliftedTypeKind }
197         | '?'              { OpenTypeKind     }
198         | '(' kind ')'     { $2 }
199
200 kind    :: { IfaceKind }
201         : akind            { $1 }
202         | akind '->' kind  { FunKind $1 $3 }
203
204 -----------------------------------------
205 --             Expressions
206
207 aexp    :: { IfaceExpr }
208         : var_occ                { IfaceLcl $1 }
209         | modid '.' qd_occ       { IfaceExt (ExtPkg $1 $3) }
210         | lit           { IfaceLit $1 }
211         | '(' exp ')'   { $2 }
212
213 fexp    :: { IfaceExpr }
214         : fexp aexp     { IfaceApp $1 $2 }
215         | fexp '@' aty  { IfaceApp $1 (IfaceType $3) }
216         | aexp          { $1 }
217
218 exp     :: { IfaceExpr }
219         : fexp                        { $1 }
220         | '\\' bndrs '->' exp         { foldr IfaceLam $4 $2 }
221         | '%let' let_bind '%in' exp   { IfaceLet $2 $4 }
222 -- gaw 2004
223         | '%case' '(' ty ')' aexp '%of' id_bndr
224           '{' alts1 '}'               { IfaceCase $5 (fst $7) $3 $9 }
225         | '%coerce' aty exp           { IfaceNote (IfaceCoerce $2) $3 }
226         | '%note' STRING exp       
227             { case $2 of
228                --"SCC"      -> IfaceNote (IfaceSCC "scc") $3
229                "InlineCall" -> IfaceNote IfaceInlineCall $3
230                "InlineMe"   -> IfaceNote IfaceInlineMe $3
231             }
232         | '%external' STRING aty   { IfaceFCall (ForeignCall.CCall 
233                                                     (CCallSpec (StaticTarget (mkFastString $2)) 
234                                                                CCallConv (PlaySafe False))) 
235                                                  $3 }
236
237 alts1   :: { [IfaceAlt] }
238         : alt           { [$1] }
239         | alt ';' alts1 { $1:$3 }
240
241 alt     :: { IfaceAlt }
242         : modid '.' d_pat_occ bndrs '->' exp 
243                 { (IfaceDataAlt $3, map ifaceBndrName $4, $6) } 
244                        -- The external syntax currently includes the types of the
245                        -- the args, but they aren't needed internally
246                        -- Nor is the module qualifier
247         | lit '->' exp
248                 { (IfaceLitAlt $1, [], $3) }
249         | '%_' '->' exp
250                 { (IfaceDefault, [], $3) }
251
252 lit     :: { Literal }
253         : '(' INTEGER '::' aty ')'      { convIntLit $2 $4 }
254         | '(' RATIONAL '::' aty ')'     { convRatLit $2 $4 }
255         | '(' CHAR '::' aty ')'         { MachChar $2 }
256         | '(' STRING '::' aty ')'       { MachStr (mkFastString $2) }
257
258 tv_occ  :: { OccName }
259         : NAME  { mkSysOcc tvName $1 }
260
261 var_occ :: { OccName }
262         : NAME  { mkSysOcc varName $1 }
263
264
265 -- Type constructor
266 q_tc_name       :: { IfaceExtName }
267         : modid '.' CNAME       { ExtPkg $1 (mkSysOcc tcName $3) }
268
269 -- Data constructor in a pattern or data type declaration; use the dataName, 
270 -- because that's what we expect in Core case patterns
271 d_pat_occ :: { OccName }
272         : CNAME      { mkSysOcc dataName $1 }
273
274 -- Data constructor occurrence in an expression;
275 -- use the varName because that's the worker Id
276 d_occ :: { OccName }
277        : CNAME { mkSysOcc varName $1 }
278
279 {
280
281 ifaceBndrName (IfaceIdBndr (n,_)) = n
282 ifaceBndrName (IfaceTvBndr (n,_)) = n
283
284 convIntLit :: Integer -> IfaceType -> Literal
285 convIntLit i (IfaceTyConApp tc [])
286   | tc `eqTc` intPrimTyCon  = MachInt  i  
287   | tc `eqTc` wordPrimTyCon = MachWord i
288   | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
289   | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
290 convIntLit i aty
291   = pprPanic "Unknown integer literal type" (ppr aty)
292
293 convRatLit :: Rational -> IfaceType -> Literal
294 convRatLit r (IfaceTyConApp tc [])
295   | tc `eqTc` floatPrimTyCon  = MachFloat  r
296   | tc `eqTc` doublePrimTyCon = MachDouble r
297 convRatLit i aty
298   = pprPanic "Unknown rational literal type" (ppr aty)
299
300 eqTc :: IfaceTyCon -> TyCon -> Bool   -- Ugh!
301 eqTc (IfaceTc (ExtPkg mod occ)) tycon
302   = mod == nameModuleName nm && occ == nameOccName nm
303   where
304     nm = tyConName tycon
305
306 -- Tiresomely, we have to generate both HsTypes (in type/class decls) 
307 -- and IfaceTypes (in Core expressions).  So we parse them as IfaceTypes,
308 -- and convert to HsTypes here.  But the IfaceTypes we can see here
309 -- are very limited (see the productions for 'ty', so the translation
310 -- isn't hard
311 toHsType :: IfaceType -> LHsType RdrName
312 toHsType (IfaceTyVar v)                  = noLoc $ HsTyVar (mkRdrUnqual v)
313 toHsType (IfaceAppTy t1 t2)              = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
314 toHsType (IfaceFunTy t1 t2)              = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
315 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts) 
316 toHsType (IfaceForAllTy tv t)            = add_forall (toHsTvBndr tv) (toHsType t)
317
318 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
319 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual tv) k
320
321 ifaceExtRdrName :: IfaceExtName -> RdrName
322 ifaceExtRdrName (ExtPkg mod occ) = mkOrig mod occ
323 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
324
325 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
326   = noLoc $ HsForAllTy exp (tv:tvs) cxt t
327 add_forall tv t
328   = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
329   
330 happyError :: P a 
331 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l
332 }
333