Remove GHC's haskell98 dependency
[ghc-hetmet.git] / compiler / parser / ParserCore.y
1 {
2 {-# OPTIONS -Wwarn -w #-}
3 -- The above warning supression flag is a temporary kludge.
4 -- While working on this module you are encouraged to remove it and fix
5 -- any warnings in the module. See
6 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
7 -- for details
8
9 module ParserCore ( parseCore ) where
10
11 import IfaceSyn
12 import ForeignCall
13 import RdrHsSyn
14 import HsSyn
15 import RdrName
16 import OccName
17 import Type ( Kind,
18               liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
19               argTypeKindTyCon, ubxTupleKindTyCon, mkArrowKind, mkTyConApp
20             )
21 import Name( Name, nameOccName, nameModule, mkExternalName )
22 import Module
23 import ParserCoreUtils
24 import LexCore
25 import Literal
26 import SrcLoc
27 import TysPrim( wordPrimTyCon, intPrimTyCon, charPrimTyCon, 
28                 floatPrimTyCon, doublePrimTyCon, addrPrimTyCon )
29 import TyCon ( TyCon, tyConName )
30 import FastString
31 import Outputable
32 import Data.Char
33 import Unique
34
35 #include "../HsVersions.h"
36
37 }
38
39 %name parseCore
40 %expect 0
41 %tokentype { Token }
42
43 %token
44  '%module'      { TKmodule }
45  '%data'        { TKdata }
46  '%newtype'     { TKnewtype }
47  '%forall'      { TKforall }
48  '%rec'         { TKrec }
49  '%let'         { TKlet }
50  '%in'          { TKin }
51  '%case'        { TKcase }
52  '%of'          { TKof }
53  '%cast'        { TKcast }
54  '%note'        { TKnote }
55  '%external'    { TKexternal }
56  '%local'       { TKlocal }
57  '%_'           { TKwild }
58  '('            { TKoparen }
59  ')'            { TKcparen }
60  '{'            { TKobrace }
61  '}'            { TKcbrace }
62  '#'            { TKhash}
63  '='            { TKeq }
64  ':'            { TKcolon }
65  '::'           { TKcoloncolon }
66  ':=:'          { TKcoloneqcolon }
67  '*'            { TKstar }
68  '->'           { TKrarrow }
69  '\\'           { TKlambda}
70  '@'            { TKat }
71  '.'            { TKdot }
72  '?'            { TKquestion}
73  ';'            { TKsemicolon }
74  NAME           { TKname $$ }
75  CNAME          { TKcname $$ }
76  INTEGER        { TKinteger $$ }
77  RATIONAL       { TKrational $$ }
78  STRING         { TKstring $$ }
79  CHAR           { TKchar $$ }
80
81 %monad { P } { thenP } { returnP }
82 %lexer { lexer } { TKEOF }
83
84 %%
85
86 module  :: { HsExtCore RdrName }
87         -- : '%module' modid tdefs vdefgs       { HsExtCore $2 $3 $4 }
88         : '%module' modid tdefs vdefgs  { HsExtCore $2 [] [] }
89
90
91 -------------------------------------------------------------
92 --     Names: the trickiest bit in here
93
94 -- A name of the form A.B.C could be:
95 --   module A.B.C
96 --   dcon C in module A.B
97 --   tcon C in module A.B
98 modid   :: { Module }
99         : NAME ':' mparts               { undefined }
100
101 q_dc_name :: { Name }
102           : NAME ':' mparts             { undefined }
103
104 q_tc_name :: { Name }
105           : NAME ':' mparts             { undefined }
106
107 q_var_occ :: { Name }
108           : NAME ':' vparts             { undefined }
109
110 mparts  :: { [String] }
111         : CNAME                         { [$1] }
112         | CNAME '.' mparts              { $1:$3 }
113
114 vparts  :: { [String] }
115         : var_occ                       { [$1] }
116         | CNAME '.' vparts              { $1:$3 }
117
118 -------------------------------------------------------------
119 --     Type and newtype declarations are in HsSyn syntax
120
121 tdefs   :: { [TyClDecl RdrName] }
122         : {- empty -}   {[]}
123         | tdef tdefs    {$1:$2}
124
125 tdef    :: { TyClDecl RdrName }
126         : '%data' q_tc_name tv_bndrs '=' '{' cons '}' ';'
127         { TyData { tcdND = DataType, tcdCtxt = noLoc [] 
128                  , tcdLName = noLoc (ifaceExtRdrName $2)
129                  , tcdTyVars = map toHsTvBndr $3
130                  , tcdTyPats = Nothing, tcdKindSig = Nothing
131                  , tcdCons = $6, tcdDerivs = Nothing } }
132         | '%newtype' q_tc_name tv_bndrs trep ';'
133                 { let tc_rdr = ifaceExtRdrName $2 in
134                     TyData { tcdND = NewType, tcdCtxt = noLoc []
135                              , tcdLName = noLoc tc_rdr
136                              , tcdTyVars = map toHsTvBndr $3
137                              , tcdTyPats = Nothing, tcdKindSig = Nothing
138                              , tcdCons = $4 (rdrNameOcc tc_rdr), tcdDerivs = Nothing } }
139
140 -- For a newtype we have to invent a fake data constructor name
141 -- It doesn't matter what it is, because it won't be used
142 trep    :: { OccName -> [LConDecl RdrName] }
143         : {- empty -}   { (\ tc_occ -> []) }
144         | '=' ty        { (\ tc_occ -> let { dc_name  = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
145                                              con_info = PrefixCon [toHsType $2] }
146                                         in [noLoc $ mkSimpleConDecl (noLoc dc_name) []
147                                                        (noLoc []) con_info]) }
148
149 cons    :: { [LConDecl RdrName] }
150         : {- empty -}   { [] } -- 20060420 Empty data types allowed. jds
151         | con           { [$1] }
152         | con ';' cons  { $1:$3 }
153
154 con     :: { LConDecl RdrName }
155         : d_pat_occ attv_bndrs hs_atys 
156                 { noLoc $ mkSimpleConDecl (noLoc (mkRdrUnqual $1)) $2 (noLoc []) (PrefixCon $3) }
157 -- ToDo: parse record-style declarations
158
159 attv_bndrs :: { [LHsTyVarBndr RdrName] }
160         : {- empty -}            { [] }
161         | '@' tv_bndr attv_bndrs {  toHsTvBndr $2 : $3 }
162
163 hs_atys :: { [LHsType RdrName] }
164          : atys               { map toHsType $1 }
165
166
167 ---------------------------------------
168 --                 Types
169 ---------------------------------------
170
171 atys    :: { [IfaceType] }
172         : {- empty -}   { [] }
173         | aty atys      { $1:$2 }
174
175 aty     :: { IfaceType }
176         : fs_var_occ { IfaceTyVar $1 }
177         | q_tc_name  { IfaceTyConApp (IfaceTc $1) [] }
178         | '(' ty ')' { $2 }
179
180 bty     :: { IfaceType }
181         : fs_var_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
182         | q_var_occ atys  { undefined }
183         | q_tc_name atys  { IfaceTyConApp (IfaceTc $1) $2 }
184         | '(' ty ')' { $2 }
185
186 ty      :: { IfaceType }
187         : bty                        { $1 }
188         | bty '->' ty                { IfaceFunTy $1 $3 }
189         | '%forall' tv_bndrs '.' ty  { foldr IfaceForAllTy $4 $2 }
190
191 ----------------------------------------------
192 --        Bindings are in Iface syntax
193
194 vdefgs  :: { [IfaceBinding] }
195         : {- empty -}           { [] }
196         | let_bind ';' vdefgs   { $1 : $3 }
197
198 let_bind :: { IfaceBinding }
199         : '%rec' '{' vdefs1 '}' { IfaceRec $3 } -- Can be empty. Do we care?
200         |  vdef                 { let (b,r) = $1
201                                   in IfaceNonRec b r }
202
203 vdefs1  :: { [(IfaceLetBndr, IfaceExpr)] }
204         : vdef                  { [$1] }
205         | vdef ';' vdefs1       { $1:$3 }
206
207 vdef    :: { (IfaceLetBndr, IfaceExpr) }
208         : fs_var_occ '::' ty '=' exp { (IfLetBndr $1 $3 NoInfo, $5) }
209         | '%local' vdef              { $2 }
210
211   -- NB: qd_occ includes data constructors, because
212   --     we allow data-constructor wrappers at top level
213   -- But we discard the module name, because it must be the
214   -- same as the module being compiled, and Iface syntax only
215   -- has OccNames in binding positions. Ah, but it has Names now!
216
217 ---------------------------------------
218 --  Binders
219 bndr    :: { IfaceBndr }
220         : '@' tv_bndr   { IfaceTvBndr $2 }
221         | id_bndr       { IfaceIdBndr $1 }
222
223 bndrs   :: { [IfaceBndr] }
224         : bndr          { [$1] }
225         | bndr bndrs    { $1:$2 }
226
227 id_bndr :: { IfaceIdBndr }
228         : '(' fs_var_occ '::' ty ')'    { ($2,$4) }
229
230 tv_bndr :: { IfaceTvBndr }
231         :  fs_var_occ                    { ($1, ifaceLiftedTypeKind) }
232         |  '(' fs_var_occ '::' akind ')' { ($2, $4) }
233
234 tv_bndrs        :: { [IfaceTvBndr] }
235         : {- empty -}   { [] }
236         | tv_bndr tv_bndrs      { $1:$2 }
237
238 akind   :: { IfaceKind }
239         : '*'              { ifaceLiftedTypeKind }      
240         | '#'              { ifaceUnliftedTypeKind }
241         | '?'              { ifaceOpenTypeKind }
242         | '(' kind ')'     { $2 }
243
244 kind    :: { IfaceKind }
245         : akind            { $1 }
246         | akind '->' kind  { ifaceArrow $1 $3 }
247         | ty ':=:' ty      { ifaceEq $1 $3 }
248
249 -----------------------------------------
250 --             Expressions
251
252 aexp    :: { IfaceExpr }
253         : fs_var_occ    { IfaceLcl $1 }
254         | q_var_occ     { IfaceExt $1 }
255         | q_dc_name     { IfaceExt $1 }
256         | lit           { IfaceLit $1 }
257         | '(' exp ')'   { $2 }
258
259 fexp    :: { IfaceExpr }
260         : fexp aexp     { IfaceApp $1 $2 }
261         | fexp '@' aty  { IfaceApp $1 (IfaceType $3) }
262         | aexp          { $1 }
263
264 exp     :: { IfaceExpr }
265         : fexp                        { $1 }
266         | '\\' bndrs '->' exp         { foldr IfaceLam $4 $2 }
267         | '%let' let_bind '%in' exp   { IfaceLet $2 $4 }
268 -- gaw 2004
269         | '%case' '(' ty ')' aexp '%of' id_bndr
270           '{' alts1 '}'               { IfaceCase $5 (fst $7) $3 $9 }
271         | '%cast' aexp aty { IfaceCast $2 $3 }
272         | '%note' STRING exp       
273             { case $2 of
274                --"SCC"      -> IfaceNote (IfaceSCC "scc") $3
275                "InlineMe"   -> IfaceNote IfaceInlineMe $3
276             }
277         | '%external' STRING aty   { IfaceFCall (ForeignCall.CCall 
278                                                     (CCallSpec (StaticTarget (mkFastString $2)) 
279                                                                CCallConv (PlaySafe False))) 
280                                                  $3 }
281
282 alts1   :: { [IfaceAlt] }
283         : alt           { [$1] }
284         | alt ';' alts1 { $1:$3 }
285
286 alt     :: { IfaceAlt }
287         : q_dc_name bndrs '->' exp 
288                 { (IfaceDataAlt $1, map ifaceBndrName $2, $4) } 
289                        -- The external syntax currently includes the types of the
290                        -- the args, but they aren't needed internally
291                        -- Nor is the module qualifier
292         | q_dc_name '->' exp 
293                 { (IfaceDataAlt $1, [], $3) } 
294         | lit '->' exp
295                 { (IfaceLitAlt $1, [], $3) }
296         | '%_' '->' exp
297                 { (IfaceDefault, [], $3) }
298
299 lit     :: { Literal }
300         : '(' INTEGER '::' aty ')'      { convIntLit $2 $4 }
301         | '(' RATIONAL '::' aty ')'     { convRatLit $2 $4 }
302         | '(' CHAR '::' aty ')'         { MachChar $2 }
303         | '(' STRING '::' aty ')'       { MachStr (mkFastString $2) }
304
305 fs_var_occ      :: { FastString }
306                 : NAME  { mkFastString $1 }
307
308 var_occ :: { String }
309         : NAME  { $1 }
310
311
312 -- Data constructor in a pattern or data type declaration; use the dataName, 
313 -- because that's what we expect in Core case patterns
314 d_pat_occ :: { OccName }
315         : CNAME      { mkOccName dataName $1 }
316
317 {
318
319 ifaceKind kc = IfaceTyConApp kc []
320
321 ifaceBndrName (IfaceIdBndr (n,_)) = n
322 ifaceBndrName (IfaceTvBndr (n,_)) = n
323
324 convIntLit :: Integer -> IfaceType -> Literal
325 convIntLit i (IfaceTyConApp tc [])
326   | tc `eqTc` intPrimTyCon  = MachInt  i  
327   | tc `eqTc` wordPrimTyCon = MachWord i
328   | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
329   | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
330 convIntLit i aty
331   = pprPanic "Unknown integer literal type" (ppr aty)
332
333 convRatLit :: Rational -> IfaceType -> Literal
334 convRatLit r (IfaceTyConApp tc [])
335   | tc `eqTc` floatPrimTyCon  = MachFloat  r
336   | tc `eqTc` doublePrimTyCon = MachDouble r
337 convRatLit i aty
338   = pprPanic "Unknown rational literal type" (ppr aty)
339
340 eqTc :: IfaceTyCon -> TyCon -> Bool   -- Ugh!
341 eqTc (IfaceTc name) tycon = name == tyConName tycon
342
343 -- Tiresomely, we have to generate both HsTypes (in type/class decls) 
344 -- and IfaceTypes (in Core expressions).  So we parse them as IfaceTypes,
345 -- and convert to HsTypes here.  But the IfaceTypes we can see here
346 -- are very limited (see the productions for 'ty', so the translation
347 -- isn't hard
348 toHsType :: IfaceType -> LHsType RdrName
349 toHsType (IfaceTyVar v)                  = noLoc $ HsTyVar (mkRdrUnqual (mkTyVarOccFS v))
350 toHsType (IfaceAppTy t1 t2)              = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
351 toHsType (IfaceFunTy t1 t2)              = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
352 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts) 
353 toHsType (IfaceForAllTy tv t)            = add_forall (toHsTvBndr tv) (toHsType t)
354
355 -- We also need to convert IfaceKinds to Kinds (now that they are different).
356 -- Only a limited form of kind will be encountered... hopefully
357 toKind :: IfaceKind -> Kind
358 toKind (IfaceFunTy ifK1 ifK2)  = mkArrowKind (toKind ifK1) (toKind ifK2)
359 toKind (IfaceTyConApp ifKc []) = mkTyConApp (toKindTc ifKc) []
360 toKind other                   = pprPanic "toKind" (ppr other)
361
362 toKindTc :: IfaceTyCon -> TyCon
363 toKindTc IfaceLiftedTypeKindTc   = liftedTypeKindTyCon
364 toKindTc IfaceOpenTypeKindTc     = openTypeKindTyCon
365 toKindTc IfaceUnliftedTypeKindTc = unliftedTypeKindTyCon
366 toKindTc IfaceUbxTupleKindTc     = ubxTupleKindTyCon
367 toKindTc IfaceArgTypeKindTc      = argTypeKindTyCon
368 toKindTc other                   = pprPanic "toKindTc" (ppr other)
369
370 ifaceTcType ifTc = IfaceTyConApp ifTc []
371
372 ifaceLiftedTypeKind   = ifaceTcType IfaceLiftedTypeKindTc
373 ifaceOpenTypeKind     = ifaceTcType IfaceOpenTypeKindTc
374 ifaceUnliftedTypeKind = ifaceTcType IfaceUnliftedTypeKindTc
375
376 ifaceArrow ifT1 ifT2 = IfaceFunTy ifT1 ifT2
377
378 ifaceEq ifT1 ifT2 = IfacePredTy (IfaceEqPred ifT1 ifT2)
379
380 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
381 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual (mkTyVarOccFS tv)) (toKind k)
382
383 ifaceExtRdrName :: Name -> RdrName
384 ifaceExtRdrName name = mkOrig (nameModule name) (nameOccName name)
385 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
386
387 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
388   = noLoc $ HsForAllTy exp (tv:tvs) cxt t
389 add_forall tv t
390   = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
391   
392 happyError :: P a 
393 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l
394 }
395