2 % (c) The GRASP/AQUA Project, Glasgow University, 1999
4 \section[ParseUtil]{Parser Utilities}
8 parseError -- String -> Pa
9 , mkVanillaCon, mkRecCon,
11 , mkRecConstrOrUpdate -- HsExp -> [HsFieldUpdate] -> P HsExp
14 , mkExtName -- RdrName -> ExtName
16 , checkPrec -- String -> P String
17 , checkContext -- HsType -> P HsContext
18 , checkInstType -- HsType -> P HsType
19 , checkDataHeader -- HsQualType -> P (HsContext,HsName,[HsName])
20 , checkPattern -- HsExp -> P HsPat
21 , checkPatterns -- SrcLoc -> [HsExp] -> P [HsPat]
22 , checkDo -- [Stmt] -> P [Stmt]
23 , checkValDef -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
24 , checkValSig -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
27 #include "HsVersions.h"
30 import HsSyn -- Lots of it
32 import RdrHsSyn ( RdrBinding(..),
33 RdrNameHsType, RdrNameBangType, RdrNameContext,
34 RdrNameHsTyVar, RdrNamePat, RdrNameHsExpr, RdrNameGRHSs,
35 RdrNameHsRecordBinds, RdrNameMonoBinds, RdrNameConDetails,
39 import PrelNames ( unitTyCon_RDR )
40 import OccName ( dataName, varName, tcClsName,
41 occNameSpace, setOccNameSpace, occNameUserString )
42 import CStrings ( CLabelString )
43 import FastString ( unpackFS )
46 -----------------------------------------------------------------------------
49 parseError :: String -> P a
51 getSrcLocP `thenP` \ loc ->
52 failMsgP (hcat [ppr loc, text ": ", text s])
55 -----------------------------------------------------------------------------
58 -- When parsing data declarations, we sometimes inadvertently parse
59 -- a constructor application as a type (eg. in data T a b = C a b `D` E a b)
60 -- This function splits up the type application, adds any pending
61 -- arguments, and converts the type constructor back into a data constructor.
63 mkVanillaCon :: RdrNameHsType -> [RdrNameBangType] -> P (RdrName, RdrNameConDetails)
68 split (HsAppTy t u) ts = split t (unbangedType u : ts)
69 split (HsTyVar tc) ts = tyConToDataCon tc `thenP` \ data_con ->
70 returnP (data_con, VanillaCon ts)
71 split _ _ = parseError "Illegal data/newtype declaration"
73 mkRecCon :: RdrName -> [([RdrName],RdrNameBangType)] -> P (RdrName, RdrNameConDetails)
75 = tyConToDataCon con `thenP` \ data_con ->
76 returnP (data_con, RecCon fields)
78 tyConToDataCon :: RdrName -> P RdrName
80 | occNameSpace tc_occ == tcClsName
81 = returnP (setRdrNameOcc tc (setOccNameSpace tc_occ dataName))
83 = parseError (showSDoc (text "Not a constructor:" <+> quotes (ppr tc)))
85 tc_occ = rdrNameOcc tc
88 ----------------------------------------------------------------------------
89 -- Various Syntactic Checks
91 checkInstType :: RdrNameHsType -> P RdrNameHsType
94 HsForAllTy tvs ctxt ty ->
95 checkDictTy ty [] `thenP` \ dict_ty ->
96 returnP (HsForAllTy tvs ctxt dict_ty)
98 ty -> checkDictTy ty [] `thenP` \ dict_ty->
99 returnP (HsForAllTy Nothing [] dict_ty)
101 checkContext :: RdrNameHsType -> P RdrNameContext
102 checkContext (HsTupleTy _ ts)
103 = mapP (\t -> checkPred t []) ts `thenP` \ps ->
105 checkContext (HsTyVar t) -- empty contexts are allowed
106 | t == unitTyCon_RDR = returnP []
108 = checkPred t [] `thenP` \p ->
111 checkPred :: RdrNameHsType -> [RdrNameHsType]
112 -> P (HsPred RdrName)
113 checkPred (HsTyVar t) args@(_:_) | not (isRdrTyVar t)
114 = returnP (HsClassP t args)
115 checkPred (HsAppTy l r) args = checkPred l (r:args)
116 checkPred (HsPredTy (HsIParam n ty)) [] = returnP (HsIParam n ty)
117 checkPred _ _ = parseError "Illegal class assertion"
119 checkDictTy :: RdrNameHsType -> [RdrNameHsType] -> P RdrNameHsType
120 checkDictTy (HsTyVar t) args@(_:_) | not (isRdrTyVar t)
121 = returnP (mkHsDictTy t args)
122 checkDictTy (HsAppTy l r) args = checkDictTy l (r:args)
123 checkDictTy _ _ = parseError "Malformed context in instance header"
125 -- Put more comments!
126 -- Checks that the lhs of a datatype declaration
127 -- is of the form Context => T a b ... z
128 checkDataHeader :: String -- data/newtype/class
130 -> P (RdrNameContext, RdrName, [RdrNameHsTyVar])
132 checkDataHeader s (HsForAllTy Nothing cs t) =
133 checkSimple s t [] `thenP` \(c,ts) ->
134 returnP (cs,c,map UserTyVar ts)
135 checkDataHeader s t =
136 checkSimple s t [] `thenP` \(c,ts) ->
137 returnP ([],c,map UserTyVar ts)
139 -- Checks the type part of the lhs of
140 -- a data/newtype/class declaration
141 checkSimple :: String -> RdrNameHsType -> [RdrName] -> P ((RdrName,[RdrName]))
142 checkSimple s (HsAppTy l (HsTyVar a)) xs | isRdrTyVar a
143 = checkSimple s l (a:xs)
144 checkSimple s (HsTyVar tycon) xs | not (isRdrTyVar tycon) = returnP (tycon,xs)
146 checkSimple s (HsOpTy (HsTyVar t1) tycon (HsTyVar t2)) []
147 | not (isRdrTyVar tycon) && isRdrTyVar t1 && isRdrTyVar t2
148 = returnP (tycon,[t1,t2])
150 checkSimple s t _ = parseError ("Malformed " ++ s ++ " declaration")
152 ---------------------------------------------------------------------------
153 -- Checking statements in a do-expression
154 -- We parse do { e1 ; e2 ; }
155 -- as [ExprStmt e1, ExprStmt e2]
156 -- checkDo (a) checks that the last thing is an ExprStmt
157 -- (b) transforms it to a ResultStmt
159 checkDo [] = parseError "Empty 'do' construct"
160 checkDo [ExprStmt e _ l] = returnP [ResultStmt e l]
161 checkDo [s] = parseError "The last statement in a 'do' construct must be an expression"
162 checkDo (s:ss) = checkDo ss `thenP` \ ss' ->
165 ---------------------------------------------------------------------------
166 -- Checking Patterns.
168 -- We parse patterns as expressions and check for valid patterns below,
169 -- converting the expression into a pattern at the same time.
171 checkPattern :: SrcLoc -> RdrNameHsExpr -> P RdrNamePat
172 checkPattern loc e = setSrcLocP loc (checkPat e [])
174 checkPatterns :: SrcLoc -> [RdrNameHsExpr] -> P [RdrNamePat]
175 checkPatterns loc es = mapP (checkPattern loc) es
177 checkPat :: RdrNameHsExpr -> [RdrNamePat] -> P RdrNamePat
178 checkPat (HsVar c) args | isRdrDataCon c = returnP (ConPatIn c args)
179 checkPat (HsApp f x) args =
180 checkPat x [] `thenP` \x ->
182 checkPat e [] = case e of
183 EWildPat -> returnP WildPatIn
184 HsVar x -> returnP (VarPatIn x)
185 HsLit l -> returnP (LitPatIn l)
186 HsOverLit l -> returnP (NPatIn l)
187 ELazyPat e -> checkPat e [] `thenP` (returnP . LazyPatIn)
188 EAsPat n e -> checkPat e [] `thenP` (returnP . AsPatIn n)
189 ExprWithTySig e t -> checkPat e [] `thenP` \e ->
190 -- Pattern signatures are parsed as sigtypes,
191 -- but they aren't explicit forall points. Hence
192 -- we have to remove the implicit forall here.
194 HsForAllTy Nothing [] ty -> ty
197 returnP (SigPatIn e t')
199 OpApp (HsVar n) (HsVar plus) _ (HsOverLit lit@(HsIntegral _ _))
201 -> returnP (mkNPlusKPat n lit)
203 plus_RDR = mkUnqual varName SLIT("+") -- Hack
205 OpApp l op fix r -> checkPat l [] `thenP` \l ->
206 checkPat r [] `thenP` \r ->
208 HsVar c -> returnP (ConOpPatIn l c fix r)
211 HsPar e -> checkPat e [] `thenP` (returnP . ParPatIn)
212 ExplicitList _ es -> mapP (\e -> checkPat e []) es `thenP` \ps ->
213 returnP (ListPatIn ps)
215 ExplicitTuple es b -> mapP (\e -> checkPat e []) es `thenP` \ps ->
216 returnP (TuplePatIn ps b)
218 RecordCon c fs -> mapP checkPatField fs `thenP` \fs ->
219 returnP (RecPatIn c fs)
221 HsType ty -> returnP (TypePatIn ty)
224 checkPat _ _ = patFail
226 checkPatField :: (RdrName, RdrNameHsExpr, Bool)
227 -> P (RdrName, RdrNamePat, Bool)
228 checkPatField (n,e,b) =
229 checkPat e [] `thenP` \p ->
232 patFail = parseError "Parse error in pattern"
235 ---------------------------------------------------------------------------
236 -- Check Equation Syntax
240 -> Maybe RdrNameHsType
245 checkValDef lhs opt_sig grhss loc
246 = case isFunLhs lhs [] of
248 checkPatterns loc es `thenP` \ps ->
249 returnP (RdrValBinding (FunMonoBind f inf [Match ps opt_sig grhss] loc))
252 checkPattern loc lhs `thenP` \lhs ->
253 returnP (RdrValBinding (PatMonoBind lhs grhss loc))
260 checkValSig (HsVar v) ty loc = returnP (RdrSig (Sig v ty loc))
261 checkValSig other ty loc = parseError "Type signature given for an expression"
264 -- A variable binding is parsed as an RdrNameFunMonoBind.
265 -- See comments with HsBinds.MonoBinds
267 isFunLhs :: RdrNameHsExpr -> [RdrNameHsExpr] -> Maybe (RdrName, Bool, [RdrNameHsExpr])
268 isFunLhs (OpApp l (HsVar op) fix r) es | not (isRdrDataCon op)
269 = Just (op, True, (l:r:es))
271 = case isFunLhs l es of
272 Just (op', True, j : k : es') ->
273 Just (op', True, j : OpApp k (HsVar op) fix r : es')
275 isFunLhs (HsVar f) es | not (isRdrDataCon f)
277 isFunLhs (HsApp f e) es = isFunLhs f (e:es)
278 isFunLhs (HsPar e) es = isFunLhs e es
279 isFunLhs _ _ = Nothing
281 ---------------------------------------------------------------------------
282 -- Miscellaneous utilities
284 checkPrec :: Integer -> P ()
285 checkPrec i | 0 <= i && i <= 9 = returnP ()
286 | otherwise = parseError "Precedence out of range"
290 -> RdrNameHsRecordBinds
293 mkRecConstrOrUpdate (HsVar c) fs | isRdrDataCon c
294 = returnP (RecordCon c fs)
295 mkRecConstrOrUpdate exp fs@(_:_)
296 = returnP (RecordUpd exp fs)
297 mkRecConstrOrUpdate _ _
298 = parseError "Empty record update"
300 -- Supplying the ext_name in a foreign decl is optional ; if it
301 -- isn't there, the Haskell name is assumed. Note that no transformation
302 -- of the Haskell name is then performed, so if you foreign export (++),
303 -- it's external name will be "++". Too bad; it's important because we don't
304 -- want z-encoding (e.g. names with z's in them shouldn't be doubled)
305 -- (This is why we use occNameUserString.)
307 mkExtName :: RdrName -> CLabelString
308 mkExtName rdrNm = _PK_ (occNameUserString (rdrNameOcc rdrNm))
310 -----------------------------------------------------------------------------
311 -- group function bindings into equation groups
313 -- we assume the bindings are coming in reverse order, so we take the srcloc
314 -- from the *last* binding in the group as the srcloc for the whole group.
316 groupBindings :: [RdrBinding] -> RdrBinding
317 groupBindings binds = group Nothing binds
318 where group :: Maybe RdrNameMonoBinds -> [RdrBinding] -> RdrBinding
319 group (Just bind) [] = RdrValBinding bind
320 group Nothing [] = RdrNullBind
322 -- don't group together FunMonoBinds if they have
323 -- no arguments. This is necessary now that variable bindings
324 -- with no arguments are now treated as FunMonoBinds rather
325 -- than pattern bindings (tests/rename/should_fail/rnfail002).
326 group (Just (FunMonoBind f inf1 mtchs ignore_srcloc))
327 (RdrValBinding (FunMonoBind f' _
328 [mtch@(Match (_:_) _ _)] loc)
330 | f == f' = group (Just (FunMonoBind f inf1 (mtch:mtchs) loc)) binds
332 group (Just so_far) binds
333 = RdrValBinding so_far `RdrAndBindings` group Nothing binds
334 group Nothing (bind:binds)
336 RdrValBinding b@(FunMonoBind _ _ _ _) -> group (Just b) binds
337 other -> bind `RdrAndBindings` group Nothing binds