@@ -14,49 +14,71 @@
1414-----------------------------------------------------------------------------
1515
1616module Language.PureScript.Scope (
17− usedNames,
17+ usedNamesDecl,
18+ usedNamesValue,
19+ usedNamesBinder,
20+ usedNamesCaseAlternative,
21+ usedNamesDoNotationElement,
1822 unusedNames
1923) where
2024
21−import Control.Applicative ((<$>))
22−
23−import Data.Data
2425import Data.List ((\\), nub)
25−import Data.Generics (extQ, mkQ, everything)
2626
2727import Language.PureScript.Declarations
2828import Language.PureScript.Names
29−import Language.PureScript.CodeGen.JS.AST
29+
30+usedNames :: (Declaration -> [Ident], Value -> [Ident], Binder -> [Ident], CaseAlternative -> [Ident], DoNotationElement -> [Ident])
31+usedNames = everythingOnValues (++) f g h (const []) (const [])
32+ where
33+ f :: Declaration -> [Ident]
34+ f (ValueDeclaration name _ _ _ _) = [name]
35+ f _ = []
36+
37+ g :: Value -> [Ident]
38+ g (Abs (Left arg) _) = [arg]
39+ g (Var (Qualified Nothing name)) = [name]
40+ g _ = []
41+
42+ h :: Binder -> [Ident]
43+ h (VarBinder name) = [name]
44+ h _ = []
45+
46+-- |
47+-- Gather all used names appearing inside a declaration
48+--
49+usedNamesDecl :: Declaration -> [Ident]
50+usedNamesDecl = let (f, _, _, _, _) = usedNames in nub . f
3051
3152-- |
3253-- Gather all used names appearing inside a value
3354--
34−usedNames :: (Data d) => d -> [Ident]
35−usedNames val = nub $ everything (++) (mkQ [] namesV `extQ` namesB `extQ` namesJS) val
36− where
37− namesV :: Value -> [Ident]
38− namesV (Abs (Left arg) _) = [arg]
39− namesV (Var (Qualified Nothing name)) = [name]
40− namesV _ = []
41− namesB :: Binder -> [Ident]
42− namesB (VarBinder name) = [name]
43− namesB _ = []
44− namesJS :: JS -> [Ident]
45− namesJS (JSVar name) = [Ident name]
46− namesJS (JSFunction (Just name) args _) = Ident name : (Ident <$> args)
47− namesJS (JSFunction Nothing args _) = Ident <$> args
48− namesJS (JSVariableIntroduction name _) = [Ident name]
49− namesJS (JSFor name _ _ _) = [Ident name]
50− namesJS (JSForIn name _ _) = [Ident name]
51− namesJS _ = []
55+usedNamesValue :: Value -> [Ident]
56+usedNamesValue = let (_, f, _, _, _) = usedNames in nub . f
57+
58+-- |
59+-- Gather all used names appearing inside a binder
60+--
61+usedNamesBinder :: Binder -> [Ident]
62+usedNamesBinder = let (_, _, f, _, _) = usedNames in nub . f
63+
64+-- |
65+-- Gather all used names appearing inside a case alternative
66+--
67+usedNamesCaseAlternative :: CaseAlternative -> [Ident]
68+usedNamesCaseAlternative = let (_, _, _, f, _) = usedNames in nub . f
69+
70+-- |
71+-- Gather all used names appearing inside a do notation element
72+--
73+usedNamesDoNotationElement :: DoNotationElement -> [Ident]
74+usedNamesDoNotationElement = let (_, _, _, _, f) = usedNames in nub . f
5275
5376-- |
5477-- Generate a set of names which are unused inside a value, of the form @_{n}@ for an integer @n@
5578--
56−unusedNames :: (Data d) => d -> [Ident]
57−unusedNames val =
79+unusedNames :: [Ident] -> [Ident]
80+unusedNames allNames =
5881 let
59− allNames = usedNames val
6082 varNames = map (Ident . ('_' :) . show) ([1..] :: [Int])
6183 in
6284 varNames \\ allNames