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