[project @ 2004-11-26 16:19:45 by simonmar]
[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, 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                  { mkSysModuleFS (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 [], 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 [], 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) []
98                                            (noLoc []) con_info]) }
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)) $2 (noLoc []) (PrefixCon $3)}
107         | d_pat_occ '::' ty
108                 { noLoc $ GadtDecl (noLoc (mkRdrUnqual $1)) (toHsType $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 -- gaw 2004
222         | '%case' '(' ty ')' aexp '%of' id_bndr
223           '{' alts1 '}'               { IfaceCase $5 (fst $7) $3 $9 }
224         | '%coerce' aty exp           { IfaceNote (IfaceCoerce $2) $3 }
225         | '%note' STRING exp       
226             { case $2 of
227                --"SCC"      -> IfaceNote (IfaceSCC "scc") $3
228                "InlineCall" -> IfaceNote IfaceInlineCall $3
229                "InlineMe"   -> IfaceNote IfaceInlineMe $3
230             }
231         | '%external' STRING aty   { IfaceFCall (ForeignCall.CCall 
232                                                     (CCallSpec (StaticTarget (mkFastString $2)) 
233                                                                CCallConv (PlaySafe False))) 
234                                                  $3 }
235
236 alts1   :: { [IfaceAlt] }
237         : alt           { [$1] }
238         | alt ';' alts1 { $1:$3 }
239
240 alt     :: { IfaceAlt }
241         : modid '.' d_pat_occ bndrs '->' exp 
242                 { (IfaceDataAlt $3, map ifaceBndrName $4, $6) } 
243                        -- The external syntax currently includes the types of the
244                        -- the args, but they aren't needed internally
245                        -- Nor is the module qualifier
246         | lit '->' exp
247                 { (IfaceLitAlt $1, [], $3) }
248         | '%_' '->' exp
249                 { (IfaceDefault, [], $3) }
250
251 lit     :: { Literal }
252         : '(' INTEGER '::' aty ')'      { convIntLit $2 $4 }
253         | '(' RATIONAL '::' aty ')'     { convRatLit $2 $4 }
254         | '(' CHAR '::' aty ')'         { MachChar $2 }
255         | '(' STRING '::' aty ')'       { MachStr (mkFastString $2) }
256
257 tv_occ  :: { OccName }
258         : NAME  { mkSysOcc tvName $1 }
259
260 var_occ :: { OccName }
261         : NAME  { mkSysOcc varName $1 }
262
263
264 -- Type constructor
265 q_tc_name       :: { IfaceExtName }
266         : modid '.' CNAME       { ExtPkg $1 (mkSysOcc tcName $3) }
267
268 -- Data constructor in a pattern or data type declaration; use the dataName, 
269 -- because that's what we expect in Core case patterns
270 d_pat_occ :: { OccName }
271         : CNAME      { mkSysOcc dataName $1 }
272
273 -- Data constructor occurrence in an expression;
274 -- use the varName because that's the worker Id
275 d_occ :: { OccName }
276        : CNAME { mkSysOcc varName $1 }
277
278 {
279
280 ifaceBndrName (IfaceIdBndr (n,_)) = n
281 ifaceBndrName (IfaceTvBndr (n,_)) = n
282
283 convIntLit :: Integer -> IfaceType -> Literal
284 convIntLit i (IfaceTyConApp tc [])
285   | tc `eqTc` intPrimTyCon  = MachInt  i  
286   | tc `eqTc` wordPrimTyCon = MachWord i
287   | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
288   | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
289 convIntLit i aty
290   = pprPanic "Unknown integer literal type" (ppr aty)
291
292 convRatLit :: Rational -> IfaceType -> Literal
293 convRatLit r (IfaceTyConApp tc [])
294   | tc `eqTc` floatPrimTyCon  = MachFloat  r
295   | tc `eqTc` doublePrimTyCon = MachDouble r
296 convRatLit i aty
297   = pprPanic "Unknown rational literal type" (ppr aty)
298
299 eqTc :: IfaceTyCon -> TyCon -> Bool   -- Ugh!
300 eqTc (IfaceTc (ExtPkg mod occ)) tycon
301   = mod == nameModule nm && occ == nameOccName nm
302   where
303     nm = tyConName tycon
304
305 -- Tiresomely, we have to generate both HsTypes (in type/class decls) 
306 -- and IfaceTypes (in Core expressions).  So we parse them as IfaceTypes,
307 -- and convert to HsTypes here.  But the IfaceTypes we can see here
308 -- are very limited (see the productions for 'ty', so the translation
309 -- isn't hard
310 toHsType :: IfaceType -> LHsType RdrName
311 toHsType (IfaceTyVar v)                  = noLoc $ HsTyVar (mkRdrUnqual v)
312 toHsType (IfaceAppTy t1 t2)              = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
313 toHsType (IfaceFunTy t1 t2)              = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
314 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts) 
315 toHsType (IfaceForAllTy tv t)            = add_forall (toHsTvBndr tv) (toHsType t)
316
317 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
318 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual tv) k
319
320 ifaceExtRdrName :: IfaceExtName -> RdrName
321 ifaceExtRdrName (ExtPkg mod occ) = mkOrig mod occ
322 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
323
324 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
325   = noLoc $ HsForAllTy exp (tv:tvs) cxt t
326 add_forall tv t
327   = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
328   
329 happyError :: P a 
330 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l
331 }
332