@@ -18,6 +18,10 @@ import Prelude ()
18import Prelude.Compat
18import Prelude.Compat
20import Data.Maybe (mapMaybe)
20import Data.Maybe (mapMaybe)
21+import Data.List (mapAccumL)
22+import Data.Foldable (fold)
23+import qualified Data.Set as S
21import Control.Monad
25import Control.Monad
22import Control.Arrow ((***), (+++), second)
26import Control.Arrow ((***), (+++), second)
@@ -25,6 +29,7 @@ import Language.PureScript.AST.Binders
25import Language.PureScript.AST.Declarations
29import Language.PureScript.AST.Declarations
26import Language.PureScript.Types
30import Language.PureScript.Types
27import Language.PureScript.Traversals
31import Language.PureScript.Traversals
32+import Language.PureScript.Names
29everywhereOnValues :: (Declaration -> Declaration) ->
34everywhereOnValues :: (Declaration -> Declaration) ->
30 (Expr -> Expr) ->
35 (Expr -> Expr) ->
@@ -389,6 +394,112 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
389 j' s (DoNotationLet ds) = DoNotationLet <$> traverse (f'' s) ds
394 j' s (DoNotationLet ds) = DoNotationLet <$> traverse (f'' s) ds
390 j' s (PositionedDoNotationElement pos com e1) = PositionedDoNotationElement pos com <$> j'' s e1
395 j' s (PositionedDoNotationElement pos com e1) = PositionedDoNotationElement pos com <$> j'' s e1
397+everythingWithScope ::
399+ (S.Set Ident -> Declaration -> r) ->
400+ (S.Set Ident -> Expr -> r) ->
401+ (S.Set Ident -> Binder -> r) ->
402+ (S.Set Ident -> CaseAlternative -> r) ->
403+ (S.Set Ident -> DoNotationElement -> r) ->
404+ ( S.Set Ident -> Declaration -> r
405+ , S.Set Ident -> Expr -> r
406+ , S.Set Ident -> Binder -> r
407+ , S.Set Ident -> CaseAlternative -> r
408+ , S.Set Ident -> DoNotationElement -> r)
409+everythingWithScope f g h i j = (f'', g'', h'', i'', \s -> snd . j'' s)
411+ -- Avoid importing Data.Monoid and getting shadowed names above
414+ f'' s a = f s a <> f' s a
416+ f' s (DataBindingGroupDeclaration ds) =
417+ let s' = S.union s (S.fromList (mapMaybe getDeclIdent ds))
418+ in foldMap (f'' s') ds
419+ f' s (ValueDeclaration name _ bs (Right val)) =
420+ let s' = S.insert name s
421+ in foldMap (h'' s') bs <> g'' s' val
422+ f' s (ValueDeclaration name _ bs (Left gs)) =
423+ let s' = S.insert name s
424+ s'' = S.union s' (S.fromList (concatMap binderNames bs))
425+ in foldMap (h'' s') bs <> foldMap (\(grd, val) -> g'' s'' grd <> g'' s'' val) gs
426+ f' s (BindingGroupDeclaration ds) =
427+ let s' = S.union s (S.fromList (map (\(name, _, _) -> name) ds))
428+ in foldMap (\(_, _, val) -> g'' s' val) ds
429+ f' s (TypeClassDeclaration _ _ _ ds) = foldMap (f'' s) ds
430+ f' s (TypeInstanceDeclaration _ _ _ _ (ExplicitInstance ds)) = foldMap (f'' s) ds
431+ f' s (PositionedDeclaration _ _ d) = f'' s d
434+ g'' s a = g s a <> g' s a
436+ g' s (UnaryMinus v1) = g'' s v1
437+ g' s (BinaryNoParens op v1 v2) = g' s op <> g' s v1 <> g' s v2
438+ g' s (Parens v1) = g'' s v1
439+ g' s (OperatorSection op (Left v)) = g'' s op <> g'' s v
440+ g' s (OperatorSection op (Right v)) = g'' s op <> g'' s v
441+ g' s (ArrayLiteral vs) = foldMap (g'' s) vs
442+ g' s (ObjectLiteral vs) = foldMap (g'' s . snd) vs
443+ g' s (ObjectConstructor vs) = foldMap (g'' s) (mapMaybe snd vs)
444+ g' s (TypeClassDictionaryConstructorApp _ v1) = g'' s v1
445+ g' s (Accessor _ v1) = g'' s v1
446+ g' s (ObjectUpdate obj vs) = g'' s obj <> foldMap (g'' s . snd) vs
447+ g' s (ObjectUpdater obj vs) = foldMap (g'' s) obj <> foldMap (g'' s) (mapMaybe snd vs)
448+ g' s (Abs (Left name) v1) =
449+ let s' = S.insert name s
451+ g' s (Abs (Right b) v1) =
452+ let s' = S.union (S.fromList (binderNames b)) s
454+ g' s (App v1 v2) = g'' s v1 <> g'' s v2
455+ g' s (IfThenElse v1 v2 v3) = g'' s v1 <> g'' s v2 <> g'' s v3
456+ g' s (Case vs alts) = foldMap (g'' s) vs <> foldMap (i'' s) alts
457+ g' s (TypedValue _ v1 _) = g'' s v1
459+ let s' = S.union s (S.fromList (mapMaybe getDeclIdent ds))
460+ in foldMap (f'' s') ds <> g'' s' v1
461+ g' s (Do es) = fold . snd . mapAccumL j'' s $ es
462+ g' s (PositionedValue _ _ v1) = g'' s v1
465+ h'' s a = h s a <> h' s a
467+ h' s (ConstructorBinder _ bs) = foldMap (h'' s) bs
468+ h' s (ObjectBinder bs) = foldMap (h'' s . snd) bs
469+ h' s (ArrayBinder bs) = foldMap (h'' s) bs
470+ h' s (NamedBinder name b1) =
471+ let s' = S.insert name s
473+ h' s (PositionedBinder _ _ b1) = h'' s b1
474+ h' s (TypedBinder _ b1) = h'' s b1
477+ i'' s a = i s a <> i' s a
479+ i' s (CaseAlternative bs (Right val)) =
480+ let s' = S.union s (S.fromList (concatMap binderNames bs))
481+ in foldMap (h'' s) bs <> g'' s' val
482+ i' s (CaseAlternative bs (Left gs)) =
483+ let s' = S.union s (S.fromList (concatMap binderNames bs))
484+ in foldMap (h'' s) bs <> foldMap (\(grd, val) -> g'' s' grd <> g'' s' val) gs
486+ j'' s a = let (s', r) = j' s a in (s', j s a <> r)
488+ j' s (DoNotationValue v) = (s, g'' s v)
489+ j' s (DoNotationBind b v) =
490+ let s' = S.union (S.fromList (binderNames b)) s
491+ in (s', h'' s b <> g'' s' v)
492+ j' s (DoNotationLet ds) =
493+ let s' = S.union s (S.fromList (mapMaybe getDeclIdent ds))
494+ in (s', foldMap (f'' s') ds)
495+ j' s (PositionedDoNotationElement _ _ e1) = j'' s e1
497+ getDeclIdent :: Declaration -> Maybe Ident
498+ getDeclIdent (PositionedDeclaration _ _ d) = getDeclIdent d
499+ getDeclIdent (ValueDeclaration ident _ _ _) = Just ident
500+ getDeclIdent (TypeDeclaration ident _) = Just ident
501+ getDeclIdent _ = Nothing
392accumTypes :: (Monoid r) => (Type -> r) -> (Declaration -> r, Expr -> r, Binder -> r, CaseAlternative -> r, DoNotationElement -> r)
503accumTypes :: (Monoid r) => (Type -> r) -> (Declaration -> r, Expr -> r, Binder -> r, CaseAlternative -> r, DoNotationElement -> r)
393accumTypes f = everythingOnValues mappend forDecls forValues (const mempty) (const mempty) (const mempty)
504accumTypes f = everythingOnValues mappend forDecls forValues (const mempty) (const mempty) (const mempty)