Move path stuff into a Utils module, and show basenames in metadata

This commit is contained in:
Adam Sampson 2007-04-24 12:52:13 +00:00
parent 129ded1cdb
commit ccda7402e2
4 changed files with 36 additions and 12 deletions

View File

@ -18,7 +18,8 @@ sources = \
SimplifyProcs.hs \
TLP.hs \
Types.hs \
Unnest.hs
Unnest.hs \
Utils.hs
$(targets): $(sources)
ghc -fglasgow-exts -o fco --make Main

View File

@ -3,6 +3,8 @@ module Metadata where
import Data.Generics
import Utils
data Meta = Meta {
metaFile :: Maybe String,
metaLine :: Int,
@ -20,5 +22,5 @@ emptyMeta = Meta {
instance Show Meta where
show m =
case metaFile m of
Just s -> s ++ ":" ++ show (metaLine m) ++ ":" ++ show (metaColumn m)
Just s -> basenamePath s ++ ":" ++ show (metaLine m) ++ ":" ++ show (metaColumn m)
Nothing -> "no source position"

View File

@ -19,6 +19,7 @@ import Metadata
import ParseState
import Pass
import Types
import Utils
--{{{ setup stuff for Parsec
type OccParser = GenParser Char ParseState
@ -1416,16 +1417,6 @@ mangleModName mod
then mod
else mod ++ ".occ"
-- | Join a relative path to an existing path (i.e. if you're given foo/bar and
-- baz, return foo/baz).
joinPath :: String -> String -> String
joinPath base new
= case matchRegex pathRE base of
Just [dir] -> dir ++ new
Nothing -> new
where
pathRE = mkRegex "^(.*/)[^/]*$"
type LoaderM a = StateT ParseState IO a
-- | Load all the source files necessary for a program.

30
fco2/Utils.hs Normal file
View File

@ -0,0 +1,30 @@
-- | Utility functions that aren't inherently related to FCO -- i.e. things
-- that could be put into the standard library.
module Utils where
import Text.Regex
-- | Split the directory and file components of a path.
splitPath :: String -> (String, String)
splitPath path
= case matchRegex dirRE path of
Just [dir, base] -> (if dir == "" then "." else dir, base)
where
dirRE = mkRegex "^(.*/)?([^/]*)$"
-- | Return the directory containing a path.
dirnamePath :: String -> String
dirnamePath = fst . splitPath
-- | Return a path without any leading directory components.
basenamePath :: String -> String
basenamePath = snd . splitPath
-- | Join a relative path to an existing path (i.e. if you're given foo/bar and
-- baz, return foo/baz).
joinPath :: String -> String -> String
joinPath base new
= case dirnamePath base of
"." -> new
dir -> dir ++ new