5 import System.Environment
6 import System.Console.GetOpt
10 -- search for definitions of things
11 -- we do this by looking for the following patterns:
12 -- data XXX = ... giving a datatype location
13 -- newtype XXX = ... giving a newtype location
14 -- bla :: ... giving a function location
16 -- by doing it this way, we avoid picking up local definitions
17 -- (whether this is good or not is a matter for debate)
20 -- We generate both CTAGS and ETAGS format tags files
21 -- The former is for use in most sensible editors, while EMACS uses ETAGS
24 -- TODO add tag categories
25 -- alternatives: http://haskell.org/haskellwiki/Tags
29 progName <- getProgName
31 let usageString = "Usage: " ++ progName ++ " [OPTION...] [files...]"
32 let (modes, filenames, errs) = getOpt Permute options args
33 if errs /= [] || elem Help modes || filenames == []
36 putStr $ usageInfo usageString options
37 exitWith (ExitFailure 1)
39 let mode = getMode (Append `delete` modes)
40 let openFileMode = if elem Append modes
43 filedata <- mapM findthings filenames
44 if mode == BothTags || mode == CTags
46 ctagsfile <- openFile "tags" openFileMode
47 writectagsfile ctagsfile filedata
50 if mode == BothTags || mode == ETags
52 etagsfile <- openFile "TAGS" openFileMode
53 writeetagsfile etagsfile filedata
57 -- | getMode takes a list of modes and extract the mode with the
58 -- highest precedence. These are as follows: Both, CTags, ETags
59 -- The default case is Both.
60 getMode :: [Mode] -> Mode
63 getMode (x:xs) = max x (getMode xs)
66 data Mode = ETags | CTags | BothTags | Append | Help deriving (Ord, Eq, Show)
68 options :: [OptDescr Mode]
69 options = [ Option "c" ["ctags"]
70 (NoArg CTags) "generate CTAGS file (ctags)"
71 , Option "e" ["etags"]
72 (NoArg ETags) "generate ETAGS file (etags)"
74 (NoArg BothTags) ("generate both CTAGS and ETAGS")
75 , Option "a" ["append"]
76 (NoArg Append) ("append to existing CTAGS and/or ETAGS file(s)")
77 , Option "h" ["help"] (NoArg Help) "This help"
80 type FileName = String
82 type ThingName = String
84 -- The position of a token or definition
89 String -- string that makes up that line
92 -- A definition we have found
93 data FoundThing = FoundThing ThingName Pos
96 -- Data we have obtained from a file
97 data FileData = FileData FileName [FoundThing]
99 data Token = Token String Pos
103 -- stuff for dealing with ctags output format
105 writectagsfile :: Handle -> [FileData] -> IO ()
106 writectagsfile ctagsfile filedata = do
107 let things = concat $ map getfoundthings filedata
108 mapM_ (\x -> hPutStrLn ctagsfile $ dumpthing x) things
110 getfoundthings :: FileData -> [FoundThing]
111 getfoundthings (FileData filename things) = things
113 dumpthing :: FoundThing -> String
114 dumpthing (FoundThing name (Pos filename line _ _)) =
115 name ++ "\t" ++ filename ++ "\t" ++ (show $ line + 1)
118 -- stuff for dealing with etags output format
120 writeetagsfile :: Handle -> [FileData] -> IO ()
121 writeetagsfile etagsfile filedata = do
122 mapM_ (\x -> hPutStr etagsfile $ e_dumpfiledata x) filedata
124 e_dumpfiledata :: FileData -> String
125 e_dumpfiledata (FileData filename things) =
126 "\x0c\n" ++ filename ++ "," ++ (show thingslength) ++ "\n" ++ thingsdump
128 thingsdump = concat $ map e_dumpthing things
129 thingslength = length thingsdump
131 e_dumpthing :: FoundThing -> String
132 e_dumpthing (FoundThing name (Pos filename line token fullline)) =
133 (concat $ take (token + 1) $ spacedwords fullline)
134 ++ "\x7f" ++ (show line) ++ "," ++ (show $ line+1) ++ "\n"
137 -- like "words", but keeping the whitespace, and so letting us build
140 spacedwords :: String -> [String]
142 spacedwords xs = (blanks ++ wordchars):(spacedwords rest2)
144 (blanks,rest) = span Char.isSpace xs
145 (wordchars,rest2) = span (\x -> not $ Char.isSpace x) rest
148 -- Find the definitions in a file
150 findthings :: FileName -> IO FileData
151 findthings filename = do
152 text <- readFile filename
153 evaluate text -- forces evaluation of text
154 -- too many files were being opened otherwise since
156 let aslines = lines text
157 let wordlines = map mywords aslines
158 let noslcoms = map stripslcomments wordlines
159 let tokens = concat $ zipWith3 (withline filename) noslcoms aslines [0 ..]
160 let nocoms = stripblockcomments tokens
161 return $ FileData filename $ findstuff nocoms
162 where evaluate [] = return ()
163 evaluate (c:cs) = c `seq` evaluate cs
164 -- my words is mainly copied from Data.List.
165 -- difference abc::def is split into three words instead of one.
166 -- We should really be lexing Haskell properly here rather
167 -- than using hacks like this. In the future we expect hasktags
168 -- to be replaced by something using the GHC API.
169 mywords :: String -> [String]
170 mywords (':':':':xs) = "::" : mywords xs
171 mywords s = case dropWhile isSpace s of
173 s' -> w : mywords s''
174 where (w, s'') = myBreak s'
176 myBreak (':':':':xs) = ([], "::"++xs)
177 myBreak (' ':xs) = ([],xs);
178 myBreak (x:xs) = let (a,b) = myBreak xs
180 -- Create tokens from words, by recording their line number
181 -- and which token they are through that line
183 withline :: FileName -> [String] -> String -> Int -> [Token]
184 withline filename words fullline i =
185 zipWith (\w t -> Token w (Pos filename i t fullline)) words $ [0 ..]
187 -- comments stripping
189 stripslcomments :: [String] -> [String]
190 stripslcomments ("--":xs) = []
191 stripslcomments (x:xs) = x : stripslcomments xs
192 stripslcomments [] = []
194 stripblockcomments :: [Token] -> [Token]
195 stripblockcomments ((Token "\\end{code}" _):xs) = afterlitend xs
196 stripblockcomments ((Token "{-" _):xs) = afterblockcomend xs
197 stripblockcomments (x:xs) = x:stripblockcomments xs
198 stripblockcomments [] = []
200 afterlitend2 :: [Token] -> [Token]
201 afterlitend2 (x:xs) = afterlitend xs
204 afterlitend :: [Token] -> [Token]
205 afterlitend ((Token "\\begin{code}" _):xs) = xs
206 afterlitend (x:xs) = afterlitend xs
209 afterblockcomend :: [Token] -> [Token]
210 afterblockcomend ((Token token _):xs) | contains "-}" token = xs
211 | otherwise = afterblockcomend xs
212 afterblockcomend [] = []
215 -- does one string contain another string
217 contains :: Eq a => [a] -> [a] -> Bool
218 contains sub full = any (isPrefixOf sub) $ tails full
221 ints i = i:(ints $ i+1)
224 -- actually pick up definitions
226 findstuff :: [Token] -> [FoundThing]
227 findstuff ((Token "data" _):(Token name pos):xs) =
228 FoundThing name pos : (getcons xs) ++ (findstuff xs)
229 findstuff ((Token "newtype" _):(Token name pos):xs) =
230 FoundThing name pos : findstuff xs
231 findstuff ((Token "type" _):(Token name pos):xs) =
232 FoundThing name pos : findstuff xs
233 findstuff ((Token name pos):(Token "::" _):xs) =
234 FoundThing name pos : findstuff xs
235 findstuff (x:xs) = findstuff xs
239 -- get the constructor definitions, knowing that a datatype has just started
241 getcons :: [Token] -> [FoundThing]
242 getcons ((Token "=" _):(Token name pos):xs) =
243 FoundThing name pos : getcons2 xs
244 getcons (x:xs) = getcons xs
248 getcons2 ((Token "=" _):xs) = []
249 getcons2 ((Token "|" _):(Token name pos):xs) =
250 FoundThing name pos : getcons2 xs
251 getcons2 (x:xs) = getcons2 xs