From 2744e4fb443cbfe1f3eeeb781079382071ca3cda Mon Sep 17 00:00:00 2001 From: rje Date: Wed, 29 Aug 2001 16:02:30 +0000 Subject: [PATCH] [project @ 2001-08-29 16:02:30 by rje] Another Haskell equivalent of CTags. Written because fptags and hstags don't seem to be maintained any more and its seemed to be less effort to write a new version, than to figure out how those one's worked. --- ghc/utils/hasktags/HaskTags.hs | 86 ++++++++++++++++++++++++++++++++++++++++ ghc/utils/hasktags/Makefile | 12 ++++++ ghc/utils/hasktags/README | 19 +++++++++ 3 files changed, 117 insertions(+) create mode 100644 ghc/utils/hasktags/HaskTags.hs create mode 100644 ghc/utils/hasktags/Makefile create mode 100644 ghc/utils/hasktags/README diff --git a/ghc/utils/hasktags/HaskTags.hs b/ghc/utils/hasktags/HaskTags.hs new file mode 100644 index 0000000..27f32e6 --- /dev/null +++ b/ghc/utils/hasktags/HaskTags.hs @@ -0,0 +1,86 @@ +module Main where +import System +import Char +import List + + +-- search for definitions of things +-- we do this by looking for the following patterns: +-- data XXX = ... giving a datatype location +-- newtype XXX = ... giving a newtype location +-- bla :: ... giving a function location +-- +-- by doing it this way, we avoid picking up local definitions + + +main :: IO () +main = do + filenames <- getArgs + foundthings <- mapM findthings filenames + mapM_ (\x -> putStrLn $ dumpthing x) (concat foundthings) + +type FileName = String + +type ThingName = String + +data Pos = Pos FileName Int + deriving Show + +data FoundThing = FoundThing ThingName Pos + deriving Show + +dumpthing :: FoundThing -> String +dumpthing (FoundThing name (Pos filename line)) = + name ++ "\t" ++ filename ++ "\t" ++ (show line) + +data Token = Token String Pos + deriving Show + +findthings :: FileName -> IO [FoundThing] +findthings filename = + do + text <- readFile filename + let aslines = lines text + let wordlines = map words aslines + let nocoms = map stripslcomments wordlines + let tokens = concat $ zipWith (withline filename) nocoms $ ints 0 + return $ findstuff tokens + +withline :: FileName -> [String] -> Int -> [Token] +withline fname words i = map (\w -> Token w (Pos fname i)) words + +stripslcomments :: [String] -> [String] +stripslcomments ("--":xs) = [] +stripslcomments (x:xs) = x : stripslcomments xs +stripslcomments [] = [] + +ints :: Int -> [Int] +ints i = i:(ints $ i+1) + +findstuff :: [Token] -> [FoundThing] +findstuff ((Token "data" _):(Token name pos):xs) = + FoundThing name pos : (getcons xs) ++ (findstuff xs) +findstuff ((Token "type" _):(Token name pos):xs) = + FoundThing name pos : findstuff xs +findstuff ((Token name pos):(Token "::" _):xs) = + FoundThing name pos : findstuff xs +findstuff (x:xs) = findstuff xs +findstuff [] = [] + + +-- get the constructor definitions, knowing that a datatype has just started + +getcons :: [Token] -> [FoundThing] +getcons ((Token "=" _):(Token name pos):xs) = + FoundThing name pos : getcons2 xs +getcons (x:xs) = getcons xs +getcons [] = [] + + +getcons2 ((Token "=" _):xs) = [] +getcons2 ((Token "|" _):(Token name pos):xs) = + FoundThing name pos : getcons2 xs +getcons2 (x:xs) = getcons2 xs +getcons2 [] = [] + + diff --git a/ghc/utils/hasktags/Makefile b/ghc/utils/hasktags/Makefile new file mode 100644 index 0000000..ad77f41 --- /dev/null +++ b/ghc/utils/hasktags/Makefile @@ -0,0 +1,12 @@ + +TOP=../.. + +include $(TOP)/mk/boilerplate.mk + +CURRENT_DIR=ghc/utils/hasktags + +HS_PROG = hasktags + +INSTALL_PROGS += $(HS_PROG) + +include $(TOP)/mk/target.mk diff --git a/ghc/utils/hasktags/README b/ghc/utils/hasktags/README new file mode 100644 index 0000000..ea806b3 --- /dev/null +++ b/ghc/utils/hasktags/README @@ -0,0 +1,19 @@ + +"hasktags" is a very simple Haskell program that produces ctags TAGS files for Haskell programs. + +As such, it does essentially the same job that hstags and fptags used to do, but, both of those seem to no longer be maintained, and it seemed to be easier to write my own version rather than to get one of them to work. + +Example usage: + +find /homes/rje33/src/fptools/ghc/ -name \*.\*hs | xargs hasktags > TAGS + + +Features + * Includes top level functions, provided a type signature is given + * Includes data declarations, and constructors + * Includes newtypes + + - But sometimes gets things wrong or misses things out + It's only a simple program + + -- 1.7.10.4