2 % (c) The GRASP/AQUA Project, Glasgow University, 1999
4 \section[ParseUtil]{Parser Utilities}
8 parseError -- String -> Pa
10 , splitForConApp -- RdrNameHsType -> [RdrNameBangType]
11 -- -> P (RdrName, [RdrNameBangType])
13 , mkRecConstrOrUpdate -- HsExp -> [HsFieldUpdate] -> P HsExp
16 , mkExtName -- Maybe ExtName -> RdrName -> ExtName
18 , checkPrec -- String -> P String
19 , checkContext -- HsType -> P HsContext
20 , checkInstType -- HsType -> P HsType
21 , checkDataHeader -- HsQualType -> P (HsContext,HsName,[HsName])
22 , checkSimple -- HsType -> [HsName] -> P ((HsName,[HsName]))
23 , checkPattern -- HsExp -> P HsPat
24 , checkPatterns -- [HsExp] -> P [HsPat]
25 -- , checkExpr -- HsExp -> P HsExp
26 , checkValDef -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
27 , checkValSig -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
30 #include "HsVersions.h"
33 import HsSyn -- Lots of it
35 import RdrHsSyn ( mkNPlusKPatIn, unitTyCon_RDR,
37 RdrNameHsType, RdrNameBangType, RdrNameContext,
38 RdrNameHsTyVar, RdrNamePat, RdrNameHsExpr, RdrNameGRHSs,
39 RdrNameHsRecordBinds, RdrNameMonoBinds
43 import OccName ( dataName, varName, tcClsName,
44 occNameSpace, setOccNameSpace, occNameUserString )
45 import FastString ( unpackFS )
46 import UniqFM ( UniqFM, listToUFM, lookupUFM )
49 -----------------------------------------------------------------------------
52 parseError :: String -> P a
54 getSrcLocP `thenP` \ loc ->
55 failMsgP (hcat [ppr loc, text ": ", text s])
57 cbot = panic "CCall:result_ty"
59 -----------------------------------------------------------------------------
62 -- When parsing data declarations, we sometimes inadvertently parse
63 -- a constructor application as a type (eg. in data T a b = C a b `D` E a b)
64 -- This function splits up the type application, adds any pending
65 -- arguments, and converts the type constructor back into a data constructor.
67 splitForConApp :: RdrNameHsType -> [RdrNameBangType]
68 -> P (RdrName, [RdrNameBangType])
70 splitForConApp t ts = split t ts
72 split (HsAppTy t u) ts = split t (Unbanged u : ts)
74 split (HsTyVar t) ts =
75 -- check that we've got a type constructor at the head
76 if occNameSpace t_occ /= tcClsName
78 (showSDoc (text "not a constructor: `" <>
80 else returnP (con, ts)
81 where t_occ = rdrNameOcc t
82 con = setRdrNameOcc t (setOccNameSpace t_occ dataName)
84 split _ _ = parseError "Illegal data/newtype declaration"
86 ----------------------------------------------------------------------------
87 -- Various Syntactic Checks
89 callConvFM :: UniqFM CallConv
90 callConvFM = listToUFM $
91 map (\ (x,y) -> (_PK_ x,y))
92 [ ("stdcall", stdCallConv),
94 -- ("pascal", pascalCallConv),
95 -- ("fastcall", fastCallConv)
98 checkCallConv :: FAST_STRING -> P CallConv
100 case lookupUFM callConvFM s of
101 Nothing -> parseError ("unknown calling convention: `"
102 ++ unpackFS s ++ "'")
103 Just conv -> returnP conv
105 checkInstType :: RdrNameHsType -> P RdrNameHsType
108 HsForAllTy tvs ctxt ty ->
109 checkDictTy ty [] `thenP` \ dict_ty ->
110 returnP (HsForAllTy tvs ctxt dict_ty)
112 ty -> checkDictTy ty [] `thenP` \ dict_ty->
113 returnP (HsForAllTy Nothing [] dict_ty)
115 checkContext :: RdrNameHsType -> P RdrNameContext
116 checkContext (HsTupleTy _ ts)
117 = mapP (\t -> checkPred t []) ts `thenP` \ps ->
119 checkContext (HsTyVar t) -- empty contexts are allowed
120 | t == unitTyCon_RDR = returnP []
122 = checkPred t [] `thenP` \p ->
125 checkPred :: RdrNameHsType -> [RdrNameHsType]
126 -> P (HsPred RdrName)
127 checkPred (HsTyVar t) args@(_:_) | not (isRdrTyVar t)
128 = returnP (HsPClass t args)
129 checkPred (HsAppTy l r) args = checkPred l (r:args)
130 checkPred (HsPredTy (HsPIParam n ty)) [] = returnP (HsPIParam n ty)
131 checkPred _ _ = parseError "Illegal class assertion"
133 checkDictTy :: RdrNameHsType -> [RdrNameHsType] -> P RdrNameHsType
134 checkDictTy (HsTyVar t) args@(_:_) | not (isRdrTyVar t)
135 = returnP (mkHsDictTy t args)
136 checkDictTy (HsAppTy l r) args = checkDictTy l (r:args)
137 checkDictTy _ _ = parseError "Illegal class assertion"
139 checkDataHeader :: RdrNameHsType
140 -> P (RdrNameContext, RdrName, [RdrNameHsTyVar])
141 checkDataHeader (HsForAllTy Nothing cs t) =
142 checkSimple t [] `thenP` \(c,ts) ->
143 returnP (cs,c,map UserTyVar ts)
145 checkSimple t [] `thenP` \(c,ts) ->
146 returnP ([],c,map UserTyVar ts)
148 checkSimple :: RdrNameHsType -> [RdrName] -> P ((RdrName,[RdrName]))
149 checkSimple (HsAppTy l (HsTyVar a)) xs | isRdrTyVar a
150 = checkSimple l (a:xs)
151 checkSimple (HsTyVar t) xs | not (isRdrTyVar t) = returnP (t,xs)
152 checkSimple t _ = trace (showSDoc (ppr t)) $ parseError "Illegal data/newtype declaration"
154 ---------------------------------------------------------------------------
155 -- Checking Patterns.
157 -- We parse patterns as expressions and check for valid patterns below,
158 -- nverting the expression into a pattern at the same time.
160 checkPattern :: RdrNameHsExpr -> P RdrNamePat
161 checkPattern e = checkPat e []
163 checkPatterns :: [RdrNameHsExpr] -> P [RdrNamePat]
164 checkPatterns es = mapP checkPattern es
166 checkPat :: RdrNameHsExpr -> [RdrNamePat] -> P RdrNamePat
167 checkPat (HsVar c) args | isRdrDataCon c = returnP (ConPatIn c args)
168 checkPat (HsApp f x) args =
169 checkPat x [] `thenP` \x ->
171 checkPat e [] = case e of
172 EWildPat -> returnP WildPatIn
173 HsVar x -> returnP (VarPatIn x)
174 HsLit l -> returnP (LitPatIn l)
175 HsOverLit l -> returnP (NPatIn l)
176 ELazyPat e -> checkPat e [] `thenP` (returnP . LazyPatIn)
177 EAsPat n e -> checkPat e [] `thenP` (returnP . AsPatIn n)
178 ExprWithTySig e t -> checkPat e [] `thenP` \e ->
179 -- Pattern signatures are parsed as sigtypes,
180 -- but they aren't explicit forall points. Hence
181 -- we have to remove the implicit forall here.
183 HsForAllTy Nothing [] ty -> ty
186 returnP (SigPatIn e t')
188 OpApp (HsVar n) (HsVar plus) _ (HsOverLit lit@(HsIntegral k _))
190 -> returnP (mkNPlusKPatIn n lit)
192 OpApp l op fix r -> checkPat l [] `thenP` \l ->
193 checkPat r [] `thenP` \r ->
195 HsVar c -> returnP (ConOpPatIn l c fix r)
198 HsPar e -> checkPat e [] `thenP` (returnP . ParPatIn)
199 ExplicitList es -> mapP (\e -> checkPat e []) es `thenP` \ps ->
200 returnP (ListPatIn ps)
202 ExplicitTuple es Boxed -> mapP (\e -> checkPat e []) es `thenP` \ps ->
203 returnP (TuplePatIn ps Boxed)
204 -- Unboxed tuples are illegal in patterns
206 RecordCon c fs -> mapP checkPatField fs `thenP` \fs ->
207 returnP (RecPatIn c fs)
210 checkPat _ _ = patFail
212 checkPatField :: (RdrName, RdrNameHsExpr, Bool)
213 -> P (RdrName, RdrNamePat, Bool)
214 checkPatField (n,e,b) =
215 checkPat e [] `thenP` \p ->
218 patFail = parseError "Parse error in pattern"
221 ---------------------------------------------------------------------------
222 -- Check Equation Syntax
226 -> Maybe RdrNameHsType
231 checkValDef lhs opt_sig grhss loc
232 = case isFunLhs lhs [] of
234 checkPatterns es `thenP` \ps ->
235 returnP (RdrValBinding (FunMonoBind f inf [Match [] ps opt_sig grhss] loc))
238 checkPattern lhs `thenP` \lhs ->
239 returnP (RdrValBinding (PatMonoBind lhs grhss loc))
246 checkValSig (HsVar v) ty loc = returnP (RdrSig (Sig v ty loc))
247 checkValSig other ty loc = parseError "Type signature given for an expression"
250 -- A variable binding is parsed as an RdrNameFunMonoBind.
251 -- See comments with HsBinds.MonoBinds
253 isFunLhs (OpApp l (HsVar op) fix r) es | not (isRdrDataCon op)
254 = Just (op, True, (l:r:es))
255 isFunLhs (HsVar f) es | not (isRdrDataCon f)
257 isFunLhs (HsApp f e) es = isFunLhs f (e:es)
258 isFunLhs (HsPar e) es = isFunLhs e es
259 isFunLhs _ _ = Nothing
261 ---------------------------------------------------------------------------
262 -- Miscellaneous utilities
264 checkPrec :: Integer -> P ()
265 checkPrec i | 0 <= i && i <= 9 = returnP ()
266 | otherwise = parseError "precedence out of range"
270 -> RdrNameHsRecordBinds
273 mkRecConstrOrUpdate (HsVar c) fs | isRdrDataCon c
274 = returnP (RecordCon c fs)
275 mkRecConstrOrUpdate exp fs@(_:_)
276 = returnP (RecordUpd exp fs)
277 mkRecConstrOrUpdate _ _
278 = parseError "Empty record update"
280 -- Supplying the ext_name in a foreign decl is optional ; if it
281 -- isn't there, the Haskell name is assumed. Note that no transformation
282 -- of the Haskell name is then performed, so if you foreign export (++),
283 -- it's external name will be "++". Too bad; it's important because we don't
284 -- want z-encoding (e.g. names with z's in them shouldn't be doubled)
285 -- (This is why we use occNameUserString.)
286 mkExtName :: Maybe ExtName -> RdrName -> ExtName
287 mkExtName Nothing rdrNm = ExtName (_PK_ (occNameUserString (rdrNameOcc rdrNm)))
289 mkExtName (Just x) _ = x
291 -----------------------------------------------------------------------------
292 -- group function bindings into equation groups
294 -- we assume the bindings are coming in reverse order, so we take the srcloc
295 -- from the *last* binding in the group as the srcloc for the whole group.
297 groupBindings :: [RdrBinding] -> RdrBinding
298 groupBindings binds = group Nothing binds
299 where group :: Maybe RdrNameMonoBinds -> [RdrBinding] -> RdrBinding
300 group (Just bind) [] = RdrValBinding bind
301 group Nothing [] = RdrNullBind
303 -- don't group together FunMonoBinds if they have
304 -- no arguments. This is necessary now that variable bindings
305 -- with no arguments are now treated as FunMonoBinds rather
306 -- than pattern bindings (tests/rename/should_fail/rnfail002).
307 group (Just (FunMonoBind f inf1 mtchs ignore_srcloc))
308 (RdrValBinding (FunMonoBind f' _
309 [mtch@(Match _ (_:_) _ _)] loc)
311 | f == f' = group (Just (FunMonoBind f inf1 (mtch:mtchs) loc)) binds
313 group (Just so_far) binds
314 = RdrValBinding so_far `RdrAndBindings` group Nothing binds
315 group Nothing (bind:binds)
317 RdrValBinding b@(FunMonoBind _ _ _ _) -> group (Just b) binds
318 other -> bind `RdrAndBindings` group Nothing binds
320 plus_RDR = mkSrcUnqual varName SLIT("+")