diff options
author | Andrea Rossato <andrea.rossato@ing.unitn.it> | 2007-06-26 13:39:48 +0200 |
---|---|---|
committer | Andrea Rossato <andrea.rossato@ing.unitn.it> | 2007-06-26 13:39:48 +0200 |
commit | a8ffb9f53aac66b31d4ef870ed88b7c0e6e5ca7e (patch) | |
tree | 358cae8830782791a31437eeb4dfad8286e52d83 /Parsers.hs | |
parent | 45ae401703267ec7618ab1315ce5b38af1756c33 (diff) | |
download | xmobar-a8ffb9f53aac66b31d4ef870ed88b7c0e6e5ca7e.tar.gz xmobar-a8ffb9f53aac66b31d4ef870ed88b7c0e6e5ca7e.tar.bz2 |
splitted files
darcs-hash:20070626113948-d6583-73d318293d1cd91894589450e5cd270dd39bdc02.gz
Diffstat (limited to 'Parsers.hs')
-rw-r--r-- | Parsers.hs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/Parsers.hs b/Parsers.hs new file mode 100644 index 0000000..6ca318c --- /dev/null +++ b/Parsers.hs @@ -0,0 +1,99 @@ +----------------------------------------------------------------------------- +-- | +-- Module : XMobar.Parsers +-- Copyright : (c) Andrea Rossato +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Andrea Rossato <andrea.rossato@unibz.it> +-- Stability : unstable +-- Portability : unportable +-- +-- Parsers needed for XMobar, a status bar for the Xmonad Window Manager +-- +----------------------------------------------------------------------------- + +module Parsers ( + -- * Parsing + -- $parser + parseString + , stringParser + , defaultColors + , colorsAndText + , templateStringParser + , templateCommandParser + , templateParser + , parseTemplate + ) where + +import Config +import Text.ParserCombinators.Parsec + + +{- $parser +These are the neede parsers. Don't trust them too much. + +There are parsers for the commands output and parsers for the +formatting template. + -} + +-- | Runs the actual string parsers +parseString :: Config -> String -> IO [(String, String)] +parseString config s = + case (parse (stringParser config) "" s) of + Left _ -> return [("Could not parse string: " ++ s + , (fgColor config))] + Right x -> return x + +-- | Gets the string and combines the needed parsers +stringParser :: Config -> Parser [(String, String)] +stringParser c = manyTill (colorsAndText c <|> defaultColors c) eof + +-- | Parses a string with the default color (no color set) +defaultColors :: Config -> Parser (String, String) +defaultColors config = + do { s <- many $ noneOf "<" + ; return (s,(fgColor config)) + } + <|> colorsAndText config + +-- | Parses a string with a color set +colorsAndText :: Config -> Parser (String, String) +colorsAndText config = + do { string "<fc=#" + ; n <- count 6 hexDigit + ; string ">" + ; s <- many $ noneOf "<" + ; string "</fc>" + ; return (s,"#"++n) + } + <|> defaultColors config + +-- | Parses the output template string +templateStringParser :: Config -> Parser (String,String,String) +templateStringParser c = + do{ s <- many $ noneOf (sepChar c) + ; (_,com,_) <- templateCommandParser c + ; ss <- many $ noneOf (sepChar c) + ; return (s, com, ss) + } + +-- | Parses the command part of the template string +templateCommandParser :: Config -> Parser (String,String,String) +templateCommandParser c = + do { let chr = head $ sepChar c + ; char chr + ; com <- many $ noneOf (sepChar c) + ; char chr + ; 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 s = + case (parse (templateParser config) "" s) of + Left _ -> return [("","","")] + Right x -> return x + |