From a1aed9cc40bd0a4752516b45fc65beded847c0f5 Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Sun, 8 Jul 2007 12:12:08 +0200 Subject: the output template is now parsed with the help of a finite map This way we can quickly implement program name aliasing. darcs-hash:20070708101208-d6583-fa4f7f490020289e6f6b6fce0886223ec7924fa6.gz --- Parsers.hs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/Parsers.hs b/Parsers.hs index 6feea97..fa85193 100644 --- a/Parsers.hs +++ b/Parsers.hs @@ -26,7 +26,9 @@ module Parsers ( ) where import Config +import Commands import Text.ParserCombinators.Parsec +import qualified Data.Map as Map {- $parser @@ -81,9 +83,9 @@ colorSpec = templateStringParser :: Config -> Parser (String,String,String) templateStringParser c = do{ s <- many $ noneOf (sepChar c) - ; (_,com,_) <- templateCommandParser c + ; (com,_,_) <- templateCommandParser c ; ss <- many $ noneOf (sepChar c) - ; return (s, com, ss) + ; return (com, s, ss) } -- | Parses the command part of the template string @@ -93,16 +95,28 @@ templateCommandParser c = ; char chr ; com <- many $ noneOf (sepChar c) ; char chr - ; return $ ("",com,"") - } + ; return $ (com,"","") + } + -- | Combines the template parsers templateParser :: Config -> Parser [(String,String,String)] templateParser c = many (templateStringParser c) -- | Actually runs the template parsers -parseTemplate :: Config -> String -> IO [(String,String,String)] +parseTemplate :: Config -> String -> IO [(Command,String,String)] parseTemplate config s = - case (parse (templateParser config) "" s) of - Left _ -> return [("","","")] - Right x -> return x + do str <- case (parse (templateParser config) "" s) of + Left _ -> return [("","","")] + Right x -> return x + let comList = map (show . fst) $ commands config + m = Map.fromList . zip comList . map fst $ (commands config) + return $ combine m str +-- | Given a finite "Map" and a parsed templatet produces the +-- | resulting output string. +combine :: Map.Map String Command -> [(String, String, String)] -> [(Command,String,String)] +combine _ [] = [] +combine m ((ts,s,ss):xs) = + [(com, s, ss)] ++ combine m xs + where com = Map.findWithDefault dflt ts m + dflt = Exec ts [] [] --"<" ++ ts ++ " not found!>" -- cgit v1.2.3