2 % (c) The GRASP/AQUA Project, Glasgow University, 1999
4 \section[ParseUtil]{Parser Utilities}
8 parseError -- String -> Pa
9 , srcParseErr -- StringBuffer -> SrcLoc -> Message
11 , splitForConApp -- RdrNameHsType -> [RdrNameBangType]
12 -- -> P (RdrName, [RdrNameBangType])
14 , mkRecConstrOrUpdate -- HsExp -> [HsFieldUpdate] -> P HsExp
17 , mkExtName -- Maybe ExtName -> RdrName -> ExtName
19 , checkPrec -- String -> P String
20 , checkContext -- HsType -> P HsContext
21 , checkInstType -- HsType -> P HsType
22 , checkAssertion -- HsType -> P HsAsst
23 , checkDataHeader -- HsQualType -> P (HsContext,HsName,[HsName])
24 , checkSimple -- HsType -> [HsName] -> P ((HsName,[HsName]))
25 , checkPattern -- HsExp -> P HsPat
26 , checkPatterns -- [HsExp] -> P [HsPat]
27 -- , checkExpr -- HsExp -> P HsExp
28 , checkValDef -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
31 -- some built-in names (all :: RdrName)
32 , unitCon_RDR, unitTyCon_RDR, nilCon_RDR, listTyCon_RDR
33 , tupleCon_RDR, tupleTyCon_RDR, ubxTupleCon_RDR, ubxTupleTyCon_RDR
36 -- pseudo-keywords, in var and tyvar forms (all :: RdrName)
37 , as_var_RDR, hiding_var_RDR, qualified_var_RDR, forall_var_RDR
38 , export_var_RDR, label_var_RDR, dynamic_var_RDR, unsafe_var_RDR
39 , stdcall_var_RDR, ccall_var_RDR
41 , as_tyvar_RDR, hiding_tyvar_RDR, qualified_tyvar_RDR
42 , export_tyvar_RDR, label_tyvar_RDR, dynamic_tyvar_RDR
43 , unsafe_tyvar_RDR, stdcall_tyvar_RDR, ccall_tyvar_RDR
45 , minus_RDR, pling_RDR, dot_RDR
49 #include "HsVersions.h"
57 import PrelMods ( pRELUDE_Name, mkUbxTupNameStr, mkTupNameStr )
58 import OccName ( dataName, tcName, varName, tvName, setOccNameSpace, occNameFS )
59 import CmdLineOpts ( opt_NoImplicitPrelude )
60 import StringBuffer ( lexemeToString )
61 import FastString ( unpackFS )
63 import UniqFM ( UniqFM, listToUFM, lookupUFM )
66 -----------------------------------------------------------------------------
69 parseError :: String -> P a
71 getSrcLocP `thenP` \ loc ->
72 failMsgP (hcat [ppr loc, text ": ", text s])
74 srcParseErr :: StringBuffer -> SrcLoc -> Message
76 = hcat [ppr l, ptext SLIT(": parse error on input "),
77 char '`', text (lexemeToString s), char '\'']
79 cbot = panic "CCall:result_ty"
81 -----------------------------------------------------------------------------
84 -- When parsing data declarations, we sometimes inadvertently parse
85 -- a constructor application as a type (eg. in data T a b = C a b `D` E a b)
86 -- This function splits up the type application, adds any pending
87 -- arguments, and converts the type constructor back into a data constructor.
89 splitForConApp :: RdrNameHsType -> [RdrNameBangType]
90 -> P (RdrName, [RdrNameBangType])
92 splitForConApp t ts = split t ts
94 split (MonoTyApp t u) ts = split t (Unbanged u : ts)
96 split (MonoTyVar t) ts = returnP (con, ts)
97 where t_occ = rdrNameOcc t
98 con = setRdrNameOcc t (setOccNameSpace t_occ dataName)
100 split _ _ = parseError "Illegal data/newtype declaration"
102 ----------------------------------------------------------------------------
103 -- Various Syntactic Checks
105 callConvFM :: UniqFM CallConv
106 callConvFM = listToUFM $
107 map (\ (x,y) -> (_PK_ x,y))
108 [ ("stdcall", stdCallConv),
110 -- ("pascal", pascalCallConv),
111 -- ("fastcall", fastCallConv)
114 checkCallConv :: FAST_STRING -> P CallConv
116 case lookupUFM callConvFM s of
117 Nothing -> parseError ("unknown calling convention: `"
118 ++ unpackFS s ++ "'")
119 Just conv -> returnP conv
121 checkInstType :: RdrNameHsType -> P RdrNameHsType
124 HsForAllTy tvs ctxt ty ->
125 checkAssertion ty [] `thenP` \(c,ts)->
126 returnP (HsForAllTy tvs ctxt (MonoDictTy c ts))
128 ty -> checkAssertion ty [] `thenP` \(c,ts)->
129 returnP (HsForAllTy Nothing [] (MonoDictTy c ts))
131 checkContext :: RdrNameHsType -> P RdrNameContext
132 checkContext (MonoTupleTy ts True)
133 = mapP (\t -> checkAssertion t []) ts `thenP` \cs ->
135 checkContext (MonoTyVar t) -- empty contexts are allowed
136 | t == unitTyCon_RDR = returnP []
138 = checkAssertion t [] `thenP` \c ->
141 checkAssertion :: RdrNameHsType -> [RdrNameHsType]
142 -> P (ClassAssertion RdrName)
143 checkAssertion (MonoTyVar t) args@(_:_) | not (isRdrTyVar t)
145 checkAssertion (MonoTyApp l r) args = checkAssertion l (r:args)
146 checkAssertion _ _ = parseError "Illegal class assertion"
148 checkDataHeader :: RdrNameHsType
149 -> P (RdrNameContext, RdrName, [RdrNameHsTyVar])
150 checkDataHeader (HsForAllTy Nothing cs t) =
151 checkSimple t [] `thenP` \(c,ts) ->
152 returnP (cs,c,map UserTyVar ts)
154 checkSimple t [] `thenP` \(c,ts) ->
155 returnP ([],c,map UserTyVar ts)
157 checkSimple :: RdrNameHsType -> [RdrName] -> P ((RdrName,[RdrName]))
158 checkSimple (MonoTyApp l (MonoTyVar a)) xs | isRdrTyVar a
159 = checkSimple l (a:xs)
160 checkSimple (MonoTyVar t) xs | not (isRdrTyVar t) = returnP (t,xs)
161 checkSimple t _ = trace (showSDoc (ppr t)) $ parseError "Illegal data/newtype declaration"
163 ---------------------------------------------------------------------------
164 -- Checking Patterns.
166 -- We parse patterns as expressions and check for valid patterns below,
167 -- nverting the expression into a pattern at the same time.
169 checkPattern :: RdrNameHsExpr -> P RdrNamePat
170 checkPattern e = checkPat e []
172 checkPatterns :: [RdrNameHsExpr] -> P [RdrNamePat]
173 checkPatterns es = mapP checkPattern es
175 checkPat :: RdrNameHsExpr -> [RdrNamePat] -> P RdrNamePat
176 checkPat (HsVar c) args | isRdrDataCon c = returnP (ConPatIn c args)
177 checkPat (HsApp f x) args =
178 checkPat x [] `thenP` \x ->
180 checkPat e [] = case e of
181 EWildPat -> returnP WildPatIn
182 HsVar x -> returnP (VarPatIn x)
183 HsLit l -> returnP (LitPatIn l)
184 ELazyPat e -> checkPat e [] `thenP` (returnP . LazyPatIn)
185 EAsPat n e -> checkPat e [] `thenP` (returnP . AsPatIn n)
186 ExprWithTySig e t -> checkPat e [] `thenP` \e ->
187 -- pattern signatures are parsed as sigtypes,
188 -- but they aren't explicit forall points. Hence
189 -- we have to remove the implicit forall here.
191 HsForAllTy Nothing [] ty -> ty
194 returnP (SigPatIn e t')
196 OpApp (HsVar n) (HsVar plus) _ (HsLit k@(HsInt _)) | plus == plus_RDR
197 -> returnP (NPlusKPatIn n k)
199 OpApp l op fix r -> checkPat l [] `thenP` \l ->
200 checkPat r [] `thenP` \r ->
202 HsVar c -> returnP (ConOpPatIn l c fix r)
205 NegApp l r -> checkPat l [] `thenP` (returnP . NegPatIn)
206 HsPar e -> checkPat e [] `thenP` (returnP . ParPatIn)
207 ExplicitList es -> mapP (\e -> checkPat e []) es `thenP` \ps ->
208 returnP (ListPatIn ps)
209 ExplicitTuple es b -> mapP (\e -> checkPat e []) es `thenP` \ps ->
210 returnP (TuplePatIn ps b)
211 RecordCon c fs -> mapP checkPatField fs `thenP` \fs ->
212 returnP (RecPatIn c fs)
215 checkPat _ _ = patFail
217 checkPatField :: (RdrName, RdrNameHsExpr, Bool)
218 -> P (RdrName, RdrNamePat, Bool)
219 checkPatField (n,e,b) =
220 checkPat e [] `thenP` \p ->
223 patFail = parseError "Parse error in pattern"
225 ---------------------------------------------------------------------------
226 -- Check Expression Syntax
229 We can get away without checkExpr if the renamer generates errors for
230 pattern syntax used in expressions (wildcards, as patterns and lazy
233 checkExpr :: RdrNameHsExpr -> P RdrNameHsExpr
234 checkExpr e = case e of
237 HsLam match -> checkMatch match `thenP` (returnP.HsLam)
238 HsApp e1 e2 -> check2Exprs e1 e2 HsApp
239 OpApp e1 e2 fix e3 -> checkExpr e1 `thenP` \e1 ->
240 checkExpr e2 `thenP` \e2 ->
241 checkExpr e3 `thenP` \e3 ->
242 returnP (OpApp e1 e2 fix e3)
243 NegApp e neg -> checkExpr e `thenP` \e ->
244 returnP (NegApp e neg)
245 HsPar e -> check1Expr e HsPar
246 SectionL e1 e2 -> check2Exprs e1 e2 SectionL
247 SectionR e1 e2 -> check2Exprs e1 e2 SectionR
248 HsCase e alts -> mapP checkMatch alts `thenP` \alts ->
249 checkExpr e `thenP` \e ->
250 returnP (HsCase e alts)
251 HsIf e1 e2 e3 -> check3Exprs e1 e2 e3 HsIf
253 HsLet bs e -> check1Expr e (HsLet bs)
254 HsDo stmts -> mapP checkStmt stmts `thenP` (returnP . HsDo)
255 HsTuple es -> checkManyExprs es HsTuple
256 HsList es -> checkManyExprs es HsList
257 HsRecConstr c fields -> mapP checkField fields `thenP` \fields ->
258 returnP (HsRecConstr c fields)
259 HsRecUpdate e fields -> mapP checkField fields `thenP` \fields ->
260 checkExpr e `thenP` \e ->
261 returnP (HsRecUpdate e fields)
262 HsEnumFrom e -> check1Expr e HsEnumFrom
263 HsEnumFromTo e1 e2 -> check2Exprs e1 e2 HsEnumFromTo
264 HsEnumFromThen e1 e2 -> check2Exprs e1 e2 HsEnumFromThen
265 HsEnumFromThenTo e1 e2 e3 -> check3Exprs e1 e2 e3 HsEnumFromThenTo
266 HsListComp e stmts -> mapP checkStmt stmts `thenP` \stmts ->
267 checkExpr e `thenP` \e ->
268 returnP (HsListComp e stmts)
269 RdrNameHsExprTypeSig loc e ty -> checkExpr e `thenP` \e ->
270 returnP (RdrNameHsExprTypeSig loc e ty)
271 _ -> parseError "parse error in expression"
273 -- type signature for polymorphic recursion!!
274 check1Expr :: RdrNameHsExpr -> (RdrNameHsExpr -> a) -> P a
275 check1Expr e f = checkExpr e `thenP` (returnP . f)
277 check2Exprs :: RdrNameHsExpr -> RdrNameHsExpr -> (RdrNameHsExpr -> RdrNameHsExpr -> a) -> P a
278 check2Exprs e1 e2 f =
279 checkExpr e1 `thenP` \e1 ->
280 checkExpr e2 `thenP` \e2 ->
283 check3Exprs :: RdrNameHsExpr -> RdrNameHsExpr -> RdrNameHsExpr -> (RdrNameHsExpr -> RdrNameHsExpr -> RdrNameHsExpr -> a) -> P a
284 check3Exprs e1 e2 e3 f =
285 checkExpr e1 `thenP` \e1 ->
286 checkExpr e2 `thenP` \e2 ->
287 checkExpr e3 `thenP` \e3 ->
290 checkManyExprs es f =
291 mapP checkExpr es `thenP` \es ->
294 checkAlt (HsAlt loc p galts bs)
295 = checkGAlts galts `thenP` \galts -> returnP (HsAlt loc p galts bs)
297 checkGAlts (HsUnGuardedAlt e) = check1Expr e HsUnGuardedAlt
298 checkGAlts (HsGuardedAlts galts)
299 = mapP checkGAlt galts `thenP` (returnP . HsGuardedAlts)
301 checkGAlt (HsGuardedAlt loc e1 e2) = check2Exprs e1 e2 (HsGuardedAlt loc)
303 checkStmt (HsGenerator p e) = check1Expr e (HsGenerator p)
304 checkStmt (HsQualifier e) = check1Expr e HsQualifier
305 checkStmt s@(HsLetStmt bs) = returnP s
307 checkField (HsFieldUpdate n e) = check1Expr e (HsFieldUpdate n)
308 checkField e = returnP e
310 ---------------------------------------------------------------------------
311 -- Check Equation Syntax
315 -> Maybe RdrNameHsType
318 -> P RdrNameMonoBinds
320 checkValDef lhs opt_sig grhss loc
321 = case isFunLhs lhs [] of
323 checkPatterns es `thenP` \ps ->
324 returnP (FunMonoBind f inf [Match [] ps opt_sig grhss] loc)
327 checkPattern lhs `thenP` \lhs ->
328 returnP (PatMonoBind lhs grhss loc)
330 -- A variable binding is parsed as an RdrNamePatBind.
332 isFunLhs (OpApp l (HsVar op) fix r) [] | not (isRdrDataCon op)
333 = Just (op, True, [l,r])
334 isFunLhs (HsVar f) es@(_:_) | not (isRdrDataCon f)
336 isFunLhs (HsApp f e) es = isFunLhs f (e:es)
337 isFunLhs (HsPar e) es = isFunLhs e es
338 isFunLhs _ _ = Nothing
340 ---------------------------------------------------------------------------
341 -- Miscellaneous utilities
343 checkPrec :: Integer -> P ()
344 checkPrec i | 0 <= i && i <= 9 = returnP ()
345 | otherwise = parseError "precedence out of range"
349 -> RdrNameHsRecordBinds
352 mkRecConstrOrUpdate (HsVar c) fs | isRdrDataCon c
353 = returnP (RecordCon c fs)
354 mkRecConstrOrUpdate exp fs@(_:_)
355 = returnP (RecordUpd exp fs)
356 mkRecConstrOrUpdate _ _
357 = parseError "Empty record update"
359 -- supplying the ext_name in a foreign decl is optional ; if it
360 -- isn't there, the Haskell name is assumed. Note that no transformation
361 -- of the Haskell name is then performed, so if you foreign export (++),
362 -- it's external name will be "++". Too bad.
363 mkExtName :: Maybe ExtName -> RdrName -> ExtName
364 mkExtName Nothing rdrNm = ExtName (occNameFS (rdrNameOcc rdrNm)) Nothing
365 mkExtName (Just x) _ = x
367 -----------------------------------------------------------------------------
368 -- group function bindings into equation groups
370 -- we assume the bindings are coming in reverse order, so we take the srcloc
371 -- from the *last* binding in the group as the srcloc for the whole group.
373 groupBindings :: [RdrBinding] -> RdrBinding
374 groupBindings binds = group Nothing binds
375 where group :: Maybe RdrNameMonoBinds -> [RdrBinding] -> RdrBinding
376 group (Just bind) [] = RdrValBinding bind
377 group Nothing [] = RdrNullBind
378 group (Just (FunMonoBind f inf1 mtchs ignore_srcloc))
379 (RdrValBinding (FunMonoBind f' _ [mtch] loc) : binds)
380 | f == f' = group (Just (FunMonoBind f inf1 (mtch:mtchs) loc)) binds
382 group (Just so_far) binds
383 = RdrValBinding so_far `RdrAndBindings` group Nothing binds
384 group Nothing (bind:binds)
386 RdrValBinding b@(FunMonoBind _ _ _ _) -> group (Just b) binds
387 other -> bind `RdrAndBindings` group Nothing binds
389 -----------------------------------------------------------------------------
392 unitCon_RDR, unitTyCon_RDR, nilCon_RDR, listTyCon_RDR :: RdrName
393 tupleCon_RDR, tupleTyCon_RDR :: Int -> RdrName
394 ubxTupleCon_RDR, ubxTupleTyCon_RDR :: Int -> RdrName
397 | opt_NoImplicitPrelude = mkSrcUnqual dataName unitName
398 | otherwise = mkPreludeQual dataName pRELUDE_Name unitName
401 | opt_NoImplicitPrelude = mkSrcUnqual tcName unitName
402 | otherwise = mkPreludeQual tcName pRELUDE_Name unitName
405 | opt_NoImplicitPrelude = mkSrcUnqual dataName listName
406 | otherwise = mkPreludeQual dataName pRELUDE_Name listName
409 | opt_NoImplicitPrelude = mkSrcUnqual tcName listName
410 | otherwise = mkPreludeQual tcName pRELUDE_Name listName
413 | opt_NoImplicitPrelude = mkSrcUnqual tcName funName
414 | otherwise = mkPreludeQual tcName pRELUDE_Name funName
417 | opt_NoImplicitPrelude = mkSrcUnqual dataName (snd (mkTupNameStr arity))
418 | otherwise = mkPreludeQual dataName pRELUDE_Name
419 (snd (mkTupNameStr arity))
422 | opt_NoImplicitPrelude = mkSrcUnqual tcName (snd (mkTupNameStr arity))
423 | otherwise = mkPreludeQual tcName pRELUDE_Name
424 (snd (mkTupNameStr arity))
427 ubxTupleCon_RDR arity
428 | opt_NoImplicitPrelude = mkSrcUnqual dataName (snd (mkUbxTupNameStr arity))
429 | otherwise = mkPreludeQual dataName pRELUDE_Name
430 (snd (mkUbxTupNameStr arity))
432 ubxTupleTyCon_RDR arity
433 | opt_NoImplicitPrelude = mkSrcUnqual tcName (snd (mkUbxTupNameStr arity))
434 | otherwise = mkPreludeQual tcName pRELUDE_Name
435 (snd (mkUbxTupNameStr arity))
437 unitName = SLIT("()")
438 funName = SLIT("(->)")
439 listName = SLIT("[]")
442 hidingName = SLIT("hiding")
443 qualifiedName = SLIT("qualified")
444 forallName = SLIT("forall")
445 exportName = SLIT("export")
446 labelName = SLIT("label")
447 dynamicName = SLIT("dynamic")
448 unsafeName = SLIT("unsafe")
449 stdcallName = SLIT("stdcall")
450 ccallName = SLIT("ccall")
452 as_var_RDR = mkSrcUnqual varName asName
453 hiding_var_RDR = mkSrcUnqual varName hidingName
454 qualified_var_RDR = mkSrcUnqual varName qualifiedName
455 forall_var_RDR = mkSrcUnqual varName forallName
456 export_var_RDR = mkSrcUnqual varName exportName
457 label_var_RDR = mkSrcUnqual varName labelName
458 dynamic_var_RDR = mkSrcUnqual varName dynamicName
459 unsafe_var_RDR = mkSrcUnqual varName unsafeName
460 stdcall_var_RDR = mkSrcUnqual varName stdcallName
461 ccall_var_RDR = mkSrcUnqual varName ccallName
463 as_tyvar_RDR = mkSrcUnqual tvName asName
464 hiding_tyvar_RDR = mkSrcUnqual tvName hidingName
465 qualified_tyvar_RDR = mkSrcUnqual tvName qualifiedName
466 export_tyvar_RDR = mkSrcUnqual tvName exportName
467 label_tyvar_RDR = mkSrcUnqual tvName labelName
468 dynamic_tyvar_RDR = mkSrcUnqual tvName dynamicName
469 unsafe_tyvar_RDR = mkSrcUnqual tvName unsafeName
470 stdcall_tyvar_RDR = mkSrcUnqual tvName stdcallName
471 ccall_tyvar_RDR = mkSrcUnqual tvName ccallName
473 minus_RDR = mkSrcUnqual varName SLIT("-")
474 pling_RDR = mkSrcUnqual varName SLIT("!")
475 dot_RDR = mkSrcUnqual varName SLIT(".")
477 plus_RDR = mkSrcUnqual varName SLIT("+")