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